ucommon
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 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef COMMONCPP_ADDRESS_H_
00044 #define COMMONCPP_ADDRESS_H_
00045 
00046 #ifndef COMMONCPP_CONFIG_H_
00047 #include <commoncpp/config.h>
00048 #endif
00049 
00050 #ifndef COMMONCPP_THREAD_H_
00051 #include <commoncpp/thread.h>
00052 #endif
00053 
00054 #ifndef COMMMONCPP_EXCEPTION_H_
00055 #include <commoncpp/exception.h>
00056 #endif
00057 
00058 namespace ost {
00059 
00060 // future definition of ipv4 specific classes, now defines
00061 
00062 #define INET_IPV4_ADDRESS_SIZE  16
00063 #define CIDR_IPV4_ADDRESS_SIZE  32
00064 #define INET_IPV6_ADDRESS_SIZE  40
00065 #define CIDR_IPV6_ADDRESS_SIZE  45
00066 
00067 #define CIDR        IPV4Cidr
00068 #define InetAddress IPV4Address
00069 #define InetHostAddress IPV4Host
00070 #define InetMaskAddress IPV4Mask
00071 #define InetMcastAddress IPV4Multicast
00072 #define InetMcastAddressValidator IPV4MulticastValidator
00073 #define InetAddrValidator IPV4Validator
00074 #define BroadcastAddress IPV4Broadcast
00075 
00079 typedef unsigned short tpport_t;
00080 
00081 class IPV4Host;
00082 
00091 class __EXPORT IPV4Validator
00092 {
00093 public:
00097     IPV4Validator() { }
00098 
00102     virtual ~IPV4Validator() {}
00103 
00108     virtual void
00109     operator()(const in_addr address) const = 0;
00110 };
00111 
00120 class __EXPORT IPV4MulticastValidator: public IPV4Validator
00121 {
00122 public:
00126     IPV4MulticastValidator(){}
00127 
00131     virtual ~IPV4MulticastValidator(){}
00132 
00137     void operator()(const in_addr address) const;
00138 };
00139 
00147 class __EXPORT IPV4Cidr
00148 {
00149 protected:
00150     struct in_addr netmask, network;
00151 
00152     unsigned getMask(const char *cp) const;
00153 public:
00159     inline struct in_addr getNetwork(void) const
00160         {return network;}
00161 
00167     inline struct in_addr getNetmask(void) const
00168         {return netmask;}
00169 
00175     struct in_addr getBroadcast(void) const;
00176 
00183     void set(const char *cidr);
00184 
00190     IPV4Cidr(const char *cidr);
00191 
00195     IPV4Cidr();
00196 
00202     IPV4Cidr(IPV4Cidr &);
00203 
00210     bool isMember(const struct sockaddr *saddr) const;
00211 
00218     bool isMember(const struct in_addr &inaddr) const;
00219 
00220     inline bool operator==(const struct sockaddr *a) const
00221         {return isMember(a);}
00222 
00223     inline bool operator==(const struct in_addr &a) const
00224         {return isMember(a);}
00225 };
00226 
00227 #ifdef  CCXX_IPV6
00228 
00235 class __EXPORT IPV6Cidr
00236 {
00237 protected:
00238     struct in6_addr netmask, network;
00239 
00240     unsigned getMask(const char *cp) const;
00241 public:
00247     inline struct in6_addr getNetwork(void) const
00248         {return network;}
00249 
00255     inline struct in6_addr getNetmask(void) const
00256         {return netmask;}
00257 
00263     struct in6_addr getBroadcast(void) const;
00264 
00271     void set(const char *cidr);
00272 
00278     IPV6Cidr(const char *cidr);
00279 
00283     IPV6Cidr();
00284 
00290     IPV6Cidr(IPV6Cidr &);
00291 
00298     bool isMember(const struct sockaddr *saddr) const;
00299 
00306     bool isMember(const struct in6_addr &inaddr) const;
00307 
00308     inline bool operator==(const struct sockaddr *sa) const
00309         {return isMember(sa);}
00310 
00311     inline bool operator==(const struct in6_addr &a) const
00312         {return isMember(a);}
00313 };
00314 
00315 #endif
00316 
00331 class __EXPORT IPV4Address
00332 {
00333 private:
00334     // The validator given to an IPV4Address object must not be a
00335     // transient object, but that must exist at least until the
00336     // last address object of its kind is deleted. This is an
00337     // artifact to be able to do specific checks for derived
00338     // classes inside constructors.
00339     const InetAddrValidator *validator;
00340 
00341 protected:
00342     struct in_addr * ipaddr;
00343     size_t addr_count;
00344     mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00345 #if defined(_MSWINDOWS_)
00346     static MutexCounter counter;
00347 #else
00348     static Mutex mutex;
00349 #endif
00350 
00357     bool setIPAddress(const char *host);
00358 
00365     void setAddress(const char *host);
00366 
00367 public:
00375     IPV4Address(const InetAddrValidator *validator = NULL);
00376 
00385     IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
00386 
00397     IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
00398 
00402     IPV4Address(const IPV4Address &rhs);
00403 
00407     virtual ~IPV4Address();
00408 
00415     const char *getHostname(void) const;
00416 
00424     bool isInetAddress(void) const;
00425 
00433     struct in_addr getAddress(void) const;
00434 
00446     struct in_addr getAddress(size_t i) const;
00447 
00453     size_t getAddressCount() const { return addr_count; }
00454 
00455     IPV4Address &operator=(const char *str);
00456     IPV4Address &operator=(struct in_addr addr);
00457     IPV4Address &operator=(const IPV4Address &rhs);
00458 
00463     IPV4Address &operator=(unsigned long addr);
00464 
00465     inline IPV4Address &operator=(unsigned int addr)
00466         {return *this = (unsigned long) addr; }
00467 
00468     inline bool operator!() const
00469         {return !isInetAddress();}
00470 
00479     bool operator==(const IPV4Address &a) const;
00480 
00488     bool operator!=(const IPV4Address &a) const;
00489 };
00490 
00503 class __EXPORT IPV4Mask : public IPV4Address
00504 {
00505 public:
00512     IPV4Mask(const char *mask);
00513 
00524     friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00525                      const IPV4Mask &mask);
00526 
00531     IPV4Address &operator=(unsigned long addr)
00532         { return IPV4Address::operator =(addr); }
00533 };
00534 
00542 class __EXPORT IPV4Host : public IPV4Address
00543 {
00544 private:
00545     static IPV4Host _host_;
00546 
00547 public:
00560     IPV4Host(const char *host = NULL);
00561 
00569     IPV4Host(struct in_addr addr);
00570 
00575     IPV4Address &operator=(unsigned long addr)
00576     { return IPV4Address::operator =(addr); }
00577 
00582     IPV4Host &operator&=(const IPV4Mask &mask);
00583 
00584     friend class IPV4Mask;
00585     friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00586                      const IPV4Mask &mask);
00587 };
00588 
00593 class __EXPORT IPV4Broadcast : public IPV4Address
00594 {
00595 public:
00603     IPV4Broadcast(const char *net = "255.255.255.255");
00604 };
00605 
00615 class __EXPORT IPV4Multicast: public IPV4Address
00616 {
00617 public:
00622     IPV4Multicast();
00623 
00630     IPV4Multicast(const struct in_addr address);
00631 
00641     IPV4Multicast(const char *address);
00642 
00643 private:
00651     static const IPV4MulticastValidator validator;
00652 };
00653 
00654 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
00655 
00656 inline struct in_addr getaddress(const IPV4Address &ia)
00657     {return ia.getAddress();}
00658 
00659 
00660 #ifdef  CCXX_IPV6
00661 
00662 class IPV6Host;
00663 
00672 class __EXPORT IPV6Validator
00673 {
00674 public:
00678     IPV6Validator() { }
00679 
00683     virtual ~IPV6Validator() {}
00684 
00689     virtual void operator()(const in6_addr address) const = 0;
00690 };
00691 
00700 class __EXPORT IPV6MulticastValidator: public IPV6Validator
00701 {
00702 public:
00706     IPV6MulticastValidator(){}
00707 
00711     virtual ~IPV6MulticastValidator(){}
00712 
00717     void operator()(const in6_addr address) const;
00718 };
00719 
00734 class __EXPORT IPV6Address
00735 {
00736 private:
00737     // The validator given to an IPV4Address object must not be a
00738     // transient object, but that must exist at least until the
00739     // last address object of its kind is deleted. This is an
00740     // artifact to be able to do specific checks for derived
00741     // classes inside constructors.
00742     const IPV6Validator *validator;
00743 
00744 protected:
00745     struct in6_addr * ipaddr;
00746     size_t addr_count;
00747     mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00748 #if defined(_MSWINDOWS_)
00749     static MutexCounter counter;
00750 #else
00751     static Mutex mutex;
00752 #endif
00753 
00760     bool setIPAddress(const char *host);
00761 
00768     void setAddress(const char *host);
00769 
00770 public:
00778     IPV6Address(const IPV6Validator *validator = NULL);
00779 
00788     IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00789 
00800     IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00801 
00805     IPV6Address(const IPV6Address &rhs);
00806 
00810     virtual ~IPV6Address();
00811 
00818     const char *getHostname(void) const;
00819 
00827     bool isInetAddress(void) const;
00828 
00836     struct in6_addr getAddress(void) const;
00837 
00849     struct in6_addr getAddress(size_t i) const;
00850 
00856     size_t getAddressCount() const { return addr_count; }
00857 
00858     IPV6Address &operator=(const char *str);
00859     IPV6Address &operator=(struct in6_addr addr);
00860     IPV6Address &operator=(const IPV6Address &rhs);
00861 
00862     inline bool operator!() const
00863         {return !isInetAddress();}
00864 
00873     bool operator==(const IPV6Address &a) const;
00874 
00882     bool operator!=(const IPV6Address &a) const;
00883 };
00884 
00897 class __EXPORT IPV6Mask : public IPV6Address
00898 {
00899 public:
00906     IPV6Mask(const char *mask);
00907 
00918     friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
00919                      const IPV6Mask &mask);
00920 };
00921 
00929 class __EXPORT IPV6Host : public IPV6Address
00930 {
00931 public:
00944     IPV6Host(const char *host = NULL);
00945 
00953     IPV6Host(struct in6_addr addr);
00954 
00959     IPV6Host &operator&=(const IPV6Mask &mask);
00960 
00961     friend class IPV6Mask;
00962     friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00963 };
00964 
00969 class __EXPORT IPV6Broadcast : public IPV6Address
00970 {
00971 public:
00979     IPV6Broadcast(const char *net = "255.255.255.255");
00980 };
00981 
00991 class __EXPORT IPV6Multicast: public IPV6Address
00992 {
00993 public:
00998     IPV6Multicast();
00999 
01006     IPV6Multicast(const struct in6_addr address);
01007 
01017     IPV6Multicast(const char *address);
01018 
01019 private:
01027     static const IPV6MulticastValidator validator;
01028 };
01029 
01030 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
01031 
01032 inline struct in6_addr getaddress(const IPV6Address &ia)
01033     {return ia.getAddress();}
01034 
01035 
01036 #endif
01037 
01038 } // namespace ost
01039 
01040 #endif