恥ずかしい勘違いから生まれた、DHCP6の不要かつ部分的な実装
Revision | bd4448817d5eef5328d76c5ec06fe60ac5319f99 (tree) |
---|---|
Zeit | 2021-08-11 17:41:17 |
Autor | dyknon <dyknon@user...> |
Commiter | dyknon |
refactor
@@ -1,4 +1,4 @@ | ||
1 | -package Net::DHCP6::Option::DUID; | |
1 | +package Net::DHCP6::AbstractOption::DUID; | |
2 | 2 | # base class for DUID options |
3 | 3 | # RFC8415 11. |
4 | 4 | # RFC8415 21.2. |
@@ -1,4 +1,4 @@ | ||
1 | -package Net::DHCP6::Option::FixedLength; | |
1 | +package Net::DHCP6::AbstractOption::FixedLength; | |
2 | 2 | # base class for fixed length options |
3 | 3 | |
4 | 4 | use strict; |
@@ -4,7 +4,11 @@ package Net::DHCP6::Message; | ||
4 | 4 | use strict; |
5 | 5 | use warnings; |
6 | 6 | |
7 | +use Net::DHCP6::Option; | |
8 | +use Net::DHCP6::OptionList; | |
9 | + | |
7 | 10 | our $VERSION = "0.0.1"; |
11 | +our @ISA = qw/Net::DHCP6::OptionList/; | |
8 | 12 | |
9 | 13 | # TODO: should be crypto-safe RNG? |
10 | 14 | our $RNG = sub{ int rand shift }; |
@@ -38,35 +42,9 @@ sub xid { | ||
38 | 42 | $self->{xid}; |
39 | 43 | } |
40 | 44 | |
41 | -sub option { | |
42 | - my $self = shift; | |
43 | - my $key = shift; | |
44 | - my @hit = $self->options($key); | |
45 | - if(@hit == 0){ | |
46 | - undef; | |
47 | - }elsif(@hit == 1){ | |
48 | - pop @hit; | |
49 | - }else{ | |
50 | - die "multiple options"; | |
51 | - } | |
52 | -} | |
53 | - | |
54 | -sub options { | |
45 | +sub all_options { | |
55 | 46 | my $self = shift; |
56 | - return @{$self->{options}} if(@_ == 0); | |
57 | - my %index; | |
58 | - my @opts = $self->options; | |
59 | - return () unless(@opts); | |
60 | - for my $key(@_){ | |
61 | - if($key =~ /^[0-9]+$/){ | |
62 | - %index = (%index, | |
63 | - map{$_, 1}grep{$opts[$_]->code == $key}(0 .. $#opts)); | |
64 | - }else{ | |
65 | - %index = (%index, | |
66 | - map{$_, 1}grep{$opts[$_]->isa($key)}(0 .. $#opts)); | |
67 | - } | |
68 | - } | |
69 | - @opts[keys %index]; | |
47 | + @{$self->{options}}; | |
70 | 48 | } |
71 | 49 | |
72 | 50 | sub serialize { |
@@ -3,11 +3,11 @@ package Net::DHCP6::Option::ClientId; | ||
3 | 3 | |
4 | 4 | use strict; |
5 | 5 | use warnings; |
6 | -use Net::DHCP6::Option::DUID; | |
6 | +use Net::DHCP6::AbstractOption::DUID; | |
7 | 7 | use Net::DHCP6::Parameters qw/DHCP6_OPT_CLIENTID/; |
8 | 8 | |
9 | 9 | our $VERSION = "0.0.1"; |
10 | -our @ISA = qw/Net::DHCP6::Option::DUID/; | |
10 | +our @ISA = qw/Net::DHCP6::AbstractOption::DUID/; | |
11 | 11 | |
12 | 12 | use constant code => DHCP6_OPT_CLIENTID; |
13 | 13 | use constant name => "ClientId"; |
@@ -3,11 +3,11 @@ package Net::DHCP6::Option::ElapsedTime; | ||
3 | 3 | |
4 | 4 | use strict; |
5 | 5 | use warnings; |
6 | -use Net::DHCP6::Option::FixedLength; | |
6 | +use Net::DHCP6::AbstractOption::FixedLength; | |
7 | 7 | use Net::DHCP6::Parameters qw/DHCP6_OPT_ELAPSED_TIME/; |
8 | 8 | |
9 | 9 | our $VERSION = "0.0.1"; |
10 | -our @ISA = qw/Net::DHCP6::Option::FixedLength/; | |
10 | +our @ISA = qw/Net::DHCP6::AbstractOption::FixedLength/; | |
11 | 11 | |
12 | 12 | use constant code => DHCP6_OPT_ELAPSED_TIME; |
13 | 13 | use constant name => "ElapsedTime"; |
@@ -3,11 +3,11 @@ package Net::DHCP6::Option::ServerId; | ||
3 | 3 | |
4 | 4 | use strict; |
5 | 5 | use warnings; |
6 | -use Net::DHCP6::Option::DUID; | |
6 | +use Net::DHCP6::AbstractOption::DUID; | |
7 | 7 | use Net::DHCP6::Parameters qw/DHCP6_OPT_SERVERID/; |
8 | 8 | |
9 | 9 | our $VERSION = "0.0.1"; |
10 | -our @ISA = qw/Net::DHCP6::Option::DUID/; | |
10 | +our @ISA = qw/Net::DHCP6::AbstractOption::DUID/; | |
11 | 11 | |
12 | 12 | use constant code => DHCP6_OPT_SERVERID; |
13 | 13 | use constant name => "ServerId"; |
@@ -0,0 +1,36 @@ | ||
1 | +package Net::DHCP6::OptionList; | |
2 | + | |
3 | +use strict; | |
4 | +use warnings; | |
5 | + | |
6 | +our $VERSION = "0.0.1"; | |
7 | + | |
8 | +sub option { | |
9 | + my $self = shift; | |
10 | + my $key = shift; | |
11 | + my @hit = $self->options($key); | |
12 | + if(@hit == 0){ | |
13 | + undef; | |
14 | + }elsif(@hit == 1){ | |
15 | + pop @hit; | |
16 | + }else{ | |
17 | + die "multiple options"; | |
18 | + } | |
19 | +} | |
20 | + | |
21 | +sub options { | |
22 | + my $self = shift; | |
23 | + return $self->all_options if(@_ == 0); | |
24 | + my %index; | |
25 | + my @opts = $self->all_options; | |
26 | + for my $key(@_){ | |
27 | + if($key =~ /^[0-9]+$/){ | |
28 | + %index = (%index, | |
29 | + map{$_, 1}grep{$opts[$_]->code == $key}(0 .. $#opts)); | |
30 | + }else{ | |
31 | + %index = (%index, | |
32 | + map{$_, 1}grep{$opts[$_]->isa($key)}(0 .. $#opts)); | |
33 | + } | |
34 | + } | |
35 | + @opts[keys %index]; | |
36 | +} |