diff --git a/echo.c b/echo.c index 168dec7..963d225 100644 --- a/echo.c +++ b/echo.c @@ -48,7 +48,10 @@ taskmain(int argc, char **argv) fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); taskexitall(1); } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + fprintf(stderr, "fdnoblock\n"); + taskexitall(1); + } while((cfd = netaccept(fd, remote, &rport)) >= 0){ if(verbose) fprintf(stderr, "connection from %s:%d\n", remote, rport); diff --git a/fd.c b/fd.c index 5b4f579..4e0fdcc 100644 --- a/fd.c +++ b/fd.c @@ -220,6 +220,8 @@ fdwait(int fd, int rw) if(r < 0 || errno == EEXIST){ duped = 1; fd = dup(fd); + if(fd < 0) + abort(); r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); if(r != 0) abort(); diff --git a/net.c b/net.c index e635518..f3ec128 100644 --- a/net.c +++ b/net.c @@ -62,7 +62,10 @@ netannounce(int istcp, char *server, int port) sn = sizeof n; if(istcp && getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0){ n = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n); + if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n) < 0){ + close(fd); + return -1; + } } if(bind(fd, (struct sockaddr*)&ss, sizeof ss) < 0){ @@ -74,7 +77,10 @@ netannounce(int istcp, char *server, int port) if(proto == SOCK_STREAM) listen(fd, 16); - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + close(fd); + return -1; + } taskstate("netannounce succeeded"); return fd; } @@ -109,9 +115,15 @@ netaccept(int fd, char *server, int *port) *port = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port); break; } - fdnoblock(cfd); + if(fdnoblock(cfd) < 0){ + close(cfd); + return -1; + } one = 1; - setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one); + if(setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one) < 0){ + close(cfd); + return -1; + } taskstate("netaccept succeeded"); return cfd; } @@ -136,10 +148,12 @@ netlookup(char *name, unsigned char *ip) break; } taskstate("netlookup succeeded"); + freeaddrinfo(result); return 0; } taskstate("netlookup failed"); + freeaddrinfo(result); return -1; } @@ -160,12 +174,18 @@ netdial(int istcp, char *server, int port) taskstate("socket failed"); return -1; } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + close(fd); + return -1; + } /* for udp */ if(!istcp){ n = 1; - setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof n); + if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof n) < 0){ + close(fd); + return -1; + } } /* start connecting */ @@ -198,7 +218,10 @@ netdial(int istcp, char *server, int port) /* report error */ sn = sizeof n; - getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &sn); + if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &sn) < 0){ + close(fd); + return -1; + } if(n == 0) n = ECONNREFUSED; close(fd); diff --git a/task.c b/task.c index 074b6bf..6360d31 100644 --- a/task.c +++ b/task.c @@ -46,7 +46,8 @@ taskdebug(char *fmt, ...) p = argv0; snprint(buf, sizeof buf, "/tmp/%s.tlog", p); if((fd = open(buf, O_CREAT|O_WRONLY, 0666)) < 0) - fd = open("/dev/null", O_WRONLY); + if((fd = open("/dev/null", O_WRONLY)) < 0) + abort(); } va_start(arg, fmt); diff --git a/tcpload.c b/tcpload.c index 9f6dfc8..d507a88 100644 --- a/tcpload.c +++ b/tcpload.c @@ -47,7 +47,6 @@ fetchtask(void *v) } snprintf(buf, sizeof buf, "xxxxxxxxxx"); fdwrite(fd, buf, strlen(buf)); - fdread(fd, buf, sizeof buf); close(fd); } } diff --git a/tcpproxy.c b/tcpproxy.c index 349d6c2..5098ae3 100644 --- a/tcpproxy.c +++ b/tcpproxy.c @@ -51,7 +51,10 @@ taskmain(int argc, char **argv) fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); taskexitall(1); } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + fprintf(stderr, "fdnoblock\n"); + taskexitall(1); + } while((cfd = netaccept(fd, remote, &rport)) >= 0){ if(verbose) fprintf(stderr, "connection from %s:%d\n", remote, rport);