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