Commit | Line | Data |
---|---|---|
7050bac5 | 1 | /* |
0e23f538 TJ |
2 | The software in this package is distributed under the GNU General |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
e93545dd GE |
20 | /*************************************************************************** |
21 | ip_functions.hxx - description | |
22 | ------------------- | |
23 | begin : Sat Feb 07 2004 | |
24 | copyright : (C) 2004 by Intra2net AG | |
e93545dd GE |
25 | ***************************************************************************/ |
26 | ||
27 | #ifndef __IPFUNC_HXX | |
28 | #define __IPFUNC_HXX | |
29 | ||
4d754c46 | 30 | #include <stdexcept> |
e93545dd GE |
31 | #include <string> |
32 | #include <map> | |
33 | #include <set> | |
180ad61f | 34 | #include <vector> |
e93545dd | 35 | |
c24a8a9c | 36 | #include <ip_type.hxx> |
e93545dd | 37 | |
c24a8a9c GE |
38 | class IP_RANGE |
39 | { | |
939b5544 | 40 | friend IP_RANGE operator+(const IP_RANGE &base, const int &ips); |
d9ed3132 | 41 | friend bool operator==(const IP_RANGE& a, const IP_RANGE& b); |
5deaa5d4 | 42 | friend bool operator!=(const IP_RANGE& a, const IP_RANGE& b); |
d9ed3132 GE |
43 | friend bool operator<(const IP_RANGE& a, const IP_RANGE& b); |
44 | ||
c24a8a9c GE |
45 | private: |
46 | unsigned int ip; | |
47 | unsigned int mask; | |
48 | unsigned int end; | |
49 | ip_type::type t; | |
50 | ||
51 | public: | |
52 | // the constructors throw runtime_error if the ip is invalid | |
53 | IP_RANGE() | |
54 | { ip=0; mask=0; end=0; t=ip_type::IP; } | |
55 | IP_RANGE(const IP_RANGE &r) | |
56 | { t=r.t; ip=r.ip; mask=r.mask; end=r.end; } | |
a0105b80 GE |
57 | IP_RANGE(const char* ip) |
58 | { load(ip); } | |
59 | IP_RANGE(const std::string& ip) | |
60 | { load(ip); } | |
c24a8a9c GE |
61 | IP_RANGE(ip_type::type t, const std::string& ip, const std::string& mask_or_end="") |
62 | { load(t,ip,mask_or_end); } | |
a0105b80 GE |
63 | IP_RANGE(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0) |
64 | { load(t,ip,mask_or_end); } | |
bb20d988 GE |
65 | |
66 | IP_RANGE& operator=(const IP_RANGE& other); | |
67 | void swap(IP_RANGE& other); | |
68 | ||
a0105b80 | 69 | void load(const std::string& ip); // can decode IP-IP (as range) and IP/MASK (as network) |
c24a8a9c | 70 | void load(ip_type::type t, const std::string& ip, const std::string& mask_or_end=""); |
a0105b80 | 71 | void load(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0); |
c24a8a9c GE |
72 | |
73 | bool is_within(const IP_RANGE& a) const; | |
74 | bool overlapping(const IP_RANGE& a) const; | |
9141afbd GE |
75 | |
76 | bool netmap(const IP_RANGE& original_net, const IP_RANGE& target_net); | |
c24a8a9c | 77 | |
939b5544 TJ |
78 | int operator-(const IP_RANGE &other); |
79 | ||
c24a8a9c | 80 | // returns the complete IP_RANGE |
2910669d | 81 | std::string to_string(bool always_mask=false, bool cidr_only=false) const; |
a0105b80 GE |
82 | |
83 | // returns the complete range in cidr blocks | |
84 | std::vector<IP_RANGE> to_cidr(void) const; | |
c24a8a9c | 85 | |
63f9b745 TJ |
86 | // network<->IP subtraction |
87 | std::vector<IP_RANGE> substract(const std::set<IP_RANGE> &to_substract) const; | |
88 | ||
c24a8a9c GE |
89 | unsigned int get_mask_bits() const |
90 | { return calc_netmask_bits(mask); } | |
91 | unsigned int get_broadcast() const | |
92 | { return end; } | |
93 | unsigned int get_base() const | |
94 | { return ip; } | |
37060daf GE |
95 | ip_type::type get_type() const |
96 | { return t; } | |
f4e41f93 | 97 | unsigned int get_netmask() const; |
c24a8a9c GE |
98 | |
99 | // static IP utility functions | |
100 | static unsigned int turn_ip(unsigned int src); | |
101 | static unsigned int calc_netmask_bits(unsigned int mask); | |
180ad61f | 102 | static unsigned int calc_netmask_from_cidr(unsigned int cidr); |
c24a8a9c | 103 | static std::string ip_string(unsigned int ip); |
f4e41f93 | 104 | static std::string ip_num_string(unsigned int ip); |
7050bac5 | 105 | static std::string resolve_ip(const std::string &iporname, const bool enable_ipv6=false); |
e9852ad9 TJ |
106 | }; |
107 | ||
108 | class dns_exception : public std::runtime_error | |
109 | { | |
110 | public: | |
111 | dns_exception(const std::string &what) | |
112 | : std::runtime_error(what) | |
113 | {} | |
c24a8a9c GE |
114 | }; |
115 | ||
116 | // DEPRECATED!!! use IP_RANGE instead | |
117 | inline unsigned ipfunc_turn_ip(unsigned int src) | |
118 | { | |
119 | return IP_RANGE::turn_ip(src); | |
120 | } | |
121 | ||
122 | // DEPRECATED!!! use IP_RANGE instead | |
123 | inline unsigned int ipfunc_netmask2cidr(unsigned int netmask) | |
124 | { | |
125 | return IP_RANGE::calc_netmask_bits(netmask); | |
126 | } | |
127 | ||
128 | // DEPRECATED!!! use IP_RANGE instead | |
129 | inline std::string ipfunc_format_ip(unsigned int ip) | |
130 | { | |
7fc56666 | 131 | return IP_RANGE::ip_string(IP_RANGE::turn_ip(ip)); |
c24a8a9c GE |
132 | } |
133 | ||
5deaa5d4 GE |
134 | inline bool operator==(const IP_RANGE& a, const IP_RANGE& b) |
135 | { | |
136 | // == even if comparing ranges and nets possible | |
137 | return (a.ip==b.ip && a.end==b.end); | |
138 | } | |
139 | ||
140 | inline bool operator!=(const IP_RANGE& a, const IP_RANGE& b) | |
141 | { | |
142 | // == even if comparing ranges and nets possible | |
143 | return !(a.ip==b.ip && a.end==b.end); | |
144 | } | |
145 | ||
146 | bool operator<(const IP_RANGE& a, const IP_RANGE& b); | |
147 | ||
c24a8a9c | 148 | // DEPRECATED!!! use IP_RANGE instead |
e93545dd | 149 | // mode 0: get network-base-address, other: broadcast-address |
93cc194d GE |
150 | inline std::string CalculateNetworkAddresses (int mode, const std::string &ip, const std::string &netmask) |
151 | { | |
152 | if (mode==0) | |
153 | return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_base()); | |
154 | else | |
155 | return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_broadcast()); | |
156 | } | |
e93545dd GE |
157 | |
158 | #endif |