mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-31 00:22:05 +03:00
utils: split string formatting from dec_addr_tlv
The function dec_addr_tlv() takes an encoded FQDN or IPv4 address and fromats it into a human readable string that contains the human readable form and the encoded hex form. Unfortunately this limits the usecase of dec_addr_tlv. Lets split the string generation into a separate function so that we can use dec_addr_tlv universally Change-Id: Id017b0786089adac4d6c5be688742eaa9699e529 Related: OS#4963
This commit is contained in:
@@ -32,6 +32,24 @@ from pySim.utils import *
|
|||||||
from smartcard.util import toBytes
|
from smartcard.util import toBytes
|
||||||
from pytlv.TLV import *
|
from pytlv.TLV import *
|
||||||
|
|
||||||
|
def format_addr(addr:str, addr_type:str) -> str:
|
||||||
|
"""
|
||||||
|
helper function to format an FQDN (addr_type = '00') or IPv4
|
||||||
|
(addr_type = '01') address string into a printable string that
|
||||||
|
contains the hexadecimal representation and the original address
|
||||||
|
string (addr)
|
||||||
|
"""
|
||||||
|
res = ""
|
||||||
|
if addr_type == '00': #FQDN
|
||||||
|
res += "\t%s # %s\n" % (s2h(addr), addr)
|
||||||
|
elif addr_type == '01': #IPv4
|
||||||
|
octets = addr.split(".")
|
||||||
|
addr_hex = ""
|
||||||
|
for o in octets:
|
||||||
|
addr_hex += ("%02x" % int(o))
|
||||||
|
res += "\t%s # %s\n" % (addr_hex, addr)
|
||||||
|
return res
|
||||||
|
|
||||||
class Card(object):
|
class Card(object):
|
||||||
|
|
||||||
name = 'SIM'
|
name = 'SIM'
|
||||||
@@ -316,7 +334,12 @@ class UsimCard(Card):
|
|||||||
def read_epdgid(self):
|
def read_epdgid(self):
|
||||||
(res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGId'])
|
(res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGId'])
|
||||||
if sw == '9000':
|
if sw == '9000':
|
||||||
return (dec_addr_tlv(res), sw)
|
try:
|
||||||
|
addr, addr_type = dec_addr_tlv(res)
|
||||||
|
except:
|
||||||
|
addr = None
|
||||||
|
addr_type = None
|
||||||
|
return (format_addr(addr, addr_type), sw)
|
||||||
else:
|
else:
|
||||||
return (None, sw)
|
return (None, sw)
|
||||||
|
|
||||||
@@ -378,7 +401,12 @@ class IsimCard(Card):
|
|||||||
for i in range(0, rec_cnt):
|
for i in range(0, rec_cnt):
|
||||||
(res, sw) = self._scc.read_record(EF_ISIM_ADF_map['PCSCF'], i + 1)
|
(res, sw) = self._scc.read_record(EF_ISIM_ADF_map['PCSCF'], i + 1)
|
||||||
if sw == '9000':
|
if sw == '9000':
|
||||||
content = dec_addr_tlv(res)
|
try:
|
||||||
|
addr, addr_type = dec_addr_tlv(res)
|
||||||
|
except:
|
||||||
|
addr = None
|
||||||
|
addr_type = None
|
||||||
|
content = format_addr(addr, addr_type)
|
||||||
pcscf_recs += "%s" % (len(content) and content or '\tNot available\n')
|
pcscf_recs += "%s" % (len(content) and content or '\tNot available\n')
|
||||||
else:
|
else:
|
||||||
pcscf_recs += "\tP-CSCF: Can't read, response code = %s\n" % (sw)
|
pcscf_recs += "\tP-CSCF: Can't read, response code = %s\n" % (sw)
|
||||||
|
|||||||
@@ -692,8 +692,6 @@ def dec_addr_tlv(hexstr):
|
|||||||
# Convert from hex str to int bytes list
|
# Convert from hex str to int bytes list
|
||||||
addr_tlv_bytes = h2i(hexstr)
|
addr_tlv_bytes = h2i(hexstr)
|
||||||
|
|
||||||
s = ""
|
|
||||||
|
|
||||||
# Get list of tuples containing parsed TLVs
|
# Get list of tuples containing parsed TLVs
|
||||||
tlvs = TLV_parser(addr_tlv_bytes)
|
tlvs = TLV_parser(addr_tlv_bytes)
|
||||||
|
|
||||||
@@ -718,15 +716,18 @@ def dec_addr_tlv(hexstr):
|
|||||||
if addr_type == 0x00: #FQDN
|
if addr_type == 0x00: #FQDN
|
||||||
# Skip address tye byte i.e. first byte in value list
|
# Skip address tye byte i.e. first byte in value list
|
||||||
content = tlv[2][1:]
|
content = tlv[2][1:]
|
||||||
s += "\t%s # %s\n" % (i2h(content), i2s(content))
|
return (i2s(content), '00')
|
||||||
|
|
||||||
elif addr_type == 0x01: #IPv4
|
elif addr_type == 0x01: #IPv4
|
||||||
# Skip address tye byte i.e. first byte in value list
|
# Skip address tye byte i.e. first byte in value list
|
||||||
# Skip the unused byte in Octect 4 after address type byte as per 3GPP TS 31.102
|
# Skip the unused byte in Octect 4 after address type byte as per 3GPP TS 31.102
|
||||||
ipv4 = tlv[2][2:]
|
ipv4 = tlv[2][2:]
|
||||||
content = '.'.join(str(x) for x in ipv4)
|
content = '.'.join(str(x) for x in ipv4)
|
||||||
s += "\t%s # %s\n" % (i2h(ipv4), content)
|
return (content, '01')
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid address type")
|
||||||
|
|
||||||
return s
|
return (None, None)
|
||||||
|
|
||||||
def enc_addr_tlv(addr, addr_type='00'):
|
def enc_addr_tlv(addr, addr_type='00'):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user