~ubuntu-branches/ubuntu/trusty/libnetpacket-perl/trusty

« back to all changes in this revision

Viewing changes to .pc/spelling.patch/lib/NetPacket/UDP.pm

  • Committer: Package Import Robot
  • Author(s): Florian Schlichting
  • Date: 2013-12-04 23:49:19 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20131204234919-5ebhy77qln1p1fis
Tags: 1.4.4-1
* Import Upstream version 1.4.4
* Drop spelling.patch, whatis.patch: both applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# NetPacket::UDP - Decode and encode UDP (User Datagram Protocol)
3
 
# packets. 
4
 
 
5
 
package NetPacket::UDP;
6
 
BEGIN {
7
 
  $NetPacket::UDP::AUTHORITY = 'cpan:YANICK';
8
 
}
9
 
{
10
 
  $NetPacket::UDP::VERSION = '1.4.2';
11
 
}
12
 
# ABSTRACT: Assemble and disassemble UDP (User Datagram Protocol) packets.
13
 
 
14
 
use strict;
15
 
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
16
 
use NetPacket;
17
 
use NetPacket::IP;
18
 
 
19
 
BEGIN {
20
 
    @ISA = qw(Exporter NetPacket);
21
 
 
22
 
# Items to export into callers namespace by default
23
 
# (move infrequently used names to @EXPORT_OK below)
24
 
 
25
 
    @EXPORT = qw(
26
 
    );
27
 
 
28
 
# Other items we are prepared to export if requested
29
 
 
30
 
    @EXPORT_OK = qw(udp_strip
31
 
    );
32
 
 
33
 
# Tags:
34
 
 
35
 
    %EXPORT_TAGS = (
36
 
    ALL         => [@EXPORT, @EXPORT_OK],
37
 
    strip       => [qw(udp_strip)],
38
 
);
39
 
 
40
 
}
41
 
 
42
 
#
43
 
# Decode the packet
44
 
#
45
 
 
46
 
sub decode {
47
 
    my $class = shift;
48
 
    my($pkt, $parent) = @_;
49
 
    my $self = {};
50
 
 
51
 
    # Class fields
52
 
 
53
 
    $self->{_parent} = $parent;
54
 
    $self->{_frame} = $pkt;
55
 
 
56
 
    # Decode UDP packet
57
 
 
58
 
    if (defined($pkt)) {
59
 
 
60
 
        ($self->{src_port}, $self->{dest_port}, $self->{len}, $self->{cksum},
61
 
         $self->{data}) = unpack("nnnna*", $pkt);
62
 
    }
63
 
 
64
 
    # Return a blessed object
65
 
 
66
 
    bless($self, $class);
67
 
    return $self;
68
 
}
69
 
 
70
 
#
71
 
# Strip header from packet and return the data contained in it
72
 
#
73
 
 
74
 
undef &udp_strip;
75
 
*udp_strip = \&strip;
76
 
 
77
 
sub strip {
78
 
    return decode(__PACKAGE__,shift)->{data};
79
 
}   
80
 
 
81
 
#
82
 
# Encode a packet
83
 
#
84
 
 
85
 
sub encode {
86
 
    my ($self, $ip) = @_;
87
 
 
88
 
    # Adjust the length accordingly
89
 
    $self->{len} = 8 + length($self->{data});
90
 
 
91
 
    # First of all, fix the checksum
92
 
    $self->checksum($ip);
93
 
 
94
 
    # Put the packet together
95
 
    return pack("nnnna*", $self->{src_port},$self->{dest_port},
96
 
                $self->{len}, $self->{cksum}, $self->{data});
97
 
 
98
 
}
99
 
 
100
 
101
 
# UDP Checksum
102
 
#
103
 
 
104
 
