7
7
8
8
"github.com/datarhei/gosrt/crypto"
9
9
"github.com/datarhei/gosrt/packet"
10
+ "github.com/datarhei/gosrt/rand"
10
11
)
11
12
12
13
// ConnRequest is an incoming connection request
@@ -340,6 +341,23 @@ func (req *connRequest) Reject(reason RejectionReason) {
340
341
delete (req .ln .connReqs , req .socketId )
341
342
}
342
343
344
+ // generateSocketId generates an SRT SocketID that can be used for this connection
345
+ func (req * connRequest ) generateSocketId () (uint32 , error ) {
346
+ for i := 0 ; i < 10 ; i ++ {
347
+ socketId , err := rand .Uint32 ()
348
+ if err != nil {
349
+ return 0 , fmt .Errorf ("could not generate random socket id" )
350
+ }
351
+
352
+ // check that the socket id is not already in use
353
+ if _ , found := req .ln .conns [socketId ]; ! found {
354
+ return socketId , nil
355
+ }
356
+ }
357
+
358
+ return 0 , fmt .Errorf ("could not generate unused socketid" )
359
+ }
360
+
343
361
func (req * connRequest ) Accept () (Conn , error ) {
344
362
if req .crypto != nil && len (req .passphrase ) == 0 {
345
363
req .Reject (REJ_BADSECRET )
@@ -354,7 +372,10 @@ func (req *connRequest) Accept() (Conn, error) {
354
372
}
355
373
356
374
// Create a new socket ID
357
- socketId := uint32 (time .Since (req .ln .start ).Microseconds ())
375
+ socketId , err := req .generateSocketId ()
376
+ if err != nil {
377
+ return nil , fmt .Errorf ("could not generate socket id: %w" , err )
378
+ }
358
379
359
380
// Select the largest TSBPD delay advertised by the caller, but at least 120ms
360
381
recvTsbpdDelay := uint16 (req .config .ReceiverLatency .Milliseconds ())
0 commit comments