Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

address.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2003 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 to the GNU General Public License, permission is 00018 // granted for additional uses of the text contained in its release 00019 // of Common C++. 00020 // 00021 // The exception is that, if you link the Common C++ library with other files 00022 // to produce an executable, this does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public License. 00024 // Your use of that executable is in no way restricted on account of 00025 // linking the Common C++ library code into it. 00026 // 00027 // This exception does not however invalidate any other reasons why 00028 // the executable file might be covered by the GNU General Public License. 00029 // 00030 // This exception applies only to the code released under the 00031 // name Common C++. If you copy code from other releases into a copy of 00032 // Common C++, as the General Public License permits, the exception does 00033 // not apply to the code that you add in this way. To avoid misleading 00034 // anyone as to the status of such modified files, you must delete 00035 // this exception notice from them. 00036 // 00037 // If you write modifications of your own for Common C++, it is your choice 00038 // whether to permit this exception to apply to your modifications. 00039 // If you do not wish that, delete this exception notice. 00040 00046 #ifndef CCXX_ADDRESS_H_ 00047 #define CCXX_ADDRESS_H_ 00048 00049 #ifndef CCXX_MISSING_H_ 00050 #include <cc++/missing.h> 00051 #endif 00052 00053 #ifndef CCXX_THREAD_H_ 00054 #include <cc++/thread.h> 00055 #endif 00056 00057 #ifndef CCXX_EXCEPTION_H_ 00058 #include <cc++/exception.h> 00059 #endif 00060 00061 #if defined(WIN32) && !defined(__CYGWIN32__) 00062 #define __WINSOCK__ 00063 #include <winsock2.h> 00064 #endif 00065 00066 #ifdef CCXX_NAMESPACES 00067 namespace ost { 00068 #endif 00069 00070 // future definition of ipv4 specific classes, now defines 00071 00072 #define InetAddress IPV4Address 00073 #define InetHostAddress IPV4Host 00074 #define InetMaskAddress IPV4Mask 00075 #define InetMcastAddress IPV4Multicast 00076 #define InetMcastAddressValidator IPV4MulticastValidator 00077 #define InetAddrValidator IPV4Validator 00078 #define BroadcastAddress IPV4Broadcast 00079 00083 typedef unsigned short tpport_t; 00084 00085 class __EXPORT IPV4Host; 00086 00095 class __EXPORT IPV4Validator 00096 { 00097 public: 00101 IPV4Validator() { }; 00102 00107 virtual void 00108 operator()(const in_addr address) const = 0; 00109 }; 00110 00119 class __EXPORT IPV4MulticastValidator: public IPV4Validator 00120 { 00121 public: 00125 IPV4MulticastValidator(){}; 00126 00131 void operator()(const in_addr address) const; 00132 private: 00133 #if __BYTE_ORDER == __BIG_ENDIAN 00134 enum { 00135 MCAST_VALID_MASK = 0xF0000000, 00136 MCAST_VALID_VALUE = 0xE0000000 00137 }; 00138 #else 00139 enum { 00140 MCAST_VALID_MASK = 0x000000F0, 00141 MCAST_VALID_VALUE = 0x000000E0 00142 }; 00143 #endif 00144 }; 00145 00160 class __EXPORT IPV4Address 00161 { 00162 private: 00163 // The validator given to an IPV4Address object must not be a 00164 // transient object, but that must exist at least until the 00165 // last address object of its kind is deleted. This is an 00166 // artifact to be able to do specific checks for derived 00167 // classes inside constructors. 00168 const InetAddrValidator *validator; 00169 00170 protected: 00171 struct in_addr * ipaddr; 00172 size_t addr_count; 00173 mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname 00174 #if defined(WIN32) 00175 static MutexCounter counter; 00176 #else 00177 static Mutex mutex; 00178 #endif 00179 00186 bool setIPAddress(const char *host); 00187 00194 void setAddress(const char *host); 00195 00196 public: 00204 IPV4Address(const InetAddrValidator *validator = NULL); 00205 00214 IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL); 00215 00226 IPV4Address(const char *address, const InetAddrValidator *validator = NULL); 00227 00231 IPV4Address(const IPV4Address &rhs); 00232 00236 virtual ~IPV4Address(); 00237 00244 const char *getHostname(void) const; 00245 00253 bool isInetAddress(void) const; 00254 00262 struct in_addr getAddress(void) const; 00263 00275 struct in_addr getAddress(size_t i) const; 00276 00282 size_t getAddressCount() const { return addr_count; } 00283 00284 IPV4Address &operator=(const char *str); 00285 IPV4Address &operator=(struct in_addr addr); 00286 IPV4Address &operator=(const IPV4Address &rhs); 00287 00292 IPV4Address &operator=(unsigned long addr); 00293 00294 inline IPV4Address &operator=(unsigned int addr) 00295 {return *this = (unsigned long) addr; } 00296 00297 inline bool operator!() const 00298 {return !isInetAddress();}; 00299 00308 bool operator==(const IPV4Address &a) const; 00309 00317 bool operator!=(const IPV4Address &a) const; 00318 }; 00319 00332 class __EXPORT IPV4Mask : public IPV4Address 00333 { 00334 public: 00341 IPV4Mask(const char *mask); 00342 00353 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00354 const IPV4Mask &mask); 00355 00360 IPV4Address &operator=(unsigned long addr) 00361 { return IPV4Address::operator =(addr); } 00362 }; 00363 00371 class __EXPORT IPV4Host : public IPV4Address 00372 { 00373 public: 00386 IPV4Host(const char *host = NULL); 00387 00395 IPV4Host(struct in_addr addr); 00396 00401 IPV4Address &operator=(unsigned long addr) 00402 { return IPV4Address::operator =(addr); } 00403 00408 IPV4Host &operator&=(const IPV4Mask &mask); 00409 00410 friend class __EXPORT IPV4Mask; 00411 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 00412 const IPV4Mask &mask); 00413 }; 00414 00419 class __EXPORT IPV4Broadcast : public IPV4Address 00420 { 00421 public: 00429 IPV4Broadcast(const char *net = "255.255.255.255"); 00430 }; 00431 00441 class __EXPORT IPV4Multicast: public IPV4Address 00442 { 00443 public: 00448 IPV4Multicast(); 00449 00456 IPV4Multicast(const struct in_addr address); 00457 00467 IPV4Multicast(const char *address); 00468 00469 private: 00477 static const IPV4MulticastValidator validator; 00478 }; 00479 00480 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia); 00481 00482 inline struct in_addr getaddress(const IPV4Address &ia) 00483 {return ia.getAddress();} 00484 00485 00486 #ifdef CCXX_IPV6 00487 00488 class __EXPORT IPV6Host; 00489 00498 class __EXPORT IPV6Validator 00499 { 00500 public: 00504 IPV6Validator() { }; 00505 00510 virtual void 00511 operator()(const in6_addr address) const = 0; 00512 }; 00513 00522 class __EXPORT IPV6MulticastValidator: public IPV6Validator 00523 { 00524 public: 00528 IPV6MulticastValidator(){}; 00529 00534 void operator()(const in6_addr address) const; 00535 }; 00536 00551 class __EXPORT IPV6Address 00552 { 00553 private: 00554 // The validator given to an IPV4Address object must not be a 00555 // transient object, but that must exist at least until the 00556 // last address object of its kind is deleted. This is an 00557 // artifact to be able to do specific checks for derived 00558 // classes inside constructors. 00559 const IPV6Validator *validator; 00560 00561 protected: 00562 struct in6_addr * ipaddr; 00563 size_t addr_count; 00564 mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname 00565 #if defined(WIN32) 00566 static MutexCounter counter; 00567 #else 00568 static Mutex mutex; 00569 #endif 00570 00577 bool setIPAddress(const char *host); 00578 00585 void setAddress(const char *host); 00586 00587 public: 00595 IPV6Address(const IPV6Validator *validator = NULL); 00596 00605 IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL); 00606 00617 IPV6Address(const char *address, const IPV6Validator *validator = NULL); 00618 00622 IPV6Address(const IPV6Address &rhs); 00623 00627 virtual ~IPV6Address(); 00628 00635 const char *getHostname(void) const; 00636 00644 bool isInetAddress(void) const; 00645 00653 struct in6_addr getAddress(void) const; 00654 00666 struct in6_addr getAddress(size_t i) const; 00667 00673 size_t getAddressCount() const { return addr_count; } 00674 00675 IPV6Address &operator=(const char *str); 00676 IPV6Address &operator=(struct in6_addr addr); 00677 IPV6Address &operator=(const IPV6Address &rhs); 00678 00679 inline bool operator!() const 00680 {return !isInetAddress();}; 00681 00690 bool operator==(const IPV6Address &a) const; 00691 00699 bool operator!=(const IPV6Address &a) const; 00700 }; 00701 00714 class __EXPORT IPV6Mask : public IPV6Address 00715 { 00716 public: 00723 IPV6Mask(const char *mask); 00724 00735 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, 00736 const IPV6Mask &mask); 00737 }; 00738 00746 class __EXPORT IPV6Host : public IPV6Address 00747 { 00748 public: 00761 IPV6Host(const char *host = NULL); 00762 00770 IPV6Host(struct in6_addr addr); 00771 00776 IPV6Host &operator&=(const IPV6Mask &mask); 00777 00778 friend class __EXPORT IPV6Mask; 00779 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask); 00780 }; 00781 00786 class __EXPORT IPV6Broadcast : public IPV6Address 00787 { 00788 public: 00796 IPV6Broadcast(const char *net = "255.255.255.255"); 00797 }; 00798 00808 class __EXPORT IPV6Multicast: public IPV6Address 00809 { 00810 public: 00815 IPV6Multicast(); 00816 00823 IPV6Multicast(const struct in6_addr address); 00824 00834 IPV6Multicast(const char *address); 00835 00836 private: 00844 static const IPV6MulticastValidator validator; 00845 }; 00846 00847 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia); 00848 00849 inline struct in6_addr getaddress(const IPV6Address &ia) 00850 {return ia.getAddress();} 00851 00852 00853 #endif 00854 00855 #ifdef CCXX_NAMESPACES 00856 } 00857 #endif 00858 00859 #endif 00860

Generated on Fri Jan 21 13:36:02 2005 for GNU CommonC++ by doxygen 1.3.8