Skip to content

Commit

Permalink
Add SipRequest.sendResponse(), UA.newTransaction() and
Browse files Browse the repository at this point in the history
UA.newUacTransaction() utility apis, to declutter UA code
a bit.

so that we don't have to repeat the same set of arguments over
and over again.
  • Loading branch information
sobomax committed Jan 16, 2025
1 parent c44f80e commit 1f3bbc9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 67 deletions.
17 changes: 14 additions & 3 deletions sippy/UA.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ def processChallenge(self, resp, cseq, ch_hfname, auth_hfname, extra_headers):
else:
return False
req = self.genRequest('INVITE', self.lSDP, (challenge, qop), extra_headers = extra_headers)
self.lCSeq += 1
self.tr = self.global_config['_sip_tm'].newTransaction(req, self.recvResponse, \
laddress = self.source_address, cb_ifver = 2, compact = self.compact_sip)
self.newUacTransaction(req)
self.tr.req_extra_headers = extra_headers
del self.reqs[cseq]
return True
Expand Down Expand Up @@ -427,6 +425,10 @@ def update_ua(self, msg):
self.remote_ua = msg.getHFBody('user-agent').name
elif msg.countHFs('server') > 0:
self.remote_ua = msg.getHFBody('server').name
def sendResponse(*rcode):
resp = msg.genResponse(*rcode, server = self.local_ua)
self.global_config['_sip_tm'].sendResponse(resp, lossemul = self.uas_lossemul)
msg.sendResponse = sendResponse
return

def cancelCreditTimer(self):
Expand All @@ -452,3 +454,12 @@ def resetCreditTime(self, rtime, new_credit_times):

def cleanup(self):
pass

def newTransaction(self, req, **kwa):
self.lCSeq += 1
return self.global_config['_sip_tm'].newTransaction(req, \
laddress = self.source_address, compact = self.compact_sip, **kwa)

def newUacTransaction(self, req, **kwa):
self.tr = self.newTransaction(req, resp_cb = self.recvResponse, \
cb_ifver = 2, **kwa)
42 changes: 13 additions & 29 deletions sippy/UaStateConnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def __init__(self, ua):
def recvRequest(self, req):
if req.getMethod() == 'REFER':
if req.countHFs('refer-to') == 0:
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(400, 'Bad Request', server = self.ua.local_ua))
req.sendResponse(400, 'Bad Request')
return None
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(202, 'Accepted', server = self.ua.local_ua))
req.sendResponse(202, 'Accepted')
also = req.getHFBody('refer-to').getCopy()
self.ua.equeue.append(CCEventDisconnect(also, rtime = req.rtime, origin = self.ua.origin))
self.ua.recvEvent(CCEventDisconnect(rtime = req.rtime, origin = self.ua.origin))
Expand Down Expand Up @@ -96,7 +96,7 @@ def recvRequest(self, req):
self.ua.equeue.append(event)
return (UasStateUpdating,)
if req.getMethod() == 'BYE':
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', server = self.ua.local_ua))
req.sendResponse(200, 'OK')
#print('BYE received in the Connected state, going to the Disconnected state')
if req.countHFs('also') > 0:
also = req.getHFBody('also').getCopy()
Expand All @@ -112,7 +112,7 @@ def recvRequest(self, req):
self.ua.disconnect_ts = req.rtime
return (UaStateDisconnected, self.ua.disc_cbs, req.rtime, self.ua.origin)
if req.getMethod() == 'INFO':
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', server = self.ua.local_ua))
req.sendResponse(200, 'OK')
event = CCEventInfo(req.getBody(), rtime = req.rtime, origin = self.ua.origin)
try:
event.reason_rfc3326 = req.getHFBody('reason')
Expand All @@ -121,7 +121,7 @@ def recvRequest(self, req):
self.ua.equeue.append(event)
return None
if req.getMethod() == 'OPTIONS':
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', server = self.ua.local_ua))
req.sendResponse(200, 'OK')
return None
#print('wrong request %s in the state Connected' % req.getMethod())
return None
Expand Down Expand Up @@ -162,21 +162,17 @@ def recvEvent(self, event):
redirect = redirects[0]
if redirect != None and self.ua.useRefer:
req = self.ua.genRequest('REFER', extra_headers = eh)
self.ua.lCSeq += 1
also = SipReferTo(address = redirect)
req.appendHeader(SipHeader(name = 'refer-to', body = also))
rby = SipReferredBy(address = SipAddress(url = self.ua.lUri.getUrl()))
req.appendHeader(SipHeader(name = 'referred-by', body = rby))
self.ua.global_config['_sip_tm'].newTransaction(req, self.rComplete, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req, resp_cb = self.rComplete)
else:
req = self.ua.genRequest('BYE', extra_headers = eh)
self.ua.lCSeq += 1
if redirect != None:
also = SipAlso(address = redirect)
req.appendHeader(SipHeader(name = 'also', body = also))
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
self.ua.cancelCreditTimer()
self.ua.disconnect_ts = event.rtime
return (UaStateDisconnected, self.ua.disc_cbs, event.rtime, event.origin)
Expand Down Expand Up @@ -208,18 +204,14 @@ def recvEvent(self, event):
max_forwards_hf = None
req = self.ua.genRequest('INVITE', body, extra_headers = eh, \
max_forwards = max_forwards_hf)
self.ua.lCSeq += 1
self.ua.lSDP = body
self.ua.tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.ua.recvResponse, \
laddress = self.ua.source_address, cb_ifver = 2, compact = self.ua.compact_sip)
self.ua.newUacTransaction(req)
return (UacStateUpdating,)
if isinstance(event, CCEventInfo):
body = event.getData()
req = self.ua.genRequest('INFO', extra_headers = eh)
req.setBody(body)
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, None, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
return None
if self.ua.pending_tr != None and isinstance(event, CCEventConnect):
if self.ua.expire_timer != None:
Expand Down Expand Up @@ -248,10 +240,8 @@ def keepAlive(self):
#self.ua.lSDP.parse()
#self.ua.lSDP.content.m_header.port += 4
req = self.ua.genRequest('INVITE', self.ua.lSDP)
self.ua.lCSeq += 1
self.triedauth = False
self.ka_tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.keepAliveResp, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ka_tr = self.ua.newTransaction(req, resp_cb = self.keepAliveResp)

