From 573cd469abcdcffe16300c6a6d975e61ab94a0a7 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 6 Aug 2013 02:09:15 +0000 Subject: [PATCH] Allow do_listen to return an error. --- ncat/ncat_listen.c | 6 ++++++ ncat/ncat_proxy.c | 2 ++ ncat/util.c | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ncat/ncat_listen.c b/ncat/ncat_listen.c index 1ece9cbf4..294da1b80 100644 --- a/ncat/ncat_listen.c +++ b/ncat/ncat_listen.c @@ -271,6 +271,8 @@ static int ncat_listen_stream(int proto) for (i = 0; i < num_listenaddrs; i++) { /* setup the main listening socket */ listen_socket[i] = do_listen(SOCK_STREAM, proto, &listenaddrs[i]); + if (listen_socket[i] == -1) + bye("do_listen: %s", socket_strerror(socket_errno())); /* Make our listening socket non-blocking because there are timing issues * which could cause us to block on accept() even though select() says it's @@ -659,6 +661,8 @@ static int ncat_listen_dgram(int proto) for (i = 0; i < num_listenaddrs; i++) { /* create the UDP listen sockets */ sockfd[i] = do_listen(SOCK_DGRAM, proto, &listenaddrs[i]); + if (sockfd[i] == -1) + bye("do_listen: %s", socket_strerror(socket_errno())); FD_SET(sockfd[i], &listen_fds); add_fd(&listen_fdlist, sockfd[i]); } @@ -676,6 +680,8 @@ static int ncat_listen_dgram(int proto) /* Rebuild the udp socket which got burnt */ sockfd[fdn] = do_listen(SOCK_DGRAM, proto, &listenaddrs[fdn]); + if (sockfd[i] == -1) + bye("do_listen: %s", socket_strerror(socket_errno())); FD_SET(sockfd[fdn], &listen_fds); add_fd(&listen_fdlist, sockfd[fdn]); diff --git a/ncat/ncat_proxy.c b/ncat/ncat_proxy.c index 818f4082b..23ab8e211 100644 --- a/ncat/ncat_proxy.c +++ b/ncat/ncat_proxy.c @@ -218,6 +218,8 @@ int ncat_http_server(void) /* Listen on each address, set up lists for select */ for (i = 0; i < num_listenaddrs; i++) { listen_socket[i] = do_listen(SOCK_STREAM, IPPROTO_TCP, &listenaddrs[i]); + if (listen_socket[i] == -1) + bye("do_listen: %s", socket_strerror(socket_errno())); /* make us not block on accepts in wierd cases. See ncat_listen.c:209 */ unblock_socket(listen_socket[i]); diff --git a/ncat/util.c b/ncat/util.c index 4b97ca48c..b8317d22b 100644 --- a/ncat/util.c +++ b/ncat/util.c @@ -387,6 +387,8 @@ unsigned short inet_port(const union sockaddr_u *su) return 0; } +/* Return a listening socket after setting various characteristics on it. + Returns -1 on error. */ int do_listen(int type, int proto, const union sockaddr_u *srcaddr_u) { int sock = 0, option_on = 1; @@ -400,7 +402,7 @@ int do_listen(int type, int proto, const union sockaddr_u *srcaddr_u) nbase. */ sock = inheritable_socket(srcaddr_u->storage.ss_family, type, proto); if (sock < 0) - bye("socket: %s", socket_strerror(socket_errno())); + return -1; Setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option_on, sizeof(int));