summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Karlage <skarlage@fb.com>2018-03-03 11:20:51 -0800
committerSean Karlage <skarlage@fb.com>2018-03-03 11:26:42 -0800
commita22cefc86285b6ba1d9879486fda11054d070b78 (patch)
tree5c77fa1b8e6ddc33b26adb32d2668229304b1502
parent28e9aa6c820ffcb9028422b1179c1326dc76229c (diff)
Add better formatting for vendor opts
-rw-r--r--dhcpv4/dhcpv4.go12
-rw-r--r--dhcpv4/options.go10
-rw-r--r--dhcpv4/options_test.go18
-rw-r--r--dhcpv4/types.go16
4 files changed, 56 insertions, 0 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go
index dc691d9..f0690b2 100644
--- a/dhcpv4/dhcpv4.go
+++ b/dhcpv4/dhcpv4.go
@@ -568,6 +568,18 @@ func (d *DHCPv4) Summary() string {
)
ret += " options=\n"
for _, opt := range d.options {
+ // Parse and display sub-options
+ if opt.Code == OptionVendorSpecificInformation {
+ ret += fmt.Sprintf(" %v ->\n", OptionCodeToString[OptionVendorSpecificInformation])
+ subopts, err := OptionsFromBytes(opt.Data)
+ if err == nil {
+ for _, subopt := range subopts {
+ ret += fmt.Sprintf(" %v\n", subopt.BSDPString())
+ }
+ continue
+ }
+ // fall-through to normal display if the above fails
+ }
ret += fmt.Sprintf(" %v\n", opt.String())
if opt.Code == OptionEnd {
break
diff --git a/dhcpv4/options.go b/dhcpv4/options.go
index aa9808b..989a93b 100644
--- a/dhcpv4/options.go
+++ b/dhcpv4/options.go
@@ -110,6 +110,16 @@ func (o *Option) String() string {
return fmt.Sprintf("%v -> %v", code, o.Data)
}
+// BSDPString converts a BSDP-specific option embedded in
+// vendor-specific information to a human-readable string.
+func (o *Option) BSDPString() string {
+ code, ok := BSDPOptionCodeToString[o.Code]
+ if !ok {
+ code = "Unknown"
+ }
+ return fmt.Sprintf("%v -> %v", code, o.Data)
+}
+
func (o *Option) ToBytes() []byte {
// Convert a single option to its wire-format representation
ret := []byte{byte(o.Code)}
diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go
index 5a34ed5..47258aa 100644
--- a/dhcpv4/options_test.go
+++ b/dhcpv4/options_test.go
@@ -146,3 +146,21 @@ func TestOptionsToStringDHCPMessageType(t *testing.T) {
t.Fatalf("Invalid string representation: %v", stropt)
}
}
+
+func TestBSDPOptionToString(t *testing.T) {
+ // Parse message type
+ option := Option{
+ Code: BSDPOptionMessageType,
+ Data: []byte{BSDPMessageTypeList},
+ }
+ stropt := option.BSDPString()
+ AssertEqual(t, stropt, "BSDP Message Type -> [1]", "BSDP string representation")
+
+ // Parse failure
+ option = Option{
+ Code: OptionCode(12), // invalid BSDP Opcode
+ Data: []byte{1, 2, 3},
+ }
+ stropt = option.BSDPString()
+ AssertEqual(t, stropt, "Unknown -> [1 2 3]", "BSDP string representation")
+}
diff --git a/dhcpv4/types.go b/dhcpv4/types.go
index f8d29bd..f840609 100644
--- a/dhcpv4/types.go
+++ b/dhcpv4/types.go
@@ -359,3 +359,19 @@ var OptionCodeToString = map[OptionCode]string{
OptionEnd: "End",
}
+
+// BSDPOptionCodeToString maps BSDP OptionCodes to human-readable strings
+// describing what they are.
+var BSDPOptionCodeToString = map[OptionCode]string{
+ BSDPOptionMessageType: "BSDP Message Type",
+ BSDPOptionVersion: "BSDP Version",
+ BSDPOptionServerIdentifier: "BSDP Server Identifier",
+ BSDPOptionServerPriority: "BSDP Server Priority",
+ BSDPOptionReplyPort: "BSDP Reply Port",
+ BSDPOptionBootImageListPath: "", // Not used
+ BSDPOptionDefaultBootImageID: "BSDP Default Boot Image ID",
+ BSDPOptionSelectedBootImageID: "BSDP Selected Boot Image ID",
+ BSDPOptionBootImageList: "BSDP Boot Image List",
+ BSDPOptionNetboot1_0Firmware: "BSDP Netboot 1.0 Firmware",
+ BSDPOptionBootImageAttributesFilterList: "BSDP Boot Image Attributes Filter List",
+}