OSDN Git Service

Fixed chkconfig option
[ultramonkey-l7/ultramonkey-l7-v2.git] / src / l7cookie
1 #!/usr/bin/perl
2 ######################################################################
3 # l7cookie
4 # Calculate cookie value for UltraMonkey-L7 cpassive module.
5 #
6 # 2009 (C) NTT COMWARE
7 #
8 # License:   GNU General Public License (GPL)
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2 of the
13 # License, or (at your option) any later version.
14
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 # 02110-1301 USA
24 ######################################################################
25
26 use strict;
27 use warnings;
28 use Getopt::Long;
29 use Sys::Hostname;
30 use Socket;
31
32 $| = 1;
33
34 # default value
35 my ($ip_def)   = inet_ntoa((gethostbyname(hostname))[4]);
36 my $port_def   = 80;
37 my $cookie_def = 'CookieName';
38
39 # option value
40 my $ip;
41 my $port;
42 my $cookie;
43 my $help;
44 my $encoded;
45
46 # get options
47 my $opt = GetOptions(
48     "cookie=s" => \$cookie,
49     "ip=s"     => \$ip,
50     "port=s"   => \$port,
51     "help"     => \$help,
52     "decode=s" => \$encoded,
53 );
54
55 # help
56 usage() if $help || !$opt;
57
58 # decode cookie value
59 decode($encoded) if (defined $encoded && $encoded =~ /^\d{15}$/);
60
61 # get IP address
62 if (!defined $ip) {
63     print "Input IP address [$ip_def]: ";
64     $ip = <>;
65     $ip =~ s/\s//g;
66     # set default IP address
67     $ip = $ip_def if $ip eq q{};
68 }
69
70 # IP address validation
71 if ($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&
72     $1 < 256 && $2 < 256 && $3 < 256 && $4 < 256) {
73     $ip = $1 + (($2 + (($3 + ($4 << 8)) << 8)) << 8);
74 } else {
75     usage("Bad IP address.($ip)");
76 }
77
78 # get port number
79 if (!defined $port) {
80     print "Input port number [$port_def]: ";
81     $port = <>;
82     $port =~ s/\s//g;
83     # set default port number
84     $port = $port_def if $port eq q{};
85 }
86
87 # Port number validation
88 if ($port =~ /^\d+$/ && $port < 65536) {
89     # same as htons(3)
90     $port = unpack('n*', pack('S*', $port));
91 } else {
92     usage("Bad port number.($port)");
93 }
94
95 # get cookie name
96 if (!defined $cookie) {
97     print "Input cookie name [$cookie_def]: ";
98     $cookie = <>;
99     $cookie =~ s/\s//g;
100     # set default cookie name
101     $cookie = $cookie_def if $cookie eq q{};
102 }
103
104 # show Set-Cookie header field
105 print  "\n";
106 printf "Set-Cookie: %s=%010s%05s\n", $cookie, $ip, $port;
107
108 #--------------------------------------------------------------------
109 # usage
110 sub usage {
111     my $msg = shift;
112     if ($msg) {
113         print $msg, "\n\n";
114     }
115     print <<"__USAGE__";
116 Usage: $0 [-i ip_address] [-p port] [-c cookie_name]
117
118 -i, --ip      Set IP address of real server.
119 -p, --port    Set port number of real server.
120 -c, --cookie  Set cookie name of UltraMonkey-L7.
121 -h, --help    Show this usage.
122 __USAGE__
123
124     exit;
125 }
126
127 #--------------------------------------------------------------------
128 # decode cookie value
129 sub decode {
130     my $encoded = shift;
131     my $_ip   = substr($encoded, 0, 10);
132     my $_port = substr($encoded, 10, 5);
133     print "IP address : ", inet_ntoa(scalar reverse pack('H*', sprintf('%08X', $_ip))), "\n";
134     print "Port number: ", unpack('S*', pack('n*', $_port)), "\n";
135     exit;
136 }