sub checksum {
105
 
 
106
 
    my( $self, $ip ) = @_;
107
 
 
108
 
    my $proto = NetPacket::IP::IP_PROTO_UDP;
109
 
 
110
 
    # Pack pseudo-header for udp checksum
111
 
 
112
 
    my $src_ip = gethostbyname($ip->{src_ip});
113
 
    my $dest_ip = gethostbyname($ip->{dest_ip});
114
 
 
115
 
    no warnings;
116
 
 
117
 
    my $packet = pack 'a4a4CCnnnnna*' =>
118
 
 
119
 
      # fake ip header part
120
 
      $src_ip, $dest_ip, 0, $proto, $self->{len},
121
 
 
122
 
      # proper UDP part
123
 
      $self->{src_port}, $self->{dest_port}, $self->{len}, 0, $self->{data};
124
 
 
125
 
    $packet .= "\x00" if length($packet) % 2;
126
 
 
127
 
    $self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet)); 
128
 
 
129
 
}
130
 
 
131
 
1;
132
 
 
133
 
__END__
134
 
 
135
 
=pod
136
 
 
137
 
=head1 NAME
138
 
 
139
 
NetPacket::UDP - Assemble and disassemble UDP (User Datagram Protocol) packets.
140
 
 
141
 
=head1 VERSION
142
 
 
143
 
version 1.4.2
144
 
 
145
 
=head1 SYNOPSIS
146
 
 
147
 
  use NetPacket::UDP;
148
 
 
149
 
  $udp_obj = NetPacket::UDP->decode($raw_pkt);
150
 
  $udp_pkt = $udp_obj->encode($l3_obj);
151
 
  $udp_data = NetPacket::UDP::strip($raw_pkt);
152
 
 
153
 
=head1 DESCRIPTION
154
 
 
155
 
C<NetPacket::UDP> provides a set of routines for assembling and
156
 
disassembling packets using UDP (User Datagram Protocol).  
157
 
 
158
 
=head2 Methods
159
 
 
160
 
=over
161
 
 
162
 
=item C<NetPacket::UDP-E<gt>decode([RAW PACKET])>
163
 
 
164
 
Decode the raw packet data given and return an object containing
165
 
instance data.  This method will quite happily decode garbage input.
166
 
It is the responsibility of the programmer to ensure valid packet data
167
 
is passed to this method.
168
 
 
169
 
=item C<$udp_packet-<gt>encode($l3_obj)>
170
 
 
171
 
Return the encoded version of the UDP packet object. Needs
172
 
part of the IP header contained (src_ip and dest_ip specifically) in $l3_obj, 
173
 
in order to calculate the UDP checksum. The length field will also be set 
174
 
automatically based on values provided.
175
 
 
176
 
=back
177
 
 
178
 
=head2 Functions
179
 
 
180
 
=over
181
 
 
182
 
=item C<NetPacket::UDP::strip([RAW PACKET])>
183
 
 
184
 
Return the encapsulated data (or payload) contained in the UDP
185
 
packet.  This data is suitable to be used as input for other
186
 
C<NetPacket::*> modules.
187
 
 
188
 
This function is equivalent to creating an object using the
189
 
C<decode()> constructor and returning the C<data> field of that
190
 
object.
191
 
 
192
 
=back
193
 
 
194
 
=head2 Instance data
195
 
 
196
 
The instance data for the C<NetPacket::UDP> object consists of
197
 
the following fields.
198
 
 
199
 
=over
200
 
 
201
 
=item src_port
202
 
 
203
 
The source UDP port for the datagram.
204
 
 
205
 
=item dest_port
206
 
 
207
 
The destination UDP port for the datagram.
208
 
 
209
 
=item len
210
 
 
211
 
The length (including length of header) in bytes for this packet.
212
 
 
213
 
=item cksum
214
 
 
215
 
The checksum value for this packet.
216
 
 
217
 
=item data
218
 
 
219
 
The encapsulated data (payload) for this packet.
220
 
 
221
 
=back
222
 
 
223
 
=head2 IP data
224
 
 
225
 
The IP data for the $l3_obj object consists of the following fields.
226
 
Additional items may be supplied as well as passing the whole
227
 
object returned by NetPacket::IP->decode but are unnecessary.
228
 
 
229
 
=over
230
 
 
231
 
=item src_ip
232
 
 
233
 
The source IP for the datagram
234
 
 
235
 
