Replace inet_aton() with inet_pton() to parse IPs correctly (#8825)
[libi2ncommon] / src / ipfunc.hxx
... / ...
CommitLineData
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
20/***************************************************************************
21 ip_functions.hxx - description
22 -------------------
23 begin : Sat Feb 07 2004
24 copyright : (C) 2004 by Intra2net AG
25 ***************************************************************************/
26
27#ifndef __IPFUNC_HXX
28#define __IPFUNC_HXX
29
30#include <stdexcept>
31#include <string>
32#include <map>
33#include <set>
34#include <vector>
35
36#include <ip_type.hxx>
37
38class IP_RANGE
39{
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);
44
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; }
57 IP_RANGE(const char* ip)
58 { load(ip); }
59 IP_RANGE(const std::string& ip)
60 { load(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); }
65
66 IP_RANGE& operator=(const IP_RANGE& other);
67 void swap(IP_RANGE& other);
68
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);
72
73 bool is_within(const IP_RANGE& a) const;
74 bool overlapping(const IP_RANGE& a) const;
75
76 bool netmap(const IP_RANGE& original_net, const IP_RANGE& target_net);
77
78 int operator-(const IP_RANGE &other);
79
80 // returns the complete IP_RANGE
81 std::string to_string(bool always_mask=false, bool cidr_only=false) const;
82
83 // returns the complete range in cidr blocks
84 std::vector<IP_RANGE> to_cidr(void) const;
85
86 // network<->IP subtraction
87 std::vector<IP_RANGE> substract(const std::set<IP_RANGE> &to_substract) const;
88
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; }
95 ip_type::type get_type() const
96 { return t; }
97 unsigned int get_netmask() const;
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);
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);
106};
107
108class dns_exception : public std::runtime_error
109{
110 public:
111 dns_exception(const std::string &what)
112 : std::runtime_error(what)
113 {}
114};
115
116// DEPRECATED!!! use IP_RANGE instead
117inline unsigned ipfunc_turn_ip(unsigned int src)
118{
119 return IP_RANGE::turn_ip(src);
120}
121
122// DEPRECATED!!! use IP_RANGE instead
123inline unsigned int ipfunc_netmask2cidr(unsigned int netmask)
124{
125 return IP_RANGE::calc_netmask_bits(netmask);
126}
127
128// DEPRECATED!!! use IP_RANGE instead
129inline std::string ipfunc_format_ip(unsigned int ip)
130{
131 return IP_RANGE::ip_string(IP_RANGE::turn_ip(ip));
132}
133
134inline 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
140inline 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
146bool operator<(const IP_RANGE& a, const IP_RANGE& b);
147
148// DEPRECATED!!! use IP_RANGE instead
149// mode 0: get network-base-address, other: broadcast-address
150inline 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}
157
158#endif