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

@ -152,8 +152,6 @@ struct bf {
#define REQUEST_DUMP "server.input"
#define DEFAULT_PORT 8999 /* UDP */
/*****************************************************************************
* GLOBAL VARIABLES *
*****************************************************************************/
@ -185,7 +183,7 @@ static int current; /* index of buffer in use */
static int newline = 0; /* fillbuf: in middle of newline expansion */
static int prevchar = -1; /* putbuf: previous char (cr check) */
static tftphdr_storage_t buf;
static tftphdr_storage_t trsbuf;
static tftphdr_storage_t ackbuf;
static srvr_sockaddr_union_t from;
@ -196,18 +194,8 @@ static curl_socket_t peer = CURL_SOCKET_BAD;
static unsigned int timeout;
static unsigned int maxtimeout = 5 * TIMEOUT;
#ifdef USE_IPV6
static bool use_ipv6 = FALSE;
#endif
static const char *ipv_inuse = "IPv4";
static const char *logdir = "log";
static char loglockfile[256];
static const char *pidname = ".tftpd.pid";
static const char *portname = NULL; /* none by default */
static int serverlogslocked = 0;
static int wrotepidfile = 0;
static int wroteportfile = 0;
static int tftpd_wrotepidfile = 0;
static int tftpd_wroteportfile = 0;
#ifdef HAVE_SIGSETJMP
static sigjmp_buf timeoutbuf;
@ -282,12 +270,12 @@ static void timer(int signum)
timeout += rexmtval;
if(timeout >= maxtimeout) {
if(wrotepidfile) {
wrotepidfile = 0;
if(tftpd_wrotepidfile) {
tftpd_wrotepidfile = 0;
unlink(pidname);
}
if(wroteportfile) {
wroteportfile = 0;
if(tftpd_wroteportfile) {
tftpd_wroteportfile = 0;
unlink(portname);
}
if(serverlogslocked) {
@ -549,7 +537,7 @@ int main(int argc, char **argv)
struct tftphdr *tp;
ssize_t n = 0;
int arg = 1;
unsigned short port = DEFAULT_PORT;
unsigned short port = 8999; /* UDP */
curl_socket_t sock = CURL_SOCKET_BAD;
int flag;
int rc;
@ -559,7 +547,9 @@ int main(int argc, char **argv)
memset(&test, 0, sizeof(test));
pidname = ".tftpd.pid";
serverlogfile = "log/tftpd.log";
serverlogslocked = 0;
while(argc > arg) {
if(!strcmp("--version", argv[arg])) {
@ -743,15 +733,15 @@ int main(int argc, char **argv)
}
}
wrotepidfile = write_pidfile(pidname);
if(!wrotepidfile) {
tftpd_wrotepidfile = write_pidfile(pidname);
if(!tftpd_wrotepidfile) {
result = 1;
goto tftpd_cleanup;
}
if(portname) {
wroteportfile = write_portfile(portname, port);
if(!wroteportfile) {
tftpd_wroteportfile = write_portfile(portname, port);
if(!tftpd_wroteportfile) {
result = 1;
goto tftpd_cleanup;
}
@ -769,7 +759,7 @@ int main(int argc, char **argv)
else
fromlen = sizeof(from.sa6);
#endif
n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0,
n = (ssize_t)recvfrom(sock, &trsbuf.storage[0], sizeof(trsbuf.storage), 0,
&from.sa, &fromlen);
if(got_exit_signal)
break;
@ -817,7 +807,7 @@ int main(int argc, char **argv)
maxtimeout = 5*TIMEOUT;
tp = &buf.hdr;
tp = &trsbuf.hdr;
tp->th_opcode = ntohs(tp->th_opcode);
if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
memset(&test, 0, sizeof(test));
@ -854,9 +844,9 @@ tftpd_cleanup:
if(got_exit_signal)
logmsg("signalled to die");
if(wrotepidfile)
if(tftpd_wrotepidfile)
unlink(pidname);
if(wroteportfile)
if(tftpd_wroteportfile)
unlink(portname);
if(serverlogslocked) {
@ -916,7 +906,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
filename = cp;
do {
bool endofit = true;
while(cp < &buf.storage[size]) {
while(cp < &trsbuf.storage[size]) {
if(*cp == '\0') {
endofit = false;
break;
@ -929,7 +919,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
/* before increasing pointer, make sure it is still within the legal
space */
if((cp + 1) < &buf.storage[size]) {
if((cp + 1) < &trsbuf.storage[size]) {
++cp;
if(first) {
/* store the mode since we need it later */
@ -1003,7 +993,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
}
/* Based on the testno, parse the correct server commands. */
static int parse_servercmd(struct testcase *req)
static int tftpd_parse_servercmd(struct testcase *req)
{
FILE *stream;
int error;
@ -1117,7 +1107,7 @@ static int validate_access(struct testcase *test,
test->testno = testno;
(void)parse_servercmd(test);
(void)tftpd_parse_servercmd(test);
stream = test2fopen(testno, logdir);
@ -1324,7 +1314,7 @@ send_ack:
alarm(rexmtval);
#endif
/* normally times out and quits */
n = sread(peer, &buf.storage[0], sizeof(buf.storage));
n = sread(peer, &trsbuf.storage[0], sizeof(trsbuf.storage));
#ifdef HAVE_ALARM
alarm(0);
#endif
@ -1354,7 +1344,7 @@ static void nak(int error)
int length;
struct errmsg *pe;
tp = &buf.hdr;
tp = &trsbuf.hdr;
tp->th_opcode = htons(opcode_ERROR);
tp->th_code = htons((unsigned short)error);
for(pe = errmsgs; pe->e_code >= 0; pe++)
@ -1370,6 +1360,6 @@ static void nak(int error)
* report from glibc with FORTIFY_SOURCE */
memcpy(tp->th_msg, pe->e_msg, length + 1);
length += 5;
if(swrite(peer, &buf.storage[0], length) != length)
if(swrite(peer, &trsbuf.storage[0], length) != length)
logmsg("nak: fail\n");
}