laforge has submitted this change. ( https://gerrit.osmocom.org/c/mncc-python/+/36322?usp=email )
(
3 is the latest approved patch-set. No files were changed between the latest approved patch-set and the submitted one. )Change subject: Adjust mncc_sock.py to MNCC v7 introducing sockaddr_storage ......................................................................
Adjust mncc_sock.py to MNCC v7 introducing sockaddr_storage
It seems that 4 years ago in 2020 we merged Change-Id Iab17f09380d7cd914cf85746b794c6c04ec7de43 to add MNCCv7 support, but never actually tested mncc_sock.py ever since, as it still tries to unconditionally access the 'ip' and 'port' members of the message, which no longer exist due to the introduction of sockaddr_storage.
Working with sockaddr_storage is a bit nasty as it contains double-underscores in the __ss_padding member, which means ctypes/python will make it a private member that we cannot access. Work around this with a small sed-script that removes the underscores from the clang2py-generated python source code.
Change-Id: I3aa267a866ffaa54e7d71c19231adc78d3831169 --- M mncc.py M mncc_sock.py M regen-mncc-py.sh 3 files changed, 55 insertions(+), 1 deletion(-)
Approvals: laforge: Looks good to me, approved; Verified pespin: Looks good to me, but someone else must approve fixeria: Looks good to me, but someone else must approve
diff --git a/mncc.py b/mncc.py index 94ace48..627ef38 100644 --- a/mncc.py +++ b/mncc.py @@ -589,7 +589,7 @@ struct_sockaddr_storage._pack_ = 1 # source:False struct_sockaddr_storage._fields_ = [ ('ss_family', ctypes.c_uint16), - ('__ss_padding', ctypes.c_char * 118), + ('ss_padding', ctypes.c_char * 118), ('__ss_align', ctypes.c_uint64), ]
diff --git a/mncc_sock.py b/mncc_sock.py index 79e2346..8918c3a 100644 --- a/mncc_sock.py +++ b/mncc_sock.py @@ -65,7 +65,39 @@ else: return "(???)"
+def pad(x, l): + if len(x) < l: + return x + (l - len(x)) * b'\x00' + else: + return x + class mncc_rtp_msg(mncc.struct_gsm_mncc_rtp, mncc_msg_common): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # we have to do this here to make sure our setters below are used + if 'ip' in kwargs: + self.ip = kwargs['ip'] + if 'port' in kwargs: + self.port = kwargs['port'] + + @property + def ip(self): + assert self.addr.ss_family == 2 + return int.from_bytes(self.addr.ss_padding[2:6], 'big') + @ip.setter + def ip(self, val): + self.addr.ss_family = 2 + val = pad(self.addr.ss_padding[:2], 2) + val.to_bytes(4, 'big') + self.addr.ss_padding[6:] + self.addr.ss_padding = val + + @property + def port(self): + return int.from_bytes(self.addr.ss_padding[:2], 'big') + @port.setter + def port(self, val): + self.addr.ss_family = 2 + self.addr.ss_padding = val.to_bytes(2, 'big') + self.addr.ss_padding[2:] + def __str__(self): return 'mncc_rtp_msg(type=0x%04x, callref=%u, ip=%x, port=%u)' % (self.msg_type, self.callref, self.ip, self.port) def __unicode__(self): diff --git a/regen-mncc-py.sh b/regen-mncc-py.sh index 08a92f1..4f9e3f3 100755 --- a/regen-mncc-py.sh +++ b/regen-mncc-py.sh @@ -4,3 +4,4 @@ echo
clang2py -k ems -o mncc.py mncc.h +sed -e 's/__ss_padding/ss_padding/' -i mncc.py