/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /*************************************************************************** ip_functions.hxx - description ------------------- begin : Sat Feb 07 2004 copyright : (C) 2004 by Intra2net AG ***************************************************************************/ #ifndef __IPFUNC_HXX #define __IPFUNC_HXX #include #include #include #include #include #include class IP_RANGE { friend IP_RANGE operator+(const IP_RANGE &base, const int &ips); friend bool operator==(const IP_RANGE& a, const IP_RANGE& b); friend bool operator!=(const IP_RANGE& a, const IP_RANGE& b); friend bool operator<(const IP_RANGE& a, const IP_RANGE& b); private: unsigned int ip; unsigned int mask; unsigned int end; ip_type::type t; public: // the constructors throw runtime_error if the ip is invalid IP_RANGE() { ip=0; mask=0; end=0; t=ip_type::IP; } IP_RANGE(const IP_RANGE &r) { t=r.t; ip=r.ip; mask=r.mask; end=r.end; } IP_RANGE(const char* ip) { load(ip); } IP_RANGE(const std::string& ip) { load(ip); } IP_RANGE(ip_type::type t, const std::string& ip, const std::string& mask_or_end="") { load(t,ip,mask_or_end); } IP_RANGE(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0) { load(t,ip,mask_or_end); } IP_RANGE& operator=(const IP_RANGE& other); void swap(IP_RANGE& other); void load(const std::string& ip); // can decode IP-IP (as range) and IP/MASK (as network) void load(ip_type::type t, const std::string& ip, const std::string& mask_or_end=""); void load(ip_type::type t, unsigned int ip, unsigned int mask_or_end=0); bool is_within(const IP_RANGE& a) const; bool overlapping(const IP_RANGE& a) const; bool netmap(const IP_RANGE& original_net, const IP_RANGE& target_net); int operator-(const IP_RANGE &other); // returns the complete IP_RANGE std::string to_string(bool always_mask=false, bool cidr_only=false) const; // returns the complete range in cidr blocks std::vector to_cidr(void) const; // network<->IP subtraction std::vector substract(const std::set &to_substract) const; unsigned int get_mask_bits() const { return calc_netmask_bits(mask); } unsigned int get_broadcast() const { return end; } unsigned int get_base() const { return ip; } ip_type::type get_type() const { return t; } unsigned int get_netmask() const; // static IP utility functions static unsigned int turn_ip(unsigned int src); static unsigned int calc_netmask_bits(unsigned int mask); static unsigned int calc_netmask_from_cidr(unsigned int cidr); static std::string ip_string(unsigned int ip); static std::string ip_num_string(unsigned int ip); static std::string resolve_ip(const std::string &iporname, const bool enable_ipv6=false); }; class dns_exception : public std::runtime_error { public: dns_exception(const std::string &what) : std::runtime_error(what) {} }; // DEPRECATED!!! use IP_RANGE instead inline unsigned ipfunc_turn_ip(unsigned int src) { return IP_RANGE::turn_ip(src); } // DEPRECATED!!! use IP_RANGE instead inline unsigned int ipfunc_netmask2cidr(unsigned int netmask) { return IP_RANGE::calc_netmask_bits(netmask); } // DEPRECATED!!! use IP_RANGE instead inline std::string ipfunc_format_ip(unsigned int ip) { return IP_RANGE::ip_string(IP_RANGE::turn_ip(ip)); } inline bool operator==(const IP_RANGE& a, const IP_RANGE& b) { // == even if comparing ranges and nets possible return (a.ip==b.ip && a.end==b.end); } inline bool operator!=(const IP_RANGE& a, const IP_RANGE& b) { // == even if comparing ranges and nets possible return !(a.ip==b.ip && a.end==b.end); } bool operator<(const IP_RANGE& a, const IP_RANGE& b); // DEPRECATED!!! use IP_RANGE instead // mode 0: get network-base-address, other: broadcast-address inline std::string CalculateNetworkAddresses (int mode, const std::string &ip, const std::string &netmask) { if (mode==0) return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_base()); else return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_broadcast()); } #endif