| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package NetPacket::ARP; | 
| 2 |  |  |  |  |  |  | BEGIN { | 
| 3 | 1 |  |  | 1 |  | 841 | $NetPacket::ARP::AUTHORITY = 'cpan:YANICK'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Assemble and disassemble ARP (Address Resolution Protocol) packets. | 
| 6 |  |  |  |  |  |  | $NetPacket::ARP::VERSION = '1.5.0'; | 
| 7 | 1 |  |  | 1 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 64 |  | 
| 8 | 1 |  |  | 1 |  | 6 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 156 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | BEGIN { | 
| 12 | 1 |  |  | 1 |  | 24 | @ISA = qw(Exporter NetPacket); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # Items to export into callers namespace by default | 
| 15 |  |  |  |  |  |  | # (move infrequently used names to @EXPORT_OK below) | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 1 |  |  |  |  | 3 | @EXPORT = qw( | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # Other items we are prepared to export if requested | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 1 |  |  |  |  | 3 | @EXPORT_OK = qw(arp_strip | 
| 23 |  |  |  |  |  |  | ARP_OPCODE_REQUEST ARP_OPCODE_REPLY RARP_OPCODE_REQUEST | 
| 24 |  |  |  |  |  |  | RARP_OPCODE_REPLY | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # Tags: | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 |  |  |  |  | 84 | %EXPORT_TAGS = ( | 
| 30 |  |  |  |  |  |  | ALL         => [@EXPORT, @EXPORT_OK], | 
| 31 |  |  |  |  |  |  | opcodes     => [qw(ARP_OPCODE_REQUEST ARP_OPCODE_REPLY RARP_OPCODE_REQUEST | 
| 32 |  |  |  |  |  |  | RARP_OPCODE_REPLY)], | 
| 33 |  |  |  |  |  |  | strip       => [qw(arp_strip)], | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # | 
| 39 |  |  |  |  |  |  | # List of opcode values | 
| 40 |  |  |  |  |  |  | # | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  | 1 |  | 6 | use constant ARP_OPCODE_REQUEST  => 1; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 74 |  | 
| 43 | 1 |  |  | 1 |  | 5 | use constant ARP_OPCODE_REPLY    => 2; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 44 | 1 |  |  | 1 |  | 5 | use constant RARP_OPCODE_REQUEST => 3; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 45 | 1 |  |  | 1 |  | 4 | use constant RARP_OPCODE_REPLY   => 4; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 259 |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # | 
| 48 |  |  |  |  |  |  | # Decode the packet | 
| 49 |  |  |  |  |  |  | # | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub decode { | 
| 52 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 53 | 0 |  |  |  |  |  | my($pkt, $parent) = @_; | 
| 54 | 0 |  |  |  |  |  | my $self = {}; | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # Class fields | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | $self->{_parent} = $parent; | 
| 59 | 0 |  |  |  |  |  | $self->{_frame} = $pkt; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # Decode ARP packet | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 | 0 |  |  |  |  | if (defined($pkt)) { | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | ($self->{htype}, $self->{proto}, $self->{hlen}, $self->{plen}, | 
| 66 |  |  |  |  |  |  | $self->{opcode}, $self->{sha}, $self->{spa}, $self->{tha}, | 
| 67 |  |  |  |  |  |  | $self->{tpa}) = | 
| 68 |  |  |  |  |  |  | unpack('nnCCnH12H8H12H8' , $pkt); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | $self->{data} = undef; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | # Return a blessed object | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | bless($self, $class); | 
| 76 | 0 |  |  |  |  |  | return $self; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # | 
| 81 |  |  |  |  |  |  | # Strip header from packet and return the data contained in it.  ARP | 
| 82 |  |  |  |  |  |  | # packets contain no encapsulated data. | 
| 83 |  |  |  |  |  |  | # | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | undef &arp_strip; | 
| 86 |  |  |  |  |  |  | *arp_strip = \&strip; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub strip { | 
| 89 | 0 |  |  | 0 | 1 |  | return undef; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # | 
| 93 |  |  |  |  |  |  | # Encode a packet | 
| 94 |  |  |  |  |  |  | # | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub encode { | 
| 97 | 0 |  |  | 0 | 1 |  | die("Not implemented"); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | # Module return value | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | 1; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =pod | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =head1 NAME | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | NetPacket::ARP - Assemble and disassemble ARP (Address Resolution Protocol) packets. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head1 VERSION | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | version 1.5.0 | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | use NetPacket::ARP; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | $tcp_obj = NetPacket::ARP->decode($raw_pkt); | 
| 119 |  |  |  |  |  |  | $tcp_pkt = NetPacket::ARP->encode(params...);   # Not implemented | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | C provides a set of routines for assembling and | 
| 124 |  |  |  |  |  |  | disassembling packets using ARP (Address Resolution Protocol). | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head2 Methods | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =over | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =item Cdecode([RAW PACKET])> | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Decode the raw packet data given and return an object containing | 
| 133 |  |  |  |  |  |  | instance data.  This method will quite happily decode garbage input. | 
| 134 |  |  |  |  |  |  | It is the responsibility of the programmer to ensure valid packet data | 
| 135 |  |  |  |  |  |  | is passed to this method. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =item Cencode(param =E value)> | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Return a ARP packet encoded with the instance data specified.  Not | 
| 140 |  |  |  |  |  |  | implemented. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =back | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head2 Functions | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =over | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =item C | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | Return the encapsulated data (or payload) contained in the TCP packet. | 
| 151 |  |  |  |  |  |  | Since no payload data is encapulated in an ARP packet (only instance | 
| 152 |  |  |  |  |  |  | data), this function returns undef. | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =back | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head2 Instance data | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | The instance data for the C object consists of | 
| 159 |  |  |  |  |  |  | the following fields. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =over | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =item htype | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | Hardware type. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =item proto | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Protocol type. | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | =item hlen | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | Header length. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =item plen | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | Protocol length. | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =item opcode | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | One of the following constants: | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =over | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =item * ARP_OPCODE_REQUEST | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =item * ARP_OPCODE_REPLY | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =item * RARP_OPCODE_REQUEST | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =item * RARP_OPCODE_REPLY | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =back | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =item sha | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Source hardware address. | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =item spa | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | Source protocol address. | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | =item tha | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | Target hardware address. | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | =item tpa | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | Target protocol address. | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =back | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =head2 Exports | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | =over | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =item default | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | none | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | =item exportable | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | none | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | =item tags | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | The following tags group together related exportable items. | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =over | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | =item C<:ALL> | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | All the above exportable items. | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =back | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =back | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | =head1 EXAMPLE | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | Print out arp requests on the local network. | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | #!/usr/bin/perl -w | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | use Net::PcapUtils; | 
| 246 |  |  |  |  |  |  | use NetPacket::Ethernet qw(:types); | 
| 247 |  |  |  |  |  |  | use NetPacket::ARP; | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | sub process_pkt { | 
| 250 |  |  |  |  |  |  | my ($arg, $hdr, $pkt) = @_; | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | my $eth_obj = NetPacket::Ethernet->decode($pkt); | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | if ($eth_obj->{type} == ETH_TYPE_ARP) { | 
| 255 |  |  |  |  |  |  | my $arp_obj = NetPacket::ARP->decode($eth_obj->{data}, $eth_obj); | 
| 256 |  |  |  |  |  |  | print("source hw addr=$arp_obj->{sha}, " . | 
| 257 |  |  |  |  |  |  | "dest hw addr=$arp_obj->{tha}\n"); | 
| 258 |  |  |  |  |  |  | } | 
| 259 |  |  |  |  |  |  | } | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | Net::PcapUtils::loop(\&process_pkt); | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | =head1 TODO | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | =over | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | =item Implement encode() function | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | =item Does this work for protocols other than IP?  Need to read RFC. | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | =item Example is a bit silly | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =back | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | Copyright (c) 2001 Tim Potter. | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of | 
| 280 |  |  |  |  |  |  | the participants in the CRC for Advanced Computational Systems | 
| 281 |  |  |  |  |  |  | ('ACSys'). | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | This module is free software.  You can redistribute it and/or | 
| 284 |  |  |  |  |  |  | modify it under the terms of the Artistic License 2.0. | 
| 285 |  |  |  |  |  |  |  | 
| 286 |  |  |  |  |  |  | This program is distributed in the hope that it will be useful, | 
| 287 |  |  |  |  |  |  | but without any warranty; without even the implied warranty of | 
| 288 |  |  |  |  |  |  | merchantability or fitness for a particular purpose. | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | =head1 AUTHOR | 
| 291 |  |  |  |  |  |  |  | 
| 292 |  |  |  |  |  |  | Tim Potter Etpot@samba.orgE | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  | =cut | 
| 295 |  |  |  |  |  |  |  | 
| 296 |  |  |  |  |  |  | __END__ |