-
Notifications
You must be signed in to change notification settings - Fork 72
/
conn.go
855 lines (746 loc) · 23.1 KB
/
conn.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// Copyright 2019-2020 go-gtp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package v2
import (
"context"
"crypto/rand"
"encoding/binary"
"io"
"net"
"sync"
"time"
"github.com/pkg/errors"
"github.com/wmnsk/go-gtp/v2/ies"
"github.com/wmnsk/go-gtp/v2/messages"
)
// Conn represents a GTPv2-C connection.
//
// Conn provides the automatic handling of message by adding handlers to it with
// AddHandler(s). See AddHandler for detailed usage.
//
// Conn also provides the functions to manage Sessions/Bearers that works over the
// connection(=between a node to another).
// See the docs of CreateSession, AddSession, DeleteSession methods for details.
type Conn struct {
mu sync.Mutex
laddr net.Addr
pktConn net.PacketConn
*imsiSessionMap
*teidSessionMap
validationEnabled bool
closeCh chan struct{}
*msgHandlerMap
// sequence is the last SequenceNumber used in the request.
//
// TS29.274 7.6 Reliable Delivery of Signalling Messages;
// The Sequence Number shall be unique for each outstanding Initial message sourced
// from the same IP/UDP endpoint(=Conn).
sequence uint32
// RestartCounter is the RestartCounter value in Recovery IE, which represents how many
// times the GTPv2-C endpoint is restarted.
RestartCounter uint8
}
// NewConn creates a new Conn used for server. On client side, use Dial instead.
func NewConn(laddr net.Addr, counter uint8) *Conn {
return &Conn{
mu: sync.Mutex{},
laddr: laddr,
imsiSessionMap: newimsiSessionMap(),
teidSessionMap: newteidSessionMap(),
validationEnabled: true,
closeCh: make(chan struct{}),
msgHandlerMap: defaultHandlerMap,
sequence: 0,
RestartCounter: counter,
}
}
// Dial sends Echo Request to raddr to check if the endpoint is alive and returns Conn.
//
// It does not bind the raddr to the underlying connection, which enables a Conn to
// send to/receive from multiple peers with single laddr.
//
// If Echo exchange is unnecessary, use NewConn and ListenAndServe instead.
func Dial(ctx context.Context, laddr, raddr net.Addr, counter uint8) (*Conn, error) {
c := &Conn{
mu: sync.Mutex{},
imsiSessionMap: newimsiSessionMap(),
teidSessionMap: newteidSessionMap(),
validationEnabled: true,
closeCh: make(chan struct{}),
msgHandlerMap: defaultHandlerMap,
sequence: 0,
RestartCounter: counter,
}
// setup underlying connection first.
// not using net.Dial, as it binds src/dst IP:Port, which makes it harder to
// handle multiple connections with a Conn.
var err error
c.pktConn, err = net.ListenPacket(raddr.Network(), laddr.String())
if err != nil {
return nil, err
}
// send EchoRequest to raddr.
if _, err := c.EchoRequest(raddr); err != nil {
return nil, err
}
buf := make([]byte, 1600)
// if no response coming within 3 seconds, returns error without retrying.
if err := c.pktConn.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
return nil, err
}
n, raddr, err := c.pktConn.ReadFrom(buf)
if err != nil {
return nil, err
}
if err := c.pktConn.SetReadDeadline(time.Time{}); err != nil {
return nil, err
}
// decode incoming message and let it be handled by default handler funcs.
msg, err := messages.Parse(buf[:n])
if err != nil {
return nil, err
}
if err := c.handleMessage(raddr, msg); err != nil {
return nil, err
}
go func() {
if err := c.serve(ctx); err != nil {
logf("fatal error on Conn %s: %s", c.LocalAddr(), err)
}
}()
return c, nil
}
// ListenAndServe creates a new GTPv2-C Conn and start serving background.
func (c *Conn) ListenAndServe(ctx context.Context) error {
var err error
c.pktConn, err = net.ListenPacket(c.laddr.Network(), c.laddr.String())
if err != nil {
return err
}
return c.listenAndServe(ctx)
}
func (c *Conn) listenAndServe(ctx context.Context) error {
// TODO: this func is left for future enhancement.
return c.serve(ctx)
}
func (c *Conn) closed() <-chan struct{} {
return c.closeCh
}
func (c *Conn) serve(ctx context.Context) error {
buf := make([]byte, 1500)
for {
select {
case <-ctx.Done():
return nil
case <-c.closed():
return nil
default:
// do nothing and go forward.
}
n, raddr, err := c.pktConn.ReadFrom(buf)
if err != nil {
if err == io.EOF {
return nil
}
return errors.Errorf("error reading from Conn %s: %s", c.LocalAddr(), err)
}
raw := make([]byte, n)
copy(raw, buf)
go func() {
msg, err := messages.Parse(raw)
if err != nil {
logf("error parsing the message: %v, %x", err, raw)
return
}
if err := c.handleMessage(raddr, msg); err != nil {
logf("error handling message on Conn %s: %s", c.LocalAddr(), err)
}
}()
}
}
// ReadFrom reads a packet from the connection,
// copying the payload into p. It returns the number of
// bytes copied into p and the return address that
// was on the packet.
// It returns the number of bytes read (0 <= n <= len(p))
// and any error encountered. Callers should always process
// the n > 0 bytes returned before considering the error err.
// ReadFrom can be made to time out and return
// an Error with Timeout() == true after a fixed time limit;
// see SetDeadline and SetReadDeadline.
func (c *Conn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
return c.pktConn.ReadFrom(p)
}
// WriteTo writes a packet with payload p to addr.
// WriteTo can be made to time out and return
// an Error with Timeout() == true after a fixed time limit;
// see SetDeadline and SetWriteDeadline.
// On packet-oriented connections, write timeouts are rare.
func (c *Conn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return c.pktConn.WriteTo(p, addr)
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *Conn) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
c.msgHandlerMap = defaultHandlerMap
c.RestartCounter = 0
close(c.closeCh)
if err := c.pktConn.Close(); err != nil {
logf("error closing the underlying conn: %s", err)
}
return nil
}
// LocalAddr returns the local network address.
func (c *Conn) LocalAddr() net.Addr {
return c.pktConn.LocalAddr()
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
func (c *Conn) SetDeadline(t time.Time) error {
return c.pktConn.SetDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.pktConn.SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.pktConn.SetWriteDeadline(t)
}
// AddHandler adds a message handler to Conn.
//
// By adding HandlerFunc, Conn(and Session, Bearer created over the Conn) will handle
// the specified type of message with it's paired HandlerFunc when receiving.
// Messages without registered handlers are just ignored and logged.
//
// This should be performed just after creating Conn, otherwise the user cannot retrieve
// any values, which is in most cases vital to continue working as a node, from the incoming
// messages.
//
// The error returned from handler is just logged. Any important due should be done inside
// the HandlerFunc before returning. This behavior might change in the future.
//
// HandlerFunc for EchoResponse and VersionNotSupportedIndication are registered by default.
// These HandlerFunc can be overridden by specifying messages.MsgTypeEchoResponse and/or
// messages.MsgTypeVersionNotSupportedIndication as msgType parameter.
func (c *Conn) AddHandler(msgType uint8, fn HandlerFunc) {
c.msgHandlerMap.store(msgType, fn)
}
// AddHandlers adds multiple handler funcs at a time, using a map.
// The key of the map is message type of the GTPv2-C messages. You can use MsgTypeFooBar
// constants defined in this package as well as any raw uint8 values.
//
// See AddHandler for how the given handlers behave.
func (c *Conn) AddHandlers(funcs map[uint8]HandlerFunc) {
for msgType, fn := range funcs {
c.msgHandlerMap.store(msgType, fn)
}
}
func (c *Conn) handleMessage(senderAddr net.Addr, msg messages.Message) error {
if c.validationEnabled {
if err := c.validate(senderAddr, msg); err != nil {
logf("failed to validate a message: %s", err)
}
}
handle, ok := c.msgHandlerMap.load(msg.MessageType())
if !ok {
logf("%v", &HandlerNotFoundError{MsgType: msg.MessageTypeName()})
}
if err := handle(c, senderAddr, msg); err != nil {
logf("failed to handle message %s: %s", msg, err)
}
return nil
}
// EnableValidation turns on automatic validation of incoming messages.
// This is expected to be used only after DisableValidation() is used, as the validation
// is enabled by default.
//
// Conn checks if;
//
// GTP Version is 2
// TEID is known to Conn
//
// Even the validation is failed, it does not return error to user. Instead, it just logs
// and discards the packets so that the HandlerFunc won't get the invalid message.
// Extra validations should be done in HandlerFunc.
func (c *Conn) EnableValidation() {
c.mu.Lock()
defer c.mu.Unlock()
c.validationEnabled = true
}
// DisableValidation turns off automatic validation of incoming messages.
// It is not recommended to use this except the node is in debugging mode.
//
// See EnableValidation for what are validated.
func (c *Conn) DisableValidation() {
c.mu.Lock()
defer c.mu.Unlock()
c.validationEnabled = false
}
func (c *Conn) validate(senderAddr net.Addr, msg messages.Message) error {
// check GTP version
if msg.Version() != 2 {
if err := c.VersionNotSupportedIndication(senderAddr, msg); err != nil {
return errors.Errorf("failed to respond with VersionNotSupportedIndication: %s", err)
}
return errors.Errorf("received an invalid version(%d) of message: %v", msg.Version(), msg)
}
// check if TEID is known or not
if teid := msg.TEID(); teid != 0 {
if _, err := c.GetSessionByTEID(teid, senderAddr); err != nil {
return &InvalidTEIDError{TEID: teid}
}
}
return nil
}
// SendMessageTo sends a message to addr.
// Unlike WriteTo, it sets the Sequence Number properly and returns the one used in the message.
func (c *Conn) SendMessageTo(msg messages.Message, addr net.Addr) (uint32, error) {
seq := c.IncSequence()
msg.SetSequenceNumber(seq)
payload, err := messages.Marshal(msg)
if err != nil {
seq = c.DecSequence()
return seq, errors.Wrapf(err, "failed to send %T", msg)
}
if _, err := c.WriteTo(payload, addr); err != nil {
seq = c.DecSequence()
return seq, errors.Wrapf(err, "failed to send %T", msg)
}
return seq, nil
}
// IncSequence increments the SequenceNumber associated with Conn.
func (c *Conn) IncSequence() uint32 {
c.mu.Lock()
defer c.mu.Unlock()
c.sequence++
// SequenceNumber is 3-octet long
if c.sequence > 0xffffff {
c.sequence = 0
}
return c.sequence
}
// DecSequence decrements the SequenceNumber associated with Conn.
func (c *Conn) DecSequence() uint32 {
c.mu.Lock()
defer c.mu.Unlock()
c.sequence--
return c.sequence
}
// SequenceNumber returns the current(=last used) SequenceNumber associated with Conn.
func (c *Conn) SequenceNumber() uint32 {
c.mu.Lock()
defer c.mu.Unlock()
return c.sequence
}
// EchoRequest sends a EchoRequest.
func (c *Conn) EchoRequest(raddr net.Addr) (uint32, error) {
msg := messages.NewEchoRequest(0, ies.NewRecovery(c.RestartCounter))
seq, err := c.SendMessageTo(msg, raddr)
if err != nil {
return 0, err
}
return seq, nil
}
// EchoResponse sends a EchoResponse in response to the EchoRequest.
func (c *Conn) EchoResponse(raddr net.Addr, req messages.Message) error {
res := messages.NewEchoResponse(0, ies.NewRecovery(c.RestartCounter))
if err := c.RespondTo(raddr, req, res); err != nil {
return err
}
return nil
}
// VersionNotSupportedIndication sends VersionNotSupportedIndication message
// in response to any kind of message.Message.
func (c *Conn) VersionNotSupportedIndication(raddr net.Addr, req messages.Message) error {
res := messages.NewVersionNotSupportedIndication(0, req.Sequence())
if err := c.RespondTo(raddr, req, res); err != nil {
return err
}
return nil
}
// CreateSession sends a CreateSessionRequest and stores information given with IE
// in the Session returned.
//
// After using this method, users don't need to call AddSession with the session
// returned.
//
// By creating a Session with this method, the values in IEs given, such as TEID in F-TEID
// are stored with "best effort". See the source code to see what kind information is
// handled automatically in this method.
//
// Also, a Bearer named "default" is also created to be used as default bearer.
// The default bearer can be retrieved by using GetDefaultBearer() or LookupBearerByName("default").
//
// Note that this method doesn't care IEs given are sufficient or not, as the required IE
// varies much depending on the context in which the Create Session Request is used.
// In other words, any kind of IE can be put on the Create Session Request message using
// this method.
func (c *Conn) CreateSession(raddr net.Addr, ie ...*ies.IE) (*Session, uint32, error) {
// retrieve values from IEs given.
sess := NewSession(raddr, &Subscriber{Location: &Location{}})
br := sess.GetDefaultBearer()
var err error
for _, i := range ie {
if i == nil {
continue
}
switch i.Type {
case ies.IMSI:
sess.IMSI, err = i.IMSI()
if err != nil {
return nil, 0, err
}
case ies.MSISDN:
sess.MSISDN, err = i.MSISDN()
if err != nil {
return nil, 0, err
}
case ies.MobileEquipmentIdentity:
sess.IMEI, err = i.MobileEquipmentIdentity()
if err != nil {
return nil, 0, err
}
case ies.ServingNetwork:
sess.MCC, err = i.MCC()
if err != nil {
return nil, 0, err
}
sess.MNC, err = i.MNC()
if err != nil {
return nil, 0, err
}
case ies.AccessPointName:
br.APN, err = i.AccessPointName()
if err != nil {
return nil, 0, err
}
case ies.RATType:
sess.RATType, err = i.RATType()
if err != nil {
return nil, 0, err
}
case ies.FullyQualifiedTEID:
it, err := i.InterfaceType()
if err != nil {
return nil, 0, err
}
teid, err := i.TEID()
if err != nil {
return nil, 0, err
}
sess.AddTEID(it, teid)
c.AddTEID(teid, sess)
case ies.BearerContext:
switch i.Instance() {
case 0:
for _, child := range i.ChildIEs {
switch child.Type {
case ies.EPSBearerID:
br.EBI, err = child.EPSBearerID()
if err != nil {
return nil, 0, err
}
case ies.BearerQoS:
br.PL, err = child.PriorityLevel()
if err != nil {
return nil, 0, err
}
br.QCI, err = child.QCILabel()
if err != nil {
return nil, 0, err
}
br.PCI = child.PreemptionCapability()
br.PVI = child.PreemptionVulnerability()
br.MBRUL, err = child.MBRForUplink()
if err != nil {
return nil, 0, err
}
br.MBRDL, err = child.MBRForDownlink()
if err != nil {
return nil, 0, err
}
br.GBRUL, err = child.GBRForUplink()
if err != nil {
return nil, 0, err
}
br.GBRDL, err = child.GBRForUplink()
if err != nil {
return nil, 0, err
}
case ies.FullyQualifiedTEID:
it, err := child.InterfaceType()
if err != nil {
return nil, 0, err
}
teid, err := child.TEID()
if err != nil {
return nil, 0, err
}
sess.AddTEID(it, teid)
case ies.BearerTFT:
// XXX - do nothing for BearerTFT?
}
}
case 1:
// XXX - do nothing for BearerContextsToBeRemoved?
}
}
}
// add session here to registry in case of the response comes super fast.
c.AddSession(sess)
// set IEs into CreateSessionRequest.
msg := messages.NewCreateSessionRequest(0, 0, ie...)
seq, err := c.SendMessageTo(msg, raddr)
if err != nil {
return nil, 0, err
}
return sess, seq, nil
}
// DeleteSession sends a DeleteSessionRequest with TEID and IEs given.
func (c *Conn) DeleteSession(teid uint32, raddr net.Addr, ie ...*ies.IE) (uint32, error) {
sess, err := c.GetSessionByTEID(teid, raddr)
if err != nil {
return 0, err
}
msg := messages.NewDeleteSessionRequest(teid, 0, ie...)
seq, err := c.SendMessageTo(msg, sess.peerAddr)
if err != nil {
return 0, err
}
return seq, nil
}
// ModifyBearer sends a ModifyBearerRequest with TEID and IEs given..
func (c *Conn) ModifyBearer(teid uint32, raddr net.Addr, ie ...*ies.IE) (uint32, error) {
sess, err := c.GetSessionByTEID(teid, raddr)
if err != nil {
return 0, err
}
msg := messages.NewModifyBearerRequest(teid, 0, ie...)
seq, err := c.SendMessageTo(msg, sess.peerAddr)
if err != nil {
return 0, err
}
return seq, nil
}
// DeleteBearer sends a DeleteBearerRequest TEID and with IEs given.
func (c *Conn) DeleteBearer(teid uint32, raddr net.Addr, ie ...*ies.IE) (uint32, error) {
sess, err := c.GetSessionByTEID(teid, raddr)
if err != nil {
return 0, err
}
msg := messages.NewDeleteBearerRequest(teid, 0, ie...)
seq, err := c.SendMessageTo(msg, sess.peerAddr)
if err != nil {
return 0, err
}
return seq, nil
}
// RespondTo sends a message(specified with "toBeSent" param) in response to a message
// (specified with "received" param).
//
// This exists to make it easier to handle SequenceNumber.
func (c *Conn) RespondTo(raddr net.Addr, received, toBeSent messages.Message) error {
toBeSent.SetSequenceNumber(received.Sequence())
b := make([]byte, toBeSent.MarshalLen())
if err := toBeSent.MarshalTo(b); err != nil {
return err
}
if _, err := c.WriteTo(b, raddr); err != nil {
return err
}
return nil
}
// GetSessionByTEID returns Session looked up by TEID and sender of the message.
func (c *Conn) GetSessionByTEID(teid uint32, peer net.Addr) (*Session, error) {
session, ok := c.teidSessionMap.load(teid)
if !ok {
return nil, &InvalidTEIDError{TEID: teid}
}
if peer.String() != session.peerAddrString {
return nil, &InvalidTEIDError{TEID: teid}
}
return session, nil
}
// GetSessionByIMSI returns Session looked up by IMSI.
func (c *Conn) GetSessionByIMSI(imsi string) (*Session, error) {
if session, ok := c.imsiSessionMap.load(imsi); ok {
return session, nil
}
return nil, &UnknownIMSIError{IMSI: imsi}
}
// GetIMSIByTEID returns IMSI associated with TEID and the peer node.
func (c *Conn) GetIMSIByTEID(teid uint32, peer net.Addr) (string, error) {
sess, err := c.GetSessionByTEID(teid, peer)
if err != nil {
return "", err
}
return sess.IMSI, nil
}
// AddTEID adds a session teidSessionMap
func (c *Conn) AddTEID(teid uint32, session *Session) {
c.teidSessionMap.store(teid, session)
}
// AddSession adds a session imsiSessionMap
func (c *Conn) AddSession(session *Session) {
c.imsiSessionMap.store(session.IMSI, session)
}
// RemoveSession removes a session from c.Session.
// The Session is identified by IMSI.
func (c *Conn) RemoveSession(session *Session) {
c.imsiSessionMap.delete(session.IMSI)
session.teidMap.rangeWithFunc(func(k, v interface{}) bool {
teid := v.(uint32)
c.teidSessionMap.delete(teid)
return true
})
}
// RemoveSessionByIMSI removes a session looked up by IMSI.
func (c *Conn) RemoveSessionByIMSI(imsi string) {
sess, ok := c.imsiSessionMap.load(imsi)
if !ok {
return
}
c.RemoveSession(sess)
}
// NewFTEID creates a new F-TEID with random TEID value that is unique within Conn.
// To ensure the uniqueness, don't create in the other way if you once use this method.
//
// Note that in the case there's a lot of Session on the Conn, it may take a long
// time to find a new unique value.
//
// TODO: optimize performance...
func (c *Conn) NewFTEID(ifType uint8, v4, v6 string) (fteidIE *ies.IE) {
var teid uint32
for try := uint32(0); try < 0xffff; try++ {
const logEvery = 0xff
if try&logEvery == logEvery {
logf("Generating NewFTEID crossed tries:%d", try)
}
t := generateRandomUint32()
if t == 0 {
continue
}
// Try to mark TEID as taken. Fails if something exists
if ok := c.teidSessionMap.tryStore(t, nil); !ok {
continue
}
teid = t
break
}
if teid == 0 {
return nil
}
return ies.NewFullyQualifiedTEID(ifType, teid, v4, v6)
}
func generateRandomUint32() uint32 {
b := make([]byte, 4)
if _, err := rand.Read(b); err != nil {
return 0
}
return binary.BigEndian.Uint32(b)
}
// Bearers returns all the bearers registered in Session.
func (c *Conn) Sessions() []*Session {
var ss []*Session
c.imsiSessionMap.rangeWithFunc(func(k, v interface{}) bool {
ss = append(ss, v.(*Session))
return true
})
return ss
}
// SessionCount returns the number of sessions registered in Conn.
//
// This may have some impact on performance in case of large number of Session exists.
func (c *Conn) SessionCount() int {
var count int
c.imsiSessionMap.rangeWithFunc(func(k, v interface{}) bool {
sess := v.(*Session)
if sess.IsActive() {
count++
}
return true
})
return count
}
// BearerCount returns the number of bearers registered in Conn.
//
// This may have some impact on performance in case of large number of Session and Bearer exist.
func (c *Conn) BearerCount() int {
var count int
c.imsiSessionMap.rangeWithFunc(func(k, v interface{}) bool {
sess := v.(*Session)
if sess.IsActive() {
count += sess.BearerCount()
}
return true
})
return count
}
type imsiSessionMap struct {
syncMap sync.Map
}
func newimsiSessionMap() *imsiSessionMap {
return &imsiSessionMap{}
}
func (i *imsiSessionMap) store(imsi string, session *Session) {
i.syncMap.Store(imsi, session)
}
func (i *imsiSessionMap) load(imsi string) (*Session, bool) {
session, ok := i.syncMap.Load(imsi)
if ok && session != nil {
return session.(*Session), true
}
return nil, ok
}
func (i *imsiSessionMap) delete(imsi string) {
i.syncMap.Delete(imsi)
}
func (i *imsiSessionMap) rangeWithFunc(fn func(imsi, session interface{}) bool) {
i.syncMap.Range(fn)
}
type teidSessionMap struct {
syncMap sync.Map
}
func newteidSessionMap() *teidSessionMap {
return &teidSessionMap{}
}
func (t *teidSessionMap) store(teid uint32, session *Session) {
t.syncMap.Store(teid, session)
}
func (t *teidSessionMap) tryStore(teid uint32, session *Session) bool {
_, loaded := t.syncMap.LoadOrStore(teid, session)
return !loaded
}
func (t *teidSessionMap) load(teid uint32) (*Session, bool) {
session, ok := t.syncMap.Load(teid)
if ok && session != nil {
return session.(*Session), true
}
return nil, ok
}
func (t *teidSessionMap) delete(teid uint32) {
t.syncMap.Delete(teid)
}