GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 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 CCXX_ADDRESS_H_ 00044 #define CCXX_ADDRESS_H_ 00045 00046 #ifndef CCXX_CONFIG_H_ 00047 #include <cc++/config.h> 00048 #endif 00049 00050 #ifndef CCXX_MISSING_H_ 00051 #include <cc++/missing.h> 00052 #endif 00053 00054 #ifndef CCXX_THREAD_H_ 00055 #include <cc++/thread.h> 00056 #endif 00057 00058 #ifndef CCXX_EXCEPTION_H_ 00059 #include <cc++/exception.h> 00060 #endif 00061 00062 #ifdef CCXX_NAMESPACES 00063 namespace ost { 00064 #endif 00065 00066 // future definition of ipv4 specific classes, now defines 00067 00068 #define INET_IPV4_ADDRESS_SIZE 16 00069 #define CIDR_IPV4_ADDRESS_SIZE 32 00070 #define INET_IPV6_ADDRESS_SIZE 40 00071 #define CIDR_IPV6_ADDRESS_SIZE 45 00072 00073 #define CIDR IPV4Cidr 00074 #define InetAddress IPV4Address 00075 #define InetHostAddress IPV4Host 00076 #define InetMaskAddress IPV4Mask 00077 #define InetMcastAddress IPV4Multicast 00078 #define InetMcastAddressValidator IPV4MulticastValidator 00079 #define InetAddrValidator IPV4Validator 00080 #define BroadcastAddress IPV4Broadcast 00081 00085 typedef unsigned short tpport_t; 00086 00087 class __EXPORT IPV4Host; 00088 00097 class __EXPORT IPV4Validator 00098 { 00099 public: 00103 IPV4Validator() { }; 00104 00108 virtual ~IPV4Validator() {}; 00109 00114 virtual void 00115 operator()(const in_addr address) const = 0; 00116 }; 00117 00126 class __EXPORT IPV4MulticastValidator: public IPV4Validator 00127 { 00128 public: 00132 IPV4MulticastValidator(){}; 00133 00137 virtual ~IPV4MulticastValidator(){}; 00138 00143 void operator()(const in_addr address) const; 00144 private: 00145 #if __BYTE_ORDER == __BIG_ENDIAN 00146 enum { 00147 MCAST_VALID_MASK = 0xF0000000, 00148 MCAST_VALID_VALUE = 0xE0000000 00149 }; 00150 #else 00151 enum { 00152 MCAST_VALID_MASK = 0x000000F0, 00153 MCAST_VALID_VALUE = 0x000000E0 00154 }; 00155 #endif 00156 }; 00157 00165 class __EXPORT IPV4Cidr 00166 { 00167 protected: 00168 struct in_addr netmask, network; 00169 00170 unsigned getMask(const char *cp) const; 00171 public: 00177 inline struct in_addr getNetwork(void) const 00178 {return network;}; 00179 00185 inline struct in_addr getNetmask(void) const 00186 {return netmask;}; 00187 00193 struct in_addr getBroadcast(void) const; 00194 00201 void set(const char *cidr); 00202 00208 IPV4Cidr(const char *cidr); 00209 00213 IPV4Cidr(); 00214 00220 IPV4Cidr(IPV4Cidr &); 00221 00228 bool isMember(const struct sockaddr *saddr) const; 00229 00236 bool isMember(const struct in_addr &inaddr) const; 00237 00238 inline bool operator==(const struct sockaddr *a) const 00239 {return isMember(a);}; 00240 00241 inline bool operator==(const struct in_addr &a) const 00242 {return isMember(a);}; 00243 }; 00244 00245 #ifdef CCXX_IPV6 00246 00253 class __EXPORT IPV6Cidr 00254 { 00255 protected: 00256 struct in6_addr netmask, network; 00257 00258 unsigned getMask(const char *cp) const; 00259 public: 00265 inline struct in6_addr getNetwork(void) const 00266 {return network;}; 00267 00273 inline struct in6_addr getNetmask(void) const 00274 {return netmask;}; 00275 00281 struct in6_addr getBroadcast(void) const; 00282 00289 void set(const char *cidr); 00290 00296 IPV6Cidr(const char *cidr); 00297 00301 IPV6Cidr(); 00302 00308 IPV6Cidr(IPV6Cidr &); 00309 00316 bool isMember(const struct sockaddr *saddr) const; 00317 00324 bool isMember(const struct in6_addr &inaddr) const; 00325 00326 inline bool operator==(const struct sockaddr *sa) const 00327 {return isMember(sa);}; 00328 00329 inline bool operator==(const struct in6_addr &a) const 00330 {return isMember(a);}; 00331 }; 00332 00333 #endif 00334 00349 class __EXPORT IPV4Address 00350 { 00351 private: 00352 // The validator given to an IPV4Address object must not be a 00353 // transient object, but that must exist at least until the 00354 // last address object of its kind is deleted. This is an 00355 // artifact to be able to do specific checks for derived 00356 // classes inside constructors. 00357 const InetAddrValidator *validator; 00358 00359 protected: 00360 struct in_addr * ipaddr; 00361 size_t addr_count; 00362 mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname 00363 #if defined(WIN32) 00364 static MutexCounter counter; 00365 #else 00366 static Mutex mutex; 00367 #endif 00368 00375 bool setIPAddress(const char *host); 00376 00383 void setAddress(const char *host); 00384 00385 public: 00393 IPV4Address(const InetAddrValidator *validator = NULL); 00394 00403 IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL); 00404 00415 IPV4Address(const char *address, const InetAddrValidator *validator = NULL); 00416 00420 IPV4Address(const IPV4Address &rhs); 00421 00425 virtual ~IPV4Address(); 00426 00433 const char *getHostname(void) const; 00434 00442 bool isInetAddress(void) const; 00443 00451 struct in_addr getAddress(void) const; 00452 00464 struct in_addr getAddress(size_t i) const; 00465 00471 size_t getAddressCount() const { return addr_count; } 00472 00473 IPV4Address &operator=(const char *str); 00474 IPV4Address &operator=(struct in_addr addr); 00475 IPV4Address &operator=(const IPV4Address &rhs); 00476 00481 IPV4Address &operator=(unsigned long addr); 00482 00483 inline IPV4Address &operator=(unsigned int addr) 00484 {return *this = (unsigned long) addr; } 00485 00486 inline bool operator!() const 00487 {return !isInetAddress();}; 00488 00497 bool operator==(const IPV4Address &a) const; 00498 00506 bool operator!=(const IPV4Address &a) const; 00507 }; 00508 00521 class __EXPORT IPV4Mask : public IPV4Address 00522 { 00523 public: 00530 IPV4Mask(const char *mask); 00531 00542 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00543 const IPV4Mask &mask); 00544 00549 IPV4Address &operator=(unsigned long addr) 00550 { return IPV4Address::operator =(addr); } 00551 }; 00552 00560 class __EXPORT IPV4Host : public IPV4Address 00561 { 00562 private: 00563 static IPV4Host _host_; 00564 00565 public: 00578 IPV4Host(const char *host = NULL); 00579 00587 IPV4Host(struct in_addr addr); 00588 00593 IPV4Address &operator=(unsigned long addr) 00594 { return IPV4Address::operator =(addr); } 00595 00600 IPV4Host &operator&=(const IPV4Mask &mask); 00601 00602 friend class __EXPORT IPV4Mask; 00603 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00604 const IPV4Mask &mask); 00605 }; 00606 00611 class __EXPORT IPV4Broadcast : public IPV4Address 00612 { 00613 public: 00621 IPV4Broadcast(const char *net = "255.255.255.255"); 00622 }; 00623 00633 class __EXPORT IPV4Multicast: public IPV4Address 00634 { 00635 public: 00640 IPV4Multicast(); 00641 00648 IPV4Multicast(const struct in_addr address); 00649 00659 IPV4Multicast(const char *address); 00660 00661 private: 00669 static const IPV4MulticastValidator validator; 00670 }; 00671 00672 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia); 00673 00674 inline struct in_addr getaddress(const IPV4Address &ia) 00675 {return ia.getAddress();} 00676 00677 00678 #ifdef CCXX_IPV6 00679 00680 class __EXPORT IPV6Host; 00681 00690 class __EXPORT IPV6Validator 00691 { 00692 public: 00696 IPV6Validator() { }; 00697 00701 virtual ~IPV6Validator() {}; 00702 00707 virtual void 00708 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