diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-29 14:48:10 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-24 08:05:49 +0000 |
commit | c90ab10024ada840e24bb028a3405961e8e4c26a (patch) | |
tree | 9b8af0c1b80ee6efc112921f9a14b92d6c73f8eb /netboot | |
parent | 2be5cae32d33f01ddecf6f167a9c0e5290e6d58f (diff) |
dhcpv4: nicer API for option parsing.
From:
r := d.GetOneOption(OptionRouter).(*OptRouter).Routers
d.UpdateOption(&OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}}})
To:
r := GetRouter(d.Options)
d.UpdateOption(OptRouter(net.IP{192, 168, 0, 1}, ...))
Diffstat (limited to 'netboot')
-rw-r--r-- | netboot/netconf.go | 33 | ||||
-rw-r--r-- | netboot/netconf_test.go | 6 |
2 files changed, 11 insertions, 28 deletions
diff --git a/netboot/netconf.go b/netboot/netconf.go index 9dfa858..8c56262 100644 --- a/netboot/netconf.go +++ b/netboot/netconf.go @@ -87,11 +87,10 @@ func GetNetConfFromPacketv4(d *dhcpv4.DHCPv4) (*NetConf, error) { // get the subnet mask from OptionSubnetMask. If the netmask is not defined // in the packet, an error is returned - netmaskOption := d.GetOneOption(dhcpv4.OptionSubnetMask) - if netmaskOption == nil { + netmask := dhcpv4.GetSubnetMask(d.Options) + if netmask == nil { return nil, errors.New("no netmask option in response packet") } - netmask := netmaskOption.(*dhcpv4.OptSubnetMask).SubnetMask ones, _ := netmask.Size() if ones == 0 { return nil, errors.New("netmask extracted from OptSubnetMask options is null") @@ -100,11 +99,7 @@ func GetNetConfFromPacketv4(d *dhcpv4.DHCPv4) (*NetConf, error) { // netconf struct requires a valid lifetime to be specified. ValidLifetime is a dhcpv6 // concept, the closest mapping in dhcpv4 world is "IP Address Lease Time". If the lease // time option is nil, we set it to 0 - leaseTimeOption := d.GetOneOption(dhcpv4.OptionIPAddressLeaseTime) - leaseTime := uint32(0) - if leaseTimeOption != nil { - leaseTime = leaseTimeOption.(*dhcpv4.OptIPAddressLeaseTime).LeaseTime - } + leaseTime := dhcpv4.GetIPAddressLeaseTime(d.Options, 0) netconf.Addresses = append(netconf.Addresses, AddrConf{ IPNet: net.IPNet{ @@ -112,24 +107,19 @@ func GetNetConfFromPacketv4(d *dhcpv4.DHCPv4) (*NetConf, error) { Mask: netmask, }, PreferredLifetime: 0, - ValidLifetime: int(leaseTime), + ValidLifetime: int(leaseTime / time.Second), }) // get DNS configuration - dnsServersOption := d.GetOneOption(dhcpv4.OptionDomainNameServer) - if dnsServersOption == nil { - return nil, errors.New("name servers option is empty") - } - dnsServers := dnsServersOption.(*dhcpv4.OptDomainNameServer).NameServers + dnsServers := dhcpv4.GetDNS(d.Options) if len(dnsServers) == 0 { return nil, errors.New("no dns servers options in response packet") } netconf.DNSServers = dnsServers // get domain search list - dnsDomainSearchListOption := d.GetOneOption(dhcpv4.OptionDNSDomainSearchList) - if dnsDomainSearchListOption != nil { - dnsSearchList := dnsDomainSearchListOption.(*dhcpv4.OptDomainSearch).DomainSearch + dnsSearchList := dhcpv4.GetDomainSearch(d.Options) + if dnsSearchList != nil { if len(dnsSearchList.Labels) == 0 { return nil, errors.New("dns search list is empty") } @@ -137,18 +127,11 @@ func GetNetConfFromPacketv4(d *dhcpv4.DHCPv4) (*NetConf, error) { } // get default gateway - routerOption := d.GetOneOption(dhcpv4.OptionRouter) - if routerOption == nil { - return nil, errors.New("no router option specified in response packet") - } - - routersList := routerOption.(*dhcpv4.OptRouter).Routers + routersList := dhcpv4.GetRouter(d.Options) if len(routersList) == 0 { return nil, errors.New("no routers specified in the corresponding option") } - netconf.Routers = routersList - return &netconf, nil } diff --git a/netboot/netconf_test.go b/netboot/netconf_test.go index 00b39b8..5e954fa 100644 --- a/netboot/netconf_test.go +++ b/netboot/netconf_test.go @@ -224,13 +224,13 @@ func TestGetNetConfFromPacketv4(t *testing.T) { require.Equal(t, 5200, netconf.Addresses[0].ValidLifetime) // check DNSes require.Equal(t, 2, len(netconf.DNSServers)) - require.Equal(t, net.ParseIP("10.10.0.1"), netconf.DNSServers[0]) - require.Equal(t, net.ParseIP("10.10.0.2"), netconf.DNSServers[1]) + require.Equal(t, net.ParseIP("10.10.0.1").To4(), netconf.DNSServers[0]) + require.Equal(t, net.ParseIP("10.10.0.2").To4(), netconf.DNSServers[1]) // check DNS search list require.Equal(t, 2, len(netconf.DNSSearchList)) require.Equal(t, "slackware.it", netconf.DNSSearchList[0]) require.Equal(t, "dhcp.slackware.it", netconf.DNSSearchList[1]) // check routers require.Equal(t, 1, len(netconf.Routers)) - require.Equal(t, net.ParseIP("10.0.0.254"), netconf.Routers[0]) + require.Equal(t, net.ParseIP("10.0.0.254").To4(), netconf.Routers[0]) } |