Annotation of embedaddon/dhcp/tests/DHCPv6/020-advertise-mcast.pl, revision 1.1.1.1

1.1       misho       1: #! /usr/bin/perl -w
                      2: 
                      3: # Copyright (c) 2007,2009 by Internet Systems Consortium, Inc. ("ISC")
                      4: #
                      5: # Permission to use, copy, modify, and distribute this software for any
                      6: # purpose with or without fee is hereby granted, provided that the above
                      7: # copyright notice and this permission notice appear in all copies.
                      8: #
                      9: # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
                     10: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11: # MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
                     12: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
                     15: # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16: #
                     17: #   Internet Systems Consortium, Inc.
                     18: #   950 Charter Street
                     19: #   Redwood City, CA 94063
                     20: #   <info@isc.org>
                     21: #   https://www.isc.org/
                     22: 
                     23: use strict;
                     24: use English;
                     25: use Time::HiRes qw( sleep );
                     26: use Socket;
                     27: use Socket6;
                     28: use IO::Select;
                     29: 
                     30: use dhcp_client;
                     31: 
                     32: # well-known addresses
                     33: my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
                     34: my $All_DHCP_Servers = "ff05::1:3";
                     35: 
                     36: # ports
                     37: my $client_port = 546;
                     38: my $server_port = 547;
                     39: 
                     40: # create a new Solicit message
                     41: my $msg = dhcp_client::msg->new($MSG_ADVERTISE);
                     42: 
                     43: # add the Client Identifier (required by DOCSIS and RFC 3315)
                     44: $msg->add_option($OPT_CLIENTID, dhcp_client::duid());
                     45: 
                     46: # add Elapsed Time, set to 0 on first packet (required by RFC 3315)
                     47: $msg->add_option($OPT_ELAPSED_TIME, "\x00\x00");
                     48: 
                     49: # add IA_NA for each interface (required by DOCSIS and RFC 3315)
                     50: # XXX: should this be a single interface only?
                     51: my $iaid = 0;
                     52: foreach my $iface (dhcp_client::iface()) {
                     53:        my $option_data = pack("NNN", ++$iaid, 0, 0);
                     54:        $msg->add_option($OPT_IA_NA, $option_data);
                     55: }
                     56: 
                     57: # add Options Request (required by DOCSIS, recommended by RFC 3315)
                     58: my @oro = ( );
                     59: $msg->add_option($OPT_ORO, pack("n*", @oro));
                     60: 
                     61: # timeout parameters
                     62: my $IRT = $SOL_TIMEOUT;
                     63: my $MRT = $SOL_MAX_RT;
                     64: my $MRC = 1;   # DOCSIS says 4, RFC 3315 says it SHOULD be 0
                     65: my $MRD = 0;
                     66: 
                     67: # sleep a random amount of time between 0 and 1 second, required by RFC 3315
                     68: # XXX: this seems pretty stupid
                     69: sleep(rand($SOL_MAX_DELAY));
                     70: 
                     71: my $RT;
                     72: my $count = 0;
                     73: my $mrd_end_time;
                     74: if ($MRD != 0) {
                     75:        $mrd_end_time = time() + $MRD;
                     76: }
                     77: my $reply_msg;
                     78: do {
                     79:        # create our socket, and send our bogus packet
                     80:        socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
                     81:        my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
                     82:        my $packet = $msg->packet();
                     83:        my $send_ret = send(SOCK, $packet, 0, 
                     84:                            pack_sockaddr_in6($server_port, $addr));
                     85:        if (not defined($send_ret)) {
                     86:                printf STDERR 
                     87:                        "Error \%d sending DHCPv6 message;\n\%s\n",
                     88:                        0+$ERRNO, $ERRNO;
                     89:                exit(1);
                     90:        } elsif ($send_ret != length($packet)) {
                     91:                print STDERR "Unable to send entire DHCPv6 message.\n";
                     92:                exit(1);
                     93:        }
                     94:        $count++;
                     95: 
                     96:        my $RAND = rand(0.2) - 0.1;
                     97:        if (defined $RT) {
                     98:                $RT = 2*$RT + $RAND*$RT;
                     99:                if (($RT > $MRT) && ($MRT != 0)) {
                    100:                        $RT = $MRT + $RAND*$RT;
                    101:                }
                    102:        } else {
                    103:                $RT = $IRT + $RAND*$IRT;
                    104:        }
                    105: 
                    106:        my $rt_end_time = time() + $RT;
                    107:        if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) { 
                    108:                $rt_end_time = $mrd_end_time;
                    109:        }
                    110: 
                    111:        for (;;) {
                    112:                my $timeout = $rt_end_time - time();
                    113:                if ($timeout < 0) {
                    114:                        last;
                    115:                }
                    116: 
                    117:                my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
                    118: 
                    119:                if (@ready) {
                    120:                        my $reply;
                    121:                        my $recv_ret;
                    122:        
                    123:                        $recv_ret = recv(SOCK, $reply, 1500, 0);
                    124:                        if (not defined $recv_ret) {
                    125:                                printf STDERR 
                    126:                                        "Error \%d receiving DHCPv6 " . 
                    127:                                                "message;\n\%s\n",
                    128:                                        0+$ERRNO, $ERRNO;
                    129:                                exit(1);
                    130:                        }
                    131: 
                    132:                        $reply_msg = dhcp_client::msg::decode($reply);
                    133:                        if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
                    134:                            ($reply_msg->{msg_type} == $MSG_REPLY)) {
                    135:                                last;
                    136:                        }
                    137:                }
                    138:        }
                    139: 
                    140: } until ($reply_msg || 
                    141:         (($MRC != 0) && ($count > $MRC)) ||
                    142:         (defined($mrd_end_time) && ($mrd_end_time > time())));
                    143: 
                    144: unless ($reply_msg) {
                    145:        if (($MRC != 0) && ($count >= $MRC)) {
                    146:                print STDERR 
                    147:                        "No reply after maximum retransmission count.\n";
                    148:        } else {
                    149:                print STDERR 
                    150:                        "No reply after maximum retransmission duration.\n";
                    151:        }
                    152: }
                    153: 
                    154: if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
                    155:        print "Got DHCPv6 Reply message.\n";
                    156:        exit(0);
                    157: }
                    158: 
                    159: #$Data::Dumper::Useqq = 1;
                    160: #print Dumper($msg), "\n";
                    161: #print Dumper($msg->packet()), "\n";
                    162: #
                    163: #print "packet length: ", length($msg->packet()), "\n";
                    164: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>