Artifactspublic-v0

curl

master
ply4 git calls27 ms305 KiB3 cachedprotocol v2
2900 lines96 KiBRaw
1#***************************************************************************
2# _ _ ____ _
3# Project ___| | | | _ \| |
4# / __| | | | |_) | |
5# | (__| |_| | _ <| |___
6# \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
21# SPDX-License-Identifier: curl
23###########################################################################
25# This module contains functions that are useful for managing the lifecycle of
26# test servers required when running tests. It is not intended for use within
27# those servers, but rather for starting and stopping them.
29package servers;
31use IO::Socket;
32use Time::HiRes;
33use strict;
34use warnings;
36BEGIN {
37 use base qw(Exporter);
39 our @EXPORT = (
40 # variables
41 qw(
42 $SOCKSIN
43 $err_unexpected
44 $debugprotocol
45 $stunnel
46 ),
48 # functions
49 qw(
50 initserverconfig
51 )
52 );
54 our @EXPORT_OK = (
55 # functions
56 qw(
57 checkcmd
58 serverfortest
59 stopserver
60 stopservers
61 subvariables
62 localhttp
63 ),
65 # for debugging only
66 qw(
67 protoport
68 )
69 );
72use serverhelp qw(
73 serverfactors
74 servername_id
75 servername_str
76 servername_canon
77 server_pidfilename
78 server_portfilename
79 server_logfilename
80 server_exe
81 );
83use sshhelp qw(
84 $hstpubmd5f
85 $hstpubsha256f
86 $sshexe
87 $sftpexe
88 $sftpconfig
89 $sshdlog
90 $sftplog
91 $sftpcmds
92 display_sshdconfig
93 display_sftpconfig
94 display_sshdlog
95 display_sftplog
96 find_sshd
97 find_ssh
98 find_sftp
99 sshversioninfo
100 );
102use pathhelp qw(
103 exe_ext
104 os_is_win
105 build_sys_abs_path
106 sys_native_abs_path
107 shell_quote
108 );
110use processhelp;
111use globalconfig;
112use testutil qw(
113 logmsg
114 runclient
115 runclientoutput
116 exerunner
117 );
119my %serverpidfile; # all server pid filenames, identified by server id
120my %serverportfile;# all server port filenames, identified by server id
121my $sshdvernum; # for socks server, ssh daemon version number
122my $sshdverstr; # for socks server, ssh daemon version string
123my $sshderror; # for socks server, ssh daemon version error
124my %doesntrun; # servers that do not work, identified by pidfile
125my %PORT = (nolisten => 47); # port we use for a local non-listening service
126my $server_response_maxtime = 13;
127my %run; # running server
128my %runcert; # cert file currently in use by an SSL running server
129my $CLIENTIP = "127.0.0.1"; # address which curl uses for incoming connections
130my $CLIENT6IP = "[::1]"; # address which curl uses for incoming connections
131my $posix_pwd = build_sys_abs_path($pwd); # current working directory in POSIX format
132my $h2cver = "h2c"; # this version is decided by the nghttp2 lib being used
133my $HOSTIP = "127.0.0.1"; # address on which the test server listens
134my $HOST6IP = "[::1]"; # address on which the test server listens
135my $HTTPUNIXPATH; # HTTP server Unix domain socket path
136my $SOCKSUNIXPATH; # socks server Unix domain socket path
137my $SSHSRVMD5 = "[uninitialized]"; # MD5 of ssh server public key
138my $SSHSRVSHA256 = "[uninitialized]"; # SHA256 of ssh server public key
139my $USER; # name of the current user
140my $sshdid; # for socks server, ssh daemon version id
141my $ftpchecktime = 1; # time it took to verify our test FTP server
142my $SERVER_TIMEOUT_SEC = 15; # time for a server to spin up
144# Variables shared with runtests.pl
145our $SOCKSIN = "socksd-request.log"; # what curl sent to the SOCKS proxy
146our $err_unexpected; # error instead of warning on server unexpectedly alive
147our $debugprotocol; # nonzero for verbose server logs
148our $stunnel; # path to stunnel command
150#######################################################################
151# Check for a command in the PATH of the test server.
153sub checkcmd {
154 my ($cmd, @extrapaths) = @_;
155 my @paths;
156 if($^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'os2') {
157 # PATH separator is different
158 @paths = (split(';', $ENV{'PATH'}), @extrapaths);
159 }
160 else {
161 @paths = (split(':', $ENV{'PATH'}), "/usr/sbin", "/usr/local/sbin",
162 "/sbin", "/usr/bin", "/usr/local/bin", @extrapaths);
163 }
164 for(@paths) {
165 if(-x "$_/$cmd" . exe_ext('SYS') && ! -d "$_/$cmd" . exe_ext('SYS')) {
166 # executable bit but not a directory!
167 return "$_/$cmd";
168 }
169 }
170 return "";
173#######################################################################
174# Create a server socket on a random (unused) port, then close it and
175# return the port number
177sub getfreeport {
178 my ($ipnum, $reuse) = @_;
179 my $server = IO::Socket->new(LocalPort => 0,
180 Domain => $ipnum == 6 ? AF_INET6 : AF_INET,
181 Type => SOCK_STREAM,
182 Reuse => $reuse,
183 Listen => 10 )
184 or die "Could not create tcp server socket: $@\n";
186 return $server->sockport();
189use File::Temp qw/ tempfile/;
191#######################################################################
192# Initialize configuration variables
193sub initserverconfig {
194 $SOCKSUNIXPATH = "$pwd/$LOGDIR/$PIDDIR/socks-uds"; # SOCKS Unix domain socket
195 $HTTPUNIXPATH = "$pwd/$LOGDIR/$PIDDIR/http-uds"; # HTTP Unix domain socket
196 $stunnel = checkcmd("stunnel4") || checkcmd("tstunnel") || checkcmd("stunnel");
198 # get the name of the current user
199 $USER = $ENV{USER}; # Linux
200 if(!$USER) {
201 $USER = $ENV{USERNAME}; # Windows
202 }
203 if(!$USER) {
204 $USER = $ENV{LOGNAME}; # Some Unix (I think)
205 }
206 if(!$USER) {
207 $USER = qx(whoami);
208 chomp $USER;
209 }
210 if(!$USER) {
211 $USER = qx(id -un);
212 chomp $USER;
213 }
214 init_serverpidfile_hash();
217#######################################################################
218# Load serverpidfile and serverportfile hashes with filenames for all
219# possible servers.
221sub init_serverpidfile_hash {
222 for my $proto (('ftp', 'gopher', 'http', 'imap', 'pop3', 'smtp', 'http/2', 'http/3')) {
223 for my $ssl (('', 's')) {
224 for my $ipvnum ((4, 6)) {
225 for my $idnum ((1, 2, 3)) {
226 my $serv = servername_id("$proto$ssl", $ipvnum, $idnum);
227 my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
228 $ipvnum, $idnum);
229 $serverpidfile{$serv} = $pidf;
230 my $portf = server_portfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
231 $ipvnum, $idnum);
232 $serverportfile{$serv} = $portf;
233 }
234 }
235 }
236 }
237 for my $proto (('tftp', 'sftp', 'socks', 'ssh', 'rtsp',
238 'dict', 'telnet', 'mqtt', 'mqtts',
239 'https-mtls', 'dns')) {
240 for my $ipvnum ((4, 6)) {
241 for my $idnum ((1, 2)) {
242 my $serv = servername_id($proto, $ipvnum, $idnum);
243 my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
244 $idnum);
245 $serverpidfile{$serv} = $pidf;
246 my $portf = server_portfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
247 $idnum);
248 $serverportfile{$serv} = $portf;
249 }
250 }
251 }
252 for my $proto (('http', 'imap', 'pop3', 'smtp', 'http/2', 'http/3')) {
253 for my $ssl (('', 's')) {
254 my $serv = servername_id("$proto$ssl", "unix", 1);
255 my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
256 "unix", 1);
257 $serverpidfile{$serv} = $pidf;
258 my $portf = server_portfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
259 "unix", 1);
260 $serverportfile{$serv} = $portf;
261 }
262 }
265#######################################################################
266# Check if a given child process has died. Reaps it if so.
268sub checkdied {
269 my $pid = $_[0];
270 if((not defined $pid) || $pid <= 0) {
271 return 0;
272 }
273 use POSIX ":sys_wait_h";
274 my $rc = pidwait($pid, &WNOHANG);
275 return ($rc == $pid) ? 1 : 0;
278##############################################################################
279# This function makes sure the right set of server is running for the
280# specified test case. This is a useful design when we run single tests as not
281# all servers need to run then!
283# Returns: a string, blank if everything is fine or a reason why it failed, and
284# an integer:
285# 0 for success
286# 1 for an error starting the server
287# 2 for not the first time getting an error starting the server
288# 3 for a failure to stop a server in order to restart it
289# 4 for an unsupported server type
291sub serverfortest {
292 my (@what) = @_;
294 for(my $i = scalar(@what) - 1; $i >= 0; $i--) {
295 my $srvrline = $what[$i];
296 chomp $srvrline if($srvrline);
298 if($srvrline =~ /^(\S+)((\s*)(.*))/) {
299 my $server = "${1}";
300 my $lnrest = "${2}";
301 my $tlsext;
303 my @lprotocols = @protocols;
305 push @lprotocols, "dns";
307 if(! grep /^\Q$server\E$/, @lprotocols) {
308 if(substr($server, 0, 5) ne "socks") {
309 if($tlsext) {
310 return ("curl lacks $tlsext support", 4);
311 }
312 else {
313 return ("curl lacks $server server support", 4);
314 }
315 }
316 }
317 $what[$i] = "$server$lnrest" if($tlsext);
318 }
319 }
321 return &startservers(@what);
324#######################################################################
325# Start a new thread/process and run the given command line in there.
326# Return the pids (yes plural) of the new child process to the parent.
328sub startnew {
329 my ($cmd, $pidfile, $timeout, $fakepidfile) = @_;
331 logmsg "startnew: $cmd\n" if($verbose);
333 my $child = fork();
335 if(not defined $child) {
336 logmsg "startnew: fork() failure detected\n";
337 return (-1, -1);
338 }
340 if(0 == $child) {
341 # Here we are the child. Run the given command.
343 # Flush output.
344 $| = 1;
346 # Put an "exec" in front of the command so that the child process
347 # keeps this child's process ID.
348 exec("exec $cmd") or die "Cannot exec() $cmd: $!";
350 # exec() should never return back here to this process. We protect
351 # ourselves by calling die() in case something goes really bad.
352 die "error: exec() has returned";
353 }
355 # Ugly hack but ssh client and gnutls-serv do not support pid files
356 if($fakepidfile) {
357 if(open(my $out, ">", $pidfile)) {
358 print $out $child . "\n";
359 close($out) or die "Failure writing pidfile";
360 logmsg "startnew: $pidfile faked with pid=$child\n" if($verbose);
361 }
362 else {
363 logmsg "startnew: failed to write fake $pidfile with pid=$child\n";
364 }
365 # could/should do a while connect fails sleep a bit and loop
366 Time::HiRes::sleep($timeout);
367 if(checkdied($child)) {
368 logmsg "startnew: child process has failed to start\n" if($verbose);
369 return (-1, -1);
370 }
371 }
373 my $pid2 = 0;
374 my $count = $timeout;
375 while($count--) {
376 $pid2 = pidfromfile($pidfile, 0);
377 if(($pid2 > 0) && pidexists($pid2)) {
378 # if $pid2 is valid, then make sure this pid is alive, as
379 # otherwise it is likely to be the _previous_ pidfile or
380 # similar!
381 last;
382 }
383 if(checkdied($child)) {
384 logmsg "startnew: child process has died, server might start up\n"
385 if($verbose);
386 # We cannot abort waiting for the server with a
387 # return (-1, -1);
388 # because the server might have forked and could still start
389 # up normally. Instead, reduce the amount of time we remain
390 # waiting.
391 $count >>= 2;
392 }
393 sleep(1);
394 }
396 # Return two PIDs, the one for the child process we spawned and the one
397 # reported by the server itself (in case it forked again on its own).
398 # Both (potentially) need to be killed at the end of the test.
399 return ($child, $pid2);
402#######################################################################
403# Return the port to use for the given protocol.
405sub protoport {
406 my ($proto) = @_;
407 return $PORT{$proto} || "[not running]";
410#######################################################################
411# Stop a test server along with pids which are not in the %run hash yet.
412# This also stops all servers which are relative to the given one.
414sub stopserver {
415 my ($server, $pidlist) = @_;
416 my $ipvnum = 4;
418 #
419 # kill sockfilter processes for pingpong relative server
420 #
421 if($server =~ /^(ftp|imap|pop3|smtp)s?(\d*)(-ipv6|)$/) {
422 my $proto = $1;
423 my $idnum = ($2 && ($2 > 1)) ? $2 : 1;
424 $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
425 killsockfilters("$LOGDIR/$PIDDIR", $proto, $ipvnum, $idnum, $verbose);
426 }
427 #
428 # All servers relative to the given one must be stopped also
429 #
430 my @killservers;
431 if($server =~ /^(ftp|http|imap|pop3|smtp)s((\d*)(-ipv6|-unix|))$/) {
432 # given a stunnel based SSL server, also kill non-SSL underlying one
433 push @killservers, "${1}${2}";
434 }
435 elsif($server =~ /^(ftp|http|imap|pop3|smtp)((\d*)(-ipv6|-unix|))$/) {
436 # given a non-SSL server, also kill stunnel based SSL piggybacking one
437 push @killservers, "${1}s${2}";
438 }
439 elsif($server =~ /^(socks)((\d*)(-ipv6|))$/) {
440 # given a socks server, also kill ssh underlying one
441 push @killservers, "ssh${2}";
442 }
443 elsif($server =~ /^(ssh)((\d*)(-ipv6|))$/) {
444 # given a ssh server, also kill socks piggybacking one
445 push @killservers, "socks${2}";
446 }
447 if($server eq "http" or $server eq "https") {
448 # since the http2+3 server is a proxy that needs to know about the
449 # dynamic http port it too needs to get restarted when the http server
450 # is killed
451 push @killservers, "http/2";
452 push @killservers, "http/3";
453 }
454 push @killservers, $server;
455 #
456 # kill given pids and server relative ones clearing them in %run hash
457 #
458 foreach my $server (@killservers) {
459 if($run{$server}) {
460 # we must prepend a space since $pidlist may already contain a pid
461 $pidlist .= " $run{$server}";
462 $run{$server} = 0;
463 }
464 $runcert{$server} = 0 if($runcert{$server});
465 }
466 killpid($verbose, $pidlist);
467 #
468 # cleanup server pid files
469 #
470 my $result = 0;
471 foreach my $server (@killservers) {
472 my $pidfile = $serverpidfile{$server};
473 if($pidfile) {
474 unlink($pidfile) if(-f $pidfile);
475 }
476 else {
477 logmsg "RUN: no PID file for '$server' to clean\n";
478 }
479 }
480 #
481 # cleanup server lock files
482 #
483 foreach my $server (@killservers) {
484 # servers seem to produce (some of) these lock files
485 my @lockfiles = (
486 "$LOGDIR/$LOCKDIR/$server.lock",
487 "$LOGDIR/$LOCKDIR/$server-IPv$ipvnum.lock",
488 "$LOGDIR/$LOCKDIR/sws-" . uc($server) . "-IPv$ipvnum.lock"
489 );
490 foreach my $lockfile (@lockfiles) {
491 if(-f $lockfile) {
492 unlink($lockfile);
493 logmsg "RUN: kill $server, cleaned up $lockfile\n" if($verbose);
494 }
495 }
496 }
498 return $result;
501#######################################################################
502# Return flags to let curl use an external HTTP proxy
504sub getexternalproxyflags {
505 return " --proxy $proxy_address ";
508#######################################################################
509# Verify that the server that runs on $ip, $port is our server. This also
510# implies that we can speak with it, as there might be occasions when the
511# server runs fine but we cannot talk to it ("Failed to connect to ::1: Cannot
512# assign requested address")
514sub verifyhttp {
515 my ($proto, $ipvnum, $idnum, $ip, $port_or_path, $do_http3) = @_;
516 my $server = servername_id($proto, $ipvnum, $idnum);
517 my $bonus = "";
518 # $port_or_path contains a path for Unix sockets, sws ignores the port
519 my $port = ($ipvnum eq "unix") ? 80 : $port_or_path;
520 my $infix = ($do_http3) ? "_h3" : "";
522 my $verifyout = "$LOGDIR/" .
523 servername_canon($proto, $ipvnum, $idnum) . $infix . '_verify.out';
524 unlink($verifyout) if(-f $verifyout);
526 my $verifylog = "$LOGDIR/" .
527 servername_canon($proto, $ipvnum, $idnum) . $infix . '_verify.log';
528 unlink($verifylog) if(-f $verifylog);
530 if($proto eq "gopher") {
531 # gopher is funny
532 $bonus = "1/";
533 }
535 my $flags = "--max-time $server_response_maxtime ";
536 $flags .= "--output $verifyout ";
537 $flags .= "--silent ";
538 $flags .= "--verbose ";
539 $flags .= "--globoff ";
540 $flags .= "--unix-socket '$port_or_path' " if $ipvnum eq "unix";
541 $flags .= "--insecure " if($proto eq 'https');
542 if($proxy_address) {
543 $flags .= getexternalproxyflags();
544 }
545 $flags .= "--http3-only " if($do_http3);
546 $flags .= "\"$proto://$ip:$port/${bonus}verifiedserver\"";
548 my $cmd = exerunner() . "$VCURL $flags 2>$verifylog";
550 # verify if our/any server is running on this port
551 logmsg "RUN: $cmd\n" if($verbose);
552 my $res = runclient($cmd);
554 $res >>= 8; # rotate the result
555 if($res & 128) {
556 logmsg "RUN: curl command died with a coredump\n";
557 return -1;
558 }
560 if($res && $verbose) {
561 logmsg "RUN: curl command returned $res\n";
562 if(open(my $file, "<", $verifylog)) {
563 while(my $string = <$file>) {
564 logmsg "RUN: $string" if($string !~ /^([ \t]*)$/);
565 }
566 close($file);
567 }
568 }
570 my $data;
571 if(open(my $file, "<", $verifyout)) {
572 while(my $string = <$file>) {
573 $data = $string;
574 last; # only want first line
575 }
576 close($file);
577 }
579 my $pid = 0;
580 if($data && ($data =~ /WE ROOLZ: (\d+)/)) {
581 $pid = 0 + $1;
582 }
583 elsif($res == 6) {
584 # curl: (6) Could not resolve hostname '::1'
585 logmsg "RUN: failed to resolve host ($proto://$ip:$port/verifiedserver)\n";
586 return -1;
587 }
588 elsif($data || ($res && ($res != 7))) {
589 logmsg "RUN: Unknown server on our $server port: $port ($res)\n";
590 return -1;
591 }
592 return $pid;
595#######################################################################
596# Verify that the server that runs on $ip, $port is our server. This also
597# implies that we can speak with it, as there might be occasions when the
598# server runs fine but we cannot talk to it ("Failed to connect to ::1: Cannot
599# assign requested address")
601sub verifyftp {
602 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
603 my $server = servername_id($proto, $ipvnum, $idnum);
604 my $time = time();
605 my $extra = "";
607 my $verifylog = "$LOGDIR/" .
608 servername_canon($proto, $ipvnum, $idnum) . '_verify.log';
609 unlink($verifylog) if(-f $verifylog);
611 if($proto eq "ftps") {
612 $extra .= "--insecure --ftp-ssl-control ";
613 }
615 my $flags = "--max-time $server_response_maxtime ";
616 $flags .= "--silent ";
617 $flags .= "--verbose ";
618 $flags .= "--globoff ";
619 $flags .= $extra;
620 if($proxy_address) {
621 $flags .= getexternalproxyflags();
622 }
623 $flags .= "\"$proto://$ip:$port/verifiedserver\"";
625 my $cmd = exerunner() . "$VCURL $flags 2>$verifylog";
627 # check if this is our server running on this port:
628 logmsg "RUN: $cmd\n" if($verbose);
629 my @data = runclientoutput($cmd);
631 my $res = $? >> 8; # rotate the result
632 if($res & 128) {
633 logmsg "RUN: curl command died with a coredump\n";
634 return -1;
635 }
637 my $pid = 0;
638 foreach my $line (@data) {
639 if($line =~ /WE ROOLZ: (\d+)/) {
640 # this is our test server with a known pid!
641 $pid = 0 + $1;
642 last;
643 }
644 }
645 if($pid <= 0 && @data && $data[0]) {
646 # this is not a known server
647 logmsg "RUN: Unknown server on our $server port: $port\n";
648 return 0;
649 }
650 # we can/should use the time it took to verify the FTP server as a measure
651 # on how fast/slow this host/FTP is.
652 my $took = int(0.5 + time() - $time);
654 if($verbose) {
655 logmsg "RUN: Verifying our test $server server took $took seconds\n";
656 }
657 $ftpchecktime = ($took >= 1) ? $took : 1; # make sure it never is below 1
659 return $pid;
662#######################################################################
663# Verify that the server that runs on $ip, $port is our server. This also
664# implies that we can speak with it, as there might be occasions when the
665# server runs fine but we cannot talk to it ("Failed to connect to ::1: Cannot
666# assign requested address")
668sub verifyrtsp {
669 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
670 my $server = servername_id($proto, $ipvnum, $idnum);
672 my $verifyout = "$LOGDIR/" .
673 servername_canon($proto, $ipvnum, $idnum) . '_verify.out';
674 unlink($verifyout) if(-f $verifyout);
676 my $verifylog = "$LOGDIR/" .
677 servername_canon($proto, $ipvnum, $idnum) . '_verify.log';
678 unlink($verifylog) if(-f $verifylog);
680 my $flags = "--max-time $server_response_maxtime ";
681 $flags .= "--output $verifyout ";
682 $flags .= "--silent ";
683 $flags .= "--verbose ";
684 $flags .= "--globoff ";
685 if($proxy_address) {
686 $flags .= getexternalproxyflags();
687 }
688 # currently verification is done using http
689 $flags .= "\"http://$ip:$port/verifiedserver\"";
691 my $cmd = exerunner() . "$VCURL $flags 2>$verifylog";
693 # verify if our/any server is running on this port
694 logmsg "RUN: $cmd\n" if($verbose);
695 my $res = runclient($cmd);
697 $res >>= 8; # rotate the result
698 if($res & 128) {
699 logmsg "RUN: curl command died with a coredump\n";
700 return -1;
701 }
703 if($res && $verbose) {
704 logmsg "RUN: curl command returned $res\n";
705 if(open(my $file, "<", $verifylog)) {
706 while(my $string = <$file>) {
707 logmsg "RUN: $string" if($string !~ /^[ \t]*$/);
708 }
709 close($file);
710 }
711 }
713 my $data;
714 if(open(my $file, "<", $verifyout)) {
715 while(my $string = <$file>) {
716 $data = $string;
717 last; # only want first line
718 }
719 close($file);
720 }
722 my $pid = 0;
723 if($data && ($data =~ /RTSP_SERVER WE ROOLZ: (\d+)/)) {
724 $pid = 0 + $1;
725 }
726 elsif($res == 6) {
727 # curl: (6) Could not resolve hostname '::1'
728 logmsg "RUN: failed to resolve host ($proto://$ip:$port/verifiedserver)\n";
729 return -1;
730 }
731 elsif($data || ($res != 7)) {
732 logmsg "RUN: Unknown server on our $server port: $port\n";
733 return -1;
734 }
735 return $pid;
738#######################################################################
739# Verify that the ssh server has written out its pidfile, recovering
740# the pid from the file and returning it if a process with that pid is
741# actually alive, or a negative value if the process is dead.
743sub verifyssh {
744 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
745 my $pidfile = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
746 $idnum);
747 my $pid = processexists($pidfile);
748 if($pid < 0) {
749 logmsg "RUN: SSH server has died after starting up\n";
750 }
751 return $pid;
754#######################################################################
755# Verify that we can connect to the sftp server, properly authenticate
756# with generated config and key files and run a simple remote pwd.
758sub verifysftp {
759 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
760 my $server = servername_id($proto, $ipvnum, $idnum);
761 my $verified = 0;
762 # Find out sftp client canonical filename
763 my $sftp = find_sftp();
764 if(!$sftp) {
765 logmsg "RUN: SFTP server cannot find $sftpexe\n";
766 return -1;
767 }
768 # Find out ssh client canonical filename
769 my $ssh = find_ssh();
770 if(!$ssh) {
771 logmsg "RUN: SFTP server cannot find $sshexe\n";
772 return -1;
773 }
774 # Connect to sftp server, authenticate and run a remote pwd
775 # command using our generated configuration and key files
776 my $cmd = "\"$sftp\" -b $LOGDIR/$PIDDIR/$sftpcmds -F $LOGDIR/$PIDDIR/$sftpconfig -S \"$ssh\" $ip > $sftplog 2>&1";
777 my $res = runclient($cmd);
778 # Search for pwd command response in log file
779 if(open(my $sftplogfile, "<", $sftplog)) {
780 while(<$sftplogfile>) {
781 if(/^Remote working directory: /) {
782 $verified = 1;
783 last;
784 }
785 }
786 close($sftplogfile);
787 }
788 return $verified;
791#######################################################################
792# For verifying mqtt and socks
794sub verifypid {
795 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
796 my $pidfile = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
797 $idnum);
798 my $pid = processexists($pidfile);
799 if($pid < 0) {
800 logmsg "RUN: $proto server has died\n";
801 }
802 return $pid;
805#######################################################################
806# Verify that the server that runs on $ip, $port is our server. This also
807# implies that we can speak with it, as there might be occasions when the
808# server runs fine but we cannot talk to it ("Failed to connect to ::1: Cannot
809# assign requested address")
811sub verifytelnet {
812 my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
813 my $server = servername_id($proto, $ipvnum, $idnum);
814 my $time = time();
815 my $extra = "";
817 my $verifylog = "$LOGDIR/" .
818 servername_canon($proto, $ipvnum, $idnum) . '_verify.log';
819 unlink($verifylog) if(-f $verifylog);
821 my $flags = "--max-time $server_response_maxtime ";
822 $flags .= "--silent ";
823 $flags .= "--verbose ";
824 $flags .= "--globoff ";
825 $flags .= "--upload-file - ";
826 $flags .= $extra;
827 $flags .= "\"$proto://$ip:$port\"";
829 my $cmd = "echo 'verifiedserver' | " .
830 exerunner() . "$VCURL $flags 2>$verifylog";
832 # check if this is our server running on this port:
833 logmsg "RUN: $cmd\n" if($verbose);
834 my @data = runclientoutput($cmd);
836 my $res = $? >> 8; # rotate the result
837 if($res & 128) {
838 logmsg "RUN: curl command died with a coredump\n";
839 return -1;
840 }
842 my $pid = 0;
843 foreach my $line (@data) {
844 if($line =~ /WE ROOLZ: (\d+)/) {
845 # this is our test server with a known pid!
846 $pid = 0 + $1;
847 last;
848 }
849 }
850 if($pid <= 0 && @data && $data[0]) {
851 # this is not a known server
852 logmsg "RUN: Unknown server on our $server port: $port\n";
853 return 0;
854 }
855 # we can/should use the time it took to verify the server as a measure
856 # on how fast/slow this host is.
857 my $took = int(0.5+time()-$time);
859 if($verbose) {
860 logmsg "RUN: Verifying our test $server server took $took seconds\n";
861 }
863 return $pid;
866#######################################################################
867# Verify that the server that runs on $ip, $port is our server.
868# Retry over several seconds before giving up. The ssh server in
869# particular can take a long time to start if it needs to generate
870# keys on a slow or loaded host.
872# For convenience, test harness uses 'https' literal
873# as values for 'proto' variable in order to differentiate different
874# servers. 'https' literal is used for stunnel based https test servers.
877my %protofunc = ('http' => \&verifyhttp,
878 'https' => \&verifyhttp,
879 'https-mtls' => \&verifypid,
880 'rtsp' => \&verifyrtsp,
881 'ftp' => \&verifyftp,
882 'pop3' => \&verifyftp,
883 'imap' => \&verifyftp,
884 'smtp' => \&verifyftp,
885 'ftps' => \&verifyftp,
886 'pop3s' => \&verifyftp,
887 'imaps' => \&verifyftp,
888 'mqtt' => \&verifypid,
889 'mqtts' => \&verifypid,
890 'smtps' => \&verifyftp,
891 'tftp' => \&verifyftp,
892 'ssh' => \&verifyssh,
893 'dns' => \&verifypid,
894 'socks' => \&verifypid,
895 'socks5unix' => \&verifypid,
896 'gopher' => \&verifyhttp,
897 'dict' => \&verifyftp,
898 'telnet' => \&verifytelnet);
900#######################################################################
901# Single shot server responsiveness test. This should only be used
902# to verify that a server present in %run hash is still functional
904sub responsiveserver {
905 my ($proto, $ipvnum, $idnum, $ip, $port, $do_http3) = @_;
906 my $prev_verbose = $verbose;
908 $verbose = 0;
909 my $fun = $protofunc{$proto};
910 my $pid = &$fun($proto, $ipvnum, $idnum, $ip, $port, $do_http3);
911 $verbose = $prev_verbose;
913 if($pid > 0) {
914 return 1; # responsive
915 }
917 my $srvrname = servername_str($proto, $ipvnum, $idnum);
918 logmsg " server precheck FAILED (unresponsive $srvrname server)\n";
919 return 0;
922#######################################################################
923# start the http server
925sub runhttpserver {
926 my ($proto, $verb, $alt, $port_or_path) = @_;
927 my $ip = $HOSTIP;
928 my $ipvnum = 4;
929 my $idnum = 1;
930 my $exe = "$perl " . shell_quote("$srcdir/http-server.pl");
931 my $verbose_flag = "--verbose ";
932 my $keepalive_secs = 30; # forwarded to sws, was 5 by default which
933 # led to pukes in CI jobs
935 if($alt eq "ipv6") {
936 # if IPv6, use a different setup
937 $ipvnum = 6;
938 $ip = $HOST6IP;
939 }
940 elsif($alt eq "proxy") {
941 # basically the same, but another ID
942 $idnum = 2;
943 }
944 elsif($alt eq "unix") {
945 # IP (protocol) is mutually exclusive with Unix sockets
946 $ipvnum = "unix";
947 }
949 my $server = servername_id($proto, $ipvnum, $idnum);
951 my $pidfile = $serverpidfile{$server};
953 # do not retry if the server does not work
954 if($doesntrun{$pidfile}) {
955 return (2, 0, 0, 0);
956 }
958 my $pid = processexists($pidfile);
959 if($pid > 0) {
960 stopserver($server, $pid);
961 }
962 unlink($pidfile) if(-f $pidfile);
964 my $srvrname = servername_str($proto, $ipvnum, $idnum);
965 my $portfile = $serverportfile{$server};
967 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
969 my $flags = "";
970 $flags .= "--gopher " if($proto eq "gopher");
971 $flags .= "--connect $HOSTIP " if($alt eq "proxy");
972 $flags .= "--keepalive $keepalive_secs ";
973 $flags .= $verbose_flag if($debugprotocol);
974 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
975 $flags .= "--logdir \"$LOGDIR\" ";
976 $flags .= "--portfile $portfile ";
977 $flags .= "--config $LOGDIR/$SERVERCMD ";
978 $flags .= "--id $idnum " if($idnum > 1);
979 if($ipvnum eq "unix") {
980 $flags .= "--unix-socket '$port_or_path' ";
981 }
982 else {
983 $flags .= "--ipv$ipvnum --port 0 ";
984 }
985 $flags .= "--srcdir \"$srcdir\"";
987 my $cmd = "$exe $flags";
989 unlink($portfile); # need to see a new one
990 my ($httppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
992 if($httppid <= 0 || !pidexists($httppid)) {
993 # it is NOT alive
994 logmsg "RUN: failed to start the $srvrname server\n";
995 stopserver($server, $pid2);
996 $doesntrun{$pidfile} = 1;
997 return (1, 0, 0, 0);
998 }
1000 # where is it?
1001 my $port = 0;
1002 $port = $port_or_path = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1003 if(!$port) {
1004 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1005 stopserver($server, $pid2);
1006 $doesntrun{$pidfile} = 1;
1007 return (1, 0, 0, 0);
1010 if($verb) {
1011 logmsg "RUN: $srvrname server is on PID $httppid port $port_or_path\n";
1014 return (0, $httppid, $pid2, $port);
1017#######################################################################
1018# start the http2 server
1020sub runhttp2server {
1021 my ($verb) = @_;
1022 my $proto = "http/2";
1023 my $ipvnum = 4;
1024 my $idnum = 0;
1025 my $exe = "$perl " . shell_quote("$srcdir/http2-server.pl");
1026 my $verbose_flag = "--verbose ";
1027 my $nghttpx = $ENV{'NGHTTPX'};
1029 if(!$nghttpx) {
1030 logmsg "runhttp2server: NGHTTPX env is not set\n";
1031 return (2, 0, 0, 0);
1034 my $server = servername_id($proto, $ipvnum, $idnum);
1036 my $pidfile = $serverpidfile{$server};
1038 # do not retry if the server does not work
1039 if($doesntrun{$pidfile}) {
1040 return (2, 0, 0, 0, 0);
1043 my $pid = processexists($pidfile);
1044 if($pid > 0) {
1045 stopserver($server, $pid);
1047 unlink($pidfile) if(-f $pidfile);
1049 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1050 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1052 my $flags = "";
1053 $flags .= "--nghttpx \"$nghttpx\" ";
1054 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1055 $flags .= "--logdir \"$LOGDIR\" ";
1056 $flags .= "--connect $HOSTIP:" . protoport("http") . " ";
1057 $flags .= $verbose_flag if($debugprotocol);
1059 my $port = getfreeport($ipvnum, 0);
1060 my $port2 = getfreeport($ipvnum, 0);
1061 my $cmd = "$exe --port \"$port\" --port2 \"$port2\" $flags";
1062 my ($http2pid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1064 if($http2pid <= 0 || !pidexists($http2pid)) {
1065 # it is NOT alive
1066 stopserver($server, $pid2);
1067 $doesntrun{$pidfile} = 1;
1068 $http2pid = $pid2 = 0;
1069 logmsg "RUN: failed to start the $srvrname server\n";
1070 return (3, 0, 0, 0, 0);
1072 $doesntrun{$pidfile} = 0;
1074 if($verb) {
1075 logmsg "RUN: $srvrname server PID $http2pid " .
1076 "http-port $port https-port $port2 " .
1077 "backend $HOSTIP:" . protoport("http") . "\n";
1080 return (0 + !$http2pid, $http2pid, $pid2, $port, $port2);
1083#######################################################################
1084# start the http3 server
1086sub runhttp3server {
1087 my ($verb, $cert) = @_;
1088 my $proto = "http/3";
1089 my $ipvnum = 4;
1090 my $idnum = 0;
1091 my $exe = "$perl " . shell_quote("$srcdir/http3-server.pl");
1092 my $verbose_flag = "--verbose ";
1093 my $nghttpx = $ENV{'NGHTTPX'};
1095 if(!$nghttpx) {
1096 logmsg "runhttp3server: NGHTTPX env is not set\n";
1097 return (2, 0, 0, 0);
1100 my $server = servername_id($proto, $ipvnum, $idnum);
1102 my $pidfile = $serverpidfile{$server};
1104 # do not retry if the server does not work
1105 if($doesntrun{$pidfile}) {
1106 return (2, 0, 0, 0);
1109 my $pid = processexists($pidfile);
1110 if($pid > 0) {
1111 stopserver($server, $pid);
1113 unlink($pidfile) if(-f $pidfile);
1115 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1116 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1118 my $flags = "";
1119 $flags .= "--nghttpx \"$nghttpx\" ";
1120 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1121 $flags .= "--logdir \"$LOGDIR\" ";
1122 $flags .= "--connect $HOSTIP:" . protoport("http") . " ";
1123 $flags .= "--cert \"$cert\" " if($cert);
1124 $flags .= $verbose_flag if($debugprotocol);
1126 my $port = getfreeport($ipvnum, 0);
1127 my $cmd = "$exe --port \"$port\" $flags";
1128 my ($http3pid, $pid3) = startnew($cmd, $pidfile, 15, 0);
1130 if($http3pid <= 0 || !pidexists($http3pid)) {
1131 # it is NOT alive
1132 stopserver($server, $pid3);
1133 $doesntrun{$pidfile} = 1;
1134 $http3pid = $pid3 = 0;
1135 logmsg "RUN: failed to start the $srvrname server\n";
1136 return (3, 0, 0, 0);
1138 $doesntrun{$pidfile} = 0;
1140 if($verb) {
1141 logmsg "RUN: $srvrname server PID $http3pid port $port\n";
1144 return (0 + !$http3pid, $http3pid, $pid3, $port);
1147#######################################################################
1148# start the https stunnel based server
1150sub runhttpsserver {
1151 my ($verb, $proto, $proxy, $certfile) = @_;
1152 my $ip = $HOSTIP;
1153 my $ipvnum = 4;
1154 my $idnum = 1;
1156 if($proxy eq "proxy") {
1157 # the https-proxy runs as https2
1158 $idnum = 2;
1161 if(!$stunnel) {
1162 return (4, 0, 0, 0);
1165 my $server = servername_id($proto, $ipvnum, $idnum);
1167 my $pidfile = $serverpidfile{$server};
1169 # do not retry if the server does not work
1170 if($doesntrun{$pidfile}) {
1171 return (2, 0, 0, 0);
1174 my $pid = processexists($pidfile);
1175 if($pid > 0) {
1176 stopserver($server, $pid);
1178 unlink($pidfile) if(-f $pidfile);
1180 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1181 $certfile = 'certs/test-localhost.pem' unless($certfile);
1182 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1184 my $flags = "";
1185 $flags .= "--verbose " if($debugprotocol);
1186 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1187 $flags .= "--logdir \"$LOGDIR\" ";
1188 $flags .= "--id $idnum " if($idnum > 1);
1189 $flags .= "--ipv$ipvnum --proto $proto ";
1190 $flags .= "--certfile \"$certfile\" " if($certfile ne 'certs/test-localhost.pem');
1191 $flags .= "--stunnel \"$stunnel\" --srcdir \"$srcdir\" ";
1192 if($proto eq "https-mtls") {
1193 $flags .= "--mtls ";
1195 if($proto eq "gophers") {
1196 $flags .= "--connect " . protoport("gopher");
1198 elsif($proto eq "mqtts") {
1199 $flags .= "--connect " . protoport("mqtt");
1201 elsif(!$proxy) {
1202 $flags .= "--connect " . protoport("http");
1204 else {
1205 # for HTTPS-proxy we connect to the HTTP proxy
1206 $flags .= "--connect " . protoport("httpproxy");
1209 my $port = getfreeport($ipvnum, 1);
1210 my $options = "$flags --accept \"$port\"";
1211 my $cmd = "$perl " . shell_quote("$srcdir/secureserver.pl") . " " . $options;
1212 my ($httpspid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1214 if($httpspid <= 0 || !pidexists($httpspid)) {
1215 # it is NOT alive
1216 # do not call stopserver since that also kills the dependent
1217 # server that has already been started properly
1218 $doesntrun{$pidfile} = 1;
1219 $httpspid = $pid2 = 0;
1220 logmsg "RUN: failed to start the $srvrname server\n";
1221 return (3, 0, 0, 0);
1224 $doesntrun{$pidfile} = 0;
1225 # we have a server!
1226 if($verb) {
1227 logmsg "RUN: $srvrname server is PID $httpspid port $port\n";
1230 $runcert{$server} = $certfile;
1232 return (0 + !$httpspid, $httpspid, $pid2, $port);
1235#######################################################################
1236# start the pingpong server (FTP, POP3, IMAP, SMTP)
1238sub runpingpongserver {
1239 my ($proto, $id, $verb, $ipv6) = @_;
1241 # Check the requested server
1242 if($proto !~ /^(?:ftp|imap|pop3|smtp)$/) {
1243 logmsg "Unsupported protocol $proto!!\n";
1244 return (4, 0, 0);
1247 my $ip = ($ipv6 && ($ipv6 =~ /6$/)) ? $HOST6IP : $HOSTIP;
1248 my $ipvnum = ($ipv6 && ($ipv6 =~ /6$/)) ? 6 : 4;
1249 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1251 my $server = servername_id($proto, $ipvnum, $idnum);
1253 my $pidfile = $serverpidfile{$server};
1254 my $portfile = $serverportfile{$server};
1256 # do not retry if the server does not work
1257 if($doesntrun{$pidfile}) {
1258 return (2, 0, 0);
1261 my $pid = processexists($pidfile);
1262 if($pid > 0) {
1263 stopserver($server, $pid);
1265 unlink($pidfile) if(-f $pidfile);
1267 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1268 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1270 my $flags = "";
1271 $flags .= "--verbose " if($debugprotocol);
1272 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1273 $flags .= "--logdir \"$LOGDIR\" ";
1274 $flags .= "--portfile \"$portfile\" ";
1275 $flags .= "--srcdir \"$srcdir\" --proto $proto ";
1276 $flags .= "--id $idnum " if($idnum > 1);
1277 $flags .= "--ipv$ipvnum --port 0 --addr \"$ip\"";
1279 unlink($portfile); # need to see a new one
1280 my $cmd = "$perl " . shell_quote("$srcdir/ftpserver.pl") . " " . $flags;
1281 my ($ftppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1283 if($ftppid <= 0 || !pidexists($ftppid)) {
1284 # it is NOT alive
1285 logmsg "RUN: failed to start the $srvrname server\n";
1286 stopserver($server, $pid2);
1287 $doesntrun{$pidfile} = 1;
1288 return (1, 0, 0);
1291 # where is it?
1292 my $port = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1293 if(!$port) {
1294 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1295 stopserver($server, $pid2);
1296 $doesntrun{$pidfile} = 1;
1297 return (1, 0, 0, 0);
1300 logmsg "PINGPONG runs on port $port ($portfile)\n" if($verb);
1302 logmsg "RUN: $srvrname server is PID $ftppid port $port\n" if($verb);
1304 # Assign the correct port variable!
1305 $PORT{$proto . ($ipvnum == 6? '6': '')} = $port;
1307 return (0, $pid2, $ftppid);
1310#######################################################################
1311# start the ftps/imaps/pop3s/smtps server (or rather, tunnel)
1313sub runsecureserver {
1314 my ($verb, $ipv6, $certfile, $proto, $clearport) = @_;
1315 my $ip = ($ipv6 && ($ipv6 =~ /6$/)) ? $HOST6IP : $HOSTIP;
1316 my $ipvnum = ($ipv6 && ($ipv6 =~ /6$/)) ? 6 : 4;
1317 my $idnum = 1;
1319 if(!$stunnel) {
1320 return (4, 0, 0, 0);
1323 my $server = servername_id($proto, $ipvnum, $idnum);
1325 my $pidfile = $serverpidfile{$server};
1327 # do not retry if the server does not work
1328 if($doesntrun{$pidfile}) {
1329 return (2, 0, 0, 0);
1332 my $pid = processexists($pidfile);
1333 if($pid > 0) {
1334 stopserver($server, $pid);
1336 unlink($pidfile) if(-f $pidfile);
1338 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1339 $certfile = 'certs/test-localhost.pem' unless($certfile);
1340 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1342 my $flags = "";
1343 $flags .= "--verbose " if($debugprotocol);
1344 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1345 $flags .= "--logdir \"$LOGDIR\" ";
1346 $flags .= "--id $idnum " if($idnum > 1);
1347 $flags .= "--ipv$ipvnum --proto $proto ";
1348 $flags .= "--certfile \"$certfile\" " if($certfile ne 'certs/test-localhost.pem');
1349 $flags .= "--stunnel \"$stunnel\" --srcdir \"$srcdir\" ";
1350 $flags .= "--connect $clearport";
1352 my $port = getfreeport($ipvnum, 1);
1353 my $options = "$flags --accept \"$port\"";
1355 my $cmd = "$perl " . shell_quote("$srcdir/secureserver.pl") . " " . $options;
1356 my ($protospid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1358 if($protospid <= 0 || !pidexists($protospid)) {
1359 # it is NOT alive
1360 # do not call stopserver since that also kills the dependent
1361 # server that has already been started properly
1362 $doesntrun{$pidfile} = 1;
1363 $protospid = $pid2 = 0;
1364 logmsg "RUN: failed to start the $srvrname server\n";
1365 return (3, 0, 0, 0);
1368 $doesntrun{$pidfile} = 0;
1369 $runcert{$server} = $certfile;
1371 if($verb) {
1372 logmsg "RUN: $srvrname server is PID $protospid port $port\n";
1375 return (0 + !$protospid, $protospid, $pid2, $port);
1378#######################################################################
1379# start the tftp server
1381sub runtftpserver {
1382 my ($id, $verb, $ipv6) = @_;
1383 my $ip = $HOSTIP;
1384 my $proto = 'tftp';
1385 my $ipvnum = 4;
1386 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1388 if($ipv6) {
1389 # if IPv6, use a different setup
1390 $ipvnum = 6;
1391 $ip = $HOST6IP;
1394 my $server = servername_id($proto, $ipvnum, $idnum);
1396 my $pidfile = $serverpidfile{$server};
1398 # do not retry if the server does not work
1399 if($doesntrun{$pidfile}) {
1400 return (2, 0, 0, 0);
1403 my $pid = processexists($pidfile);
1404 if($pid > 0) {
1405 stopserver($server, $pid);
1407 unlink($pidfile) if(-f $pidfile);
1409 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1410 my $portfile = $serverportfile{$server};
1411 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1413 my $flags = "";
1414 $flags .= "--verbose " if($debugprotocol);
1415 $flags .= "--pidfile \"$pidfile\" ";
1416 $flags .= "--portfile \"$portfile\" ";
1417 $flags .= "--logfile \"$logfile\" ";
1418 $flags .= "--logdir \"$LOGDIR\" ";
1419 $flags .= "--id $idnum " if($idnum > 1);
1420 $flags .= "--ipv$ipvnum --port 0 --srcdir \"$srcdir\"";
1422 unlink($portfile); # need to see a new one
1423 my $cmd = "$perl " . shell_quote("$srcdir/tftpserver.pl") . " " . $flags;
1424 my ($tftppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1426 if($tftppid <= 0 || !pidexists($tftppid)) {
1427 # it is NOT alive
1428 logmsg "RUN: failed to start the $srvrname server\n";
1429 stopserver($server, $pid2);
1430 $doesntrun{$pidfile} = 1;
1431 return (1, 0, 0, 0);
1434 my $port = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1435 if(!$port) {
1436 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1437 stopserver($server, $pid2);
1438 $doesntrun{$pidfile} = 1;
1439 return (1, 0, 0, 0);
1442 if($verb) {
1443 logmsg "RUN: $srvrname server on PID $tftppid port $port\n";
1446 return (0, $pid2, $tftppid, $port);
1449#######################################################################
1450# start the dns server
1452sub rundnsserver {
1453 my ($id, $verb, $ipv6) = @_;
1454 my $ip = $HOSTIP;
1455 my $proto = 'dns';
1456 my $ipvnum = 4;
1457 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1459 if($ipv6) {
1460 # if IPv6, use a different setup
1461 $ipvnum = 6;
1462 $ip = $HOST6IP;
1465 my $server = servername_id($proto, $ipvnum, $idnum);
1467 my $pidfile = $serverpidfile{$server};
1469 # do not retry if the server does not work
1470 if($doesntrun{$pidfile}) {
1471 return (2, 0, 0, 0);
1474 my $pid = processexists($pidfile);
1475 if($pid > 0) {
1476 stopserver($server, $pid);
1478 unlink($pidfile) if(-f $pidfile);
1480 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1481 my $portfile = $serverportfile{$server};
1482 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1484 my $cmd = server_exe('dnsd');
1485 $cmd .= " --port 0";
1486 $cmd .= " --verbose" if($debugprotocol);
1487 $cmd .= " --pidfile \"$pidfile\"";
1488 $cmd .= " --portfile \"$portfile\"";
1489 $cmd .= " --logfile \"$logfile\"";
1490 $cmd .= " --logdir \"$LOGDIR\"";
1491 $cmd .= " --id $idnum" if($idnum > 1);
1492 $cmd .= " --ipv$ipvnum";
1494 unlink($portfile); # need to see a new one
1495 # start DNS server on a random port
1496 my ($dnspid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1498 if($dnspid <= 0 || !pidexists($dnspid)) {
1499 # it is NOT alive
1500 logmsg "RUN: failed to start the $srvrname server\n";
1501 stopserver($server, $pid2);
1502 $doesntrun{$pidfile} = 1;
1503 return (1, 0, 0, 0);
1506 my $port = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1507 if(!$port) {
1508 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1509 stopserver($server, $pid2);
1510 $doesntrun{$pidfile} = 1;
1511 return (1, 0, 0, 0);
1514 if($verb) {
1515 logmsg "RUN: $srvrname server on PID $dnspid port $port\n";
1518 return (0, $pid2, $dnspid, $port);
1521#######################################################################
1522# start the rtsp server
1524sub runrtspserver {
1525 my ($verb, $ipv6) = @_;
1526 my $ip = $HOSTIP;
1527 my $proto = 'rtsp';
1528 my $ipvnum = 4;
1529 my $idnum = 1;
1531 if($ipv6) {
1532 # if IPv6, use a different setup
1533 $ipvnum = 6;
1534 $ip = $HOST6IP;
1537 my $server = servername_id($proto, $ipvnum, $idnum);
1539 my $pidfile = $serverpidfile{$server};
1540 my $portfile = $serverportfile{$server};
1542 # do not retry if the server does not work
1543 if($doesntrun{$pidfile}) {
1544 return (2, 0, 0, 0);
1547 my $pid = processexists($pidfile);
1548 if($pid > 0) {
1549 stopserver($server, $pid);
1551 unlink($pidfile) if(-f $pidfile);
1553 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1554 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1556 my $flags = "";
1557 $flags .= "--verbose " if($debugprotocol);
1558 $flags .= "--pidfile \"$pidfile\" ";
1559 $flags .= "--portfile \"$portfile\" ";
1560 $flags .= "--logfile \"$logfile\" ";
1561 $flags .= "--logdir \"$LOGDIR\" ";
1562 $flags .= "--id $idnum " if($idnum > 1);
1563 $flags .= "--ipv$ipvnum --port 0 --srcdir \"$srcdir\"";
1565 unlink($portfile); # need to see a new one
1566 my $cmd = "$perl " . shell_quote("$srcdir/rtspserver.pl") . " " . $flags;
1567 my ($rtsppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1569 if($rtsppid <= 0 || !pidexists($rtsppid)) {
1570 # it is NOT alive
1571 logmsg "RUN: failed to start the $srvrname server\n";
1572 stopserver($server, $pid2);
1573 $doesntrun{$pidfile} = 1;
1574 return (1, 0, 0, 0);
1577 my $port = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1578 if(!$port) {
1579 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1580 stopserver($server, $pid2);
1581 $doesntrun{$pidfile} = 1;
1582 return (1, 0, 0, 0);
1585 if($verb) {
1586 logmsg "RUN: $srvrname server PID $rtsppid port $port\n";
1589 return (0, $rtsppid, $pid2, $port);
1592#***************************************************************************
1593# Return key algorithm string
1595sub sshkeyalgostr {
1596 my ($algo) = @_;
1597 my %algomap = (
1598 ecdsa => 'ecdsa-sha2-nistp256',
1599 );
1600 return exists $algomap{$algo} ? $algomap{$algo} : 'ssh-' . $algo;
1603#######################################################################
1604# Start the ssh (scp/sftp) server
1606sub runsshserver {
1607 my ($id, $verb, $ipv6) = @_;
1608 my $ip = $HOSTIP;
1609 my $proto = 'ssh';
1610 my $ipvnum = 4;
1611 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1613 if(!$USER) {
1614 logmsg "Cannot start ssh server due to lack of username\n";
1615 return (4, 0, 0, 0);
1618 my $server = servername_id($proto, $ipvnum, $idnum);
1620 my $pidfile = $serverpidfile{$server};
1622 # do not retry if the server does not work
1623 if($doesntrun{$pidfile}) {
1624 return (2, 0, 0, 0);
1627 my $sshd = find_sshd();
1628 if($sshd) {
1629 ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd);
1630 logmsg $sshderror if($sshderror);
1633 my $pid = processexists($pidfile);
1634 if($pid > 0) {
1635 stopserver($server, $pid);
1637 unlink($pidfile) if(-f $pidfile);
1639 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1640 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1642 my $flags = "";
1643 $flags .= "--verbose " if($verb);
1644 $flags .= "--debugprotocol " if($debugprotocol);
1645 $flags .= "--pidfile \"$pidfile\" ";
1646 $flags .= "--logdir \"$LOGDIR\" ";
1647 $flags .= "--id $idnum " if($idnum > 1);
1648 $flags .= "--ipv$ipvnum --addr \"$ip\" ";
1649 $flags .= "--user \"$USER\"";
1650 if(defined $feature{"sshkeyalgo"}) {
1651 $flags .= ' --keyalgo ' . $feature{"sshkeyalgo"};
1654 my @tports;
1655 my $port = getfreeport($ipvnum, 1);
1657 push @tports, $port;
1659 my $options = "$flags --sshport \"$port\"";
1661 my $cmd = "$perl " . shell_quote("$srcdir/sshserver.pl") . " " . $options;
1662 my ($sshpid, $pid2) = startnew($cmd, $pidfile, 60, 0);
1664 # on loaded systems sshserver start up can take longer than the
1665 # timeout passed to startnew, when this happens startnew completes
1666 # without being able to read the pidfile and consequently returns a
1667 # zero pid2 above.
1668 if($sshpid <= 0 || !pidexists($sshpid)) {
1669 # it is NOT alive
1670 stopserver($server, $pid2);
1671 $doesntrun{$pidfile} = 1;
1672 $sshpid = $pid2 = 0;
1673 logmsg "RUN: failed to start the $srvrname server on $port\n";
1674 return (3, 0, 0, 0);
1677 # once it is known that the ssh server is alive, sftp server
1678 # verification is performed actually connecting to it, authenticating
1679 # and performing a simple remote command. This verification is tried
1680 # only one time.
1682 $sshdlog = server_logfilename($LOGDIR, 'ssh', $ipvnum, $idnum);
1683 $sftplog = server_logfilename($LOGDIR, 'sftp', $ipvnum, $idnum);
1685 if(verifysftp('sftp', $ipvnum, $idnum, $ip, $port) < 1) {
1686 logmsg "RUN: SFTP server failed verification\n";
1687 # failed to talk to it properly. Kill the server and return failure
1688 display_sftplog();
1689 display_sftpconfig();
1690 display_sshdlog();
1691 display_sshdconfig();
1692 stopserver($server, "$sshpid $pid2");
1693 $doesntrun{$pidfile} = 1;
1694 $sshpid = $pid2 = 0;
1695 logmsg "RUN: failed to verify the $srvrname server on $port\n";
1696 return (5, 0, 0, 0);
1698 # we are happy, no need to loop anymore!
1699 $doesntrun{$pidfile} = 0;
1701 my $hostfile;
1702 if(!open($hostfile, "<", "$LOGDIR/$PIDDIR/$hstpubmd5f") ||
1703 (read($hostfile, $SSHSRVMD5, 32) != 32) ||
1704 !close($hostfile) ||
1705 ($SSHSRVMD5 !~ /^[a-f0-9]{32}$/i))
1707 my $msg = "Fatal: $srvrname pubkey MD5 missing : \"$hstpubmd5f\" : $!";
1708 logmsg "$msg\n";
1709 stopservers($verb);
1710 die $msg;
1713 if(!open($hostfile, "<", "$LOGDIR/$PIDDIR/$hstpubsha256f") ||
1714 (read($hostfile, $SSHSRVSHA256, 48) == 0) ||
1715 !close($hostfile))
1717 my $msg = "Fatal: $srvrname pubkey SHA256 missing : \"$hstpubsha256f\" : $!";
1718 logmsg "$msg\n";
1719 stopservers($verb);
1720 die $msg;
1723 logmsg "RUN: $srvrname on PID $pid2 port $port\n" if($verb);
1725 return (0, $pid2, $sshpid, $port);
1728#######################################################################
1729# Start the MQTT server
1731sub runmqttserver {
1732 my ($id, $verb, $ipv6) = @_;
1733 my $ip = $HOSTIP;
1734 my $proto = 'mqtt';
1735 my $port = protoport($proto);
1736 my $ipvnum = 4;
1737 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1739 my $server = servername_id($proto, $ipvnum, $idnum);
1740 my $pidfile = $serverpidfile{$server};
1741 my $portfile = $serverportfile{$server};
1743 # do not retry if the server does not work
1744 if($doesntrun{$pidfile}) {
1745 return (2, 0, 0);
1748 my $pid = processexists($pidfile);
1749 if($pid > 0) {
1750 stopserver($server, $pid);
1752 unlink($pidfile) if(-f $pidfile);
1754 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1755 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1757 unlink($portfile); # need to see a new one
1758 # start our MQTT server - on a random port!
1759 my $cmd = server_exe('mqttd') .
1760 " --port 0" .
1761 " --pidfile $pidfile" .
1762 " --portfile $portfile" .
1763 " --config $LOGDIR/$SERVERCMD" .
1764 " --logfile $logfile" .
1765 " --logdir $LOGDIR";
1766 my ($sockspid, $pid2) = startnew($cmd, $pidfile, 30, 0);
1768 if($sockspid <= 0 || !pidexists($sockspid)) {
1769 # it is NOT alive
1770 logmsg "RUN: failed to start the $srvrname server\n";
1771 stopserver($server, $pid2);
1772 $doesntrun{$pidfile} = 1;
1773 return (1, 0, 0);
1776 my $mqttport = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1777 if(!$mqttport) {
1778 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1779 stopserver($server, $pid2);
1780 $doesntrun{$pidfile} = 1;
1781 return (1, 0, 0, 0);
1784 if($verb) {
1785 logmsg "RUN: $srvrname server is now running PID $pid2 on PORT $mqttport\n";
1788 return (0, $pid2, $sockspid, $mqttport);
1791#######################################################################
1792# Start the socks server
1794sub runsocksserver {
1795 my ($id, $verb, $ipv6, $is_unix) = @_;
1796 my $ip = $HOSTIP;
1797 my $proto = 'socks';
1798 my $ipvnum = 4;
1799 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
1801 my $server = servername_id($proto, $ipvnum, $idnum);
1803 my $pidfile = $serverpidfile{$server};
1805 # do not retry if the server does not work
1806 if($doesntrun{$pidfile}) {
1807 return (2, 0, 0, 0);
1810 my $pid = processexists($pidfile);
1811 if($pid > 0) {
1812 stopserver($server, $pid);
1814 unlink($pidfile) if(-f $pidfile);
1816 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1817 my $portfile = $serverportfile{$server};
1818 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1820 unlink($portfile); # need to see a new one
1821 # start our socks server, get commands from the FTP cmd file
1822 my $cmd = "";
1823 if($is_unix) {
1824 $cmd = server_exe('socksd') .
1825 " --pidfile $pidfile" .
1826 " --reqfile $LOGDIR/$SOCKSIN" .
1827 " --logfile $logfile" .
1828 " --unix-socket $SOCKSUNIXPATH" .
1829 " --backend $HOSTIP" .
1830 " --config $LOGDIR/$SERVERCMD";
1831 $portfile = "none";
1833 else {
1834 $cmd = server_exe('socksd') .
1835 " --port 0" .
1836 " --pidfile $pidfile" .
1837 " --portfile $portfile" .
1838 " --reqfile $LOGDIR/$SOCKSIN" .
1839 " --logfile $logfile" .
1840 " --backend $HOSTIP" .
1841 " --config $LOGDIR/$SERVERCMD";
1843 my ($sockspid, $pid2) = startnew($cmd, $pidfile, 30, 0);
1845 if($sockspid <= 0 || !pidexists($sockspid)) {
1846 # it is NOT alive
1847 logmsg "RUN: failed to start the $srvrname server\n";
1848 stopserver($server, $pid2);
1849 $doesntrun{$pidfile} = 1;
1850 return (1, 0, 0, 0);
1853 my $port = 0;
1854 if($portfile ne "none") {
1855 $port = pidfromfile($portfile, $SERVER_TIMEOUT_SEC);
1856 if(!$port) {
1857 logmsg "RUN: timeout for $srvrname to produce port file $portfile\n";
1858 stopserver($server, $pid2);
1859 $doesntrun{$pidfile} = 1;
1860 return (1, 0, 0, 0);
1864 if($verb) {
1865 logmsg "RUN: $srvrname server is now running PID $pid2\n";
1868 return (0, $pid2, $sockspid, $port);
1871#######################################################################
1872# start the dict server
1874sub rundictserver {
1875 my ($verb, $alt) = @_;
1876 my $proto = "dict";
1877 my $ip = $HOSTIP;
1878 my $ipvnum = 4;
1879 my $idnum = 1;
1881 if($alt eq "ipv6") {
1882 # No IPv6
1885 my $server = servername_id($proto, $ipvnum, $idnum);
1887 my $pidfile = $serverpidfile{$server};
1889 # do not retry if the server does not work
1890 if($doesntrun{$pidfile}) {
1891 return (2, 0, 0, 0);
1894 my $pid = processexists($pidfile);
1895 if($pid > 0) {
1896 stopserver($server, $pid);
1898 unlink($pidfile) if(-f $pidfile);
1900 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1901 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1903 my $flags = "";
1904 $flags .= "--verbose 1 " if($debugprotocol);
1905 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1906 $flags .= "--id $idnum " if($idnum > 1);
1907 $flags .= "--srcdir \"$srcdir\" ";
1908 $flags .= "--host $HOSTIP";
1910 my $port = getfreeport($ipvnum, 1);
1911 my $cmd = "$srcdir/dictserver.py --port \"$port\" $flags";
1912 my ($dictpid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1914 if($dictpid <= 0 || !pidexists($dictpid)) {
1915 # it is NOT alive
1916 stopserver($server, $pid2);
1917 $doesntrun{$pidfile} = 1;
1918 $dictpid = $pid2 = 0;
1919 logmsg "RUN: failed to start the $srvrname server\n";
1920 return (3, 0, 0, 0);
1922 $doesntrun{$pidfile} = 0;
1924 if($verb) {
1925 logmsg "RUN: $srvrname server PID $dictpid port $port\n";
1928 return (0 + !$dictpid, $dictpid, $pid2, $port);
1931#######################################################################
1932# start the telnet server
1934sub runnegtelnetserver {
1935 my ($verb, $alt) = @_;
1936 my $proto = "telnet";
1937 my $ip = $HOSTIP;
1938 my $ipvnum = 4;
1939 my $idnum = 1;
1941 if($alt eq "ipv6") {
1942 # No IPv6
1945 my $server = servername_id($proto, $ipvnum, $idnum);
1947 my $pidfile = $serverpidfile{$server};
1949 # do not retry if the server does not work
1950 if($doesntrun{$pidfile}) {
1951 return (2, 0, 0, 0);
1954 my $pid = processexists($pidfile);
1955 if($pid > 0) {
1956 stopserver($server, $pid);
1958 unlink($pidfile) if(-f $pidfile);
1960 my $srvrname = servername_str($proto, $ipvnum, $idnum);
1961 my $logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
1963 my $flags = "";
1964 $flags .= "--verbose 1 " if($debugprotocol);
1965 $flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
1966 $flags .= "--id $idnum " if($idnum > 1);
1967 $flags .= "--srcdir \"$srcdir\"";
1969 my $port = getfreeport($ipvnum, 1);
1970 my $cmd = "$srcdir/negtelnetserver.py --port \"$port\" $flags";
1971 my ($ntelpid, $pid2) = startnew($cmd, $pidfile, 15, 0);
1973 if($ntelpid <= 0 || !pidexists($ntelpid)) {
1974 # it is NOT alive
1975 stopserver($server, $pid2);
1976 $doesntrun{$pidfile} = 1;
1977 $ntelpid = $pid2 = 0;
1978 logmsg "RUN: failed to start the $srvrname server\n";
1979 return (3, 0, 0, 0);
1981 $doesntrun{$pidfile} = 0;
1983 if($verb) {
1984 logmsg "RUN: $srvrname server PID $ntelpid port $port\n";
1987 return (0 + !$ntelpid, $ntelpid, $pid2, $port);
1990#######################################################################
1991# Single shot http and gopher server responsiveness test. This should only
1992# be used to verify that a server present in %run hash is still functional
1994sub responsive_http_server {
1995 my ($proto, $verb, $alt, $port_or_path, $do_http3) = @_;
1996 my $ip = $HOSTIP;
1997 my $ipvnum = 4;
1998 my $idnum = 1;
2000 if($alt eq "ipv6") {
2001 # if IPv6, use a different setup
2002 $ipvnum = 6;
2003 $ip = $HOST6IP;
2005 elsif($alt eq "proxy") {
2006 $idnum = 2;
2008 elsif($alt eq "unix") {
2009 # IP (protocol) is mutually exclusive with Unix sockets
2010 $ipvnum = "unix";
2013 return &responsiveserver($proto, $ipvnum, $idnum, $ip, $port_or_path, $do_http3);
2016#######################################################################
2017# Single shot mqtt server responsiveness test. This should only
2018# be used to verify that a server present in %run hash is still functional
2020sub responsive_mqtt_server {
2021 my ($proto, $id, $verb, $ipv6) = @_;
2022 my $ip = ($ipv6 && ($ipv6 =~ /6$/)) ? $HOST6IP : $HOSTIP;
2023 my $ipvnum = ($ipv6 && ($ipv6 =~ /6$/)) ? 6 : 4;
2024 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
2026 return &responsiveserver($proto, $ipvnum, $idnum, $ip);
2029#######################################################################
2030# Single shot pingpong server responsiveness test. This should only be
2031# used to verify that a server present in %run hash is still functional
2033sub responsive_pingpong_server {
2034 my ($proto, $id, $verb, $ipv6) = @_;
2035 my $port;
2036 my $ip = ($ipv6 && ($ipv6 =~ /6$/)) ? $HOST6IP : $HOSTIP;
2037 my $ipvnum = ($ipv6 && ($ipv6 =~ /6$/)) ? 6 : 4;
2038 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
2039 my $protoip = $proto . ($ipvnum == 6? '6': '');
2041 if($proto =~ /^(?:ftp|imap|pop3|smtp)$/) {
2042 $port = protoport($protoip);
2044 else {
2045 logmsg "Unsupported protocol $proto!!\n";
2046 return 0;
2049 return &responsiveserver($proto, $ipvnum, $idnum, $ip, $port);
2052#######################################################################
2053# Single shot rtsp server responsiveness test. This should only be
2054# used to verify that a server present in %run hash is still functional
2056sub responsive_rtsp_server {
2057 my ($verb, $ipv6) = @_;
2058 my $proto = 'rtsp';
2059 my $port = protoport($proto);
2060 my $ip = $HOSTIP;
2061 my $ipvnum = 4;
2062 my $idnum = 1;
2064 if($ipv6) {
2065 # if IPv6, use a different setup
2066 $ipvnum = 6;
2067 $port = protoport('rtsp6');
2068 $ip = $HOST6IP;
2071 return &responsiveserver($proto, $ipvnum, $idnum, $ip, $port);
2074#######################################################################
2075# Single shot tftp server responsiveness test. This should only be
2076# used to verify that a server present in %run hash is still functional
2078sub responsive_tftp_server {
2079 my ($id, $verb, $ipv6) = @_;
2080 my $proto = 'tftp';
2081 my $port = protoport($proto);
2082 my $ip = $HOSTIP;
2083 my $ipvnum = 4;
2084 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
2086 if($ipv6) {
2087 # if IPv6, use a different setup
2088 $ipvnum = 6;
2089 $port = protoport('tftp6');
2090 $ip = $HOST6IP;
2093 return &responsiveserver($proto, $ipvnum, $idnum, $ip, $port);
2096#######################################################################
2097# Single shot dns server responsiveness test. This should only be
2098# used to verify that a server present in %run hash is still functional
2100sub responsive_dns_server {
2101 my ($id, $verb, $ipv6) = @_;
2102 my $proto = 'dns';
2103 my $port = protoport($proto);
2104 my $ip = $HOSTIP;
2105 my $ipvnum = 4;
2106 my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
2108 if($ipv6) {
2109 # if IPv6, use a different setup
2110 $ipvnum = 6;
2111 $port = protoport('dns6');
2112 $ip = $HOST6IP;
2115 return &responsiveserver($proto, $ipvnum, $idnum, $ip, $port);
2118#######################################################################
2119# startservers() starts all the named servers
2121# Returns: string with error reason or blank for success, and an integer:
2122# 0 for success
2123# 1 for an error starting the server
2124# 2 for not the first time getting an error starting the server
2125# 3 for a failure to stop a server in order to restart it
2126# 4 for an unsupported server type
2128sub startservers {
2129 my @what = @_;
2130 my ($pid, $pid2);
2131 my $serr; # error while starting a server (as of the return enumerations)
2132 for(@what) {
2133 my (@whatlist) = split(/\s+/,$_);
2134 my $what = lc($whatlist[0]);
2135 $what =~ s/[^a-z0-9\/-]//g;
2137 my $certfile;
2138 if($what =~ /^(ftp|gopher|http|imap|pop3|smtp)s|https-mtls((\d*)(-ipv6|-unix|))$/) {
2139 $certfile = ($whatlist[1]) ? $whatlist[1] : 'certs/test-localhost.pem';
2142 if(($what eq "pop3") ||
2143 ($what eq "ftp") ||
2144 ($what eq "imap") ||
2145 ($what eq "smtp")) {
2146 if($run{$what} &&
2147 !responsive_pingpong_server($what, "", $verbose)) {
2148 if(stopserver($what)) {
2149 return ("failed stopping unresponsive " . uc($what) . " server", 3);
2152 if(!$run{$what}) {
2153 ($serr, $pid, $pid2) = runpingpongserver($what, "", $verbose);
2154 if($pid <= 0) {
2155 return ("failed starting " . uc($what) . " server", $serr);
2157 logmsg sprintf("* pid $what => %d %d\n", $pid, $pid2) if($verbose);
2158 $run{$what} = "$pid $pid2";
2161 elsif($what eq "ftp-ipv6") {
2162 if($run{'ftp-ipv6'} &&
2163 !responsive_pingpong_server("ftp", "", $verbose, "ipv6")) {
2164 if(stopserver('ftp-ipv6')) {
2165 return ("failed stopping unresponsive FTP-IPv6 server", 3);
2168 if(!$run{'ftp-ipv6'}) {
2169 ($serr, $pid, $pid2) = runpingpongserver("ftp", "", $verbose, "ipv6");
2170 if($pid <= 0) {
2171 return ("failed starting FTP-IPv6 server", $serr);
2173 logmsg sprintf("* pid ftp-ipv6 => %d %d\n", $pid,
2174 $pid2) if($verbose);
2175 $run{'ftp-ipv6'} = "$pid $pid2";
2178 elsif($what eq "gopher") {
2179 if($run{'gopher'} &&
2180 !responsive_http_server("gopher", $verbose, 0,
2181 protoport("gopher"))) {
2182 if(stopserver('gopher')) {
2183 return ("failed stopping unresponsive GOPHER server", 3);
2186 if(!$run{'gopher'}) {
2187 ($serr, $pid, $pid2, $PORT{'gopher'}) =
2188 runhttpserver("gopher", $verbose, 0);
2189 if($pid <= 0) {
2190 return ("failed starting GOPHER server", $serr);
2192 logmsg sprintf ("* pid gopher => %d %d\n", $pid, $pid2)
2193 if($verbose);
2194 $run{'gopher'} = "$pid $pid2";
2197 elsif($what eq "gopher-ipv6") {
2198 if($run{'gopher-ipv6'} &&
2199 !responsive_http_server("gopher", $verbose, "ipv6",
2200 protoport("gopher6"))) {
2201 if(stopserver('gopher-ipv6')) {
2202 return ("failed stopping unresponsive GOPHER-IPv6 server", 3);
2205 if(!$run{'gopher-ipv6'}) {
2206 ($serr, $pid, $pid2, $PORT{"gopher6"}) =
2207 runhttpserver("gopher", $verbose, "ipv6");
2208 if($pid <= 0) {
2209 return ("failed starting GOPHER-IPv6 server", $serr);
2211 logmsg sprintf("* pid gopher-ipv6 => %d %d\n", $pid,
2212 $pid2) if($verbose);
2213 $run{'gopher-ipv6'} = "$pid $pid2";
2216 elsif($what eq "http") {
2217 if($run{'http'} &&
2218 !responsive_http_server("http", $verbose, 0, protoport('http'))) {
2219 logmsg "* restarting unresponsive HTTP server\n";
2220 if(stopserver('http')) {
2221 return ("failed stopping unresponsive HTTP server", 3);
2224 if(!$run{'http'}) {
2225 ($serr, $pid, $pid2, $PORT{'http'}) =
2226 runhttpserver("http", $verbose, 0);
2227 if($pid <= 0) {
2228 return ("failed starting HTTP server (for http)", $serr);
2230 logmsg sprintf ("* pid http => %d %d\n", $pid, $pid2)
2231 if($verbose);
2232 $run{'http'} = "$pid $pid2";
2235 elsif($what eq "http-proxy") {
2236 if($run{'http-proxy'} &&
2237 !responsive_http_server("http", $verbose, "proxy",
2238 protoport("httpproxy"))) {
2239 if(stopserver('http-proxy')) {
2240 return ("failed stopping unresponsive HTTP-proxy server", 3);
2243 if(!$run{'http-proxy'}) {
2244 ($serr, $pid, $pid2, $PORT{"httpproxy"}) =
2245 runhttpserver("http", $verbose, "proxy");
2246 if($pid <= 0) {
2247 return ("failed starting HTTP-proxy server", $serr);
2249 logmsg sprintf ("* pid http-proxy => %d %d\n", $pid, $pid2)
2250 if($verbose);
2251 $run{'http-proxy'} = "$pid $pid2";
2254 elsif($what eq "http-ipv6") {
2255 if($run{'http-ipv6'} &&
2256 !responsive_http_server("http", $verbose, "ipv6",
2257 protoport("http6"))) {
2258 if(stopserver('http-ipv6')) {
2259 return ("failed stopping unresponsive HTTP-IPv6 server", 3);
2262 if(!$run{'http-ipv6'}) {
2263 ($serr, $pid, $pid2, $PORT{"http6"}) =
2264 runhttpserver("http", $verbose, "ipv6");
2265 if($pid <= 0) {
2266 return ("failed starting HTTP-IPv6 server", $serr);
2268 logmsg sprintf("* pid http-ipv6 => %d %d\n", $pid, $pid2)
2269 if($verbose);
2270 $run{'http-ipv6'} = "$pid $pid2";
2273 elsif($what eq "rtsp") {
2274 if($run{'rtsp'} &&
2275 !responsive_rtsp_server($verbose)) {
2276 if(stopserver('rtsp')) {
2277 return ("failed stopping unresponsive RTSP server", 3);
2280 if(!$run{'rtsp'}) {
2281 ($serr, $pid, $pid2, $PORT{'rtsp'}) = runrtspserver($verbose);
2282 if($pid <= 0) {
2283 return ("failed starting RTSP server", $serr);
2285 logmsg sprintf("* pid rtsp => %d %d\n", $pid, $pid2) if($verbose);
2286 $run{'rtsp'} = "$pid $pid2";
2289 elsif($what eq "rtsp-ipv6") {
2290 if($run{'rtsp-ipv6'} &&
2291 !responsive_rtsp_server($verbose, "ipv6")) {
2292 if(stopserver('rtsp-ipv6')) {
2293 return ("failed stopping unresponsive RTSP-IPv6 server", 3);
2296 if(!$run{'rtsp-ipv6'}) {
2297 ($serr, $pid, $pid2, $PORT{'rtsp6'}) = runrtspserver($verbose, "ipv6");
2298 if($pid <= 0) {
2299 return ("failed starting RTSP-IPv6 server", $serr);
2301 logmsg sprintf("* pid rtsp-ipv6 => %d %d\n", $pid, $pid2)
2302 if($verbose);
2303 $run{'rtsp-ipv6'} = "$pid $pid2";
2306 elsif($what =~ /^(ftp|imap|pop3|smtp)s$/) {
2307 my $cproto = $1;
2308 if(!$stunnel) {
2309 # we cannot run ftps tests without stunnel
2310 return ("no stunnel", 4);
2312 if($runcert{$what} && ($runcert{$what} ne $certfile)) {
2313 # stop server when running and using a different cert
2314 if(stopserver($what)) {
2315 return ("failed stopping $what server with different cert", 3);
2318 if($run{$cproto} &&
2319 !responsive_pingpong_server($cproto, "", $verbose)) {
2320 if(stopserver($cproto)) {
2321 return ("failed stopping unresponsive $cproto server", 3);
2324 if(!$run{$cproto}) {
2325 ($serr, $pid, $pid2) = runpingpongserver($cproto, "", $verbose);
2326 if($pid <= 0) {
2327 return ("failed starting $cproto server", $serr);
2329 logmsg sprintf("* pid $cproto => %d %d\n", $pid, $pid2) if($verbose);
2330 $run{$cproto} = "$pid $pid2";
2332 if(!$run{$what}) {
2333 ($serr, $pid, $pid2, $PORT{$what}) =
2334 runsecureserver($verbose, "", $certfile, $what,
2335 protoport($cproto));
2336 if($pid <= 0) {
2337 return ("failed starting $what server (stunnel)", $serr);
2339 logmsg sprintf("* pid $what => %d %d\n", $pid, $pid2)
2340 if($verbose);
2341 $run{$what} = "$pid $pid2";
2344 elsif($what eq "file") {
2345 # we support it but have no server!
2347 elsif($what eq "https" || $what eq "https-mtls") {
2348 if(!$stunnel) {
2349 # we cannot run https tests without stunnel
2350 return ("no stunnel", 4);
2352 if($runcert{$what} && ($runcert{$what} ne $certfile)) {
2353 # stop server when running and using a different cert
2354 if(stopserver($what)) {
2355 return ("failed stopping HTTPS server with different cert", 3);
2357 # also stop http server, we do not know which state it is in
2358 if($run{'http'} && stopserver('http')) {
2359 return ("failed stopping HTTP server", 3);
2362 if($run{$what} &&
2363 !responsive_http_server($what, $verbose, 0,
2364 protoport($what))) {
2365 if(stopserver($what)) {
2366 return ("failed stopping unresponsive HTTPS server", 3);
2368 # also stop http server, we do not know which state it is in
2369 if($run{'http'} && stopserver('http')) {
2370 return ("failed stopping unresponsive HTTP server", 3);
2373 # check a running http server if we not already checked https
2374 if($run{'http'} && !$run{$what} &&
2375 !responsive_http_server("http", $verbose, 0,
2376 protoport('http'))) {
2377 if(stopserver('http')) {
2378 return ("failed stopping unresponsive HTTP server", 3);
2381 if(!$run{'http'}) {
2382 ($serr, $pid, $pid2, $PORT{'http'}) =
2383 runhttpserver("http", $verbose, 0);
2384 if($pid <= 0) {
2385 return ("failed starting HTTP server (for https/https-mtls)", $serr);
2387 logmsg sprintf("* pid http => %d %d\n", $pid, $pid2) if($verbose);
2388 $run{'http'} = "$pid $pid2";
2390 if(!$run{$what}) {
2391 ($serr, $pid, $pid2, $PORT{$what}) =
2392 runhttpsserver($verbose, $what, "", $certfile);
2393 if($pid <= 0) {
2394 return ("failed starting HTTPS server (stunnel)", $serr);
2396 logmsg sprintf("* pid $what => %d %d\n", $pid, $pid2)
2397 if($verbose);
2398 $run{$what} = "$pid $pid2";
2401 elsif($what eq "http/2") {
2402 # http/2 server proxies to an HTTP server
2403 if($run{'http/2'} &&
2404 !responsive_http_server("https", $verbose, 0, protoport('http2tls'))) {
2405 logmsg "* restarting unresponsive HTTP/2 server\n";
2406 if(stopserver('http/2')) {
2407 return ("failed stopping unresponsive HTTP/2 server", 3);
2409 # also stop http server, we do not know which state it is in
2410 if($run{'http'} && stopserver('http')) {
2411 return ("failed stopping HTTP server", 3);
2414 # check a running http server if we not already checked http/2
2415 if($run{'http'} && !$run{'http/2'} &&
2416 !responsive_http_server("http", $verbose, 0,
2417 protoport('http'))) {
2418 if(stopserver('http')) {
2419 return ("failed stopping unresponsive HTTP server", 3);
2422 if(!$run{'http'}) {
2423 ($serr, $pid, $pid2, $PORT{'http'}) =
2424 runhttpserver("http", $verbose, 0);
2425 if($pid <= 0) {
2426 return ("failed starting HTTP server (for http/2)", $serr);
2428 logmsg sprintf("* pid http => %d %d\n", $pid, $pid2) if($verbose);
2429 $run{'http'} = "$pid $pid2";
2431 if(!$run{'http/2'}) {
2432 ($serr, $pid, $pid2, $PORT{"http2"}, $PORT{"http2tls"}) =
2433 runhttp2server($verbose);
2434 if($pid <= 0) {
2435 return ("failed starting HTTP/2 server", $serr);
2437 logmsg sprintf ("* pid http/2 => %d %d\n", $pid, $pid2)
2438 if($verbose);
2439 $run{'http/2'} = "$pid $pid2";
2442 elsif($what eq "http/3") {
2443 # http/3 server proxies to an HTTP server
2444 if($run{'http/3'} &&
2445 !responsive_http_server("https", $verbose, 0, protoport('http3'), 1)) {
2446 logmsg "* restarting unresponsive HTTP/3 server\n";
2447 if(stopserver('http/3')) {
2448 return ("failed stopping unresponsive HTTP/3 server", 3);
2450 # also stop http server, we do not know which state it is in
2451 if($run{'http'} && stopserver('http')) {
2452 return ("failed stopping HTTP server", 3);
2455 # check a running http server if we not already checked http/3
2456 if($run{'http'} && !$run{'http/3'} &&
2457 !responsive_http_server("http", $verbose, 0,
2458 protoport('http'))) {
2459 if(stopserver('http')) {
2460 return ("failed stopping unresponsive HTTP server", 3);
2463 if(!$run{'http'}) {
2464 ($serr, $pid, $pid2, $PORT{'http'}) =
2465 runhttpserver("http", $verbose, 0);
2466 if($pid <= 0) {
2467 return ("failed starting HTTP server (for http/3)", $serr);
2469 logmsg sprintf("* pid http => %d %d\n", $pid, $pid2) if($verbose);
2470 $run{'http'} = "$pid $pid2";
2472 if(!$run{'http/3'}) {
2473 ($serr, $pid, $pid2, $PORT{"http3"}) = runhttp3server($verbose);
2474 if($pid <= 0) {
2475 return ("failed starting HTTP/3 server", $serr);
2477 logmsg sprintf ("* pid http/3 => %d %d\n", $pid, $pid2)
2478 if($verbose);
2479 $run{'http/3'} = "$pid $pid2";
2482 elsif($what eq "gophers") {
2483 if(!$stunnel) {
2484 # we cannot run TLS tests without stunnel
2485 return ("no stunnel", 4);
2487 if($runcert{'gophers'} && ($runcert{'gophers'} ne $certfile)) {
2488 # stop server when running and using a different cert
2489 if(stopserver('gophers')) {
2490 return ("failed stopping GOPHERS server with different cert", 3);
2493 if($run{'gopher'} &&
2494 !responsive_http_server("gopher", $verbose, 0,
2495 protoport('gopher'))) {
2496 if(stopserver('gopher')) {
2497 return ("failed stopping unresponsive GOPHER server", 3);
2500 if(!$run{'gopher'}) {
2501 my $port;
2502 ($serr, $pid, $pid2, $port) = runhttpserver("gopher", $verbose, 0);
2503 $PORT{'gopher'} = $port;
2504 if($pid <= 0) {
2505 return ("failed starting GOPHER server", $serr);
2507 logmsg sprintf("* pid gopher => %d %d\n", $pid, $pid2) if($verbose);
2508 logmsg "GOPHERPORT => $port\n" if($verbose);
2509 $run{'gopher'} = "$pid $pid2";
2511 if(!$run{'gophers'}) {
2512 my $port;
2513 ($serr, $pid, $pid2, $port) =
2514 runhttpsserver($verbose, "gophers", "", $certfile);
2515 $PORT{'gophers'} = $port;
2516 if($pid <= 0) {
2517 return ("failed starting GOPHERS server (stunnel)", $serr);
2519 logmsg sprintf("* pid gophers => %d %d\n", $pid, $pid2)
2520 if($verbose);
2521 logmsg "GOPHERSPORT => $port\n" if($verbose);
2522 $run{'gophers'} = "$pid $pid2";
2525 elsif($what eq "https-proxy") {
2526 if(!$stunnel) {
2527 # we cannot run https-proxy tests without stunnel
2528 return ("no stunnel", 4);
2530 if($runcert{'https-proxy'} &&
2531 ($runcert{'https-proxy'} ne $certfile)) {
2532 # stop server when running and using a different cert
2533 if(stopserver('https-proxy')) {
2534 return ("failed stopping HTTPS-proxy with different cert", 3);
2538 # we front the http-proxy with stunnel so we need to make sure the
2539 # proxy runs as well
2540 my ($f, $e) = startservers("http-proxy");
2541 if($f) {
2542 return ($f, $e);
2545 if(!$run{'https-proxy'}) {
2546 ($serr, $pid, $pid2, $PORT{"httpsproxy"}) =
2547 runhttpsserver($verbose, "https", "proxy", $certfile);
2548 if($pid <= 0) {
2549 return ("failed starting HTTPS-proxy (stunnel)", $serr);
2551 logmsg sprintf("* pid https-proxy => %d %d\n", $pid, $pid2)
2552 if($verbose);
2553 $run{'https-proxy'} = "$pid $pid2";
2556 elsif($what eq "dns") {
2557 if($run{'dns'} &&
2558 !responsive_dns_server("", $verbose)) {
2559 if(stopserver('dns')) {
2560 return ("failed stopping unresponsive DNS server", 3);
2563 if(!$run{'dns'}) {
2564 ($serr, $pid, $pid2, $PORT{'dns'}) =
2565 rundnsserver("", $verbose);
2566 if($pid <= 0) {
2567 return ("failed starting DNS server", $serr);
2569 logmsg sprintf("* pid dns => %d %d\n", $pid, $pid2) if($verbose);
2570 $run{'dns'} = "$pid $pid2";
2573 elsif($what eq "tftp") {
2574 if($run{'tftp'} &&
2575 !responsive_tftp_server("", $verbose)) {
2576 if(stopserver('tftp')) {
2577 return ("failed stopping unresponsive TFTP server", 3);
2580 if(!$run{'tftp'}) {
2581 ($serr, $pid, $pid2, $PORT{'tftp'}) =
2582 runtftpserver("", $verbose);
2583 if($pid <= 0) {
2584 return ("failed starting TFTP server", $serr);
2586 logmsg sprintf("* pid tftp => %d %d\n", $pid, $pid2) if($verbose);
2587 $run{'tftp'} = "$pid $pid2";
2590 elsif($what eq "tftp-ipv6") {
2591 if($run{'tftp-ipv6'} &&
2592 !responsive_tftp_server("", $verbose, "ipv6")) {
2593 if(stopserver('tftp-ipv6')) {
2594 return ("failed stopping unresponsive TFTP-IPv6 server", 3);
2597 if(!$run{'tftp-ipv6'}) {
2598 ($serr, $pid, $pid2, $PORT{'tftp6'}) =
2599 runtftpserver("", $verbose, "ipv6");
2600 if($pid <= 0) {
2601 return ("failed starting TFTP-IPv6 server", $serr);
2603 logmsg sprintf("* pid tftp-ipv6 => %d %d\n", $pid, $pid2) if($verbose);
2604 $run{'tftp-ipv6'} = "$pid $pid2";
2607 elsif($what eq "sftp" || $what eq "scp") {
2608 if(!$run{'ssh'}) {
2609 ($serr, $pid, $pid2, $PORT{'ssh'}) = runsshserver("", $verbose);
2610 if($pid <= 0) {
2611 return ("failed starting SSH server", $serr);
2613 logmsg sprintf("* pid ssh => %d %d\n", $pid, $pid2) if($verbose);
2614 $run{'ssh'} = "$pid $pid2";
2617 elsif($what eq "socks4" || $what eq "socks5" ) {
2618 if(!$run{'socks'}) {
2619 ($serr, $pid, $pid2, $PORT{"socks"}) = runsocksserver("", $verbose);
2620 if($pid <= 0) {
2621 return ("failed starting socks server", $serr);
2623 logmsg sprintf("* pid socks => %d %d\n", $pid, $pid2) if($verbose);
2624 $run{'socks'} = "$pid $pid2";
2627 elsif($what eq "socks5unix") {
2628 if(!$run{'socks5unix'}) {
2629 ($serr, $pid, $pid2) = runsocksserver("2", $verbose, "", "unix");
2630 if($pid <= 0) {
2631 return ("failed starting socks5unix server", $serr);
2633 logmsg sprintf("* pid socks5unix => %d %d\n", $pid, $pid2) if($verbose);
2634 $run{'socks5unix'} = "$pid $pid2";
2637 elsif($what eq "mqtt" ) {
2638 if($run{'mqtt'} &&
2639 !responsive_mqtt_server("mqtt", "", $verbose)) {
2640 if(stopserver('mqtt')) {
2641 return ("failed stopping unresponsive MQTT server", 3);
2644 if(!$run{'mqtt'}) {
2645 ($serr, $pid, $pid2, $PORT{"mqtt"}) = runmqttserver("", $verbose);
2646 if($pid <= 0) {
2647 return ("failed starting mqtt server", $serr);
2649 logmsg sprintf("* pid mqtt => %d %d\n", $pid, $pid2) if($verbose);
2650 $run{'mqtt'} = "$pid $pid2";
2653 elsif($what eq "mqtts" ) {
2654 if(!$stunnel) {
2655 # we cannot run mqtts tests without stunnel
2656 return ("no stunnel", 4);
2658 if($run{'mqtt'} &&
2659 !responsive_mqtt_server("mqtt", "", $verbose)) {
2660 if(stopserver('mqtt')) {
2661 return ("failed stopping unresponsive MQTT server", 3);
2664 if(!$run{'mqtt'}) {
2665 ($serr, $pid, $pid2, $PORT{"mqtt"}) = runmqttserver("", $verbose);
2666 if($pid <= 0) {
2667 return ("failed starting mqtt server", $serr);
2669 logmsg sprintf("* pid mqtt => %d %d\n", $pid, $pid2) if($verbose);
2670 $run{'mqtt'} = "$pid $pid2";
2672 if(!$run{$what}) {
2673 ($serr, $pid, $pid2, $PORT{$what}) =
2674 runhttpsserver($verbose, $what, "", $certfile);
2675 if($pid <= 0) {
2676 return ("failed starting MQTTS server (stunnel)", $serr);
2678 logmsg sprintf("* pid $what => %d %d\n", $pid, $pid2)
2679 if($verbose);
2680 $run{$what} = "$pid $pid2";
2683 elsif($what eq "http-unix") {
2684 if($run{'http-unix'} &&
2685 !responsive_http_server("http", $verbose, "unix", $HTTPUNIXPATH)) {
2686 if(stopserver('http-unix')) {
2687 return ("failed stopping unresponsive HTTP-unix server", 3);
2690 if(!$run{'http-unix'}) {
2691 my $unused;
2692 ($serr, $pid, $pid2, $unused) =
2693 runhttpserver("http", $verbose, "unix", $HTTPUNIXPATH);
2694 if($pid <= 0) {
2695 return ("failed starting HTTP-unix server", $serr);
2697 logmsg sprintf("* pid http-unix => %d %d\n", $pid, $pid2)
2698 if($verbose);
2699 $run{'http-unix'} = "$pid $pid2";
2702 elsif($what eq "dict") {
2703 if(!$run{'dict'}) {
2704 ($serr, $pid, $pid2, $PORT{"dict"}) = rundictserver($verbose, "");
2705 if($pid <= 0) {
2706 return ("failed starting DICT server", $serr);
2708 logmsg sprintf ("* pid DICT => %d %d\n", $pid, $pid2)
2709 if($verbose);
2710 $run{'dict'} = "$pid $pid2";
2713 elsif($what eq "telnet") {
2714 if(!$run{'telnet'}) {
2715 ($serr, $pid, $pid2, $PORT{"telnet"}) =
2716 runnegtelnetserver($verbose, "");
2717 if($pid <= 0) {
2718 return ("failed starting neg TELNET server", $serr);
2720 logmsg sprintf ("* pid neg TELNET => %d %d\n", $pid, $pid2)
2721 if($verbose);
2722 $run{'telnet'} = "$pid $pid2";
2725 else {
2726 warn "we do not support a server for $what";
2727 return ("no server for $what", 4);
2730 return ("", 0);
2733#######################################################################
2734# Stop all running test servers
2736sub stopservers {
2737 my $verb = $_[0];
2739 # kill sockfilter processes for all pingpong servers
2741 killallsockfilters("$LOGDIR/$PIDDIR", $verb);
2743 # kill all server pids from %run hash clearing them
2745 my $pidlist;
2746 foreach my $server (keys %run) {
2747 if($run{$server}) {
2748 if($verb) {
2749 my $prev = 0;
2750 my $pids = $run{$server};
2751 foreach my $pid (split(' ', $pids)) {
2752 if($pid != $prev) {
2753 logmsg sprintf("* kill pid for %s => %d\n",
2754 $server, $pid);
2755 $prev = $pid;
2759 $pidlist .= "$run{$server} ";
2760 $run{$server} = 0;
2762 $runcert{$server} = 0 if($runcert{$server});
2764 killpid($verb, $pidlist);
2766 # cleanup all server pid files
2768 my $result = 0;
2769 foreach my $server (keys %serverpidfile) {
2770 my $pidfile = $serverpidfile{$server};
2771 my $pid = processexists($pidfile);
2772 if($pid > 0) {
2773 if($err_unexpected) {
2774 logmsg "ERROR: ";
2775 $result = -1;
2777 else {
2778 logmsg "Warning: ";
2780 logmsg "$server server unexpectedly alive\n";
2781 killpid($verb, $pid);
2783 unlink($pidfile) if(-f $pidfile);
2786 return $result;
2789#######################################################################
2790# substitute the variable stuff into either a joined up file or
2791# a command, in either case passed by reference
2793sub subvariables {
2794 my ($thing, $testnum, $prefix) = @_;
2795 my $port;
2797 if(!$prefix) {
2798 $prefix = "%";
2801 # test server ports
2802 # Substitutes variables like %HTTPPORT and %SMTP6PORT with the server ports
2803 foreach my $proto ('DICT', 'DNS',
2804 'FTP', 'FTP6', 'FTPS',
2805 'GOPHER', 'GOPHER6', 'GOPHERS',
2806 'HTTP', 'HTTP6', 'HTTPS', 'HTTPS-MTLS',
2807 'HTTPSPROXY',
2808 'HTTP2', 'HTTP2TLS',
2809 'HTTP3',
2810 'IMAP', 'IMAP6', 'IMAPS',
2811 'MQTT', 'MQTTS',
2812 'NOLISTEN',
2813 'POP3', 'POP36', 'POP3S',
2814 'RTSP', 'RTSP6',
2815 'SMTP', 'SMTP6', 'SMTPS',
2816 'SOCKS',
2817 'SSH',
2818 'TELNET',
2819 'TFTP', 'TFTP6') {
2820 $port = protoport(lc $proto);
2821 $$thing =~ s/${prefix}(?:$proto)PORT/$port/g;
2823 # Special case: for PROXYPORT substitution, use httpproxy.
2824 $port = protoport('httpproxy');
2825 $$thing =~ s/${prefix}PROXYPORT/$port/g;
2827 # server Unix domain socket paths
2828 $$thing =~ s/${prefix}HTTPUNIXPATH/$HTTPUNIXPATH/g;
2829 $$thing =~ s/${prefix}SOCKSUNIXPATH/$SOCKSUNIXPATH/g;
2831 # client IP addresses
2832 my $nb = $CLIENT6IP;
2833 $nb =~ s/^\[(.*)\]/$1/; # trim off the brackets
2835 $$thing =~ s/${prefix}CLIENT6IP-NB/$nb/g;
2836 $$thing =~ s/${prefix}CLIENT6IP/$CLIENT6IP/g;
2837 $$thing =~ s/${prefix}CLIENTIP/$CLIENTIP/g;
2839 # server IP addresses
2840 $$thing =~ s/${prefix}HOST6IP/$HOST6IP/g;
2841 $$thing =~ s/${prefix}HOSTIP/$HOSTIP/g;
2843 # misc
2844 $$thing =~ s/${prefix}PERL/$perlcmd/g;
2845 $$thing =~ s/${prefix}CURL/$CURL/g;
2846 $$thing =~ s/${prefix}LOGDIR/$LOGDIR/g;
2847 $$thing =~ s/${prefix}PWD/$pwd/g;
2848 $$thing =~ s/${prefix}VERSION/$CURLVERSION/g;
2849 $$thing =~ s/${prefix}VERNUM/$CURLVERNUM/g;
2850 $$thing =~ s/${prefix}DATE/$DATE/g;
2851 $$thing =~ s/${prefix}TESTNUMBER/$testnum/g;
2852 my $resolve = server_exe('resolve', 'TOOL');
2853 $$thing =~ s/${prefix}RESOLVE/$resolve/g;
2855 # POSIX/MSYS/Cygwin curl needs: file://localhost/d/path/to
2856 # Windows native curl needs: file://localhost/D:/path/to
2857 my $file_pwd = $pwd;
2858 if($file_pwd !~ /^\//) {
2859 $file_pwd = "/$file_pwd";
2861 my $ssh_pwd = $posix_pwd;
2862 # this only works after the SSH server has been started
2863 # TODO: call sshversioninfo early and store $sshdid so this substitution
2864 # always works
2865 if($sshdid && $sshdid =~ /OpenSSH-Windows/) {
2866 $ssh_pwd = $file_pwd;
2869 $$thing =~ s/${prefix}FILE_PWD/$file_pwd/g;
2870 $$thing =~ s/${prefix}SCP_PWD/$posix_pwd/g;
2871 $$thing =~ s/${prefix}SFTP_PWD/$ssh_pwd/g;
2872 $$thing =~ s/${prefix}SRCDIR/$srcdir/g;
2873 $$thing =~ s/${prefix}CERTDIR/./g;
2874 $$thing =~ s/${prefix}USER/$USER/g;
2875 $$thing =~ s/${prefix}DEV_NULL/$dev_null/g;
2876 my $libtests = $LIBDIR . 'libtests' . exe_ext('TOOL');
2877 $$thing =~ s/${prefix}LIBTESTS/$libtests/g;
2879 $$thing =~ s/${prefix}SSHSRVMD5/$SSHSRVMD5/g;
2880 $$thing =~ s/${prefix}SSHSRVSHA256/$SSHSRVSHA256/g;
2881 my $keyalgostr = sshkeyalgostr(defined $feature{"sshkeyalgo"} ? $feature{"sshkeyalgo"} : "");
2882 $$thing =~ s/${prefix}SSHKEYALGO/$keyalgostr/g;
2884 # The purpose of FTPTIME2 is to provide times that can be
2885 # used for time-out tests and that would work on most hosts as these
2886 # adjust for the startup/check time for this particular host. We needed to
2887 # do this to make the test suite run better on slow hosts.
2888 my $ftp2 = $ftpchecktime * 8;
2890 $$thing =~ s/${prefix}FTPTIME2/$ftp2/g;
2892 # HTTP2
2893 $$thing =~ s/${prefix}H2CVER/$h2cver/g;
2896sub localhttp {
2897 return $HOSTIP eq "127.0.0.1";
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20018 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=18613
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=b16ec653be29d3acd3706c926e854cad35e29b48
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=18613
git.pkt.payload.max=18592
git.sideband.band1.bytes=18591
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2d0b23b7fefad
Request
5a0e38a2…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format