1 | n/a | # Copyright 2007 Google Inc. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """A fast, lightweight IPv4/IPv6 manipulation library in Python. |
---|
5 | n/a | |
---|
6 | n/a | This library is used to create/poke/manipulate IPv4 and IPv6 addresses |
---|
7 | n/a | and networks. |
---|
8 | n/a | |
---|
9 | n/a | """ |
---|
10 | n/a | |
---|
11 | n/a | __version__ = '1.0' |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | import functools |
---|
15 | n/a | |
---|
16 | n/a | IPV4LENGTH = 32 |
---|
17 | n/a | IPV6LENGTH = 128 |
---|
18 | n/a | |
---|
19 | n/a | class AddressValueError(ValueError): |
---|
20 | n/a | """A Value Error related to the address.""" |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | class NetmaskValueError(ValueError): |
---|
24 | n/a | """A Value Error related to the netmask.""" |
---|
25 | n/a | |
---|
26 | n/a | |
---|
27 | n/a | def ip_address(address): |
---|
28 | n/a | """Take an IP string/int and return an object of the correct type. |
---|
29 | n/a | |
---|
30 | n/a | Args: |
---|
31 | n/a | address: A string or integer, the IP address. Either IPv4 or |
---|
32 | n/a | IPv6 addresses may be supplied; integers less than 2**32 will |
---|
33 | n/a | be considered to be IPv4 by default. |
---|
34 | n/a | |
---|
35 | n/a | Returns: |
---|
36 | n/a | An IPv4Address or IPv6Address object. |
---|
37 | n/a | |
---|
38 | n/a | Raises: |
---|
39 | n/a | ValueError: if the *address* passed isn't either a v4 or a v6 |
---|
40 | n/a | address |
---|
41 | n/a | |
---|
42 | n/a | """ |
---|
43 | n/a | try: |
---|
44 | n/a | return IPv4Address(address) |
---|
45 | n/a | except (AddressValueError, NetmaskValueError): |
---|
46 | n/a | pass |
---|
47 | n/a | |
---|
48 | n/a | try: |
---|
49 | n/a | return IPv6Address(address) |
---|
50 | n/a | except (AddressValueError, NetmaskValueError): |
---|
51 | n/a | pass |
---|
52 | n/a | |
---|
53 | n/a | raise ValueError('%r does not appear to be an IPv4 or IPv6 address' % |
---|
54 | n/a | address) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | def ip_network(address, strict=True): |
---|
58 | n/a | """Take an IP string/int and return an object of the correct type. |
---|
59 | n/a | |
---|
60 | n/a | Args: |
---|
61 | n/a | address: A string or integer, the IP network. Either IPv4 or |
---|
62 | n/a | IPv6 networks may be supplied; integers less than 2**32 will |
---|
63 | n/a | be considered to be IPv4 by default. |
---|
64 | n/a | |
---|
65 | n/a | Returns: |
---|
66 | n/a | An IPv4Network or IPv6Network object. |
---|
67 | n/a | |
---|
68 | n/a | Raises: |
---|
69 | n/a | ValueError: if the string passed isn't either a v4 or a v6 |
---|
70 | n/a | address. Or if the network has host bits set. |
---|
71 | n/a | |
---|
72 | n/a | """ |
---|
73 | n/a | try: |
---|
74 | n/a | return IPv4Network(address, strict) |
---|
75 | n/a | except (AddressValueError, NetmaskValueError): |
---|
76 | n/a | pass |
---|
77 | n/a | |
---|
78 | n/a | try: |
---|
79 | n/a | return IPv6Network(address, strict) |
---|
80 | n/a | except (AddressValueError, NetmaskValueError): |
---|
81 | n/a | pass |
---|
82 | n/a | |
---|
83 | n/a | raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % |
---|
84 | n/a | address) |
---|
85 | n/a | |
---|
86 | n/a | |
---|
87 | n/a | def ip_interface(address): |
---|
88 | n/a | """Take an IP string/int and return an object of the correct type. |
---|
89 | n/a | |
---|
90 | n/a | Args: |
---|
91 | n/a | address: A string or integer, the IP address. Either IPv4 or |
---|
92 | n/a | IPv6 addresses may be supplied; integers less than 2**32 will |
---|
93 | n/a | be considered to be IPv4 by default. |
---|
94 | n/a | |
---|
95 | n/a | Returns: |
---|
96 | n/a | An IPv4Interface or IPv6Interface object. |
---|
97 | n/a | |
---|
98 | n/a | Raises: |
---|
99 | n/a | ValueError: if the string passed isn't either a v4 or a v6 |
---|
100 | n/a | address. |
---|
101 | n/a | |
---|
102 | n/a | Notes: |
---|
103 | n/a | The IPv?Interface classes describe an Address on a particular |
---|
104 | n/a | Network, so they're basically a combination of both the Address |
---|
105 | n/a | and Network classes. |
---|
106 | n/a | |
---|
107 | n/a | """ |
---|
108 | n/a | try: |
---|
109 | n/a | return IPv4Interface(address) |
---|
110 | n/a | except (AddressValueError, NetmaskValueError): |
---|
111 | n/a | pass |
---|
112 | n/a | |
---|
113 | n/a | try: |
---|
114 | n/a | return IPv6Interface(address) |
---|
115 | n/a | except (AddressValueError, NetmaskValueError): |
---|
116 | n/a | pass |
---|
117 | n/a | |
---|
118 | n/a | raise ValueError('%r does not appear to be an IPv4 or IPv6 interface' % |
---|
119 | n/a | address) |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | def v4_int_to_packed(address): |
---|
123 | n/a | """Represent an address as 4 packed bytes in network (big-endian) order. |
---|
124 | n/a | |
---|
125 | n/a | Args: |
---|
126 | n/a | address: An integer representation of an IPv4 IP address. |
---|
127 | n/a | |
---|
128 | n/a | Returns: |
---|
129 | n/a | The integer address packed as 4 bytes in network (big-endian) order. |
---|
130 | n/a | |
---|
131 | n/a | Raises: |
---|
132 | n/a | ValueError: If the integer is negative or too large to be an |
---|
133 | n/a | IPv4 IP address. |
---|
134 | n/a | |
---|
135 | n/a | """ |
---|
136 | n/a | try: |
---|
137 | n/a | return address.to_bytes(4, 'big') |
---|
138 | n/a | except OverflowError: |
---|
139 | n/a | raise ValueError("Address negative or too large for IPv4") |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | def v6_int_to_packed(address): |
---|
143 | n/a | """Represent an address as 16 packed bytes in network (big-endian) order. |
---|
144 | n/a | |
---|
145 | n/a | Args: |
---|
146 | n/a | address: An integer representation of an IPv6 IP address. |
---|
147 | n/a | |
---|
148 | n/a | Returns: |
---|
149 | n/a | The integer address packed as 16 bytes in network (big-endian) order. |
---|
150 | n/a | |
---|
151 | n/a | """ |
---|
152 | n/a | try: |
---|
153 | n/a | return address.to_bytes(16, 'big') |
---|
154 | n/a | except OverflowError: |
---|
155 | n/a | raise ValueError("Address negative or too large for IPv6") |
---|
156 | n/a | |
---|
157 | n/a | |
---|
158 | n/a | def _split_optional_netmask(address): |
---|
159 | n/a | """Helper to split the netmask and raise AddressValueError if needed""" |
---|
160 | n/a | addr = str(address).split('/') |
---|
161 | n/a | if len(addr) > 2: |
---|
162 | n/a | raise AddressValueError("Only one '/' permitted in %r" % address) |
---|
163 | n/a | return addr |
---|
164 | n/a | |
---|
165 | n/a | |
---|
166 | n/a | def _find_address_range(addresses): |
---|
167 | n/a | """Find a sequence of sorted deduplicated IPv#Address. |
---|
168 | n/a | |
---|
169 | n/a | Args: |
---|
170 | n/a | addresses: a list of IPv#Address objects. |
---|
171 | n/a | |
---|
172 | n/a | Yields: |
---|
173 | n/a | A tuple containing the first and last IP addresses in the sequence. |
---|
174 | n/a | |
---|
175 | n/a | """ |
---|
176 | n/a | it = iter(addresses) |
---|
177 | n/a | first = last = next(it) |
---|
178 | n/a | for ip in it: |
---|
179 | n/a | if ip._ip != last._ip + 1: |
---|
180 | n/a | yield first, last |
---|
181 | n/a | first = ip |
---|
182 | n/a | last = ip |
---|
183 | n/a | yield first, last |
---|
184 | n/a | |
---|
185 | n/a | |
---|
186 | n/a | def _count_righthand_zero_bits(number, bits): |
---|
187 | n/a | """Count the number of zero bits on the right hand side. |
---|
188 | n/a | |
---|
189 | n/a | Args: |
---|
190 | n/a | number: an integer. |
---|
191 | n/a | bits: maximum number of bits to count. |
---|
192 | n/a | |
---|
193 | n/a | Returns: |
---|
194 | n/a | The number of zero bits on the right hand side of the number. |
---|
195 | n/a | |
---|
196 | n/a | """ |
---|
197 | n/a | if number == 0: |
---|
198 | n/a | return bits |
---|
199 | n/a | return min(bits, (~number & (number-1)).bit_length()) |
---|
200 | n/a | |
---|
201 | n/a | |
---|
202 | n/a | def summarize_address_range(first, last): |
---|
203 | n/a | """Summarize a network range given the first and last IP addresses. |
---|
204 | n/a | |
---|
205 | n/a | Example: |
---|
206 | n/a | >>> list(summarize_address_range(IPv4Address('192.0.2.0'), |
---|
207 | n/a | ... IPv4Address('192.0.2.130'))) |
---|
208 | n/a | ... #doctest: +NORMALIZE_WHITESPACE |
---|
209 | n/a | [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), |
---|
210 | n/a | IPv4Network('192.0.2.130/32')] |
---|
211 | n/a | |
---|
212 | n/a | Args: |
---|
213 | n/a | first: the first IPv4Address or IPv6Address in the range. |
---|
214 | n/a | last: the last IPv4Address or IPv6Address in the range. |
---|
215 | n/a | |
---|
216 | n/a | Returns: |
---|
217 | n/a | An iterator of the summarized IPv(4|6) network objects. |
---|
218 | n/a | |
---|
219 | n/a | Raise: |
---|
220 | n/a | TypeError: |
---|
221 | n/a | If the first and last objects are not IP addresses. |
---|
222 | n/a | If the first and last objects are not the same version. |
---|
223 | n/a | ValueError: |
---|
224 | n/a | If the last object is not greater than the first. |
---|
225 | n/a | If the version of the first address is not 4 or 6. |
---|
226 | n/a | |
---|
227 | n/a | """ |
---|
228 | n/a | if (not (isinstance(first, _BaseAddress) and |
---|
229 | n/a | isinstance(last, _BaseAddress))): |
---|
230 | n/a | raise TypeError('first and last must be IP addresses, not networks') |
---|
231 | n/a | if first.version != last.version: |
---|
232 | n/a | raise TypeError("%s and %s are not of the same version" % ( |
---|
233 | n/a | first, last)) |
---|
234 | n/a | if first > last: |
---|
235 | n/a | raise ValueError('last IP address must be greater than first') |
---|
236 | n/a | |
---|
237 | n/a | if first.version == 4: |
---|
238 | n/a | ip = IPv4Network |
---|
239 | n/a | elif first.version == 6: |
---|
240 | n/a | ip = IPv6Network |
---|
241 | n/a | else: |
---|
242 | n/a | raise ValueError('unknown IP version') |
---|
243 | n/a | |
---|
244 | n/a | ip_bits = first._max_prefixlen |
---|
245 | n/a | first_int = first._ip |
---|
246 | n/a | last_int = last._ip |
---|
247 | n/a | while first_int <= last_int: |
---|
248 | n/a | nbits = min(_count_righthand_zero_bits(first_int, ip_bits), |
---|
249 | n/a | (last_int - first_int + 1).bit_length() - 1) |
---|
250 | n/a | net = ip((first_int, ip_bits - nbits)) |
---|
251 | n/a | yield net |
---|
252 | n/a | first_int += 1 << nbits |
---|
253 | n/a | if first_int - 1 == ip._ALL_ONES: |
---|
254 | n/a | break |
---|
255 | n/a | |
---|
256 | n/a | |
---|
257 | n/a | def _collapse_addresses_internal(addresses): |
---|
258 | n/a | """Loops through the addresses, collapsing concurrent netblocks. |
---|
259 | n/a | |
---|
260 | n/a | Example: |
---|
261 | n/a | |
---|
262 | n/a | ip1 = IPv4Network('192.0.2.0/26') |
---|
263 | n/a | ip2 = IPv4Network('192.0.2.64/26') |
---|
264 | n/a | ip3 = IPv4Network('192.0.2.128/26') |
---|
265 | n/a | ip4 = IPv4Network('192.0.2.192/26') |
---|
266 | n/a | |
---|
267 | n/a | _collapse_addresses_internal([ip1, ip2, ip3, ip4]) -> |
---|
268 | n/a | [IPv4Network('192.0.2.0/24')] |
---|
269 | n/a | |
---|
270 | n/a | This shouldn't be called directly; it is called via |
---|
271 | n/a | collapse_addresses([]). |
---|
272 | n/a | |
---|
273 | n/a | Args: |
---|
274 | n/a | addresses: A list of IPv4Network's or IPv6Network's |
---|
275 | n/a | |
---|
276 | n/a | Returns: |
---|
277 | n/a | A list of IPv4Network's or IPv6Network's depending on what we were |
---|
278 | n/a | passed. |
---|
279 | n/a | |
---|
280 | n/a | """ |
---|
281 | n/a | # First merge |
---|
282 | n/a | to_merge = list(addresses) |
---|
283 | n/a | subnets = {} |
---|
284 | n/a | while to_merge: |
---|
285 | n/a | net = to_merge.pop() |
---|
286 | n/a | supernet = net.supernet() |
---|
287 | n/a | existing = subnets.get(supernet) |
---|
288 | n/a | if existing is None: |
---|
289 | n/a | subnets[supernet] = net |
---|
290 | n/a | elif existing != net: |
---|
291 | n/a | # Merge consecutive subnets |
---|
292 | n/a | del subnets[supernet] |
---|
293 | n/a | to_merge.append(supernet) |
---|
294 | n/a | # Then iterate over resulting networks, skipping subsumed subnets |
---|
295 | n/a | last = None |
---|
296 | n/a | for net in sorted(subnets.values()): |
---|
297 | n/a | if last is not None: |
---|
298 | n/a | # Since they are sorted, last.network_address <= net.network_address |
---|
299 | n/a | # is a given. |
---|
300 | n/a | if last.broadcast_address >= net.broadcast_address: |
---|
301 | n/a | continue |
---|
302 | n/a | yield net |
---|
303 | n/a | last = net |
---|
304 | n/a | |
---|
305 | n/a | |
---|
306 | n/a | def collapse_addresses(addresses): |
---|
307 | n/a | """Collapse a list of IP objects. |
---|
308 | n/a | |
---|
309 | n/a | Example: |
---|
310 | n/a | collapse_addresses([IPv4Network('192.0.2.0/25'), |
---|
311 | n/a | IPv4Network('192.0.2.128/25')]) -> |
---|
312 | n/a | [IPv4Network('192.0.2.0/24')] |
---|
313 | n/a | |
---|
314 | n/a | Args: |
---|
315 | n/a | addresses: An iterator of IPv4Network or IPv6Network objects. |
---|
316 | n/a | |
---|
317 | n/a | Returns: |
---|
318 | n/a | An iterator of the collapsed IPv(4|6)Network objects. |
---|
319 | n/a | |
---|
320 | n/a | Raises: |
---|
321 | n/a | TypeError: If passed a list of mixed version objects. |
---|
322 | n/a | |
---|
323 | n/a | """ |
---|
324 | n/a | addrs = [] |
---|
325 | n/a | ips = [] |
---|
326 | n/a | nets = [] |
---|
327 | n/a | |
---|
328 | n/a | # split IP addresses and networks |
---|
329 | n/a | for ip in addresses: |
---|
330 | n/a | if isinstance(ip, _BaseAddress): |
---|
331 | n/a | if ips and ips[-1]._version != ip._version: |
---|
332 | n/a | raise TypeError("%s and %s are not of the same version" % ( |
---|
333 | n/a | ip, ips[-1])) |
---|
334 | n/a | ips.append(ip) |
---|
335 | n/a | elif ip._prefixlen == ip._max_prefixlen: |
---|
336 | n/a | if ips and ips[-1]._version != ip._version: |
---|
337 | n/a | raise TypeError("%s and %s are not of the same version" % ( |
---|
338 | n/a | ip, ips[-1])) |
---|
339 | n/a | try: |
---|
340 | n/a | ips.append(ip.ip) |
---|
341 | n/a | except AttributeError: |
---|
342 | n/a | ips.append(ip.network_address) |
---|
343 | n/a | else: |
---|
344 | n/a | if nets and nets[-1]._version != ip._version: |
---|
345 | n/a | raise TypeError("%s and %s are not of the same version" % ( |
---|
346 | n/a | ip, nets[-1])) |
---|
347 | n/a | nets.append(ip) |
---|
348 | n/a | |
---|
349 | n/a | # sort and dedup |
---|
350 | n/a | ips = sorted(set(ips)) |
---|
351 | n/a | |
---|
352 | n/a | # find consecutive address ranges in the sorted sequence and summarize them |
---|
353 | n/a | if ips: |
---|
354 | n/a | for first, last in _find_address_range(ips): |
---|
355 | n/a | addrs.extend(summarize_address_range(first, last)) |
---|
356 | n/a | |
---|
357 | n/a | return _collapse_addresses_internal(addrs + nets) |
---|
358 | n/a | |
---|
359 | n/a | |
---|
360 | n/a | def get_mixed_type_key(obj): |
---|
361 | n/a | """Return a key suitable for sorting between networks and addresses. |
---|
362 | n/a | |
---|
363 | n/a | Address and Network objects are not sortable by default; they're |
---|
364 | n/a | fundamentally different so the expression |
---|
365 | n/a | |
---|
366 | n/a | IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') |
---|
367 | n/a | |
---|
368 | n/a | doesn't make any sense. There are some times however, where you may wish |
---|
369 | n/a | to have ipaddress sort these for you anyway. If you need to do this, you |
---|
370 | n/a | can use this function as the key= argument to sorted(). |
---|
371 | n/a | |
---|
372 | n/a | Args: |
---|
373 | n/a | obj: either a Network or Address object. |
---|
374 | n/a | Returns: |
---|
375 | n/a | appropriate key. |
---|
376 | n/a | |
---|
377 | n/a | """ |
---|
378 | n/a | if isinstance(obj, _BaseNetwork): |
---|
379 | n/a | return obj._get_networks_key() |
---|
380 | n/a | elif isinstance(obj, _BaseAddress): |
---|
381 | n/a | return obj._get_address_key() |
---|
382 | n/a | return NotImplemented |
---|
383 | n/a | |
---|
384 | n/a | |
---|
385 | n/a | class _IPAddressBase: |
---|
386 | n/a | |
---|
387 | n/a | """The mother class.""" |
---|
388 | n/a | |
---|
389 | n/a | __slots__ = () |
---|
390 | n/a | |
---|
391 | n/a | @property |
---|
392 | n/a | def exploded(self): |
---|
393 | n/a | """Return the longhand version of the IP address as a string.""" |
---|
394 | n/a | return self._explode_shorthand_ip_string() |
---|
395 | n/a | |
---|
396 | n/a | @property |
---|
397 | n/a | def compressed(self): |
---|
398 | n/a | """Return the shorthand version of the IP address as a string.""" |
---|
399 | n/a | return str(self) |
---|
400 | n/a | |
---|
401 | n/a | @property |
---|
402 | n/a | def reverse_pointer(self): |
---|
403 | n/a | """The name of the reverse DNS pointer for the IP address, e.g.: |
---|
404 | n/a | >>> ipaddress.ip_address("127.0.0.1").reverse_pointer |
---|
405 | n/a | '1.0.0.127.in-addr.arpa' |
---|
406 | n/a | >>> ipaddress.ip_address("2001:db8::1").reverse_pointer |
---|
407 | n/a | '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa' |
---|
408 | n/a | |
---|
409 | n/a | """ |
---|
410 | n/a | return self._reverse_pointer() |
---|
411 | n/a | |
---|
412 | n/a | @property |
---|
413 | n/a | def version(self): |
---|
414 | n/a | msg = '%200s has no version specified' % (type(self),) |
---|
415 | n/a | raise NotImplementedError(msg) |
---|
416 | n/a | |
---|
417 | n/a | def _check_int_address(self, address): |
---|
418 | n/a | if address < 0: |
---|
419 | n/a | msg = "%d (< 0) is not permitted as an IPv%d address" |
---|
420 | n/a | raise AddressValueError(msg % (address, self._version)) |
---|
421 | n/a | if address > self._ALL_ONES: |
---|
422 | n/a | msg = "%d (>= 2**%d) is not permitted as an IPv%d address" |
---|
423 | n/a | raise AddressValueError(msg % (address, self._max_prefixlen, |
---|
424 | n/a | self._version)) |
---|
425 | n/a | |
---|
426 | n/a | def _check_packed_address(self, address, expected_len): |
---|
427 | n/a | address_len = len(address) |
---|
428 | n/a | if address_len != expected_len: |
---|
429 | n/a | msg = "%r (len %d != %d) is not permitted as an IPv%d address" |
---|
430 | n/a | raise AddressValueError(msg % (address, address_len, |
---|
431 | n/a | expected_len, self._version)) |
---|
432 | n/a | |
---|
433 | n/a | @classmethod |
---|
434 | n/a | def _ip_int_from_prefix(cls, prefixlen): |
---|
435 | n/a | """Turn the prefix length into a bitwise netmask |
---|
436 | n/a | |
---|
437 | n/a | Args: |
---|
438 | n/a | prefixlen: An integer, the prefix length. |
---|
439 | n/a | |
---|
440 | n/a | Returns: |
---|
441 | n/a | An integer. |
---|
442 | n/a | |
---|
443 | n/a | """ |
---|
444 | n/a | return cls._ALL_ONES ^ (cls._ALL_ONES >> prefixlen) |
---|
445 | n/a | |
---|
446 | n/a | @classmethod |
---|
447 | n/a | def _prefix_from_ip_int(cls, ip_int): |
---|
448 | n/a | """Return prefix length from the bitwise netmask. |
---|
449 | n/a | |
---|
450 | n/a | Args: |
---|
451 | n/a | ip_int: An integer, the netmask in expanded bitwise format |
---|
452 | n/a | |
---|
453 | n/a | Returns: |
---|
454 | n/a | An integer, the prefix length. |
---|
455 | n/a | |
---|
456 | n/a | Raises: |
---|
457 | n/a | ValueError: If the input intermingles zeroes & ones |
---|
458 | n/a | """ |
---|
459 | n/a | trailing_zeroes = _count_righthand_zero_bits(ip_int, |
---|
460 | n/a | cls._max_prefixlen) |
---|
461 | n/a | prefixlen = cls._max_prefixlen - trailing_zeroes |
---|
462 | n/a | leading_ones = ip_int >> trailing_zeroes |
---|
463 | n/a | all_ones = (1 << prefixlen) - 1 |
---|
464 | n/a | if leading_ones != all_ones: |
---|
465 | n/a | byteslen = cls._max_prefixlen // 8 |
---|
466 | n/a | details = ip_int.to_bytes(byteslen, 'big') |
---|
467 | n/a | msg = 'Netmask pattern %r mixes zeroes & ones' |
---|
468 | n/a | raise ValueError(msg % details) |
---|
469 | n/a | return prefixlen |
---|
470 | n/a | |
---|
471 | n/a | @classmethod |
---|
472 | n/a | def _report_invalid_netmask(cls, netmask_str): |
---|
473 | n/a | msg = '%r is not a valid netmask' % netmask_str |
---|
474 | n/a | raise NetmaskValueError(msg) from None |
---|
475 | n/a | |
---|
476 | n/a | @classmethod |
---|
477 | n/a | def _prefix_from_prefix_string(cls, prefixlen_str): |
---|
478 | n/a | """Return prefix length from a numeric string |
---|
479 | n/a | |
---|
480 | n/a | Args: |
---|
481 | n/a | prefixlen_str: The string to be converted |
---|
482 | n/a | |
---|
483 | n/a | Returns: |
---|
484 | n/a | An integer, the prefix length. |
---|
485 | n/a | |
---|
486 | n/a | Raises: |
---|
487 | n/a | NetmaskValueError: If the input is not a valid netmask |
---|
488 | n/a | """ |
---|
489 | n/a | # int allows a leading +/- as well as surrounding whitespace, |
---|
490 | n/a | # so we ensure that isn't the case |
---|
491 | n/a | if not _BaseV4._DECIMAL_DIGITS.issuperset(prefixlen_str): |
---|
492 | n/a | cls._report_invalid_netmask(prefixlen_str) |
---|
493 | n/a | try: |
---|
494 | n/a | prefixlen = int(prefixlen_str) |
---|
495 | n/a | except ValueError: |
---|
496 | n/a | cls._report_invalid_netmask(prefixlen_str) |
---|
497 | n/a | if not (0 <= prefixlen <= cls._max_prefixlen): |
---|
498 | n/a | cls._report_invalid_netmask(prefixlen_str) |
---|
499 | n/a | return prefixlen |
---|
500 | n/a | |
---|
501 | n/a | @classmethod |
---|
502 | n/a | def _prefix_from_ip_string(cls, ip_str): |
---|
503 | n/a | """Turn a netmask/hostmask string into a prefix length |
---|
504 | n/a | |
---|
505 | n/a | Args: |
---|
506 | n/a | ip_str: The netmask/hostmask to be converted |
---|
507 | n/a | |
---|
508 | n/a | Returns: |
---|
509 | n/a | An integer, the prefix length. |
---|
510 | n/a | |
---|
511 | n/a | Raises: |
---|
512 | n/a | NetmaskValueError: If the input is not a valid netmask/hostmask |
---|
513 | n/a | """ |
---|
514 | n/a | # Parse the netmask/hostmask like an IP address. |
---|
515 | n/a | try: |
---|
516 | n/a | ip_int = cls._ip_int_from_string(ip_str) |
---|
517 | n/a | except AddressValueError: |
---|
518 | n/a | cls._report_invalid_netmask(ip_str) |
---|
519 | n/a | |
---|
520 | n/a | # Try matching a netmask (this would be /1*0*/ as a bitwise regexp). |
---|
521 | n/a | # Note that the two ambiguous cases (all-ones and all-zeroes) are |
---|
522 | n/a | # treated as netmasks. |
---|
523 | n/a | try: |
---|
524 | n/a | return cls._prefix_from_ip_int(ip_int) |
---|
525 | n/a | except ValueError: |
---|
526 | n/a | pass |
---|
527 | n/a | |
---|
528 | n/a | # Invert the bits, and try matching a /0+1+/ hostmask instead. |
---|
529 | n/a | ip_int ^= cls._ALL_ONES |
---|
530 | n/a | try: |
---|
531 | n/a | return cls._prefix_from_ip_int(ip_int) |
---|
532 | n/a | except ValueError: |
---|
533 | n/a | cls._report_invalid_netmask(ip_str) |
---|
534 | n/a | |
---|
535 | n/a | def __reduce__(self): |
---|
536 | n/a | return self.__class__, (str(self),) |
---|
537 | n/a | |
---|
538 | n/a | |
---|
539 | n/a | @functools.total_ordering |
---|
540 | n/a | class _BaseAddress(_IPAddressBase): |
---|
541 | n/a | |
---|
542 | n/a | """A generic IP object. |
---|
543 | n/a | |
---|
544 | n/a | This IP class contains the version independent methods which are |
---|
545 | n/a | used by single IP addresses. |
---|
546 | n/a | """ |
---|
547 | n/a | |
---|
548 | n/a | __slots__ = () |
---|
549 | n/a | |
---|
550 | n/a | def __int__(self): |
---|
551 | n/a | return self._ip |
---|
552 | n/a | |
---|
553 | n/a | def __eq__(self, other): |
---|
554 | n/a | try: |
---|
555 | n/a | return (self._ip == other._ip |
---|
556 | n/a | and self._version == other._version) |
---|
557 | n/a | except AttributeError: |
---|
558 | n/a | return NotImplemented |
---|
559 | n/a | |
---|
560 | n/a | def __lt__(self, other): |
---|
561 | n/a | if not isinstance(other, _BaseAddress): |
---|
562 | n/a | return NotImplemented |
---|
563 | n/a | if self._version != other._version: |
---|
564 | n/a | raise TypeError('%s and %s are not of the same version' % ( |
---|
565 | n/a | self, other)) |
---|
566 | n/a | if self._ip != other._ip: |
---|
567 | n/a | return self._ip < other._ip |
---|
568 | n/a | return False |
---|
569 | n/a | |
---|
570 | n/a | # Shorthand for Integer addition and subtraction. This is not |
---|
571 | n/a | # meant to ever support addition/subtraction of addresses. |
---|
572 | n/a | def __add__(self, other): |
---|
573 | n/a | if not isinstance(other, int): |
---|
574 | n/a | return NotImplemented |
---|
575 | n/a | return self.__class__(int(self) + other) |
---|
576 | n/a | |
---|
577 | n/a | def __sub__(self, other): |
---|
578 | n/a | if not isinstance(other, int): |
---|
579 | n/a | return NotImplemented |
---|
580 | n/a | return self.__class__(int(self) - other) |
---|
581 | n/a | |
---|
582 | n/a | def __repr__(self): |
---|
583 | n/a | return '%s(%r)' % (self.__class__.__name__, str(self)) |
---|
584 | n/a | |
---|
585 | n/a | def __str__(self): |
---|
586 | n/a | return str(self._string_from_ip_int(self._ip)) |
---|
587 | n/a | |
---|
588 | n/a | def __hash__(self): |
---|
589 | n/a | return hash(hex(int(self._ip))) |
---|
590 | n/a | |
---|
591 | n/a | def _get_address_key(self): |
---|
592 | n/a | return (self._version, self) |
---|
593 | n/a | |
---|
594 | n/a | def __reduce__(self): |
---|
595 | n/a | return self.__class__, (self._ip,) |
---|
596 | n/a | |
---|
597 | n/a | |
---|
598 | n/a | @functools.total_ordering |
---|
599 | n/a | class _BaseNetwork(_IPAddressBase): |
---|
600 | n/a | |
---|
601 | n/a | """A generic IP network object. |
---|
602 | n/a | |
---|
603 | n/a | This IP class contains the version independent methods which are |
---|
604 | n/a | used by networks. |
---|
605 | n/a | |
---|
606 | n/a | """ |
---|
607 | n/a | def __init__(self, address): |
---|
608 | n/a | self._cache = {} |
---|
609 | n/a | |
---|
610 | n/a | def __repr__(self): |
---|
611 | n/a | return '%s(%r)' % (self.__class__.__name__, str(self)) |
---|
612 | n/a | |
---|
613 | n/a | def __str__(self): |
---|
614 | n/a | return '%s/%d' % (self.network_address, self.prefixlen) |
---|
615 | n/a | |
---|
616 | n/a | def hosts(self): |
---|
617 | n/a | """Generate Iterator over usable hosts in a network. |
---|
618 | n/a | |
---|
619 | n/a | This is like __iter__ except it doesn't return the network |
---|
620 | n/a | or broadcast addresses. |
---|
621 | n/a | |
---|
622 | n/a | """ |
---|
623 | n/a | network = int(self.network_address) |
---|
624 | n/a | broadcast = int(self.broadcast_address) |
---|
625 | n/a | for x in range(network + 1, broadcast): |
---|
626 | n/a | yield self._address_class(x) |
---|
627 | n/a | |
---|
628 | n/a | def __iter__(self): |
---|
629 | n/a | network = int(self.network_address) |
---|
630 | n/a | broadcast = int(self.broadcast_address) |
---|
631 | n/a | for x in range(network, broadcast + 1): |
---|
632 | n/a | yield self._address_class(x) |
---|
633 | n/a | |
---|
634 | n/a | def __getitem__(self, n): |
---|
635 | n/a | network = int(self.network_address) |
---|
636 | n/a | broadcast = int(self.broadcast_address) |
---|
637 | n/a | if n >= 0: |
---|
638 | n/a | if network + n > broadcast: |
---|
639 | n/a | raise IndexError('address out of range') |
---|
640 | n/a | return self._address_class(network + n) |
---|
641 | n/a | else: |
---|
642 | n/a | n += 1 |
---|
643 | n/a | if broadcast + n < network: |
---|
644 | n/a | raise IndexError('address out of range') |
---|
645 | n/a | return self._address_class(broadcast + n) |
---|
646 | n/a | |
---|
647 | n/a | def __lt__(self, other): |
---|
648 | n/a | if not isinstance(other, _BaseNetwork): |
---|
649 | n/a | return NotImplemented |
---|
650 | n/a | if self._version != other._version: |
---|
651 | n/a | raise TypeError('%s and %s are not of the same version' % ( |
---|
652 | n/a | self, other)) |
---|
653 | n/a | if self.network_address != other.network_address: |
---|
654 | n/a | return self.network_address < other.network_address |
---|
655 | n/a | if self.netmask != other.netmask: |
---|
656 | n/a | return self.netmask < other.netmask |
---|
657 | n/a | return False |
---|
658 | n/a | |
---|
659 | n/a | def __eq__(self, other): |
---|
660 | n/a | try: |
---|
661 | n/a | return (self._version == other._version and |
---|
662 | n/a | self.network_address == other.network_address and |
---|
663 | n/a | int(self.netmask) == int(other.netmask)) |
---|
664 | n/a | except AttributeError: |
---|
665 | n/a | return NotImplemented |
---|
666 | n/a | |
---|
667 | n/a | def __hash__(self): |
---|
668 | n/a | return hash(int(self.network_address) ^ int(self.netmask)) |
---|
669 | n/a | |
---|
670 | n/a | def __contains__(self, other): |
---|
671 | n/a | # always false if one is v4 and the other is v6. |
---|
672 | n/a | if self._version != other._version: |
---|
673 | n/a | return False |
---|
674 | n/a | # dealing with another network. |
---|
675 | n/a | if isinstance(other, _BaseNetwork): |
---|
676 | n/a | return False |
---|
677 | n/a | # dealing with another address |
---|
678 | n/a | else: |
---|
679 | n/a | # address |
---|
680 | n/a | return (int(self.network_address) <= int(other._ip) <= |
---|
681 | n/a | int(self.broadcast_address)) |
---|
682 | n/a | |
---|
683 | n/a | def overlaps(self, other): |
---|
684 | n/a | """Tell if self is partly contained in other.""" |
---|
685 | n/a | return self.network_address in other or ( |
---|
686 | n/a | self.broadcast_address in other or ( |
---|
687 | n/a | other.network_address in self or ( |
---|
688 | n/a | other.broadcast_address in self))) |
---|
689 | n/a | |
---|
690 | n/a | @property |
---|
691 | n/a | def broadcast_address(self): |
---|
692 | n/a | x = self._cache.get('broadcast_address') |
---|
693 | n/a | if x is None: |
---|
694 | n/a | x = self._address_class(int(self.network_address) | |
---|
695 | n/a | int(self.hostmask)) |
---|
696 | n/a | self._cache['broadcast_address'] = x |
---|
697 | n/a | return x |
---|
698 | n/a | |
---|
699 | n/a | @property |
---|
700 | n/a | def hostmask(self): |
---|
701 | n/a | x = self._cache.get('hostmask') |
---|
702 | n/a | if x is None: |
---|
703 | n/a | x = self._address_class(int(self.netmask) ^ self._ALL_ONES) |
---|
704 | n/a | self._cache['hostmask'] = x |
---|
705 | n/a | return x |
---|
706 | n/a | |
---|
707 | n/a | @property |
---|
708 | n/a | def with_prefixlen(self): |
---|
709 | n/a | return '%s/%d' % (self.network_address, self._prefixlen) |
---|
710 | n/a | |
---|
711 | n/a | @property |
---|
712 | n/a | def with_netmask(self): |
---|
713 | n/a | return '%s/%s' % (self.network_address, self.netmask) |
---|
714 | n/a | |
---|
715 | n/a | @property |
---|
716 | n/a | def with_hostmask(self): |
---|
717 | n/a | return '%s/%s' % (self.network_address, self.hostmask) |
---|
718 | n/a | |
---|
719 | n/a | @property |
---|
720 | n/a | def num_addresses(self): |
---|
721 | n/a | """Number of hosts in the current subnet.""" |
---|
722 | n/a | return int(self.broadcast_address) - int(self.network_address) + 1 |
---|
723 | n/a | |
---|
724 | n/a | @property |
---|
725 | n/a | def _address_class(self): |
---|
726 | n/a | # Returning bare address objects (rather than interfaces) allows for |
---|
727 | n/a | # more consistent behaviour across the network address, broadcast |
---|
728 | n/a | # address and individual host addresses. |
---|
729 | n/a | msg = '%200s has no associated address class' % (type(self),) |
---|
730 | n/a | raise NotImplementedError(msg) |
---|
731 | n/a | |
---|
732 | n/a | @property |
---|
733 | n/a | def prefixlen(self): |
---|
734 | n/a | return self._prefixlen |
---|
735 | n/a | |
---|
736 | n/a | def address_exclude(self, other): |
---|
737 | n/a | """Remove an address from a larger block. |
---|
738 | n/a | |
---|
739 | n/a | For example: |
---|
740 | n/a | |
---|
741 | n/a | addr1 = ip_network('192.0.2.0/28') |
---|
742 | n/a | addr2 = ip_network('192.0.2.1/32') |
---|
743 | n/a | list(addr1.address_exclude(addr2)) = |
---|
744 | n/a | [IPv4Network('192.0.2.0/32'), IPv4Network('192.0.2.2/31'), |
---|
745 | n/a | IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.8/29')] |
---|
746 | n/a | |
---|
747 | n/a | or IPv6: |
---|
748 | n/a | |
---|
749 | n/a | addr1 = ip_network('2001:db8::1/32') |
---|
750 | n/a | addr2 = ip_network('2001:db8::1/128') |
---|
751 | n/a | list(addr1.address_exclude(addr2)) = |
---|
752 | n/a | [ip_network('2001:db8::1/128'), |
---|
753 | n/a | ip_network('2001:db8::2/127'), |
---|
754 | n/a | ip_network('2001:db8::4/126'), |
---|
755 | n/a | ip_network('2001:db8::8/125'), |
---|
756 | n/a | ... |
---|
757 | n/a | ip_network('2001:db8:8000::/33')] |
---|
758 | n/a | |
---|
759 | n/a | Args: |
---|
760 | n/a | other: An IPv4Network or IPv6Network object of the same type. |
---|
761 | n/a | |
---|
762 | n/a | Returns: |
---|
763 | n/a | An iterator of the IPv(4|6)Network objects which is self |
---|
764 | n/a | minus other. |
---|
765 | n/a | |
---|
766 | n/a | Raises: |
---|
767 | n/a | TypeError: If self and other are of differing address |
---|
768 | n/a | versions, or if other is not a network object. |
---|
769 | n/a | ValueError: If other is not completely contained by self. |
---|
770 | n/a | |
---|
771 | n/a | """ |
---|
772 | n/a | if not self._version == other._version: |
---|
773 | n/a | raise TypeError("%s and %s are not of the same version" % ( |
---|
774 | n/a | self, other)) |
---|
775 | n/a | |
---|
776 | n/a | if not isinstance(other, _BaseNetwork): |
---|
777 | n/a | raise TypeError("%s is not a network object" % other) |
---|
778 | n/a | |
---|
779 | n/a | if not (other.network_address >= self.network_address and |
---|
780 | n/a | other.broadcast_address <= self.broadcast_address): |
---|
781 | n/a | raise ValueError('%s not contained in %s' % (other, self)) |
---|
782 | n/a | if other == self: |
---|
783 | n/a | return |
---|
784 | n/a | |
---|
785 | n/a | # Make sure we're comparing the network of other. |
---|
786 | n/a | other = other.__class__('%s/%s' % (other.network_address, |
---|
787 | n/a | other.prefixlen)) |
---|
788 | n/a | |
---|
789 | n/a | s1, s2 = self.subnets() |
---|
790 | n/a | while s1 != other and s2 != other: |
---|
791 | n/a | if (other.network_address >= s1.network_address and |
---|
792 | n/a | other.broadcast_address <= s1.broadcast_address): |
---|
793 | n/a | yield s2 |
---|
794 | n/a | s1, s2 = s1.subnets() |
---|
795 | n/a | elif (other.network_address >= s2.network_address and |
---|
796 | n/a | other.broadcast_address <= s2.broadcast_address): |
---|
797 | n/a | yield s1 |
---|
798 | n/a | s1, s2 = s2.subnets() |
---|
799 | n/a | else: |
---|
800 | n/a | # If we got here, there's a bug somewhere. |
---|
801 | n/a | raise AssertionError('Error performing exclusion: ' |
---|
802 | n/a | 's1: %s s2: %s other: %s' % |
---|
803 | n/a | (s1, s2, other)) |
---|
804 | n/a | if s1 == other: |
---|
805 | n/a | yield s2 |
---|
806 | n/a | elif s2 == other: |
---|
807 | n/a | yield s1 |
---|
808 | n/a | else: |
---|
809 | n/a | # If we got here, there's a bug somewhere. |
---|
810 | n/a | raise AssertionError('Error performing exclusion: ' |
---|
811 | n/a | 's1: %s s2: %s other: %s' % |
---|
812 | n/a | (s1, s2, other)) |
---|
813 | n/a | |
---|
814 | n/a | def compare_networks(self, other): |
---|
815 | n/a | """Compare two IP objects. |
---|
816 | n/a | |
---|
817 | n/a | This is only concerned about the comparison of the integer |
---|
818 | n/a | representation of the network addresses. This means that the |
---|
819 | n/a | host bits aren't considered at all in this method. If you want |
---|
820 | n/a | to compare host bits, you can easily enough do a |
---|
821 | n/a | 'HostA._ip < HostB._ip' |
---|
822 | n/a | |
---|
823 | n/a | Args: |
---|
824 | n/a | other: An IP object. |
---|
825 | n/a | |
---|
826 | n/a | Returns: |
---|
827 | n/a | If the IP versions of self and other are the same, returns: |
---|
828 | n/a | |
---|
829 | n/a | -1 if self < other: |
---|
830 | n/a | eg: IPv4Network('192.0.2.0/25') < IPv4Network('192.0.2.128/25') |
---|
831 | n/a | IPv6Network('2001:db8::1000/124') < |
---|
832 | n/a | IPv6Network('2001:db8::2000/124') |
---|
833 | n/a | 0 if self == other |
---|
834 | n/a | eg: IPv4Network('192.0.2.0/24') == IPv4Network('192.0.2.0/24') |
---|
835 | n/a | IPv6Network('2001:db8::1000/124') == |
---|
836 | n/a | IPv6Network('2001:db8::1000/124') |
---|
837 | n/a | 1 if self > other |
---|
838 | n/a | eg: IPv4Network('192.0.2.128/25') > IPv4Network('192.0.2.0/25') |
---|
839 | n/a | IPv6Network('2001:db8::2000/124') > |
---|
840 | n/a | IPv6Network('2001:db8::1000/124') |
---|
841 | n/a | |
---|
842 | n/a | Raises: |
---|
843 | n/a | TypeError if the IP versions are different. |
---|
844 | n/a | |
---|
845 | n/a | """ |
---|
846 | n/a | # does this need to raise a ValueError? |
---|
847 | n/a | if self._version != other._version: |
---|
848 | n/a | raise TypeError('%s and %s are not of the same type' % ( |
---|
849 | n/a | self, other)) |
---|
850 | n/a | # self._version == other._version below here: |
---|
851 | n/a | if self.network_address < other.network_address: |
---|
852 | n/a | return -1 |
---|
853 | n/a | if self.network_address > other.network_address: |
---|
854 | n/a | return 1 |
---|
855 | n/a | # self.network_address == other.network_address below here: |
---|
856 | n/a | if self.netmask < other.netmask: |
---|
857 | n/a | return -1 |
---|
858 | n/a | if self.netmask > other.netmask: |
---|
859 | n/a | return 1 |
---|
860 | n/a | return 0 |
---|
861 | n/a | |
---|
862 | n/a | def _get_networks_key(self): |
---|
863 | n/a | """Network-only key function. |
---|
864 | n/a | |
---|
865 | n/a | Returns an object that identifies this address' network and |
---|
866 | n/a | netmask. This function is a suitable "key" argument for sorted() |
---|
867 | n/a | and list.sort(). |
---|
868 | n/a | |
---|
869 | n/a | """ |
---|
870 | n/a | return (self._version, self.network_address, self.netmask) |
---|
871 | n/a | |
---|
872 | n/a | def subnets(self, prefixlen_diff=1, new_prefix=None): |
---|
873 | n/a | """The subnets which join to make the current subnet. |
---|
874 | n/a | |
---|
875 | n/a | In the case that self contains only one IP |
---|
876 | n/a | (self._prefixlen == 32 for IPv4 or self._prefixlen == 128 |
---|
877 | n/a | for IPv6), yield an iterator with just ourself. |
---|
878 | n/a | |
---|
879 | n/a | Args: |
---|
880 | n/a | prefixlen_diff: An integer, the amount the prefix length |
---|
881 | n/a | should be increased by. This should not be set if |
---|
882 | n/a | new_prefix is also set. |
---|
883 | n/a | new_prefix: The desired new prefix length. This must be a |
---|
884 | n/a | larger number (smaller prefix) than the existing prefix. |
---|
885 | n/a | This should not be set if prefixlen_diff is also set. |
---|
886 | n/a | |
---|
887 | n/a | Returns: |
---|
888 | n/a | An iterator of IPv(4|6) objects. |
---|
889 | n/a | |
---|
890 | n/a | Raises: |
---|
891 | n/a | ValueError: The prefixlen_diff is too small or too large. |
---|
892 | n/a | OR |
---|
893 | n/a | prefixlen_diff and new_prefix are both set or new_prefix |
---|
894 | n/a | is a smaller number than the current prefix (smaller |
---|
895 | n/a | number means a larger network) |
---|
896 | n/a | |
---|
897 | n/a | """ |
---|
898 | n/a | if self._prefixlen == self._max_prefixlen: |
---|
899 | n/a | yield self |
---|
900 | n/a | return |
---|
901 | n/a | |
---|
902 | n/a | if new_prefix is not None: |
---|
903 | n/a | if new_prefix < self._prefixlen: |
---|
904 | n/a | raise ValueError('new prefix must be longer') |
---|
905 | n/a | if prefixlen_diff != 1: |
---|
906 | n/a | raise ValueError('cannot set prefixlen_diff and new_prefix') |
---|
907 | n/a | prefixlen_diff = new_prefix - self._prefixlen |
---|
908 | n/a | |
---|
909 | n/a | if prefixlen_diff < 0: |
---|
910 | n/a | raise ValueError('prefix length diff must be > 0') |
---|
911 | n/a | new_prefixlen = self._prefixlen + prefixlen_diff |
---|
912 | n/a | |
---|
913 | n/a | if new_prefixlen > self._max_prefixlen: |
---|
914 | n/a | raise ValueError( |
---|
915 | n/a | 'prefix length diff %d is invalid for netblock %s' % ( |
---|
916 | n/a | new_prefixlen, self)) |
---|
917 | n/a | |
---|
918 | n/a | start = int(self.network_address) |
---|
919 | n/a | end = int(self.broadcast_address) + 1 |
---|
920 | n/a | step = (int(self.hostmask) + 1) >> prefixlen_diff |
---|
921 | n/a | for new_addr in range(start, end, step): |
---|
922 | n/a | current = self.__class__((new_addr, new_prefixlen)) |
---|
923 | n/a | yield current |
---|
924 | n/a | |
---|
925 | n/a | def supernet(self, prefixlen_diff=1, new_prefix=None): |
---|
926 | n/a | """The supernet containing the current network. |
---|
927 | n/a | |
---|
928 | n/a | Args: |
---|
929 | n/a | prefixlen_diff: An integer, the amount the prefix length of |
---|
930 | n/a | the network should be decreased by. For example, given a |
---|
931 | n/a | /24 network and a prefixlen_diff of 3, a supernet with a |
---|
932 | n/a | /21 netmask is returned. |
---|
933 | n/a | |
---|
934 | n/a | Returns: |
---|
935 | n/a | An IPv4 network object. |
---|
936 | n/a | |
---|
937 | n/a | Raises: |
---|
938 | n/a | ValueError: If self.prefixlen - prefixlen_diff < 0. I.e., you have |
---|
939 | n/a | a negative prefix length. |
---|
940 | n/a | OR |
---|
941 | n/a | If prefixlen_diff and new_prefix are both set or new_prefix is a |
---|
942 | n/a | larger number than the current prefix (larger number means a |
---|
943 | n/a | smaller network) |
---|
944 | n/a | |
---|
945 | n/a | """ |
---|
946 | n/a | if self._prefixlen == 0: |
---|
947 | n/a | return self |
---|
948 | n/a | |
---|
949 | n/a | if new_prefix is not None: |
---|
950 | n/a | if new_prefix > self._prefixlen: |
---|
951 | n/a | raise ValueError('new prefix must be shorter') |
---|
952 | n/a | if prefixlen_diff != 1: |
---|
953 | n/a | raise ValueError('cannot set prefixlen_diff and new_prefix') |
---|
954 | n/a | prefixlen_diff = self._prefixlen - new_prefix |
---|
955 | n/a | |
---|
956 | n/a | new_prefixlen = self.prefixlen - prefixlen_diff |
---|
957 | n/a | if new_prefixlen < 0: |
---|
958 | n/a | raise ValueError( |
---|
959 | n/a | 'current prefixlen is %d, cannot have a prefixlen_diff of %d' % |
---|
960 | n/a | (self.prefixlen, prefixlen_diff)) |
---|
961 | n/a | return self.__class__(( |
---|
962 | n/a | int(self.network_address) & (int(self.netmask) << prefixlen_diff), |
---|
963 | n/a | new_prefixlen |
---|
964 | n/a | )) |
---|
965 | n/a | |
---|
966 | n/a | @property |
---|
967 | n/a | def is_multicast(self): |
---|
968 | n/a | """Test if the address is reserved for multicast use. |
---|
969 | n/a | |
---|
970 | n/a | Returns: |
---|
971 | n/a | A boolean, True if the address is a multicast address. |
---|
972 | n/a | See RFC 2373 2.7 for details. |
---|
973 | n/a | |
---|
974 | n/a | """ |
---|
975 | n/a | return (self.network_address.is_multicast and |
---|
976 | n/a | self.broadcast_address.is_multicast) |
---|
977 | n/a | |
---|
978 | n/a | @property |
---|
979 | n/a | def is_reserved(self): |
---|
980 | n/a | """Test if the address is otherwise IETF reserved. |
---|
981 | n/a | |
---|
982 | n/a | Returns: |
---|
983 | n/a | A boolean, True if the address is within one of the |
---|
984 | n/a | reserved IPv6 Network ranges. |
---|
985 | n/a | |
---|
986 | n/a | """ |
---|
987 | n/a | return (self.network_address.is_reserved and |
---|
988 | n/a | self.broadcast_address.is_reserved) |
---|
989 | n/a | |
---|
990 | n/a | @property |
---|
991 | n/a | def is_link_local(self): |
---|
992 | n/a | """Test if the address is reserved for link-local. |
---|
993 | n/a | |
---|
994 | n/a | Returns: |
---|
995 | n/a | A boolean, True if the address is reserved per RFC 4291. |
---|
996 | n/a | |
---|
997 | n/a | """ |
---|
998 | n/a | return (self.network_address.is_link_local and |
---|
999 | n/a | self.broadcast_address.is_link_local) |
---|
1000 | n/a | |
---|
1001 | n/a | @property |
---|
1002 | n/a | def is_private(self): |
---|
1003 | n/a | """Test if this address is allocated for private networks. |
---|
1004 | n/a | |
---|
1005 | n/a | Returns: |
---|
1006 | n/a | A boolean, True if the address is reserved per |
---|
1007 | n/a | iana-ipv4-special-registry or iana-ipv6-special-registry. |
---|
1008 | n/a | |
---|
1009 | n/a | """ |
---|
1010 | n/a | return (self.network_address.is_private and |
---|
1011 | n/a | self.broadcast_address.is_private) |
---|
1012 | n/a | |
---|
1013 | n/a | @property |
---|
1014 | n/a | def is_global(self): |
---|
1015 | n/a | """Test if this address is allocated for public networks. |
---|
1016 | n/a | |
---|
1017 | n/a | Returns: |
---|
1018 | n/a | A boolean, True if the address is not reserved per |
---|
1019 | n/a | iana-ipv4-special-registry or iana-ipv6-special-registry. |
---|
1020 | n/a | |
---|
1021 | n/a | """ |
---|
1022 | n/a | return not self.is_private |
---|
1023 | n/a | |
---|
1024 | n/a | @property |
---|
1025 | n/a | def is_unspecified(self): |
---|
1026 | n/a | """Test if the address is unspecified. |
---|
1027 | n/a | |
---|
1028 | n/a | Returns: |
---|
1029 | n/a | A boolean, True if this is the unspecified address as defined in |
---|
1030 | n/a | RFC 2373 2.5.2. |
---|
1031 | n/a | |
---|
1032 | n/a | """ |
---|
1033 | n/a | return (self.network_address.is_unspecified and |
---|
1034 | n/a | self.broadcast_address.is_unspecified) |
---|
1035 | n/a | |
---|
1036 | n/a | @property |
---|
1037 | n/a | def is_loopback(self): |
---|
1038 | n/a | """Test if the address is a loopback address. |
---|
1039 | n/a | |
---|
1040 | n/a | Returns: |
---|
1041 | n/a | A boolean, True if the address is a loopback address as defined in |
---|
1042 | n/a | RFC 2373 2.5.3. |
---|
1043 | n/a | |
---|
1044 | n/a | """ |
---|
1045 | n/a | return (self.network_address.is_loopback and |
---|
1046 | n/a | self.broadcast_address.is_loopback) |
---|
1047 | n/a | |
---|
1048 | n/a | |
---|
1049 | n/a | class _BaseV4: |
---|
1050 | n/a | |
---|
1051 | n/a | """Base IPv4 object. |
---|
1052 | n/a | |
---|
1053 | n/a | The following methods are used by IPv4 objects in both single IP |
---|
1054 | n/a | addresses and networks. |
---|
1055 | n/a | |
---|
1056 | n/a | """ |
---|
1057 | n/a | |
---|
1058 | n/a | __slots__ = () |
---|
1059 | n/a | _version = 4 |
---|
1060 | n/a | # Equivalent to 255.255.255.255 or 32 bits of 1's. |
---|
1061 | n/a | _ALL_ONES = (2**IPV4LENGTH) - 1 |
---|
1062 | n/a | _DECIMAL_DIGITS = frozenset('0123456789') |
---|
1063 | n/a | |
---|
1064 | n/a | # the valid octets for host and netmasks. only useful for IPv4. |
---|
1065 | n/a | _valid_mask_octets = frozenset({255, 254, 252, 248, 240, 224, 192, 128, 0}) |
---|
1066 | n/a | |
---|
1067 | n/a | _max_prefixlen = IPV4LENGTH |
---|
1068 | n/a | # There are only a handful of valid v4 netmasks, so we cache them all |
---|
1069 | n/a | # when constructed (see _make_netmask()). |
---|
1070 | n/a | _netmask_cache = {} |
---|
1071 | n/a | |
---|
1072 | n/a | def _explode_shorthand_ip_string(self): |
---|
1073 | n/a | return str(self) |
---|
1074 | n/a | |
---|
1075 | n/a | @classmethod |
---|
1076 | n/a | def _make_netmask(cls, arg): |
---|
1077 | n/a | """Make a (netmask, prefix_len) tuple from the given argument. |
---|
1078 | n/a | |
---|
1079 | n/a | Argument can be: |
---|
1080 | n/a | - an integer (the prefix length) |
---|
1081 | n/a | - a string representing the prefix length (e.g. "24") |
---|
1082 | n/a | - a string representing the prefix netmask (e.g. "255.255.255.0") |
---|
1083 | n/a | """ |
---|
1084 | n/a | if arg not in cls._netmask_cache: |
---|
1085 | n/a | if isinstance(arg, int): |
---|
1086 | n/a | prefixlen = arg |
---|
1087 | n/a | else: |
---|
1088 | n/a | try: |
---|
1089 | n/a | # Check for a netmask in prefix length form |
---|
1090 | n/a | prefixlen = cls._prefix_from_prefix_string(arg) |
---|
1091 | n/a | except NetmaskValueError: |
---|
1092 | n/a | # Check for a netmask or hostmask in dotted-quad form. |
---|
1093 | n/a | # This may raise NetmaskValueError. |
---|
1094 | n/a | prefixlen = cls._prefix_from_ip_string(arg) |
---|
1095 | n/a | netmask = IPv4Address(cls._ip_int_from_prefix(prefixlen)) |
---|
1096 | n/a | cls._netmask_cache[arg] = netmask, prefixlen |
---|
1097 | n/a | return cls._netmask_cache[arg] |
---|
1098 | n/a | |
---|
1099 | n/a | @classmethod |
---|
1100 | n/a | def _ip_int_from_string(cls, ip_str): |
---|
1101 | n/a | """Turn the given IP string into an integer for comparison. |
---|
1102 | n/a | |
---|
1103 | n/a | Args: |
---|
1104 | n/a | ip_str: A string, the IP ip_str. |
---|
1105 | n/a | |
---|
1106 | n/a | Returns: |
---|
1107 | n/a | The IP ip_str as an integer. |
---|
1108 | n/a | |
---|
1109 | n/a | Raises: |
---|
1110 | n/a | AddressValueError: if ip_str isn't a valid IPv4 Address. |
---|
1111 | n/a | |
---|
1112 | n/a | """ |
---|
1113 | n/a | if not ip_str: |
---|
1114 | n/a | raise AddressValueError('Address cannot be empty') |
---|
1115 | n/a | |
---|
1116 | n/a | octets = ip_str.split('.') |
---|
1117 | n/a | if len(octets) != 4: |
---|
1118 | n/a | raise AddressValueError("Expected 4 octets in %r" % ip_str) |
---|
1119 | n/a | |
---|
1120 | n/a | try: |
---|
1121 | n/a | return int.from_bytes(map(cls._parse_octet, octets), 'big') |
---|
1122 | n/a | except ValueError as exc: |
---|
1123 | n/a | raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
---|
1124 | n/a | |
---|
1125 | n/a | @classmethod |
---|
1126 | n/a | def _parse_octet(cls, octet_str): |
---|
1127 | n/a | """Convert a decimal octet into an integer. |
---|
1128 | n/a | |
---|
1129 | n/a | Args: |
---|
1130 | n/a | octet_str: A string, the number to parse. |
---|
1131 | n/a | |
---|
1132 | n/a | Returns: |
---|
1133 | n/a | The octet as an integer. |
---|
1134 | n/a | |
---|
1135 | n/a | Raises: |
---|
1136 | n/a | ValueError: if the octet isn't strictly a decimal from [0..255]. |
---|
1137 | n/a | |
---|
1138 | n/a | """ |
---|
1139 | n/a | if not octet_str: |
---|
1140 | n/a | raise ValueError("Empty octet not permitted") |
---|
1141 | n/a | # Whitelist the characters, since int() allows a lot of bizarre stuff. |
---|
1142 | n/a | if not cls._DECIMAL_DIGITS.issuperset(octet_str): |
---|
1143 | n/a | msg = "Only decimal digits permitted in %r" |
---|
1144 | n/a | raise ValueError(msg % octet_str) |
---|
1145 | n/a | # We do the length check second, since the invalid character error |
---|
1146 | n/a | # is likely to be more informative for the user |
---|
1147 | n/a | if len(octet_str) > 3: |
---|
1148 | n/a | msg = "At most 3 characters permitted in %r" |
---|
1149 | n/a | raise ValueError(msg % octet_str) |
---|
1150 | n/a | # Convert to integer (we know digits are legal) |
---|
1151 | n/a | octet_int = int(octet_str, 10) |
---|
1152 | n/a | # Any octets that look like they *might* be written in octal, |
---|
1153 | n/a | # and which don't look exactly the same in both octal and |
---|
1154 | n/a | # decimal are rejected as ambiguous |
---|
1155 | n/a | if octet_int > 7 and octet_str[0] == '0': |
---|
1156 | n/a | msg = "Ambiguous (octal/decimal) value in %r not permitted" |
---|
1157 | n/a | raise ValueError(msg % octet_str) |
---|
1158 | n/a | if octet_int > 255: |
---|
1159 | n/a | raise ValueError("Octet %d (> 255) not permitted" % octet_int) |
---|
1160 | n/a | return octet_int |
---|
1161 | n/a | |
---|
1162 | n/a | @classmethod |
---|
1163 | n/a | def _string_from_ip_int(cls, ip_int): |
---|
1164 | n/a | """Turns a 32-bit integer into dotted decimal notation. |
---|
1165 | n/a | |
---|
1166 | n/a | Args: |
---|
1167 | n/a | ip_int: An integer, the IP address. |
---|
1168 | n/a | |
---|
1169 | n/a | Returns: |
---|
1170 | n/a | The IP address as a string in dotted decimal notation. |
---|
1171 | n/a | |
---|
1172 | n/a | """ |
---|
1173 | n/a | return '.'.join(map(str, ip_int.to_bytes(4, 'big'))) |
---|
1174 | n/a | |
---|
1175 | n/a | def _is_valid_netmask(self, netmask): |
---|
1176 | n/a | """Verify that the netmask is valid. |
---|
1177 | n/a | |
---|
1178 | n/a | Args: |
---|
1179 | n/a | netmask: A string, either a prefix or dotted decimal |
---|
1180 | n/a | netmask. |
---|
1181 | n/a | |
---|
1182 | n/a | Returns: |
---|
1183 | n/a | A boolean, True if the prefix represents a valid IPv4 |
---|
1184 | n/a | netmask. |
---|
1185 | n/a | |
---|
1186 | n/a | """ |
---|
1187 | n/a | mask = netmask.split('.') |
---|
1188 | n/a | if len(mask) == 4: |
---|
1189 | n/a | try: |
---|
1190 | n/a | for x in mask: |
---|
1191 | n/a | if int(x) not in self._valid_mask_octets: |
---|
1192 | n/a | return False |
---|
1193 | n/a | except ValueError: |
---|
1194 | n/a | # Found something that isn't an integer or isn't valid |
---|
1195 | n/a | return False |
---|
1196 | n/a | for idx, y in enumerate(mask): |
---|
1197 | n/a | if idx > 0 and y > mask[idx - 1]: |
---|
1198 | n/a | return False |
---|
1199 | n/a | return True |
---|
1200 | n/a | try: |
---|
1201 | n/a | netmask = int(netmask) |
---|
1202 | n/a | except ValueError: |
---|
1203 | n/a | return False |
---|
1204 | n/a | return 0 <= netmask <= self._max_prefixlen |
---|
1205 | n/a | |
---|
1206 | n/a | def _is_hostmask(self, ip_str): |
---|
1207 | n/a | """Test if the IP string is a hostmask (rather than a netmask). |
---|
1208 | n/a | |
---|
1209 | n/a | Args: |
---|
1210 | n/a | ip_str: A string, the potential hostmask. |
---|
1211 | n/a | |
---|
1212 | n/a | Returns: |
---|
1213 | n/a | A boolean, True if the IP string is a hostmask. |
---|
1214 | n/a | |
---|
1215 | n/a | """ |
---|
1216 | n/a | bits = ip_str.split('.') |
---|
1217 | n/a | try: |
---|
1218 | n/a | parts = [x for x in map(int, bits) if x in self._valid_mask_octets] |
---|
1219 | n/a | except ValueError: |
---|
1220 | n/a | return False |
---|
1221 | n/a | if len(parts) != len(bits): |
---|
1222 | n/a | return False |
---|
1223 | n/a | if parts[0] < parts[-1]: |
---|
1224 | n/a | return True |
---|
1225 | n/a | return False |
---|
1226 | n/a | |
---|
1227 | n/a | def _reverse_pointer(self): |
---|
1228 | n/a | """Return the reverse DNS pointer name for the IPv4 address. |
---|
1229 | n/a | |
---|
1230 | n/a | This implements the method described in RFC1035 3.5. |
---|
1231 | n/a | |
---|
1232 | n/a | """ |
---|
1233 | n/a | reverse_octets = str(self).split('.')[::-1] |
---|
1234 | n/a | return '.'.join(reverse_octets) + '.in-addr.arpa' |
---|
1235 | n/a | |
---|
1236 | n/a | @property |
---|
1237 | n/a | def max_prefixlen(self): |
---|
1238 | n/a | return self._max_prefixlen |
---|
1239 | n/a | |
---|
1240 | n/a | @property |
---|
1241 | n/a | def version(self): |
---|
1242 | n/a | return self._version |
---|
1243 | n/a | |
---|
1244 | n/a | |
---|
1245 | n/a | class IPv4Address(_BaseV4, _BaseAddress): |
---|
1246 | n/a | |
---|
1247 | n/a | """Represent and manipulate single IPv4 Addresses.""" |
---|
1248 | n/a | |
---|
1249 | n/a | __slots__ = ('_ip', '__weakref__') |
---|
1250 | n/a | |
---|
1251 | n/a | def __init__(self, address): |
---|
1252 | n/a | |
---|
1253 | n/a | """ |
---|
1254 | n/a | Args: |
---|
1255 | n/a | address: A string or integer representing the IP |
---|
1256 | n/a | |
---|
1257 | n/a | Additionally, an integer can be passed, so |
---|
1258 | n/a | IPv4Address('192.0.2.1') == IPv4Address(3221225985). |
---|
1259 | n/a | or, more generally |
---|
1260 | n/a | IPv4Address(int(IPv4Address('192.0.2.1'))) == |
---|
1261 | n/a | IPv4Address('192.0.2.1') |
---|
1262 | n/a | |
---|
1263 | n/a | Raises: |
---|
1264 | n/a | AddressValueError: If ipaddress isn't a valid IPv4 address. |
---|
1265 | n/a | |
---|
1266 | n/a | """ |
---|
1267 | n/a | # Efficient constructor from integer. |
---|
1268 | n/a | if isinstance(address, int): |
---|
1269 | n/a | self._check_int_address(address) |
---|
1270 | n/a | self._ip = address |
---|
1271 | n/a | return |
---|
1272 | n/a | |
---|
1273 | n/a | # Constructing from a packed address |
---|
1274 | n/a | if isinstance(address, bytes): |
---|
1275 | n/a | self._check_packed_address(address, 4) |
---|
1276 | n/a | self._ip = int.from_bytes(address, 'big') |
---|
1277 | n/a | return |
---|
1278 | n/a | |
---|
1279 | n/a | # Assume input argument to be string or any object representation |
---|
1280 | n/a | # which converts into a formatted IP string. |
---|
1281 | n/a | addr_str = str(address) |
---|
1282 | n/a | if '/' in addr_str: |
---|
1283 | n/a | raise AddressValueError("Unexpected '/' in %r" % address) |
---|
1284 | n/a | self._ip = self._ip_int_from_string(addr_str) |
---|
1285 | n/a | |
---|
1286 | n/a | @property |
---|
1287 | n/a | def packed(self): |
---|
1288 | n/a | """The binary representation of this address.""" |
---|
1289 | n/a | return v4_int_to_packed(self._ip) |
---|
1290 | n/a | |
---|
1291 | n/a | @property |
---|
1292 | n/a | def is_reserved(self): |
---|
1293 | n/a | """Test if the address is otherwise IETF reserved. |
---|
1294 | n/a | |
---|
1295 | n/a | Returns: |
---|
1296 | n/a | A boolean, True if the address is within the |
---|
1297 | n/a | reserved IPv4 Network range. |
---|
1298 | n/a | |
---|
1299 | n/a | """ |
---|
1300 | n/a | return self in self._constants._reserved_network |
---|
1301 | n/a | |
---|
1302 | n/a | @property |
---|
1303 | n/a | @functools.lru_cache() |
---|
1304 | n/a | def is_private(self): |
---|
1305 | n/a | """Test if this address is allocated for private networks. |
---|
1306 | n/a | |
---|
1307 | n/a | Returns: |
---|
1308 | n/a | A boolean, True if the address is reserved per |
---|
1309 | n/a | iana-ipv4-special-registry. |
---|
1310 | n/a | |
---|
1311 | n/a | """ |
---|
1312 | n/a | return any(self in net for net in self._constants._private_networks) |
---|
1313 | n/a | |
---|
1314 | n/a | @property |
---|
1315 | n/a | @functools.lru_cache() |
---|
1316 | n/a | def is_global(self): |
---|
1317 | n/a | return self not in self._constants._public_network and not self.is_private |
---|
1318 | n/a | |
---|
1319 | n/a | @property |
---|
1320 | n/a | def is_multicast(self): |
---|
1321 | n/a | """Test if the address is reserved for multicast use. |
---|
1322 | n/a | |
---|
1323 | n/a | Returns: |
---|
1324 | n/a | A boolean, True if the address is multicast. |
---|
1325 | n/a | See RFC 3171 for details. |
---|
1326 | n/a | |
---|
1327 | n/a | """ |
---|
1328 | n/a | return self in self._constants._multicast_network |
---|
1329 | n/a | |
---|
1330 | n/a | @property |
---|
1331 | n/a | def is_unspecified(self): |
---|
1332 | n/a | """Test if the address is unspecified. |
---|
1333 | n/a | |
---|
1334 | n/a | Returns: |
---|
1335 | n/a | A boolean, True if this is the unspecified address as defined in |
---|
1336 | n/a | RFC 5735 3. |
---|
1337 | n/a | |
---|
1338 | n/a | """ |
---|
1339 | n/a | return self == self._constants._unspecified_address |
---|
1340 | n/a | |
---|
1341 | n/a | @property |
---|
1342 | n/a | def is_loopback(self): |
---|
1343 | n/a | """Test if the address is a loopback address. |
---|
1344 | n/a | |
---|
1345 | n/a | Returns: |
---|
1346 | n/a | A boolean, True if the address is a loopback per RFC 3330. |
---|
1347 | n/a | |
---|
1348 | n/a | """ |
---|
1349 | n/a | return self in self._constants._loopback_network |
---|
1350 | n/a | |
---|
1351 | n/a | @property |
---|
1352 | n/a | def is_link_local(self): |
---|
1353 | n/a | """Test if the address is reserved for link-local. |
---|
1354 | n/a | |
---|
1355 | n/a | Returns: |
---|
1356 | n/a | A boolean, True if the address is link-local per RFC 3927. |
---|
1357 | n/a | |
---|
1358 | n/a | """ |
---|
1359 | n/a | return self in self._constants._linklocal_network |
---|
1360 | n/a | |
---|
1361 | n/a | |
---|
1362 | n/a | class IPv4Interface(IPv4Address): |
---|
1363 | n/a | |
---|
1364 | n/a | def __init__(self, address): |
---|
1365 | n/a | if isinstance(address, (bytes, int)): |
---|
1366 | n/a | IPv4Address.__init__(self, address) |
---|
1367 | n/a | self.network = IPv4Network(self._ip) |
---|
1368 | n/a | self._prefixlen = self._max_prefixlen |
---|
1369 | n/a | return |
---|
1370 | n/a | |
---|
1371 | n/a | if isinstance(address, tuple): |
---|
1372 | n/a | IPv4Address.__init__(self, address[0]) |
---|
1373 | n/a | if len(address) > 1: |
---|
1374 | n/a | self._prefixlen = int(address[1]) |
---|
1375 | n/a | else: |
---|
1376 | n/a | self._prefixlen = self._max_prefixlen |
---|
1377 | n/a | |
---|
1378 | n/a | self.network = IPv4Network(address, strict=False) |
---|
1379 | n/a | self.netmask = self.network.netmask |
---|
1380 | n/a | self.hostmask = self.network.hostmask |
---|
1381 | n/a | return |
---|
1382 | n/a | |
---|
1383 | n/a | addr = _split_optional_netmask(address) |
---|
1384 | n/a | IPv4Address.__init__(self, addr[0]) |
---|
1385 | n/a | |
---|
1386 | n/a | self.network = IPv4Network(address, strict=False) |
---|
1387 | n/a | self._prefixlen = self.network._prefixlen |
---|
1388 | n/a | |
---|
1389 | n/a | self.netmask = self.network.netmask |
---|
1390 | n/a | self.hostmask = self.network.hostmask |
---|
1391 | n/a | |
---|
1392 | n/a | def __str__(self): |
---|
1393 | n/a | return '%s/%d' % (self._string_from_ip_int(self._ip), |
---|
1394 | n/a | self.network.prefixlen) |
---|
1395 | n/a | |
---|
1396 | n/a | def __eq__(self, other): |
---|
1397 | n/a | address_equal = IPv4Address.__eq__(self, other) |
---|
1398 | n/a | if not address_equal or address_equal is NotImplemented: |
---|
1399 | n/a | return address_equal |
---|
1400 | n/a | try: |
---|
1401 | n/a | return self.network == other.network |
---|
1402 | n/a | except AttributeError: |
---|
1403 | n/a | # An interface with an associated network is NOT the |
---|
1404 | n/a | # same as an unassociated address. That's why the hash |
---|
1405 | n/a | # takes the extra info into account. |
---|
1406 | n/a | return False |
---|
1407 | n/a | |
---|
1408 | n/a | def __lt__(self, other): |
---|
1409 | n/a | address_less = IPv4Address.__lt__(self, other) |
---|
1410 | n/a | if address_less is NotImplemented: |
---|
1411 | n/a | return NotImplemented |
---|
1412 | n/a | try: |
---|
1413 | n/a | return self.network < other.network |
---|
1414 | n/a | except AttributeError: |
---|
1415 | n/a | # We *do* allow addresses and interfaces to be sorted. The |
---|
1416 | n/a | # unassociated address is considered less than all interfaces. |
---|
1417 | n/a | return False |
---|
1418 | n/a | |
---|
1419 | n/a | def __hash__(self): |
---|
1420 | n/a | return self._ip ^ self._prefixlen ^ int(self.network.network_address) |
---|
1421 | n/a | |
---|
1422 | n/a | __reduce__ = _IPAddressBase.__reduce__ |
---|
1423 | n/a | |
---|
1424 | n/a | @property |
---|
1425 | n/a | def ip(self): |
---|
1426 | n/a | return IPv4Address(self._ip) |
---|
1427 | n/a | |
---|
1428 | n/a | @property |
---|
1429 | n/a | def with_prefixlen(self): |
---|
1430 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
1431 | n/a | self._prefixlen) |
---|
1432 | n/a | |
---|
1433 | n/a | @property |
---|
1434 | n/a | def with_netmask(self): |
---|
1435 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
1436 | n/a | self.netmask) |
---|
1437 | n/a | |
---|
1438 | n/a | @property |
---|
1439 | n/a | def with_hostmask(self): |
---|
1440 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
1441 | n/a | self.hostmask) |
---|
1442 | n/a | |
---|
1443 | n/a | |
---|
1444 | n/a | class IPv4Network(_BaseV4, _BaseNetwork): |
---|
1445 | n/a | |
---|
1446 | n/a | """This class represents and manipulates 32-bit IPv4 network + addresses.. |
---|
1447 | n/a | |
---|
1448 | n/a | Attributes: [examples for IPv4Network('192.0.2.0/27')] |
---|
1449 | n/a | .network_address: IPv4Address('192.0.2.0') |
---|
1450 | n/a | .hostmask: IPv4Address('0.0.0.31') |
---|
1451 | n/a | .broadcast_address: IPv4Address('192.0.2.32') |
---|
1452 | n/a | .netmask: IPv4Address('255.255.255.224') |
---|
1453 | n/a | .prefixlen: 27 |
---|
1454 | n/a | |
---|
1455 | n/a | """ |
---|
1456 | n/a | # Class to use when creating address objects |
---|
1457 | n/a | _address_class = IPv4Address |
---|
1458 | n/a | |
---|
1459 | n/a | def __init__(self, address, strict=True): |
---|
1460 | n/a | |
---|
1461 | n/a | """Instantiate a new IPv4 network object. |
---|
1462 | n/a | |
---|
1463 | n/a | Args: |
---|
1464 | n/a | address: A string or integer representing the IP [& network]. |
---|
1465 | n/a | '192.0.2.0/24' |
---|
1466 | n/a | '192.0.2.0/255.255.255.0' |
---|
1467 | n/a | '192.0.0.2/0.0.0.255' |
---|
1468 | n/a | are all functionally the same in IPv4. Similarly, |
---|
1469 | n/a | '192.0.2.1' |
---|
1470 | n/a | '192.0.2.1/255.255.255.255' |
---|
1471 | n/a | '192.0.2.1/32' |
---|
1472 | n/a | are also functionally equivalent. That is to say, failing to |
---|
1473 | n/a | provide a subnetmask will create an object with a mask of /32. |
---|
1474 | n/a | |
---|
1475 | n/a | If the mask (portion after the / in the argument) is given in |
---|
1476 | n/a | dotted quad form, it is treated as a netmask if it starts with a |
---|
1477 | n/a | non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it |
---|
1478 | n/a | starts with a zero field (e.g. 0.255.255.255 == /8), with the |
---|
1479 | n/a | single exception of an all-zero mask which is treated as a |
---|
1480 | n/a | netmask == /0. If no mask is given, a default of /32 is used. |
---|
1481 | n/a | |
---|
1482 | n/a | Additionally, an integer can be passed, so |
---|
1483 | n/a | IPv4Network('192.0.2.1') == IPv4Network(3221225985) |
---|
1484 | n/a | or, more generally |
---|
1485 | n/a | IPv4Interface(int(IPv4Interface('192.0.2.1'))) == |
---|
1486 | n/a | IPv4Interface('192.0.2.1') |
---|
1487 | n/a | |
---|
1488 | n/a | Raises: |
---|
1489 | n/a | AddressValueError: If ipaddress isn't a valid IPv4 address. |
---|
1490 | n/a | NetmaskValueError: If the netmask isn't valid for |
---|
1491 | n/a | an IPv4 address. |
---|
1492 | n/a | ValueError: If strict is True and a network address is not |
---|
1493 | n/a | supplied. |
---|
1494 | n/a | |
---|
1495 | n/a | """ |
---|
1496 | n/a | _BaseNetwork.__init__(self, address) |
---|
1497 | n/a | |
---|
1498 | n/a | # Constructing from a packed address or integer |
---|
1499 | n/a | if isinstance(address, (int, bytes)): |
---|
1500 | n/a | self.network_address = IPv4Address(address) |
---|
1501 | n/a | self.netmask, self._prefixlen = self._make_netmask(self._max_prefixlen) |
---|
1502 | n/a | #fixme: address/network test here. |
---|
1503 | n/a | return |
---|
1504 | n/a | |
---|
1505 | n/a | if isinstance(address, tuple): |
---|
1506 | n/a | if len(address) > 1: |
---|
1507 | n/a | arg = address[1] |
---|
1508 | n/a | else: |
---|
1509 | n/a | # We weren't given an address[1] |
---|
1510 | n/a | arg = self._max_prefixlen |
---|
1511 | n/a | self.network_address = IPv4Address(address[0]) |
---|
1512 | n/a | self.netmask, self._prefixlen = self._make_netmask(arg) |
---|
1513 | n/a | packed = int(self.network_address) |
---|
1514 | n/a | if packed & int(self.netmask) != packed: |
---|
1515 | n/a | if strict: |
---|
1516 | n/a | raise ValueError('%s has host bits set' % self) |
---|
1517 | n/a | else: |
---|
1518 | n/a | self.network_address = IPv4Address(packed & |
---|
1519 | n/a | int(self.netmask)) |
---|
1520 | n/a | return |
---|
1521 | n/a | |
---|
1522 | n/a | # Assume input argument to be string or any object representation |
---|
1523 | n/a | # which converts into a formatted IP prefix string. |
---|
1524 | n/a | addr = _split_optional_netmask(address) |
---|
1525 | n/a | self.network_address = IPv4Address(self._ip_int_from_string(addr[0])) |
---|
1526 | n/a | |
---|
1527 | n/a | if len(addr) == 2: |
---|
1528 | n/a | arg = addr[1] |
---|
1529 | n/a | else: |
---|
1530 | n/a | arg = self._max_prefixlen |
---|
1531 | n/a | self.netmask, self._prefixlen = self._make_netmask(arg) |
---|
1532 | n/a | |
---|
1533 | n/a | if strict: |
---|
1534 | n/a | if (IPv4Address(int(self.network_address) & int(self.netmask)) != |
---|
1535 | n/a | self.network_address): |
---|
1536 | n/a | raise ValueError('%s has host bits set' % self) |
---|
1537 | n/a | self.network_address = IPv4Address(int(self.network_address) & |
---|
1538 | n/a | int(self.netmask)) |
---|
1539 | n/a | |
---|
1540 | n/a | if self._prefixlen == (self._max_prefixlen - 1): |
---|
1541 | n/a | self.hosts = self.__iter__ |
---|
1542 | n/a | |
---|
1543 | n/a | @property |
---|
1544 | n/a | @functools.lru_cache() |
---|
1545 | n/a | def is_global(self): |
---|
1546 | n/a | """Test if this address is allocated for public networks. |
---|
1547 | n/a | |
---|
1548 | n/a | Returns: |
---|
1549 | n/a | A boolean, True if the address is not reserved per |
---|
1550 | n/a | iana-ipv4-special-registry. |
---|
1551 | n/a | |
---|
1552 | n/a | """ |
---|
1553 | n/a | return (not (self.network_address in IPv4Network('100.64.0.0/10') and |
---|
1554 | n/a | self.broadcast_address in IPv4Network('100.64.0.0/10')) and |
---|
1555 | n/a | not self.is_private) |
---|
1556 | n/a | |
---|
1557 | n/a | |
---|
1558 | n/a | class _IPv4Constants: |
---|
1559 | n/a | _linklocal_network = IPv4Network('169.254.0.0/16') |
---|
1560 | n/a | |
---|
1561 | n/a | _loopback_network = IPv4Network('127.0.0.0/8') |
---|
1562 | n/a | |
---|
1563 | n/a | _multicast_network = IPv4Network('224.0.0.0/4') |
---|
1564 | n/a | |
---|
1565 | n/a | _public_network = IPv4Network('100.64.0.0/10') |
---|
1566 | n/a | |
---|
1567 | n/a | _private_networks = [ |
---|
1568 | n/a | IPv4Network('0.0.0.0/8'), |
---|
1569 | n/a | IPv4Network('10.0.0.0/8'), |
---|
1570 | n/a | IPv4Network('127.0.0.0/8'), |
---|
1571 | n/a | IPv4Network('169.254.0.0/16'), |
---|
1572 | n/a | IPv4Network('172.16.0.0/12'), |
---|
1573 | n/a | IPv4Network('192.0.0.0/29'), |
---|
1574 | n/a | IPv4Network('192.0.0.170/31'), |
---|
1575 | n/a | IPv4Network('192.0.2.0/24'), |
---|
1576 | n/a | IPv4Network('192.168.0.0/16'), |
---|
1577 | n/a | IPv4Network('198.18.0.0/15'), |
---|
1578 | n/a | IPv4Network('198.51.100.0/24'), |
---|
1579 | n/a | IPv4Network('203.0.113.0/24'), |
---|
1580 | n/a | IPv4Network('240.0.0.0/4'), |
---|
1581 | n/a | IPv4Network('255.255.255.255/32'), |
---|
1582 | n/a | ] |
---|
1583 | n/a | |
---|
1584 | n/a | _reserved_network = IPv4Network('240.0.0.0/4') |
---|
1585 | n/a | |
---|
1586 | n/a | _unspecified_address = IPv4Address('0.0.0.0') |
---|
1587 | n/a | |
---|
1588 | n/a | |
---|
1589 | n/a | IPv4Address._constants = _IPv4Constants |
---|
1590 | n/a | |
---|
1591 | n/a | |
---|
1592 | n/a | class _BaseV6: |
---|
1593 | n/a | |
---|
1594 | n/a | """Base IPv6 object. |
---|
1595 | n/a | |
---|
1596 | n/a | The following methods are used by IPv6 objects in both single IP |
---|
1597 | n/a | addresses and networks. |
---|
1598 | n/a | |
---|
1599 | n/a | """ |
---|
1600 | n/a | |
---|
1601 | n/a | __slots__ = () |
---|
1602 | n/a | _version = 6 |
---|
1603 | n/a | _ALL_ONES = (2**IPV6LENGTH) - 1 |
---|
1604 | n/a | _HEXTET_COUNT = 8 |
---|
1605 | n/a | _HEX_DIGITS = frozenset('0123456789ABCDEFabcdef') |
---|
1606 | n/a | _max_prefixlen = IPV6LENGTH |
---|
1607 | n/a | |
---|
1608 | n/a | # There are only a bunch of valid v6 netmasks, so we cache them all |
---|
1609 | n/a | # when constructed (see _make_netmask()). |
---|
1610 | n/a | _netmask_cache = {} |
---|
1611 | n/a | |
---|
1612 | n/a | @classmethod |
---|
1613 | n/a | def _make_netmask(cls, arg): |
---|
1614 | n/a | """Make a (netmask, prefix_len) tuple from the given argument. |
---|
1615 | n/a | |
---|
1616 | n/a | Argument can be: |
---|
1617 | n/a | - an integer (the prefix length) |
---|
1618 | n/a | - a string representing the prefix length (e.g. "24") |
---|
1619 | n/a | - a string representing the prefix netmask (e.g. "255.255.255.0") |
---|
1620 | n/a | """ |
---|
1621 | n/a | if arg not in cls._netmask_cache: |
---|
1622 | n/a | if isinstance(arg, int): |
---|
1623 | n/a | prefixlen = arg |
---|
1624 | n/a | else: |
---|
1625 | n/a | prefixlen = cls._prefix_from_prefix_string(arg) |
---|
1626 | n/a | netmask = IPv6Address(cls._ip_int_from_prefix(prefixlen)) |
---|
1627 | n/a | cls._netmask_cache[arg] = netmask, prefixlen |
---|
1628 | n/a | return cls._netmask_cache[arg] |
---|
1629 | n/a | |
---|
1630 | n/a | @classmethod |
---|
1631 | n/a | def _ip_int_from_string(cls, ip_str): |
---|
1632 | n/a | """Turn an IPv6 ip_str into an integer. |
---|
1633 | n/a | |
---|
1634 | n/a | Args: |
---|
1635 | n/a | ip_str: A string, the IPv6 ip_str. |
---|
1636 | n/a | |
---|
1637 | n/a | Returns: |
---|
1638 | n/a | An int, the IPv6 address |
---|
1639 | n/a | |
---|
1640 | n/a | Raises: |
---|
1641 | n/a | AddressValueError: if ip_str isn't a valid IPv6 Address. |
---|
1642 | n/a | |
---|
1643 | n/a | """ |
---|
1644 | n/a | if not ip_str: |
---|
1645 | n/a | raise AddressValueError('Address cannot be empty') |
---|
1646 | n/a | |
---|
1647 | n/a | parts = ip_str.split(':') |
---|
1648 | n/a | |
---|
1649 | n/a | # An IPv6 address needs at least 2 colons (3 parts). |
---|
1650 | n/a | _min_parts = 3 |
---|
1651 | n/a | if len(parts) < _min_parts: |
---|
1652 | n/a | msg = "At least %d parts expected in %r" % (_min_parts, ip_str) |
---|
1653 | n/a | raise AddressValueError(msg) |
---|
1654 | n/a | |
---|
1655 | n/a | # If the address has an IPv4-style suffix, convert it to hexadecimal. |
---|
1656 | n/a | if '.' in parts[-1]: |
---|
1657 | n/a | try: |
---|
1658 | n/a | ipv4_int = IPv4Address(parts.pop())._ip |
---|
1659 | n/a | except AddressValueError as exc: |
---|
1660 | n/a | raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
---|
1661 | n/a | parts.append('%x' % ((ipv4_int >> 16) & 0xFFFF)) |
---|
1662 | n/a | parts.append('%x' % (ipv4_int & 0xFFFF)) |
---|
1663 | n/a | |
---|
1664 | n/a | # An IPv6 address can't have more than 8 colons (9 parts). |
---|
1665 | n/a | # The extra colon comes from using the "::" notation for a single |
---|
1666 | n/a | # leading or trailing zero part. |
---|
1667 | n/a | _max_parts = cls._HEXTET_COUNT + 1 |
---|
1668 | n/a | if len(parts) > _max_parts: |
---|
1669 | n/a | msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str) |
---|
1670 | n/a | raise AddressValueError(msg) |
---|
1671 | n/a | |
---|
1672 | n/a | # Disregarding the endpoints, find '::' with nothing in between. |
---|
1673 | n/a | # This indicates that a run of zeroes has been skipped. |
---|
1674 | n/a | skip_index = None |
---|
1675 | n/a | for i in range(1, len(parts) - 1): |
---|
1676 | n/a | if not parts[i]: |
---|
1677 | n/a | if skip_index is not None: |
---|
1678 | n/a | # Can't have more than one '::' |
---|
1679 | n/a | msg = "At most one '::' permitted in %r" % ip_str |
---|
1680 | n/a | raise AddressValueError(msg) |
---|
1681 | n/a | skip_index = i |
---|
1682 | n/a | |
---|
1683 | n/a | # parts_hi is the number of parts to copy from above/before the '::' |
---|
1684 | n/a | # parts_lo is the number of parts to copy from below/after the '::' |
---|
1685 | n/a | if skip_index is not None: |
---|
1686 | n/a | # If we found a '::', then check if it also covers the endpoints. |
---|
1687 | n/a | parts_hi = skip_index |
---|
1688 | n/a | parts_lo = len(parts) - skip_index - 1 |
---|
1689 | n/a | if not parts[0]: |
---|
1690 | n/a | parts_hi -= 1 |
---|
1691 | n/a | if parts_hi: |
---|
1692 | n/a | msg = "Leading ':' only permitted as part of '::' in %r" |
---|
1693 | n/a | raise AddressValueError(msg % ip_str) # ^: requires ^:: |
---|
1694 | n/a | if not parts[-1]: |
---|
1695 | n/a | parts_lo -= 1 |
---|
1696 | n/a | if parts_lo: |
---|
1697 | n/a | msg = "Trailing ':' only permitted as part of '::' in %r" |
---|
1698 | n/a | raise AddressValueError(msg % ip_str) # :$ requires ::$ |
---|
1699 | n/a | parts_skipped = cls._HEXTET_COUNT - (parts_hi + parts_lo) |
---|
1700 | n/a | if parts_skipped < 1: |
---|
1701 | n/a | msg = "Expected at most %d other parts with '::' in %r" |
---|
1702 | n/a | raise AddressValueError(msg % (cls._HEXTET_COUNT-1, ip_str)) |
---|
1703 | n/a | else: |
---|
1704 | n/a | # Otherwise, allocate the entire address to parts_hi. The |
---|
1705 | n/a | # endpoints could still be empty, but _parse_hextet() will check |
---|
1706 | n/a | # for that. |
---|
1707 | n/a | if len(parts) != cls._HEXTET_COUNT: |
---|
1708 | n/a | msg = "Exactly %d parts expected without '::' in %r" |
---|
1709 | n/a | raise AddressValueError(msg % (cls._HEXTET_COUNT, ip_str)) |
---|
1710 | n/a | if not parts[0]: |
---|
1711 | n/a | msg = "Leading ':' only permitted as part of '::' in %r" |
---|
1712 | n/a | raise AddressValueError(msg % ip_str) # ^: requires ^:: |
---|
1713 | n/a | if not parts[-1]: |
---|
1714 | n/a | msg = "Trailing ':' only permitted as part of '::' in %r" |
---|
1715 | n/a | raise AddressValueError(msg % ip_str) # :$ requires ::$ |
---|
1716 | n/a | parts_hi = len(parts) |
---|
1717 | n/a | parts_lo = 0 |
---|
1718 | n/a | parts_skipped = 0 |
---|
1719 | n/a | |
---|
1720 | n/a | try: |
---|
1721 | n/a | # Now, parse the hextets into a 128-bit integer. |
---|
1722 | n/a | ip_int = 0 |
---|
1723 | n/a | for i in range(parts_hi): |
---|
1724 | n/a | ip_int <<= 16 |
---|
1725 | n/a | ip_int |= cls._parse_hextet(parts[i]) |
---|
1726 | n/a | ip_int <<= 16 * parts_skipped |
---|
1727 | n/a | for i in range(-parts_lo, 0): |
---|
1728 | n/a | ip_int <<= 16 |
---|
1729 | n/a | ip_int |= cls._parse_hextet(parts[i]) |
---|
1730 | n/a | return ip_int |
---|
1731 | n/a | except ValueError as exc: |
---|
1732 | n/a | raise AddressValueError("%s in %r" % (exc, ip_str)) from None |
---|
1733 | n/a | |
---|
1734 | n/a | @classmethod |
---|
1735 | n/a | def _parse_hextet(cls, hextet_str): |
---|
1736 | n/a | """Convert an IPv6 hextet string into an integer. |
---|
1737 | n/a | |
---|
1738 | n/a | Args: |
---|
1739 | n/a | hextet_str: A string, the number to parse. |
---|
1740 | n/a | |
---|
1741 | n/a | Returns: |
---|
1742 | n/a | The hextet as an integer. |
---|
1743 | n/a | |
---|
1744 | n/a | Raises: |
---|
1745 | n/a | ValueError: if the input isn't strictly a hex number from |
---|
1746 | n/a | [0..FFFF]. |
---|
1747 | n/a | |
---|
1748 | n/a | """ |
---|
1749 | n/a | # Whitelist the characters, since int() allows a lot of bizarre stuff. |
---|
1750 | n/a | if not cls._HEX_DIGITS.issuperset(hextet_str): |
---|
1751 | n/a | raise ValueError("Only hex digits permitted in %r" % hextet_str) |
---|
1752 | n/a | # We do the length check second, since the invalid character error |
---|
1753 | n/a | # is likely to be more informative for the user |
---|
1754 | n/a | if len(hextet_str) > 4: |
---|
1755 | n/a | msg = "At most 4 characters permitted in %r" |
---|
1756 | n/a | raise ValueError(msg % hextet_str) |
---|
1757 | n/a | # Length check means we can skip checking the integer value |
---|
1758 | n/a | return int(hextet_str, 16) |
---|
1759 | n/a | |
---|
1760 | n/a | @classmethod |
---|
1761 | n/a | def _compress_hextets(cls, hextets): |
---|
1762 | n/a | """Compresses a list of hextets. |
---|
1763 | n/a | |
---|
1764 | n/a | Compresses a list of strings, replacing the longest continuous |
---|
1765 | n/a | sequence of "0" in the list with "" and adding empty strings at |
---|
1766 | n/a | the beginning or at the end of the string such that subsequently |
---|
1767 | n/a | calling ":".join(hextets) will produce the compressed version of |
---|
1768 | n/a | the IPv6 address. |
---|
1769 | n/a | |
---|
1770 | n/a | Args: |
---|
1771 | n/a | hextets: A list of strings, the hextets to compress. |
---|
1772 | n/a | |
---|
1773 | n/a | Returns: |
---|
1774 | n/a | A list of strings. |
---|
1775 | n/a | |
---|
1776 | n/a | """ |
---|
1777 | n/a | best_doublecolon_start = -1 |
---|
1778 | n/a | best_doublecolon_len = 0 |
---|
1779 | n/a | doublecolon_start = -1 |
---|
1780 | n/a | doublecolon_len = 0 |
---|
1781 | n/a | for index, hextet in enumerate(hextets): |
---|
1782 | n/a | if hextet == '0': |
---|
1783 | n/a | doublecolon_len += 1 |
---|
1784 | n/a | if doublecolon_start == -1: |
---|
1785 | n/a | # Start of a sequence of zeros. |
---|
1786 | n/a | doublecolon_start = index |
---|
1787 | n/a | if doublecolon_len > best_doublecolon_len: |
---|
1788 | n/a | # This is the longest sequence of zeros so far. |
---|
1789 | n/a | best_doublecolon_len = doublecolon_len |
---|
1790 | n/a | best_doublecolon_start = doublecolon_start |
---|
1791 | n/a | else: |
---|
1792 | n/a | doublecolon_len = 0 |
---|
1793 | n/a | doublecolon_start = -1 |
---|
1794 | n/a | |
---|
1795 | n/a | if best_doublecolon_len > 1: |
---|
1796 | n/a | best_doublecolon_end = (best_doublecolon_start + |
---|
1797 | n/a | best_doublecolon_len) |
---|
1798 | n/a | # For zeros at the end of the address. |
---|
1799 | n/a | if best_doublecolon_end == len(hextets): |
---|
1800 | n/a | hextets += [''] |
---|
1801 | n/a | hextets[best_doublecolon_start:best_doublecolon_end] = [''] |
---|
1802 | n/a | # For zeros at the beginning of the address. |
---|
1803 | n/a | if best_doublecolon_start == 0: |
---|
1804 | n/a | hextets = [''] + hextets |
---|
1805 | n/a | |
---|
1806 | n/a | return hextets |
---|
1807 | n/a | |
---|
1808 | n/a | @classmethod |
---|
1809 | n/a | def _string_from_ip_int(cls, ip_int=None): |
---|
1810 | n/a | """Turns a 128-bit integer into hexadecimal notation. |
---|
1811 | n/a | |
---|
1812 | n/a | Args: |
---|
1813 | n/a | ip_int: An integer, the IP address. |
---|
1814 | n/a | |
---|
1815 | n/a | Returns: |
---|
1816 | n/a | A string, the hexadecimal representation of the address. |
---|
1817 | n/a | |
---|
1818 | n/a | Raises: |
---|
1819 | n/a | ValueError: The address is bigger than 128 bits of all ones. |
---|
1820 | n/a | |
---|
1821 | n/a | """ |
---|
1822 | n/a | if ip_int is None: |
---|
1823 | n/a | ip_int = int(cls._ip) |
---|
1824 | n/a | |
---|
1825 | n/a | if ip_int > cls._ALL_ONES: |
---|
1826 | n/a | raise ValueError('IPv6 address is too large') |
---|
1827 | n/a | |
---|
1828 | n/a | hex_str = '%032x' % ip_int |
---|
1829 | n/a | hextets = ['%x' % int(hex_str[x:x+4], 16) for x in range(0, 32, 4)] |
---|
1830 | n/a | |
---|
1831 | n/a | hextets = cls._compress_hextets(hextets) |
---|
1832 | n/a | return ':'.join(hextets) |
---|
1833 | n/a | |
---|
1834 | n/a | def _explode_shorthand_ip_string(self): |
---|
1835 | n/a | """Expand a shortened IPv6 address. |
---|
1836 | n/a | |
---|
1837 | n/a | Args: |
---|
1838 | n/a | ip_str: A string, the IPv6 address. |
---|
1839 | n/a | |
---|
1840 | n/a | Returns: |
---|
1841 | n/a | A string, the expanded IPv6 address. |
---|
1842 | n/a | |
---|
1843 | n/a | """ |
---|
1844 | n/a | if isinstance(self, IPv6Network): |
---|
1845 | n/a | ip_str = str(self.network_address) |
---|
1846 | n/a | elif isinstance(self, IPv6Interface): |
---|
1847 | n/a | ip_str = str(self.ip) |
---|
1848 | n/a | else: |
---|
1849 | n/a | ip_str = str(self) |
---|
1850 | n/a | |
---|
1851 | n/a | ip_int = self._ip_int_from_string(ip_str) |
---|
1852 | n/a | hex_str = '%032x' % ip_int |
---|
1853 | n/a | parts = [hex_str[x:x+4] for x in range(0, 32, 4)] |
---|
1854 | n/a | if isinstance(self, (_BaseNetwork, IPv6Interface)): |
---|
1855 | n/a | return '%s/%d' % (':'.join(parts), self._prefixlen) |
---|
1856 | n/a | return ':'.join(parts) |
---|
1857 | n/a | |
---|
1858 | n/a | def _reverse_pointer(self): |
---|
1859 | n/a | """Return the reverse DNS pointer name for the IPv6 address. |
---|
1860 | n/a | |
---|
1861 | n/a | This implements the method described in RFC3596 2.5. |
---|
1862 | n/a | |
---|
1863 | n/a | """ |
---|
1864 | n/a | reverse_chars = self.exploded[::-1].replace(':', '') |
---|
1865 | n/a | return '.'.join(reverse_chars) + '.ip6.arpa' |
---|
1866 | n/a | |
---|
1867 | n/a | @property |
---|
1868 | n/a | def max_prefixlen(self): |
---|
1869 | n/a | return self._max_prefixlen |
---|
1870 | n/a | |
---|
1871 | n/a | @property |
---|
1872 | n/a | def version(self): |
---|
1873 | n/a | return self._version |
---|
1874 | n/a | |
---|
1875 | n/a | |
---|
1876 | n/a | class IPv6Address(_BaseV6, _BaseAddress): |
---|
1877 | n/a | |
---|
1878 | n/a | """Represent and manipulate single IPv6 Addresses.""" |
---|
1879 | n/a | |
---|
1880 | n/a | __slots__ = ('_ip', '__weakref__') |
---|
1881 | n/a | |
---|
1882 | n/a | def __init__(self, address): |
---|
1883 | n/a | """Instantiate a new IPv6 address object. |
---|
1884 | n/a | |
---|
1885 | n/a | Args: |
---|
1886 | n/a | address: A string or integer representing the IP |
---|
1887 | n/a | |
---|
1888 | n/a | Additionally, an integer can be passed, so |
---|
1889 | n/a | IPv6Address('2001:db8::') == |
---|
1890 | n/a | IPv6Address(42540766411282592856903984951653826560) |
---|
1891 | n/a | or, more generally |
---|
1892 | n/a | IPv6Address(int(IPv6Address('2001:db8::'))) == |
---|
1893 | n/a | IPv6Address('2001:db8::') |
---|
1894 | n/a | |
---|
1895 | n/a | Raises: |
---|
1896 | n/a | AddressValueError: If address isn't a valid IPv6 address. |
---|
1897 | n/a | |
---|
1898 | n/a | """ |
---|
1899 | n/a | # Efficient constructor from integer. |
---|
1900 | n/a | if isinstance(address, int): |
---|
1901 | n/a | self._check_int_address(address) |
---|
1902 | n/a | self._ip = address |
---|
1903 | n/a | return |
---|
1904 | n/a | |
---|
1905 | n/a | # Constructing from a packed address |
---|
1906 | n/a | if isinstance(address, bytes): |
---|
1907 | n/a | self._check_packed_address(address, 16) |
---|
1908 | n/a | self._ip = int.from_bytes(address, 'big') |
---|
1909 | n/a | return |
---|
1910 | n/a | |
---|
1911 | n/a | # Assume input argument to be string or any object representation |
---|
1912 | n/a | # which converts into a formatted IP string. |
---|
1913 | n/a | addr_str = str(address) |
---|
1914 | n/a | if '/' in addr_str: |
---|
1915 | n/a | raise AddressValueError("Unexpected '/' in %r" % address) |
---|
1916 | n/a | self._ip = self._ip_int_from_string(addr_str) |
---|
1917 | n/a | |
---|
1918 | n/a | @property |
---|
1919 | n/a | def packed(self): |
---|
1920 | n/a | """The binary representation of this address.""" |
---|
1921 | n/a | return v6_int_to_packed(self._ip) |
---|
1922 | n/a | |
---|
1923 | n/a | @property |
---|
1924 | n/a | def is_multicast(self): |
---|
1925 | n/a | """Test if the address is reserved for multicast use. |
---|
1926 | n/a | |
---|
1927 | n/a | Returns: |
---|
1928 | n/a | A boolean, True if the address is a multicast address. |
---|
1929 | n/a | See RFC 2373 2.7 for details. |
---|
1930 | n/a | |
---|
1931 | n/a | """ |
---|
1932 | n/a | return self in self._constants._multicast_network |
---|
1933 | n/a | |
---|
1934 | n/a | @property |
---|
1935 | n/a | def is_reserved(self): |
---|
1936 | n/a | """Test if the address is otherwise IETF reserved. |
---|
1937 | n/a | |
---|
1938 | n/a | Returns: |
---|
1939 | n/a | A boolean, True if the address is within one of the |
---|
1940 | n/a | reserved IPv6 Network ranges. |
---|
1941 | n/a | |
---|
1942 | n/a | """ |
---|
1943 | n/a | return any(self in x for x in self._constants._reserved_networks) |
---|
1944 | n/a | |
---|
1945 | n/a | @property |
---|
1946 | n/a | def is_link_local(self): |
---|
1947 | n/a | """Test if the address is reserved for link-local. |
---|
1948 | n/a | |
---|
1949 | n/a | Returns: |
---|
1950 | n/a | A boolean, True if the address is reserved per RFC 4291. |
---|
1951 | n/a | |
---|
1952 | n/a | """ |
---|
1953 | n/a | return self in self._constants._linklocal_network |
---|
1954 | n/a | |
---|
1955 | n/a | @property |
---|
1956 | n/a | def is_site_local(self): |
---|
1957 | n/a | """Test if the address is reserved for site-local. |
---|
1958 | n/a | |
---|
1959 | n/a | Note that the site-local address space has been deprecated by RFC 3879. |
---|
1960 | n/a | Use is_private to test if this address is in the space of unique local |
---|
1961 | n/a | addresses as defined by RFC 4193. |
---|
1962 | n/a | |
---|
1963 | n/a | Returns: |
---|
1964 | n/a | A boolean, True if the address is reserved per RFC 3513 2.5.6. |
---|
1965 | n/a | |
---|
1966 | n/a | """ |
---|
1967 | n/a | return self in self._constants._sitelocal_network |
---|
1968 | n/a | |
---|
1969 | n/a | @property |
---|
1970 | n/a | @functools.lru_cache() |
---|
1971 | n/a | def is_private(self): |
---|
1972 | n/a | """Test if this address is allocated for private networks. |
---|
1973 | n/a | |
---|
1974 | n/a | Returns: |
---|
1975 | n/a | A boolean, True if the address is reserved per |
---|
1976 | n/a | iana-ipv6-special-registry. |
---|
1977 | n/a | |
---|
1978 | n/a | """ |
---|
1979 | n/a | return any(self in net for net in self._constants._private_networks) |
---|
1980 | n/a | |
---|
1981 | n/a | @property |
---|
1982 | n/a | def is_global(self): |
---|
1983 | n/a | """Test if this address is allocated for public networks. |
---|
1984 | n/a | |
---|
1985 | n/a | Returns: |
---|
1986 | n/a | A boolean, true if the address is not reserved per |
---|
1987 | n/a | iana-ipv6-special-registry. |
---|
1988 | n/a | |
---|
1989 | n/a | """ |
---|
1990 | n/a | return not self.is_private |
---|
1991 | n/a | |
---|
1992 | n/a | @property |
---|
1993 | n/a | def is_unspecified(self): |
---|
1994 | n/a | """Test if the address is unspecified. |
---|
1995 | n/a | |
---|
1996 | n/a | Returns: |
---|
1997 | n/a | A boolean, True if this is the unspecified address as defined in |
---|
1998 | n/a | RFC 2373 2.5.2. |
---|
1999 | n/a | |
---|
2000 | n/a | """ |
---|
2001 | n/a | return self._ip == 0 |
---|
2002 | n/a | |
---|
2003 | n/a | @property |
---|
2004 | n/a | def is_loopback(self): |
---|
2005 | n/a | """Test if the address is a loopback address. |
---|
2006 | n/a | |
---|
2007 | n/a | Returns: |
---|
2008 | n/a | A boolean, True if the address is a loopback address as defined in |
---|
2009 | n/a | RFC 2373 2.5.3. |
---|
2010 | n/a | |
---|
2011 | n/a | """ |
---|
2012 | n/a | return self._ip == 1 |
---|
2013 | n/a | |
---|
2014 | n/a | @property |
---|
2015 | n/a | def ipv4_mapped(self): |
---|
2016 | n/a | """Return the IPv4 mapped address. |
---|
2017 | n/a | |
---|
2018 | n/a | Returns: |
---|
2019 | n/a | If the IPv6 address is a v4 mapped address, return the |
---|
2020 | n/a | IPv4 mapped address. Return None otherwise. |
---|
2021 | n/a | |
---|
2022 | n/a | """ |
---|
2023 | n/a | if (self._ip >> 32) != 0xFFFF: |
---|
2024 | n/a | return None |
---|
2025 | n/a | return IPv4Address(self._ip & 0xFFFFFFFF) |
---|
2026 | n/a | |
---|
2027 | n/a | @property |
---|
2028 | n/a | def teredo(self): |
---|
2029 | n/a | """Tuple of embedded teredo IPs. |
---|
2030 | n/a | |
---|
2031 | n/a | Returns: |
---|
2032 | n/a | Tuple of the (server, client) IPs or None if the address |
---|
2033 | n/a | doesn't appear to be a teredo address (doesn't start with |
---|
2034 | n/a | 2001::/32) |
---|
2035 | n/a | |
---|
2036 | n/a | """ |
---|
2037 | n/a | if (self._ip >> 96) != 0x20010000: |
---|
2038 | n/a | return None |
---|
2039 | n/a | return (IPv4Address((self._ip >> 64) & 0xFFFFFFFF), |
---|
2040 | n/a | IPv4Address(~self._ip & 0xFFFFFFFF)) |
---|
2041 | n/a | |
---|
2042 | n/a | @property |
---|
2043 | n/a | def sixtofour(self): |
---|
2044 | n/a | """Return the IPv4 6to4 embedded address. |
---|
2045 | n/a | |
---|
2046 | n/a | Returns: |
---|
2047 | n/a | The IPv4 6to4-embedded address if present or None if the |
---|
2048 | n/a | address doesn't appear to contain a 6to4 embedded address. |
---|
2049 | n/a | |
---|
2050 | n/a | """ |
---|
2051 | n/a | if (self._ip >> 112) != 0x2002: |
---|
2052 | n/a | return None |
---|
2053 | n/a | return IPv4Address((self._ip >> 80) & 0xFFFFFFFF) |
---|
2054 | n/a | |
---|
2055 | n/a | |
---|
2056 | n/a | class IPv6Interface(IPv6Address): |
---|
2057 | n/a | |
---|
2058 | n/a | def __init__(self, address): |
---|
2059 | n/a | if isinstance(address, (bytes, int)): |
---|
2060 | n/a | IPv6Address.__init__(self, address) |
---|
2061 | n/a | self.network = IPv6Network(self._ip) |
---|
2062 | n/a | self._prefixlen = self._max_prefixlen |
---|
2063 | n/a | return |
---|
2064 | n/a | if isinstance(address, tuple): |
---|
2065 | n/a | IPv6Address.__init__(self, address[0]) |
---|
2066 | n/a | if len(address) > 1: |
---|
2067 | n/a | self._prefixlen = int(address[1]) |
---|
2068 | n/a | else: |
---|
2069 | n/a | self._prefixlen = self._max_prefixlen |
---|
2070 | n/a | self.network = IPv6Network(address, strict=False) |
---|
2071 | n/a | self.netmask = self.network.netmask |
---|
2072 | n/a | self.hostmask = self.network.hostmask |
---|
2073 | n/a | return |
---|
2074 | n/a | |
---|
2075 | n/a | addr = _split_optional_netmask(address) |
---|
2076 | n/a | IPv6Address.__init__(self, addr[0]) |
---|
2077 | n/a | self.network = IPv6Network(address, strict=False) |
---|
2078 | n/a | self.netmask = self.network.netmask |
---|
2079 | n/a | self._prefixlen = self.network._prefixlen |
---|
2080 | n/a | self.hostmask = self.network.hostmask |
---|
2081 | n/a | |
---|
2082 | n/a | def __str__(self): |
---|
2083 | n/a | return '%s/%d' % (self._string_from_ip_int(self._ip), |
---|
2084 | n/a | self.network.prefixlen) |
---|
2085 | n/a | |
---|
2086 | n/a | def __eq__(self, other): |
---|
2087 | n/a | address_equal = IPv6Address.__eq__(self, other) |
---|
2088 | n/a | if not address_equal or address_equal is NotImplemented: |
---|
2089 | n/a | return address_equal |
---|
2090 | n/a | try: |
---|
2091 | n/a | return self.network == other.network |
---|
2092 | n/a | except AttributeError: |
---|
2093 | n/a | # An interface with an associated network is NOT the |
---|
2094 | n/a | # same as an unassociated address. That's why the hash |
---|
2095 | n/a | # takes the extra info into account. |
---|
2096 | n/a | return False |
---|
2097 | n/a | |
---|
2098 | n/a | def __lt__(self, other): |
---|
2099 | n/a | address_less = IPv6Address.__lt__(self, other) |
---|
2100 | n/a | if address_less is NotImplemented: |
---|
2101 | n/a | return NotImplemented |
---|
2102 | n/a | try: |
---|
2103 | n/a | return self.network < other.network |
---|
2104 | n/a | except AttributeError: |
---|
2105 | n/a | # We *do* allow addresses and interfaces to be sorted. The |
---|
2106 | n/a | # unassociated address is considered less than all interfaces. |
---|
2107 | n/a | return False |
---|
2108 | n/a | |
---|
2109 | n/a | def __hash__(self): |
---|
2110 | n/a | return self._ip ^ self._prefixlen ^ int(self.network.network_address) |
---|
2111 | n/a | |
---|
2112 | n/a | __reduce__ = _IPAddressBase.__reduce__ |
---|
2113 | n/a | |
---|
2114 | n/a | @property |
---|
2115 | n/a | def ip(self): |
---|
2116 | n/a | return IPv6Address(self._ip) |
---|
2117 | n/a | |
---|
2118 | n/a | @property |
---|
2119 | n/a | def with_prefixlen(self): |
---|
2120 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
2121 | n/a | self._prefixlen) |
---|
2122 | n/a | |
---|
2123 | n/a | @property |
---|
2124 | n/a | def with_netmask(self): |
---|
2125 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
2126 | n/a | self.netmask) |
---|
2127 | n/a | |
---|
2128 | n/a | @property |
---|
2129 | n/a | def with_hostmask(self): |
---|
2130 | n/a | return '%s/%s' % (self._string_from_ip_int(self._ip), |
---|
2131 | n/a | self.hostmask) |
---|
2132 | n/a | |
---|
2133 | n/a | @property |
---|
2134 | n/a | def is_unspecified(self): |
---|
2135 | n/a | return self._ip == 0 and self.network.is_unspecified |
---|
2136 | n/a | |
---|
2137 | n/a | @property |
---|
2138 | n/a | def is_loopback(self): |
---|
2139 | n/a | return self._ip == 1 and self.network.is_loopback |
---|
2140 | n/a | |
---|
2141 | n/a | |
---|
2142 | n/a | class IPv6Network(_BaseV6, _BaseNetwork): |
---|
2143 | n/a | |
---|
2144 | n/a | """This class represents and manipulates 128-bit IPv6 networks. |
---|
2145 | n/a | |
---|
2146 | n/a | Attributes: [examples for IPv6('2001:db8::1000/124')] |
---|
2147 | n/a | .network_address: IPv6Address('2001:db8::1000') |
---|
2148 | n/a | .hostmask: IPv6Address('::f') |
---|
2149 | n/a | .broadcast_address: IPv6Address('2001:db8::100f') |
---|
2150 | n/a | .netmask: IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0') |
---|
2151 | n/a | .prefixlen: 124 |
---|
2152 | n/a | |
---|
2153 | n/a | """ |
---|
2154 | n/a | |
---|
2155 | n/a | # Class to use when creating address objects |
---|
2156 | n/a | _address_class = IPv6Address |
---|
2157 | n/a | |
---|
2158 | n/a | def __init__(self, address, strict=True): |
---|
2159 | n/a | """Instantiate a new IPv6 Network object. |
---|
2160 | n/a | |
---|
2161 | n/a | Args: |
---|
2162 | n/a | address: A string or integer representing the IPv6 network or the |
---|
2163 | n/a | IP and prefix/netmask. |
---|
2164 | n/a | '2001:db8::/128' |
---|
2165 | n/a | '2001:db8:0000:0000:0000:0000:0000:0000/128' |
---|
2166 | n/a | '2001:db8::' |
---|
2167 | n/a | are all functionally the same in IPv6. That is to say, |
---|
2168 | n/a | failing to provide a subnetmask will create an object with |
---|
2169 | n/a | a mask of /128. |
---|
2170 | n/a | |
---|
2171 | n/a | Additionally, an integer can be passed, so |
---|
2172 | n/a | IPv6Network('2001:db8::') == |
---|
2173 | n/a | IPv6Network(42540766411282592856903984951653826560) |
---|
2174 | n/a | or, more generally |
---|
2175 | n/a | IPv6Network(int(IPv6Network('2001:db8::'))) == |
---|
2176 | n/a | IPv6Network('2001:db8::') |
---|
2177 | n/a | |
---|
2178 | n/a | strict: A boolean. If true, ensure that we have been passed |
---|
2179 | n/a | A true network address, eg, 2001:db8::1000/124 and not an |
---|
2180 | n/a | IP address on a network, eg, 2001:db8::1/124. |
---|
2181 | n/a | |
---|
2182 | n/a | Raises: |
---|
2183 | n/a | AddressValueError: If address isn't a valid IPv6 address. |
---|
2184 | n/a | NetmaskValueError: If the netmask isn't valid for |
---|
2185 | n/a | an IPv6 address. |
---|
2186 | n/a | ValueError: If strict was True and a network address was not |
---|
2187 | n/a | supplied. |
---|
2188 | n/a | |
---|
2189 | n/a | """ |
---|
2190 | n/a | _BaseNetwork.__init__(self, address) |
---|
2191 | n/a | |
---|
2192 | n/a | # Efficient constructor from integer or packed address |
---|
2193 | n/a | if isinstance(address, (bytes, int)): |
---|
2194 | n/a | self.network_address = IPv6Address(address) |
---|
2195 | n/a | self.netmask, self._prefixlen = self._make_netmask(self._max_prefixlen) |
---|
2196 | n/a | return |
---|
2197 | n/a | |
---|
2198 | n/a | if isinstance(address, tuple): |
---|
2199 | n/a | if len(address) > 1: |
---|
2200 | n/a | arg = address[1] |
---|
2201 | n/a | else: |
---|
2202 | n/a | arg = self._max_prefixlen |
---|
2203 | n/a | self.netmask, self._prefixlen = self._make_netmask(arg) |
---|
2204 | n/a | self.network_address = IPv6Address(address[0]) |
---|
2205 | n/a | packed = int(self.network_address) |
---|
2206 | n/a | if packed & int(self.netmask) != packed: |
---|
2207 | n/a | if strict: |
---|
2208 | n/a | raise ValueError('%s has host bits set' % self) |
---|
2209 | n/a | else: |
---|
2210 | n/a | self.network_address = IPv6Address(packed & |
---|
2211 | n/a | int(self.netmask)) |
---|
2212 | n/a | return |
---|
2213 | n/a | |
---|
2214 | n/a | # Assume input argument to be string or any object representation |
---|
2215 | n/a | # which converts into a formatted IP prefix string. |
---|
2216 | n/a | addr = _split_optional_netmask(address) |
---|
2217 | n/a | |
---|
2218 | n/a | self.network_address = IPv6Address(self._ip_int_from_string(addr[0])) |
---|
2219 | n/a | |
---|
2220 | n/a | if len(addr) == 2: |
---|
2221 | n/a | arg = addr[1] |
---|
2222 | n/a | else: |
---|
2223 | n/a | arg = self._max_prefixlen |
---|
2224 | n/a | self.netmask, self._prefixlen = self._make_netmask(arg) |
---|
2225 | n/a | |
---|
2226 | n/a | if strict: |
---|
2227 | n/a | if (IPv6Address(int(self.network_address) & int(self.netmask)) != |
---|
2228 | n/a | self.network_address): |
---|
2229 | n/a | raise ValueError('%s has host bits set' % self) |
---|
2230 | n/a | self.network_address = IPv6Address(int(self.network_address) & |
---|
2231 | n/a | int(self.netmask)) |
---|
2232 | n/a | |
---|
2233 | n/a | if self._prefixlen == (self._max_prefixlen - 1): |
---|
2234 | n/a | self.hosts = self.__iter__ |
---|
2235 | n/a | |
---|
2236 | n/a | def hosts(self): |
---|
2237 | n/a | """Generate Iterator over usable hosts in a network. |
---|
2238 | n/a | |
---|
2239 | n/a | This is like __iter__ except it doesn't return the |
---|
2240 | n/a | Subnet-Router anycast address. |
---|
2241 | n/a | |
---|
2242 | n/a | """ |
---|
2243 | n/a | network = int(self.network_address) |
---|
2244 | n/a | broadcast = int(self.broadcast_address) |
---|
2245 | n/a | for x in range(network + 1, broadcast + 1): |
---|
2246 | n/a | yield self._address_class(x) |
---|
2247 | n/a | |
---|
2248 | n/a | @property |
---|
2249 | n/a | def is_site_local(self): |
---|
2250 | n/a | """Test if the address is reserved for site-local. |
---|
2251 | n/a | |
---|
2252 | n/a | Note that the site-local address space has been deprecated by RFC 3879. |
---|
2253 | n/a | Use is_private to test if this address is in the space of unique local |
---|
2254 | n/a | addresses as defined by RFC 4193. |
---|
2255 | n/a | |
---|
2256 | n/a | Returns: |
---|
2257 | n/a | A boolean, True if the address is reserved per RFC 3513 2.5.6. |
---|
2258 | n/a | |
---|
2259 | n/a | """ |
---|
2260 | n/a | return (self.network_address.is_site_local and |
---|
2261 | n/a | self.broadcast_address.is_site_local) |
---|
2262 | n/a | |
---|
2263 | n/a | |
---|
2264 | n/a | class _IPv6Constants: |
---|
2265 | n/a | |
---|
2266 | n/a | _linklocal_network = IPv6Network('fe80::/10') |
---|
2267 | n/a | |
---|
2268 | n/a | _multicast_network = IPv6Network('ff00::/8') |
---|
2269 | n/a | |
---|
2270 | n/a | _private_networks = [ |
---|
2271 | n/a | IPv6Network('::1/128'), |
---|
2272 | n/a | IPv6Network('::/128'), |
---|
2273 | n/a | IPv6Network('::ffff:0:0/96'), |
---|
2274 | n/a | IPv6Network('100::/64'), |
---|
2275 | n/a | IPv6Network('2001::/23'), |
---|
2276 | n/a | IPv6Network('2001:2::/48'), |
---|
2277 | n/a | IPv6Network('2001:db8::/32'), |
---|
2278 | n/a | IPv6Network('2001:10::/28'), |
---|
2279 | n/a | IPv6Network('fc00::/7'), |
---|
2280 | n/a | IPv6Network('fe80::/10'), |
---|
2281 | n/a | ] |
---|
2282 | n/a | |
---|
2283 | n/a | _reserved_networks = [ |
---|
2284 | n/a | IPv6Network('::/8'), IPv6Network('100::/8'), |
---|
2285 | n/a | IPv6Network('200::/7'), IPv6Network('400::/6'), |
---|
2286 | n/a | IPv6Network('800::/5'), IPv6Network('1000::/4'), |
---|
2287 | n/a | IPv6Network('4000::/3'), IPv6Network('6000::/3'), |
---|
2288 | n/a | IPv6Network('8000::/3'), IPv6Network('A000::/3'), |
---|
2289 | n/a | IPv6Network('C000::/3'), IPv6Network('E000::/4'), |
---|
2290 | n/a | IPv6Network('F000::/5'), IPv6Network('F800::/6'), |
---|
2291 | n/a | IPv6Network('FE00::/9'), |
---|
2292 | n/a | ] |
---|
2293 | n/a | |
---|
2294 | n/a | _sitelocal_network = IPv6Network('fec0::/10') |
---|
2295 | n/a | |
---|
2296 | n/a | |
---|
2297 | n/a | IPv6Address._constants = _IPv6Constants |
---|