]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/udp-banger.pl
Source Format Enforcement (#532)
[thirdparty/squid.git] / scripts / udp-banger.pl
CommitLineData
090089c4 1#!/usr/local/bin/perl
a151895d 2#
77b1029d 3## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
a151895d
AJ
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##
090089c4 9
10# udp-banger.pl
11#
12# Duane Wessels, Dec 1995
13#
14# Usage: udp-banger.pl [host [port]] < url-list
15#
16# Sends a continuous stream of ICP queries to a cache. Stdin is a list of
17# URLs to request. Run N of these at the same time to simulate a heavy
18# neighbor cache load.
19
4d62b0af 20use Fcntl;
21use Getopt::Std;
22use IO::Socket;
090089c4 23
24$|=1;
25
4d62b0af 26getopts('qlnr');
1f67cd29 27
090089c4 28$host=(shift || 'localhost') ;
29$port=(shift || '3130') ;
30
30a4f2a8 31# just copy this from src/proto.c
32@CODES=(
33 "ICP_INVALID",
34 "ICP_QUERY",
35 "UDP_HIT",
36 "UDP_MISS",
37 "ICP_ERR",
38 "ICP_SEND",
39 "ICP_SENDA",
40 "ICP_DATABEG",
41 "ICP_DATA",
42 "ICP_DATAEND",
43 "ICP_SECHO",
44 "ICP_DECHO",
45 "ICP_OP_UNUSED0",
46 "ICP_OP_UNUSED1",
47 "ICP_OP_UNUSED2",
48 "ICP_OP_UNUSED3",
49 "ICP_OP_UNUSED4",
50 "ICP_OP_UNUSED5",
51 "ICP_OP_UNUSED6",
52 "ICP_OP_UNUSED7",
53 "ICP_OP_UNUSED8",
4c5cabe1 54 "UDP_RELOADING",
30a4f2a8 55 "UDP_DENIED",
56 "UDP_HIT_OBJ",
57 "ICP_END"
58);
090089c4 59
4d62b0af 60$sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'udp');
61die "socket: $!\n" unless defined($sock);
090089c4 62
63chop($me=`uname -a|cut -f2 -d' '`);
64$myip=(gethostbyname($me))[4];
65
4d62b0af 66$flags = fcntl ($sock, &F_GETFL, 0);
db77b9ec 67$flags |= &O_NONBLOCK;
68die "fcntl O_NONBLOCK: $!\n" unless
4d62b0af 69 fcntl ($sock, &F_SETFL, $flags);
db77b9ec 70
1f67cd29 71$flags = 0;
72$flags |= 0x80000000;
73$flags |= 0x40000000 if ($opt_n);
74$flags = ~0;
db77b9ec 75$rn = 0;
1f67cd29 76
db77b9ec 77$start = time;
090089c4 78while (<>) {
79 chop;
4d62b0af 80
81 if ($opt_l) { # it's a Squid log file
82 @stuff = split(/\s+/, $_);
83 $_ = $stuff[6];
84 }
85
db77b9ec 86 $len = length($_) + 1;
87 $request_template = sprintf 'CCnNNa4a4x4a%d', $len;
88 $request = pack($request_template,
89 1, # C opcode
90 2, # C version
91 24 + $len, # n length
92 ++$rn, # N reqnum
93 $flags, # N flags
94 '', # a4 pad
95 $myip, # a4 shostid
96 $_); # a%d payload
090089c4 97 die "send: $!\n" unless
42cc97c7 98 send($sock, $request, 0);
db77b9ec 99 $nsent++;
4cc6d51c 100 $rin = '';
4d62b0af 101 vec($rin,fileno($sock),1) = 1;
4cc6d51c 102 ($nfound,$timeleft) = select($rout=$rin, undef, undef, 2.0);
103 next if ($nfound == 0);
db77b9ec 104 while (1) {
4d62b0af 105 last unless ($theiraddr = recv($sock, $reply, 1024, 0));
106 next if $opt_q; # quietly carry on
db77b9ec 107 $nrecv++;
108 if ($opt_r) {
109 # only print send/receive rates
110 if (($nsent & 0xFF) == 0) {
111 $dt = time - $start;
112 printf "SENT %d %f/sec; RECV %d %f/sec\n",
113 $nsent,
114 $nsent / $dt,
115 $nrecv,
116 $nrecv / $dt;
117 }
118 } else {
119 # print the whole reply
120 ($junk, $junk, $sourceaddr, $junk) = unpack($sockaddr, $theiraddr);
121 @theirip = unpack('C4', $sourceaddr);
122 ($type,$ver,$len,$flag,$p1,$p2,$payload) = unpack('CCnx4Nnnx4A', $reply);
123 print join('.', @theirip) . ' ' . $CODES[$type] . " $_";
124 print " hop=$p1" if ($opt_n);
125 print " rtt=$p2" if ($opt_n);
126 print "\n";
127 }
128 }
090089c4 129}
130