address.h

Go to the documentation of this file.
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 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 __EXPORT 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 private:
00139 #if __BYTE_ORDER == __BIG_ENDIAN
00140         enum {
00141                 MCAST_VALID_MASK = 0xF0000000,
00142                 MCAST_VALID_VALUE = 0xE0000000
00143         };
00144 #else
00145         enum { 
00146                 MCAST_VALID_MASK = 0x000000F0,
00147                 MCAST_VALID_VALUE = 0x000000E0 
00148         };
00149 #endif
00150 };
00151 
00166 class __EXPORT IPV4Address
00167 {
00168 private:
00169         // The validator given to an IPV4Address object must not be a
00170         // transient object, but that must exist at least until the
00171         // last address object of its kind is deleted. This is an
00172         // artifact to be able to do specific checks for derived
00173         // classes inside constructors.
00174         const InetAddrValidator *validator;
00175 
00176 protected:
00177         struct in_addr * ipaddr;
00178         size_t addr_count;
00179         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00180 #if defined(WIN32)
00181         static MutexCounter counter;
00182 #else
00183         static Mutex mutex;
00184 #endif
00185 
00192         bool setIPAddress(const char *host);
00193 
00200         void setAddress(const char *host);
00201 
00202 public:
00210         IPV4Address(const InetAddrValidator *validator = NULL);
00211 
00220         IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
00221 
00232         IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
00233 
00237         IPV4Address(const IPV4Address &rhs);
00238 
00242         virtual ~IPV4Address();
00243 
00250         const char *getHostname(void) const;
00251 
00259         bool isInetAddress(void) const;
00260 
00268         struct in_addr getAddress(void) const;
00269 
00281         struct in_addr getAddress(size_t i) const;
00282 
00288         size_t getAddressCount() const { return addr_count; }
00289 
00290         IPV4Address &operator=(const char *str);
00291         IPV4Address &operator=(struct in_addr addr);
00292         IPV4Address &operator=(const IPV4Address &rhs);
00293 
00298         IPV4Address &operator=(unsigned long addr);
00299 
00300         inline IPV4Address &operator=(unsigned int addr)
00301                 {return *this = (unsigned long) addr; }
00302 
00303         inline bool operator!() const
00304                 {return !isInetAddress();};
00305 
00314         bool operator==(const IPV4Address &a) const;
00315 
00323         bool operator!=(const IPV4Address &a) const;
00324 };      
00325 
00338 class __EXPORT IPV4Mask : public IPV4Address
00339 {
00340 public:
00347         IPV4Mask(const char *mask);
00348 
00359         friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 
00360                                          const IPV4Mask &mask);
00361 
00366         IPV4Address &operator=(unsigned long addr) 
00367                 { return IPV4Address::operator =(addr); }
00368 };
00369 
00377 class __EXPORT IPV4Host : public IPV4Address
00378 {
00379 public: 
00392         IPV4Host(const char *host = NULL);
00393 
00401         IPV4Host(struct in_addr addr);
00402 
00407         IPV4Address &operator=(unsigned long addr) 
00408         { return IPV4Address::operator =(addr); }
00409 
00414         IPV4Host &operator&=(const IPV4Mask &mask);
00415 
00416         friend class __EXPORT IPV4Mask;
00417         friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 
00418                                          const IPV4Mask &mask);
00419 };
00420 
00425 class __EXPORT IPV4Broadcast : public IPV4Address
00426 {
00427 public:
00435         IPV4Broadcast(const char *net = "255.255.255.255");
00436 };
00437 
00447 class __EXPORT IPV4Multicast: public IPV4Address
00448 {
00449 public:
00454         IPV4Multicast();
00455 
00462         IPV4Multicast(const struct in_addr address);
00463 
00473         IPV4Multicast(const char *address);
00474         
00475 private:
00483         static const IPV4MulticastValidator validator;
00484 };
00485 
00486 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
00487 
00488 inline struct in_addr getaddress(const IPV4Address &ia)
00489         {return ia.getAddress();}
00490 
00491 
00492 #ifdef  CCXX_IPV6
00493 
00494 class __EXPORT IPV6Host;
00495 
00504 class __EXPORT IPV6Validator 
00505 {
00506 public:
00510         IPV6Validator() { };
00511 
00515         virtual ~IPV6Validator() {};
00516 
00521         virtual void 
00522         operator()(const in6_addr address) const = 0;
00523 };
00524 
00533 class __EXPORT IPV6MulticastValidator: public IPV6Validator
00534 {
00535 public:
00539         IPV6MulticastValidator(){};
00540 
00544         virtual ~IPV6MulticastValidator(){};
00545 
00550         void operator()(const in6_addr address) const; 
00551 };
00552 
00567 class __EXPORT IPV6Address
00568 {
00569 private:
00570         // The validator given to an IPV4Address object must not be a
00571         // transient object, but that must exist at least until the
00572         // last address object of its kind is deleted. This is an
00573         // artifact to be able to do specific checks for derived
00574         // classes inside constructors.
00575         const IPV6Validator *validator;
00576 
00577 protected:
00578         struct in6_addr * ipaddr;
00579         size_t addr_count;
00580         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00581 #if defined(WIN32)
00582         static MutexCounter counter;
00583 #else
00584         static Mutex mutex;
00585 #endif
00586 
00593         bool setIPAddress(const char *host);
00594 
00601         void setAddress(const char *host);
00602 
00603 public:
00611         IPV6Address(const IPV6Validator *validator = NULL);
00612 
00621         IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00622 
00633         IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00634 
00638         IPV6Address(const IPV6Address &rhs);
00639 
00643         virtual ~IPV6Address();
00644 
00651         const char *getHostname(void) const;
00652 
00660         bool isInetAddress(void) const;
00661 
00669         struct in6_addr getAddress(void) const;
00670 
00682         struct in6_addr getAddress(size_t i) const;
00683 
00689         size_t getAddressCount() const { return addr_count; }
00690 
00691         IPV6Address &operator=(const char *str);
00692         IPV6Address &operator=(struct in6_addr addr);
00693         IPV6Address &operator=(const IPV6Address &rhs);
00694 
00695         inline bool operator!() const
00696                 {return !isInetAddress();};
00697 
00706         bool operator==(const IPV6Address &a) const;
00707 
00715         bool operator!=(const IPV6Address &a) const;
00716 };      
00717 
00730 class __EXPORT IPV6Mask : public IPV6Address
00731 {
00732 public:
00739         IPV6Mask(const char *mask);
00740 
00751         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, 
00752                                          const IPV6Mask &mask);
00753 };
00754 
00762 class __EXPORT IPV6Host : public IPV6Address
00763 {
00764 public: 
00777         IPV6Host(const char *host = NULL);
00778 
00786         IPV6Host(struct in6_addr addr);
00787 
00792         IPV6Host &operator&=(const IPV6Mask &mask);
00793 
00794         friend class __EXPORT IPV6Mask;
00795         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00796 };
00797 
00802 class __EXPORT IPV6Broadcast : public IPV6Address
00803 {
00804 public:
00812         IPV6Broadcast(const char *net = "255.255.255.255");
00813 };
00814 
00824 class __EXPORT IPV6Multicast: public IPV6Address
00825 {
00826 public:
00831         IPV6Multicast();
00832 
00839         IPV6Multicast(const struct in6_addr address);
00840 
00850         IPV6Multicast(const char *address);
00851         
00852 private:
00860         static const IPV6MulticastValidator validator;
00861 };
00862 
00863 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
00864 
00865 inline struct in6_addr getaddress(const IPV6Address &ia)
00866         {return ia.getAddress();}
00867 
00868 
00869 #endif
00870 
00871 #ifdef  CCXX_NAMESPACES
00872 }
00873 #endif
00874 
00875 #endif
00876 

Generated on Fri Nov 25 03:37:18 2005 for GNU CommonC++ by  doxygen 1.4.5