summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2019-12-28 01:56:49 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2020-03-05 15:51:55 +0000
commitee59cc7ad87ad6705c3f4b9f9a73b6a1f690a330 (patch)
treea30f6e0d73baec498e70e2897e4fc548a0bd4718
parentfc0886ce943f6f84775882b2147782a4ec26fb1f (diff)
v6: Status getter for message options
Status Code is one of the few options used across many Option types. Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/dhcpv6message.go13
-rw-r--r--dhcpv6/option_statuscode.go16
-rw-r--r--dhcpv6/option_statuscode_test.go8
3 files changed, 25 insertions, 12 deletions
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go
index ba592d6..1bc8670 100644
--- a/dhcpv6/dhcpv6message.go
+++ b/dhcpv6/dhcpv6message.go
@@ -66,6 +66,19 @@ func (mo MessageOptions) OneIANA() *OptIANA {
return ianas[0]
}
+// Status returns the status code associated with this option.
+func (mo MessageOptions) Status() *OptStatusCode {
+ opt := mo.Options.GetOne(OptionStatusCode)
+ if opt == nil {
+ return nil
+ }
+ sc, ok := opt.(*OptStatusCode)
+ if !ok {
+ return nil
+ }
+ return sc
+}
+
// Message represents a DHCPv6 Message as defined by RFC 3315 Section 6.
type Message struct {
MessageType MessageType
diff --git a/dhcpv6/option_statuscode.go b/dhcpv6/option_statuscode.go
index db0df3e..9248cbd 100644
--- a/dhcpv6/option_statuscode.go
+++ b/dhcpv6/option_statuscode.go
@@ -13,26 +13,26 @@ import (
// https://www.ietf.org/rfc/rfc3315.txt
type OptStatusCode struct {
StatusCode iana.StatusCode
- StatusMessage []byte
+ StatusMessage string
}
-// Code returns the option code
+// Code returns the option code.
func (op *OptStatusCode) Code() OptionCode {
return OptionStatusCode
}
-// ToBytes serializes the option and returns it as a sequence of bytes
+// ToBytes serializes the option and returns it as a sequence of bytes.
func (op *OptStatusCode) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
buf.Write16(uint16(op.StatusCode))
- buf.WriteBytes(op.StatusMessage)
+ buf.WriteBytes([]byte(op.StatusMessage))
return buf.Data()
}
+// String returns a human-readable option.
func (op *OptStatusCode) String() string {
- return fmt.Sprintf("OptStatusCode{code=%s (%d), message=%v}",
- op.StatusCode.String(), op.StatusCode,
- string(op.StatusMessage))
+ return fmt.Sprintf("StatusCode: Code: %s (%d); Message: %s",
+ op.StatusCode, op.StatusCode, op.StatusMessage)
}
// ParseOptStatusCode builds an OptStatusCode structure from a sequence of
@@ -41,6 +41,6 @@ func ParseOptStatusCode(data []byte) (*OptStatusCode, error) {
var opt OptStatusCode
buf := uio.NewBigEndianBuffer(data)
opt.StatusCode = iana.StatusCode(buf.Read16())
- opt.StatusMessage = buf.ReadAll()
+ opt.StatusMessage = string(buf.ReadAll())
return &opt, buf.FinError()
}
diff --git a/dhcpv6/option_statuscode_test.go b/dhcpv6/option_statuscode_test.go
index cf43ab7..1478678 100644
--- a/dhcpv6/option_statuscode_test.go
+++ b/dhcpv6/option_statuscode_test.go
@@ -15,7 +15,7 @@ func TestParseOptStatusCode(t *testing.T) {
opt, err := ParseOptStatusCode(data)
require.NoError(t, err)
require.Equal(t, iana.StatusUseMulticast, opt.StatusCode)
- require.Equal(t, []byte("use multicast"), opt.StatusMessage)
+ require.Equal(t, "use multicast", opt.StatusMessage)
}
func TestOptStatusCodeToBytes(t *testing.T) {
@@ -24,8 +24,8 @@ func TestOptStatusCodeToBytes(t *testing.T) {
's', 'u', 'c', 'c', 'e', 's', 's',
}
opt := OptStatusCode{
- iana.StatusSuccess,
- []byte("success"),
+ StatusCode: iana.StatusSuccess,
+ StatusMessage: "success",
}
actual := opt.ToBytes()
require.Equal(t, expected, actual)
@@ -47,7 +47,7 @@ func TestOptStatusCodeString(t *testing.T) {
require.Contains(
t,
opt.String(),
- "code=UseMulticast (5), message=use multicast",
+ "Code: UseMulticast (5); Message: use multicast",
"String() should contain the code and message",
)
}