def keepAliveResp(self, resp):
if self.ua.state != self:
Expand All @@ -261,18 +251,14 @@ def keepAliveResp(self, resp):
self.ua.username != None and self.ua.password != None and not self.triedauth:
challenge = resp.getHFBody('www-authenticate')
req = self.ua.genRequest('INVITE', self.ua.lSDP, challenge)
self.ua.lCSeq += 1
self.ka_tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.keepAliveResp, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ka_tr = self.ua.newTransaction(req, resp_cb = self.keepAliveResp)
self.triedauth = True
return
if code == 407 and resp.countHFs('proxy-authenticate') != 0 and \
self.ua.username != None and self.ua.password != None and not self.triedauth:
challenge = resp.getHFBody('proxy-authenticate')
req = self.ua.genRequest('INVITE', self.ua.lSDP, challenge)
self.ua.lCSeq += 1
self.ka_tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.keepAliveResp, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ka_tr = self.ua.newTransaction(req, resp_cb = self.keepAliveResp)
self.triedauth = True
return
if code < 200:
Expand Down Expand Up @@ -303,9 +289,7 @@ def onStateChange(self, newstate):

def rComplete(self, resp):
req = self.ua.genRequest('BYE')
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)

if 'UaStateDisconnected' not in globals():
from sippy.UaStateDisconnected import UaStateDisconnected
Expand Down
4 changes: 2 additions & 2 deletions sippy/UaStateDisconnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def __init__(self, ua):
def recvRequest(self, req):
if req.getMethod() == 'BYE':
#print('BYE received in the Disconnected state')
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', server = self.ua.local_ua))
req.sendResponse(200, 'OK')
else:
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(500, 'Disconnected', server = self.ua.local_ua))
req.sendResponse(500, 'Disconnected')
return None

def goDead(self):
Expand Down
4 changes: 1 addition & 3 deletions sippy/UacStateCancelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def recvResponse(self, resp, tr):
self.ua.rAddr = self.ua.rTarget.getTAddr()
self.ua.rUri.setTag(resp.getHFBody('to').getTag())
req = self.ua.genRequest('BYE')
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
return (UaStateDisconnected,)
return (UaStateDead,)

Expand Down
4 changes: 1 addition & 3 deletions sippy/UacStateIdle.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def recvEvent(self, event):
max_forwards = event.max_forwards)
if auth != None and self.ua.pass_auth:
req.appendHeader(SipHeader(body = auth))
self.ua.lCSeq += 1
self.ua.tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.ua.recvResponse, \
laddress = self.ua.source_address, cb_ifver = 2, compact = self.ua.compact_sip)
self.ua.newUacTransaction(req)
self.ua.auth = None
if self.ua.expire_time != None:
self.ua.expire_mtime = event.rtime.getOffsetCopy(self.ua.expire_time)
Expand Down
4 changes: 1 addition & 3 deletions sippy/UacStateTrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ def updateRoutes(self, resp):

def genBYE(self):
req = self.ua.genRequest('BYE')
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)

def recvResponse(self, resp, tr):
try:
Expand Down
12 changes: 4 additions & 8 deletions sippy/UacStateUpdating.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class UacStateUpdating(UaStateGeneric):

