-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoding.go
373 lines (319 loc) · 9.16 KB
/
encoding.go
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package pcp
import (
"encoding/binary"
"net"
"github.com/boljen/go-bitmap"
log "github.com/sirupsen/logrus"
)
const (
ProtocolAll Protocol = 0
ProtocolTCP Protocol = 6
ProtocolUDP Protocol = 17
)
type OpCode uint8
type OptionOpCode uint8
//At a later date it would be worth integrating with the IANA package to be fully compliant.
//For now, just implement common protocols
type Protocol uint8
type ResultCode uint8
type OpDataMap struct {
Protocol Protocol
InternalPort uint16
ExternalPort uint16 //This is only a suggestion in request. Server ultimately decides.
ExternalIP net.IP //Also only a suggestion
}
type OpDataPeer struct {
OpDataMap
RemotePort uint16
RemoteIP net.IP
}
//Potentially add in progress bool
type RefreshTime struct {
Attempt int
Time int64
}
type PortMap struct {
OpDataMap
Active bool
Lifetime uint32
Refresh RefreshTime
}
type PeerMap struct {
PortMap
RemotePort uint16
RemoteIP net.IP
}
func (o OpCode) String() string {
return [...]string{"OpAnnounce", "OpMap", "OpPeer"}[o]
}
func (o OptionOpCode) String() string {
return [...]string{"OptionOpReserved", "OptionOpThirdParty", "OptionOpPreferFailure", "OptionOpFilter", "OptionOpNonce", "OptionOpAuthenticationTag", "OptionOpPaAuthenticationTag", "OptionOpEapPayload", "OptionOpPrf", "OptionOpMacAlgorithm", "OptionOpSessionLifetime", "OptionOpReceivedPak", "OptionOpIdIndicator", "OptionOpThirdPartyId"}[o]
}
func (p Protocol) String() string {
return [...]string{"ProtocolAll", "ProtocolTCP", "ProtocolUDP"}[p]
}
func (r ResultCode) String() string {
return [...]string{"ResultSuccess", "ResultUnsupportedVersion", "ResultNotAuthorised", "ResultMalformedRequest", "ResultUnsupportedOpcode", "ResultUnsupportedOption", "ResultMalformedOption", "ResultNetworkFailure", "ResultNoResources", "ResultUnsupportedProtocol", "ResultUserExceededQuota", "ResultCannotProvideExternal", "ResultAddressMismatch", "ResultExcessiveRemotePeers"}[r]
}
const (
DefaultLifetimeSeconds = 3600
)
const (
OpAnnounce OpCode = iota
OpMap
OpPeer
)
const (
OptionOpReserved OptionOpCode = iota
OptionOpThirdParty
OptionOpPreferFailure
OptionOpFilter
OptionOpNonce
OptionOpAuthenticationTag
OptionOpPaAuthenticationTag
OptionOpEapPayload
OptionOpPrf
OptionOpMacAlgorithm
OptionOpSessionLifetime
OptionOpReceivedPak
OptionOpIdIndicator
OptionOpThirdPartyId
//Currently not implementing 128+
)
const (
ResultSuccess ResultCode = iota
ResultUnsupportedVersion
ResultNotAuthorised
ResultMalformedRequest
ResultUnsupportedOpcode
ResultUnsupportedOption
ResultMalformedOption
ResultNetworkFailure
ResultNoResources
ResultUnsupportedProtocol
ResultUserExceededQuota
ResultCannotProvideExternal
ResultAddressMismatch
ResultExcessiveRemotePeers
)
type PCPOption struct {
opCode OptionOpCode
data []byte
}
type RequestPacket struct {
opCode OpCode
lifetime uint32
clientAddr net.IP
opData []byte
pcpOptions []PCPOption
}
type ResponsePacket struct {
opCode OpCode
resultCode ResultCode
lifetime uint32
epoch uint32
opData []byte
pcpOptions []PCPOption
}
func (data *OpDataMap) marshal(nonce []byte) (msg []byte, err error) {
//Potentially relax requirement for non-zero. Appears to be valid in the spec when combined with ProtocolAll.
//Also, marshal is probably the wrong place for this, since it technically doesn't cause an error.
if data.InternalPort == 0 {
return nil, ErrPortNotSpecified
}
empty := make([]byte, 3)
internalPortBytes := make([]byte, 2)
binary.BigEndian.PutUint16(internalPortBytes, data.InternalPort)
externalPortBytes := make([]byte, 2)
binary.BigEndian.PutUint16(externalPortBytes, data.ExternalPort)
addr := make([]byte, 16)
if data.ExternalIP != nil {
copy(addr, data.ExternalIP)
}
var slices = [][]byte{
nonce,
[]byte{byte(data.Protocol)},
empty,
internalPortBytes,
externalPortBytes,
addr,
}
msg = concatCopyPreAllocate(slices)
return
}
func (data *OpDataMap) unmarshal(msg []byte) (err error) {
data = &OpDataMap{
Protocol: Protocol(msg[12]),
InternalPort: binary.BigEndian.Uint16(msg[16:18]),
ExternalPort: binary.BigEndian.Uint16(msg[18:20]),
ExternalIP: net.IP(msg[20:36]),
}
return
}
func (data *OpDataPeer) marshal(nonce []byte) (msg []byte, err error) {
//Potentially relax requirement for non-zero. Appears to be valid in the spec when combined with ProtocolAll.
//Also, marshal is probably the wrong place for this, since it technically doesn't cause an error.
if data.InternalPort == 0 {
return nil, ErrPortNotSpecified
}
r1, r2 := make([]byte, 3), make([]byte, 2)
internalPortBytes, externalPortBytes, remotePortBytes := make([]byte, 2), make([]byte, 2), make([]byte, 2)
binary.BigEndian.PutUint16(internalPortBytes, data.InternalPort)
binary.BigEndian.PutUint16(externalPortBytes, data.ExternalPort)
binary.BigEndian.PutUint16(remotePortBytes, data.RemotePort)
addr, remoteAddr := make([]byte, 16), make([]byte, 16)
if data.ExternalIP != nil {
copy(addr, data.ExternalIP)
}
if data.RemoteIP == nil {
return nil, ErrNoAddress
}
copy(remoteAddr, data.RemoteIP)
var slices = [][]byte{
nonce,
[]byte{byte(data.Protocol)},
r1,
internalPortBytes,
externalPortBytes,
addr,
remotePortBytes,
r2,
remoteAddr,
}
msg = concatCopyPreAllocate(slices)
return
}
func (data *OpDataPeer) unmarshal(msg []byte) (err error) {
data = &OpDataPeer{
OpDataMap: OpDataMap{
Protocol: Protocol(msg[12]),
InternalPort: binary.BigEndian.Uint16(msg[16:18]),
ExternalPort: binary.BigEndian.Uint16(msg[18:20]),
ExternalIP: net.IP(msg[20:36]),
},
RemotePort: binary.BigEndian.Uint16(msg[36:38]),
RemoteIP: net.IP(msg[40:56]),
}
return
}
func (req *RequestPacket) marshal() (msg []byte, err error) {
opMap := bitmap.NewSlice(8)
//Bits at indexes 0-6 set from opCode int.
for i := 0; i < 7; i++ {
opCodeBit := bitmap.GetBit(byte(req.opCode), i)
bitmap.Set(opMap, i, opCodeBit)
}
//Bit at index 7 is 0 as it is a request
bitmap.Set(opMap, 7, false)
empty := make([]byte, 2)
lifetimeBytes := make([]byte, 4)
binary.BigEndian.PutUint32(lifetimeBytes, req.lifetime)
addr := make([]byte, 16)
log.Debugf("Client addr: %s\n", req.clientAddr)
copy(addr, req.clientAddr)
var options []byte
log.Debugf("Number of options in request: %d", len(req.pcpOptions))
for _, option := range req.pcpOptions {
//8 bits reserved
empty := make([]byte, 1)
optionSlices := [][]byte{
[]byte{byte(option.opCode)},
empty,
//length of option data payload
[]byte{byte(len(option.data))},
option.data,
}
optionBytes := concatCopyPreAllocate(optionSlices)
//Pad option data to multiple of 4
optionBytes = addPadding(optionBytes)
options = append(options, optionBytes...)
}
var slices = [][]byte{
//The current PCP version number
[]byte{2},
opMap,
//Next 2 bytes (16 bits) reserved
empty,
//lifetime is an unsigned 32 bit integer in seconds
lifetimeBytes,
//client ip is always a 16 byte (128 bit) block
addr,
//opData is the opcode-specific data
req.opData,
options,
}
msg = concatCopyPreAllocate(slices)
//Pad message to multiple of 4
msg = addPadding(msg)
if len(msg) > 1100 {
return nil, ErrPacketTooLarge
}
log.Debugf("Request Bytes: %x\n", msg)
return msg, nil
}
func (res *ResponsePacket) unmarshal(data []byte) (err error) {
log.Debugf("Response Bytes: %x\n", data)
version := uint8(data[0])
if version != 2 {
return ErrUnsupportedVersion
}
if !bitmap.GetBit(data[1], 7) {
return ErrWrongPacketType
}
var opCode byte
for i := 0; i < 7; i++ {
opCodeBit := bitmap.GetBit(data[1], i)
opCode = bitmap.SetBit(opCode, i, opCodeBit)
}
res.opCode = OpCode(opCode)
res.resultCode = ResultCode(data[3])
log.Debugf("Result Code: %s", res.resultCode)
res.lifetime = binary.BigEndian.Uint32(data[4:8])
log.Debugf("Response Lifetime: %d", res.lifetime)
res.epoch = binary.BigEndian.Uint32(data[8:12])
log.Debugf("Response Epoch: %d", res.epoch)
var opDataLen int
//This could be trimmed down - left for clarity.
switch res.opCode {
case OpAnnounce:
opDataLen = 0
case OpMap:
opDataLen = 36
case OpPeer:
opDataLen = 56
default:
opDataLen = 0
}
log.Debugf("Opcode: %s\n", res.opCode)
log.Debugf("Op data len: %d\n", opDataLen)
if opDataLen > 0 {
res.opData = data[24 : 24+opDataLen]
}
currentOffset := 24 + opDataLen
for currentOffset < len(data) {
log.Debugf("Current offset: %d\n", currentOffset)
log.Debugf("Remaining data: %x\n", data[currentOffset:])
opCode := OptionOpCode(data[currentOffset])
log.Debugf("Option OpCode: %s\n", opCode)
optionLengthBytes := make([]byte, 2)
copy(optionLengthBytes, data[currentOffset+2:currentOffset+3])
optionLength := binary.BigEndian.Uint16(optionLengthBytes)
log.Debugf("Option data length: %d", optionLength)
var optionData []byte
dataStart := currentOffset + 3
if optionLength > 0 {
dataEnd := dataStart + int(optionLength)
optionData = data[dataStart:dataEnd]
currentOffset = dataEnd
} else {
currentOffset = dataStart
}
//OpCode 0 is reserved, has no function, hence dropped due to possible
//entries from reading empty bytes
if opCode != 0 {
option := PCPOption{opCode, optionData}
res.pcpOptions = append(res.pcpOptions, option)
}
}
return
}