-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.go
396 lines (338 loc) · 9.65 KB
/
listener.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
package tomtp
import (
"bytes"
"crypto/ecdh"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"log/slog"
"math"
"net"
"strconv"
"strings"
"sync"
"time"
)
type Listener struct {
// this is the port we are listening to
localConn NetworkConn
pubKeyId *ecdh.PublicKey
privKeyId *ecdh.PrivateKey
connMap map[uint64]*Connection // here we store the connection to remote peers, we can have up to
incomingStream chan *Stream
accept func(s *Stream)
closed bool
mu sync.Mutex
}
type ListenOption struct {
seed *[32]byte
privKeyId *ecdh.PrivateKey
manualUpdate bool
}
type ListenFunc func(*ListenOption)
func WithSeed(seed [32]byte) ListenFunc {
return func(c *ListenOption) {
c.seed = &seed
}
}
func WithPrivKeyId(privKeyId *ecdh.PrivateKey) ListenFunc {
return func(c *ListenOption) {
if c.seed != nil {
slog.Warn("overwriting seed with this key")
}
c.privKeyId = privKeyId
}
}
func WithSeedStrHex(seedStrHex string) ListenFunc {
return func(c *ListenOption) {
if c.privKeyId != nil {
slog.Warn("overwriting privKeyId with this seed")
}
if c.seed != nil {
slog.Warn("overwriting old seed with this new seed")
}
if strings.HasPrefix(seedStrHex, "0x") {
seedStrHex = strings.Replace(seedStrHex, "0x", "", 1)
}
seed, err := hex.DecodeString(seedStrHex)
if err != nil {
slog.Error(
"cannot decode seedStrHex",
slog.Any("error", err),
slog.Any("seed", string(seed[:6])))
}
copy(c.seed[:], seed)
}
}
func WithSeedStr(seedStr string) ListenFunc {
return func(c *ListenOption) {
if c.privKeyId != nil {
slog.Warn("overwriting privKeyId with this seed")
}
if c.seed != nil {
slog.Warn("overwriting old seed with this new seed")
}
hashSum := sha256.Sum256([]byte(seedStr))
c.seed = &hashSum
}
}
func WithManualUpdate() ListenFunc {
return func(c *ListenOption) {
c.manualUpdate = true
}
}
func Listen(listenAddrStr string, accept func(s *Stream), options ...ListenFunc) (*Listener, error) {
listenAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
if err != nil {
slog.Error(
"error resolving remote address",
slog.Any("error", err),
slog.String("address", listenAddrStr))
return nil, err
}
return ListenTP(listenAddr, accept, options...)
}
func ListenTP(listenAddr *net.UDPAddr, accept func(s *Stream), options ...ListenFunc) (*Listener, error) {
conn, err := net.ListenUDP("udp", listenAddr)
if err != nil {
slog.Error(
"cannot listen to UDP",
slog.Any("error", err),
slog.Any("listenAddr", listenAddr))
return nil, err
}
err = setDF(conn)
if err != nil {
slog.Error(
"cannot set do-not-fragment",
slog.Any("error", err))
return nil, err
}
return ListenTPNetwork(
NewUDPNetworkConn(conn),
accept,
options...,
)
}
func fillListenOpts(options ...ListenFunc) *ListenOption {
lOpts := &ListenOption{
seed: nil,
privKeyId: nil,
manualUpdate: false,
}
for _, opt := range options {
opt(lOpts)
}
if lOpts.seed != nil {
privKeyId, err := ecdh.X25519().NewPrivateKey(lOpts.seed[:])
if err != nil {
slog.Error(
"error generating private id key from seed",
slog.Any("error", err))
}
lOpts.privKeyId = privKeyId
}
if lOpts.privKeyId == nil {
privKeyId, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
slog.Error(
"error generating private id key random",
slog.Any("error", err))
}
lOpts.privKeyId = privKeyId
}
return lOpts
}
func ListenTPNetwork(localConn NetworkConn, accept func(s *Stream), options ...ListenFunc) (*Listener, error) {
lOpts := fillListenOpts(options...)
privKeyId := lOpts.privKeyId
l := &Listener{
localConn: localConn,
pubKeyId: privKeyId.Public().(*ecdh.PublicKey),
privKeyId: privKeyId,
incomingStream: make(chan *Stream),
connMap: make(map[uint64]*Connection),
accept: accept,
mu: sync.Mutex{},
}
slog.Info(
"Listen",
slog.Any("listenAddr", localConn.LocalAddr()),
slog.String("privKeyId", "0x"+hex.EncodeToString(l.privKeyId.Bytes()[:3])+"…"),
slog.String("pubKeyId", "0x"+hex.EncodeToString(l.pubKeyId.Bytes()[:3])+"…"))
if !lOpts.manualUpdate {
go func() {
var err error
sleepMillis := uint64(0)
for err == nil {
sleepMillis, err = l.Update(timeMilli(), sleepMillis)
}
}()
}
return l, nil
}
func (l *Listener) Close() (error, []error) {
slog.Debug("ListenerClose", debugGoroutineID(), l.debug(nil))
l.mu.Lock()
defer l.mu.Unlock()
l.closed = true
var streamErrors []error
for _, conn := range l.connMap {
for _, stream := range conn.streams {
stream.Close()
}
}
remoteConnError := l.localConn.Close()
l.connMap = make(map[uint64]*Connection)
close(l.incomingStream)
return remoteConnError, streamErrors
}
func (l *Listener) Update(nowMillis uint64, sleepMillis uint64) (newSleepMillis uint64, err error) {
//incoming packets
err = l.handleIncomingUDP(nowMillis, sleepMillis)
if err != nil {
return 0, err
}
//timeouts, retries, ping, sending packets
nextSleepMillis := maxIdleMillis
for _, c := range l.connMap {
for _, s := range c.streams {
if s.streamId == math.MaxUint32 {
continue
}
sleepMillis := s.Update(nowMillis)
if sleepMillis < nextSleepMillis {
nextSleepMillis = sleepMillis
}
}
//connection ping if nothing was sent in s.Update
if nowMillis-c.lastSentMillis > 200 {
s := c.streams[math.MaxUint32]
t, b, err := s.doEncode([]byte{})
if err != nil {
slog.Info("outgoing msg failed", slog.Any("error", err))
s.Close()
}
s.bytesWritten += t
segment := &SndSegment[[]byte]{
sn: 0,
data: b,
sentMillis: 0,
}
n, err := s.conn.listener.handleOutgoingUDP(segment.data, s.conn.remoteAddr)
s.conn.lastSentMillis = nowMillis
if err != nil {
slog.Info("outgoing msg failed", slog.Any("error", err))
s.Close()
}
slog.Debug("SndUDP", debugGoroutineID(), s.debug(), slog.Int("n", n), slog.Any("sn", segment.sn))
}
}
if nextSleepMillis > sleepMillis {
newSleepMillis = nextSleepMillis - sleepMillis
} else {
newSleepMillis = 0
}
return newSleepMillis, nil
}
func (l *Listener) handleOutgoingUDP(data []byte, remoteAddr net.Addr) (int, error) {
return l.localConn.WriteToUDP(data, remoteAddr)
}
func (l *Listener) handleIncomingUDP(nowMillis uint64, sleepMillis uint64) error {
buffer := make([]byte, maxBuffer)
if sleepMillis > 0 {
err := l.localConn.SetReadDeadline(timeNow().Add(time.Duration(sleepMillis) * time.Millisecond))
if err != nil {
slog.Info("error setting deadline for connection", slog.Any("error", err))
return err
}
}
n, remoteAddr, err := l.localConn.ReadFromUDP(buffer)
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "use of closed network connection" {
slog.Info("the connection was closed", slog.Any("error", err))
} else {
slog.Info("error reading from connection", slog.Any("error", err))
return err
}
}
if n > 0 {
slog.Debug("RcvUDP", debugGoroutineID(), l.debug(remoteAddr), slog.Int("n", n))
err = l.startDecode(buffer[:n], remoteAddr, n, nowMillis)
if err != nil {
slog.Info("error from decode", slog.Any("error", err))
}
}
return nil
}
func (l *Listener) startDecode(buffer []byte, remoteAddr net.Addr, n int, nowMillis uint64) error {
header, connId, err := decodeConnId(buffer)
conn := l.connMap[connId]
var m *Message
if conn == nil {
privKeyEpRcv, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
slog.Info("error in rnd from new connection", slog.Any("error", err))
return err
}
slog.Debug("DecodeNew", debugGoroutineID(), l.debug(remoteAddr), slog.Any("connId", connId))
m, err = Decode(buffer, header, connId, l.privKeyId, privKeyEpRcv, nil, nil)
if err != nil {
slog.Info("error in decode", slog.Any("error", err))
return err
}
conn, err = l.newConn(remoteAddr, m.PukKeyIdSnd, privKeyEpRcv, m.PukKeyEpSnd)
if err != nil {
slog.Info("error in newConn from new connection", slog.Any("error", err))
return err
}
} else {
slog.Debug("DecodeExisting", debugGoroutineID(), l.debug(remoteAddr), slog.Any("connId", connId))
m, err = Decode(buffer, header, connId, l.privKeyId, conn.privKeyEpSnd, conn.pubKeyIdRcv, conn.sharedSecret)
}
if err != nil {
slog.Info("error in decoding from new connection", debugGoroutineID(), slog.Any("error", err), slog.Any("conn", conn))
return err
}
p, err := DecodePayload(bytes.NewBuffer(m.PayloadRaw))
if err != nil {
slog.Info("error in decoding payload from new connection", slog.Any("error", err))
return err
}
slog.Debug("DecodedData", debugGoroutineID(), l.debug(remoteAddr), slog.Any("sn", m.Sn), slog.Any("typ", m.MessageHeader.Type))
m.Payload = p
if m.Type == InitRcv || m.Type == InitSnd {
slog.Debug("SetSecret", debugGoroutineID(), l.debug(remoteAddr), slog.Any("sec", m.SharedSecret[:5]))
conn.sharedSecret = m.SharedSecret
}
s, isNew := conn.NewOrGetStream(p.StreamId)
if s.rbSnd != nil {
//channel ping does not have acks
s.rbSnd.Remove(p.AckSn)
}
if len(m.Payload.Data) > 0 {
r := RcvSegment[[]byte]{
sn: m.Sn,
data: m.Payload.Data,
insertedAt: nowMillis,
}
s.rbRcv.Insert(&r) //todo: handle dups
}
if isNew {
l.accept(s)
}
conn.lastSentMillis = nowMillis
return nil
}
func (l *Listener) isOpen() bool {
return !l.closed
}
func (l *Listener) debug(addr net.Addr) slog.Attr {
localAddr := l.localConn.LocalAddr()
lastColonIndex := strings.LastIndex(localAddr.String(), ":")
if cAddr, ok := addr.(*net.UDPAddr); ok {
return slog.String("net", strconv.Itoa(cAddr.Port)+"->"+localAddr.String()[lastColonIndex+1:])
} else {
return slog.String("net", addr.String()+"->"+localAddr.String())
}
}