serverhelp.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2010, 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. # Return server characterization factors given a server id string.
  57. #
  58. sub serverfactors {
  59. my $server = $_[0];
  60. my $proto;
  61. my $ipvnum;
  62. my $idnum;
  63. if($server =~ /^((ftp|http|imap|pop3|smtp)s?)(\d*)(-ipv6|)$/) {
  64. $proto = $1;
  65. $idnum = ($3 && ($3 > 1)) ? $3 : 1;
  66. $ipvnum = ($4 && ($4 =~ /6$/)) ? 6 : 4;
  67. }
  68. elsif($server =~ /^(tftp|sftp|socks|ssh|rtsp)(\d*)(-ipv6|)$/) {
  69. $proto = $1;
  70. $idnum = ($2 && ($2 > 1)) ? $2 : 1;
  71. $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
  72. }
  73. else {
  74. die "invalid server id: $server"
  75. }
  76. return($proto, $ipvnum, $idnum);
  77. }
  78. #***************************************************************************
  79. # Return server name string formatted for presentation purposes
  80. #
  81. sub servername_str {
  82. my ($proto, $ipver, $idnum) = @_;
  83. $proto = uc($proto) if($proto);
  84. die "unsupported protocol: $proto" unless($proto &&
  85. ($proto =~ /^(((FTP|HTTP|IMAP|POP3|SMTP)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP|GOPHER))$/));
  86. $ipver = (not $ipver) ? 'ipv4' : lc($ipver);
  87. die "unsupported IP version: $ipver" unless($ipver &&
  88. ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6)$/));
  89. $ipver = ($ipver =~ /6$/) ? '-IPv6' : '';
  90. $idnum = 1 if(not $idnum);
  91. die "unsupported ID number: $idnum" unless($idnum &&
  92. ($idnum =~ /^(\d+)$/));
  93. $idnum = '' unless($idnum > 1);
  94. return "${proto}${idnum}${ipver}";
  95. }
  96. #***************************************************************************
  97. # Return server name string formatted for identification purposes
  98. #
  99. sub servername_id {
  100. my ($proto, $ipver, $idnum) = @_;
  101. return lc(servername_str($proto, $ipver, $idnum));
  102. }
  103. #***************************************************************************
  104. # Return server name string formatted for file name purposes
  105. #
  106. sub servername_canon {
  107. my ($proto, $ipver, $idnum) = @_;
  108. my $string = lc(servername_str($proto, $ipver, $idnum));
  109. $string =~ tr/-/_/;
  110. return $string;
  111. }
  112. #***************************************************************************
  113. # Return file name for server pid file.
  114. #
  115. sub server_pidfilename {
  116. my ($proto, $ipver, $idnum) = @_;
  117. my $trailer = '_server.pid';
  118. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  119. }
  120. #***************************************************************************
  121. # Return file name for server log file.
  122. #
  123. sub server_logfilename {
  124. my ($logdir, $proto, $ipver, $idnum) = @_;
  125. my $trailer = '_server.log';
  126. $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/);
  127. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  128. }
  129. #***************************************************************************
  130. # Return file name for server commands file.
  131. #
  132. sub server_cmdfilename {
  133. my ($logdir, $proto, $ipver, $idnum) = @_;
  134. my $trailer = '_server.cmd';
  135. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  136. }
  137. #***************************************************************************
  138. # Return file name for server input file.
  139. #
  140. sub server_inputfilename {
  141. my ($logdir, $proto, $ipver, $idnum) = @_;
  142. my $trailer = '_server.input';
  143. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  144. }
  145. #***************************************************************************
  146. # Return file name for server output file.
  147. #
  148. sub server_outputfilename {
  149. my ($logdir, $proto, $ipver, $idnum) = @_;
  150. my $trailer = '_server.output';
  151. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  152. }
  153. #***************************************************************************
  154. # Return file name for main or primary sockfilter pid file.
  155. #
  156. sub mainsockf_pidfilename {
  157. my ($proto, $ipver, $idnum) = @_;
  158. die "unsupported protocol: $proto" unless($proto &&
  159. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  160. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid';
  161. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  162. }
  163. #***************************************************************************
  164. # Return file name for main or primary sockfilter log file.
  165. #
  166. sub mainsockf_logfilename {
  167. my ($logdir, $proto, $ipver, $idnum) = @_;
  168. die "unsupported protocol: $proto" unless($proto &&
  169. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  170. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log';
  171. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  172. }
  173. #***************************************************************************
  174. # Return file name for data or secondary sockfilter pid file.
  175. #
  176. sub datasockf_pidfilename {
  177. my ($proto, $ipver, $idnum) = @_;
  178. die "unsupported protocol: $proto" unless($proto &&
  179. (lc($proto) =~ /^ftps?$/));
  180. my $trailer = '_sockdata.pid';
  181. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  182. }
  183. #***************************************************************************
  184. # Return file name for data or secondary sockfilter log file.
  185. #
  186. sub datasockf_logfilename {
  187. my ($logdir, $proto, $ipver, $idnum) = @_;
  188. die "unsupported protocol: $proto" unless($proto &&
  189. (lc($proto) =~ /^ftps?$/));
  190. my $trailer = '_sockdata.log';
  191. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  192. }
  193. #***************************************************************************
  194. # End of library
  195. 1;