Hotline/Protocol/Packet.pm

filter: Statementstmt 17.1% 29 / 169 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 3 totaltotal 12.3% 38 / 308 CC 49 SCAR 76.0
CC
49
Cov
10%
read_parse
2450.0
new
2.0
clear
2.0
CRAP 2001.0
« Hotline/Client.pm ↑ Summary Hotline/Shared.pm »
1 package Net::Hotline::Protocol::Packet;
2  
3 ## Copyright(c) 1998-2002 by John C. Siracusa. All rights reserved. This
4 ## program is free software; you can redistribute it and/or modify it under
5 ## the same terms as Perl itself.
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::Hotline::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::Hotline::FileListItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::Hotline::Protocol::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Net::Hotline::Shared qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use Net::Hotline::Constants
18 1 ▶ qw(HTLC_DATA_PCHAT_SUBJECT HTLC_DATA_RFLT HTLC_EWOULDBLOCK HTLC_NEWLINE
Subroutine BEGIN: called CC=1 SCAR=0.0
19 HTLS_DATA_AGREEMENT HTLS_DATA_CHAT HTLS_DATA_COLOR
20 HTLS_DATA_FILE_COMMENT HTLS_DATA_FILE_CREATOR HTLS_DATA_FILE_CTIME
21 HTLS_DATA_FILE_ICON HTLS_DATA_FILE_LIST HTLS_DATA_FILE_MTIME
22 HTLS_DATA_FILE_NAME HTLS_DATA_FILE_SIZE HTLS_DATA_FILE_TYPE
23 HTLS_DATA_HTXF_REF HTLS_DATA_HTXF_SIZE HTLS_DATA_ICON HTLS_DATA_MSG
24 HTLS_DATA_NEWS HTLS_DATA_NEWS_POST HTLS_DATA_NICKNAME
25 HTLS_DATA_PCHAT_REF HTLS_DATA_SERVER_MSG HTLS_DATA_SOCKET
26 HTLS_DATA_TASK_ERROR HTLS_DATA_USER_INFO HTLS_DATA_USER_LIST
27 1 HTLS_HDR_TASK SIZEOF_HL_PROTO_HDR HTLS_DATA_REPLY HTLS_DATA_IS_REPLY);
28  
29 1 $VERSION = '0.80';
30  
31 sub new
32 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my($class) = shift;
34 0 my($self);
35  
36 0 $self =
37 {
38 'PROTO_HEADER' => undef,
39  
40 'USER_LIST' => undef,
41 'FILE_LIST' => undef,
42 'USER_INFO' => undef,
43 'NEWS' => undef,
44  
45 'SOCKET' => undef,
46 'ICON' => undef,
47 'COLOR' => undef,
48 'NICK' => undef,
49 'TASK_ERROR' => undef,
50 'DATA' => undef,
51  
52 'FILE_ICON' => undef,
53 'FILE_TYPE' => undef,
54 'FILE_CREATOR' => undef,
55 'FILE_SIZE' => undef,
56 'FILE_NAME' => undef,
57 'FILE_COMMENT' => undef,
58 'FILE_CTIME' => undef,
59 'FILE_MTIME' => undef,
60  
61 'HTXF_SIZE' => undef,
62 'HTXF_REF' => undef,
63 'HTXF_RFLT' => undef,
64  
65 'PCHAT_REF' => undef,
66  
67 'IS_REPLY' => undef,
68 'REPLY_TO' => undef,
69  
70 'TYPE' => undef
71 };
72  
73 0 bless $self, $class;
74 0 return $self;
75 }
76  
77 sub clear
78 0 ▶{
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my($self) = shift;
80  
81 $self->{'PROTO_HEADER'} =
82  
83 $self->{'USER_LIST'} =
84 $self->{'FILE_LIST'} =
85 $self->{'USER_INFO'} =
86 $self->{'NEWS'} =
87  
88 $self->{'SOCKET'} =
89 $self->{'ICON'} =
90 $self->{'COLOR'} =
91 $self->{'NICK'} =
92 $self->{'TASK_ERROR'} =
93 $self->{'DATA'} =
94  
95 $self->{'FILE_ICON'} =
96 $self->{'FILE_TYPE'} =
97 $self->{'FILE_CREATOR'} =
98 $self->{'FILE_SIZE'} =
99 $self->{'FILE_NAME'} =
100 $self->{'FILE_COMMENT'} =
101 $self->{'FILE_CTIME'} =
102 $self->{'FILE_MTIME'} =
103  
104 $self->{'HTXF_SIZE'} =
105 $self->{'HTXF_REF'} =
106 $self->{'HTXF_RFLT'} =
107  
108 $self->{'PCHAT_REF'} =
109  
110 $self->{'IS_REPLY'} =
111 $self->{'REPLY_TO'} =
112  
113 0 $self->{'TYPE'} = undef;
114 }
115  
116 sub read_parse
117 0 ▶{
Subroutine read_parse: not called CC=49 SCAR=78.0
Pod undocumented
118 0 my($self, $fh, $blocking) = @_;
119  
120 0 my($data, $length, $atom_count, $atom_type, $atom_len, $read_err,
121 $nick, $socket, $icon, $user_type, $name, $color, $read);
122  
123 0 $self->clear();
124  
125 0 ▶ unless($fh->opened())
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->opened) 0 0 0
126 {
127 0 $self->{'TYPE'} = 'DISCONNECTED';
128 0 return(1);
129 }
130  
131 0 $read = _read($fh, \$data, SIZEOF_HL_PROTO_HDR, $blocking);
132 0 $read_err = 0 + $!; # Get the numerical value of the magical $!
133  
134 0 ▶ unless(defined($read) && $read > 0)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read and $read > 0) 0 0 0
Condition0%0/3
defined $read and $read > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $read and $read > 0
defined $read $read > 0
Truth table0%0/3
A and B
A= defined $readB= $read > 0
ABresult
0X0
100
111
135 {
136 0 ▶ if($read_err == EWOULDBLOCK || $read_err == EAGAIN)
Branch: 2 branches
BranchTrueFalseTotal
if ($read_err == 11 or $read_err == 11) { } 0 0 0
elsif ($read_err == 104 or $read_err == 103 or $read_err == 107) { } 0 0 0
Condition0%0/3
$read_err == 11 or $read_err == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$read_err == 11 or $read_err == 11
$read_err == 11 $read_err == 11
Truth table0%0/3
A or B
A= $read_err == 11B= $read_err == 11
ABresult
1X1
011
000
137 {
138 #_debug("WOULDBLOCK\n");
139 0 return(HTLC_EWOULDBLOCK);
140 }
141 ▶ elsif($read_err == ECONNRESET || $read_err == ECONNABORTED ||
Condition0%0/6
$read_err == 104 or $read_err == 103 or $read_err == 107
l!l&&r!l&&!r
000
$read_err == 104 or $read_err == 103
l!l&&r!l&&!r
000
MC/DC0%0/3
$read_err == 104 or $read_err == 103 or $read_err == 107
$read_err == 104 $read_err == 103 $read_err == 107
Truth table0%0/4
A or B or C
A= $read_err == 104B= $read_err == 103C= $read_err == 107
ABCresult
1XX1
01X1
0011
0000
142 $read_err == ENOTCONN)
143 {
144 #_debug("DISCONNECTED\n");
145 0 $self->clear();
146 0 $self->{'TYPE'} = 'DISCONNECTED';
147 0 return(1);
148 }
149 else
150 {
151 # I'm assuming this is a MacPerl bug: sysread() sometimes returns
152 # undefined without setting $!. I use the "shrug and continue"
153 # method here and just treat it as an idle event.
154 0 ▶ return(HTLC_EWOULDBLOCK) if($^O eq 'MacOS');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MacOS" 0 0 0
155  
156 # It's fatal on non-Mac OS systems, however.
157 0 die "sysread() error($read_err): $!\n";
158  
159 # I'm also getting:
160 #
161 # sysread() error(145): Connection timed out
162 #
163 # On Solaris. Hmmmm...
164 }
165 }
166  
167 0 _debug("Packet data:\n", _hexdump($data));
168  
169 0 $self->{'PROTO_HEADER'} = new Net::Hotline::Protocol::Header($data);
170  
171 0 $length = unpack("N", $self->{'PROTO_HEADER'}->len());
172 0 $self->{'TYPE'} = unpack("N", $self->{'PROTO_HEADER'}->type());
173  
174 0 ▶ if($self->{'TYPE'} == HTLS_HDR_TASK)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"TYPE"} == 65536) 0 0 0
175 {
176 0 $self->{'TASK_NUM'} = unpack("N", $self->{'PROTO_HEADER'}->seq());
177 }
178  
179 0 $length -= _read($fh, \$atom_count, 2);
180 0 $atom_count = unpack("n", $atom_count);
181  
182 0 _debug("Atom count: $atom_count\n");
183  
184 0 ▶ for(; $atom_count != 0; $atom_count--)
Branch: 1 branch
BranchTrueFalseTotal
if ($atom_count != 0) 0 0 0
185 {
186 # This probably doesn't need to be here anymore, but just to be safe...
187 0 ▶ if($length < 4)
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 4) 0 0 0
188 {
189 0 $length -= _read($fh, \$data, $length);
190 0 _debug("Slurped up < 4 bytes, length = $length\n");
191 0 return(1);
192 }
193  
194 0 $length -= _read($fh, \$atom_type, 2);
195 0 $length -= _read($fh, \$atom_len, 2);
196  
197 0 _debug("Atom type:\n", _hexdump($atom_type));
198 0 _debug("Atom length:\n", _hexdump($atom_len));
199  
200 0 $atom_type = unpack("n", $atom_type);
201 0 $atom_len = unpack("n", $atom_len);
202  
203 0 ▶ if($atom_type == HTLS_DATA_USER_LIST)
Branch: 22 branches
BranchTrueFalseTotal
if ($atom_type == 300) { } 0 0 0
elsif ($atom_type == 200) { } 0 0 0
elsif ($atom_type == 103) { } 0 0 0
elsif ($atom_type == 104) { } 0 0 0
elsif ($atom_type == 112) { } 0 0 0
elsif ($atom_type == 102) { } 0 0 0
elsif ($atom_type == 100) { } 0 0 0
elsif ($atom_type == 213) { } 0 0 0
elsif ($atom_type == 205) { } 0 0 0
elsif ($atom_type == 206) { } 0 0 0
elsif ($atom_type == 207) { } 0 0 0
elsif ($atom_type == 201) { } 0 0 0
elsif ($atom_type == 210) { } 0 0 0
elsif ($atom_type == 208) { } 0 0 0
elsif ($atom_type == 209) { } 0 0 0
elsif ($atom_type == 114) { } 0 0 0
elsif ($atom_type == 113) { } 0 0 0
elsif ($atom_type == 214) { } 0 0 0
elsif ($atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115 or $atom_type == 101 or $atom_type == 109 or $atom_type == 101) { } 0 0 0
elsif ($atom_type == 108) { } 0 0 0
elsif ($atom_type == 107) { } 0 0 0
elsif ($atom_type == 203) { } 0 0 0
204 {
205 0 my($user_data, $user);
206  
207 0 $length -= _read($fh, \$user_data, $atom_len);
208  
209 0 $user = new Net::Hotline::User($user_data);
210  
211 0 _debug(" Nick: ", $user->nick(), "\n",
212 " Icon: ", $user->icon(), "\n",
213 "Socket: ", $user->socket(), "\n",
214 " Color: ", $user->color(), "\n");
215  
216 0 $self->{'USER_LIST'}->{$user->socket()} = $user;
217 }
218 elsif($atom_type == HTLS_DATA_FILE_LIST)
219 {
220 0 my($file_data, $file);
221  
222 0 $length -= _read($fh, \$file_data, $atom_len);
223  
224 0 $file = new Net::Hotline::FileListItem($file_data);
225  
226 0 _debug(" Type: ", $file->type(), "\n",
227 "Creator: ", $file->creator(), "\n",
228 " Size: ", $file->size(), "\n",
229 " Name: ", $file->name(), "\n");
230  
231 0 push(@{$self->{'FILE_LIST'}}, $file);
232 }
233 elsif($atom_type == HTLS_DATA_SOCKET)
234 {
235 0 $length -= _read($fh, \$socket, $atom_len);
236  
237 0 _debug("Socket: ", _hexdump($socket));
238  
239 # Older versions of the Hotline server sent socket numbers
240 # in 4 bytes. Newer versions send it in 2. Nice.
241 0 ▶ if($atom_len == 4)
Branch: 1 branch
BranchTrueFalseTotal
if ($atom_len == 4) { } 0 0 0
242 {
243 0 $self->{'SOCKET'} = unpack("N", $socket);
244 }
245 else
246 {
247 0 $self->{'SOCKET'} = unpack("n", $socket);
248 }
249 }
250 elsif($atom_type == HTLS_DATA_ICON)
251 {
252 0 $length -= _read($fh, \$icon, $atom_len);
253  
254 0 _debug("Icon: ", _hexdump($icon));
255  
256 0 $self->{'ICON'} = unpack("n", $icon);
257 }
258 elsif($atom_type == HTLS_DATA_COLOR)
259 {
260 0 $length -= _read($fh, \$color, $atom_len);
261  
262 0 _debug("Color: ", _hexdump($color));
263  
264 0 $self->{'COLOR'} = unpack("n", $color);
265 }
266 elsif($atom_type == HTLS_DATA_NICKNAME)
267 {
268 0 $length -= _read($fh, \$nick, $atom_len);
269  
270 0 _debug("Nick: ", _hexdump($nick));
271  
272 0 $self->{'NICK'} = $nick;
273 }
274 elsif($atom_type == HTLS_DATA_TASK_ERROR)
275 {
276 0 $length -= _read($fh, \$data, $atom_len);
277  
278 0 _debug("Task error:\n", _hexdump($data));
279  
280 0 $data =~ s/@{[HTLC_NEWLINE]}/\n/osg;
281 0 $self->{'TASK_ERROR'} = $data;
282 }
283 elsif($atom_type == HTLS_DATA_FILE_ICON)
284 {
285 0 $length -= _read($fh, \$data, $atom_len);
286  
287 0 _debug("File icon:\n", _hexdump($data));
288  
289 0 $self->{'FILE_ICON'} = unpack("n", $data);
290 }
291 elsif($atom_type == HTLS_DATA_FILE_TYPE)
292 {
293 0 $length -= _read($fh, \$data, $atom_len);
294  
295 0 _debug("File type:\n", _hexdump($data));
296  
297 0 $self->{'FILE_TYPE'} = $data;
298 }
299 elsif($atom_type == HTLS_DATA_FILE_CREATOR)
300 {
301 0 $length -= _read($fh, \$data, $atom_len);
302  
303 0 _debug("File creator:\n", _hexdump($data));
304  
305 0 $self->{'FILE_CREATOR'} = $data;
306 }
307 elsif($atom_type == HTLS_DATA_FILE_SIZE)
308 {
309 0 $length -= _read($fh, \$data, $atom_len);
310  
311 0 _debug("File size:\n", _hexdump($data));
312  
313 0 ▶ if($atom_len == 2) # Grrrrrrr...
Branch: 1 branch
BranchTrueFalseTotal
if ($atom_len == 2) { } 0 0 0
314 {
315 0 $self->{'FILE_SIZE'} = unpack("n", $data);
316 }
317 else
318 {
319 0 $self->{'FILE_SIZE'} = unpack("N", $data);
320 }
321 }
322 elsif($atom_type == HTLS_DATA_FILE_NAME)
323 {
324 0 $length -= _read($fh, \$data, $atom_len);
325  
326 0 _debug("File name:\n", _hexdump($data));
327  
328 0 $self->{'FILE_NAME'} = $data;
329 }
330 elsif($atom_type == HTLS_DATA_FILE_COMMENT)
331 {
332 0 $length -= _read($fh, \$data, $atom_len);
333  
334 0 _debug("File comment:\n", _hexdump($data));
335  
336 0 $self->{'FILE_COMMENT'} = $data;
337 }
338 elsif($atom_type == HTLS_DATA_FILE_CTIME)
339 {
340 0 $length -= _read($fh, \$data, $atom_len);
341  
342 0 $data =~ s/^....//;
343 0 _debug("File ctime:\n", _hexdump($data));
344  
345 0 $self->{'FILE_CTIME'} = unpack("N", $data);
346 }
347 elsif($atom_type == HTLS_DATA_FILE_MTIME)
348 {
349 0 $length -= _read($fh, \$data, $atom_len);
350  
351 0 $data =~ s/^....//;
352 0 _debug("File mtime:\n", _hexdump($data));
353  
354 0 $self->{'FILE_MTIME'} = unpack("N", $data);
355 }
356 elsif($atom_type == HTLS_DATA_PCHAT_REF)
357 {
358 0 $length -= _read($fh, \$data, $atom_len);
359  
360 0 _debug("Private chat ref: ", _hexdump($data));
361  
362 # Server 1.2.1 gives chat refs in 2 bytes. Annoying!
363 0 ▶ if($atom_len == 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($atom_len == 2) { } 0 0 0
364 {
365 0 $self->{'PCHAT_REF'} = unpack("n", $data);
366 }
367 else
368 {
369 0 $self->{'PCHAT_REF'} = unpack("N", $data);
370 }
371 }
372 elsif($atom_type == HTLS_DATA_IS_REPLY)
373 {
374 0 $length -= _read($fh, \$data, $atom_len);
375  
376 0 _debug("Is reply:\n", _hexdump($data));
377
378 0 $self->{'IS_REPLY'} = unpack("n", $data);
379 }
380 elsif($atom_type == HTLS_DATA_REPLY)
381 {
382 0 $length -= _read($fh, \$data, $atom_len);
383  
384 0 _debug("In reply to:\n", _hexdump($data));
385  
386 0 $data =~ s/@{[HTLC_NEWLINE]}/\n/osg;
387 0 $self->{'REPLY_TO'} = $data;
388 }
389 ▶ elsif($atom_type == HTLS_DATA_MSG ||
Condition0%0/24
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115 or $atom_type == 101 or $atom_type == 109 or $atom_type == 101
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115 or $atom_type == 101 or $atom_type == 109
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115 or $atom_type == 101
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101 or $atom_type == 101
l!l&&r!l&&!r
000
$atom_type == 101 or $atom_type == 101
l!l&&r!l&&!r
000
MC/DC0%0/9
$atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 101 or $atom_type == 115 or $atom_type == 101 or $atom_type == 109 or $atom_type == 101
$atom_type == 101 $atom_type == 101 $atom_type == 101 $atom_type == 101 $atom_type == 101 $atom_type == 115 $atom_type == 101 $atom_type == 109 $atom_type == 101
Truth table0%0/10
A or B or C or D or E or F or G or H or I
A= $atom_type == 101B= $atom_type == 101C= $atom_type == 101D= $atom_type == 101E= $atom_type == 101F= $atom_type == 115G= $atom_type == 101H= $atom_type == 109I= $atom_type == 101
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
390 $atom_type == HTLS_DATA_NEWS ||
391 $atom_type == HTLS_DATA_AGREEMENT ||
392 $atom_type == HTLS_DATA_USER_INFO ||
393 $atom_type == HTLS_DATA_CHAT ||
394 $atom_type == HTLC_DATA_PCHAT_SUBJECT ||
395 $atom_type == HTLS_DATA_MSG ||
396 $atom_type == HTLS_DATA_SERVER_MSG ||
397 $atom_type == HTLS_DATA_NEWS_POST)
398 {
399 0 $length -= _read($fh, \$data, $atom_len);
400  
401 0 _debug("Data:\n", _hexdump($data));
402  
403 0 $data =~ s/@{[HTLC_NEWLINE]}/\n/osg;
404 0 $self->{'DATA'} = $data;
405 }
406 elsif($atom_type == HTLS_DATA_HTXF_SIZE)
407 {
408 0 $length -= _read($fh, \$data, $atom_len);
409  
410 0 _debug("HTXF size:\n", _hexdump($data));
411  
412 0 ▶ if($atom_len == 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($atom_len == 2) { } 0 0 0
413 {
414 0 $self->{'HTXF_SIZE'} = unpack("n", $data);
415 }
416 else
417 {
418 0 $self->{'HTXF_SIZE'} = unpack("N", $data);
419 }
420 }
421 elsif($atom_type == HTLS_DATA_HTXF_REF)
422 {
423 0 $length -= _read($fh, \$data, $atom_len);
424  
425 0 _debug("HTXF ref:\n", _hexdump($data));
426  
427 0 $self->{'HTXF_REF'} = unpack("N", $data);
428 }
429 elsif($atom_type == HTLC_DATA_RFLT)
430 {
431 0 $length -= _read($fh, \$data, $atom_len);
432  
433 0 _debug("HTXF RFLT:\n", _hexdump($data));
434  
435 0 $self->{'HTXF_RFLT'} = $data;
436 }
437 else
438 {
439 0 $length -= _read($fh, \$data, $atom_len);
440  
441 0 _debug("Default data:\n", _hexdump($data));
442 0 $self->{'DATA'} = $data;
443 }
444 }
445  
446 0 ▶ if($length > 0) # Should not be reached...
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
447 {
448 0 _debug("Left-over length!\n");
449  
450 0 ▶ while($length > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
451 {
452 0 $length -= _read($fh, \$data, $length);
453 0 _debug("Left over data:\n", _hexdump($data));
454 }
455 }
456  
457 0 return(1);
458 }
459  
460 1 1;
« Hotline/Client.pm ↑ Summary Hotline/Shared.pm »