From b6819dce2365126fe14e6249c80bb06906e21e34 Mon Sep 17 00:00:00 2001 From: Doug Wegscheid <5349196+fovea1959@users.noreply.github.com> Date: Wed, 29 Jan 2025 17:55:35 -0500 Subject: [PATCH] Fix listener, beef up logging a little. (#166) * Beef up the network logging a smidge. * Don't let a failed listen attempt prevent others. --- net_util.c | 39 +++++++++++++++++++++++++++++++++++---- npu_net.c | 29 ++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/net_util.c b/net_util.c index be96e1b..a593595 100644 --- a/net_util.c +++ b/net_util.c @@ -177,13 +177,23 @@ int netCreateListener(int port) int sd; #endif +#if DEBUG + char debug_buffer[80]; +#endif + sd = netCreateSocket(port, TRUE); #if defined(_WIN32) - if (sd == INVALID_SOCKET) return sd; + if (sd == INVALID_SOCKET) #else - if (sd == -1) return sd; + if (sd == -1) #endif + { +#if DEBUG + fprintf(stderr, "(net_util) netCreateSocket could not create socket\n"); +#endif + return sd; + } /* ** Start listening for new connections on this TCP port number @@ -191,7 +201,8 @@ int netCreateListener(int port) if (listen(sd, MaxListenBacklog) == -1) { #if DEBUG - perror("(net_util) listen"); + sprintf(debug_buffer, "(net_util) netCreateSocket listen on port %d failed", port); + perror(debug_buffer); #endif #if defined(_WIN32) closesocket(sd); @@ -202,6 +213,10 @@ int netCreateListener(int port) #endif } +#if DEBUG >= 2 + fprintf(stderr, "(net_util) netCreateSocket listen on port %d succeeded\n", port); +#endif + return sd; } @@ -237,6 +252,10 @@ int netCreateSocket(int port, bool isReuse) int sd; #endif +#if DEBUG + char debug_buffer[80]; +#endif + sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); #if defined(_WIN32) if (sd == INVALID_SOCKET) return sd; @@ -256,7 +275,8 @@ int netCreateSocket(int port, bool isReuse) if (bind(sd, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) == -1) { #if DEBUG - perror("(net_util) bind"); + sprintf(debug_buffer, "(net_util) netCreateSocket bind to %s:%d", ipAddress, port); + perror(debug_buffer); #endif #if defined(_WIN32) closesocket(sd); @@ -275,6 +295,9 @@ int netCreateSocket(int port, bool isReuse) fcntl(sd, F_SETFL, O_NONBLOCK); #endif +#if DEBUG >= 2 + fprintf(stderr, "(net_util) netCreateSocket to %s:%d successful\n", ipAddress, port); +#endif return sd; } @@ -428,6 +451,10 @@ int netInitiateConnection(struct sockaddr *sap) int sd; #endif +#if DEBUG + char debug_buffer[80]; +#endif + sd = netCreateSocket(0, FALSE); #if defined(_WIN32) @@ -454,6 +481,10 @@ int netInitiateConnection(struct sockaddr *sap) } #endif +#if DEBUG >= 2 + fprintf(stderr, "(net_util) connect succeeded\n"); +#endif + return sd; } diff --git a/npu_net.c b/npu_net.c index 1aa73fc..f68922c 100644 --- a/npu_net.c +++ b/npu_net.c @@ -61,6 +61,8 @@ #include #endif +#define DEBUG 0 + /* ** ----------------- ** Private Constants @@ -1194,11 +1196,16 @@ static bool npuNetCreateListeningSocket(Ncb *ncbp) if (sd == -1) #endif { - fprintf(stderr, "(npu_net) Can't create socket for port %d\n", ncbs->tcpPort); + fprintf(stderr, "(npu_net) Can't create listener for port %d\n", ncbp->tcpPort); return FALSE; } ncbp->lstnFd = sd; +#if DEBUG >= 2 + fprintf(stderr, "(npu_net) created listener for port %d\n", ncbp->tcpPort); +#endif + + return TRUE; } @@ -1279,6 +1286,15 @@ static void *npuNetThread(void *param) FD_ZERO(&listenFds); +#if DEBUG >= 2 + fprintf(stderr, "npuNetThread has %d Ncbs to check\n", numNcbs); + for (i = 0; i < numNcbs; i++) + { + ncbp = &ncbs[i]; + fprintf(stderr, "npuNetThread .. (%d), type %d, port %d\n", i, ncbp->connType, ncbp->tcpPort); + } +#endif + /* ** Create a listening socket for every configured connection type that listens ** for connections. @@ -1286,6 +1302,9 @@ static void *npuNetThread(void *param) for (i = 0; i < numNcbs; i++) { ncbp = &ncbs[i]; +#if DEBUG >= 2 + fprintf(stderr, "npuNetThread checking Ncb %d, type %d, port %d\n", i, ncbp->connType, ncbp->tcpPort); +#endif switch (ncbp->connType) { case ConnTypeTrunk: @@ -1329,14 +1348,6 @@ static void *npuNetThread(void *param) maxFd = ncbp->lstnFd; } } - else - { -#if defined(_WIN32) - return; -#else - return NULL; -#endif - } break; case ConnTypeRevHasp: