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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package dhcpv6
import (
"errors"
"fmt"
"net"
)
const RelayHeaderSize = 34
type DHCPv6Relay struct {
messageType MessageType
hopCount uint8
linkAddr net.IP
peerAddr net.IP
options []Option
}
func (r *DHCPv6Relay) Type() MessageType {
return r.messageType
}
func (r *DHCPv6Relay) MessageTypeToString() string {
return MessageTypeToString(r.messageType)
}
func (r *DHCPv6Relay) String() string {
ret := fmt.Sprintf(
"DHCPv6Relay(messageType=%v hopcount=%v, linkaddr=%v, peeraddr=%v, %d options)",
r.MessageTypeToString(), r.hopCount, r.linkAddr, r.peerAddr, len(r.options),
)
return ret
}
func (r *DHCPv6Relay) Summary() string {
ret := fmt.Sprintf(
"DHCPv6Relay\n"+
" messageType=%v\n"+
" hopcount=%v\n"+
" linkaddr=%v\n"+
" peeraddr=%v\n"+
" options=%v\n",
r.MessageTypeToString(),
r.hopCount,
r.linkAddr,
r.peerAddr,
r.options,
)
return ret
}
func (r *DHCPv6Relay) ToBytes() []byte {
ret := make([]byte, RelayHeaderSize)
ret[0] = byte(r.messageType)
ret[1] = byte(r.hopCount)
copy(ret[2:18], r.linkAddr)
copy(ret[18:34], r.peerAddr)
for _, opt := range r.options {
ret = append(ret, opt.ToBytes()...)
}
return ret
}
func (r *DHCPv6Relay) MessageType() MessageType {
return r.messageType
}
func (r *DHCPv6Relay) SetMessageType(messageType MessageType) {
// not enforcing if message type is not a RELAY_FORW or a RELAY_REPL message
r.messageType = messageType
}
func (r *DHCPv6Relay) HopCount() uint8 {
return r.hopCount
}
func (r *DHCPv6Relay) SetHopCount(hopCount uint8) {
r.hopCount = hopCount
}
func (r *DHCPv6Relay) LinkAddr() net.IP {
return r.linkAddr
}
func (r *DHCPv6Relay) SetLinkAddr(linkAddr net.IP) {
r.linkAddr = linkAddr
}
func (r *DHCPv6Relay) PeerAddr() net.IP {
return r.peerAddr
}
func (r *DHCPv6Relay) SetPeerAddr(peerAddr net.IP) {
r.peerAddr = peerAddr
}
func (r *DHCPv6Relay) Length() int {
mLen := RelayHeaderSize
for _, opt := range r.options {
mLen += opt.Length() + 4 // +4 for opt code and opt len
}
return mLen
}
func (r *DHCPv6Relay) Options() []Option {
return r.options
}
func (r *DHCPv6Relay) GetOption(code OptionCode) []Option {
return getOptions(r.options, code, false)
}
func (r *DHCPv6Relay) GetOneOption(code OptionCode) Option {
return getOption(r.options, code)
}
func (r *DHCPv6Relay) SetOptions(options []Option) {
r.options = options
}
func (r *DHCPv6Relay) AddOption(option Option) {
r.options = append(r.options, option)
}
// UpdateOption replaces the first option of the same type as the specified one.
func (r *DHCPv6Relay) UpdateOption(option Option) {
for idx, opt := range r.options {
if opt.Code() == option.Code() {
r.options[idx] = option
// don't look further
return
}
}
// if not found, add it
r.AddOption(option)
}
func (r *DHCPv6Relay) IsRelay() bool {
return true
}
// Recurse into a relay message and extract and return the inner DHCPv6Message.
// Return nil if none found (e.g. not a relay message).
func (d *DHCPv6Relay) GetInnerMessage() (DHCPv6, error) {
var (
p DHCPv6
err error
)
p = d
for {
if !p.IsRelay() {
return p, nil
}
p, err = DecapsulateRelay(p)
if err != nil {
return nil, err
}
}
}
// NewRelayReplFromRelayForw creates a MessageTypeRelayReply based on a
// MessageTypeRelayForward and replaces the inner message with the passed
// DHCPv6 message. It copies the OptionInterfaceID and OptionRemoteID if the
// options are present in the Relay packet.
func NewRelayReplFromRelayForw(relayForw, msg DHCPv6) (DHCPv6, error) {
var (
err error
linkAddr, peerAddr []net.IP
optiid []Option
optrid []Option
)
if relayForw == nil {
return nil, errors.New("Relay message cannot be nil")
}
relay, ok := relayForw.(*DHCPv6Relay)
if !ok {
return nil, errors.New("Not a DHCPv6Relay")
}
if relay.Type() != MessageTypeRelayForward {
return nil, errors.New("The passed packet is not of type MessageTypeRelayForward")
}
if msg == nil {
return nil, errors.New("The passed message cannot be nil")
}
if msg.IsRelay() {
return nil, errors.New("The passed message cannot be a relay")
}
for {
linkAddr = append(linkAddr, relay.LinkAddr())
peerAddr = append(peerAddr, relay.PeerAddr())
optiid = append(optiid, relay.GetOneOption(OptionInterfaceID))
optrid = append(optrid, relay.GetOneOption(OptionRemoteID))
decap, err := DecapsulateRelay(relay)
if err != nil {
return nil, err
}
if decap.IsRelay() {
relay = decap.(*DHCPv6Relay)
} else {
break
}
}
for i := len(linkAddr) - 1; i >= 0; i-- {
msg, err = EncapsulateRelay(msg, MessageTypeRelayReply, linkAddr[i], peerAddr[i])
if err != nil {
return nil, err
}
if opt := optiid[i]; opt != nil {
msg.AddOption(opt)
}
if opt := optrid[i]; opt != nil {
msg.AddOption(opt)
}
}
return msg, nil
}
|