diff options
author | Andrea Barberio <insomniac@slackware.it> | 2017-12-07 23:17:53 +0000 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2017-12-07 23:17:53 +0000 |
commit | 55d9d52d0a25d3826c5109f0bf101448322ec565 (patch) | |
tree | b953d5ad611eb4f6e6e5021d3f72fe258e1780da /dhcpv6/option_statuscode.go | |
parent | d38c49539b87fb57fec7ff62f787304e4f0eccee (diff) |
Refactored options into the dhcpv6 package to resolve circular imports. Sadly.
Diffstat (limited to 'dhcpv6/option_statuscode.go')
-rw-r--r-- | dhcpv6/option_statuscode.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/dhcpv6/option_statuscode.go b/dhcpv6/option_statuscode.go new file mode 100644 index 0000000..017a26f --- /dev/null +++ b/dhcpv6/option_statuscode.go @@ -0,0 +1,63 @@ +package dhcpv6 + +// This module defines the OptStatusCode structure. +// https://www.ietf.org/rfc/rfc3315.txt + +import ( + "encoding/binary" + "fmt" +) + +type OptStatusCode struct { + statusCode uint16 + statusMessage []byte +} + +func (op *OptStatusCode) Code() OptionCode { + return OPTION_STATUS_CODE +} + +func (op *OptStatusCode) ToBytes() []byte { + buf := make([]byte, 6) + binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_STATUS_CODE)) + binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) + binary.BigEndian.PutUint16(buf[4:6], op.statusCode) + buf = append(buf, op.statusMessage...) + return buf +} + +func (op *OptStatusCode) StatusCode() uint16 { + return op.statusCode +} + +func (op *OptStatusCode) SetStatusCode(code uint16) { + op.statusCode = code +} + +func (op *OptStatusCode) StatusMessage() uint16 { + return op.statusCode +} + +func (op *OptStatusCode) SetStatusMessage(message []byte) { + op.statusMessage = message +} + +func (op *OptStatusCode) Length() int { + return 2 + len(op.statusMessage) +} + +func (op *OptStatusCode) String() string { + return fmt.Sprintf("OptStatusCode{code=%v, message=%v}", op.statusCode, string(op.statusMessage)) +} + +// build an OptStatusCode structure from a sequence of bytes. +// The input data does not include option code and length bytes. +func ParseOptStatusCode(data []byte) (*OptStatusCode, error) { + if len(data) < 2 { + return nil, fmt.Errorf("Invalid OptStatusCode data: length is shorter than 2") + } + opt := OptStatusCode{} + opt.statusCode = binary.BigEndian.Uint16(data[0:2]) + opt.statusMessage = append(opt.statusMessage, data[2:]...) + return &opt, nil +} |