diff options
-rw-r--r-- | dhcpv4/option_ip_address_lease_time.go | 57 | ||||
-rw-r--r-- | dhcpv4/option_ip_address_lease_time_test.go | 41 | ||||
-rw-r--r-- | dhcpv4/option_maximum_dhcp_message_size.go | 4 | ||||
-rw-r--r-- | dhcpv4/option_maximum_dhcp_message_size_test.go | 2 | ||||
-rw-r--r-- | dhcpv4/options.go | 2 |
5 files changed, 103 insertions, 3 deletions
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 +} diff --git a/dhcpv4/option_ip_address_lease_time_test.go b/dhcpv4/option_ip_address_lease_time_test.go new file mode 100644 index 0000000..36bfe40 --- /dev/null +++ b/dhcpv4/option_ip_address_lease_time_test.go @@ -0,0 +1,41 @@ +package dhcpv4 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptIPAddressLeaseTimeInterfaceMethods(t *testing.T) { + o := OptIPAddressLeaseTime{LeaseTime: 43200} + require.Equal(t, OptionIPAddressLeaseTime, o.Code(), "Code") + require.Equal(t, 4, o.Length(), "Length") + require.Equal(t, []byte{51, 4, 0, 0, 168, 192}, o.ToBytes(), "ToBytes") +} + +func TestParseOptIPAddressLeaseTime(t *testing.T) { + data := []byte{51, 4, 0, 0, 168, 192} + o, err := ParseOptIPAddressLeaseTime(data) + require.NoError(t, err) + require.Equal(t, &OptIPAddressLeaseTime{LeaseTime: 43200}, o) + + // Short byte stream + data = []byte{51, 4} + _, err = ParseOptIPAddressLeaseTime(data) + require.Error(t, err, "should get error from short byte stream") + + // Wrong code + data = []byte{54, 4, 0, 0, 168, 192} + _, err = ParseOptIPAddressLeaseTime(data) + require.Error(t, err, "should get error from wrong code") + + // Bad length + data = []byte{51, 5, 1, 1, 1, 1, 1} + _, err = ParseOptMaximumDHCPMessageSize(data) + require.Error(t, err, "should get error from bad length") +} + +func TestOptIPAddressLeaseTimeString(t *testing.T) { + o := OptIPAddressLeaseTime{LeaseTime: 43200} + require.Equal(t, "IP Addresses Lease Time -> 43200", o.String()) +} diff --git a/dhcpv4/option_maximum_dhcp_message_size.go b/dhcpv4/option_maximum_dhcp_message_size.go index 05186f5..e5fedc6 100644 --- a/dhcpv4/option_maximum_dhcp_message_size.go +++ b/dhcpv4/option_maximum_dhcp_message_size.go @@ -8,7 +8,7 @@ import ( // This option implements the Maximum DHCP Message size option // https://tools.ietf.org/html/rfc2132 -// OptMaximumDHCPMessageSize represents the DHCP message type option. +// OptMaximumDHCPMessageSize represents the Maximum DHCP Message size option. type OptMaximumDHCPMessageSize struct { Size uint16 } @@ -16,7 +16,7 @@ type OptMaximumDHCPMessageSize struct { // ParseOptMaximumDHCPMessageSize constructs an OptMaximumDHCPMessageSize struct from a sequence of // bytes and returns it, or an error. func ParseOptMaximumDHCPMessageSize(data []byte) (*OptMaximumDHCPMessageSize, error) { - // Should at least have code, length, and message type. + // Should at least have code, length, and message size. if len(data) < 4 { return nil, ErrShortByteStream } diff --git a/dhcpv4/option_maximum_dhcp_message_size_test.go b/dhcpv4/option_maximum_dhcp_message_size_test.go index 65a26fc..f24b499 100644 --- a/dhcpv4/option_maximum_dhcp_message_size_test.go +++ b/dhcpv4/option_maximum_dhcp_message_size_test.go @@ -14,7 +14,7 @@ func TestOptMaximumDHCPMessageSizeInterfaceMethods(t *testing.T) { } func TestParseOptMaximumDHCPMessageSize(t *testing.T) { - data := []byte{57, 2, 5, 220} // DISCOVER + data := []byte{57, 2, 5, 220} o, err := ParseOptMaximumDHCPMessageSize(data) require.NoError(t, err) require.Equal(t, &OptMaximumDHCPMessageSize{Size: 1500}, o) diff --git a/dhcpv4/options.go b/dhcpv4/options.go index 4224fa0..760bc98 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -46,6 +46,8 @@ func ParseOption(data []byte) (Option, error) { opt, err = ParseOptRouter(data) case OptionRequestedIPAddress: opt, err = ParseOptRequestedIPAddress(data) + case OptionIPAddressLeaseTime: + opt, err = ParseOptIPAddressLeaseTime(data) case OptionDHCPMessageType: opt, err = ParseOptMessageType(data) case OptionParameterRequestList: |