diff options
author | Christopher Koch <c@chrisko.ch> | 2019-01-15 21:11:07 +0000 |
---|---|---|
committer | Chris K <c@chrisko.ch> | 2019-01-19 14:44:00 -0800 |
commit | 0b587bcfd1b0e8808e57d77fb4028adbd54bfd25 (patch) | |
tree | b9431e29f54157978762db67d36c1b19a7f3b3ef /dhcpv4 | |
parent | feb8958aff848a6d7363fea2e29a2ac071685055 (diff) |
dhcpv4: consolidate all IP options into one file.
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/option_broadcast_address.go | 36 | ||||
-rw-r--r-- | dhcpv4/option_broadcast_address_test.go | 36 | ||||
-rw-r--r-- | dhcpv4/option_ip.go | 92 | ||||
-rw-r--r-- | dhcpv4/option_ip_test.go | 68 | ||||
-rw-r--r-- | dhcpv4/option_requested_ip_address.go | 36 | ||||
-rw-r--r-- | dhcpv4/option_requested_ip_address_test.go | 36 | ||||
-rw-r--r-- | dhcpv4/option_server_identifier.go | 36 | ||||
-rw-r--r-- | dhcpv4/option_server_identifier_test.go | 36 |
8 files changed, 160 insertions, 216 deletions
diff --git a/dhcpv4/option_broadcast_address.go b/dhcpv4/option_broadcast_address.go deleted file mode 100644 index 0f5fb4b..0000000 --- a/dhcpv4/option_broadcast_address.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "net" - - "github.com/u-root/u-root/pkg/uio" -) - -// OptBroadcastAddress implements the broadcast address option described in RFC -// 2132, Section 5.3. -type OptBroadcastAddress struct { - BroadcastAddress net.IP -} - -// ParseOptBroadcastAddress returns a new OptBroadcastAddress from a byte -// stream, or error if any. -func ParseOptBroadcastAddress(data []byte) (*OptBroadcastAddress, error) { - buf := uio.NewBigEndianBuffer(data) - return &OptBroadcastAddress{BroadcastAddress: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() -} - -// Code returns the option code. -func (o *OptBroadcastAddress) Code() OptionCode { - return OptionBroadcastAddress -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptBroadcastAddress) ToBytes() []byte { - return []byte(o.BroadcastAddress.To4()) -} - -// String returns a human-readable string. -func (o *OptBroadcastAddress) String() string { - return fmt.Sprintf("Broadcast Address -> %v", o.BroadcastAddress.String()) -} diff --git a/dhcpv4/option_broadcast_address_test.go b/dhcpv4/option_broadcast_address_test.go deleted file mode 100644 index 5b6b44f..0000000 --- a/dhcpv4/option_broadcast_address_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptBroadcastAddressInterfaceMethods(t *testing.T) { - ip := net.IP{192, 168, 0, 1} - o := OptBroadcastAddress{BroadcastAddress: ip} - - require.Equal(t, OptionBroadcastAddress, o.Code(), "Code") - - expectedBytes := []byte{192, 168, 0, 1} - require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes") - - require.Equal(t, "Broadcast Address -> 192.168.0.1", o.String(), "String") -} - -func TestParseOptBroadcastAddress(t *testing.T) { - var ( - o *OptBroadcastAddress - err error - ) - o, err = ParseOptBroadcastAddress([]byte{}) - require.Error(t, err, "empty byte stream") - - o, err = ParseOptBroadcastAddress([]byte{192, 168, 0}) - require.Error(t, err, "wrong IP length") - - o, err = ParseOptBroadcastAddress([]byte{192, 168, 0, 1}) - require.NoError(t, err) - require.Equal(t, net.IP{192, 168, 0, 1}, o.BroadcastAddress) -} diff --git a/dhcpv4/option_ip.go b/dhcpv4/option_ip.go new file mode 100644 index 0000000..ee0f5fe --- /dev/null +++ b/dhcpv4/option_ip.go @@ -0,0 +1,92 @@ +package dhcpv4 + +import ( + "fmt" + "net" + + "github.com/u-root/u-root/pkg/uio" +) + +// OptBroadcastAddress implements the broadcast address option described in RFC +// 2132, Section 5.3. +type OptBroadcastAddress struct { + BroadcastAddress net.IP +} + +// ParseOptBroadcastAddress returns a new OptBroadcastAddress from a byte +// stream, or error if any. +func ParseOptBroadcastAddress(data []byte) (*OptBroadcastAddress, error) { + buf := uio.NewBigEndianBuffer(data) + return &OptBroadcastAddress{BroadcastAddress: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() +} + +// Code returns the option code. +func (o *OptBroadcastAddress) Code() OptionCode { + return OptionBroadcastAddress +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptBroadcastAddress) ToBytes() []byte { + return []byte(o.BroadcastAddress.To4()) +} + +// String returns a human-readable string. +func (o *OptBroadcastAddress) String() string { + return fmt.Sprintf("Broadcast Address -> %v", o.BroadcastAddress.String()) +} + +// OptRequestedIPAddress implements the requested IP address option described +// by RFC 2132, Section 9.1. +type OptRequestedIPAddress struct { + RequestedAddr net.IP +} + +// ParseOptRequestedIPAddress returns a new OptRequestedIPAddress from a byte +// stream, or error if any. +func ParseOptRequestedIPAddress(data []byte) (*OptRequestedIPAddress, error) { + buf := uio.NewBigEndianBuffer(data) + return &OptRequestedIPAddress{RequestedAddr: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() +} + +// Code returns the option code. +func (o *OptRequestedIPAddress) Code() OptionCode { + return OptionRequestedIPAddress +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptRequestedIPAddress) ToBytes() []byte { + return o.RequestedAddr.To4() +} + +// String returns a human-readable string. +func (o *OptRequestedIPAddress) String() string { + return fmt.Sprintf("Requested IP Address -> %v", o.RequestedAddr.String()) +} + +// OptServerIdentifier implements the server identifier option described by RFC +// 2132, Section 9.7. +type OptServerIdentifier struct { + ServerID net.IP +} + +// ParseOptServerIdentifier returns a new OptServerIdentifier from a byte +// stream, or error if any. +func ParseOptServerIdentifier(data []byte) (*OptServerIdentifier, error) { + buf := uio.NewBigEndianBuffer(data) + return &OptServerIdentifier{ServerID: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() +} + +// Code returns the option code. +func (o *OptServerIdentifier) Code() OptionCode { + return OptionServerIdentifier +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptServerIdentifier) ToBytes() []byte { + return o.ServerID.To4() +} + +// String returns a human-readable string. +func (o *OptServerIdentifier) String() string { + return fmt.Sprintf("Server Identifier -> %v", o.ServerID.String()) +} diff --git a/dhcpv4/option_ip_test.go b/dhcpv4/option_ip_test.go new file mode 100644 index 0000000..fe31487 --- /dev/null +++ b/dhcpv4/option_ip_test.go @@ -0,0 +1,68 @@ +package dhcpv4 + +import ( + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptBroadcastAddress(t *testing.T) { + o := OptBroadcastAddress{BroadcastAddress: net.IP{192, 168, 0, 1}} + + require.Equal(t, OptionBroadcastAddress, o.Code(), "Code") + require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes") + require.Equal(t, "Broadcast Address -> 192.168.0.1", o.String(), "String") +} + +func TestParseOptBroadcastAddress(t *testing.T) { + o, err := ParseOptBroadcastAddress([]byte{}) + require.Error(t, err, "empty byte stream") + + o, err = ParseOptBroadcastAddress([]byte{192, 168, 0}) + require.Error(t, err, "wrong IP length") + + o, err = ParseOptBroadcastAddress([]byte{192, 168, 0, 1}) + require.NoError(t, err) + require.Equal(t, net.IP{192, 168, 0, 1}, o.BroadcastAddress) +} + +func TestOptRequestedIPAddress(t *testing.T) { + o := OptRequestedIPAddress{RequestedAddr: net.IP{192, 168, 0, 1}} + + require.Equal(t, OptionRequestedIPAddress, o.Code(), "Code") + require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes") + require.Equal(t, "Requested IP Address -> 192.168.0.1", o.String(), "String") +} + +func TestParseOptRequestedIPAddress(t *testing.T) { + o, err := ParseOptRequestedIPAddress([]byte{}) + require.Error(t, err, "empty byte stream") + + o, err = ParseOptRequestedIPAddress([]byte{192}) + require.Error(t, err, "wrong IP length") + + o, err = ParseOptRequestedIPAddress([]byte{192, 168, 0, 1}) + require.NoError(t, err) + require.Equal(t, net.IP{192, 168, 0, 1}, o.RequestedAddr) +} + +func TestOptServerIdentifierInterfaceMethods(t *testing.T) { + o := OptServerIdentifier{ServerID: net.IP{192, 168, 0, 1}} + + require.Equal(t, OptionServerIdentifier, o.Code(), "Code") + require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes") + require.Equal(t, "Server Identifier -> 192.168.0.1", o.String(), "String") +} + +func TestParseOptServerIdentifier(t *testing.T) { + o, err := ParseOptServerIdentifier([]byte{}) + require.Error(t, err, "empty byte stream") + + o, err = ParseOptServerIdentifier([]byte{192, 168, 0}) + require.Error(t, err, "wrong IP length") + + o, err = ParseOptServerIdentifier([]byte{192, 168, 0, 1}) + require.NoError(t, err) + require.Equal(t, net.IP{192, 168, 0, 1}, o.ServerID) +} diff --git a/dhcpv4/option_requested_ip_address.go b/dhcpv4/option_requested_ip_address.go deleted file mode 100644 index bd3bc09..0000000 --- a/dhcpv4/option_requested_ip_address.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "net" - - "github.com/u-root/u-root/pkg/uio" -) - -// OptRequestedIPAddress implements the requested IP address option described -// by RFC 2132, Section 9.1. -type OptRequestedIPAddress struct { - RequestedAddr net.IP -} - -// ParseOptRequestedIPAddress returns a new OptServerIdentifier from a byte -// stream, or error if any. -func ParseOptRequestedIPAddress(data []byte) (*OptRequestedIPAddress, error) { - buf := uio.NewBigEndianBuffer(data) - return &OptRequestedIPAddress{RequestedAddr: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() -} - -// Code returns the option code. -func (o *OptRequestedIPAddress) Code() OptionCode { - return OptionRequestedIPAddress -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptRequestedIPAddress) ToBytes() []byte { - return o.RequestedAddr.To4() -} - -// String returns a human-readable string. -func (o *OptRequestedIPAddress) String() string { - return fmt.Sprintf("Requested IP Address -> %v", o.RequestedAddr.String()) -} diff --git a/dhcpv4/option_requested_ip_address_test.go b/dhcpv4/option_requested_ip_address_test.go deleted file mode 100644 index 7d33b1e..0000000 --- a/dhcpv4/option_requested_ip_address_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptRequestedIPAddressInterfaceMethods(t *testing.T) { - ip := net.IP{192, 168, 0, 1} - o := OptRequestedIPAddress{RequestedAddr: ip} - - require.Equal(t, OptionRequestedIPAddress, o.Code(), "Code") - - expectedBytes := []byte{192, 168, 0, 1} - require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes") - - require.Equal(t, "Requested IP Address -> 192.168.0.1", o.String(), "String") -} - -func TestParseOptRequestedIPAddress(t *testing.T) { - var ( - o *OptRequestedIPAddress - err error - ) - o, err = ParseOptRequestedIPAddress([]byte{}) - require.Error(t, err, "empty byte stream") - - o, err = ParseOptRequestedIPAddress([]byte{192}) - require.Error(t, err, "wrong IP length") - - o, err = ParseOptRequestedIPAddress([]byte{192, 168, 0, 1}) - require.NoError(t, err) - require.Equal(t, net.IP{192, 168, 0, 1}, o.RequestedAddr) -} diff --git a/dhcpv4/option_server_identifier.go b/dhcpv4/option_server_identifier.go deleted file mode 100644 index 6fc09a9..0000000 --- a/dhcpv4/option_server_identifier.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "net" - - "github.com/u-root/u-root/pkg/uio" -) - -// OptServerIdentifier implements the server identifier option described by RFC -// 2132, Section 9.7. -type OptServerIdentifier struct { - ServerID net.IP -} - -// ParseOptServerIdentifier returns a new OptServerIdentifier from a byte -// stream, or error if any. -func ParseOptServerIdentifier(data []byte) (*OptServerIdentifier, error) { - buf := uio.NewBigEndianBuffer(data) - return &OptServerIdentifier{ServerID: net.IP(buf.CopyN(net.IPv4len))}, buf.FinError() -} - -// Code returns the option code. -func (o *OptServerIdentifier) Code() OptionCode { - return OptionServerIdentifier -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptServerIdentifier) ToBytes() []byte { - return o.ServerID.To4() -} - -// String returns a human-readable string. -func (o *OptServerIdentifier) String() string { - return fmt.Sprintf("Server Identifier -> %v", o.ServerID.String()) -} diff --git a/dhcpv4/option_server_identifier_test.go b/dhcpv4/option_server_identifier_test.go deleted file mode 100644 index e250ad7..0000000 --- a/dhcpv4/option_server_identifier_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptServerIdentifierInterfaceMethods(t *testing.T) { - ip := net.IP{192, 168, 0, 1} - o := OptServerIdentifier{ServerID: ip} - - require.Equal(t, OptionServerIdentifier, o.Code(), "Code") - - expectedBytes := []byte{192, 168, 0, 1} - require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes") - - require.Equal(t, "Server Identifier -> 192.168.0.1", o.String(), "String") -} - -func TestParseOptServerIdentifier(t *testing.T) { - var ( - o *OptServerIdentifier - err error - ) - o, err = ParseOptServerIdentifier([]byte{}) - require.Error(t, err, "empty byte stream") - - o, err = ParseOptServerIdentifier([]byte{192, 168, 0}) - require.Error(t, err, "wrong IP length") - - o, err = ParseOptServerIdentifier([]byte{192, 168, 0, 1}) - require.NoError(t, err) - require.Equal(t, net.IP{192, 168, 0, 1}, o.ServerID) -} |