1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
21 from ryu.lib import addrconv
22 from ryu.lib import type_desc
25 def _valid_ip(strategy, bits, addr, flags=0):
26 addr = addr.split('/')
28 return strategy(addr[0], flags)
30 return strategy(addr[0], flags) and 0 <= int(addr[1]) <= bits
35 def valid_ipv4(addr, flags=0):
37 Wrapper function of "netaddr.valid_ipv4()".
39 The function extends "netaddr.valid_ipv4()" to enable to validate
40 IPv4 network address in "xxx.xxx.xxx.xxx/xx" format.
42 :param addr: IP address to be validated.
43 :param flags: See the "netaddr.valid_ipv4()" docs for details.
44 :return: True is valid. False otherwise.
46 return _valid_ip(netaddr.valid_ipv4, 32, addr, flags)
49 def valid_ipv6(addr, flags=0):
51 Wrapper function of "netaddr.valid_ipv6()".
53 The function extends "netaddr.valid_ipv6()" to enable to validate
54 IPv4 network address in "xxxx:xxxx:xxxx::/xx" format.
56 :param addr: IP address to be validated.
57 :param flags: See the "netaddr.valid_ipv6()" docs for details.
58 :return: True is valid. False otherwise.
60 return _valid_ip(netaddr.valid_ipv6, 128, addr, flags)
65 Converts human readable IPv4 string to binary representation.
66 :param str ip: IPv4 address string
67 :return: binary representation of IPv4 address
69 return addrconv.ipv4.text_to_bin(ip)
74 Converts human readable IPv4 string to int type representation.
75 :param str ip: IPv4 address string w.x.y.z
76 :returns: unsigned int of form w << 24 | x << 16 | y << 8 | z
78 return struct.unpack("!I", addrconv.ipv4.text_to_bin(ip))[0]
83 Converts binary or int type representation to human readable IPv4 string.
84 :param ip: binary or int type representation of IPv4 address
85 :return: IPv4 address string
87 if isinstance(ip, numbers.Integral):
88 return addrconv.ipv4.bin_to_text(struct.pack("!I", ip))
90 return addrconv.ipv4.bin_to_text(ip)
95 Converts human readable IPv6 string to binary representation.
96 :param str ip: IPv6 address string
97 :return: binary representation of IPv6 address
99 return addrconv.ipv6.text_to_bin(ip)
104 Converts human readable IPv6 string to int type representation.
105 :param str ip: IPv6 address string
106 :returns: int type representation of IPv6 address
108 return type_desc.Int16.to_user(addrconv.ipv6.text_to_bin(ip))
113 Converts binary or int type representation to human readable IPv6 string.
114 :param ip: binary or int type representation of IPv6 address
115 :return: IPv6 address string
117 if isinstance(ip, numbers.Integral):
118 return addrconv.ipv6.bin_to_text(type_desc.Int16.from_user(ip))
120 return addrconv.ipv6.bin_to_text(ip)
125 Converts human readable IPv4 or IPv6 string to binary representation.
126 :param str ip: IPv4 or IPv6 address string
127 :return: binary representation of IPv4 or IPv6 address
131 return ipv4_to_bin(ip)
133 return ipv6_to_bin(ip)
138 Converts human readable IPv4 or IPv6 string to int type representation.
139 :param str ip: IPv4 or IPv6 address string
140 :return: int type representation of IPv4 or IPv6 address
144 return ipv4_to_int(ip)
146 return ipv6_to_int(ip)
151 Converts binary representation to human readable IPv4 or IPv6 string.
152 :param ip: binary representation of IPv4 or IPv6 address
153 :return: IPv4 or IPv6 address string
156 return ipv4_to_str(ip)
158 return ipv6_to_str(ip)
160 raise struct.error('Invalid ip address length: %s' % len(ip))