summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dhcpv4/option_domain_name_server.go72
-rw-r--r--dhcpv4/option_domain_name_server_test.go65
-rw-r--r--dhcpv4/options.go2
3 files changed, 139 insertions, 0 deletions
diff --git a/dhcpv4/option_domain_name_server.go b/dhcpv4/option_domain_name_server.go
new file mode 100644
index 0000000..78aaf90
--- /dev/null
+++ b/dhcpv4/option_domain_name_server.go
@@ -0,0 +1,72 @@
+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) {
+ if len(data) < 2 {
+ return nil, ErrShortByteStream
+ }
+ code := OptionCode(data[0])
+ if code != OptionDomainNameServer {
+ return nil, fmt.Errorf("expected code %v, got %v", OptionDomainNameServer, code)
+ }
+ length := int(data[1])
+ if length == 0 || length%4 != 0 {
+ return nil, fmt.Errorf("Invalid length: expected multiple of 4 larger than 4, got %v", length)
+ }
+ if len(data) < 2+length {
+ return nil, ErrShortByteStream
+ }
+ nameservers := make([]net.IP, 0, length%4)
+ for idx := 0; idx < length; idx += 4 {
+ b := data[2+idx : 2+idx+4]
+ nameservers = append(nameservers, net.IPv4(b[0], b[1], b[2], b[3]))
+ }
+ return &OptDomainNameServer{NameServers: nameservers}, 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 {
+ ret := []byte{byte(o.Code()), byte(o.Length())}
+ for _, ns := range o.NameServers {
+ ret = append(ret, ns...)
+ }
+ return ret
+}
+
+// String returns a human-readable string.
+func (o *OptDomainNameServer) String() string {
+ var servers string
+ for idx, ns := range o.NameServers {
+ servers += ns.String()
+ if idx < len(o.NameServers)-1 {
+ servers += ", "
+ }
+ }
+ return fmt.Sprintf("Domain Name Servers -> %v", servers)
+}
+
+// Length returns the length of the data portion (excluding option code an byte
+// length).
+func (o *OptDomainNameServer) Length() int {
+ return len(o.NameServers) * 4
+}
diff --git a/dhcpv4/option_domain_name_server_test.go b/dhcpv4/option_domain_name_server_test.go
new file mode 100644
index 0000000..c801cb6
--- /dev/null
+++ b/dhcpv4/option_domain_name_server_test.go
@@ -0,0 +1,65 @@
+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, net.IPv4len*len(servers), o.Length(), "Length")
+ 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)
+ require.NoError(t, err)
+ servers := []net.IP{
+ net.IPv4(192, 168, 0, 10),
+ net.IPv4(192, 168, 0, 20),
+ }
+ require.Equal(t, &OptDomainNameServer{NameServers: servers}, o)
+
+ // Short byte stream
+ data = []byte{byte(OptionDomainNameServer)}
+ _, err = ParseOptDomainNameServer(data)
+ require.Error(t, err, "should get error from short byte stream")
+
+ // Wrong code
+ data = []byte{54, 2, 1, 1}
+ _, err = ParseOptDomainNameServer(data)
+ require.Error(t, err, "should get error from wrong code")
+
+ // Bad length
+ data = []byte{byte(OptionDomainNameServer), 6, 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
+ data := []byte{
+ byte(OptionDomainNameServer),
+ 0, // Length
+ }
+ _, err := ParseOptDomainNameServer(data)
+ 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/options.go b/dhcpv4/options.go
index 528c175..2bd26a5 100644
--- a/dhcpv4/options.go
+++ b/dhcpv4/options.go
@@ -56,6 +56,8 @@ func ParseOption(data []byte) (Option, error) {
opt, err = ParseOptClassIdentifier(data)
case OptionDomainName:
opt, err = ParseOptDomainName(data)
+ case OptionDomainNameServer:
+ opt, err = ParseOptDomainNameServer(data)
case OptionVendorIdentifyingVendorClass:
opt, err = ParseOptVIVC(data)
default: