Skip to content

Commit cad8e87

Browse files
committed
feat: randomize Cseq value #166
1 parent cc00838 commit cad8e87

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package sipgo
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"encoding/binary"
57
"fmt"
68
"log/slog"
79
"net"
@@ -389,8 +391,15 @@ func clientRequestBuildReq(c *Client, req *sip.Request) error {
389391
}
390392

391393
if v := req.CSeq(); v == nil {
394+
var b [4]byte
395+
_, err := rand.Read(b[:])
396+
if err != nil {
397+
return err
398+
}
399+
n := binary.BigEndian.Uint32(b[:]) & 0x7FFFFFFF // ensure < 2^31
400+
n = max(1<<31-100, n)
392401
cseq := sip.CSeqHeader{
393-
SeqNo: 1,
402+
SeqNo: n,
394403
MethodName: req.Method,
395404
}
396405
mustHeader = append(mustHeader, &cseq)

client_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sipgo
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"net"
78
"os"
@@ -56,7 +57,8 @@ func TestClientRequestBuild(t *testing.T) {
5657
assert.NotEmpty(t, callid.Value())
5758

5859
cseq := req.CSeq()
59-
assert.Equal(t, "1 OPTIONS", cseq.Value())
60+
assert.True(t, cseq.SeqNo > 1)
61+
assert.Equal(t, fmt.Sprintf("%d %s", cseq.SeqNo, "OPTIONS"), cseq.Value())
6062

6163
maxfwd := req.MaxForwards()
6264
assert.Equal(t, "70", maxfwd.Value())

0 commit comments

Comments
 (0)