UCommon
/usr/src/RPM/BUILD/ucommon-6.3.3/inc/commoncpp/address.h
Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00003 // Copyright (C) 2015 Cherokees of Idaho.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free software
00019 // library without restriction.  Specifically, if other files instantiate
00020 // templates or use macros or inline functions from this file, or you compile
00021 // this file and link it with other files to produce an executable, this
00022 // file does not by itself cause the resulting executable to be covered by
00023 // the GNU General Public License.  This exception does not however
00024 // invalidate any other reasons why the executable file might be covered by
00025 // the GNU General Public License.
00026 //
00027 // This exception applies only to the code released under the name GNU
00028 // Common C++.  If you copy code from other releases into a copy of GNU
00029 // Common C++, as the General Public License permits, the exception does
00030 // not apply to the code that you add in this way.  To avoid misleading
00031 // anyone as to the status of such modified files, you must delete
00032 // this exception notice from them.
00033 //
00034 // If you write modifications of your own for GNU Common C++, it is your choice
00035 // whether to permit this exception to apply to your modifications.
00036 // If you do not wish that, delete this exception notice.
00037 //
00038 
00044 #ifndef COMMONCPP_ADDRESS_H_
00045 #define COMMONCPP_ADDRESS_H_
00046 
00047 #ifndef COMMONCPP_CONFIG_H_
00048 #include <commoncpp/config.h>
00049 #endif
00050 
00051 #ifndef COMMONCPP_THREAD_H_
00052 #include <commoncpp/thread.h>
00053 #endif
00054 
00055 #ifndef COMMMONCPP_EXCEPTION_H_
00056 #include <commoncpp/exception.h>
00057 #endif
00058 
00059 namespace ost {
00060 
00061 // future definition of ipv4 specific classes, now defines
00062 
00063 #define INET_IPV4_ADDRESS_SIZE  16
00064 #define CIDR_IPV4_ADDRESS_SIZE  32
00065 #define INET_IPV6_ADDRESS_SIZE  40
00066 #define CIDR_IPV6_ADDRESS_SIZE  45
00067 
00068 #define CIDR        IPV4Cidr
00069 #define InetAddress IPV4Address
00070 #define InetHostAddress IPV4Host
00071 #define InetMaskAddress IPV4Mask
00072 #define InetMcastAddress IPV4Multicast
00073 #define InetMcastAddressValidator IPV4MulticastValidator
00074 #define InetAddrValidator IPV4Validator
00075 #define BroadcastAddress IPV4Broadcast
00076 
00080 typedef unsigned short tpport_t;
00081 
00082 class IPV4Host;
00083 
00092 class __EXPORT IPV4Validator
00093 {
00094 public:
00098     IPV4Validator() { }
00099 
00103     virtual ~IPV4Validator() {}
00104 
00109     virtual void
00110     operator()(const in_addr address) const = 0;
00111 };
00112 
00121 class __EXPORT IPV4MulticastValidator: public IPV4Validator
00122 {
00123 public:
00127     IPV4MulticastValidator(){}
00128 
00132     virtual ~IPV4MulticastValidator(){}
00133 
00138     void operator()(const in_addr address) const;
00139 };
00140 
00148 class __EXPORT IPV4Cidr
00149 {
00150 protected:
00151     struct in_addr netmask, network;
00152 
00153     unsigned getMask(const char *cp) const;
00154 public:
00160     inline struct in_addr getNetwork(void) const
00161         {return network;}
00162 
00168     inline struct in_addr getNetmask(void) const
00169         {return netmask;}
00170 
00176     struct in_addr getBroadcast(void) const;
00177 
00184     void set(const char *cidr);
00185 
00191     IPV4Cidr(const char *cidr);
00192 
00196     IPV4Cidr();
00197 
00203     IPV4Cidr(IPV4Cidr &);
00204 
00211     bool isMember(const struct sockaddr *saddr) const;
00212 
00219     bool isMember(const struct in_addr &inaddr) const;
00220 
00221     inline bool operator==(const struct sockaddr *a) const
00222         {return isMember(a);}
00223 
00224     inline bool operator==(const struct in_addr &a) const
00225         {return isMember(a);}
00226 };
00227 
00228 #ifdef  CCXX_IPV6
00229 
00236 class __EXPORT IPV6Cidr
00237 {
00238 protected:
00239     struct in6_addr netmask, network;
00240 
00241     unsigned getMask(const char *cp) const;
00242 public:
00248     inline struct in6_addr getNetwork(void) const
00249         {return network;}
00250 
00256     inline struct in6_addr getNetmask(void) const
00257         {return netmask;}
00258 
00264     struct in6_addr getBroadcast(void) const;
00265 
00272     void set(const char *cidr);
00273 
00279     IPV6Cidr(const char *cidr);
00280 
00284     IPV6Cidr();
00285 
00291     IPV6Cidr(IPV6Cidr &);
00292 
00299     bool isMember(const struct sockaddr *saddr) const;
00300 
00307     bool isMember(const struct in6_addr &inaddr) const;
00308 
00309     inline bool operator==(const struct sockaddr *sa) const
00310         {return isMember(sa);}
00311 
00312     inline bool operator==(const struct in6_addr &a) const
00313         {return isMember(a);}
00314 };
00315 
00316 #endif
00317 
00332 class __EXPORT IPV4Address
00333 {
00334 private:
00335     // The validator given to an IPV4Address object must not be a
00336     // transient object, but that must exist at least until the
00337     // last address object of its kind is deleted. This is an
00338     // artifact to be able to do specific checks for derived
00339     // classes inside constructors.
00340     const InetAddrValidator *validator;
00341 
00342 protected:
00343     struct in_addr * ipaddr;
00344     size_t addr_count;
00345     mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00346 #if defined(_MSWINDOWS_)
00347     static MutexCounter counter;
00348 #else
00349     static Mutex mutex;
00350 #endif
00351 
00358     bool setIPAddress(const char *host);
00359 
00366     void setAddress(const char *host);
00367 
00368 public:
00376     IPV4Address(const InetAddrValidator *validator = NULL);
00377 
00386     IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
00387 
00398     IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
00399 
00403     IPV4Address(const IPV4Address &rhs);
00404 
00408     virtual ~IPV4Address();
00409 
00416     const char *getHostname(void) const;
00417 
00425     bool isInetAddress(void) const;
00426 
00434     struct in_addr getAddress(void) const;
00435 
00447     struct in_addr getAddress(size_t i) const;
00448 
00454     size_t getAddressCount() const { return addr_count; }
00455 
00456     IPV4Address &operator=(const char *str);
00457     IPV4Address &operator=(struct in_addr addr);
00458     IPV4Address &operator=(const IPV4Address &rhs);
00459 
00464     IPV4Address &operator=(unsigned long addr);
00465 
00466     inline IPV4Address &operator=(unsigned int addr)
00467         {return *this = (unsigned long) addr; }
00468 
00469     inline bool operator!() const
00470         {return !isInetAddress();}
00471 
00480     bool operator==(const IPV4Address &a) const;
00481 
00489     bool operator!=(const IPV4Address &a) const;
00490 };
00491 
00504 class __EXPORT IPV4Mask : public IPV4Address
00505 {
00506 public:
00513     IPV4Mask(const char *mask);
00514 
00525     friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00526                      const IPV4Mask &mask);
00527 
00532     IPV4Address &operator=(unsigned long addr)
00533         { return IPV4Address::operator =(addr); }
00534 };
00535 
00543 class __EXPORT IPV4Host : public IPV4Address
00544 {
00545 private:
00546     static IPV4Host _host_;
00547 
00548 public:
00561     IPV4Host(const char *host = NULL);
00562 
00570     IPV4Host(struct in_addr addr);
00571 
00576     IPV4Address &operator=(unsigned long addr)
00577     { return IPV4Address::operator =(addr); }
00578 
00583     IPV4Host &operator&=(const IPV4Mask &mask);
00584 
00585     friend class IPV4Mask;
00586     friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00587                      const IPV4Mask &mask);
00588 };
00589 
00594 class __EXPORT IPV4Broadcast : public IPV4Address
00595 {
00596 public:
00604     IPV4Broadcast(const char *net = "255.255.255.255");
00605 };
00606 
00616 class __EXPORT IPV4Multicast: public IPV4Address
00617 {
00618 public:
00623     IPV4Multicast();
00624 
00631     IPV4Multicast(const struct in_addr address);
00632 
00642     IPV4Multicast(const char *address);
00643 
00644 private:
00652     static const IPV4MulticastValidator validator;
00653 };
00654 
00655 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
00656 
00657 inline struct in_addr getaddress(const IPV4Address &ia)
00658     {return ia.getAddress();}
00659 
00660 
00661 #ifdef  CCXX_IPV6
00662 
00663 class IPV6Host;
00664 
00673 class __EXPORT IPV6Validator
00674 {
00675 public:
00679     IPV6Validator() { }
00680 
00684     virtual ~IPV6Validator() {}
00685 
00690     virtual void operator()(const in6_addr address) const = 0;
00691 };
00692 
00701 class __EXPORT IPV6MulticastValidator: public IPV6Validator
00702 {
00703 public:
00707     IPV6MulticastValidator(){}
00708 
00712     virtual ~IPV6MulticastValidator(){}
00713 
00718     void operator()(const in6_addr address) const;
00719 };
00720 
00735 class __EXPORT IPV6Address
00736 {
00737 private:
00738     // The validator given to an IPV4Address object must not be a
00739     // transient object, but that must exist at least until the
00740     // last address object of its kind is deleted. This is an
00741     // artifact to be able to do specific checks for derived
00742     // classes inside constructors.
00743     const IPV6Validator *validator;
00744 
00745 protected:
00746     struct in6_addr * ipaddr;
00747     size_t addr_count;
00748     mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00749 #if defined(_MSWINDOWS_)
00750     static MutexCounter counter;
00751 #else
00752     static Mutex mutex;
00753 #endif
00754 
00761     bool setIPAddress(const char *host);
00762 
00769     void setAddress(const char *host);
00770 
00771 public:
00779     IPV6Address(const IPV6Validator *validator = NULL);
00780 
00789     IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00790 
00801     IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00802 
00806     IPV6Address(const IPV6Address &rhs);
00807 
00811     virtual ~IPV6Address();
00812 
00819     const char *getHostname(void) const;
00820 
00828     bool isInetAddress(void) const;
00829 
00837     struct in6_addr getAddress(void) const;
00838 
00850     struct in6_addr getAddress(size_t i) const;
00851 
00857     size_t getAddressCount() const { return addr_count; }
00858 
00859     IPV6Address &operator=(const char *str);
00860     IPV6Address &operator=(struct in6_addr addr);
00861     IPV6Address &operator=(const IPV6Address &rhs);
00862 
00863     inline bool operator!() const
00864         {return !isInetAddress();}
00865 
00874     bool operator==(const IPV6Address &a) const;
00875 
00883     bool operator!=(const IPV6Address &a) const;
00884 };
00885 
00898 class __EXPORT IPV6Mask : public IPV6Address
00899 {
00900 public:
00907     IPV6Mask(const char *mask);
00908 
00919     friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
00920                      const IPV6Mask &mask);
00921 };
00922 
00930 class __EXPORT IPV6Host : public IPV6Address
00931 {
00932 public:
00945     IPV6Host(const char *host = NULL);
00946 
00954     IPV6Host(struct in6_addr addr);
00955 
00960     IPV6Host &operator&=(const IPV6Mask &mask);
00961 
00962     friend class IPV6Mask;
00963     friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00964 };
00965 
00970 class __EXPORT IPV6Broadcast : public IPV6Address
00971 {
00972 public:
00980     IPV6Broadcast(const char *net = "255.255.255.255");
00981 };
00982 
00992 class __EXPORT IPV6Multicast: public IPV6Address
00993 {
00994 public:
00999     IPV6Multicast();
01000 
01007     IPV6Multicast(const struct in6_addr address);
01008 
01018     IPV6Multicast(const char *address);
01019 
01020 private:
01028     static const IPV6MulticastValidator validator;
01029 };
01030 
01031 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
01032 
01033 inline struct in6_addr getaddress(const IPV6Address &ia)
01034     {return ia.getAddress();}
01035 
01036 
01037 #endif
01038 
01039 } // namespace ost
01040 
01041 #endif