summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_clientid.go
diff options
context:
space:
mode:
authorAndrea Barberio <insomniac@slackware.it>2017-12-07 23:17:53 +0000
committerAndrea Barberio <insomniac@slackware.it>2017-12-07 23:17:53 +0000
commit55d9d52d0a25d3826c5109f0bf101448322ec565 (patch)
treeb953d5ad611eb4f6e6e5021d3f72fe258e1780da /dhcpv6/option_clientid.go
parentd38c49539b87fb57fec7ff62f787304e4f0eccee (diff)
Refactored options into the dhcpv6 package to resolve circular imports. Sadly.
Diffstat (limited to 'dhcpv6/option_clientid.go')
-rw-r--r--dhcpv6/option_clientid.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/dhcpv6/option_clientid.go b/dhcpv6/option_clientid.go
new file mode 100644
index 0000000..1a61047
--- /dev/null
+++ b/dhcpv6/option_clientid.go
@@ -0,0 +1,57 @@
+package dhcpv6
+
+// This module defines the OptClientId and DUID structures.
+// https://www.ietf.org/rfc/rfc3315.txt
+
+import (
+ "encoding/binary"
+ "fmt"
+)
+
+type OptClientId struct {
+ cid Duid
+}
+
+func (op *OptClientId) Code() OptionCode {
+ return OPTION_CLIENTID
+}
+
+func (op *OptClientId) ToBytes() []byte {
+ buf := make([]byte, 4)
+ binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_CLIENTID))
+ binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
+ buf = append(buf, op.cid.ToBytes()...)
+ return buf
+}
+
+func (op *OptClientId) ClientID() Duid {
+ return op.cid
+}
+
+func (op *OptClientId) SetClientID(cid Duid) {
+ op.cid = cid
+}
+
+func (op *OptClientId) Length() int {
+ return op.cid.Length()
+}
+
+func (op *OptClientId) String() string {
+ return fmt.Sprintf("OptClientId{cid=%v}", op.cid.String())
+}
+
+// build an OptClientId structure from a sequence of bytes.
+// The input data does not include option code and length bytes.
+func ParseOptClientId(data []byte) (*OptClientId, error) {
+ if len(data) < 2 {
+ // at least the DUID type is necessary to continue
+ return nil, fmt.Errorf("Invalid OptClientId data: shorter than 2 bytes")
+ }
+ opt := OptClientId{}
+ cid, err := DuidFromBytes(data)
+ if err != nil {
+ return nil, err
+ }
+ opt.cid = *cid
+ return &opt, nil
+}