Skip to content

Commit

Permalink
Extend routing specification to allow inserting arbitrary
Browse files Browse the repository at this point in the history
pre-call-originate ("po_proc") routine to do various
conditioning, such is in this case appending arbitrary
attributes into accounting request. 3 such functions are
provided:

- VAL2Xattrs: append to both originate and answering accounting
  requests;
- VAL2XattrsO: append to originate request only;
- VAL2XattrsA: append to answering request only.

PR: #38
  • Loading branch information
sobomax committed Aug 5, 2024
1 parent 2f8c181 commit 68ded67
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions sippy/B2BRoute.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from sippy.SipHeader import SipHeader
from sippy.SipConf import SipConf
from sippy.B2BTransforms import getTransProc

try:
from urllib import unquote
Expand Down Expand Up @@ -163,6 +164,8 @@ def __init__(self, sroute = None, cself = None):
self.params['outbound_proxy'] = (v, 5060)
else:
self.params['outbound_proxy'] = (host_port[0], int(host_port[1]))
elif a == 'po_proc':
self.params['po_proc'] = getTransProc(v)
else:
self.params[a] = v
if len(extra_headers) > 0:
Expand Down
27 changes: 26 additions & 1 deletion sippy/B2BTransforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import sys

from sippy.SipRequest import SipRequest
from sippy.CCEvents import CCEventTry

class HDR2Xattrs():
# Source: https://github.com/sippy/b2bua/pull/38
Expand Down Expand Up @@ -55,10 +56,34 @@ def __call__(self, cc:'CallController', req:SipRequest):
else:
cc.extra_attributes.extend(extra_attributes)

class VAL2Xattrs():
# Source: https://github.com/sippy/b2bua/pull/39
# Author: @twmobius
doO: bool = True
doA: bool = True
radius_parameters: list
def __init__(self, v:str):
radius_parameters = []
pairs = v.split(';')
for pair in pairs:
[key, _, value] = pair.partition("=")
if value == '': continue
radius_parameters.append((key, value))
self.radius_parameters = radius_parameters

def __call__(self, cc:'CallController', _:CCEventTry):
if self.doO and cc.acctO is not None:
cc.acctO.addAttributes(self.radius_parameters)
if self.doA and cc.acctA is not None:
cc.acctA.addAttributes(self.radius_parameters)

class Nop():
def __init__(self, v:str): pass
def __call__(self, *a, **kwa): pass

class VAL2XattrsA(VAL2Xattrs): doO = False
class VAL2XattrsO(VAL2Xattrs): doA = False

def getTransProc(value:str):
rparts = value.split('[', 1)
if not len(rparts) == 2 or not value.endswith(']'):
Expand All @@ -70,5 +95,5 @@ def getTransProc(value:str):
return fclass(farg)

if __name__ == '__main__':
for t in ('HDR2Xattrs[X-foo-hdr]',):
for t in ('HDR2Xattrs[X-foo-hdr]', 'VAL2Xattrs[foo=bar;baz=xxx]', 'VAL2XattrsA[foo=bar;baz=xxx]', 'VAL2XattrsO[foo=bar;baz=xxx]'):
p = getTransProc(t)
4 changes: 4 additions & 0 deletions sippy/RadiusAccounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,7 @@ def _process_result(self, results, sip_cid, btime):
else:
message = 'Error sending Acct/%s request (delay is %.3f)\n' % (self.origin, delay)
self.global_config['_sip_logger'].write(message, call_id = sip_cid)

def addAttributes(self, attributes):
self._attributes.extend(tuple(x) for x in attributes \
if tuple(x) not in self._attributes)
3 changes: 3 additions & 0 deletions sippy/b2bua_radius.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ def placeOriginate(self, oroute):
self.state = CCStateDead
return
event.reason = self.eTry.reason
po_proc = oroute.params.get('po_proc', None)
if po_proc is not None:
po_proc(self, event)
self.uaO.recvEvent(event)

def disconnect(self, rtime = None):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_B2BTransforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class TestB2BTransforms(unittest.TestCase):
def test_getTransProc(self):
transformations = [
('HDR2Xattrs[X-foo-hdr]', (FakeCC(), FakeRequest(self))),
('VAL2Xattrs[foo=bar;baz=xxx]', (FakeCC(), FakeEvent())),
('VAL2XattrsA[foo=bar;baz=xxx]', (FakeCC(), FakeEvent())),
('VAL2XattrsO[foo=bar;baz=xxx]', (FakeCC(), FakeEvent())),
('Nop[]', (None, None)),
]

Expand Down

0 comments on commit 68ded67

Please sign in to comment.