2 ######################################################################
4 # Calculate cookie value for UltraMonkey-L7 cpassive module.
8 # License: GNU General Public License (GPL)
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.
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.
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
24 ######################################################################
35 my ($ip_def) = inet_ntoa((gethostbyname(hostname))[4]);
37 my $cookie_def = 'CookieName';
48 "cookie=s" => \$cookie,
52 "decode=s" => \$encoded,
56 usage() if $help || !$opt;
59 decode($encoded) if (defined $encoded && $encoded =~ /^\d{15}$/);
63 print "Input IP address [$ip_def]: ";
66 # set default IP address
67 $ip = $ip_def if $ip eq q{};
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);
75 usage("Bad IP address.($ip)");
80 print "Input port number [$port_def]: ";
83 # set default port number
84 $port = $port_def if $port eq q{};
87 # Port number validation
88 if ($port =~ /^\d+$/ && $port < 65536) {
90 $port = unpack('n*', pack('S*', $port));
92 usage("Bad port number.($port)");
96 if (!defined $cookie) {
97 print "Input cookie name [$cookie_def]: ";
100 # set default cookie name
101 $cookie = $cookie_def if $cookie eq q{};
104 # show Set-Cookie header field
106 printf "Set-Cookie: %s=%010s%05s\n", $cookie, $ip, $port;
108 #--------------------------------------------------------------------
116 Usage: $0 [-i ip_address] [-p port] [-c cookie_name]
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.
127 #--------------------------------------------------------------------
128 # decode cookie value
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";