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