-
Notifications
You must be signed in to change notification settings - Fork 3
/
twamp-server.go
588 lines (472 loc) · 10.6 KB
/
twamp-server.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"flag"
"fmt"
"github.com/golang/glog"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
const (
Unauthenticated = 1 << iota
Authenticated
Encrypted
)
const (
AcceptOK = iota
AcceptFailure
AcceptInternalError
AcceptNotSupported
AcceptPermLimit
AcceptTempLimit
)
type Timestamp struct {
Seconds uint32
Fraction uint32
}
type ServerGreeting struct {
Unused [12]byte
Modes uint32
Challenge [16]byte
Salt [16]byte
Count uint32
MBZ [12]byte
}
type SetupResponse struct {
Mode uint32
KeyID [80]byte
Token [64]byte
ClientIV [16]byte
}
type ServerStart struct {
MBZ [15]byte
Accept byte
ServerIV [16]byte
StartTime Timestamp
MBZ2 [8]byte
}
type RequestSession struct {
Five byte
IPVN byte
ConfSender byte
ConfReceiver byte
Slots uint32
Packets uint32
SenderPort uint16
ReceiverPort uint16
SendAddress uint32
SendAddress2 [12]byte
RecvAddress uint32
RecvAddress2 [12]byte
SID [16]byte
PaddingLength uint32
StartTime Timestamp
Timeout uint64
TypeP uint32
MBZ [8]byte
HMAC [16]byte
}
type AcceptSession struct {
Accept byte
MBZ byte
Port uint16
SID [16]byte
MBZ2 [12]byte
HMAC [16]byte
}
type StartSessions struct {
Two byte
MBZ [15]byte
HMAC [16]byte
}
type StartAck struct {
Accept byte
MBZ [15]byte
HMAC [16]byte
}
type StopSessions struct {
Three byte
Accept byte
MBZ [2]byte
Number uint32
MBZ2 [8]byte
}
type TestRequest struct {
Sequence uint32
Timestamp Timestamp
ErrorEst uint16
}
type TestResponse struct {
Sequence uint32
Timestamp Timestamp
ErrorEst uint16
MBZ [2]byte
RcvTimestamp Timestamp
SenderSequence uint32
SenderTimestamp Timestamp
SenderErrorEst uint16
MBZ2 [2]byte
SenderTTL byte
}
func serveTwamp(listen string, udp_start uint) error {
sock, err := net.Listen("tcp", listen)
if err != nil {
return fmt.Errorf("Error listening on %s: %s", listen, err)
}
glog.Infoln("Listening on", listen)
defer sock.Close()
var udp_port = uint16(udp_start)
for {
conn, err := sock.Accept()
if err != nil {
return fmt.Errorf("Error accepting connection: %s", err)
}
go handleClient(conn, udp_port)
udp_port++
}
}
func handleClient(conn net.Conn, udp_port uint16) {
err := serveClient(conn, udp_port)
if err != nil {
glog.Error(err)
}
}
func serveClient(conn net.Conn, udp_port uint16) error {
defer conn.Close()
glog.Infoln("Handling control connection from client", conn.RemoteAddr())
err := sendServerGreeting(conn)
if err != nil {
return fmt.Errorf("Error sending greeting: %s", err)
}
_, err = receiveSetupResponse(conn)
if err != nil {
return fmt.Errorf("Error receiving setup: %s", err)
}
err = sendServerStart(conn)
if err != nil {
return fmt.Errorf("Error sending start: %s", err)
}
_, err = receiveRequestSession(conn)
if err != nil {
return fmt.Errorf("Error receiving session: %s", err)
}
udp_conn, err := startReflector(udp_port)
if err != nil {
return fmt.Errorf("Error starting reflector on port %d: %s", udp_port, err)
}
err = sendAcceptSession(conn, udp_port)
if err != nil {
return fmt.Errorf("Error sending session accept: %s", err)
}
_, err = receiveStartSessions(conn)
if err != nil {
return fmt.Errorf("Error receiving start sessions: %s", err)
}
test_done := make(chan bool)
defer close(test_done)
go handleReflector(udp_conn, test_done)
err = sendStartAck(conn)
if err != nil {
return fmt.Errorf("Error sending start ACK: %s", err)
}
_, err = receiveStopSessions(conn)
if err != nil {
return fmt.Errorf("Error receiving stop sessions: %s", err)
}
glog.Infoln("Finished control connection from client", conn.RemoteAddr())
return nil
}
func sendServerGreeting(conn net.Conn) error {
greeting, err := createServerGreeting(Unauthenticated)
if err != nil {
return err
}
err = sendMessage(conn, greeting)
if err != nil {
return err
}
return nil
}
func createServerGreeting(modes uint32) (*ServerGreeting, error) {
greeting := new(ServerGreeting)
greeting.Modes = modes
greeting.Count = 1024
_, err := rand.Read(greeting.Challenge[:])
if err != nil {
return nil, err
}
_, err = rand.Read(greeting.Salt[:])
if err != nil {
return nil, err
}
return greeting, nil
}
func sendMessage(conn net.Conn, msg interface{}) error {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, msg)
if err != nil {
return err
}
size := buf.Len()
n, err := conn.Write(buf.Bytes())
if err != nil {
return err
}
if size != n {
return errors.New("Could not send message")
}
return nil
}
func receiveSetupResponse(conn net.Conn) (*SetupResponse, error) {
setup := new(SetupResponse)
err := receiveMessage(conn, setup)
if err != nil {
return nil, err
}
if setup.Mode != Unauthenticated {
err = errors.New("Unsupported setup mode received")
return nil, err
}
return setup, nil
}
func receiveRequestSession(conn net.Conn) (*RequestSession, error) {
session := new(RequestSession)
err := receiveMessage(conn, session)
if err != nil {
return nil, err
}
return session, nil
}
func receiveStartSessions(conn net.Conn) (*StartSessions, error) {
msg := new(StartSessions)
err := receiveMessage(conn, msg)
if err != nil {
return nil, err
}
return msg, nil
}
func receiveStopSessions(conn net.Conn) (*StopSessions, error) {
msg := new(StopSessions)
err := receiveMessage(conn, msg)
if err != nil {
return nil, err
}
return msg, nil
}
func receiveMessage(conn net.Conn, msg interface{}) error {
buf := make([]byte, binary.Size(msg))
_, err := conn.Read(buf)
if err != nil {
return err
}
reader := bytes.NewBuffer(buf)
err = binary.Read(reader, binary.BigEndian, msg)
if err != nil {
return err
}
return nil
}
func getTimestamp(time time.Time) Timestamp {
var ts Timestamp
ts.Seconds = uint32(time.Unix() + 2208988800)
usec := time.Nanosecond() / 1000
ts.Fraction = uint32((4294967296 * usec) / 1000000)
return ts
}
func createServerStart(accept byte) (*ServerStart, error) {
start := new(ServerStart)
start.Accept = accept
ts := getTimestamp(time.Now())
start.StartTime.Seconds = ts.Seconds
start.StartTime.Fraction = ts.Fraction
_, err := rand.Read(start.ServerIV[:])
if err != nil {
return nil, err
}
return start, nil
}
func sendServerStart(conn net.Conn) error {
start, err := createServerStart(AcceptOK)
if err != nil {
return err
}
err = sendMessage(conn, start)
if err != nil {
return err
}
return nil
}
func createAcceptSession(accept byte, port uint16) (*AcceptSession, error) {
msg := new(AcceptSession)
msg.Accept = accept
msg.Port = port
_, err := rand.Read(msg.SID[:])
if err != nil {
return nil, err
}
return msg, nil
}
func sendAcceptSession(conn net.Conn, udp_port uint16) error {
msg, err := createAcceptSession(AcceptOK, udp_port)
if err != nil {
return err
}
err = sendMessage(conn, msg)
if err != nil {
return err
}
return nil
}
func createStartAck(accept byte) (*StartAck, error) {
msg := new(StartAck)
msg.Accept = accept
return msg, nil
}
func sendStartAck(conn net.Conn) error {
msg, err := createStartAck(AcceptOK)
if err != nil {
return err
}
err = sendMessage(conn, msg)
if err != nil {
return err
}
return nil
}
func createTestResponse(buf []byte, seq uint32) ([]byte, error) {
req_len := len(buf)
req := new(TestRequest)
reader := bytes.NewBuffer(buf)
err := binary.Read(reader, binary.BigEndian, req)
if err != nil {
return nil, err
}
received := time.Now()
resp := new(TestResponse)
resp.SenderSequence = req.Sequence
resp.SenderTimestamp = req.Timestamp
resp.SenderErrorEst = req.ErrorEst
resp.SenderTTL = 255
resp.Sequence = seq
resp.RcvTimestamp = getTimestamp(received)
resp.ErrorEst = createErrorEstimate()
writer := new(bytes.Buffer)
resp.Timestamp = getTimestamp(time.Now())
err = binary.Write(writer, binary.BigEndian, resp)
if err != nil {
return nil, err
}
if writer.Len() < req_len {
padding := make([]byte, req_len-writer.Len())
_, err := writer.Write(padding)
if err != nil {
return nil, err
}
}
return writer.Bytes(), nil
}
func createErrorEstimate() uint16 {
var estimate uint16 = 0x3FFF
var buf syscall.Timex
_, err := syscall.Adjtimex(&buf)
if err != nil {
return estimate
}
multiplier := buf.Esterror
multiplier <<= 32
multiplier /= 1000000
var scale uint16
for multiplier >= 0xFF {
scale++
multiplier >>= 1
}
estimate = 1 << 15
estimate |= scale << 8
estimate |= uint16(multiplier & 0xFF)
return estimate
}
func startReflector(udp_port uint16) (*net.UDPConn, error) {
listen := ":" + strconv.Itoa(int(udp_port))
laddr, err := net.ResolveUDPAddr("udp", listen)
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", laddr)
if err != nil {
return nil, err
}
return conn, nil
}
func handleReflector(conn *net.UDPConn, test_done chan bool) {
err := runReflector(conn, test_done)
if err != nil {
glog.Error(err)
}
}
func runReflector(conn *net.UDPConn, test_done chan bool) error {
var seq uint32 = 0
buf := make([]byte, 10240)
timeout := 10 * time.Second
defer conn.Close()
glog.Infoln("Handling test session on port", conn.LocalAddr())
for {
err := conn.SetReadDeadline(time.Now().Add(timeout))
if err != nil {
return fmt.Errorf("Error setting test deadline: %s", err)
}
_, addr, err := conn.ReadFromUDP(buf)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
if _, ok := <-test_done; !ok {
glog.Infoln("Finished test session on port", conn.LocalAddr())
return nil
} else {
glog.V(2).Infoln("Timeout waiting for test packet:", err)
continue
}
}
return fmt.Errorf("Error receiving test packet: %s", err)
}
response, err := createTestResponse(buf, seq)
if err != nil {
return fmt.Errorf("Error creating test response: %s", err)
}
_, err = conn.WriteToUDP(response, addr)
if err != nil {
return fmt.Errorf("Error sending test reponse: %s", err)
}
seq++
}
}
func cleanup() {
glog.Flush()
}
func handleSignals(c chan os.Signal) {
sig := <-c
glog.Infoln("Exiting, got signal:", sig)
cleanup()
os.Exit(0)
}
func setupSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go handleSignals(c)
}
func main() {
listenPtr := flag.String("listen", "localhost:2000", "listen address")
udpStart := flag.Uint("udp-start", 2000, "initial UDP port for tests")
flag.Parse()
setupSignals()
err := serveTwamp(*listenPtr, *udpStart)
if err != nil {
glog.Error(err)
}
cleanup()
}