summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dhcpv6/dhcpv6_test.go4
-rw-r--r--dhcpv6/dhcpv6message.go9
-rw-r--r--dhcpv6/modifiers.go16
-rw-r--r--dhcpv6/modifiers_test.go6
-rw-r--r--dhcpv6/option_serverid.go31
-rw-r--r--dhcpv6/option_serverid_test.go38
-rw-r--r--dhcpv6/options.go2
7 files changed, 54 insertions, 52 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go
index 6f476a1..dad07f6 100644
--- a/dhcpv6/dhcpv6_test.go
+++ b/dhcpv6/dhcpv6_test.go
@@ -180,9 +180,7 @@ func TestNewReplyFromMessage(t *testing.T) {
}
var duid Duid
msg.AddOption(OptClientID(duid))
- sid := OptServerId{}
- sid.Sid = Duid{}
- msg.AddOption(&sid)
+ msg.AddOption(OptServerID(duid))
rep, err := NewReplyFromMessage(&msg, WithServerID(duid))
require.NoError(t, err)
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go
index 00452dc..bc87f9b 100644
--- a/dhcpv6/dhcpv6message.go
+++ b/dhcpv6/dhcpv6message.go
@@ -38,6 +38,15 @@ func (mo MessageOptions) ClientID() *Duid {
return &opt.(*optClientID).Duid
}
+// ServerID returns the server identifier option.
+func (mo MessageOptions) ServerID() *Duid {
+ opt := mo.GetOne(OptionServerID)
+ if opt == nil {
+ return nil
+ }
+ return &opt.(*optServerID).Duid
+}
+
// Message represents a DHCPv6 Message as defined by RFC 3315 Section 6.
type Message struct {
MessageType MessageType
diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go
index 4d00a0c..be4c367 100644
--- a/dhcpv6/modifiers.go
+++ b/dhcpv6/modifiers.go
@@ -8,19 +8,21 @@ import (
"github.com/insomniacslk/dhcp/rfc1035label"
)
-// WithClientID adds a client ID option to a DHCPv6 packet
-func WithClientID(duid Duid) Modifier {
+// WithOption adds the specific option to the DHCPv6 message.
+func WithOption(o Option) Modifier {
return func(d DHCPv6) {
- d.UpdateOption(OptClientID(duid))
+ d.UpdateOption(o)
}
}
+// WithClientID adds a client ID option to a DHCPv6 packet
+func WithClientID(duid Duid) Modifier {
+ return WithOption(OptClientID(duid))
+}
+
// WithServerID adds a client ID option to a DHCPv6 packet
func WithServerID(duid Duid) Modifier {
- return func(d DHCPv6) {
- sid := OptServerId{Sid: duid}
- d.UpdateOption(&sid)
- }
+ return WithOption(OptServerID(duid))
}
// WithNetboot adds bootfile URL and bootfile param options to a DHCPv6 packet.
diff --git a/dhcpv6/modifiers_test.go b/dhcpv6/modifiers_test.go
index ff3a87a..d904ca1 100644
--- a/dhcpv6/modifiers_test.go
+++ b/dhcpv6/modifiers_test.go
@@ -29,10 +29,8 @@ func TestWithServerID(t *testing.T) {
}
m, err := NewMessage(WithServerID(duid))
require.NoError(t, err)
- opt := m.GetOneOption(OptionServerID)
- require.NotNil(t, opt)
- sid := opt.(*OptServerId)
- require.Equal(t, sid.Sid, duid)
+ sid := m.Options.ServerID()
+ require.Equal(t, sid, &duid)
}
func TestWithRequestedOptions(t *testing.T) {
diff --git a/dhcpv6/option_serverid.go b/dhcpv6/option_serverid.go
index 2a60209..c2c10da 100644
--- a/dhcpv6/option_serverid.go
+++ b/dhcpv6/option_serverid.go
@@ -4,35 +4,30 @@ import (
"fmt"
)
-// OptServerId represents a Server ID option
-//
-// This module defines the OptServerId and DUID structures.
-// https://www.ietf.org/rfc/rfc3315.txt
-type OptServerId struct {
- Sid Duid
+// OptServerID represents a Server Identifier option as defined by RFC 3315
+// Section 22.1.
+func OptServerID(d Duid) Option {
+ return &optServerID{d}
}
-func (op *OptServerId) Code() OptionCode {
- return OptionServerID
+type optServerID struct {
+ Duid
}
-// ToBytes serializes this option.
-func (op *OptServerId) ToBytes() []byte {
- return op.Sid.ToBytes()
+func (*optServerID) Code() OptionCode {
+ return OptionServerID
}
-func (op *OptServerId) String() string {
- return fmt.Sprintf("OptServerId{sid=%v}", op.Sid.String())
+func (op *optServerID) String() string {
+ return fmt.Sprintf("ServerID: %v", op.Duid.String())
}
-// ParseOptServerId builds an OptServerId structure from a sequence of bytes.
+// parseOptServerID builds an optServerID structure from a sequence of bytes.
// The input data does not include option code and length bytes.
-func ParseOptServerId(data []byte) (*OptServerId, error) {
- var opt OptServerId
+func parseOptServerID(data []byte) (*optServerID, error) {
sid, err := DuidFromBytes(data)
if err != nil {
return nil, err
}
- opt.Sid = *sid
- return &opt, nil
+ return &optServerID{*sid}, nil
}
diff --git a/dhcpv6/option_serverid_test.go b/dhcpv6/option_serverid_test.go
index 23c00f5..05158c4 100644
--- a/dhcpv6/option_serverid_test.go
+++ b/dhcpv6/option_serverid_test.go
@@ -8,27 +8,27 @@ import (
"github.com/stretchr/testify/require"
)
-func TestParseOptServerId(t *testing.T) {
+func TestParseOptServerID(t *testing.T) {
data := []byte{
0, 3, // DUID_LL
0, 1, // hwtype ethernet
0, 1, 2, 3, 4, 5, // hw addr
}
- opt, err := ParseOptServerId(data)
+ opt, err := parseOptServerID(data)
require.NoError(t, err)
- require.Equal(t, DUID_LL, opt.Sid.Type)
- require.Equal(t, iana.HWTypeEthernet, opt.Sid.HwType)
- require.Equal(t, net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}), opt.Sid.LinkLayerAddr)
+ require.Equal(t, DUID_LL, opt.Type)
+ require.Equal(t, iana.HWTypeEthernet, opt.HwType)
+ require.Equal(t, net.HardwareAddr{0, 1, 2, 3, 4, 5}, opt.LinkLayerAddr)
}
func TestOptServerIdToBytes(t *testing.T) {
- opt := OptServerId{
- Sid: Duid{
+ opt := OptServerID(
+ Duid{
Type: DUID_LL,
HwType: iana.HWTypeEthernet,
- LinkLayerAddr: net.HardwareAddr([]byte{5, 4, 3, 2, 1, 0}),
+ LinkLayerAddr: net.HardwareAddr{5, 4, 3, 2, 1, 0},
},
- }
+ )
expected := []byte{
0, 3, // DUID_LL
0, 1, // hwtype ethernet
@@ -43,42 +43,42 @@ func TestOptServerIdDecodeEncode(t *testing.T) {
0, 1, // hwtype ethernet
5, 4, 3, 2, 1, 0, // hw addr
}
- opt, err := ParseOptServerId(data)
+ opt, err := parseOptServerID(data)
require.NoError(t, err)
require.Equal(t, data, opt.ToBytes())
}
func TestOptionServerId(t *testing.T) {
- opt := OptServerId{
- Sid: Duid{
+ opt := OptServerID(
+ Duid{
Type: DUID_LL,
HwType: iana.HWTypeEthernet,
- LinkLayerAddr: net.HardwareAddr([]byte{0xde, 0xad, 0, 0, 0xbe, 0xef}),
+ LinkLayerAddr: net.HardwareAddr{0xde, 0xad, 0, 0, 0xbe, 0xef},
},
- }
+ )
require.Equal(t, OptionServerID, opt.Code())
require.Contains(
t,
opt.String(),
- "sid=DUID{type=DUID-LL hwtype=Ethernet hwaddr=de:ad:00:00:be:ef}",
+ "ServerID: DUID{type=DUID-LL hwtype=Ethernet hwaddr=de:ad:00:00:be:ef}",
"String() should contain the correct sid output",
)
}
-func TestOptServerIdParseOptServerIdBogusDUID(t *testing.T) {
+func TestOptServerIdparseOptServerIDBogusDUID(t *testing.T) {
data := []byte{
0, 4, // DUID_UUID
1, 2, 3, 4, 5, 6, 7, 8, 9, // a UUID should be 18 bytes not 17
10, 11, 12, 13, 14, 15, 16, 17,
}
- _, err := ParseOptServerId(data)
+ _, err := parseOptServerID(data)
require.Error(t, err, "A truncated OptServerId DUID should return an error")
}
-func TestOptServerIdParseOptServerIdInvalidTooShort(t *testing.T) {
+func TestOptServerIdparseOptServerIDInvalidTooShort(t *testing.T) {
data := []byte{
0, // truncated: DUIDs are at least 2 bytes
}
- _, err := ParseOptServerId(data)
+ _, err := parseOptServerID(data)
require.Error(t, err, "A truncated OptServerId should return an error")
}
diff --git a/dhcpv6/options.go b/dhcpv6/options.go
index ef7c23a..f773c7f 100644
--- a/dhcpv6/options.go
+++ b/dhcpv6/options.go
@@ -42,7 +42,7 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) {
case OptionClientID:
opt, err = parseOptClientID(optData)
case OptionServerID:
- opt, err = ParseOptServerId(optData)
+ opt, err = parseOptServerID(optData)
case OptionIANA:
opt, err = ParseOptIANA(optData)
case OptionIAAddr: