2
0

serverhelp.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2011, 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 http://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)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|IMAP|POP3|SMTP)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP|GOPHER|HTTPTLS))$/));
  93. $ipver = (not $ipver) ? 'ipv4' : lc($ipver);
  94. die "unsupported IP version: '$ipver'" unless($ipver &&
  95. ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6)$/));
  96. $ipver = ($ipver =~ /6$/) ? '-IPv6' : '';
  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. return $string;
  118. }
  119. #***************************************************************************
  120. # Return file name for server pid file.
  121. #
  122. sub server_pidfilename {
  123. my ($proto, $ipver, $idnum) = @_;
  124. my $trailer = '_server.pid';
  125. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  126. }
  127. #***************************************************************************
  128. # Return file name for server log file.
  129. #
  130. sub server_logfilename {
  131. my ($logdir, $proto, $ipver, $idnum) = @_;
  132. my $trailer = '_server.log';
  133. $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/);
  134. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  135. }
  136. #***************************************************************************
  137. # Return file name for server commands file.
  138. #
  139. sub server_cmdfilename {
  140. my ($logdir, $proto, $ipver, $idnum) = @_;
  141. my $trailer = '_server.cmd';
  142. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  143. }
  144. #***************************************************************************
  145. # Return file name for server input file.
  146. #
  147. sub server_inputfilename {
  148. my ($logdir, $proto, $ipver, $idnum) = @_;
  149. my $trailer = '_server.input';
  150. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  151. }
  152. #***************************************************************************
  153. # Return file name for server output file.
  154. #
  155. sub server_outputfilename {
  156. my ($logdir, $proto, $ipver, $idnum) = @_;
  157. my $trailer = '_server.output';
  158. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  159. }
  160. #***************************************************************************
  161. # Return file name for main or primary sockfilter pid file.
  162. #
  163. sub mainsockf_pidfilename {
  164. my ($proto, $ipver, $idnum) = @_;
  165. die "unsupported protocol: '$proto'" unless($proto &&
  166. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  167. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid';
  168. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  169. }
  170. #***************************************************************************
  171. # Return file name for main or primary sockfilter log file.
  172. #
  173. sub mainsockf_logfilename {
  174. my ($logdir, $proto, $ipver, $idnum) = @_;
  175. die "unsupported protocol: '$proto'" unless($proto &&
  176. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  177. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log';
  178. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  179. }
  180. #***************************************************************************
  181. # Return file name for data or secondary sockfilter pid file.
  182. #
  183. sub datasockf_pidfilename {
  184. my ($proto, $ipver, $idnum) = @_;
  185. die "unsupported protocol: '$proto'" unless($proto &&
  186. (lc($proto) =~ /^ftps?$/));
  187. my $trailer = '_sockdata.pid';
  188. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  189. }
  190. #***************************************************************************
  191. # Return file name for data or secondary sockfilter log file.
  192. #
  193. sub datasockf_logfilename {
  194. my ($logdir, $proto, $ipver, $idnum) = @_;
  195. die "unsupported protocol: '$proto'" unless($proto &&
  196. (lc($proto) =~ /^ftps?$/));
  197. my $trailer = '_sockdata.log';
  198. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  199. }
  200. #***************************************************************************
  201. # End of library
  202. 1;