diff options
Diffstat (limited to 'FICS/network.c')
-rw-r--r-- | FICS/network.c | 75 |
1 files changed, 48 insertions, 27 deletions
diff --git a/FICS/network.c b/FICS/network.c index 1b8f822..1e97e20 100644 --- a/FICS/network.c +++ b/FICS/network.c @@ -10,6 +10,7 @@ #include <arpa/telnet.h> #include <netinet/in.h> +#include <err.h> #include <errno.h> #include "common.h" @@ -266,7 +267,9 @@ net_send_string(int fd, char *str, int format) if ((which = findConnection(fd)) < 0) return -1; while (*str) { - for (i = 0; str[i] >= ' '; i++) { + const int upbound = (int)strlen(str); + + for (i = 0; i < upbound && str[i] >= ' '; i++) { /* null */; } @@ -311,6 +314,7 @@ net_send_string(int fd, char *str, int format) break; case '\033': con[which].outPos -= 3; + // XXX: fallthrough here? default: sendme(which, str, 1); } @@ -331,12 +335,11 @@ net_send_string(int fd, char *str, int format) PUBLIC int readline2(comstr_t *cs, int who) { - int howmany, state, fd, pending; - unsigned char *start, *s, *d; - - static unsigned char ayt[] = "[Responding to AYT: Yes, I'm here.]\n"; - static unsigned char will_sga[] = { IAC, WILL, TELOPT_SGA, '\0' }; - static unsigned char will_tm[] = { IAC, WILL, TELOPT_TM, '\0' }; + int howmany, state, fd, v_pending; + static const uint8_t ayt[] = "[Responding to AYT: Yes, I'm here.]\n"; + static const uint8_t will_sga[] = { IAC, WILL, TELOPT_SGA, '\0' }; + static const uint8_t will_tm[] = { IAC, WILL, TELOPT_TM, '\0' }; + unsigned char *start, *s, *d; state = con[who].state; @@ -347,11 +350,11 @@ readline2(comstr_t *cs, int who) } s = start = con[who].inBuf; - pending = con[who].numPending; + v_pending = con[who].numPending; fd = con[who].fd; - if ((howmany = recv(fd, start + pending, MAX_STRING_LENGTH - 1 - - pending, 0)) == 0) { // error: they've disconnected + if ((howmany = recv(fd, start + v_pending, MAX_STRING_LENGTH - 1 - + v_pending, 0)) == 0) { // error: they've disconnected return -1; } else if (howmany == -1) { if (errno != EWOULDBLOCK) { // some other error @@ -365,9 +368,9 @@ readline2(comstr_t *cs, int who) } if (con[who].processed) - s += pending; + s += v_pending; else - howmany += pending; + howmany += v_pending; d = s; for (; howmany-- > 0; s++) { @@ -402,7 +405,7 @@ readline2(comstr_t *cs, int who) state = 3; // this is cheesy // but we aren't using em } else if (*s == AYT) { - send(fd, (char *)ayt, strlen((char *)ayt), 0); + send(fd, (char *)ayt, sizeof ayt - 1, 0); state = 2; } else if (*s == EL) { // erase line d = start; @@ -435,11 +438,13 @@ readline2(comstr_t *cs, int who) break; case 4: // got IAC DO if (*s == TELOPT_TM) { - send(fd, (char *)will_tm, - strlen((char *)will_tm), 0); + if (send(fd, (char *)will_tm, + sizeof will_tm - 1, 0) == -1) + warn("%s: cannot send", __func__); } else if (*s == TELOPT_SGA) { - send(fd, (char *)will_sga, - strlen((char *)will_sga), 0); + if (send(fd, (char *)will_sga, + sizeof will_sga - 1, 0) == -1) + warn("%s: cannot send", __func__); } state = 2; break; @@ -467,9 +472,9 @@ readline2(comstr_t *cs, int who) } PUBLIC int -net_init(int port) +net_init(int p_port) { - int opt; + int opt, ret; struct linger lingeropt; struct sockaddr_in serv_addr; @@ -501,22 +506,30 @@ net_init(int port) memset(&serv_addr, 0, sizeof serv_addr); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); - serv_addr.sin_port = htons(port); + serv_addr.sin_port = htons(p_port); /* * Attempt to allow rebinding to the port... */ opt = 1; - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof opt); + ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, + sizeof opt); + if (ret == -1) + warn("%s: SO_REUSEADDR", __func__); opt = 1; - setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof opt); + ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, + sizeof opt); + if (ret == -1) + warn("%s: SO_KEEPALIVE", __func__); lingeropt.l_onoff = 0; lingeropt.l_linger = 0; - setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char *) &lingeropt, + ret = setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char *) &lingeropt, sizeof(lingeropt)); + if (ret == -1) + warn("%s: SO_LINGER", __func__); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof serv_addr) < 0) { fprintf(stderr, "FICS: can't bind local address. errno=%d\n", @@ -553,15 +566,23 @@ net_close_connection(int fd) PUBLIC void turn_echo_on(int fd) { - static unsigned char wont_echo[] = { IAC, WONT, TELOPT_ECHO, '\0' }; - send(fd, (char *) wont_echo, strlen((char *) wont_echo), 0); + int ret; + static unsigned char wont_echo[] = {IAC, WONT, TELOPT_ECHO, '\0'}; + + ret = send(fd, (char *)wont_echo, sizeof wont_echo - 1, 0); + if (ret == -1) + warn("%s: cannot send", __func__); } PUBLIC void turn_echo_off(int fd) { - static unsigned char will_echo[] = { IAC, WILL, TELOPT_ECHO, '\0' }; - send(fd, (char *) will_echo, strlen((char *) will_echo), 0); + int ret; + static unsigned char will_echo[] = {IAC, WILL, TELOPT_ECHO, '\0'}; + + ret = send(fd, (char *)will_echo, sizeof will_echo - 1, 0); + if (ret == -1) + warn("%s: cannot send", __func__); } PUBLIC unsigned int |