GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2010 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 General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 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 CCXX_ADDRESS_H_ 00045 #define CCXX_ADDRESS_H_ 00046 00047 #ifndef CCXX_CONFIG_H_ 00048 #include <cc++/config.h> 00049 #endif 00050 00051 #ifndef CCXX_MISSING_H_ 00052 #include <cc++/missing.h> 00053 #endif 00054 00055 #ifndef CCXX_THREAD_H_ 00056 #include <cc++/thread.h> 00057 #endif 00058 00059 #ifndef CCXX_EXCEPTION_H_ 00060 #include <cc++/exception.h> 00061 #endif 00062 00063 #ifdef CCXX_NAMESPACES 00064 namespace ost { 00065 #endif 00066 00067 // future definition of ipv4 specific classes, now defines 00068 00069 #define INET_IPV4_ADDRESS_SIZE 16 00070 #define CIDR_IPV4_ADDRESS_SIZE 32 00071 #define INET_IPV6_ADDRESS_SIZE 40 00072 #define CIDR_IPV6_ADDRESS_SIZE 45 00073 00074 #define CIDR IPV4Cidr 00075 #define InetAddress IPV4Address 00076 #define InetHostAddress IPV4Host 00077 #define InetMaskAddress IPV4Mask 00078 #define InetMcastAddress IPV4Multicast 00079 #define InetMcastAddressValidator IPV4MulticastValidator 00080 #define InetAddrValidator IPV4Validator 00081 #define BroadcastAddress IPV4Broadcast 00082 00086 typedef unsigned short tpport_t; 00087 00088 class __EXPORT IPV4Host; 00089 00098 class __EXPORT IPV4Validator 00099 { 00100 public: 00104 IPV4Validator() { }; 00105 00109 virtual ~IPV4Validator() {}; 00110 00115 virtual void 00116 operator()(const in_addr address) const = 0; 00117 }; 00118 00127 class __EXPORT IPV4MulticastValidator: public IPV4Validator 00128 { 00129 public: 00133 IPV4MulticastValidator(){}; 00134 00138 virtual ~IPV4MulticastValidator(){}; 00139 00144 void operator()(const in_addr address) const; 00145 private: 00146 #if __BYTE_ORDER == __BIG_ENDIAN 00147 enum { 00148 MCAST_VALID_MASK = 0xF0000000, 00149 MCAST_VALID_VALUE = 0xE0000000 00150 }; 00151 #else 00152 enum { 00153 MCAST_VALID_MASK = 0x000000F0, 00154 MCAST_VALID_VALUE = 0x000000E0 00155 }; 00156 #endif 00157 }; 00158 00166 class __EXPORT IPV4Cidr 00167 { 00168 protected: 00169 struct in_addr netmask, network; 00170 00171 unsigned getMask(const char *cp) const; 00172 public: 00178 inline struct in_addr getNetwork(void) const 00179 {return network;}; 00180 00186 inline struct in_addr getNetmask(void) const 00187 {return netmask;}; 00188 00194 struct in_addr getBroadcast(void) const; 00195 00202 void set(const char *cidr); 00203 00209 IPV4Cidr(const char *cidr); 00210 00214 IPV4Cidr(); 00215 00221 IPV4Cidr(IPV4Cidr &); 00222 00229 bool isMember(const struct sockaddr *saddr) const; 00230 00237 bool isMember(const struct in_addr &inaddr) const; 00238 00239 inline bool operator==(const struct sockaddr *a) const 00240 {return isMember(a);}; 00241 00242 inline bool operator==(const struct in_addr &a) const 00243 {return isMember(a);}; 00244 }; 00245 00246 #ifdef CCXX_IPV6 00247 00254 class __EXPORT IPV6Cidr 00255 { 00256 protected: 00257 struct in6_addr netmask, network; 00258 00259 unsigned getMask(const char *cp) const; 00260 public: 00266 inline struct in6_addr getNetwork(void) const 00267 {return network;}; 00268 00274 inline struct in6_addr getNetmask(void) const 00275 {return netmask;}; 00276 00282 struct in6_addr getBroadcast(void) const; 00283 00290 void set(const char *cidr); 00291 00297 IPV6Cidr(const char *cidr); 00298 00302 IPV6Cidr(); 00303 00309 IPV6Cidr(IPV6Cidr &); 00310 00317 bool isMember(const struct sockaddr *saddr) const; 00318 00325 bool isMember(const struct in6_addr &inaddr) const; 00326 00327 inline bool operator==(const struct sockaddr *sa) const 00328 {return isMember(sa);}; 00329 00330 inline bool operator==(const struct in6_addr &a) const 00331 {return isMember(a);}; 00332 }; 00333 00334 #endif 00335 00350 class __EXPORT IPV4Address 00351 { 00352 private: 00353 // The validator given to an IPV4Address object must not be a 00354 // transient object, but that must exist at least until the 00355 // last address object of its kind is deleted. This is an 00356 // artifact to be able to do specific checks for derived 00357 // classes inside constructors. 00358 const InetAddrValidator *validator; 00359 00360 protected: 00361 struct in_addr * ipaddr; 00362 size_t addr_count; 00363 mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname 00364 #if defined(WIN32) 00365 static MutexCounter counter; 00366 #else 00367 static Mutex mutex; 00368 #endif 00369 00376 bool setIPAddress(const char *host); 00377 00384 void setAddress(const char *host); 00385 00386 public: 00394 IPV4Address(const InetAddrValidator *validator = NULL); 00395 00404 IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL); 00405 00416 IPV4Address(const char *address, const InetAddrValidator *validator = NULL); 00417 00421 IPV4Address(const IPV4Address &rhs); 00422 00426 virtual ~IPV4Address(); 00427 00434 const char *getHostname(void) const; 00435 00443 bool isInetAddress(void) const; 00444 00452 struct in_addr getAddress(void) const; 00453 00465 struct in_addr getAddress(size_t i) const; 00466 00472 size_t getAddressCount() const { return addr_count; } 00473 00474 IPV4Address &operator=(const char *str); 00475 IPV4Address &operator=(struct in_addr addr); 00476 IPV4Address &operator=(const IPV4Address &rhs); 00477 00482 IPV4Address &operator=(unsigned long addr); 00483 00484 inline IPV4Address &operator=(unsigned int addr) 00485 {return *this = (unsigned long) addr; } 00486 00487 inline bool operator!() const 00488 {return !isInetAddress();}; 00489 00498 bool operator==(const IPV4Address &a) const; 00499 00507 bool operator!=(const IPV4Address &a) const; 00508 }; 00509 00522 class __EXPORT IPV4Mask : public IPV4Address 00523 { 00524 public: 00531 IPV4Mask(const char *mask); 00532 00543 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00544 const IPV4Mask &mask); 00545 00550 IPV4Address &operator=(unsigned long addr) 00551 { return IPV4Address::operator =(addr); } 00552 }; 00553 00561 class __EXPORT IPV4Host : public IPV4Address 00562 { 00563 private: 00564 static IPV4Host _host_; 00565 00566 public: 00579 IPV4Host(const char *host = NULL); 00580 00588 IPV4Host(struct in_addr addr); 00589 00594 IPV4Address &operator=(unsigned long addr) 00595 { return IPV4Address::operator =(addr); } 00596 00601 IPV4Host &operator&=(const IPV4Mask &mask); 00602 00603 friend class __EXPORT IPV4Mask; 00604 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00605 const IPV4Mask &mask); 00606 }; 00607 00612 class __EXPORT IPV4Broadcast : public IPV4Address 00613 { 00614 public: 00622 IPV4Broadcast(const char *net = "255.255.255.255"); 00623 }; 00624 00634 class __EXPORT IPV4Multicast: public IPV4Address 00635 { 00636 public: 00641 IPV4Multicast(); 00642 00649 IPV4Multicast(const struct in_addr address); 00650 00660 IPV4Multicast(const char *address); 00661 00662 private: 00670 static const IPV4MulticastValidator validator; 00671 }; 00672 00673 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia); 00674 00675 inline struct in_addr getaddress(const IPV4Address &ia) 00676 {return ia.getAddress();} 00677 00678 00679 #ifdef CCXX_IPV6 00680 00681 class __EXPORT IPV6Host; 00682 00691 class __EXPORT IPV6Validator 00692 { 00693 public: 00697 IPV6Validator() { }; 00698 00702 virtual ~IPV6Validator() {}; 00703 00708 virtual void operator()(const in6_addr address) const = 0; 00709 }; 00710 00719 class __EXPORT IPV6MulticastValidator: public IPV6Validator 00720 { 00721 public: 00725 IPV6MulticastValidator(){}; 00726 00730 virtual ~IPV6MulticastValidator(){}; 00731 00736 void operator()(const in6_addr address) const; 00737 }; 00738 00753 class __EXPORT IPV6Address 00754 { 00755 private: 00756 // The validator given to an IPV4Address object must not be a 00757 // transient object, but that must exist at least until the 00758 // last address object of its kind is deleted. This is an 00759 // artifact to be able to do specific checks for derived 00760 // classes inside constructors. 00761 const IPV6Validator *validator; 00762 00763 protected: 00764 struct in6_addr * ipaddr; 00765 size_t addr_count; 00766 mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname 00767 #if defined(WIN32) 00768 static MutexCounter counter; 00769 #else 00770 static Mutex mutex; 00771 #endif 00772 00779 bool setIPAddress(const char *host); 00780 00787 void setAddress(const char *host); 00788 00789 public: 00797 IPV6Address(const IPV6Validator *validator = NULL); 00798 00807 IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL); 00808 00819 IPV6Address(const char *address, const IPV6Validator *validator = NULL); 00820 00824 IPV6Address(const IPV6Address &rhs); 00825 00829 virtual ~IPV6Address(); 00830 00837 const char *getHostname(void) const; 00838 00846 bool isInetAddress(void) const; 00847 00855 struct in6_addr getAddress(void) const; 00856 00868 struct in6_addr getAddress(size_t i) const; 00869 00875 size_t getAddressCount() const { return addr_count; } 00876 00877 IPV6Address &operator=(const char *str); 00878 IPV6Address &operator=(struct in6_addr addr); 00879 IPV6Address &operator=(const IPV6Address &rhs); 00880 00881 inline bool operator!() const 00882 {return !isInetAddress();}; 00883 00892 bool operator==(const IPV6Address &a) const; 00893 00901 bool operator!=(const IPV6Address &a) const; 00902 }; 00903 00916 class __EXPORT IPV6Mask : public IPV6Address 00917 { 00918 public: 00925 IPV6Mask(const char *mask); 00926 00937 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, 00938 const IPV6Mask &mask); 00939 }; 00940 00948 class __EXPORT IPV6Host : public IPV6Address 00949 { 00950 public: 00963 IPV6Host(const char *host = NULL); 00964 00972 IPV6Host(struct in6_addr addr); 00973 00978 IPV6Host &operator&=(const IPV6Mask &mask); 00979 00980 friend class __EXPORT IPV6Mask; 00981 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask); 00982 }; 00983 00988 class __EXPORT IPV6Broadcast : public IPV6Address 00989 { 00990 public: 00998 IPV6Broadcast(const char *net = "255.255.255.255"); 00999 }; 01000 01010 class __EXPORT IPV6Multicast: public IPV6Address 01011 { 01012 public: 01017 IPV6Multicast(); 01018 01025 IPV6Multicast(const struct in6_addr address); 01026 01036 IPV6Multicast(const char *address); 01037 01038 private: 01046 static const IPV6MulticastValidator validator; 01047 }; 01048 01049 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia); 01050 01051 inline struct in6_addr getaddress(const IPV6Address &ia) 01052 {return ia.getAddress();} 01053 01054 01055 #endif 01056 01057 #ifdef CCXX_NAMESPACES 01058 } 01059 #endif 01060 01061 #endif 01062