usim: Properly decode/encode IPv4 + IPv6 addresses

use normal textual representation for IPv4 and IPv6 addresses

Change-Id: I2c6c377f4502af37639e555826c85d5dcf602f9b
This commit is contained in:
Harald Welte
2023-12-08 14:57:19 +01:00
committed by laforge
parent 478b5fe8e3
commit 6e9ae8a584
2 changed files with 31 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ from construct.lib import integertypes
from pySim.utils import b2h, h2b, swap_nibbles from pySim.utils import b2h, h2b, swap_nibbles
import gsm0338 import gsm0338
import codecs import codecs
import ipaddress
"""Utility code related to the integration of the 'construct' declarative parser.""" """Utility code related to the integration of the 'construct' declarative parser."""
@@ -138,6 +139,32 @@ class GsmStringAdapter(Adapter):
def _encode(self, obj, context, path): def _encode(self, obj, context, path):
return obj.encode(self.codec, self.err) return obj.encode(self.codec, self.err)
class Ipv4Adapter(Adapter):
"""
Encoder converts from 4 bytes to string representation (A.B.C.D).
Decoder converts from string representation (A.B.C.D) to four bytes.
"""
def _decode(self, obj, context, path):
ia = ipaddress.IPv4Address(obj)
return ia.compressed
def _encode(self, obj, context, path):
ia = ipaddress.IPv4Address(obj)
return ia.packed
class Ipv6Adapter(Adapter):
"""
Encoder converts from 16 bytes to string representation.
Decoder converts from string representation to 16 bytes.
"""
def _decode(self, obj, context, path):
ia = ipaddress.IPv6Address(obj)
return ia.compressed
def _encode(self, obj, context, path):
ia = ipaddress.IPv6Address(obj)
return ia.packed
def filter_dict(d, exclude_prefix='_'): def filter_dict(d, exclude_prefix='_'):
"""filter the input dict to ensure no keys starting with 'exclude_prefix' remain.""" """filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""

View File

@@ -875,14 +875,15 @@ class EF_IPS(CyclicEF):
class EF_ePDGId(TransparentEF): class EF_ePDGId(TransparentEF):
_test_de_encode = [ _test_de_encode = [
( '801100657064672e6f736d6f636f6d2e6f7267', {'e_pdg_id': {"type_of_ePDG_address": "FQDN", "ePDG_address" : "epdg.osmocom.org" } } ), ( '801100657064672e6f736d6f636f6d2e6f7267', {'e_pdg_id': {"type_of_ePDG_address": "FQDN", "ePDG_address" : "epdg.osmocom.org" } } ),
( '800501c0a8a001', {'e_pdg_id': {"type_of_ePDG_address": "IPv4", "ePDG_address" : "c0a8a001" } } ), ( '800501c0a8a001', {'e_pdg_id': {"type_of_ePDG_address": "IPv4", "ePDG_address" : "192.168.160.1" } } ),
( '80110220010db8000000000000000000000023', {'e_pdg_id': {"type_of_ePDG_address": "IPv6", "ePDG_address" : "2001:db8::23" } } ),
] ]
class ePDGId(BER_TLV_IE, tag=0x80): class ePDGId(BER_TLV_IE, tag=0x80):
_construct = Struct('type_of_ePDG_address'/Enum(Byte, FQDN=0, IPv4=1, IPv6=2), _construct = Struct('type_of_ePDG_address'/Enum(Byte, FQDN=0, IPv4=1, IPv6=2),
'ePDG_address'/Switch(this.type_of_ePDG_address, 'ePDG_address'/Switch(this.type_of_ePDG_address,
{'FQDN': Utf8Adapter(GreedyBytes), {'FQDN': Utf8Adapter(GreedyBytes),
'IPv4': HexAdapter(GreedyBytes), 'IPv4': Ipv4Adapter(GreedyBytes),
'IPv6': HexAdapter(GreedyBytes)})) 'IPv6': Ipv6Adapter(GreedyBytes)}))
def __init__(self, fid='6ff3', sfid=None, name='EF.ePDGId', desc='Home ePDG Identifier', **kwargs): def __init__(self, fid='6ff3', sfid=None, name='EF.ePDGId', desc='Home ePDG Identifier', **kwargs):
super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs) super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)