blib/lib/WebCache/ICP.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 250 CC 20 SCAR 21.1
CC
20
Cov
0%
send
20.0
new
12.0
opcode
12.0
CRAP 8.2
1 # The WebCache::ICP Perl module
2 # $Id: ICP.pm,v 1.1 1999/04/27 16:35:21 martin Exp $
3  
4 # Copyright (c) 1999 Martin Hamilton. All rights reserved.
5  
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8  
9 package WebCache::ICP;
10  
11 use strict;
12 use vars qw($VERSION);
13  
14 use Carp;
15 use Socket;
16 use vars qw($VERSION %CODES);
17  
18 $VERSION = "1.00";
19  
20  
21 # ICP request and response packet opcodes
22 %CODES=(
23 "OP_INVALID" => 0,
24 "OP_QUERY" => 1,
25 "OP_HIT" => 2,
26 "OP_MISS" => 3,
27 "OP_ERR" => 4,
28 "UNUSED5" => 5,
29 "UNUSED6" => 6,
30 "UNUSED7" => 7,
31 "UNUSED8" => 8,
32 "UNUSED9" => 9,
33 "OP_SECHO" => 10,
34 "OP_DECHO" => 11,
35 "UNUSED12" => 12,
36 "UNUSED13" => 13,
37 "UNUSED14" => 14,
38 "UNUSED15" => 15,
39 "UNUSED16" => 16,
40 "UNUSED17" => 17,
41 "UNUSED18" => 18,
42 "UNUSED19" => 19,
43 "UNUSED20" => 20,
44 "OP_MISS_NOFETCH" => 21,
45 "OP_DENIED" => 22,
46 "OP_HIT_OBJ" => 23,
47 );
48  
49  
50 sub new {
51 my ($this, $that) = @_;
52 my ($class) = ref($this) || $this;
53 my ($self) = [];
54 bless $self, $class;
55  
56 $self = $self->burst($that) if defined($that);
57  
58 unless (defined($that)) {
59 # some vaguely plausible defaults
60 $self->opcode("OP_QUERY");
61 $self->version(2);
62 $self->sequence(1);
63 $self->options(0);
64 $self->option_data(0);
65 }
66  
67 return $self;
68 }
69  
70  
71 # get/set header/payload values out of a packet
72 sub opcode {
73 my ($self, $opcode) = @_;
74  
75 return $self->[0] unless defined($opcode);
76  
77 if ($opcode =~ /^\d+$/) {
78 $self->[0] = $opcode;
79 } else {
80 $self->[0] = $CODES{$opcode};
81 }
82 }
83 sub version { my $self = shift; @_ ? $self->[1] = shift : $self->[1]; }
84 sub length { my $self = shift; @_ ? $self->[2] = shift : $self->[2]; }
85 sub sequence { my $self = shift; @_ ? $self->[3] = shift : $self->[3]; }
86 sub options { my $self = shift; @_ ? $self->[4] = shift : $self->[4]; }
87 sub option_data { my $self = shift; @_ ? $self->[5] = shift : $self->[5]; }
88 sub peer_addr { my $self = shift; @_ ? $self->[6] = shift : $self->[6]; }
89 sub payload { my $self = shift; @_ ? $self->[7] = shift : $self->[7]; }
90  
91  
92 # turn an ICP object into a wire-format packet
93 sub assemble {
94 my ($self) = @_;
95 my ($payload_length);
96 my ($packet_template);
97  
98 # fixed size header is 20 bytes
99 if ($self->opcode == 1) {
100 # OP_QUERY (1) prefixes URL with 4 byte addr
101 $payload_length = length($self->payload) + 4;
102 $packet_template = "CCnNNNNN";
103 } else {
104 $payload_length = length($self->payload) + 0;
105 $packet_template = "CCnNNNN";
106 }
107  
108 my ($packet) = pack($packet_template,
109 int($self->opcode), # C opcode
110 2, # C protocol version
111 20 + $payload_length, # n packet length
112 int($self->sequence), # N sequence number
113 int($self->options), # N option flags
114 0, # N option data
115 0); # N client IP addr
116  
117 $packet .= $self->payload;
118 #$packet .= '\0'; # XXX URL is null-terminated?
119  
120 return $packet;
121 }
122  
123  
124 # burst open wire-format packet and pull out ICP object
125 sub burst {
126 my ($self, $data) = @_;
127  
128 # XXX should be capable of using burst to unpack type 1 packets
129 my ($payload_length) = length($data) - 20;
130 my ($packet_template) = sprintf 'CCnNNNNa%d', $payload_length;
131 return bless [ unpack($packet_template, $data) ], "WebCache::ICP";
132 }
133  
134  
135 # dump out header/payload from an ICP object in human readable form
136 sub dump {
137 my ($self) = @_;
138  
139 printf "Opcode: %0x\n", $self->opcode;
140 printf "Version: %0x\n", $self->version;
141 printf "Packet length: %d\n", $self->length;
142 printf "Sequence number: %d\n", $self->sequence;
143 printf "Option flags: %0x\n", $self->options;
144 printf "Option data: %0x\n", $self->option_data;
145 printf "Peer address: %0x\n", $self->peer_addr;
146 printf "Payload: %s\n", $self->payload;
147 }
148  
149  
150 # dump out ICP object in hex
151 sub hex_dump {
152 my ($self) = @_;
153 my ($packet_template) = sprintf 'CCnNNNNa%d', ($self->length - 20);
154 my ($packet) = pack($packet_template, $self->opcode, $self->version,
155 $self->length, $self->sequence, $self->options,
156 $self->option_data, $self->peer_addr, $self->payload);
157 print unpack("H*", $packet) . "\n";
158 }
159  
160  
161 # send ICP packet to peer
162 sub send {
163 my ($self, %args) = @_;
164 my ($sock, $flags, $iaddr, $sin, $request);
165  
166 if (defined($args{fd})) {
167 $sock = $args{fd};
168 $sin = $args{sin};
169 } else {
170 my $proto = getprotobyname('udp');
171 socket(SOCK, PF_INET, SOCK_DGRAM, $proto);
172 $sock = *SOCK{IO};
173 $iaddr = gethostbyname($args{host} || "localhost");
174 $sin = sockaddr_in(($args{port} || 3130), $iaddr);
175 }
176  
177 $request = $args{packet} || $self->assemble;
178 return -3 unless send($sock, $request, 0, $sin);
179 close(SOCK) unless defined($args{fd});
180 }
181  
182  
183 # receive ICP packet from peer
184 sub recv {
185 my ($self, %args) = @_;
186 my ($flags, $iaddr, $sin, $response);
187 my ($sock, $flags, $iaddr, $sin, $request);
188 my ($their_sin);
189  
190 if (defined($args{fd})) {
191 $sock = $args{fd};
192 } else {
193 my $proto = getprotobyname('udp');
194 socket(SOCK, PF_INET, SOCK_DGRAM, $proto);
195 $sock = *SOCK{IO};
196 $sin = sockaddr_in(($args{port} || 3130), pack("N", 0));
197 bind($sock, $sin) || croak "$0: couldn't bind: $!";;
198 }
199  
200 ($their_sin = recv($sock, $response, 1024, 0))
201 || croak "$0: couldn't recv: $!";
202 close(SOCK) unless defined($args{fd});
203 return $response;
204 }
205  
206  
207 # implements an ICP server
208 sub server {
209 my ($self, %args) = @_;
210 my ($flags, $iaddr, $sin, $response);
211 my($their_sin,$rin,$rout,$nfound,$timeleft);
212 my ($sock, $flags, $iaddr, $sin, $request);
213 my ($their_sin);
214 my ($callback);
215  
216 if (defined($args{fd})) {
217 $sock = $args{fd};
218 } else {
219 my $proto = getprotobyname('udp');
220 socket(SOCK, PF_INET, SOCK_DGRAM, $proto) || croak "$0: socket: $!";
221 $sock = *SOCK{IO};
222 $sin = sockaddr_in(($args{port} || 3130), pack("N", 0));
223 bind($sock, $sin) || croak "$0: couldn't bind: $!";;
224 }
225  
226 if (defined($args{callback})) {
227 $callback = $args{callback};
228 } else {
229 $callback = \&server_stub;
230 }
231  
232 while(1) {
233 ($their_sin = recv($sock, $request, 1024, 0))
234 || croak "$0: couldn't recv: $!";
235 $self->$callback($sock, $their_sin, $request);
236 }
237 }
238  
239  
240 # default callback for server
241 sub server_stub {
242 my ($self, $fd, $sin, $request) = @_;
243  
244 $self = $self->burst($request);
245 $self->dump;
246 $self->opcode("OP_HIT");
247 $self->send ( fd => $fd, sin => $sin );
248 print "---------------------------------------------------------\n";
249 }
250  
251  
252 1;
253 __END__