Annotation of embedaddon/dhcp/tests/DHCPv6/011-solicit-serverid.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: # XXX: for debugging
                     33: use Data::Dumper;
                     34: 
                     35: # not-yet-standard options
                     36: my $OPT_TIME_SERVERS = 40;
                     37: my $OPT_TIME_OFFSET = 41;
                     38: 
                     39: # DOCSIS sub-options
                     40: my $DOCSIS_OPT_ORO = 1;
                     41: # 2 to 31 are reserved
                     42: my $DOCSIS_OPT_TFTP_SERVERS = 32;
                     43: my $DOCSIS_OPT_CONFIG_FILE_NAME = 33;
                     44: my $DOCSIS_OPT_SYSLOG_SERVERS = 34;
                     45: my $DOCSIS_OPT_TLV5 = 35;
                     46: my $DOCSIS_OPT_DEVICE_ID = 36;
                     47: my $DOCSIS_OPT_CCC = 37;
                     48: my $DOCSIS_OPT_VERS = 38;
                     49: 
                     50: # well-known addresses
                     51: my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
                     52: my $All_DHCP_Servers = "ff05::1:3";
                     53: 
                     54: # ports
                     55: my $client_port = 546;
                     56: my $server_port = 547;
                     57: 
                     58: # create a new Solicit message
                     59: my $msg = dhcp_client::msg->new($MSG_SOLICIT);
                     60: 
                     61: # add the Client Identifier (required by DOCSIS and RFC 3315)
                     62: $msg->add_option($OPT_CLIENTID, dhcp_client::duid());
                     63: 
                     64: # add the Server Identifier (NOT ALLOWED by DOCSIS and RFC 3315)
                     65: $msg->add_option($OPT_SERVERID, dhcp_client::duid());
                     66: 
                     67: # add Elapsed Time, set to 0 on first packet (required by RFC 3315)
                     68: $msg->add_option($OPT_ELAPSED_TIME, "\x00\x00");
                     69: 
                     70: # add IA_NA for each interface (required by DOCSIS and RFC 3315)
                     71: # XXX: should this be a single interface only?
                     72: my $iaid = 0;
                     73: foreach my $iface (dhcp_client::iface()) {
                     74:        my $option_data = pack("NNN", ++$iaid, 0, 0);
                     75:        $msg->add_option($OPT_IA_NA, $option_data);
                     76: }
                     77: 
                     78: # add Reconfigure Accept (required by DOCSIS)
                     79: $msg->add_option($OPT_RECONF_ACCEPT, "");
                     80: 
                     81: # add Options Request (required by DOCSIS, recommended by RFC 3315)
                     82: my @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET );
                     83: $msg->add_option($OPT_ORO, pack("n*", @oro));
                     84: 
                     85: 
                     86: # add Vendor Class option (required by DOCSIS)
                     87: $msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
                     88: 
                     89: # add Vendor-specific Information Option option (required by DOCSIS)
                     90: my $vsio = pack("N", 4491);
                     91: 
                     92: # ORO (required by DOCSIS)
                     93: my @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS );
                     94: $vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro);
                     95: 
                     96: # TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS)
                     97: my $tlv5_data = "\x01\x02\x03\x0";  
                     98: $vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data;
                     99: 
                    100: # DOCSIS Device (required by DOCSIS)
                    101: my $docsis_device_id = dhcp_client::mac_addr_binary();
                    102: $vsio .= pack("nn", $DOCSIS_OPT_DEVICE_ID, length($docsis_device_id));
                    103: $vsio .= $docsis_device_id;
                    104: 
                    105: $msg->add_option($OPT_VENDOR_OPTS, $vsio);
                    106: 
                    107: # add Rapid Commit option (required by DOCSIS)
                    108: $msg->add_option($OPT_RAPID_COMMIT, "");
                    109: 
                    110: # timeout parameters, from DOCSIS
                    111: my $IRT = $SOL_TIMEOUT;
                    112: my $MRT = $SOL_MAX_RT;
                    113: my $MRC = 1;   # DOCSIS says 4, RFC 3315 says it SHOULD be 0
                    114: my $MRD = 0;
                    115: 
                    116: # sleep a random amount of time between 0 and 1 second, required by RFC 3315
                    117: # XXX: this seems pretty stupid
                    118: sleep(rand($SOL_MAX_DELAY));
                    119: 
                    120: my $RT;
                    121: my $count = 0;
                    122: my $mrd_end_time;
                    123: if ($MRD != 0) {
                    124:        $mrd_end_time = time() + $MRD;
                    125: }
                    126: my $reply_msg;
                    127: do {
                    128:        # create our socket, and send our Solicit
                    129:        socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
                    130:        my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
                    131:        my $packet = $msg->packet();
                    132:        my $send_ret = send(SOCK, $packet, 0, 
                    133:                            pack_sockaddr_in6($server_port, $addr));
                    134:        if (not defined($send_ret)) {
                    135:                printf STDERR 
                    136:                        "Error \%d sending DHCPv6 Solicit message;\n\%s\n",
                    137:                        0+$ERRNO, $ERRNO;
                    138:                exit(1);
                    139:        } elsif ($send_ret != length($packet)) {
                    140:                print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
                    141:                exit(1);
                    142:        }
                    143:        $count++;
                    144: 
                    145:        my $RAND = rand(0.2) - 0.1;
                    146:        if (defined $RT) {
                    147:                $RT = 2*$RT + $RAND*$RT;
                    148:                if (($RT > $MRT) && ($MRT != 0)) {
                    149:                        $RT = $MRT + $RAND*$RT;
                    150:                }
                    151:        } else {
                    152:                $RT = $IRT + $RAND*$IRT;
                    153:        }
                    154: 
                    155:        my $rt_end_time = time() + $RT;
                    156:        if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) { 
                    157:                $rt_end_time = $mrd_end_time;
                    158:        }
                    159: 
                    160:        for (;;) {
                    161:                my $timeout = $rt_end_time - time();
                    162:                if ($timeout < 0) {
                    163: #                      print STDERR "Timeout waiting for DHCPv6 Advertise ",
                    164: #                              "or Reply message.\n";
                    165:                        last;
                    166:                }
                    167: 
                    168:                my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
                    169: 
                    170:                if (@ready) {
                    171:                        my $reply;
                    172:                        my $recv_ret;
                    173:        
                    174:                        $recv_ret = recv(SOCK, $reply, 1500, 0);
                    175:                        if (not defined $recv_ret) {
                    176:                                printf STDERR 
                    177:                                        "Error \%d receiving DHCPv6 " . 
                    178:                                                "message;\n\%s\n",
                    179:                                        0+$ERRNO, $ERRNO;
                    180:                                exit(1);
                    181:                        }
                    182: 
                    183:                        $reply_msg = dhcp_client::msg::decode($reply);
                    184:                        if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
                    185:                            ($reply_msg->{msg_type} == $MSG_REPLY)) {
                    186:                                last;
                    187:                        }
                    188:                }
                    189:        }
                    190: 
                    191: } until ($reply_msg || 
                    192:         (($MRC != 0) && ($count > $MRC)) ||
                    193:         (defined($mrd_end_time) && ($mrd_end_time > time())));
                    194: 
                    195: unless ($reply_msg) {
                    196:        if (($MRC != 0) && ($count >= $MRC)) {
                    197:                print STDERR 
                    198:                        "No reply after maximum retransmission count.\n";
                    199:        } else {
                    200:                print STDERR 
                    201:                        "No reply after maximum retransmission duration.\n";
                    202:        }
                    203: }
                    204: 
                    205: if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
                    206:        print "Got DHCPv6 Reply message.\n";
                    207:        exit(0);
                    208: }
                    209: 
                    210: #$Data::Dumper::Useqq = 1;
                    211: #print Dumper($msg), "\n";
                    212: #print Dumper($msg->packet()), "\n";
                    213: #
                    214: #print "packet length: ", length($msg->packet()), "\n";
                    215: 

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