backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / ip.py
1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import numbers
17 import struct
18
19 import netaddr
20
21 from ryu.lib import addrconv
22 from ryu.lib import type_desc
23
24
25 def _valid_ip(strategy, bits, addr, flags=0):
26     addr = addr.split('/')
27     if len(addr) == 1:
28         return strategy(addr[0], flags)
29     elif len(addr) == 2:
30         return strategy(addr[0], flags) and 0 <= int(addr[1]) <= bits
31     else:
32         return False
33
34
35 def valid_ipv4(addr, flags=0):
36     """
37     Wrapper function of "netaddr.valid_ipv4()".
38
39     The function extends "netaddr.valid_ipv4()" to enable to validate
40     IPv4 network address in "xxx.xxx.xxx.xxx/xx" format.
41
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.
45     """
46     return _valid_ip(netaddr.valid_ipv4, 32, addr, flags)
47
48
49 def valid_ipv6(addr, flags=0):
50     """
51     Wrapper function of "netaddr.valid_ipv6()".
52
53     The function extends "netaddr.valid_ipv6()" to enable to validate
54     IPv4 network address in "xxxx:xxxx:xxxx::/xx" format.
55
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.
59     """
60     return _valid_ip(netaddr.valid_ipv6, 128, addr, flags)
61
62
63 def ipv4_to_bin(ip):
64     """
65     Converts human readable IPv4 string to binary representation.
66     :param str ip: IPv4 address string
67     :return: binary representation of IPv4 address
68     """
69     return addrconv.ipv4.text_to_bin(ip)
70
71
72 def ipv4_to_int(ip):
73     """
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
77     """
78     return struct.unpack("!I", addrconv.ipv4.text_to_bin(ip))[0]
79
80
81 def ipv4_to_str(ip):
82     """
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
86     """
87     if isinstance(ip, numbers.Integral):
88         return addrconv.ipv4.bin_to_text(struct.pack("!I", ip))
89     else:
90         return addrconv.ipv4.bin_to_text(ip)
91
92
93 def ipv6_to_bin(ip):
94     """
95     Converts human readable IPv6 string to binary representation.
96     :param str ip: IPv6 address string
97     :return: binary representation of IPv6 address
98     """
99     return addrconv.ipv6.text_to_bin(ip)
100
101
102 def ipv6_to_int(ip):
103     """
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
107     """
108     return type_desc.Int16.to_user(addrconv.ipv6.text_to_bin(ip))
109
110
111 def ipv6_to_str(ip):
112     """
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
116     """
117     if isinstance(ip, numbers.Integral):
118         return addrconv.ipv6.bin_to_text(type_desc.Int16.from_user(ip))
119     else:
120         return addrconv.ipv6.bin_to_text(ip)
121
122
123 def text_to_bin(ip):
124     """
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
128     """
129
130     if ':' not in ip:
131         return ipv4_to_bin(ip)
132     else:
133         return ipv6_to_bin(ip)
134
135
136 def text_to_int(ip):
137     """
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
141     """
142
143     if ':' not in ip:
144         return ipv4_to_int(ip)
145     else:
146         return ipv6_to_int(ip)
147
148
149 def bin_to_text(ip):
150     """
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
154     """
155     if len(ip) == 4:
156         return ipv4_to_str(ip)
157     elif len(ip) == 16:
158         return ipv6_to_str(ip)
159     else:
160         raise struct.error('Invalid ip address length: %s' % len(ip))