summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-08-04 23:44:03 +0200
committerChristian Poessinger <christian@poessinger.com>2019-08-04 23:44:07 +0200
commit752229cf7ef080d7a5dd723e7d9b1aa13e44ecd0 (patch)
tree8b365a675eb662ecfdf584c1740b1521333d7f71 /python
parent2a2e3e8cded406ce209b94f565b16e5a766ec97e (diff)
downloadvyos-1x-752229cf7ef080d7a5dd723e7d9b1aa13e44ecd0.tar.gz
vyos-1x-752229cf7ef080d7a5dd723e7d9b1aa13e44ecd0.zip
Python/VyOS validate: improve logic on is_ipv4() and is_ipv6()
Previosly the check failed when a network statement was passed which contained host bits set e.g. 192.0.2.1/24. This no longer is an issue b/c this is a valid v4 address. Address is now split on / and validated.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/validate.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/python/vyos/validate.py b/python/vyos/validate.py
index 8def0a510..8f453f85d 100644
--- a/python/vyos/validate.py
+++ b/python/vyos/validate.py
@@ -18,22 +18,24 @@ import ipaddress
def is_ipv4(addr):
"""
- Check addr if it is an IPv4 address/network.
-
- Return True/False
+ Check addr if it is an IPv4 address/network. Returns True/False
"""
- if ipaddress.ip_network(addr).version == 4:
+
+ # With the below statement we can check for IPv4 networks and host
+ # addresses at the same time
+ if ipaddress.ip_address(addr.split(r'/')[0]).version == 4:
return True
else:
return False
def is_ipv6(addr):
"""
- Check addr if it is an IPv6 address/network.
-
- Return True/False
+ Check addr if it is an IPv6 address/network. Returns True/False
"""
- if ipaddress.ip_network(addr).version == 6:
+
+ # With the below statement we can check for IPv4 networks and host
+ # addresses at the same time
+ if ipaddress.ip_network(addr.split(r'/')[0]).version == 6:
return True
else:
return False