=item dest_ip
236
 
 
237
 
The destination IP for the datagram
238
 
 
239
 
=back
240
 
 
241
 
=head2 Exports
242
 
 
243
 
=over
244
 
 
245
 
=item default
246
 
 
247
 
none
248
 
 
249
 
=item exportable
250
 
 
251
 
udp_strip
252
 
 
253
 
=item tags
254
 
 
255
 
The following tags group together related exportable items.
256
 
 
257
 
=over
258
 
 
259
 
=item C<:strip>
260
 
 
261
 
Import the strip function C<udp_strip>.
262
 
 
263
 
=item C<:ALL>
264
 
 
265
 
All the above exportable items.
266
 
 
267
 
=back
268
 
 
269
 
=back
270
 
 
271
 
=head1 EXAMPLE
272
 
 
273
 
The following example prints the source IP address and port, the
274
 
destination IP address and port, and the UDP packet length:
275
 
 
276
 
  #!/usr/bin/perl -w
277
 
 
278
 
  use strict;
279
 
  use Net::PcapUtils;
280
 
  use NetPacket::Ethernet qw(:strip);
281
 
  use NetPacket::IP;
282
 
  use NetPacket::UDP;
283
 
 
284
 
  sub process_pkt {
285
 
      my($arg, $hdr, $pkt) = @_;
286
 
 
287
 
      my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
288
 
      my $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
289
 
 
290
 
      print("$ip_obj->{src_ip}:$udp_obj->{src_port} -> ",
291
 
            "$ip_obj->{dest_ip}:$udp_obj->{dest_port} ",
292
 
            "$udp_obj->{len}\n");
293
 
  }
294
 
 
295
 
  Net::PcapUtils::loop(\&process_pkt, FILTER => 'udp');
296
 
 
297
 
The following is an example use in combination with Net::Divert 
298
 
to alter the payload of packets that pass through. All occurences
299
 
of foo will be replaced with bar. This example is easy to test with 
300
 
netcat, but otherwise makes little sense. :) Adapt to your needs:
301
 
 
302
 
    use Net::Divert;
303
 
    use NetPacket::IP qw(IP_PROTO_UDP);
304
 
    use NetPacket::UDP;
305
 
 
306
 
    $divobj = Net::Divert->new('yourhost',9999);
307
 
 
308
 
    $divobj->getPackets(\&alterPacket);
309
 
 
310
 
    sub alterPacket
311
 
    {
312
 
        my ($data, $fwtag) = @_;
313
 
 
314
 
        $ip_obj = NetPacket::IP->decode($data);
315
 
 
316
 
        if($ip_obj->{proto} == IP_PROTO_UDP) {
317
 
 
318
 
            # decode the UDP header
319
 
            $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
320
 
 
321
 
            # replace foo in the payload with bar
322
 
            $udp_obj->{data} =~ s/foo/bar/g;
323
 
 
324
 
            # reencode the packet
325
 
            $ip_obj->{data} = $udp_obj->encode($udp_obj, $ip_obj);
326
 
            $data = $ip_obj->encode;
327
 
 
328
 
        }
329
 
 
330
 
        $divobj->putPacket($data,$fwtag);
331
 
    }
332
 
 
333
 
=head1 COPYRIGHT
334
 
 
335
 
Copyright (c) 2001 Tim Potter.
336
 
 
337
 
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 
338
 
the participants in the CRC for Advanced Computational Systems
339
 
('ACSys').
340
 
 
341
 
This module is free software.  You can redistribute it and/or
342
 
modify it under the terms of the Artistic License 2.0.
343
 
 
344
 
This program is distributed in the hope that it will be useful,
345
 
but without any warranty; without even the implied warranty of
346
 
merchantability or fitness for a particular purpose.
347
 
 
348
 
=head1 AUTHOR
349
 
 
350
 
Tim Potter E<lt>tpot@samba.orgE<gt>
351
 
 
352
 
Stephanie Wehner E<lt>atrak@itsx.comE<gt>
353
 
 
354
 
Yanick Champoux <yanick@cpan.org>
355
 
 
356
 
=cut