Commit | Line | Data |
---|---|---|
3454dce4 SE |
1 | # ipaddr.py -- handle IP addresses and set of IP addresses. |
2 | # Copyright (C) 1996-2000 Cendio Systems AB | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 2 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | ||
18 | """IP address manipulation. | |
19 | ||
20 | This module is useful if you need to manipulate IP addresses or sets | |
21 | of IP addresses. | |
22 | ||
23 | The main classes are: | |
24 | ||
25 | ipaddr -- a single IP address. | |
26 | netmask -- a netmask. | |
27 | network -- an IP address/netmask combination. It is often, but | |
28 | not always, better to use the ip_set class instead. | |
29 | ip_set -- a set of IP addresses, that may or may not be adjacent. | |
30 | ||
31 | So, what can you do with this module? As a simple example of the kind | |
32 | of things this module can do, this code computes the set of all IP | |
33 | addresses except 127.0.0.0/8 and prints it, expressed as a union of | |
34 | network/netmask pairs. | |
35 | ||
36 | import ipaddr | |
37 | ||
38 | s = ipaddr.ip_set() | |
39 | s.add_network(ipaddr.network('127.0.0.0', '255.0.0.0', | |
40 | ipaddr.DEMAND_FILTER)) | |
41 | for nw in s.complement().as_list_of_networks(): | |
42 | print nw.ip_str() + '/' + nw.mask.netmask_bits_str | |
43 | ||
44 | Errors are reported by raising an exception from the following | |
45 | exception hierarcy: | |
46 | ||
47 | Exception # The standard Python base exception class. | |
48 | | | |
49 | +-- BadType # Only raised if the programmer makes an error. | |
50 | +-- IpError # Base class for errors that depend on the data. | |
51 | | | |
52 | +-- SetNotRepresentable | |
53 | +-- BrokenIpAddress | |
54 | | | | |
55 | | +-- PartNegative | |
56 | | +-- PartOverflow | |
57 | | | |
58 | +-- BrokenNetmask | |
59 | | | | |
60 | | +-- NeedOneBit | |
61 | | +-- NeedMoreBits | |
62 | | +-- NeedLessBits | |
63 | | | |
64 | +-- BrokenNetwork | |
65 | | | |
66 | +-- EmptyIpAddress | |
67 | +-- EmptyNetmask | |
68 | +-- BrokenNetworkAddress | |
69 | +-- NetworkAddressClash | |
70 | +-- BroadcastAddressClash | |
71 | ||
72 | BadType may be raised at any time if the programmer makes an error | |
73 | (such as passing a dictionary to a function that expects a string). | |
74 | SetNotRepresentable may be raised by ip_set.as_str_range(). All other | |
75 | exceptions are raised from the constructors and helper functions only. | |
76 | ||
77 | The following constants are present in this module: | |
78 | ||
79 | DEMAND_NONE See class network. | |
80 | DEMAND_FILTER See class network. | |
81 | DEMAND_NETWORK See class network. | |
82 | DEMAND_INTERFACE See class network. | |
83 | ||
84 | hostmask A netmask object with all 32 bits set. | |
85 | complete_network A network object representing all IP addresses. | |
86 | complete_set An ip_set object representing all IP addresses. | |
87 | broadcast_network A network object representing 255.255.255.255. | |
88 | broadcast_set An ip_set object representing 255.255.255.255. | |
89 | ||
90 | The as_ipaddr function can be used when you have an object that you | |
91 | know are an ipaddr or network, and you want to get the ipaddr part. | |
92 | ||
93 | All the other functions in this module are internal helper functions, | |
94 | and they should not be used. | |
95 | ||
96 | The internal representation used for IP addresses is currently a long | |
97 | number. That may change in the future, so where the internal | |
98 | representation is visible, you should do nothing with it except | |
99 | compare it to None. | |
100 | ||
101 | This module was developed by Cendio Systems AB for use in the Fuego | |
102 | Firewall. Bug reports can be sent to Per Cederqvist <ceder@cendio.se> | |
103 | who is currently acting as maintainer for this module. | |
104 | ||
105 | Brief history: | |
106 | 1997-03-11 Module created, and used internally. | |
107 | 2000-03-09 1.0: First non-public beta release outside of Cendio Systems. | |
108 | 2000-03-17 1.1: First public release under the GNU GPL license. | |
109 | ||
110 | """ | |
111 | ||
112 | ||
113 | import copy | |
114 | import string | |
115 | import types | |
116 | ||
117 | # The error messages are marked with a call to this function, so that | |
118 | # they can easily be found and translated. | |
119 | def _(s): | |
120 | return s | |
121 | ||
122 | # The exception hierarchy. | |
123 | class IpError(Exception): | |
124 | """Base class for errors that are cause by errors in input data. | |
125 | """ | |
126 | def __str__(self): | |
127 | return self.format % self.args | |
128 | ||
129 | class SetNotRepresentable(IpError): | |
130 | format = _("The set of IP addresses cannot be represented " | |
131 | "as a single network range") | |
132 | ||
133 | class BrokenIpAddress(IpError): | |
134 | format = _("Felaktigt IP-nummer") | |
135 | ||
136 | class PartNegative(BrokenIpAddress): | |
137 |