恥ずかしい勘違いから生まれた、DHCP6の不要かつ部分的な実装
リビジョン | 88d6ccca92ba93399efae9d65e5f631af818a92a (tree) |
---|---|
日時 | 2021-08-12 19:43:39 |
作者 | dyknon <dyknon@user...> |
コミッター | dyknon |
Rename + changes on InfoReq
MessageExchange -> Exchange
Stateless -> Exchange::InfoReq
@@ -20,9 +20,16 @@ sub new { | ||
20 | 20 | die "invalid data length" |
21 | 21 | unless(length $opts{data} >= 1+2 && length $opts{data} <= 128+2); |
22 | 22 | $self = \$opts{data}; |
23 | + }elsif(@_ == 1 && ref $_[0] eq "ARRAY" && !eval{$_[0]->isa("ARRAY")}){ | |
24 | + my $self2 = shift; | |
25 | + return $class->new(@$self2); | |
23 | 26 | }elsif(@_ == 1){ |
24 | - $self = shift; | |
25 | - $self = \$self if(!length ref $self); | |
27 | + my $self2 = shift; | |
28 | + if(ref $self2 == ""){ | |
29 | + $self = \$self2; | |
30 | + }else{ | |
31 | + $self = $self2; | |
32 | + } | |
26 | 33 | }elsif(lc($_[0]) eq "llt" && @_ == 4){ |
27 | 34 | shift; #llt |
28 | 35 | $self = \(pack("S>S>L>", 1, shift, shift) . shift); |
@@ -1,4 +1,4 @@ | ||
1 | -package Net::DHCP6::MessageExchange; | |
1 | +package Net::DHCP6::Exchange; | |
2 | 2 | # RFC8415 15. |
3 | 3 | |
4 | 4 | use strict; |
@@ -1,23 +1,25 @@ | ||
1 | -package Net::DHCP6::Stateless; | |
1 | +package Net::DHCP6::Exchange::InfoReq; | |
2 | 2 | # RFC8415 6.1. |
3 | 3 | # RFC8415 18.2.6. |
4 | 4 | |
5 | 5 | use strict; |
6 | 6 | use warnings; |
7 | 7 | |
8 | -use Net::DHCP6::MessageExchange qw/NEXT LAST/; | |
8 | +use Net::DHCP6::Exchange qw/NEXT LAST/; | |
9 | 9 | |
10 | 10 | use Net::DHCP6::Parameters qw( |
11 | 11 | DHCP6_MT_REPLY |
12 | 12 | DHCP6_MT_INFORMATION_REQUEST |
13 | + DHCP6_OPT_CLIENTID | |
14 | + DHCP6_OPT_ORO | |
13 | 15 | DHCP6_OPT_INF_MAX_RT |
14 | 16 | DHCP6_OPT_INFORMATION_REFRESH_TIME |
15 | 17 | ); |
16 | 18 | use Net::DHCP6::Message; |
17 | -use Net::DHCP6::Option qw/ElapsedTime OptionRequest/; | |
19 | +use Net::DHCP6::Option qw/ElapsedTime OptionRequest ClientId/; | |
18 | 20 | |
19 | 21 | our $VERSION = "0.0.1"; |
20 | -our @ISA = qw/Net::DHCP6::MessageExchange/; | |
22 | +our @ISA = qw/Net::DHCP6::Exchange/; | |
21 | 23 | |
22 | 24 | use constant irt => 1; |
23 | 25 | use constant mrc => 0; |
@@ -35,8 +37,9 @@ sub add_elements_if_not_exists { | ||
35 | 37 | sub new { |
36 | 38 | my $class = shift; |
37 | 39 | my %opts = @_; |
38 | - die "requesting options are not given" if(ref $opts{request} ne "ARRAY"); | |
39 | 40 | |
41 | + $opts{request} //= []; | |
42 | + $opts{options} //= []; | |
40 | 43 | $opts{mrt} //= 3600; |
41 | 44 | if($opts{auto_request} // 1){ |
42 | 45 | add_elements_if_not_exists($opts{request}, |
@@ -45,10 +48,28 @@ sub new { | ||
45 | 48 | ); |
46 | 49 | } |
47 | 50 | delete $opts{auto_request}; |
51 | + | |
52 | + my @ooro = grep{$_->code == DHCP6_OPT_ORO}@{$opts{options}}; | |
53 | + die "there are multiple option request options" if(@ooro >= 2); | |
54 | + @{$opts{options}} = grep{$_->code != DHCP6_OPT_ORO}@{$opts{options}}; | |
55 | + push @{$opts{request}}, $ooro[0]->all_elems if(@ooro); | |
56 | + push @{$opts{options}}, d6opt_new_option_request(list => @{$opts{request}}); | |
57 | + delete $opts{request}; | |
58 | + | |
59 | + my $ocid = grep{$_->code == DHCP6_OPT_CLIENTID}@{$opts{options}}; | |
60 | + die "2 client ids given" if($ocid && defined $opts{client_id}); | |
61 | + if(!$ocid && !exists $opts{client_id}){ | |
62 | + die "no client id given (To send no client ids, specify undef)" | |
63 | + } | |
64 | + if(defined $opts{client_id}){ | |
65 | + push @{$opts{options}}, d6opt_new_client_id($opts{client_id}); | |
66 | + } | |
67 | + delete $opts{client_id}; | |
68 | + | |
48 | 69 | $opts{failed} //= sub{}; |
49 | 70 | $opts{recved} //= sub{ 1; }; |
50 | 71 | |
51 | - Net::DHCP6::MessageExchange::new($class, %opts); | |
72 | + Net::DHCP6::Exchange::new($class, %opts); | |
52 | 73 | } |
53 | 74 | |
54 | 75 | sub mrt { |
@@ -70,12 +91,7 @@ sub recved { | ||
70 | 91 | |
71 | 92 | sub options { |
72 | 93 | my $self = shift; |
73 | - $self->{options} ? @{$self->{options}} : (); | |
74 | -} | |
75 | - | |
76 | -sub requests { | |
77 | - my $self = shift; | |
78 | - @{$self->{request}} | |
94 | + @{$self->{options}}; | |
79 | 95 | } |
80 | 96 | |
81 | 97 | sub message { |
@@ -83,7 +99,6 @@ sub message { | ||
83 | 99 | Net::DHCP6::Message->new(DHCP6_MT_INFORMATION_REQUEST, |
84 | 100 | $self->xid, |
85 | 101 | $self->options, |
86 | - d6opt_new_option_request($self->requests), | |
87 | 102 | d6opt_new_elapsed_time($self->elapsed // 0), |
88 | 103 | ); |
89 | 104 | } |
@@ -11,7 +11,7 @@ use Net::DHCP6::Parameters qw( | ||
11 | 11 | DHCP6_OPT_DNS_SERVERS |
12 | 12 | ); |
13 | 13 | use Net::DHCP6::Option qw/:all/; |
14 | -use Net::DHCP6::Stateless; | |
14 | +use Net::DHCP6::Exchange::InfoReq; | |
15 | 15 | use Socket qw/getaddrinfo AF_INET6 SOCK_DGRAM AI_PASSIVE/; |
16 | 16 | |
17 | 17 | use constant IF => "eth1"; |
@@ -43,11 +43,9 @@ my $bind_address_packed = $gai[1]->{addr}; | ||
43 | 43 | |
44 | 44 | my $ll_packed = pack "C*", map{hex $_}split(/[:.-]/, $ll_str); |
45 | 45 | |
46 | -my $client = Net::DHCP6::Stateless->new( | |
46 | +my $client = Net::DHCP6::Exchange::InfoReq->new( | |
47 | 47 | local => $bind_address_packed, |
48 | - options => [ | |
49 | - d6opt_new_client_id(LL => 1, $ll_packed) | |
50 | - ], | |
48 | + client_id => [ LL => 1, $ll_packed ], | |
51 | 49 | request => [ |
52 | 50 | DHCP6_OPT_DNS_SERVERS, |
53 | 51 | ], |