Skip to content

Commit e95cc28

Browse files
committed
Randomize initial UA's local CSeq selection.
1 parent fb39d19 commit e95cc28

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

sippy/SipCSeq.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

2727
from sippy.SipGenericHF import SipGenericHF
28+
from secrets import randbelow
2829

2930
class SipCSeq(SipGenericHF):
3031
hf_names = ('cseq',)
3132
cseq = None
3233
method = None
34+
initial_min = 1
35+
initial_max = (2 ** 31) - 1
36+
initial_headroom = 1024
3337

3438
def __init__(self, body = None, cseq = None, method = None):
3539
SipGenericHF.__init__(self, body)
@@ -71,3 +75,9 @@ def getCanName(self, name, compact = False):
7175
def incCSeqNum(self):
7276
self.cseq += 1
7377
return self.cseq
78+
79+
@classmethod
80+
def genRandCSeq(cls):
81+
cseq_max = cls.initial_max - cls.initial_headroom
82+
cseq_span = cseq_max - cls.initial_min + 1
83+
return cls.initial_min + randbelow(cseq_span)

sippy/UacStateIdle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from sippy.SipTo import SipTo
3737
from sippy.SipFrom import SipFrom
3838
from sippy.SipCallId import SipCallId
39+
from sippy.SipCSeq import SipCSeq
3940
from sippy.SipHeader import SipHeader
4041

4142
class UacStateIdle(UaStateGeneric):
@@ -66,7 +67,7 @@ def recvEvent(self, event):
6667
lurl.port = lurl.transport = None
6768
self.ua.lUri = SipFrom(address = SipAddress(url = lurl, hadbrace = True, name = callingName))
6869
self.ua.lUri.setTag(self.ua.lTag)
69-
self.ua.lCSeq = 200
70+
self.ua.lCSeq = SipCSeq.genRandCSeq()
7071
if self.ua.lContact == None:
7172
self.ua.lContact = SipContact()
7273
curl = self.ua.lContact.getUrl()

sippy/UasStateIdle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from sippy.UaStateGeneric import UaStateGeneric
3333
from sippy.CCEvents import CCEventTry
3434
from sippy.SipContact import SipContact
35+
from sippy.SipCSeq import SipCSeq
3536
from sippy.SipFrom import SipFrom
3637
from sippy.SipTo import SipTo
3738

@@ -45,7 +46,7 @@ def recvRequest(self, req):
4546
self.ua.origin = 'caller'
4647
#print('INVITE received in the Idle state, going to the Trying state')
4748
self.ua.uasResp = req.genResponse(100, 'Trying', server = self.ua.local_ua)
48-
self.ua.lCSeq = 100 # XXX: 100 for debugging so that incorrect CSeq generation will be easily spotted
49+
self.ua.lCSeq = SipCSeq.genRandCSeq()
4950
if self.ua.lContact == None:
5051
self.ua.lContact = SipContact()
5152
self.ua.rTarget = req.getHFBody('contact').getUrl().getCopy()

0 commit comments

Comments
 (0)