summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/dhcpv6relay.go2
-rw-r--r--dhcpv6/duid.go (renamed from dhcpv6/option_duid.go)4
-rw-r--r--dhcpv6/option_relaymsg_test.go39
-rw-r--r--dhcpv6/options.go3
4 files changed, 44 insertions, 4 deletions
diff --git a/dhcpv6/dhcpv6relay.go b/dhcpv6/dhcpv6relay.go
index b8d635e..236a23a 100644
--- a/dhcpv6/dhcpv6relay.go
+++ b/dhcpv6/dhcpv6relay.go
@@ -56,7 +56,7 @@ func (r *DHCPv6Relay) ToBytes() []byte {
func (r *DHCPv6Relay) Length() int {
mLen := RelayHeaderSize
for _, opt := range r.options {
- mLen += opt.Length()
+ mLen += opt.Length() + 4 // +4 for opt code and opt len
}
return mLen
}
diff --git a/dhcpv6/option_duid.go b/dhcpv6/duid.go
index 4d32d6e..59a48a4 100644
--- a/dhcpv6/option_duid.go
+++ b/dhcpv6/duid.go
@@ -37,7 +37,7 @@ func (d *Duid) Length() int {
if d.Type == DUID_EN {
return 6 + len(d.EnterpriseIdentifier)
}
- return 0 // should never happen
+ panic(fmt.Sprintf("Unknown DUID type: %v", d.Type))
}
func (d *Duid) ToBytes() []byte {
@@ -53,7 +53,7 @@ func (d *Duid) ToBytes() []byte {
binary.BigEndian.PutUint32(buf[2:6], d.EnterpriseNumber)
return append(buf, d.EnterpriseIdentifier...)
}
- return []byte{} // should never happen
+ panic(fmt.Sprintf("Unknown DUID type: %v", d.Type))
}
func (d *Duid) String() string {
diff --git a/dhcpv6/option_relaymsg_test.go b/dhcpv6/option_relaymsg_test.go
index 93bab2f..3eb3260 100644
--- a/dhcpv6/option_relaymsg_test.go
+++ b/dhcpv6/option_relaymsg_test.go
@@ -116,3 +116,42 @@ func TestRelayMsgParseOptRelayMsgSingleEncapsulation(t *testing.T) {
t.Fatal("Invalid elapsed time. Expected 0x1122, got 0x%04x", eTime)
}
}
+
+func TestSample(t *testing.T) {
+ // Nested relay message. This test only checks if it parses correctly, but
+ // could/should be extended to check all the fields like done in other tests
+ buf := []byte{
+ 12, // relay
+ 1, // hop count
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // linkAddr
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // peerAddr
+ // relay msg
+ 0, 9, // opt relay msg
+ 0, 66, // opt len
+ // relay fwd
+ 12, // relay
+ 0, // hop count
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // linkAddr
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // peerAddr
+ // opt interface ID
+ 0, 18, // interface id
+ 0, 6, // opt len
+ 0xba, 0xbe, 0xb1, 0xb0, 0xbe, 0xbe, // opt value
+ // relay msg
+ 0, 9, // relay msg
+ 0, 18, // msg len
+ // dhcpv6 msg
+ 1, // solicit
+ 0xaa, 0xbb, 0xcc, // transaction ID
+ // client ID
+ 0, 1, // opt client id
+ 0, 10, // opt len
+ 0, 3, // duid type
+ 0, 1, // hw type
+ 5, 6, 7, 8, 9, 10, // duid value
+ }
+ _, err := FromBytes(buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/dhcpv6/options.go b/dhcpv6/options.go
index 2fcc9e9..bb80791 100644
--- a/dhcpv6/options.go
+++ b/dhcpv6/options.go
@@ -97,7 +97,8 @@ func ParseOption(dataStart []byte) (Option, error) {
return nil, err
}
if length != opt.Length() {
- return nil, fmt.Errorf("different lengths for opt %v: %v != %v", code, length, opt.Length())
+ return nil, fmt.Errorf("Error: declared length is different from actual length for option %v: %v != %v",
+ code, opt.Length(), length)
}
return opt, nil
}