serverhelp.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2016, 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.haxx.se/docs/copyright.html.
  13. #
  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.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. #***************************************************************************
  22. package serverhelp;
  23. use strict;
  24. use warnings;
  25. use Exporter;
  26. #***************************************************************************
  27. # Global symbols allowed without explicit package name
  28. #
  29. use vars qw(
  30. @ISA
  31. @EXPORT_OK
  32. );
  33. #***************************************************************************
  34. # Inherit Exporter's capabilities
  35. #
  36. @ISA = qw(Exporter);
  37. #***************************************************************************
  38. # Global symbols this module will export upon request
  39. #
  40. @EXPORT_OK = qw(
  41. serverfactors
  42. servername_id
  43. servername_str
  44. servername_canon
  45. server_pidfilename
  46. server_logfilename
  47. server_cmdfilename
  48. server_inputfilename
  49. server_outputfilename
  50. mainsockf_pidfilename
  51. mainsockf_logfilename
  52. datasockf_pidfilename
  53. datasockf_logfilename
  54. );
  55. #***************************************************************************
  56. # Just for convenience, test harness uses 'https' and 'httptls' literals as
  57. # values for 'proto' variable in order to differentiate different servers.
  58. # 'https' literal is used for stunnel based https test servers, and 'httptls'
  59. # is used for non-stunnel https test servers.
  60. #***************************************************************************
  61. # Return server characterization factors given a server id string.
  62. #
  63. sub serverfactors {
  64. my $server = $_[0];
  65. my $proto;
  66. my $ipvnum;
  67. my $idnum;
  68. if($server =~
  69. /^((ftp|http|imap|pop3|smtp|http-pipe)s?)(\d*)(-ipv6|)$/) {
  70. $proto = $1;
  71. $idnum = ($3 && ($3 > 1)) ? $3 : 1;
  72. $ipvnum = ($4 && ($4 =~ /6$/)) ? 6 : 4;
  73. }
  74. elsif($server =~
  75. /^(tftp|sftp|socks|ssh|rtsp|gopher|httptls)(\d*)(-ipv6|)$/) {
  76. $proto = $1;
  77. $idnum = ($2 && ($2 > 1)) ? $2 : 1;
  78. $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
  79. }
  80. else {
  81. die "invalid server id: '$server'"
  82. }
  83. return($proto, $ipvnum, $idnum);
  84. }
  85. #***************************************************************************
  86. # Return server name string formatted for presentation purposes
  87. #
  88. sub servername_str {
  89. my ($proto, $ipver, $idnum) = @_;
  90. $proto = uc($proto) if($proto);
  91. die "unsupported protocol: '$proto'" unless($proto &&
  92. ($proto =~ /^(((FTP|HTTP|HTTP\/2|IMAP|POP3|SMTP|HTTP-PIPE)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP|GOPHER|HTTPTLS|DICT|SMB|SMBS|TELNET))$/));
  93. $ipver = (not $ipver) ? 'ipv4' : lc($ipver);
  94. die "unsupported IP version: '$ipver'" unless($ipver &&
  95. ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6|unix)$/));
  96. $ipver = ($ipver =~ /6$/) ? '-IPv6' : (($ipver =~ /unix$/) ? '-unix' : '');
  97. $idnum = 1 if(not $idnum);
  98. die "unsupported ID number: '$idnum'" unless($idnum &&
  99. ($idnum =~ /^(\d+)$/));
  100. $idnum = '' unless($idnum > 1);
  101. return "${proto}${idnum}${ipver}";
  102. }
  103. #***************************************************************************
  104. # Return server name string formatted for identification purposes
  105. #
  106. sub servername_id {
  107. my ($proto, $ipver, $idnum) = @_;
  108. return lc(servername_str($proto, $ipver, $idnum));
  109. }
  110. #***************************************************************************
  111. # Return server name string formatted for file name purposes
  112. #
  113. sub servername_canon {
  114. my ($proto, $ipver, $idnum) = @_;
  115. my $string = lc(servername_str($proto, $ipver, $idnum));
  116. $string =~ tr/-/_/;
  117. $string =~ s/\//_v/;
  118. return $string;
  119. }
  120. #***************************************************************************
  121. # Return file name for server pid file.
  122. #
  123. sub server_pidfilename {
  124. my ($proto, $ipver, $idnum) = @_;
  125. my $trailer = '_server.pid';
  126. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  127. }
  128. #***************************************************************************
  129. # Return file name for server log file.
  130. #
  131. sub server_logfilename {
  132. my ($logdir, $proto, $ipver, $idnum) = @_;
  133. my $trailer = '_server.log';
  134. $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/);
  135. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  136. }
  137. #***************************************************************************
  138. # Return file name for server commands file.
  139. #
  140. sub server_cmdfilename {
  141. my ($logdir, $proto, $ipver, $idnum) = @_;
  142. my $trailer = '_server.cmd';
  143. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  144. }
  145. #***************************************************************************
  146. # Return file name for server input file.
  147. #
  148. sub server_inputfilename {
  149. my ($logdir, $proto, $ipver, $idnum) = @_;
  150. my $trailer = '_server.input';
  151. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  152. }
  153. #***************************************************************************
  154. # Return file name for server output file.
  155. #
  156. sub server_outputfilename {
  157. my ($logdir, $proto, $ipver, $idnum) = @_;
  158. my $trailer = '_server.output';
  159. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  160. }
  161. #***************************************************************************
  162. # Return file name for main or primary sockfilter pid file.
  163. #
  164. sub mainsockf_pidfilename {
  165. my ($proto, $ipver, $idnum) = @_;
  166. die "unsupported protocol: '$proto'" unless($proto &&
  167. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  168. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid';
  169. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  170. }
  171. #***************************************************************************
  172. # Return file name for main or primary sockfilter log file.
  173. #
  174. sub mainsockf_logfilename {
  175. my ($logdir, $proto, $ipver, $idnum) = @_;
  176. die "unsupported protocol: '$proto'" unless($proto &&
  177. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  178. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log';
  179. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  180. }
  181. #***************************************************************************
  182. # Return file name for data or secondary sockfilter pid file.
  183. #
  184. sub datasockf_pidfilename {
  185. my ($proto, $ipver, $idnum) = @_;
  186. die "unsupported protocol: '$proto'" unless($proto &&
  187. (lc($proto) =~ /^ftps?$/));
  188. my $trailer = '_sockdata.pid';
  189. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  190. }
  191. #***************************************************************************
  192. # Return file name for data or secondary sockfilter log file.
  193. #
  194. sub datasockf_logfilename {
  195. my ($logdir, $proto, $ipver, $idnum) = @_;
  196. die "unsupported protocol: '$proto'" unless($proto &&
  197. (lc($proto) =~ /^ftps?$/));
  198. my $trailer = '_sockdata.log';
  199. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  200. }
  201. #***************************************************************************
  202. # End of library
  203. 1;