summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/ztpv6/parse_vendor_options.go
diff options
context:
space:
mode:
authorChris Gorham <gorhamc90@gmail.com>2018-12-12 07:17:09 -0700
committerPablo Mazzini <pmazzini@gmail.com>2018-12-12 14:17:09 +0000
commit2b569280e441c779406c8b2aaa1283ca5ce0f15b (patch)
tree958baf282cbdbce007a2c566dfdec53cb2246c91 /dhcpv6/ztpv6/parse_vendor_options.go
parent94fe644699296ec03ad8eadfed31289b1098c5a0 (diff)
[dhcpv6/ztpv6] Adding Parsing Vendor Opts and Parsing Remote Id Opts lib for ztpv6 (#208)
Diffstat (limited to 'dhcpv6/ztpv6/parse_vendor_options.go')
-rw-r--r--dhcpv6/ztpv6/parse_vendor_options.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/dhcpv6/ztpv6/parse_vendor_options.go b/dhcpv6/ztpv6/parse_vendor_options.go
new file mode 100644
index 0000000..63a6a05
--- /dev/null
+++ b/dhcpv6/ztpv6/parse_vendor_options.go
@@ -0,0 +1,60 @@
+package ztpv6
+
+import (
+ "errors"
+ "strings"
+
+ "github.com/insomniacslk/dhcp/dhcpv6"
+)
+
+var (
+ errVendorOptionMalformed = errors.New("malformed vendor option")
+)
+
+// VendorData contains fields extracted from Option 17 data
+type VendorData struct {
+ VendorName, Model, Serial string
+}
+
+// ParseVendorData will try to parse dhcp6 Vendor Specific Information options data
+// looking for more specific vendor data (like model, serial number, etc).
+// If the options are missing we will just return nil
+func ParseVendorData(packet dhcpv6.DHCPv6) (*VendorData, error) {
+ opt := packet.GetOneOption(dhcpv6.OptionVendorOpts)
+ if opt == nil {
+ return nil, errors.New("vendor options not found")
+ }
+
+ vd := VendorData{}
+ vo := opt.(*dhcpv6.OptVendorOpts).VendorOpts
+
+ for _, opt := range vo {
+ optData := string(opt.(*dhcpv6.OptionGeneric).OptionData)
+ switch {
+ // Arista;DCS-0000;00.00;ZZZ00000000
+ case strings.HasPrefix(optData, "Arista;"):
+ p := strings.Split(optData, ";")
+ if len(p) < 4 {
+ return nil, errVendorOptionMalformed
+ }
+
+ vd.VendorName = p[0]
+ vd.Model = p[1]
+ vd.Serial = p[3]
+ return &vd, nil
+
+ // ZPESystems:NSC:000000000
+ case strings.HasPrefix(optData, "ZPESystems:"):
+ p := strings.Split(optData, ":")
+ if len(p) < 3 {
+ return nil, errVendorOptionMalformed
+ }
+
+ vd.VendorName = p[0]
+ vd.Model = p[1]
+ vd.Serial = p[2]
+ return &vd, nil
+ }
+ }
+ return nil, errors.New("failed to parse vendor option data")
+}