File Coverage

blib/lib/NetPacket.pm
Criterion Covered Total %
statement 23 30 76.6
branch 1 2 50.0
condition n/a
subroutine 5 8 62.5
pod 0 5 0.0
total 29 45 64.4


line stmt bran cond sub pod time code
1             package NetPacket;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: assemble/disassemble network packets at the protocol level
4             $NetPacket::VERSION = '1.7.2';
5 14     14   5827 use strict;
  14         28  
  14         415  
6 14     14   69 use warnings;
  14         24  
  14         372  
7              
8 14     14   66 use parent 'Exporter';
  14         24  
  14         89  
9              
10             our @EXPORT_OK = qw(in_cksum htons htonl ntohs ntohl);
11              
12             our %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
13              
14             #
15             # Utility functions useful for all modules
16             #
17              
18             # Calculate IP checksum
19              
20             sub in_cksum {
21              
22 12     12 0 34 my ($packet) = @_;
23 12         29 my ($plen, $short, $num, $count, $chk);
24              
25 12         23 $plen = length($packet);
26 12         48 $num = int($plen / 2);
27 12         25 $chk = 0;
28 12         21 $count = $plen;
29              
30 12         85 foreach $short (unpack("S$num", $packet)) {
31 447         522 $chk += $short;
32 447         536 $count = $count - 2;
33             }
34              
35 12 50       64 if($count == 1) {
36 0         0 $chk += unpack("C", substr($packet, $plen -1, 1));
37             }
38              
39             # add the two halves together (CKSUM_CARRY -> libnet)
40 12         37 $chk = ($chk >> 16) + ($chk & 0xffff);
41 12         57 return(~(($chk >> 16) + $chk) & 0xffff);
42             }
43              
44             # Network/host byte order conversion routines. Network byte order is
45             # defined as being big-endian.
46              
47             sub htons
48             {
49 12     12 0 31 my ($in) = @_;
50              
51 12         80 return(unpack('n*', pack('S*', $in)));
52             }
53              
54             sub htonl
55             {
56 0     0 0   my ($in) = @_;
57              
58 0           return(unpack('N*', pack('L*', $in)));
59             }
60              
61             sub ntohl
62             {
63 0     0 0   my ($in) = @_;
64              
65 0           return(unpack('L*', pack('N*', $in)));
66             }
67              
68             sub ntohs
69             {
70 0     0 0   my ($in) = @_;
71              
72 0           return(unpack('S*', pack('n*', $in)));
73             }
74              
75             #
76             # Module initialisation
77             #
78              
79             1;
80              
81             __END__