1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-23 08:38:49 -05:00

tests: run the TFTP test server on a dynamic port number

Picking a dynamic unused port is better than a fixed to avoid the
collision risk.

Closes #5265
This commit is contained in:
Daniel Stenberg 2020-04-19 23:29:21 +02:00
parent 5811beba39
commit 369ce38ac1
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
3 changed files with 85 additions and 23 deletions

View File

@ -1940,7 +1940,6 @@ sub runftpsserver {
# #
sub runtftpserver { sub runtftpserver {
my ($id, $verbose, $ipv6) = @_; my ($id, $verbose, $ipv6) = @_;
my $port = $TFTPPORT;
my $ip = $HOSTIP; my $ip = $HOSTIP;
my $proto = 'tftp'; my $proto = 'tftp';
my $ipvnum = 4; my $ipvnum = 4;
@ -1954,13 +1953,13 @@ sub runtftpserver {
if($ipv6) { if($ipv6) {
# if IPv6, use a different setup # if IPv6, use a different setup
$ipvnum = 6; $ipvnum = 6;
$port = $TFTP6PORT;
$ip = $HOST6IP; $ip = $HOST6IP;
} }
$server = servername_id($proto, $ipvnum, $idnum); $server = servername_id($proto, $ipvnum, $idnum);
$pidfile = $serverpidfile{$server}; $pidfile = $serverpidfile{$server};
my $portfile = $serverportfile{$server};
# don't retry if the server doesn't work # don't retry if the server doesn't work
if ($doesntrun{$pidfile}) { if ($doesntrun{$pidfile}) {
@ -1978,9 +1977,11 @@ sub runtftpserver {
$logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum); $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
$flags .= "--verbose " if($debugprotocol); $flags .= "--verbose " if($debugprotocol);
$flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" "; $flags .= "--pidfile \"$pidfile\" ".
"--portfile \"$portfile\" ".
"--logfile \"$logfile\" ";
$flags .= "--id $idnum " if($idnum > 1); $flags .= "--id $idnum " if($idnum > 1);
$flags .= "--ipv$ipvnum --port $port --srcdir \"$srcdir\""; $flags .= "--ipv$ipvnum --port 0 --srcdir \"$srcdir\"";
my $cmd = "$perl $srcdir/tftpserver.pl $flags"; my $cmd = "$perl $srcdir/tftpserver.pl $flags";
my ($tftppid, $pid2) = startnew($cmd, $pidfile, 15, 0); my ($tftppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
@ -1994,6 +1995,8 @@ sub runtftpserver {
return (0,0); return (0,0);
} }
my $port = pidfromfile($portfile);
# Server is up. Verify that we can speak to it. # Server is up. Verify that we can speak to it.
my $pid3 = verifyserver($proto, $ipvnum, $idnum, $ip, $port); my $pid3 = verifyserver($proto, $ipvnum, $idnum, $ip, $port);
if(!$pid3) { if(!$pid3) {
@ -2007,12 +2010,12 @@ sub runtftpserver {
$pid2 = $pid3; $pid2 = $pid3;
if($verbose) { if($verbose) {
logmsg "RUN: $srvrname server is now running PID $tftppid\n"; logmsg "RUN: $srvrname server on PID $tftppid port $port\n";
} }
sleep(1); sleep(1);
return ($pid2, $tftppid); return ($pid2, $tftppid, $port);
} }
@ -3152,13 +3155,9 @@ sub checksystem {
logmsg sprintf("FTPS/%d ", $FTPSPORT); logmsg sprintf("FTPS/%d ", $FTPSPORT);
logmsg sprintf("HTTPS/%d ", $HTTPSPORT); logmsg sprintf("HTTPS/%d ", $HTTPSPORT);
} }
logmsg sprintf("TFTP/%d ", $TFTPPORT);
if($http_ipv6) { if($http_ipv6) {
logmsg sprintf("RTSP-IPv6/%d ", $RTSP6PORT); logmsg sprintf("RTSP-IPv6/%d ", $RTSP6PORT);
} }
if($tftp_ipv6) {
logmsg sprintf("TFTP-IPv6/%d ", $TFTP6PORT);
}
logmsg sprintf("\n* SSH/%d ", $SSHPORT); logmsg sprintf("\n* SSH/%d ", $SSHPORT);
logmsg sprintf("SOCKS/%d ", $SOCKSPORT); logmsg sprintf("SOCKS/%d ", $SOCKSPORT);
if($httptlssrv) { if($httptlssrv) {
@ -4848,7 +4847,8 @@ sub startservers {
stopserver('tftp'); stopserver('tftp');
} }
if(!$run{'tftp'}) { if(!$run{'tftp'}) {
($pid, $pid2) = runtftpserver("", $verbose); ($pid, $pid2, $TFTPPORT) =
runtftpserver("", $verbose);
if($pid <= 0) { if($pid <= 0) {
return "failed starting TFTP server"; return "failed starting TFTP server";
} }
@ -4862,7 +4862,8 @@ sub startservers {
stopserver('tftp-ipv6'); stopserver('tftp-ipv6');
} }
if(!$run{'tftp-ipv6'}) { if(!$run{'tftp-ipv6'}) {
($pid, $pid2) = runtftpserver("", $verbose, "ipv6"); ($pid, $pid2, $TFTP6PORT) =
runtftpserver("", $verbose, "ipv6");
if($pid <= 0) { if($pid <= 0) {
return "failed starting TFTP-IPv6 server"; return "failed starting TFTP-IPv6 server";
} }
@ -5440,8 +5441,6 @@ if ($gdbthis) {
$minport = $base; # original base port number $minport = $base; # original base port number
$HTTPSPORT = $base++; # HTTPS (stunnel) server port $HTTPSPORT = $base++; # HTTPS (stunnel) server port
$FTPSPORT = $base++; # FTPS (stunnel) server port $FTPSPORT = $base++; # FTPS (stunnel) server port
$TFTPPORT = $base++; # TFTP (UDP) port
$TFTP6PORT = $base++; # TFTP IPv6 (UDP) port
$SSHPORT = $base++; # SSH (SCP/SFTP) port $SSHPORT = $base++; # SSH (SCP/SFTP) port
$SOCKSPORT = $base++; # SOCKS port $SOCKSPORT = $base++; # SOCKS port
$RTSPPORT = $base++; # RTSP server port $RTSPPORT = $base++; # RTSP server port

View File

@ -210,6 +210,7 @@ static const char *ipv_inuse = "IPv4";
const char *serverlogfile = DEFAULT_LOGFILE; const char *serverlogfile = DEFAULT_LOGFILE;
static const char *pidname = ".tftpd.pid"; static const char *pidname = ".tftpd.pid";
static const char *portfile = NULL;
static int serverlogslocked = 0; static int serverlogslocked = 0;
static int wrotepidfile = 0; static int wrotepidfile = 0;
@ -574,6 +575,11 @@ int main(int argc, char **argv)
if(argc>arg) if(argc>arg)
pidname = argv[arg++]; pidname = argv[arg++];
} }
else if(!strcmp("--portfile", argv[arg])) {
arg++;
if(argc>arg)
portfile = argv[arg++];
}
else if(!strcmp("--logfile", argv[arg])) { else if(!strcmp("--logfile", argv[arg])) {
arg++; arg++;
if(argc>arg) if(argc>arg)
@ -598,12 +604,6 @@ int main(int argc, char **argv)
if(argc>arg) { if(argc>arg) {
char *endptr; char *endptr;
unsigned long ulnum = strtoul(argv[arg], &endptr, 10); unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
if((endptr != argv[arg] + strlen(argv[arg])) ||
(ulnum < 1025UL) || (ulnum > 65535UL)) {
fprintf(stderr, "tftpd: invalid --port argument (%s)\n",
argv[arg]);
return 0;
}
port = curlx_ultous(ulnum); port = curlx_ultous(ulnum);
arg++; arg++;
} }
@ -690,12 +690,64 @@ int main(int argc, char **argv)
goto tftpd_cleanup; goto tftpd_cleanup;
} }
if(!port) {
/* The system was supposed to choose a port number, figure out which
port we actually got and update the listener port value with it. */
curl_socklen_t la_size;
srvr_sockaddr_union_t localaddr;
#ifdef ENABLE_IPV6
if(!use_ipv6)
#endif
la_size = sizeof(localaddr.sa4);
#ifdef ENABLE_IPV6
else
la_size = sizeof(localaddr.sa6);
#endif
memset(&localaddr.sa, 0, (size_t)la_size);
if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
switch(localaddr.sa.sa_family) {
case AF_INET:
port = ntohs(localaddr.sa4.sin_port);
break;
#ifdef ENABLE_IPV6
case AF_INET6:
port = ntohs(localaddr.sa6.sin6_port);
break;
#endif
default:
break;
}
if(!port) {
/* Real failure, listener port shall not be zero beyond this point. */
logmsg("Apparently getsockname() succeeded, with listener port zero.");
logmsg("A valid reason for this failure is a binary built without");
logmsg("proper network library linkage. This might not be the only");
logmsg("reason, but double check it before anything else.");
result = 2;
goto tftpd_cleanup;
}
}
wrotepidfile = write_pidfile(pidname); wrotepidfile = write_pidfile(pidname);
if(!wrotepidfile) { if(!wrotepidfile) {
result = 1; result = 1;
goto tftpd_cleanup; goto tftpd_cleanup;
} }
if(portfile) {
wrotepidfile = write_portfile(portfile, port);
if(!wrotepidfile) {
result = 1;
goto tftpd_cleanup;
}
}
logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port); logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
for(;;) { for(;;) {
@ -800,6 +852,8 @@ tftpd_cleanup:
if(wrotepidfile) if(wrotepidfile)
unlink(pidname); unlink(pidname);
if(portfile)
unlink(portfile);
if(serverlogslocked) { if(serverlogslocked) {
serverlogslocked = 0; serverlogslocked = 0;

View File

@ -43,8 +43,9 @@ my $port = 8997; # just a default
my $ipvnum = 4; # default IP version of tftp server my $ipvnum = 4; # default IP version of tftp server
my $idnum = 1; # default tftp server instance number my $idnum = 1; # default tftp server instance number
my $proto = 'tftp'; # protocol the tftp server speaks my $proto = 'tftp'; # protocol the tftp server speaks
my $pidfile; # tftp server pid file my $pidfile;
my $logfile; # tftp server log file my $portfile;
my $logfile;
my $srcdir; my $srcdir;
my $fork; my $fork;
@ -59,6 +60,12 @@ while(@ARGV) {
shift @ARGV; shift @ARGV;
} }
} }
elsif($ARGV[0] eq '--portfile') {
if($ARGV[1]) {
$portfile = $ARGV[1];
shift @ARGV;
}
}
elsif($ARGV[0] eq '--logfile') { elsif($ARGV[0] eq '--logfile') {
if($ARGV[1]) { if($ARGV[1]) {
$logfile = $ARGV[1]; $logfile = $ARGV[1];
@ -108,7 +115,9 @@ if(!$logfile) {
$logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum); $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
} }
$flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" "; $flags .= "--pidfile \"$pidfile\" ".
"--portfile \"$portfile\" ".
"--logfile \"$logfile\" ";
$flags .= "--ipv$ipvnum --port $port --srcdir \"$srcdir\""; $flags .= "--ipv$ipvnum --port $port --srcdir \"$srcdir\"";
exec("server/tftpd".exe_ext('SRV')." $flags"); exec("server/tftpd".exe_ext('SRV')." $flags");