Skip to content

Commit

Permalink
Generate socket id randomly and validate that it's not already in
Browse files Browse the repository at this point in the history
use.
  • Loading branch information
mlrsmith committed Nov 7, 2024
1 parent d8162b5 commit d0681ec
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion conn_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/datarhei/gosrt/crypto"
"github.com/datarhei/gosrt/packet"
"github.com/datarhei/gosrt/rand"
)

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

// generateSocketId generates an SRT SocketID that can be used for this connection
func (req *connRequest) generateSocketId() (uint32, error) {
for i := 0; i < 10; i++ {
socketId, err := rand.Uint32()
if err != nil {
return 0, fmt.Errorf("could not generate random socket id")
}

// check that the socket id is not already in use
if _, found := req.ln.conns[socketId]; !found {
return socketId, nil
}
}

return 0, fmt.Errorf("could not generate unused socketid")
}

func (req *connRequest) Accept() (Conn, error) {
if req.crypto != nil && len(req.passphrase) == 0 {
req.Reject(REJ_BADSECRET)
Expand All @@ -354,7 +372,10 @@ func (req *connRequest) Accept() (Conn, error) {
}

// Create a new socket ID
socketId := uint32(time.Since(req.ln.start).Microseconds())
socketId, err := req.generateSocketId()
if err != nil {
return nil, fmt.Errorf("could not generate socket id")
}

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

0 comments on commit d0681ec

Please sign in to comment.