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 /dhcpv4/ztpv4 | |
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 'dhcpv4/ztpv4')
-rw-r--r-- | dhcpv4/ztpv4/ztp.go | 10 | ||||
-rw-r--r-- | dhcpv4/ztpv4/ztp_test.go | 9 |
2 files changed, 6 insertions, 13 deletions
diff --git a/dhcpv4/ztpv4/ztp.go b/dhcpv4/ztpv4/ztp.go index 18075e9..4401e9d 100644 --- a/dhcpv4/ztpv4/ztp.go +++ b/dhcpv4/ztpv4/ztp.go @@ -18,11 +18,10 @@ var errVendorOptionMalformed = errors.New("malformed vendor option") // ParseVendorData will try to parse dhcp4 options looking for more // specific vendor data (like model, serial number, etc). func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { - opt := packet.GetOneOption(dhcpv4.OptionClassIdentifier) - if opt == nil { + vc := dhcpv4.GetClassIdentifier(packet.Options) + if len(vc) == 0 { return nil, errors.New("vendor options not found") } - vc := opt.(*dhcpv4.OptClassIdentifier).Identifier vd := &VendorData{} switch { @@ -59,9 +58,8 @@ func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { p := strings.Split(vc, "-") if len(p) < 3 { vd.Model = p[1] - if opt := packet.GetOneOption(dhcpv4.OptionHostName); opt != nil { - vd.Serial = opt.(*dhcpv4.OptHostName).HostName - } else { + vd.Serial = dhcpv4.GetHostName(packet.Options) + if len(vd.Serial) == 0 { return nil, errors.New("host name option is missing") } } else { diff --git a/dhcpv4/ztpv4/ztp_test.go b/dhcpv4/ztpv4/ztp_test.go index 680f15a..6e050d9 100644 --- a/dhcpv4/ztpv4/ztp_test.go +++ b/dhcpv4/ztpv4/ztp_test.go @@ -54,15 +54,10 @@ func TestParseV4VendorClass(t *testing.T) { } if tc.vc != "" { - packet.UpdateOption(&dhcpv4.OptClassIdentifier{ - Identifier: tc.vc, - }) + packet.UpdateOption(dhcpv4.OptClassIdentifier(tc.vc)) } - if tc.hostname != "" { - packet.UpdateOption(&dhcpv4.OptHostName{ - HostName: tc.hostname, - }) + packet.UpdateOption(dhcpv4.OptHostName(tc.hostname)) } vd, err := ParseVendorData(packet) |