From 752229cf7ef080d7a5dd723e7d9b1aa13e44ecd0 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 4 Aug 2019 23:44:03 +0200 Subject: 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. --- python/vyos/validate.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'python') 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 -- cgit v1.2.3