File: udp-banger.pl

package info (click to toggle)
squid 7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,440 kB
  • sloc: cpp: 184,513; ansic: 12,442; sh: 5,688; makefile: 5,247; perl: 2,560; sql: 326; python: 240; awk: 141; sed: 1
file content (130 lines) | stat: -rwxr-xr-x 3,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/local/bin/perl
#
## Copyright (C) 1996-2025 The Squid Software Foundation and contributors
##
## Squid software is distributed under GPLv2+ license and includes
## contributions from numerous individuals and organizations.
## Please see the COPYING and CONTRIBUTORS files for details.
##

# udp-banger.pl
#
# Duane Wessels, Dec 1995
#
# Usage: udp-banger.pl [host [port]] < url-list
#
# Sends a continuous stream of ICP queries to a cache.  Stdin is a list of
# URLs to request.  Run N of these at the same time to simulate a heavy
# neighbor cache load.

use Fcntl;
use Getopt::Std;
use IO::Socket;

$|=1;

getopts('qlnr');

$host=(shift || 'localhost') ;
$port=(shift || '3130') ;

# just copy this from src/proto.c
@CODES=(
    "ICP_INVALID",
    "ICP_QUERY",
    "UDP_HIT",
    "UDP_MISS",
    "ICP_ERR",
    "ICP_SEND",
    "ICP_SENDA",
    "ICP_DATABEG",
    "ICP_DATA",
    "ICP_DATAEND",
    "ICP_SECHO",
    "ICP_DECHO",
    "ICP_OP_UNUSED0",
    "ICP_OP_UNUSED1",
    "ICP_OP_UNUSED2",
    "ICP_OP_UNUSED3",
    "ICP_OP_UNUSED4",
    "ICP_OP_UNUSED5",
    "ICP_OP_UNUSED6",
    "ICP_OP_UNUSED7",
    "ICP_OP_UNUSED8",
    "UDP_RELOADING",
    "UDP_DENIED",
    "UDP_HIT_OBJ",
    "ICP_END"
    );

$sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'udp');
die "socket: $!\n" unless defined($sock);

chop($me=`uname -a|cut -f2 -d' '`);
$myip=(gethostbyname($me))[4];

$flags = fcntl ($sock, &F_GETFL, 0);
$flags |= &O_NONBLOCK;
die "fcntl O_NONBLOCK: $!\n" unless
    fcntl ($sock, &F_SETFL, $flags);

$flags = 0;
$flags |= 0x80000000;
$flags |= 0x40000000 if ($opt_n);
$flags = ~0;
$rn = 0;

$start = time;
while (<>) {
    chop;

    if ($opt_l) { # it's a Squid log file
        @stuff = split(/\s+/, $_);
        $_ = $stuff[6];
    }

    $len = length($_) + 1;
    $request_template = sprintf 'CCnNNa4a4x4a%d', $len;
    $request = pack($request_template,
        1,              # C opcode
        2,              # C version
        24 + $len,      # n length
        ++$rn,          # N reqnum
        $flags,         # N flags
        '',             # a4 pad
        $myip,          # a4 shostid
        $_);            # a%d payload
    die "send: $!\n" unless
        send($sock, $request, 0);
    $nsent++;
    $rin = '';
    vec($rin,fileno($sock),1) = 1;
    ($nfound,$timeleft) = select($rout=$rin, undef, undef, 2.0);
    next if ($nfound == 0);
    while (1) {
        last unless ($theiraddr = recv($sock, $reply, 1024, 0));
        next if $opt_q; # quietly carry on
        $nrecv++;
        if ($opt_r) {
            # only print send/receive rates
            if (($nsent & 0xFF) == 0) {
                $dt = time - $start;
                printf "SENT %d %f/sec; RECV %d %f/sec\n",
                    $nsent,
                    $nsent / $dt,
                    $nrecv,
                    $nrecv / $dt;
            }
        } else {
            # print the whole reply
            ($junk, $junk, $sourceaddr, $junk) = unpack($sockaddr, $theiraddr);
            @theirip = unpack('C4', $sourceaddr);
            ($type,$ver,$len,$flag,$p1,$p2,$payload) = unpack('CCnx4Nnnx4A', $reply);
            print join('.', @theirip) . ' ' . $CODES[$type] . " $_";
            print " hop=$p1" if ($opt_n);
            print " rtt=$p2" if ($opt_n);
            print "\n";
        }
    }
}