diff options
-rw-r--r-- | dhcpv4/option_domain_name_server.go | 40 | ||||
-rw-r--r-- | dhcpv4/option_domain_name_server_test.go | 50 | ||||
-rw-r--r-- | dhcpv4/option_ips.go (renamed from dhcpv4/option_router.go) | 70 | ||||
-rw-r--r-- | dhcpv4/option_ips_test.go | 138 | ||||
-rw-r--r-- | dhcpv4/option_ntp_servers.go | 38 | ||||
-rw-r--r-- | dhcpv4/option_ntp_servers_test.go | 50 | ||||
-rw-r--r-- | dhcpv4/option_router_test.go | 54 |
7 files changed, 203 insertions, 237 deletions
diff --git a/dhcpv4/option_domain_name_server.go b/dhcpv4/option_domain_name_server.go deleted file mode 100644 index a8a679f..0000000 --- a/dhcpv4/option_domain_name_server.go +++ /dev/null @@ -1,40 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "net" -) - -// This option implements the domain name server option -// https://tools.ietf.org/html/rfc2132 - -// OptDomainNameServer represents an option encapsulating the domain name -// servers. -type OptDomainNameServer struct { - NameServers []net.IP -} - -// ParseOptDomainNameServer returns a new OptDomainNameServer from a byte -// stream, or error if any. -func ParseOptDomainNameServer(data []byte) (*OptDomainNameServer, error) { - ips, err := ParseIPs(data) - if err != nil { - return nil, err - } - return &OptDomainNameServer{NameServers: ips}, nil -} - -// Code returns the option code. -func (o *OptDomainNameServer) Code() OptionCode { - return OptionDomainNameServer -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptDomainNameServer) ToBytes() []byte { - return IPsToBytes(o.NameServers) -} - -// String returns a human-readable string. -func (o *OptDomainNameServer) String() string { - return fmt.Sprintf("Domain Name Servers -> %s", IPsToString(o.NameServers)) -} diff --git a/dhcpv4/option_domain_name_server_test.go b/dhcpv4/option_domain_name_server_test.go deleted file mode 100644 index 407e0d6..0000000 --- a/dhcpv4/option_domain_name_server_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptDomainNameServerInterfaceMethods(t *testing.T) { - servers := []net.IP{ - net.IPv4(192, 168, 0, 10), - net.IPv4(192, 168, 0, 20), - } - o := OptDomainNameServer{NameServers: servers} - require.Equal(t, OptionDomainNameServer, o.Code(), "Code") - require.Equal(t, servers, o.NameServers, "NameServers") -} - -func TestParseOptDomainNameServer(t *testing.T) { - data := []byte{ - byte(OptionDomainNameServer), - 8, // Length - 192, 168, 0, 10, // DNS #1 - 192, 168, 0, 20, // DNS #2 - } - o, err := ParseOptDomainNameServer(data[2:]) - require.NoError(t, err) - servers := []net.IP{ - net.IP{192, 168, 0, 10}, - net.IP{192, 168, 0, 20}, - } - require.Equal(t, &OptDomainNameServer{NameServers: servers}, o) - - // Bad length - data = []byte{1, 1, 1} - _, err = ParseOptDomainNameServer(data) - require.Error(t, err, "should get error from bad length") -} - -func TestParseOptDomainNameServerNoServers(t *testing.T) { - // RFC2132 requires that at least one DNS server IP is specified - _, err := ParseOptDomainNameServer([]byte{}) - require.Error(t, err) -} - -func TestOptDomainNameServerString(t *testing.T) { - o := OptDomainNameServer{NameServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}} - require.Equal(t, "Domain Name Servers -> 192.168.0.1, 192.168.0.10", o.String()) -} diff --git a/dhcpv4/option_router.go b/dhcpv4/option_ips.go index a082414..a14a68c 100644 --- a/dhcpv4/option_router.go +++ b/dhcpv4/option_ips.go @@ -11,11 +11,6 @@ import ( // This option implements the router option // https://tools.ietf.org/html/rfc2132 -// OptRouter represents an option encapsulating the routers. -type OptRouter struct { - Routers []net.IP -} - // ParseIPs parses an IPv4 address from a DHCP packet as used and specified by // options in RFC 2132, Sections 3.5 through 3.13, 8.2, 8.3, 8.5, 8.6, 8.9, and // 8.10. @@ -53,6 +48,11 @@ func IPsToString(i []net.IP) string { return strings.Join(s, ", ") } +// OptRouter represents an option encapsulating the routers. +type OptRouter struct { + Routers []net.IP +} + // ParseOptRouter returns a new OptRouter from a byte stream, or error if any. func ParseOptRouter(data []byte) (*OptRouter, error) { ips, err := ParseIPs(data) @@ -76,3 +76,63 @@ func (o *OptRouter) ToBytes() []byte { func (o *OptRouter) String() string { return fmt.Sprintf("Routers -> %s", IPsToString(o.Routers)) } + +// OptNTPServers represents an option encapsulating the NTP servers. +type OptNTPServers struct { + NTPServers []net.IP +} + +// ParseOptNTPServers returns a new OptNTPServers from a byte stream, or error if any. +func ParseOptNTPServers(data []byte) (*OptNTPServers, error) { + ips, err := ParseIPs(data) + if err != nil { + return nil, err + } + return &OptNTPServers{NTPServers: ips}, nil +} + +// Code returns the option code. +func (o *OptNTPServers) Code() OptionCode { + return OptionNTPServers +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptNTPServers) ToBytes() []byte { + return IPsToBytes(o.NTPServers) +} + +// String returns a human-readable string. +func (o *OptNTPServers) String() string { + return fmt.Sprintf("NTP Servers -> %v", IPsToString(o.NTPServers)) +} + +// OptDomainNameServer represents an option encapsulating the domain name +// servers. +type OptDomainNameServer struct { + NameServers []net.IP +} + +// ParseOptDomainNameServer returns a new OptDomainNameServer from a byte +// stream, or error if any. +func ParseOptDomainNameServer(data []byte) (*OptDomainNameServer, error) { + ips, err := ParseIPs(data) + if err != nil { + return nil, err + } + return &OptDomainNameServer{NameServers: ips}, nil +} + +// Code returns the option code. +func (o *OptDomainNameServer) Code() OptionCode { + return OptionDomainNameServer +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptDomainNameServer) ToBytes() []byte { + return IPsToBytes(o.NameServers) +} + +// String returns a human-readable string. +func (o *OptDomainNameServer) String() string { + return fmt.Sprintf("Domain Name Servers -> %s", IPsToString(o.NameServers)) +} diff --git a/dhcpv4/option_ips_test.go b/dhcpv4/option_ips_test.go new file mode 100644 index 0000000..5efd537 --- /dev/null +++ b/dhcpv4/option_ips_test.go @@ -0,0 +1,138 @@ +package dhcpv4 + +import ( + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptRoutersInterfaceMethods(t *testing.T) { + routers := []net.IP{ + net.IPv4(192, 168, 0, 10), + net.IPv4(192, 168, 0, 20), + } + o := OptRouter{Routers: routers} + require.Equal(t, OptionRouter, o.Code(), "Code") + require.Equal(t, routers, o.Routers, "Routers") +} + +func TestParseOptRouter(t *testing.T) { + data := []byte{ + byte(OptionRouter), + 8, // Length + 192, 168, 0, 10, // Router #1 + 192, 168, 0, 20, // Router #2 + } + o, err := ParseOptRouter(data[2:]) + require.NoError(t, err) + routers := []net.IP{ + net.IP{192, 168, 0, 10}, + net.IP{192, 168, 0, 20}, + } + require.Equal(t, &OptRouter{Routers: routers}, o) + + // Short byte stream + data = []byte{byte(OptionRouter)} + _, err = ParseOptRouter(data) + require.Error(t, err, "should get error from short byte stream") +} + +func TestParseOptRouterNoRouters(t *testing.T) { + // RFC2132 requires that at least one Router IP is specified + data := []byte{ + byte(OptionRouter), + 0, // Length + } + _, err := ParseOptRouter(data) + require.Error(t, err) +} + +func TestOptRouterString(t *testing.T) { + o := OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}}} + require.Equal(t, "Routers -> 192.168.0.1, 192.168.0.10", o.String()) +} + +func TestOptDomainNameServerInterfaceMethods(t *testing.T) { + servers := []net.IP{ + net.IPv4(192, 168, 0, 10), + net.IPv4(192, 168, 0, 20), + } + o := OptDomainNameServer{NameServers: servers} + require.Equal(t, OptionDomainNameServer, o.Code(), "Code") + require.Equal(t, servers, o.NameServers, "NameServers") +} + +func TestParseOptDomainNameServer(t *testing.T) { + data := []byte{ + byte(OptionDomainNameServer), + 8, // Length + 192, 168, 0, 10, // DNS #1 + 192, 168, 0, 20, // DNS #2 + } + o, err := ParseOptDomainNameServer(data[2:]) + require.NoError(t, err) + servers := []net.IP{ + net.IP{192, 168, 0, 10}, + net.IP{192, 168, 0, 20}, + } + require.Equal(t, &OptDomainNameServer{NameServers: servers}, o) + + // Bad length + data = []byte{1, 1, 1} + _, err = ParseOptDomainNameServer(data) + require.Error(t, err, "should get error from bad length") +} + +func TestParseOptDomainNameServerNoServers(t *testing.T) { + // RFC2132 requires that at least one DNS server IP is specified + _, err := ParseOptDomainNameServer([]byte{}) + require.Error(t, err) +} + +func TestOptDomainNameServerString(t *testing.T) { + o := OptDomainNameServer{NameServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}} + require.Equal(t, "Domain Name Servers -> 192.168.0.1, 192.168.0.10", o.String()) +} + +func TestOptNTPServersInterfaceMethods(t *testing.T) { + ntpServers := []net.IP{ + net.IPv4(192, 168, 0, 10), + net.IPv4(192, 168, 0, 20), + } + o := OptNTPServers{NTPServers: ntpServers} + require.Equal(t, OptionNTPServers, o.Code(), "Code") + require.Equal(t, ntpServers, o.NTPServers, "NTPServers") +} + +func TestParseOptNTPServers(t *testing.T) { + data := []byte{ + byte(OptionNTPServers), + 8, // Length + 192, 168, 0, 10, // NTP server #1 + 192, 168, 0, 20, // NTP server #2 + } + o, err := ParseOptNTPServers(data[2:]) + require.NoError(t, err) + ntpServers := []net.IP{ + net.IP{192, 168, 0, 10}, + net.IP{192, 168, 0, 20}, + } + require.Equal(t, &OptNTPServers{NTPServers: ntpServers}, o) + + // Bad length + data = []byte{1, 1, 1} + _, err = ParseOptNTPServers(data) + require.Error(t, err, "should get error from bad length") +} + +func TestParseOptNTPserversNoNTPServers(t *testing.T) { + // RFC2132 requires that at least one NTP server IP is specified + _, err := ParseOptNTPServers([]byte{}) + require.Error(t, err) +} + +func TestOptNTPServersString(t *testing.T) { + o := OptNTPServers{NTPServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}} + require.Equal(t, "NTP Servers -> 192.168.0.1, 192.168.0.10", o.String()) +} diff --git a/dhcpv4/option_ntp_servers.go b/dhcpv4/option_ntp_servers.go deleted file mode 100644 index 1c74e13..0000000 --- a/dhcpv4/option_ntp_servers.go +++ /dev/null @@ -1,38 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "net" -) - -// This option implements the network time protocol servers option -// https://tools.ietf.org/html/rfc2132 - -// OptNTPServers represents an option encapsulating the NTP servers. -type OptNTPServers struct { - NTPServers []net.IP -} - -// ParseOptNTPServers returns a new OptNTPServers from a byte stream, or error if any. -func ParseOptNTPServers(data []byte) (*OptNTPServers, error) { - ips, err := ParseIPs(data) - if err != nil { - return nil, err - } - return &OptNTPServers{NTPServers: ips}, nil -} - -// Code returns the option code. -func (o *OptNTPServers) Code() OptionCode { - return OptionNTPServers -} - -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptNTPServers) ToBytes() []byte { - return IPsToBytes(o.NTPServers) -} - -// String returns a human-readable string. -func (o *OptNTPServers) String() string { - return fmt.Sprintf("NTP Servers -> %v", IPsToString(o.NTPServers)) -} diff --git a/dhcpv4/option_ntp_servers_test.go b/dhcpv4/option_ntp_servers_test.go deleted file mode 100644 index d73cd24..0000000 --- a/dhcpv4/option_ntp_servers_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptNTPServersInterfaceMethods(t *testing.T) { - ntpServers := []net.IP{ - net.IPv4(192, 168, 0, 10), - net.IPv4(192, 168, 0, 20), - } - o := OptNTPServers{NTPServers: ntpServers} - require.Equal(t, OptionNTPServers, o.Code(), "Code") - require.Equal(t, ntpServers, o.NTPServers, "NTPServers") -} - -func TestParseOptNTPServers(t *testing.T) { - data := []byte{ - byte(OptionNTPServers), - 8, // Length - 192, 168, 0, 10, // NTP server #1 - 192, 168, 0, 20, // NTP server #2 - } - o, err := ParseOptNTPServers(data[2:]) - require.NoError(t, err) - ntpServers := []net.IP{ - net.IP{192, 168, 0, 10}, - net.IP{192, 168, 0, 20}, - } - require.Equal(t, &OptNTPServers{NTPServers: ntpServers}, o) - - // Bad length - data = []byte{1, 1, 1} - _, err = ParseOptNTPServers(data) - require.Error(t, err, "should get error from bad length") -} - -func TestParseOptNTPserversNoNTPServers(t *testing.T) { - // RFC2132 requires that at least one NTP server IP is specified - _, err := ParseOptNTPServers([]byte{}) - require.Error(t, err) -} - -func TestOptNTPServersString(t *testing.T) { - o := OptNTPServers{NTPServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}} - require.Equal(t, "NTP Servers -> 192.168.0.1, 192.168.0.10", o.String()) -} diff --git a/dhcpv4/option_router_test.go b/dhcpv4/option_router_test.go deleted file mode 100644 index 0f23858..0000000 --- a/dhcpv4/option_router_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package dhcpv4 - -import ( - "net" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOptRoutersInterfaceMethods(t *testing.T) { - routers := []net.IP{ - net.IPv4(192, 168, 0, 10), - net.IPv4(192, 168, 0, 20), - } - o := OptRouter{Routers: routers} - require.Equal(t, OptionRouter, o.Code(), "Code") - require.Equal(t, routers, o.Routers, "Routers") -} - -func TestParseOptRouter(t *testing.T) { - data := []byte{ - byte(OptionRouter), - 8, // Length - 192, 168, 0, 10, // Router #1 - 192, 168, 0, 20, // Router #2 - } - o, err := ParseOptRouter(data[2:]) - require.NoError(t, err) - routers := []net.IP{ - net.IP{192, 168, 0, 10}, - net.IP{192, 168, 0, 20}, - } - require.Equal(t, &OptRouter{Routers: routers}, o) - - // Short byte stream - data = []byte{byte(OptionRouter)} - _, err = ParseOptRouter(data) - require.Error(t, err, "should get error from short byte stream") -} - -func TestParseOptRouterNoRouters(t *testing.T) { - // RFC2132 requires that at least one Router IP is specified - data := []byte{ - byte(OptionRouter), - 0, // Length - } - _, err := ParseOptRouter(data) - require.Error(t, err) -} - -func TestOptRouterString(t *testing.T) { - o := OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}}} - require.Equal(t, "Routers -> 192.168.0.1, 192.168.0.10", o.String()) -} |