-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
375 lines (330 loc) · 9.55 KB
/
messages.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
374
375
// Protocol flow
// =============
//
// *Host* *Netpuncher* *Client*
// ([2001:db8::2]:11113) ([2001:db8::1]:11113)
//
// C4NetIOUDP Connect <----------------->
//
// IDReq ------------------------------->
//
// <------------------------------- AssID[1337]
// (announce on master server)
//
// <--------------------------> C4NetIOUDP Connect
//
// <--------------------------- SReq[1337]
//
// <------------------------------- CReq["[2001:db8::1]:11113"]
// CReq["[2001:db8::2]:11113"] -----------> (ignores this message, I think?)
//
// PID_Pong ---------------------------------------------------------------------->
//
// **TCP Connect (IPv6)**
//
// <--------------------------- SReqTCP[1337]
//
// (generates two ports)
// <------------------------------- CReqTCP["[2001:db8::2]:60002",
// "[2001:db8::1]:60001"]
// CReqTCP["[2001:db8::1]:60001",
// "[2001:db8::2]:60002"] -------->
//
// TCP SYN <---------------------------------------------------------------------> TCP SYN (simultaneous open)
//
package netpuncher
import (
"bytes"
"encoding"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
)
const (
PID_Puncher_AssID = 0x51 // Puncher announcing ID to client
PID_Puncher_SReq = 0x52 // Client requesting to be served with punching (for an ID)
PID_Puncher_CReq = 0x53 // Puncher requesting clients to punch (towards an address)
PID_Puncher_IDReq = 0x54 // Client requesting an ID
PID_Puncher_SReqTCP = 0x62 // Client requesting to be served with TCP-punching (for an ID)
PID_Puncher_CReqTCP = 0x63 // Puncher requesting clients to TCP-punch (towards an address)
)
// 2 byte header, CReqTCP is largest (two port and IP)
const MaxPacketSize = 2 + 36
type PuncherPacket interface {
Type() byte
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
// Encountered an unknown message type while decoding.
type ErrUnknownType byte
func (t ErrUnknownType) Error() string {
return fmt.Sprintf("netpuncher: unknown message type 0x%x", byte(t))
}
// Message has an unsupported protocol version
type ErrUnsupportedVersion ProtocolVersion
func (v ErrUnsupportedVersion) Error() string {
return fmt.Sprintf("netpuncher: unsupported protocol version %d", int(v))
}
// Message not properly formatted.
type ErrInvalidMessage string
func (msg ErrInvalidMessage) Error() string {
return fmt.Sprintf("netpuncher: %s", string(msg))
}
// Not read enough bytes for a full message.
type ErrNotReadEnough int
func (n ErrNotReadEnough) Error() string {
return fmt.Sprintf("netpuncher: message not long enough, read %d byte", n)
}
// Reads one puncher message.
func ReadFrom(r io.Reader) (PuncherPacket, error) {
buf := make([]byte, MaxPacketSize)
n, err := r.Read(buf)
if err != nil {
return nil, err
}
if n < 2 {
return nil, ErrNotReadEnough(n)
}
var p PuncherPacket
switch buf[0] {
case PID_Puncher_AssID:
p = &AssID{}
case PID_Puncher_SReq:
p = &SReq{}
case PID_Puncher_CReq:
p = &CReq{}
case PID_Puncher_IDReq:
p = &IDReq{}
case PID_Puncher_SReqTCP:
p = &SReqTCP{}
case PID_Puncher_CReqTCP:
p = &CReqTCP{}
default:
return nil, ErrUnknownType(buf[0])
}
if err = p.UnmarshalBinary(buf); err != nil {
return nil, err
}
return p, nil
}
type ProtocolVersion byte
// Newest version supported
var NewestProtocolVersion = ProtocolVersion(1)
// Returns whether the implementation supports the protocol version.
func (v ProtocolVersion) Supported() bool {
return v == 1
}
// Header preceding all messages.
type Header struct {
Type byte // See PID_Puncher_* constants
Version ProtocolVersion
}
func (h Header) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
binary.Write(&b, binary.LittleEndian, h)
return b.Bytes(), nil
}
func (h *Header) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
err := binary.Read(b, binary.LittleEndian, h)
if err != nil {
return ErrInvalidMessage(err.Error())
}
if !h.Version.Supported() {
return ErrUnsupportedVersion(h.Version)
}
return nil
}
type IDReq struct {
Header
}
func (*IDReq) Type() byte { return PID_Puncher_IDReq }
func (p IDReq) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p)
return b.Bytes(), nil
}
func (p *IDReq) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
err := binary.Read(b, binary.LittleEndian, p)
if err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
return nil
}
type AssID struct {
Header
CID uint32
}
func (*AssID) Type() byte { return PID_Puncher_AssID }
// error is always nil
func (p AssID) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p)
return b.Bytes(), nil
}
func (p *AssID) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
err := binary.Read(b, binary.LittleEndian, p)
if err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
return nil
}
type SReq struct {
Header
CID uint32
}
func (*SReq) Type() byte { return PID_Puncher_SReq }
// error is always nil
func (p SReq) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p)
return b.Bytes(), nil
}
func (p *SReq) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
err := binary.Read(b, binary.LittleEndian, p)
if err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
return nil
}
// Addr is encoded as 16 bit port (little endian) and 16 byte IPv6 address.
type CReq struct {
Header
Addr net.UDPAddr
}
func (*CReq) Type() byte { return PID_Puncher_CReq }
// Fails if Addr is not set
func (p CReq) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p.Header)
binary.Write(&b, binary.LittleEndian, uint16(p.Addr.Port))
v6 := p.Addr.IP.To16()
if v6 == nil {
return nil, errors.New("cannot marshal CReq: Addr.IP nil")
}
binary.Write(&b, binary.LittleEndian, v6)
return b.Bytes(), nil
}
func (p *CReq) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
if err := binary.Read(b, binary.LittleEndian, &p.Header); err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
var port uint16
if err := binary.Read(b, binary.LittleEndian, &port); err != nil {
return ErrInvalidMessage(err.Error())
}
var ip [16]byte
if err := binary.Read(b, binary.LittleEndian, &ip); err != nil {
return ErrInvalidMessage(err.Error())
}
p.Addr = net.UDPAddr{Port: int(port), IP: ip[:]}
return nil
}
type SReqTCP struct {
Header
CID uint32
}
func (*SReqTCP) Type() byte { return PID_Puncher_SReqTCP }
// error is always nil
func (p SReqTCP) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p)
return b.Bytes(), nil
}
func (p *SReqTCP) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
err := binary.Read(b, binary.LittleEndian, p)
if err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
return nil
}
// Addr is encoded as 16 bit TCP port (little endian) and 16 byte IPv6 address.
type CReqTCP struct {
Header
SourceAddr net.TCPAddr
DestAddr net.TCPAddr
}
func (*CReqTCP) Type() byte { return PID_Puncher_CReqTCP }
func writeTCPAddr(w io.Writer, addr net.TCPAddr) error {
err := binary.Write(w, binary.LittleEndian, uint16(addr.Port))
if err != nil {
return err
}
v6 := addr.IP.To16()
if v6 == nil {
return errors.New("cannot marshal TCPAddr: IP nil")
}
return binary.Write(w, binary.LittleEndian, v6)
}
func readTCPAddr(r io.Reader) (net.TCPAddr, error) {
var port uint16
if err := binary.Read(r, binary.LittleEndian, &port); err != nil {
return net.TCPAddr{}, ErrInvalidMessage(err.Error())
}
var ip [16]byte
if err := binary.Read(r, binary.LittleEndian, &ip); err != nil {
return net.TCPAddr{}, ErrInvalidMessage(err.Error())
}
return net.TCPAddr{Port: int(port), IP: ip[:]}, nil
}
// Fails if SourceAddr or DestAddr is not set
func (p CReqTCP) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
p.Header.Type = p.Type()
binary.Write(&b, binary.LittleEndian, p.Header)
err := writeTCPAddr(&b, p.SourceAddr)
if err != nil {
return nil, err
}
err = writeTCPAddr(&b, p.DestAddr)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (p *CReqTCP) UnmarshalBinary(buf []byte) error {
b := bytes.NewReader(buf)
if err := binary.Read(b, binary.LittleEndian, &p.Header); err != nil {
return ErrInvalidMessage(err.Error())
}
if !p.Header.Version.Supported() {
return ErrUnsupportedVersion(p.Header.Version)
}
var err error
p.SourceAddr, err = readTCPAddr(b)
if err != nil {
return err
}
p.DestAddr, err = readTCPAddr(b)
if err != nil {
return err
}
return nil
}