diff options
author | MikoĊaj Walczak <mikiwalczak+github@gmail.com> | 2018-07-11 13:32:51 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-07-11 13:32:51 +0100 |
commit | 34154e71da6f5b4527809dc0babdefcbd262281c (patch) | |
tree | 1c483f4c6422247e1d1b8099951ec943a4c0729f | |
parent | ab01f8d86f0edc6873008db8983ffa34363db3a5 (diff) |
NewSolicitWithCID for DHCPv6 (#76)
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 34 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message.go | 47 |
2 files changed, 58 insertions, 23 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index f8ec76d..3db4cee 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -1,8 +1,10 @@ package dhcpv6 import ( + "net" "testing" + "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" ) @@ -139,5 +141,37 @@ func TestNewReplyFromDHCPv6Message(t *testing.T) { require.Error(t, err) } +func TestNewSolicitWithCID(t *testing.T) { + hwAddr, err := net.ParseMAC("24:0A:9E:9F:EB:2B") + require.NoError(t, err) + + duid := Duid{ + Type: DUID_LL, + HwType: iana.HwTypeEthernet, + LinkLayerAddr: hwAddr, + } + + s, err := NewSolicitWithCID(duid) + require.NoError(t, err) + + require.Equal(t, s.Type(), SOLICIT) + // Check CID + cidOption := s.GetOneOption(OPTION_CLIENTID) + require.NotNil(t, cidOption) + cid, ok := cidOption.(*OptClientId) + require.True(t, ok) + require.Equal(t, cid.Cid, duid) + + // Check ORO + oroOption := s.GetOneOption(OPTION_ORO) + require.NotNil(t, oroOption) + oro, ok := oroOption.(*OptRequestedOption) + require.True(t, ok) + opts := oro.RequestedOptions() + require.Contains(t, opts, DNS_RECURSIVE_NAME_SERVER) + require.Contains(t, opts, DOMAIN_SEARCH_LIST) + require.Equal(t, len(opts), 2) +} + // TODO test NewSolicit // test String and Summary diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index ef87a43..0b84dae 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -65,49 +65,50 @@ func GetTime() uint32 { return uint32((now.Nanoseconds() / 1000000000) % 0xffffffff) } -// NewSolicitForInterface creates a new SOLICIT message with DUID-LLT, using the -// given network interface's hardware address and current time -func NewSolicitForInterface(ifname string, modifiers ...Modifier) (DHCPv6, error) { +// NewSolicitWithCID creates a new SOLICIT message with CID. +func NewSolicitWithCID(duid Duid, modifiers ...Modifier) (DHCPv6, error) { d, err := NewMessage() if err != nil { return nil, err } d.(*DHCPv6Message).SetMessage(SOLICIT) - iface, err := net.InterfaceByName(ifname) - if err != nil { - return nil, err - } - cid := OptClientId{ - Cid: Duid{ - Type: DUID_LLT, - HwType: iana.HwTypeEthernet, - Time: GetTime(), - LinkLayerAddr: iface.HardwareAddr, - }, - } - - d.AddOption(&cid) - oro := OptRequestedOption{} + d.AddOption(&OptClientId{Cid: duid}) + oro := new(OptRequestedOption) oro.SetRequestedOptions([]OptionCode{ DNS_RECURSIVE_NAME_SERVER, DOMAIN_SEARCH_LIST, }) - d.AddOption(&oro) + d.AddOption(oro) d.AddOption(&OptElapsedTime{}) // FIXME use real values for IA_NA - iaNa := OptIANA{} + iaNa := &OptIANA{} iaNa.IaId = [4]byte{0xfa, 0xce, 0xb0, 0x0c} iaNa.T1 = 0xe10 iaNa.T2 = 0x1518 - d.AddOption(&iaNa) - - // apply modifiers + d.AddOption(iaNa) + // Apply modifiers for _, mod := range modifiers { d = mod(d) } return d, nil } +// NewSolicitForInterface creates a new SOLICIT message with DUID-LLT, using the +// given network interface's hardware address and current time +func NewSolicitForInterface(ifname string, modifiers ...Modifier) (DHCPv6, error) { + iface, err := net.InterfaceByName(ifname) + if err != nil { + return nil, err + } + duid := Duid{ + Type: DUID_LLT, + HwType: iana.HwTypeEthernet, + Time: GetTime(), + LinkLayerAddr: iface.HardwareAddr, + } + return NewSolicitWithCID(duid, modifiers...) +} + // NewAdvertiseFromSolicit creates a new ADVERTISE packet based on an SOLICIT packet. func NewAdvertiseFromSolicit(solicit DHCPv6, modifiers ...Modifier) (DHCPv6, error) { if solicit == nil { |