summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2018-08-29 22:40:10 +0200
committerChristian Poessinger <christian@poessinger.com>2018-08-29 22:40:10 +0200
commita30dac7c24549f9ee32a484b74af049607ffd42b (patch)
treec42f7a5548e2997a37884d27650231762158c996
parenteb5a1be8845e75c115910d96b1bc468c872ce705 (diff)
downloadvyos-1x-a30dac7c24549f9ee32a484b74af049607ffd42b.tar.gz
vyos-1x-a30dac7c24549f9ee32a484b74af049607ffd42b.zip
vyos package: add IP address validators
* is_addr_assigned(addr) - Test if address is assigned to ANY interface on the system * is_ipv4(addr) - Test if it is an IPv4 address, both network and host * is_ipv6(addr) - Test if it is an IPv6 address, both network and host
-rw-r--r--python/vyos/validate.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/python/vyos/validate.py b/python/vyos/validate.py
new file mode 100644
index 000000000..549812371
--- /dev/null
+++ b/python/vyos/validate.py
@@ -0,0 +1,63 @@
+# Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+import netifaces
+import ipaddress
+
+def is_ipv4(addr):
+ """
+ Check addr if it is an IPv4 address/network.
+
+ Return True/False
+ """
+ if ipaddress.ip_network(addr).version == 4:
+ return True
+ else:
+ return False
+
+def is_ipv6(addr):
+ """
+ Check addr if it is an IPv6 address/network.
+
+ Return True/False
+ """
+ if ipaddress.ip_network(addr).version == 6:
+ return True
+ else:
+ return False
+
+def is_addr_assigned(addr):
+ """
+ Verify if the given IPv4/IPv6 address is assigned to any interface on this system
+
+ Return True/False
+ """
+
+ # determine IP version (AF_INET or AF_INET6) depending on passed address
+ addr_type = netifaces.AF_INET
+ if is_ipv6(addr):
+ addr_type = netifaces.AF_INET6
+
+ for interface in netifaces.interfaces():
+ # check if the requested address type is configured at all
+ if addr_type in netifaces.ifaddresses(interface).keys():
+ # Check every IP address on this interface for a match
+ for ip in netifaces.ifaddresses(interface)[addr_type]:
+ # Check if it matches to the address requested
+ if ip['addr'] == addr:
+ return True
+
+ return False
+