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_iaprefix.go | |
parent | d38c49539b87fb57fec7ff62f787304e4f0eccee (diff) |
Refactored options into the dhcpv6 package to resolve circular imports. Sadly.
Diffstat (limited to 'dhcpv6/option_iaprefix.go')
-rw-r--r-- | dhcpv6/option_iaprefix.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/dhcpv6/option_iaprefix.go b/dhcpv6/option_iaprefix.go new file mode 100644 index 0000000..b4adacb --- /dev/null +++ b/dhcpv6/option_iaprefix.go @@ -0,0 +1,98 @@ +package dhcpv6 + +// This module defines the OptIAPrefix structure. +// https://www.ietf.org/rfc/rfc3633.txt + +import ( + "encoding/binary" + "fmt" + "net" +) + +type OptIAPrefix struct { + preferredLifetime uint32 + validLifetime uint32 + prefixLength byte + ipv6Prefix [16]byte + options []byte +} + +func (op *OptIAPrefix) Code() OptionCode { + return OPTION_IAPREFIX +} + +func (op *OptIAPrefix) ToBytes() []byte { + buf := make([]byte, 25) + binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_IAPREFIX)) + binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) + binary.BigEndian.PutUint32(buf[4:8], op.preferredLifetime) + binary.BigEndian.PutUint32(buf[8:12], op.validLifetime) + buf = append(buf, op.prefixLength) + buf = append(buf, op.ipv6Prefix[:]...) + buf = append(buf, op.options...) + return buf +} + +func (op *OptIAPrefix) PreferredLifetime() uint32 { + return op.preferredLifetime +} + +func (op *OptIAPrefix) SetPreferredLifetime(pl uint32) { + op.preferredLifetime = pl +} + +func (op *OptIAPrefix) ValidLifetime() uint32 { + return op.validLifetime +} + +func (op *OptIAPrefix) SetValidLifetime(vl uint32) { + op.validLifetime = vl +} + +func (op *OptIAPrefix) PrefixLength() byte { + return op.prefixLength +} + +func (op *OptIAPrefix) SetPrefixLength(pl byte) { + op.prefixLength = pl +} + +func (op *OptIAPrefix) IPv6Prefix() []byte { + return op.ipv6Prefix[:] +} + +func (op *OptIAPrefix) SetIPv6Prefix(p [16]byte) { + op.ipv6Prefix = p +} + +func (op *OptIAPrefix) Options() []byte { + return op.options +} + +func (op *OptIAPrefix) SetOptions(options []byte) { + op.options = options +} + +func (op *OptIAPrefix) Length() int { + return 25 + len(op.options) +} + +func (op *OptIAPrefix) String() string { + return fmt.Sprintf("OptIAPrefix{preferredlifetime=%v, validlifetime=%v, prefixlength=%v, ipv6prefix=%v, options=%v}", + op.preferredLifetime, op.validLifetime, op.prefixLength, net.IP(op.ipv6Prefix[:]), op.options) +} + +// build an OptIAPrefix structure from a sequence of bytes. +// The input data does not include option code and length bytes. +func ParseOptIAPrefix(data []byte) (*OptIAPrefix, error) { + opt := OptIAPrefix{} + if len(data) < 12 { + return nil, fmt.Errorf("Invalid IA for Prefix Delegation data length. Expected at least 12 bytes, got %v", len(data)) + } + opt.preferredLifetime = binary.BigEndian.Uint32(data[:4]) + opt.validLifetime = binary.BigEndian.Uint32(data[4:8]) + opt.prefixLength = data[9] + copy(opt.ipv6Prefix[:], data[9:17]) + copy(opt.options, data[17:]) + return &opt, nil +} |