summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/modifiers.go
blob: 96fbf4b617e3123f8962e11f8842e4032460a12c (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package dhcpv6

import (
	"net"

	"github.com/insomniacslk/dhcp/iana"
	"github.com/insomniacslk/dhcp/rfc1035label"
)

// WithOption adds the specific option to the DHCPv6 message.
func WithOption(o Option) Modifier {
	return func(d DHCPv6) {
		d.UpdateOption(o)
	}
}

// WithClientID adds a client ID option to a DHCPv6 packet
func WithClientID(duid Duid) Modifier {
	return WithOption(OptClientID(duid))
}

// WithServerID adds a client ID option to a DHCPv6 packet
func WithServerID(duid Duid) Modifier {
	return WithOption(OptServerID(duid))
}

// WithNetboot adds bootfile URL and bootfile param options to a DHCPv6 packet.
func WithNetboot(d DHCPv6) {
	WithRequestedOptions(OptionBootfileURL, OptionBootfileParam)(d)
}

// WithFQDN adds a fully qualified domain name option to the packet
func WithFQDN(flags uint8, domainname string) Modifier {
	return func(d DHCPv6) {
		d.UpdateOption(&OptFQDN{
			Flags: flags,
			DomainName: &rfc1035label.Labels{
				Labels: []string{domainname},
			},
		})
	}
}

// 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) {
		ouc := OptUserClass{UserClasses: [][]byte{uc}}
		d.AddOption(&ouc)
	}
}

// WithArchType adds an arch type option to the packet
func WithArchType(at iana.Arch) Modifier {
	return func(d DHCPv6) {
		d.AddOption(OptClientArchType(at))
	}
}

// WithIANA adds or updates an OptIANA option with the provided IAAddress
// options
func WithIANA(addrs ...OptIAAddress) Modifier {
	return func(d DHCPv6) {
		if msg, ok := d.(*Message); ok {
			iana := msg.Options.OneIANA()
			if iana == nil {
				iana = &OptIANA{}
			}
			for _, addr := range addrs {
				iana.Options.Add(&addr)
			}
			msg.UpdateOption(iana)
		}
	}
}

// WithIAID updates an OptIANA option with the provided IAID
func WithIAID(iaid [4]byte) Modifier {
	return func(d DHCPv6) {
		if msg, ok := d.(*Message); ok {
			iana := msg.Options.OneIANA()
			if iana == nil {
				iana = &OptIANA{
					Options: IdentityOptions{Options: []Option{}},
				}
			}
			copy(iana.IaId[:], iaid[:])
			d.UpdateOption(iana)
		}
	}
}

// WithDNS adds or updates an OptDNSRecursiveNameServer
func WithDNS(dnses ...net.IP) Modifier {
	return WithOption(OptDNS(dnses...))
}

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

// WithRapidCommit adds the rapid commit option to a message.
func WithRapidCommit(d DHCPv6) {
	d.UpdateOption(&OptionGeneric{OptionCode: OptionRapidCommit})
}

// WithRequestedOptions adds requested options to the packet
func WithRequestedOptions(codes ...OptionCode) Modifier {
	return func(d DHCPv6) {
		if msg, ok := d.(*Message); ok {
			oro := msg.Options.RequestedOptions()
			for _, c := range codes {
				oro.Add(c)
			}
			d.UpdateOption(OptRequestedOption(oro...))
		}
	}
}

// WithDHCP4oDHCP6Server adds or updates an OptDHCP4oDHCP6Server
func WithDHCP4oDHCP6Server(addrs ...net.IP) Modifier {
	return func(d DHCPv6) {
		opt := OptDHCP4oDHCP6Server{
			DHCP4oDHCP6Servers: addrs,
		}
		d.UpdateOption(&opt)
	}
}

// WithIAPD adds or updates an IAPD option with the provided IAID and
// prefix options to a DHCPv6 packet.
func WithIAPD(iaid [4]byte, prefixes ...*OptIAPrefix) Modifier {
	return func(d DHCPv6) {
		if msg, ok := d.(*Message); ok {
			opt := msg.Options.OneIAPD()
			if opt == nil {
				opt = &OptIAPD{}
			}
			copy(opt.IaId[:], iaid[:])

			for _, prefix := range prefixes {
				opt.Options.Add(prefix)
			}
			d.UpdateOption(opt)
		}
	}
}