From 74f2ee0b2fc6af0602360c563490c0b140f98b9e Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Mon, 30 Jul 2018 10:42:41 +0200 Subject: add OptIPAddressLeaseTime --- dhcpv4/option_ip_address_lease_time.go | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dhcpv4/option_ip_address_lease_time.go (limited to 'dhcpv4/option_ip_address_lease_time.go') diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go new file mode 100644 index 0000000..a171b04 --- /dev/null +++ b/dhcpv4/option_ip_address_lease_time.go @@ -0,0 +1,57 @@ +package dhcpv4 + +import ( + "encoding/binary" + "fmt" +) + +// This option implements the IP Address Lease Time option +// https://tools.ietf.org/html/rfc2132 + +// OptIPAddressLeaseTime represents the IP Address Lease Time option. +type OptIPAddressLeaseTime struct { + LeaseTime uint32 +} + +// ParseOptIPAddressLeaseTime constructs an OptIPAddressLeaseTime struct from a +// sequence of bytes and returns it, or an error. +func ParseOptIPAddressLeaseTime(data []byte) (*OptIPAddressLeaseTime, error) { + // Should at least have code, length, and lease time. + if len(data) < 4 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionIPAddressLeaseTime { + return nil, fmt.Errorf("expected option %v, got %v instead", OptionIPAddressLeaseTime, code) + } + length := int(data[1]) + if length != 4 { + return nil, fmt.Errorf("expected length 4, got %v instead", length) + } + leaseTime := binary.BigEndian.Uint32(data[2:6]) + return &OptIPAddressLeaseTime{LeaseTime: leaseTime}, nil +} + +// Code returns the option code. +func (o *OptIPAddressLeaseTime) Code() OptionCode { + return OptionIPAddressLeaseTime +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptIPAddressLeaseTime) ToBytes() []byte { + serializedTime := make([]byte, 4) + binary.BigEndian.PutUint32(serializedTime, o.LeaseTime) + serializedOpt := []byte{byte(o.Code()), byte(o.Length())} + return append(serializedOpt, serializedTime...) +} + +// String returns a human-readable string for this option. +func (o *OptIPAddressLeaseTime) String() string { + return fmt.Sprintf("IP Addresses Lease Time -> %v", o.LeaseTime) +} + +// Length returns the length of the data portion (excluding option code and byte +// for length, if any). +func (o *OptIPAddressLeaseTime) Length() int { + return 4 +} -- cgit v1.2.3 From c3434f38325856dcdc19adc29b5e31944614221b Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Mon, 30 Jul 2018 19:05:13 +0200 Subject: fix OptIPAddressLeaseTime --- dhcpv4/option_ip_address_lease_time.go | 2 +- dhcpv4/option_ip_address_lease_time_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'dhcpv4/option_ip_address_lease_time.go') diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go index a171b04..7562c58 100644 --- a/dhcpv4/option_ip_address_lease_time.go +++ b/dhcpv4/option_ip_address_lease_time.go @@ -17,7 +17,7 @@ type OptIPAddressLeaseTime struct { // sequence of bytes and returns it, or an error. func ParseOptIPAddressLeaseTime(data []byte) (*OptIPAddressLeaseTime, error) { // Should at least have code, length, and lease time. - if len(data) < 4 { + if len(data) < 6 { return nil, ErrShortByteStream } code := OptionCode(data[0]) diff --git a/dhcpv4/option_ip_address_lease_time_test.go b/dhcpv4/option_ip_address_lease_time_test.go index 66715bd..7d507bf 100644 --- a/dhcpv4/option_ip_address_lease_time_test.go +++ b/dhcpv4/option_ip_address_lease_time_test.go @@ -20,7 +20,7 @@ func TestParseOptIPAddressLeaseTime(t *testing.T) { require.Equal(t, &OptIPAddressLeaseTime{LeaseTime: 43200}, o) // Short byte stream - data = []byte{51, 4} + data = []byte{51, 4, 168, 192} _, err = ParseOptIPAddressLeaseTime(data) require.Error(t, err, "should get error from short byte stream") -- cgit v1.2.3