summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2017-02-13 09:45:26 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2017-02-17 20:14:00 +0900
commit88aaba68bb6db0c7c8d1dc9512b68318a3898307 (patch)
treeb928e47697a4f661beb7c7b5b8e8682e7b4abeac
parent15a12a7a6af10db3e5c3726a6e099e0ed6908007 (diff)
lib/ip: Add wrapper for netaddr.valid_ipv4/6
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/ip.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py
index 4db69cc2..c4094b9a 100644
--- a/ryu/lib/ip.py
+++ b/ryu/lib/ip.py
@@ -16,10 +16,50 @@
import numbers
import struct
+import netaddr
+
from ryu.lib import addrconv
from ryu.lib import type_desc
+def _valid_ip(strategy, bits, addr, flags=0):
+ addr = addr.split('/')
+ if len(addr) == 1:
+ return strategy(addr[0], flags)
+ elif len(addr) == 2:
+ return strategy(addr[0], flags) and 0 <= int(addr[1]) <= bits
+ else:
+ return False
+
+
+def valid_ipv4(addr, flags=0):
+ """
+ Wrapper function of "netaddr.valid_ipv4()".
+
+ The function extends "netaddr.valid_ipv4()" to enable to validate
+ IPv4 network address in "xxx.xxx.xxx.xxx/xx" format.
+
+ :param addr: IP address to be validated.
+ :param flags: See the "netaddr.valid_ipv4()" docs for details.
+ :return: True is valid. False otherwise.
+ """
+ return _valid_ip(netaddr.valid_ipv4, 32, addr, flags)
+
+
+def valid_ipv6(addr, flags=0):
+ """
+ Wrapper function of "netaddr.valid_ipv6()".
+
+ The function extends "netaddr.valid_ipv6()" to enable to validate
+ IPv4 network address in "xxxx:xxxx:xxxx::/xx" format.
+
+ :param addr: IP address to be validated.
+ :param flags: See the "netaddr.valid_ipv6()" docs for details.
+ :return: True is valid. False otherwise.
+ """
+ return _valid_ip(netaddr.valid_ipv6, 128, addr, flags)
+
+
def ipv4_to_bin(ip):
"""
Converts human readable IPv4 string to binary representation.