113498266Sopenharmony_ci#*************************************************************************** 213498266Sopenharmony_ci# _ _ ____ _ 313498266Sopenharmony_ci# Project ___| | | | _ \| | 413498266Sopenharmony_ci# / __| | | | |_) | | 513498266Sopenharmony_ci# | (__| |_| | _ <| |___ 613498266Sopenharmony_ci# \___|\___/|_| \_\_____| 713498266Sopenharmony_ci# 813498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci# 1013498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci# you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci# 1413498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci# 1813498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci# KIND, either express or implied. 2013498266Sopenharmony_ci# 2113498266Sopenharmony_ci# SPDX-License-Identifier: curl 2213498266Sopenharmony_ci# 2313498266Sopenharmony_ci#*************************************************************************** 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci# This perl module contains functions useful in writing test servers. 2613498266Sopenharmony_ci 2713498266Sopenharmony_cipackage serverhelp; 2813498266Sopenharmony_ci 2913498266Sopenharmony_ciuse strict; 3013498266Sopenharmony_ciuse warnings; 3113498266Sopenharmony_ci 3213498266Sopenharmony_ciBEGIN { 3313498266Sopenharmony_ci use base qw(Exporter); 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci our @EXPORT_OK = qw( 3613498266Sopenharmony_ci logmsg 3713498266Sopenharmony_ci $logfile 3813498266Sopenharmony_ci serverfactors 3913498266Sopenharmony_ci servername_id 4013498266Sopenharmony_ci servername_str 4113498266Sopenharmony_ci servername_canon 4213498266Sopenharmony_ci server_pidfilename 4313498266Sopenharmony_ci server_portfilename 4413498266Sopenharmony_ci server_logfilename 4513498266Sopenharmony_ci server_cmdfilename 4613498266Sopenharmony_ci server_inputfilename 4713498266Sopenharmony_ci server_outputfilename 4813498266Sopenharmony_ci mainsockf_pidfilename 4913498266Sopenharmony_ci mainsockf_logfilename 5013498266Sopenharmony_ci datasockf_pidfilename 5113498266Sopenharmony_ci datasockf_logfilename 5213498266Sopenharmony_ci ); 5313498266Sopenharmony_ci 5413498266Sopenharmony_ci # sub second timestamping needs Time::HiRes 5513498266Sopenharmony_ci eval { 5613498266Sopenharmony_ci no warnings "all"; 5713498266Sopenharmony_ci require Time::HiRes; 5813498266Sopenharmony_ci import Time::HiRes qw( gettimeofday ); 5913498266Sopenharmony_ci } 6013498266Sopenharmony_ci} 6113498266Sopenharmony_ci 6213498266Sopenharmony_ci 6313498266Sopenharmony_ciour $logfile; # server log file name, for logmsg 6413498266Sopenharmony_ci 6513498266Sopenharmony_ci#*************************************************************************** 6613498266Sopenharmony_ci# Just for convenience, test harness uses 'https' and 'httptls' literals as 6713498266Sopenharmony_ci# values for 'proto' variable in order to differentiate different servers. 6813498266Sopenharmony_ci# 'https' literal is used for stunnel based https test servers, and 'httptls' 6913498266Sopenharmony_ci# is used for non-stunnel https test servers. 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci#********************************************************************** 7213498266Sopenharmony_ci# logmsg is general message logging subroutine for our test servers. 7313498266Sopenharmony_ci# 7413498266Sopenharmony_cisub logmsg { 7513498266Sopenharmony_ci my $now; 7613498266Sopenharmony_ci # sub second timestamping needs Time::HiRes 7713498266Sopenharmony_ci if($Time::HiRes::VERSION) { 7813498266Sopenharmony_ci my ($seconds, $usec) = gettimeofday(); 7913498266Sopenharmony_ci my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 8013498266Sopenharmony_ci localtime($seconds); 8113498266Sopenharmony_ci $now = sprintf("%02d:%02d:%02d.%06d ", $hour, $min, $sec, $usec); 8213498266Sopenharmony_ci } 8313498266Sopenharmony_ci else { 8413498266Sopenharmony_ci my $seconds = time(); 8513498266Sopenharmony_ci my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 8613498266Sopenharmony_ci localtime($seconds); 8713498266Sopenharmony_ci $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec); 8813498266Sopenharmony_ci } 8913498266Sopenharmony_ci if(open(my $logfilefh, ">>", "$logfile")) { 9013498266Sopenharmony_ci print $logfilefh $now; 9113498266Sopenharmony_ci print $logfilefh @_; 9213498266Sopenharmony_ci close($logfilefh); 9313498266Sopenharmony_ci } 9413498266Sopenharmony_ci} 9513498266Sopenharmony_ci 9613498266Sopenharmony_ci 9713498266Sopenharmony_ci#*************************************************************************** 9813498266Sopenharmony_ci# Return server characterization factors given a server id string. 9913498266Sopenharmony_ci# 10013498266Sopenharmony_cisub serverfactors { 10113498266Sopenharmony_ci my $server = $_[0]; 10213498266Sopenharmony_ci my $proto; 10313498266Sopenharmony_ci my $ipvnum; 10413498266Sopenharmony_ci my $idnum; 10513498266Sopenharmony_ci 10613498266Sopenharmony_ci if($server =~ 10713498266Sopenharmony_ci /^((ftp|http|imap|pop3|smtp|http-pipe)s?)(\d*)(-ipv6|)$/) { 10813498266Sopenharmony_ci $proto = $1; 10913498266Sopenharmony_ci $idnum = ($3 && ($3 > 1)) ? $3 : 1; 11013498266Sopenharmony_ci $ipvnum = ($4 && ($4 =~ /6$/)) ? 6 : 4; 11113498266Sopenharmony_ci } 11213498266Sopenharmony_ci elsif($server =~ 11313498266Sopenharmony_ci /^(tftp|sftp|socks|ssh|rtsp|gopher|httptls)(\d*)(-ipv6|)$/) { 11413498266Sopenharmony_ci $proto = $1; 11513498266Sopenharmony_ci $idnum = ($2 && ($2 > 1)) ? $2 : 1; 11613498266Sopenharmony_ci $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4; 11713498266Sopenharmony_ci } 11813498266Sopenharmony_ci else { 11913498266Sopenharmony_ci die "invalid server id: '$server'" 12013498266Sopenharmony_ci } 12113498266Sopenharmony_ci return($proto, $ipvnum, $idnum); 12213498266Sopenharmony_ci} 12313498266Sopenharmony_ci 12413498266Sopenharmony_ci 12513498266Sopenharmony_ci#*************************************************************************** 12613498266Sopenharmony_ci# Return server name string formatted for presentation purposes 12713498266Sopenharmony_ci# 12813498266Sopenharmony_cisub servername_str { 12913498266Sopenharmony_ci my ($proto, $ipver, $idnum) = @_; 13013498266Sopenharmony_ci 13113498266Sopenharmony_ci $proto = uc($proto) if($proto); 13213498266Sopenharmony_ci die "unsupported protocol: '$proto'" unless($proto && 13313498266Sopenharmony_ci ($proto =~ /^(((FTP|HTTP|HTTP\/2|HTTP\/3|IMAP|POP3|GOPHER|SMTP|HTTP-PIPE)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP|HTTPTLS|DICT|SMB|SMBS|TELNET|MQTT))$/)); 13413498266Sopenharmony_ci 13513498266Sopenharmony_ci $ipver = (not $ipver) ? 'ipv4' : lc($ipver); 13613498266Sopenharmony_ci die "unsupported IP version: '$ipver'" unless($ipver && 13713498266Sopenharmony_ci ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6|unix)$/)); 13813498266Sopenharmony_ci $ipver = ($ipver =~ /6$/) ? '-IPv6' : (($ipver =~ /unix$/) ? '-unix' : ''); 13913498266Sopenharmony_ci 14013498266Sopenharmony_ci $idnum = 1 if(not $idnum); 14113498266Sopenharmony_ci die "unsupported ID number: '$idnum'" unless($idnum && 14213498266Sopenharmony_ci ($idnum =~ /^(\d+)$/)); 14313498266Sopenharmony_ci $idnum = '' if($idnum <= 1); 14413498266Sopenharmony_ci 14513498266Sopenharmony_ci return "${proto}${idnum}${ipver}"; 14613498266Sopenharmony_ci} 14713498266Sopenharmony_ci 14813498266Sopenharmony_ci 14913498266Sopenharmony_ci#*************************************************************************** 15013498266Sopenharmony_ci# Return server name string formatted for identification purposes 15113498266Sopenharmony_ci# 15213498266Sopenharmony_cisub servername_id { 15313498266Sopenharmony_ci my ($proto, $ipver, $idnum) = @_; 15413498266Sopenharmony_ci return lc(servername_str($proto, $ipver, $idnum)); 15513498266Sopenharmony_ci} 15613498266Sopenharmony_ci 15713498266Sopenharmony_ci 15813498266Sopenharmony_ci#*************************************************************************** 15913498266Sopenharmony_ci# Return server name string formatted for file name purposes 16013498266Sopenharmony_ci# 16113498266Sopenharmony_cisub servername_canon { 16213498266Sopenharmony_ci my ($proto, $ipver, $idnum) = @_; 16313498266Sopenharmony_ci my $string = lc(servername_str($proto, $ipver, $idnum)); 16413498266Sopenharmony_ci $string =~ tr/-/_/; 16513498266Sopenharmony_ci $string =~ s/\//_v/; 16613498266Sopenharmony_ci return $string; 16713498266Sopenharmony_ci} 16813498266Sopenharmony_ci 16913498266Sopenharmony_ci 17013498266Sopenharmony_ci#*************************************************************************** 17113498266Sopenharmony_ci# Return file name for server pid file. 17213498266Sopenharmony_ci# 17313498266Sopenharmony_cisub server_pidfilename { 17413498266Sopenharmony_ci my ($piddir, $proto, $ipver, $idnum) = @_; 17513498266Sopenharmony_ci my $trailer = '_server.pid'; 17613498266Sopenharmony_ci return "${piddir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 17713498266Sopenharmony_ci} 17813498266Sopenharmony_ci 17913498266Sopenharmony_ci#*************************************************************************** 18013498266Sopenharmony_ci# Return file name for server port file. 18113498266Sopenharmony_ci# 18213498266Sopenharmony_cisub server_portfilename { 18313498266Sopenharmony_ci my ($piddir, $proto, $ipver, $idnum) = @_; 18413498266Sopenharmony_ci my $trailer = '_server.port'; 18513498266Sopenharmony_ci return "${piddir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 18613498266Sopenharmony_ci} 18713498266Sopenharmony_ci 18813498266Sopenharmony_ci 18913498266Sopenharmony_ci#*************************************************************************** 19013498266Sopenharmony_ci# Return file name for server log file. 19113498266Sopenharmony_ci# 19213498266Sopenharmony_cisub server_logfilename { 19313498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 19413498266Sopenharmony_ci my $trailer = '_server.log'; 19513498266Sopenharmony_ci $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/); 19613498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 19713498266Sopenharmony_ci} 19813498266Sopenharmony_ci 19913498266Sopenharmony_ci 20013498266Sopenharmony_ci#*************************************************************************** 20113498266Sopenharmony_ci# Return file name for server commands file. 20213498266Sopenharmony_ci# 20313498266Sopenharmony_cisub server_cmdfilename { 20413498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 20513498266Sopenharmony_ci my $trailer = '_server.cmd'; 20613498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 20713498266Sopenharmony_ci} 20813498266Sopenharmony_ci 20913498266Sopenharmony_ci 21013498266Sopenharmony_ci#*************************************************************************** 21113498266Sopenharmony_ci# Return file name for server input file. 21213498266Sopenharmony_ci# 21313498266Sopenharmony_cisub server_inputfilename { 21413498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 21513498266Sopenharmony_ci my $trailer = '_server.input'; 21613498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 21713498266Sopenharmony_ci} 21813498266Sopenharmony_ci 21913498266Sopenharmony_ci 22013498266Sopenharmony_ci#*************************************************************************** 22113498266Sopenharmony_ci# Return file name for server output file. 22213498266Sopenharmony_ci# 22313498266Sopenharmony_cisub server_outputfilename { 22413498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 22513498266Sopenharmony_ci my $trailer = '_server.output'; 22613498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 22713498266Sopenharmony_ci} 22813498266Sopenharmony_ci 22913498266Sopenharmony_ci 23013498266Sopenharmony_ci#*************************************************************************** 23113498266Sopenharmony_ci# Return file name for main or primary sockfilter pid file. 23213498266Sopenharmony_ci# 23313498266Sopenharmony_cisub mainsockf_pidfilename { 23413498266Sopenharmony_ci my ($piddir, $proto, $ipver, $idnum) = @_; 23513498266Sopenharmony_ci die "unsupported protocol: '$proto'" unless($proto && 23613498266Sopenharmony_ci (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/)); 23713498266Sopenharmony_ci my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid'; 23813498266Sopenharmony_ci return "${piddir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 23913498266Sopenharmony_ci} 24013498266Sopenharmony_ci 24113498266Sopenharmony_ci 24213498266Sopenharmony_ci#*************************************************************************** 24313498266Sopenharmony_ci# Return file name for main or primary sockfilter log file. 24413498266Sopenharmony_ci# 24513498266Sopenharmony_cisub mainsockf_logfilename { 24613498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 24713498266Sopenharmony_ci die "unsupported protocol: '$proto'" unless($proto && 24813498266Sopenharmony_ci (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/)); 24913498266Sopenharmony_ci my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log'; 25013498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 25113498266Sopenharmony_ci} 25213498266Sopenharmony_ci 25313498266Sopenharmony_ci 25413498266Sopenharmony_ci#*************************************************************************** 25513498266Sopenharmony_ci# Return file name for data or secondary sockfilter pid file. 25613498266Sopenharmony_ci# 25713498266Sopenharmony_cisub datasockf_pidfilename { 25813498266Sopenharmony_ci my ($piddir, $proto, $ipver, $idnum) = @_; 25913498266Sopenharmony_ci die "unsupported protocol: '$proto'" unless($proto && 26013498266Sopenharmony_ci (lc($proto) =~ /^ftps?$/)); 26113498266Sopenharmony_ci my $trailer = '_sockdata.pid'; 26213498266Sopenharmony_ci return "${piddir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 26313498266Sopenharmony_ci} 26413498266Sopenharmony_ci 26513498266Sopenharmony_ci 26613498266Sopenharmony_ci#*************************************************************************** 26713498266Sopenharmony_ci# Return file name for data or secondary sockfilter log file. 26813498266Sopenharmony_ci# 26913498266Sopenharmony_cisub datasockf_logfilename { 27013498266Sopenharmony_ci my ($logdir, $proto, $ipver, $idnum) = @_; 27113498266Sopenharmony_ci die "unsupported protocol: '$proto'" unless($proto && 27213498266Sopenharmony_ci (lc($proto) =~ /^ftps?$/)); 27313498266Sopenharmony_ci my $trailer = '_sockdata.log'; 27413498266Sopenharmony_ci return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer"; 27513498266Sopenharmony_ci} 27613498266Sopenharmony_ci 27713498266Sopenharmony_ci 27813498266Sopenharmony_ci#*************************************************************************** 27913498266Sopenharmony_ci# End of library 28013498266Sopenharmony_ci1; 281