Change in pysim[master]: utils.py: Add helper method to get type of address (FQDN, IPv4, IPv6)

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Tue Jan 5 14:22:42 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/21860 )

Change subject: utils.py: Add helper method to get type of address (FQDN, IPv4, IPv6)
......................................................................

utils.py: Add helper method to get type of address (FQDN, IPv4, IPv6)

The function takes address string as input, then validates it and returns the type.
Return: 0x00 (FQDN), 0x01 (IPv4), 0x02 (IPv6), None (Bad address format)

Change-Id: I0fabd4f17bbb11f6bb191c1a9e6276427f9d001f
---
M pySim/utils.py
1 file changed, 58 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/pySim/utils.py b/pySim/utils.py
index 38cc580..c5a38d8 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -685,3 +685,61 @@
 					(rec_info['mcc'], rec_info['mnc'], rec_info['epdg_priority'], rec_info['epdg_fqdn_format'])
 		s += "\t%s # %s\n" % (rec_data, rec_str)
 	return s
+
+def get_addr_type(addr):
+	"""
+	Validates the given address and returns it's type (FQDN or IPv4 or IPv6)
+	Return: 0x00 (FQDN), 0x01 (IPv4), 0x02 (IPv6), None (Bad address argument given)
+
+	TODO: Handle IPv6
+	"""
+
+	# Empty address string
+	if not len(addr):
+		return None
+
+	import sys
+	# Handle python3 and python2 - unicode
+	if sys.version_info[0] < 3:
+		addr_str = unicode(addr)
+	else:
+		addr_str = addr
+
+	addr_list = addr.split('.')
+
+	# Check for IPv4/IPv6
+	try:
+		import ipaddress
+		# Throws ValueError if addr is not correct
+		ipa = ipaddress.ip_address(addr_str)
+
+		if ipa.version == 4:
+			return 0x01
+		elif ipa.version == 6:
+			return 0x02
+	except Exception as e:
+		invalid_ipv4 = True
+		for i in addr_list:
+			# Invalid IPv4 may qualify for a valid FQDN, so make check here
+			# e.g. 172.24.15.300
+			import re
+			if not re.match('^[0-9_]+$', i):
+				invalid_ipv4 = False
+				break
+
+		if invalid_ipv4:
+			return None
+
+	fqdn_flag = True
+	for i in addr_list:
+		# Only Alpha-numeric characters and hyphen - RFC 1035
+		import re
+		if not re.match("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)?$", i):
+			fqdn_flag = False
+			break
+
+	# FQDN
+	if fqdn_flag:
+		return 0x00
+
+	return None

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/21860
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I0fabd4f17bbb11f6bb191c1a9e6276427f9d001f
Gerrit-Change-Number: 21860
Gerrit-PatchSet: 5
Gerrit-Owner: herlesupreeth <herlesupreeth at gmail.com>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210105/45ba8a48/attachment.htm>


More information about the gerrit-log mailing list