summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv4')
-rw-r--r--dhcpv4/ztp/ztp.go80
-rw-r--r--dhcpv4/ztp/ztp_test.go75
2 files changed, 155 insertions, 0 deletions
diff --git a/dhcpv4/ztp/ztp.go b/dhcpv4/ztp/ztp.go
new file mode 100644
index 0000000..b899425
--- /dev/null
+++ b/dhcpv4/ztp/ztp.go
@@ -0,0 +1,80 @@
+package ztpv4
+
+import (
+ "errors"
+ "strings"
+
+ "github.com/insomniacslk/dhcp/dhcpv4"
+)
+
+// VendorData is optional data a particular vendor may or may not include
+// in the Vendor Class options.
+type VendorData struct {
+ VendorName string
+ Model string
+ Serial string
+}
+
+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 {
+ return nil, nil
+ }
+ vc := opt.(*dhcpv4.OptClassIdentifier).Identifier
+ vd := &VendorData{}
+
+ switch {
+ // Arista;DCS-7050S-64;01.23;JPE12221671
+ case strings.HasPrefix(vc, "Arista;"):
+ p := strings.Split(vc, ";")
+ if len(p) < 4 {
+ return nil, errVendorOptionMalformed
+ }
+
+ vd.VendorName = p[0]
+ vd.Model = p[1]
+ vd.Serial = p[3]
+ return vd, nil
+
+ // ZPESystems:NSC:002251623
+ case strings.HasPrefix(vc, "ZPESystems:"):
+ p := strings.Split(vc, ":")
+ if len(p) < 3 {
+ return nil, errVendorOptionMalformed
+ }
+
+ vd.VendorName = p[0]
+ vd.Model = p[1]
+ vd.Serial = p[2]
+ return vd, nil
+
+ // Juniper option 60 parsing is a bit more nuanced. The following are all
+ // "valid" identifying stings for Juniper:
+ // Juniper-ptx1000-DD576 <vendor>-<model>-<serial
+ // Juniper-qfx10008 <vendor>-<model> (serial in hostname option)
+ // Juniper-qfx10002-361-DN817 <vendor>-<model>-<serial> (model has a dash in it!)
+ case strings.HasPrefix(vc, "Juniper-"):
+ 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 {
+ return nil, errors.New("host name option is missing")
+ }
+ } else {
+ vd.Model = strings.Join(p[1:len(p)-1], "-")
+ vd.Serial = p[len(p)-1]
+ }
+
+ vd.VendorName = p[0]
+ return vd, nil
+ }
+
+ // We didn't match anything.
+ return nil, nil
+}
diff --git a/dhcpv4/ztp/ztp_test.go b/dhcpv4/ztp/ztp_test.go
new file mode 100644
index 0000000..15a9d26
--- /dev/null
+++ b/dhcpv4/ztp/ztp_test.go
@@ -0,0 +1,75 @@
+package ztpv4
+
+import (
+ "testing"
+
+ "github.com/insomniacslk/dhcp/dhcpv4"
+ "github.com/stretchr/testify/require"
+)
+
+func TestParseV4VendorClass(t *testing.T) {
+ tt := []struct {
+ name string
+ vc, hostname string
+ want *VendorData
+ fail bool
+ }{
+ {name: "empty"},
+ {name: "unknownVendor", vc: "VendorX;BFR10K;XX12345"},
+ {name: "truncatedVendor", vc: "Arista;1234", fail: true},
+ {
+ name: "arista",
+ vc: "Arista;DCS-7050S-64;01.23;JPE12345678",
+ want: &VendorData{VendorName: "Arista", Model: "DCS-7050S-64", Serial: "JPE12345678"},
+ },
+ {
+ name: "juniper",
+ vc: "Juniper-ptx1000-DD123",
+ want: &VendorData{VendorName: "Juniper", Model: "ptx1000", Serial: "DD123"},
+ },
+ {
+ name: "juniperModelDash",
+ vc: "Juniper-qfx10002-36q-DN817",
+ want: &VendorData{VendorName: "Juniper", Model: "qfx10002-36q", Serial: "DN817"},
+ },
+ {
+ name: "juniperHostnameSerial",
+ vc: "Juniper-qfx10008",
+ hostname: "DE123",
+ want: &VendorData{VendorName: "Juniper", Model: "qfx10008", Serial: "DE123"},
+ },
+ {name: "juniperNoSerial", vc: "Juniper-qfx10008", fail: true},
+ {
+ name: "zpe",
+ vc: "ZPESystems:NSC:001234567",
+ want: &VendorData{VendorName: "ZPESystems", Model: "NSC", Serial: "001234567"},
+ },
+ }
+
+ for _, tc := range tt {
+ t.Run(tc.name, func(t *testing.T) {
+ packet, err := dhcpv4.New()
+ if err != nil {
+ t.Fatalf("failed to creat dhcpv4 packet object: %v", err)
+ }
+
+ packet.AddOption(&dhcpv4.OptClassIdentifier{
+ Identifier: tc.vc,
+ })
+
+ if tc.hostname != "" {
+ packet.AddOption(&dhcpv4.OptHostName{
+ HostName: tc.hostname,
+ })
+ }
+
+ vd, err := ParseVendorData(packet)
+ if tc.fail {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ require.Equal(t, tc.want, vd)
+ }
+ })
+ }
+}