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/.

herlesupreeth gerrit-no-reply at lists.osmocom.org
Wed Dec 23 13:52:21 UTC 2020


herlesupreeth has uploaded this change for review. ( 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, 42 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/60/21860/1

diff --git a/pySim/utils.py b/pySim/utils.py
index a6c6be9..376e2da 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -680,3 +680,45 @@
 					(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
+
+	addr_list = addr.split('.')
+
+	ipv4_flag = True
+	for i in addr_list:
+		import re
+		if not re.match('^[0-9_]+$', i):
+			ipv4_flag = False
+			break
+
+	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
+
+	if (len(addr_list) == 4) and ipv4_flag: # IPv4
+		try:
+			import ipaddress
+			# Throws ValueError if addr is not correct
+			ipaddress.ip_address(unicode(addr))
+			return 0x01
+		except ValueError:
+			return None
+	elif fqdn_flag: # FQDN
+		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: 1
Gerrit-Owner: herlesupreeth <herlesupreeth at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201223/5c983fed/attachment.htm>


More information about the gerrit-log mailing list