summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
authorAndrea Barberio <insomniac@slackware.it>2018-12-08 19:49:47 +0000
committerinsomniac <insomniacslk@users.noreply.github.com>2018-12-08 20:17:38 +0000
commit4dabb19b0ae8c2c0287807d4454eedee46d39a2d (patch)
tree8feead59557ce224bcfcb858a0448e96b9ad2342 /dhcpv6
parentc1bfa9934849aa7934f14dd1a9e24780530855fc (diff)
Added netconf basic testing and required modifiers
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/modifiers.go50
-rw-r--r--dhcpv6/modifiers_test.go41
-rw-r--r--dhcpv6/option_nontemporaryaddress.go5
-rw-r--r--dhcpv6/option_nontemporaryaddress_test.go7
4 files changed, 103 insertions, 0 deletions
diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go
index cbd2231..d0d3268 100644
--- a/dhcpv6/modifiers.go
+++ b/dhcpv6/modifiers.go
@@ -2,8 +2,10 @@ package dhcpv6
import (
"log"
+ "net"
"github.com/insomniacslk/dhcp/iana"
+ "github.com/insomniacslk/dhcp/rfc1035label"
)
// WithClientID adds a client ID option to a DHCPv6 packet
@@ -63,6 +65,54 @@ func WithArchType(at iana.ArchType) Modifier {
}
}
+// WithIANA adds or updates an OptIANA option with the provided IAAddress
+// options
+func WithIANA(addrs ...OptIAAddress) Modifier {
+ return func(d DHCPv6) DHCPv6 {
+ opt := d.GetOneOption(OptionIANA)
+ if opt == nil {
+ opt = &OptIANA{}
+ }
+ iaNa := opt.(*OptIANA)
+ for _, addr := range addrs {
+ iaNa.AddOption(&addr)
+ }
+ d.UpdateOption(iaNa)
+ return d
+ }
+}
+
+// WithDNS adds or updates an OptDNSRecursiveNameServer
+func WithDNS(dnses ...net.IP) Modifier {
+ return func(d DHCPv6) DHCPv6 {
+ opt := d.GetOneOption(OptionDNSRecursiveNameServer)
+ if opt == nil {
+ opt = &OptDNSRecursiveNameServer{}
+ }
+ odns := opt.(*OptDNSRecursiveNameServer)
+ odns.NameServers = append(odns.NameServers, dnses[:]...)
+ d.UpdateOption(odns)
+ return d
+ }
+}
+
+// WithDomainSearchList adds or updates an OptDomainSearchList
+func WithDomainSearchList(searchlist ...string) Modifier {
+ return func(d DHCPv6) DHCPv6 {
+ opt := d.GetOneOption(OptionDomainSearchList)
+ if opt == nil {
+ opt = &OptDomainSearchList{}
+ }
+ osl := opt.(*OptDomainSearchList)
+ labels := rfc1035label.Labels{
+ Labels: searchlist,
+ }
+ osl.DomainSearchList = &labels
+ d.UpdateOption(osl)
+ return d
+ }
+}
+
// WithRequestedOptions adds requested options to the packet
func WithRequestedOptions(optionCodes ...OptionCode) Modifier {
return func(d DHCPv6) DHCPv6 {
diff --git a/dhcpv6/modifiers_test.go b/dhcpv6/modifiers_test.go
index 9953f8b..6fc6ae7 100644
--- a/dhcpv6/modifiers_test.go
+++ b/dhcpv6/modifiers_test.go
@@ -1,6 +1,7 @@
package dhcpv6
import (
+ "log"
"net"
"testing"
@@ -51,3 +52,43 @@ func TestWithRequestedOptions(t *testing.T) {
oro = opt.(*OptRequestedOption)
require.ElementsMatch(t, oro.RequestedOptions(), []OptionCode{OptionClientID, OptionServerID})
}
+
+func TestWithIANA(t *testing.T) {
+ d := WithIANA(OptIAAddress{
+ IPv6Addr: net.ParseIP("::1"),
+ PreferredLifetime: 3600,
+ ValidLifetime: 5200,
+ })(&DHCPv6Message{})
+ require.Equal(t, 1, len(d.Options()))
+ require.Equal(t, OptionIANA, d.Options()[0].Code())
+}
+
+func TestWithDNS(t *testing.T) {
+ d := WithDNS([]net.IP{
+ net.ParseIP("fe80::1"),
+ net.ParseIP("fe80::2"),
+ }...)(&DHCPv6Message{})
+ require.Equal(t, 1, len(d.Options()))
+ dns := d.Options()[0].(*OptDNSRecursiveNameServer)
+ log.Printf("DNS %+v", dns)
+ require.Equal(t, OptionDNSRecursiveNameServer, dns.Code())
+ require.Equal(t, 2, len(dns.NameServers))
+ require.Equal(t, net.ParseIP("fe80::1"), dns.NameServers[0])
+ require.Equal(t, net.ParseIP("fe80::2"), dns.NameServers[1])
+ require.NotEqual(t, net.ParseIP("fe80::1"), dns.NameServers[1])
+}
+
+func TestWithDomainSearchList(t *testing.T) {
+ d := WithDomainSearchList([]string{
+ "slackware.it",
+ "dhcp.slackware.it",
+ }...)(&DHCPv6Message{})
+ require.Equal(t, 1, len(d.Options()))
+ osl := d.Options()[0].(*OptDomainSearchList)
+ require.Equal(t, OptionDomainSearchList, osl.Code())
+ require.NotNil(t, osl.DomainSearchList)
+ labels := osl.DomainSearchList.Labels
+ require.Equal(t, 2, len(labels))
+ require.Equal(t, "slackware.it", labels[0])
+ require.Equal(t, "dhcp.slackware.it", labels[1])
+}
diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go
index 62442c1..ec09937 100644
--- a/dhcpv6/option_nontemporaryaddress.go
+++ b/dhcpv6/option_nontemporaryaddress.go
@@ -45,6 +45,11 @@ func (op *OptIANA) String() string {
op.IaId, op.T1, op.T2, op.Options)
}
+// AddOption adds an option at the end of the IA_NA options
+func (op *OptIANA) AddOption(opt Option) {
+ op.Options = append(op.Options, opt)
+}
+
// GetOneOption will get an option of the give type from the Options field, if
// it is present. It will return `nil` otherwise
func (op *OptIANA) GetOneOption(code OptionCode) Option {
diff --git a/dhcpv6/option_nontemporaryaddress_test.go b/dhcpv6/option_nontemporaryaddress_test.go
index 9edf521..a03e0c9 100644
--- a/dhcpv6/option_nontemporaryaddress_test.go
+++ b/dhcpv6/option_nontemporaryaddress_test.go
@@ -51,6 +51,13 @@ func TestOptIANAGetOneOption(t *testing.T) {
require.Equal(t, oaddr, opt.GetOneOption(OptionIAAddr))
}
+func TestOptIANAAddOption(t *testing.T) {
+ opt := OptIANA{}
+ opt.AddOption(&OptElapsedTime{})
+ require.Equal(t, 1, len(opt.Options))
+ require.Equal(t, OptionElapsedTime, opt.Options[0].Code())
+}
+
func TestOptIANAGetOneOptionMissingOpt(t *testing.T) {
oaddr := &OptIAAddress{
IPv6Addr: net.ParseIP("::1"),