]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/icp-test.pl
SourceFormat Enforcement
[thirdparty/squid.git] / scripts / icp-test.pl
1 #!/usr/local/bin/perl
2 #
3 ## Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 ##
5 ## Squid software is distributed under GPLv2+ license and includes
6 ## contributions from numerous individuals and organizations.
7 ## Please see the COPYING and CONTRIBUTORS files for details.
8 ##
9
10 # icp-test.pl
11 #
12 # Duane Wessels, Nov 1996
13 #
14 # Usage: icp-test.pl host:port ... < url-list
15 #
16 # Sends a continuous stream of ICP queries to a set of caches. Stdin is
17 # a list of URLs to request.
18
19 require 'getopts.pl';
20
21 $|=1;
22
23 &Getopts('n');
24
25 # just copy this from src/proto.c
26 @CODES=(
27 "ICP_INVALID",
28 "ICP_QUERY",
29 "UDP_HIT",
30 "UDP_MISS",
31 "ICP_ERR",
32 "ICP_SEND",
33 "ICP_SENDA",
34 "ICP_DATABEG",
35 "ICP_DATA",
36 "ICP_DATAEND",
37 "ICP_SECHO",
38 "ICP_DECHO",
39 "ICP_OP_UNUSED0",
40 "ICP_OP_UNUSED1",
41 "ICP_OP_UNUSED2",
42 "ICP_OP_UNUSED3",
43 "ICP_OP_UNUSED4",
44 "ICP_OP_UNUSED5",
45 "ICP_OP_UNUSED6",
46 "ICP_OP_UNUSED7",
47 "ICP_OP_UNUSED8",
48 "UDP_RELOADING",
49 "UDP_DENIED",
50 "UDP_HIT_OBJ",
51 "ICP_END"
52 );
53
54 require 'sys/socket.ph';
55
56 $sockaddr = 'S n a4 x8';
57 ($name, $aliases, $proto) = getprotobyname("udp");
58 $thissock = pack($sockaddr, &AF_INET, 0, "\0\0\0\0");
59
60 chop($me=`uname -a|cut -f2 -d' '`);
61 $myip=(gethostbyname($me))[4];
62
63 die "socket: $!\n" unless
64 socket (SOCK, &AF_INET, &SOCK_DGRAM, $proto);
65
66 $flags = 0;
67 $flags |= 0x80000000;
68 $flags |= 0x40000000 if ($opt_n);
69 $flags = ~0;
70
71 while ($ARGV[0] =~ /([^:]+):(\d+)/) {
72 $host = $1;
73 $port = $2;
74 ($fqdn, $aliases, $type, $len, $themaddr) = gethostbyname($host);
75 $ADDR{$host} = pack('Sna4x8', &AF_INET, $port, $themaddr);
76 $ip = join('.', unpack('C4', $themaddr));
77 $FQDN{$ip} = $fqdn;
78 shift;
79 }
80
81 $rn = 0;
82 while (<>) {
83 print;
84 chop;
85 $len = length($_) + 1;
86 $request_template = sprintf 'CCnNNa4a4x4a%d', $len;
87 $request = pack($request_template,
88 1, # C opcode
89 2, # C version
90 24 + $len, # n length
91 ++$rn, # N reqnum
92 $flags, # N flags
93 '', # a4 pad
94 $myip, # a4 shostid
95 $_); # a%d payload
96 $n = 0;
97 foreach $host (keys %ADDR) {
98 $port = $PORT{$host};
99 @ip = split('\.', $IP{$host});
100 $them = pack('SnC4x8', &AF_INET, $port, @ip);
101 ($sport,@IP) = unpack('x2nC4x8', $ADDR{$host});
102 die "send: $!\n" unless send(SOCK, $request, 0, $ADDR{$host});
103 $n++;
104 }
105 while ($n > 0) {
106 $rin = '';
107 vec($rin,fileno(SOCK),1) = 1;
108 ($nfound,$timeleft) = select($rout=$rin, undef, undef, 2.0);
109 last if ($nfound == 0);
110 die "recv: $!\n" unless
111 $theiraddr = recv(SOCK, $reply, 1024, 0);
112 ($junk, $junk, $sourceaddr, $junk) = unpack($sockaddr, $theiraddr);
113 $ip = join('.', unpack('C4', $sourceaddr));
114 ($type,$ver,$len,$flag,$p1,$p2,$payload) = unpack('CCnx4Nnnx4A', $reply);
115 printf "\t%-20.20s %-10.10s",
116 $FQDN{$ip},
117 $CODES[$type];
118 print " hop=$p1" if ($opt_n);
119 print " rtt=$p2" if ($opt_n);
120 print "\n";
121 $n--;
122 }
123 }
124