diff options
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 8 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message.go | 25 | ||||
-rw-r--r-- | dhcpv6/modifiers.go | 32 | ||||
-rw-r--r-- | dhcpv6/option_nontemporaryaddress.go | 2 | ||||
-rw-r--r-- | netboot/netconf.go | 10 |
5 files changed, 48 insertions, 29 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index dad07f6..d975308 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -253,11 +253,9 @@ func TestNewMessageTypeSolicit(t *testing.T) { // Check IA_NA iaid := [4]byte{hwAddr[2], hwAddr[3], hwAddr[4], hwAddr[5]} - iaNaOption := s.GetOneOption(OptionIANA) - require.NotNil(t, iaNaOption) - iaNa, ok := iaNaOption.(*OptIANA) - require.True(t, ok) - require.Equal(t, iaid, iaNa.IaId) + iana := s.Options.OneIANA() + require.NotNil(t, iana) + require.Equal(t, iaid, iana.IaId) } func TestIsUsingUEFIArchTypeTrue(t *testing.T) { diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index bc87f9b..ba592d6 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -47,6 +47,25 @@ func (mo MessageOptions) ServerID() *Duid { return &opt.(*optServerID).Duid } +// IANA returns all Identity Association for Non-temporary Address options. +func (mo MessageOptions) IANA() []*OptIANA { + opts := mo.Get(OptionIANA) + var ianas []*OptIANA + for _, o := range opts { + ianas = append(ianas, o.(*OptIANA)) + } + return ianas +} + +// OneIANA returns the first IANA option. +func (mo MessageOptions) OneIANA() *OptIANA { + ianas := mo.IANA() + if len(ianas) == 0 { + return nil + } + return ianas[0] +} + // Message represents a DHCPv6 Message as defined by RFC 3315 Section 6. type Message struct { MessageType MessageType @@ -168,11 +187,11 @@ func NewRequestFromAdvertise(adv *Message, modifiers ...Modifier) (*Message, err // add Elapsed Time req.AddOption(&OptElapsedTime{}) // add IA_NA - iaNa := adv.GetOneOption(OptionIANA) - if iaNa == nil { + iana := adv.Options.OneIANA() + if iana == nil { return nil, fmt.Errorf("IA_NA cannot be nil in ADVERTISE when building REQUEST") } - req.AddOption(iaNa) + req.AddOption(iana) // add OptRequestedOption oro := OptRequestedOption{} oro.SetRequestedOptions([]OptionCode{ diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go index be4c367..af5e6a8 100644 --- a/dhcpv6/modifiers.go +++ b/dhcpv6/modifiers.go @@ -72,30 +72,32 @@ func WithArchType(at iana.Arch) Modifier { // options func WithIANA(addrs ...OptIAAddress) Modifier { return func(d DHCPv6) { - opt := d.GetOneOption(OptionIANA) - if opt == nil { - opt = &OptIANA{} - } - iaNa := opt.(*OptIANA) - for _, addr := range addrs { - iaNa.AddOption(&addr) + if msg, ok := d.(*Message); ok { + iana := msg.Options.OneIANA() + if iana == nil { + iana = &OptIANA{} + } + for _, addr := range addrs { + iana.AddOption(&addr) + } + msg.UpdateOption(iana) } - d.UpdateOption(iaNa) } } // WithIAID updates an OptIANA option with the provided IAID func WithIAID(iaid [4]byte) Modifier { return func(d DHCPv6) { - opt := d.GetOneOption(OptionIANA) - if opt == nil { - opt = &OptIANA{ - Options: Options{}, + if msg, ok := d.(*Message); ok { + iana := msg.Options.OneIANA() + if iana == nil { + iana = &OptIANA{ + Options: Options{}, + } } + copy(iana.IaId[:], iaid[:]) + d.UpdateOption(iana) } - iaNa := opt.(*OptIANA) - copy(iaNa.IaId[:], iaid[:]) - d.UpdateOption(iaNa) } } diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 6b3d8d6..3ea46fe 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -54,7 +54,7 @@ func (op *OptIANA) ToBytes() []byte { } func (op *OptIANA) String() string { - return fmt.Sprintf("OptIANA{IAID=%v, t1=%v, t2=%v, options=%v}", + return fmt.Sprintf("IANA: {IAID=%v, t1=%v, t2=%v, options=%v}", op.IaId, op.T1, op.T2, op.Options) } diff --git a/netboot/netconf.go b/netboot/netconf.go index 96bfff2..ea82e05 100644 --- a/netboot/netconf.go +++ b/netboot/netconf.go @@ -35,15 +35,15 @@ type NetConf struct { // GetNetConfFromPacketv6 extracts network configuration information from a DHCPv6 // Reply packet and returns a populated NetConf structure func GetNetConfFromPacketv6(d *dhcpv6.Message) (*NetConf, error) { - opt := d.GetOneOption(dhcpv6.OptionIANA) - if opt == nil { - return nil, errors.New("No option IA NA found") + iana := d.Options.OneIANA() + if iana == nil { + return nil, errors.New("no option IA NA found") } netconf := NetConf{} + // get IP configuration - oiana := opt.(*dhcpv6.OptIANA) iaaddrs := make([]*dhcpv6.OptIAAddress, 0) - for _, o := range oiana.Options { + for _, o := range iana.Options { if o.Code() == dhcpv6.OptionIAAddr { iaaddrs = append(iaaddrs, o.(*dhcpv6.OptIAAddress)) } |