Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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         inline void 
00535         operator()(const in6_addr address) const; 
00536 };
00537 
00552 class __EXPORT IPV6Address
00553 {
00554 private:
00555         // The validator given to an IPV4Address object must not be a
00556         // transient object, but that must exist at least until the
00557         // last address object of its kind is deleted. This is an
00558         // artifact to be able to do specific checks for derived
00559         // classes inside constructors.
00560         const IPV6Validator *validator;
00561 
00562 protected:
00563         struct in6_addr * ipaddr;
00564         size_t addr_count;
00565         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00566 #if defined(WIN32)
00567         static MutexCounter counter;
00568 #else
00569         static Mutex mutex;
00570 #endif
00571 
00578         bool setIPAddress(const char *host);
00579 
00586         void setAddress(const char *host);
00587 
00588 public:
00596         IPV6Address(const IPV6Validator *validator = NULL);
00597 
00606         IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00607 
00618         IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00619 
00623         IPV6Address(const IPV6Address &rhs);
00624 
00628         virtual ~IPV6Address();
00629 
00636         const char *getHostname(void) const;
00637 
00645         bool isInetAddress(void) const;
00646 
00654         struct in6_addr getAddress(void) const;
00655 
00667         struct in6_addr getAddress(size_t i) const;
00668 
00674         size_t getAddressCount() const { return addr_count; }
00675 
00676         IPV6Address &operator=(const char *str);
00677         IPV6Address &operator=(struct in6_addr addr);
00678         IPV6Address &operator=(const IPV6Address &rhs);
00679 
00680         inline bool operator!() const
00681                 {return !isInetAddress();};
00682 
00691         bool operator==(const IPV6Address &a) const;
00692 
00700         bool operator!=(const IPV6Address &a) const;
00701 };      
00702 
00715 class __EXPORT IPV6Mask : public IPV6Address
00716 {
00717 public:
00724         IPV6Mask(const char *mask);
00725 
00736         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, 
00737                                          const IPV6Mask &mask);
00738 };
00739 
00747 class __EXPORT IPV6Host : public IPV6Address
00748 {
00749 public: 
00762         IPV6Host(const char *host = NULL);
00763 
00771         IPV6Host(struct in6_addr addr);
00772 
00777         IPV6Host &operator&=(const IPV6Mask &mask);
00778 
00779         friend class __EXPORT IPV6Mask;
00780         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00781 };
00782 
00787 class __EXPORT IPV6Broadcast : public IPV6Address
00788 {
00789 public:
00797         IPV6Broadcast(const char *net = "255.255.255.255");
00798 };
00799 
00809 class __EXPORT IPV6Multicast: public IPV6Address
00810 {
00811 public:
00816         IPV6Multicast();
00817 
00824         IPV6Multicast(const struct in6_addr address);
00825 
00835         IPV6Multicast(const char *address);
00836         
00837 private:
00845         static const IPV6MulticastValidator validator;
00846 };
00847 
00848 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
00849 
00850 inline struct in6_addr getaddress(const IPV6Address &ia)
00851         {return ia.getAddress();}
00852 
00853 
00854 #endif
00855 
00856 #ifdef  CCXX_NAMESPACES
00857 }
00858 #endif
00859 
00860 #endif
00861 

Generated on Tue Jan 18 14:32:36 2005 for GNU CommonC++ by  doxygen 1.3.9.1