tests/server: round of tidy-ups (part 2)

General tidy-ups, to identify and reduce duplications and potential
issues, while also making the server modules compile as a single binary.

- ensure unique symbols and no shadowing across server sources, by
  renaming variables.
- move globals common to multiple servers into shared `util` module.
- drop constants with a single use.
- undef macro before re-using them across server sources.
- move common functions into shared `util` module.
- drop redundant static declarations.
- disable IPv6 code when built without IPv6.
- start syncing the 3 almost identical copies of `sockdaemon` function.
- drop unused `timeval.h` header.
- drop `poll()` from `wait_ms()`, for macOS, following an earlier core
  update.
  Follow-up to c72cefea0f #15096

Follow-up to 9213e4e497 #16525
Cherry-picked from #15000
Closes #16609
This commit is contained in:
Viktor Szakats 2024-09-22 17:48:17 +02:00
parent 07f99b72d5
commit bc55b435af
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
9 changed files with 369 additions and 430 deletions

View file

@ -114,20 +114,14 @@
/* include memdebug.h last */
#include "memdebug.h"
#define DEFAULT_PORT 8999
/* buffer is this excessively large only to be able to support things like
test 1003 which tests exceedingly large server response lines */
#define BUFFER_SIZE 17010
static bool verbose = FALSE;
static bool bind_only = FALSE;
#ifdef USE_IPV6
static bool use_ipv6 = FALSE;
#endif
static const char *ipv_inuse = "IPv4";
static unsigned short port = DEFAULT_PORT;
static unsigned short connectport = 0; /* if non-zero, we activate this mode */
static bool s_bind_only = FALSE;
static unsigned short server_connectport = 0; /* if non-zero,
we activate this mode */
enum sockmode {
PASSIVE_LISTEN, /* as a server waiting for connections */
@ -1126,7 +1120,8 @@ static bool juggle(curl_socket_t *sockfdp,
else if(!memcmp("PORT", buffer, 4)) {
/* Question asking us what PORT number we are listening to.
Replies to PORT with "IPv[num]/[port]" */
msnprintf((char *)buffer, sizeof(buffer), "%s/%hu\n", ipv_inuse, port);
msnprintf((char *)buffer, sizeof(buffer), "%s/%hu\n",
ipv_inuse, server_port);
buffer_len = (ssize_t)strlen((char *)buffer);
msnprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
if(!write_stdout(data, 10))
@ -1231,8 +1226,9 @@ static bool juggle(curl_socket_t *sockfdp,
#endif
}
static curl_socket_t sockdaemon(curl_socket_t sock,
unsigned short *listenport)
static curl_socket_t sockfilt_sockdaemon(curl_socket_t sock,
unsigned short *listenport,
bool bind_only)
{
/* passive daemon style */
srvr_sockaddr_union_t listener;
@ -1380,8 +1376,6 @@ int main(int argc, char *argv[])
curl_socket_t msgsock = CURL_SOCKET_BAD;
int wrotepidfile = 0;
int wroteportfile = 0;
const char *pidname = ".sockfilt.pid";
const char *portname = NULL; /* none by default */
bool juggle_again;
int rc;
int error;
@ -1389,7 +1383,9 @@ int main(int argc, char *argv[])
enum sockmode mode = PASSIVE_LISTEN; /* default */
const char *addr = NULL;
pidname = ".sockfilt.pid";
serverlogfile = "log/sockfilt.log";
server_port = 8999;
while(argc > arg) {
if(!strcmp("--version", argv[arg])) {
@ -1437,7 +1433,7 @@ int main(int argc, char *argv[])
arg++;
}
else if(!strcmp("--bindonly", argv[arg])) {
bind_only = TRUE;
s_bind_only = TRUE;
arg++;
}
else if(!strcmp("--port", argv[arg])) {
@ -1445,7 +1441,7 @@ int main(int argc, char *argv[])
if(argc > arg) {
char *endptr;
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
port = util_ultous(ulnum);
server_port = util_ultous(ulnum);
arg++;
}
}
@ -1462,7 +1458,7 @@ int main(int argc, char *argv[])
argv[arg]);
return 0;
}
connectport = util_ultous(ulnum);
server_connectport = util_ultous(ulnum);
arg++;
}
}
@ -1518,7 +1514,7 @@ int main(int argc, char *argv[])
goto sockfilt_cleanup;
}
if(connectport) {
if(server_connectport) {
/* Active mode, we should connect to the given port number */
mode = ACTIVE;
#ifdef USE_IPV6
@ -1526,7 +1522,7 @@ int main(int argc, char *argv[])
#endif
memset(&me.sa4, 0, sizeof(me.sa4));
me.sa4.sin_family = AF_INET;
me.sa4.sin_port = htons(connectport);
me.sa4.sin_port = htons(server_connectport);
me.sa4.sin_addr.s_addr = INADDR_ANY;
if(!addr)
addr = "127.0.0.1";
@ -1538,7 +1534,7 @@ int main(int argc, char *argv[])
else {
memset(&me.sa6, 0, sizeof(me.sa6));
me.sa6.sin6_family = AF_INET6;
me.sa6.sin6_port = htons(connectport);
me.sa6.sin6_port = htons(server_connectport);
if(!addr)
addr = "::1";
Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);
@ -1549,7 +1545,7 @@ int main(int argc, char *argv[])
if(rc) {
error = SOCKERRNO;
logmsg("Error connecting to port %hu (%d) %s",
connectport, error, sstrerror(error));
server_connectport, error, sstrerror(error));
write_stdout("FAIL\n", 5);
goto sockfilt_cleanup;
}
@ -1558,7 +1554,7 @@ int main(int argc, char *argv[])
}
else {
/* passive daemon style */
sock = sockdaemon(sock, &port);
sock = sockfilt_sockdaemon(sock, &server_port, s_bind_only);
if(CURL_SOCKET_BAD == sock) {
write_stdout("FAIL\n", 5);
goto sockfilt_cleanup;
@ -1568,12 +1564,12 @@ int main(int argc, char *argv[])
logmsg("Running %s version", ipv_inuse);
if(connectport)
logmsg("Connected to port %hu", connectport);
else if(bind_only)
logmsg("Bound without listening on port %hu", port);
if(server_connectport)
logmsg("Connected to port %hu", server_connectport);
else if(s_bind_only)
logmsg("Bound without listening on port %hu", server_port);
else
logmsg("Listening on port %hu", port);
logmsg("Listening on port %hu", server_port);
wrotepidfile = write_pidfile(pidname);
if(!wrotepidfile) {
@ -1581,7 +1577,7 @@ int main(int argc, char *argv[])
goto sockfilt_cleanup;
}
if(portname) {
wroteportfile = write_portfile(portname, port);
wroteportfile = write_portfile(portname, server_port);
if(!wroteportfile) {
write_stdout("FAIL\n", 5);
goto sockfilt_cleanup;