summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/ztp/ztp.go
diff options
context:
space:
mode:
authorinsomniac <insomniacslk@users.noreply.github.com>2018-11-09 15:34:24 +0000
committerGitHub <noreply@github.com>2018-11-09 15:34:24 +0000
commit01251f1da16d2734ca8e4fe503ba1c911ade2032 (patch)
tree5585d62b44f24ec3836c87339bd413b472d210f5 /dhcpv4/ztp/ztp.go
parent17761a875ffa249b422d660f3e3c797f0e011dce (diff)
parent50982cccaa34a2427acf9fba2072c394d37777cf (diff)
[ztpv4] add Opt60 (vc) parsing (#187)
Diffstat (limited to 'dhcpv4/ztp/ztp.go')
-rw-r--r--dhcpv4/ztp/ztp.go80
1 files changed, 80 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
+}