def recvRequest(self, req):
if req.getMethod() == 'INVITE':
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(491, 'Request Pending', server = self.ua.local_ua))
req.sendResponse(491, 'Request Pending')
return None
elif req.getMethod() == 'BYE':
self.ua.global_config['_sip_tm'].cancelTransaction(self.ua.tr)
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', server = self.ua.local_ua))
req.sendResponse(200, 'OK')
#print('BYE received in the Updating state, going to the Disconnected state')
event = CCEventDisconnect(rtime = req.rtime, origin = self.ua.origin)
try:
Expand Down Expand Up @@ -111,9 +111,7 @@ def recvResponse(self, resp, tr):
def updateFailed(self, event):
self.ua.equeue.append(event)
req = self.ua.genRequest('BYE', extra_headers = event.getExtraHeaders())
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
self.ua.cancelCreditTimer()
self.ua.disconnect_ts = event.rtime
self.ua.equeue.append(CCEventDisconnect(rtime = event.rtime, \
Expand All @@ -124,9 +122,7 @@ def recvEvent(self, event):
if isinstance(event, CCEventDisconnect) or isinstance(event, CCEventFail) or isinstance(event, CCEventRedirect):
self.ua.global_config['_sip_tm'].cancelTransaction(self.ua.tr)
req = self.ua.genRequest('BYE', extra_headers = event.getExtraHeaders())
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
self.ua.cancelCreditTimer()
self.ua.disconnect_ts = event.rtime
return (UaStateDisconnected, self.ua.disc_cbs, event.rtime, event.origin)
Expand Down
3 changes: 1 addition & 2 deletions sippy/UasStateRinging.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ def recvEvent(self, event):
def recvRequest(self, req):
if req.getMethod() == 'BYE':
self.ua.sendUasResponse(487, 'Request Terminated')
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK',
server = self.ua.local_ua), lossemul = self.ua.uas_lossemul)
req.sendResponse(200, 'OK')
#print('BYE received in the Ringing state, going to the Disconnected state')
if req.countHFs('also') > 0:
also = req.getHFBody('also').getCopy()
Expand Down
20 changes: 6 additions & 14 deletions sippy/UasStateUpdating.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ class UasStateUpdating(UaStateGeneric):

def recvRequest(self, req):
if req.getMethod() == 'INVITE':
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(491, \
'Request Pending', server = self.ua.local_ua), lossemul = self.ua.uas_lossemul)
req.sendResponse(491, 'Request Pending')
return None
elif req.getMethod() == 'BYE':
self.ua.sendUasResponse(487, 'Request Terminated')
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(200, 'OK', \
server = self.ua.local_ua), lossemul = self.ua.uas_lossemul)
req.sendResponse(200, 'OK')
#print('BYE received in the Updating state, going to the Disconnected state')
event = CCEventDisconnect(rtime = req.rtime, origin = self.ua.origin)
try:
Expand All @@ -55,12 +53,10 @@ def recvRequest(self, req):
return (UaStateDisconnected, self.ua.disc_cbs, req.rtime, self.ua.origin)
elif req.getMethod() == 'REFER':
if req.countHFs('refer-to') == 0:
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(400, 'Bad Request',
server = self.ua.local_ua), lossemul = self.ua.uas_lossemul)
req.sendResponse(400, 'Bad Request')
return None
self.ua.sendUasResponse(487, 'Request Terminated')
self.ua.global_config['_sip_tm'].sendResponse(req.genResponse(202, 'Accepted', \
server = self.ua.local_ua), lossemul = self.ua.uas_lossemul)
req.sendResponse(202, 'Accepted')
also = req.getHFBody('refer-to').getCopy()
self.ua.equeue.append(CCEventDisconnect(also, rtime = req.rtime, origin = self.ua.origin))
self.ua.cancelCreditTimer()
Expand Down Expand Up @@ -110,9 +106,7 @@ def recvEvent(self, event):
elif isinstance(event, CCEventDisconnect):
self.ua.sendUasResponse(487, 'Request Terminated', extra_headers = eh)
req = self.ua.genRequest('BYE', extra_headers = eh)
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
self.ua.cancelCreditTimer()
self.ua.disconnect_ts = event.rtime
return (UaStateDisconnected, self.ua.disc_cbs, event.rtime, event.origin)
Expand All @@ -121,9 +115,7 @@ def recvEvent(self, event):

def cancel(self, rtime, inreq):
req = self.ua.genRequest('BYE')
self.ua.lCSeq += 1
self.ua.global_config['_sip_tm'].newTransaction(req, \
laddress = self.ua.source_address, compact = self.ua.compact_sip)
self.ua.newTransaction(req)
self.ua.cancelCreditTimer()
self.ua.disconnect_ts = rtime
self.ua.changeState((UaStateDisconnected, self.ua.disc_cbs, rtime, self.ua.origin))
Expand Down

0 comments on commit 1f3bbc9

Please sign in to comment.