Skip to content

Commit

Permalink
Fix listener, beef up logging a little. (#166)
Browse files Browse the repository at this point in the history
* Beef up the network logging a smidge.

* Don't let a failed listen attempt prevent others.
  • Loading branch information
fovea1959 authored Jan 29, 2025
1 parent 8039113 commit b6819dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
39 changes: 35 additions & 4 deletions net_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,32 @@ 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
*/
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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand All @@ -454,6 +481,10 @@ int netInitiateConnection(struct sockaddr *sap)
}
#endif

#if DEBUG >= 2
fprintf(stderr, "(net_util) connect succeeded\n");
#endif

return sd;
}

Expand Down
29 changes: 20 additions & 9 deletions npu_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#include <netdb.h>
#endif

#define DEBUG 0

/*
** -----------------
** Private Constants
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1279,13 +1286,25 @@ 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.
*/
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:
Expand Down Expand Up @@ -1329,14 +1348,6 @@ static void *npuNetThread(void *param)
maxFd = ncbp->lstnFd;
}
}
else
{
#if defined(_WIN32)
return;
#else
return NULL;
#endif
}
break;

case ConnTypeRevHasp:
Expand Down

0 comments on commit b6819dc

Please sign in to comment.