Skip to content

Commit f22b668

Browse files
authored
Merge pull request #78 from muxinc/msmith/random-socket-id
Generate socket id randomly and validate that it's not already in use
2 parents 6b7714e + aa2acb6 commit f22b668

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

conn_request.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/datarhei/gosrt/crypto"
99
"github.com/datarhei/gosrt/packet"
10+
"github.com/datarhei/gosrt/rand"
1011
)
1112

1213
// ConnRequest is an incoming connection request
@@ -340,6 +341,23 @@ func (req *connRequest) Reject(reason RejectionReason) {
340341
delete(req.ln.connReqs, req.socketId)
341342
}
342343

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+
343361
func (req *connRequest) Accept() (Conn, error) {
344362
if req.crypto != nil && len(req.passphrase) == 0 {
345363
req.Reject(REJ_BADSECRET)
@@ -354,7 +372,10 @@ func (req *connRequest) Accept() (Conn, error) {
354372
}
355373

356374
// 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+
}
358379

359380
// Select the largest TSBPD delay advertised by the caller, but at least 120ms
360381
recvTsbpdDelay := uint16(req.config.ReceiverLatency.Milliseconds())

0 commit comments

Comments
 (0)