summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/modifiers.go
blob: fc6d87b67007e26c53d1f229d97486f51d2c8ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
func WithClientID(duid Duid) Modifier {
	return func(d DHCPv6) DHCPv6 {
		cid := OptClientId{Cid: duid}
		d.UpdateOption(&cid)
		return d
	}
}

// WithServerID adds a client ID option to a DHCPv6 packet
func WithServerID(duid Duid) Modifier {
	return func(d DHCPv6) DHCPv6 {
		sid := OptServerId{Sid: duid}
		d.UpdateOption(&sid)
		return d
	}
}

// WithNetboot adds bootfile URL and bootfile param options to a DHCPv6 packet.
func WithNetboot(d DHCPv6) DHCPv6 {
	msg, ok := d.(*DHCPv6Message)
	if !ok {
		log.Printf("WithNetboot: not a DHCPv6Message")
		return d
	}
	// add OptionBootfileURL and OptionBootfileParam
	opt := msg.GetOneOption(OptionORO)
	if opt == nil {
		opt = &OptRequestedOption{}
	}
	// TODO only add options if they are not there already
	oro := opt.(*OptRequestedOption)
	oro.AddRequestedOption(OptionBootfileURL)
	oro.AddRequestedOption(OptionBootfileParam)
	msg.UpdateOption(oro)
	return d
}

// WithUserClass adds a user class option to the packet
func WithUserClass(uc []byte) Modifier {
	// TODO let the user specify multiple user classes
	return func(d DHCPv6) DHCPv6 {
		ouc := OptUserClass{UserClasses: [][]byte{uc}}
		d.AddOption(&ouc)
		return d
	}
}

// WithArchType adds an arch type option to the packet
func WithArchType(at iana.ArchType) Modifier {
	return func(d DHCPv6) DHCPv6 {
		ao := OptClientArchType{ArchTypes: []iana.ArchType{at}}
		d.AddOption(&ao)
		return d
	}
}

// WithIANA adds or updates an OptIANA option with the provided IAAddress
// options
func WithIANA(addrs ...OptIAAddress) Modifier {
	return func(d DHCPv6) DHCPv6 {
		iaNa := &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 {
		odns := OptDNSRecursiveNameServer{
			NameServers: append([]net.IP{}, dnses[:]...),
		}
		d.UpdateOption(&odns)
		return d
	}
}

// WithDomainSearchList adds or updates an OptDomainSearchList
func WithDomainSearchList(searchlist ...string) Modifier {
	return func(d DHCPv6) DHCPv6 {
		osl := OptDomainSearchList{
			DomainSearchList: &rfc1035label.Labels{
				Labels: searchlist,
			},
		}
		d.UpdateOption(&osl)
		return d
	}
}

// WithRequestedOptions adds requested options to the packet
func WithRequestedOptions(optionCodes ...OptionCode) Modifier {
	return func(d DHCPv6) DHCPv6 {
		opt := d.GetOneOption(OptionORO)
		if opt == nil {
			opt = &OptRequestedOption{}
		}
		oro := opt.(*OptRequestedOption)
		for _, optionCode := range optionCodes {
			oro.AddRequestedOption(optionCode)
		}
		d.UpdateOption(oro)
		return d
	}
}