2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 /***************************************************************************
21 ip_functions.hxx - description
23 begin : Sat Feb 07 2004
24 copyright : (C) 2004 by Intra2net AG
25 ***************************************************************************/
36 #include <ip_type.hxx>
40 friend IP_RANGE operator+(const IP_RANGE &base, const int &ips);
41 friend bool operator==(const IP_RANGE& a, const IP_RANGE& b);
42 friend bool operator!=(const IP_RANGE& a, const IP_RANGE& b);
43 friend bool operator<(const IP_RANGE& a, const IP_RANGE& b);
52 // the constructors throw runtime_error if the ip is invalid
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; }
57 IP_RANGE(const char* ip)
59 IP_RANGE(const std::string& ip)
61 IP_RANGE(ip_type::type t, const std::string& ip, const std::string& mask_or_end="")
62 { load(t,ip,mask_or_end); }
63 IP_RANGE(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0)
64 { load(t,ip,mask_or_end); }
66 IP_RANGE& operator=(const IP_RANGE& other);
67 void swap(IP_RANGE& other);
69 void load(const std::string& ip); // can decode IP-IP (as range) and IP/MASK (as network)
70 void load(ip_type::type t, const std::string& ip, const std::string& mask_or_end="");
71 void load(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0);
73 bool is_within(const IP_RANGE& a) const;
74 bool overlapping(const IP_RANGE& a) const;
76 bool netmap(const IP_RANGE& original_net, const IP_RANGE& target_net);
78 int operator-(const IP_RANGE &other);
80 // returns the complete IP_RANGE
81 std::string to_string(bool always_mask=false, bool cidr_only=false) const;
83 // returns the complete range in cidr blocks
84 std::vector<IP_RANGE> to_cidr(void) const;
86 // network<->IP subtraction
87 std::vector<IP_RANGE> substract(const std::set<IP_RANGE> &to_substract) const;
89 unsigned int get_mask_bits() const
90 { return calc_netmask_bits(mask); }
91 unsigned int get_broadcast() const
93 unsigned int get_base() const
95 ip_type::type get_type() const
97 unsigned int get_netmask() const;
99 // static IP utility functions
100 static unsigned int turn_ip(unsigned int src);
101 static unsigned int calc_netmask_bits(unsigned int mask);
102 static unsigned int calc_netmask_from_cidr(unsigned int cidr);
103 static std::string ip_string(unsigned int ip);
104 static std::string ip_num_string(unsigned int ip);
105 static std::string resolve_ip(const std::string &iporname, const bool enable_ipv6=false);
108 class dns_exception : public std::runtime_error
111 dns_exception(const std::string &what)
112 : std::runtime_error(what)
116 // DEPRECATED!!! use IP_RANGE instead
117 inline unsigned ipfunc_turn_ip(unsigned int src)
119 return IP_RANGE::turn_ip(src);
122 // DEPRECATED!!! use IP_RANGE instead
123 inline unsigned int ipfunc_netmask2cidr(unsigned int netmask)
125 return IP_RANGE::calc_netmask_bits(netmask);
128 // DEPRECATED!!! use IP_RANGE instead
129 inline std::string ipfunc_format_ip(unsigned int ip)
131 return IP_RANGE::ip_string(IP_RANGE::turn_ip(ip));
134 inline bool operator==(const IP_RANGE& a, const IP_RANGE& b)
136 // == even if comparing ranges and nets possible
137 return (a.ip==b.ip && a.end==b.end);
140 inline bool operator!=(const IP_RANGE& a, const IP_RANGE& b)
142 // == even if comparing ranges and nets possible
143 return !(a.ip==b.ip && a.end==b.end);
146 bool operator<(const IP_RANGE& a, const IP_RANGE& b);
148 // DEPRECATED!!! use IP_RANGE instead
149 // mode 0: get network-base-address, other: broadcast-address
150 inline std::string CalculateNetworkAddresses (int mode, const std::string &ip, const std::string &netmask)
153 return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_base());
155 return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_broadcast());