blib/lib/Net/DHCPClient.pm untested

filter: Statementstmt 0.0% 0 / 259 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 384 CC 14 SCAR 34.7
CC
14
Cov
0%
doit
132.0
new
6.0
AUTOLOAD
6.0
CRAP 32.3
1 package Net::DHCPClient;
2 $Net::DHCPClient::VERSION = (qw($Revision: 1.0 $))[1];
3  
4 =head1 NAME
5  
6 Net::DHCPClient - A DHCP Client API
7  
8 =cut
9  
10 use strict;
11  
12 use Carp;
13 use Net::RawIP qw(:pcap);
14  
15 use Data::Dumper;
16 use FileHandle;
17  
18 use vars qw($AUTOLOAD);
19  
20 my %fields = (
21 interface => undef,
22 macaddr => undef,
23 src => undef,
24 dest => undef,
25 op => undef,
26 htype => undef,
27 hlen => undef,
28 hops => undef,
29 xid => undef,
30 secs => undef,
31 flags => undef,
32 ciaddr => undef,
33 yiaddr => undef,
34 siaddr => undef,
35 giaddr => undef,
36 sname => undef,
37 bootfile => undef,
38 debug => undef,
39 options => undef,
40 timeout => undef,
41 );
42  
43 sub new {
44 my $that = shift;
45 my $class = ref( $that ) || $that;
46 my $self = {
47 _permitted => \%fields,
48 %fields,
49 };
50  
51 bless $self, $class;
52  
53 if ( @_ ) {
54 my %conf = @_;
55 while ( my( $k, $v ) = each %conf ) {
56 $self->$k( $v );
57 }
58 }
59  
60 return $self;
61 }
62  
63 sub AUTOLOAD {
64 my $self = shift;
65 my $type = ref( $self ) || croak "$self is not an object";
66 my $name = $AUTOLOAD;
67  
68 if ( @_ ) {
69 return $self->{$name} = shift;
70 } else {
71 return $self->{$name};
72 }
73 }
74  
75 sub discover {
76 my $self = shift;
77 my %options = @_;
78
79 $self->ciaddr( 0 );
80 $self->yiaddr( 0 );
81 $self->siaddr( 0 );
82 $self->giaddr( 0 );
83  
84 $options{53} = '1';
85  
86 $self->doit( \%options );
87 }
88  
89 sub request {
90 my $self = shift;
91 my %options = @_;
92  
93 $self->ciaddr( $self->yiaddr );
94  
95 $options{53} = '3';
96  
97 $self->doit( \%options );
98 }
99  
100 sub decline {
101 my $self = shift;
102 my %options = @_;
103  
104 $options{53} = '4';
105  
106 $self->doit( \%options );
107 }
108  
109 sub release {
110 my $self = shift;
111 my %options = @_;
112  
113 $options{53} = '7';
114  
115 $self->doit( \%options );
116 }
117
118 sub inform {
119 my $self = shift;
120 my %options = @_;
121  
122 $options{53} = '8';
123  
124 $self->doit( \%options );
125 }
126  
127 sub doit {
128 my $self = shift;
129 my $o = shift;
130 my %options = %$o;
131  
132 my $macaddr = $self->macaddr;
133 my $interface = $self->interface;
134  
135 my $op = 1;
136 my $htype = $self->htype || 1;
137 my $hlen = $self->hlen || 6;
138 my $ciaddr = $self->client_ip() || '0.0.0.0';
139 my $yiaddr = $self->your_ip() || '0.0.0.0';
140 my $siaddr = $self->server_ip() || '0.0.0.0';
141 my $giaddr = $self->relay_ip() || '0.0.0.0';
142 my $sname = $self->sname || 0;
143 my $file = $self->bootfile || 0;
144  
145 my $timeout = $self->timeout || 10;
146  
147 my $dstmac;
148 my $dstip;
149  
150 if ( $self->src ) {
151 $dstmac = net2mac( $self->src );
152 } else {
153 $dstmac = 'ff:ff:ff:ff:ff:ff';
154 }
155  
156 if ( $self->siaddr ) {
157 $dstip = $self->server_ip();
158 } else {
159 $dstip = '255.255.255.255';
160 }
161  
162 my $srcip = $self->your_ip();
163  
164 my $xid = sprintf "%0.8d", int( rand 99999999 );
165  
166 my $data = $self->encode( op => $op, htype => $htype, hlen => $hlen,
167 hops => 0, xid => $xid, secs => 0, flags => 0,
168 ciaddr => $ciaddr, yiaddr => $yiaddr,
169 siaddr => $siaddr, giaddr => $giaddr,
170 chaddr => $macaddr, sname => $sname,
171 file => $file, options => \%options );
172  
173 my $p = new Net::RawIP( {udp => {}} );
174  
175 $p->ethnew( $interface );
176 $p->ethset( source => $macaddr, dest => $dstmac);
177  
178 $p->set( {ip => {saddr => $srcip, daddr => $dstip},
179 udp => {source => 68, dest => 67, data => $data}} );
180  
181 my $filter = "ether dst $macaddr and dst port 68";
182  
183 my $a = new Net::RawIP;
184 my @f;
185  
186 my $done = 0;
187  
188 STDERR->close;
189  
190 my $pcap = $a->pcapinit( $interface, $filter, 1500, 500 );
191  
192 STDERR->fdopen( 2, "w+" );
193  
194 $SIG{ALRM} = sub { die "timeout" };
195  
196 while ( ! $done ) {
197  
198 if ( $self->debug ) {
199  
200 my $op53 = $options{'53'};
201  
202 print "XMIT:\n";
203 printf
204 "xid %u:\tsaddr=%s daddr=%s\n\t\tsrc=%s dest=%s op=%d\n",
205 $xid, $srcip, $dstip, $macaddr, $dstmac, $op53;
206 }
207  
208 $p->ethsend;
209  
210 if ( $options{'53'} == 7 ) {
211 return 1;
212 }
213  
214 eval {
215  
216 while ( sprintf( "%x", $self->xid ) ne sprintf( "%u", $xid ) ) {
217  
218 alarm( $timeout );
219  
220 loop $pcap, 1, sub {
221 my $time = timem();
222 $p->bset( substr( $_[2], 14 ) );
223
224 $self->dest( substr( $_[2], 0, 6) );
225 $self->src( substr( $_[2], 6, 6) );
226
227 my @g = $p->get( {ip => [qw(ttl saddr daddr)],
228 udp => [qw(data)]} );
229  
230 $self->decode( $g[3] );
231  
232 $done = 1;
233  
234 if ( $self->debug ) {
235
236 my $x = $self->options();
237 my %options = %$x;
238  
239 my $op53 = $options{'53'};
240  
241 print "RCVD:\n";
242 printf
243 "xid %lx:\n %lu\tttl=%u saddr=%s daddr=%s\n\t\tsrc=%s dest=%s op=%d\n",
244 $self->xid, $xid, $g[0], ip2dot( $g[1] ), ip2dot( $g[2] ),
245 net2mac( $self->src ), net2mac( $self->dest ), $op53;
246 }
247  
248 if ( $self->debug > 5 ) {
249 printf "%u %x\n", $xid, $self->xid;
250 }
251
252 }, \@f;
253  
254 alarm( 0 );
255 }
256 };
257  
258 if ( $@ ) {
259 if ( $@ =~ /timeout/ ) {
260 if ( $self->debug ) {
261 print "TIMEOUT\n";
262 }
263 return 0;
264 } else {
265 alarm( 0 );
266 die;
267 }
268 }
269 }
270  
271 my $x = $self->options();
272  
273 my %options = %$x;
274  
275 if ( $self->debug > 5 ) {
276 print "Returning from doit()\n";
277 }
278  
279 return 1;
280 }
281  
282 sub decode {
283 my $self = shift;
284 my $data = shift;
285 my @fields = ('op', 'htype', 'hlen', 'hops', 'xid', 'secs', 'flags',
286 'ciaddr', 'yiaddr', 'siaddr', 'giaddr');
287 my %options;
288  
289 my $bootp = substr $data, 0, 239;
290 my $opts = substr $data, 240;
291 my @bootp = unpack "H2 H2 H2 H2 H8 H4 H4 H8 H8 H8 H8 H32 H128 H256", $bootp;
292  
293 for ( my $i; $i <= 10; $i++ ) { $bootp[$i] = hex( $bootp[$i] ); }
294  
295 for ( my $i; $i <= $#bootp; $i++ ) {
296 my $method = $fields[$i];
297 $self->$method( $bootp[$i] );
298 }
299  
300 my @opts = unpack "C" x ( length( $data ) - 240 ), $opts;
301  
302 for ( my $i = 0; $i <= $#opts; $i++ ) {
303
304 my $opt = $opts[$i++];
305 my $len = $opts[$i++];
306 my $offset = $len + $i - 1;
307 my $string = "";
308  
309 for ( my $q = $i; $q <= $offset; $q++ ) {
310 if ( $string ) {
311 $string = sprintf "%s %d", $string, $opts[$q];
312 } else {
313 $string = sprintf "%d", $opts[$q];
314 }
315 }
316  
317 $options{$opt} = $string;
318  
319 $i = $i + $len - 1;
320 }
321  
322 $self->options( \%options );
323 }
324  
325 sub encode {
326 my $self = shift;
327 my %c = @_;
328
329 my $magic = pack "C4", 99, 130, 83, 99;
330  
331 my @ciaddr = split /\./, $c{'ciaddr'};
332 my @yiaddr = split /\./, $c{'yiaddr'};
333 my @siaddr = split /\./, $c{'siaddr'};
334 my @giaddr = split /\./, $c{'giaddr'};
335
336 my @chaddr = mac2net( $c{'chaddr'} );
337  
338 my $data = pack "C4 H8 H4 H4 C4 C4 C4 C4 C16 H128 H256",
339 $c{'op'}, $c{'htype'}, $c{'hlen'}, $c{'hops'}, $c{'xid'}, $c{'secs'},
340 $c{'flags'}, @ciaddr, @yiaddr, @siaddr, @giaddr, @chaddr, $c{'sname'},
341 $c{'file'};
342  
343 $data = join '', $data, $magic;
344  
345 my $o = $c{'options'};
346 my %options = %$o;
347  
348 foreach my $key ( keys %options ) {
349 my @p = split / /, $options{$key};
350 map { $_ = hex( $_ ); } @p;
351  
352 my $format = sprintf "C%d", $#p+3;
353  
354 my $options = pack $format, $key, $#p+1, @p;
355  
356 $data = join '', $data, $options;
357 }
358  
359 my $end = pack "C2", 255, 0;
360  
361 $data = join '', $data, $end;
362  
363 return $data;
364 }
365  
366 sub net2mac {
367 return sprintf( "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", unpack( "C6", shift ) );
368 }
369  
370 sub mac2net {
371 my @a = split /:/, shift;
372  
373 for ( 1..10 ) {
374 push @a, '0';
375 }
376  
377 map { $_ = hex($_); } @a;
378
379 return @a;
380 }
381  
382 sub ip2dot {
383 return sprintf( "%u.%u.%u.%u", unpack( "C4", pack( "N", shift ) ) );
384 }
385  
386 sub dot2ip {
387 return unpack( "N", pack( "C4", split( /\./, shift ) ) );
388 }
389  
390 sub client_ip {
391 my $self = shift;
392  
393 return ip2dot( $self->ciaddr );
394 }
395  
396 sub reply {
397 my $self = shift;
398  
399 my @ops = ( '', 'discover', 'offer', 'request', 'decline', 'ack', 'nak',
400 'release', 'inform' );
401  
402 my $x = $self->options;
403 my %options = %$x;
404  
405 return ( $ops[$options{'53'}] );
406 }
407  
408 sub your_ip {
409 my $self = shift;
410  
411 return ip2dot( $self->yiaddr );
412 }
413  
414 sub server_ip {
415 my $self = shift;
416  
417 return ip2dot( $self->siaddr );
418 }
419  
420 sub relay_ip {
421 my $self = shift;
422  
423 return ip2dot( $self->giaddr );
424 }
425  
426 sub domain_name {
427 my $self = shift;
428  
429 my $o = $self->options;
430 my %options = %$o;
431  
432 my @l = split / /, $options{15};
433  
434 return( pack "C*", @l );
435 }
436  
437 sub server_lookup {
438 my $self = shift;
439 my $option = shift;
440  
441 my $o = $self->options;
442 my %options = %$o;
443  
444 my @val = split / /, $options{$option};
445  
446 my @servers;
447  
448 for ( my $i; $i <= $#val; $i++ ) {
449 push @servers, sprintf "%d.%d.%d.%d",
450 $val[$i++], $val[$i++], $val[$i++], $val[$i];
451 }
452  
453 return @servers;
454 }
455
456 sub domain_name_server {
457 my $self = shift;
458  
459 return $self->server_lookup(6);
460 }
461  
462 sub server_identifier {
463 my $self = shift;
464  
465 return $self->server_lookup(54);
466 }
467  
468 sub router {
469 my $self = shift;
470  
471 return $self->server_lookup(3);
472 }
473  
474 sub subnet_mask {
475 my $self = shift;
476
477 return $self->server_lookup(1);
478 }
479  
480 sub ntp_server {
481 my $self = shift;
482
483 return $self->server_lookup(42);
484 }
485  
486 sub time_server {
487 my $self = shift;
488  
489 return $self->server_lookup(4);
490 }
491  
492 sub netbios_nameserver {
493 my $self = shift;
494
495 return $self->server_lookup(44);
496 }
497  
498 sub broadcast {
499 my $self = shift;
500  
501 return $self->server_lookup(28);
502 }
503  
504 =head1 SYNOPSIS
505  
506 use Net::DHCPClient;
507  
508 my $dhcp = new Net::DHCPClient( maccaddr => '0a:0a:0a:0a:0a:0a',
509 interface => 'eth0' );
510  
511 $dhcp->discover( 61 => '0a 0a 0a 0a 0a 0a' );
512  
513 =cut
514  
515 =head1 DESCRIPTION
516  
517 This module provides methods for implementing a DHCP client. It allows perl
518 scripts to interacts with DHCP servers.
519  
520 This module is used by constructing a new DHCPClient object via the
521 constructor method, providing it (minimally) with the machine address and
522 the network interface that is to be used for the DHCP transaction with
523 the DHCP server.
524  
525 Information after each DHCP call is stored in the instance variables of
526 the object. Refer to RFC2131 for the designations of the named fields.
527 Options (sometimes called vendor extensions) are stored in an instance
528 variable in the object called options as a references to a hash. The options
529 themselves are keys by the decimal designation of the option, and the values
530 of the hash are string representations of the hex values that should be passed
531 to the DHCP server with the options. Please refer to RFC2132 for a complete
532 listing.
533  
534 Some common option fields are available through methods in this object for
535 convenience. They are listed in this document.
536  
537 This document is not a replacement for RFC2131 or RFC2132, and you will
538 probably need a copy of them handy if you are coding DHCP clients.
539  
540  
541 =cut
542  
543 =head2 Class Methods
544  
545 The constructor is the only class method that should be invoked. It takes
546 as its parameters minimally the machine address and the interface for which
547 the DHCP transaction should take place.
548  
549 =head2 Object Methods
550  
551 Object methods must be invoked against objects created via the B<new>
552 method.
553  
554 There are two kinds of object methods -- those that invoke DHCP transactions
555 and those that interpret the results of a DHCP transaction. I am grouping
556 them separately for this reason.
557  
558 B<DHCPClient> transaction object methods
559  
560 All of the B<DHCPClient> transaction methods take named parameters as their
561 arguments, where paramters are named for the decimal representation of the
562 options that can be set in the DHCP packet, and set to a string representation
563 of the hex values that should be set for the named parameter in the DHCP
564 packet. Whew. Fear not, examples are below.
565  
566 =over 4
567  
568 =item decline
569  
570 The decline method declines an offer from a DHCP server.
571  
572 =item discover
573  
574 The discover method broadcasts a DHCPDISCOVER on the local subnet.
575  
576 =item inform
577  
578 The inform method requests information from a DHCP server.
579  
580 =item release
581  
582 The release method releases the lease of the IP address.
583  
584 =item request
585  
586 The request method requests a DHCP lease.
587  
588 =back
589  
590 B<DHCPClient> result object methods
591  
592 These methods are built as conveniences to get the data that is contained
593 in the result of a DHCP transaction returned in a usable form. All of the
594 data can be had by going through the object instance variables by hand, but
595 I would not recommend it. These methods will not return anything useful
596 until a DHCP transaction has taken place.
597  
598 Note -- unless otherwise noted, an IP address returned by an object
599 method listed in this documentation should be considered to be a string
600 containing the address is dotted notation.
601  
602 =over 4
603  
604 =item broadcast
605  
606 Returns the broadcast address.
607  
608 =item client_ip
609  
610 Returns the IP address of the client that made the request to the DHCP server.
611  
612 =item domain_name
613  
614 Returns the domain name that the server has provided.
615  
616 =item domain_name_server
617  
618 Returns a list of IP addresses of the domain name servers.
619  
620 =item netbiod_nameserver
621  
622 Returns a list of the IP addresses of the netbiod domain name servers
623 (i.e. NT Domain servers)
624  
625 =item ntp_server
626  
627 Returns a list of IP address of the Network Time Protocol server.
628  
629 =item relay_ip
630  
631 Returns the IP address of the agent that relayed your DHCP request to the
632 DHCP server (probably a router).
633  
634 =item reply
635  
636 Returns a string indicating what the server reply was.
637  
638 =item server_identifier
639  
640 Returns a server identifier -- it is usually an IP address of the server,
641 but does not necessarily need to be. This method assumes an IP address.
642  
643 =item server_ip
644  
645 Returns the IP address of the DHCP server that handled your request.
646  
647 =item subnet_mask
648  
649 Returns the subnet mask.
650  
651 =item time_server
652  
653 Returns a list of the IP addresses of the time servers.
654  
655 =item your_ip
656  
657 Returns the IP address that the DHCP server has offered to your client.
658  
659 =back
660  
661 =head1 Change log
662  
663 Revision 1.0 2001/04/19
664 Initial release
665  
666 =head1 AUTHOR
667  
668 Joshua Walgenbach
669 Indiana Unversity
670 jwalgenb@indiana.edu
671  
672 =head1 COPYRIGHT
673  
674 Copyright 2000, Joshua Walgenbach
675 All rights reserved
676  
677 This program is free software; you can redistribute it and/or modify
678 it under the terms of either:
679  
680 =over 4
681  
682 =item
683  
684 a) the "Artistic License" which comes with this Kit, or
685  
686 b) the GNU General Public License as published by the Free Software
687 Foundation; either version 1, or (at your option) any later version.
688  
689 =back
690  
691 This program is distributed in the hope that it will be useful,
692 but WITHOUT ANY WARRANTY; without even the implied warranty of
693 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
694 the GNU General Public License or the Artistic License for more details.
695  
696 =cut
697  
698 1;
699