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-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_MISSING_H_
00047 #include <cc++/missing.h>
00048 #endif
00049 
00050 #ifndef CCXX_THREAD_H_
00051 #include <cc++/thread.h>
00052 #endif
00053 
00054 #ifndef CCXX_EXCEPTION_H_
00055 #include <cc++/exception.h>
00056 #endif
00057 
00058 #if defined(WIN32) && !defined(__CYGWIN32__)
00059 #define __WINSOCK__
00060 #include <winsock2.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 InetAddress     IPV4Address
00070 #define InetHostAddress IPV4Host
00071 #define InetMaskAddress IPV4Mask
00072 #define InetMcastAddress IPV4Multicast
00073 #define InetMcastAddressValidator IPV4MulticastValidator
00074 #define InetAddrValidator IPV4Validator
00075 #define BroadcastAddress IPV4Broadcast
00076 
00080 typedef unsigned short tpport_t;
00081 
00082 class __EXPORT IPV4Host;
00083 
00092 class __EXPORT IPV4Validator 
00093 {
00094 public:
00098         IPV4Validator() { };
00099 
00103         virtual ~IPV4Validator() {};
00104 
00109         virtual void 
00110         operator()(const in_addr address) const = 0;
00111 };
00112 
00121 class __EXPORT IPV4MulticastValidator: public IPV4Validator
00122 {
00123 public:
00127         IPV4MulticastValidator(){};
00128 
00132         virtual ~IPV4MulticastValidator(){};
00133 
00138         void operator()(const in_addr address) const; 
00139 private:
00140 #if __BYTE_ORDER == __BIG_ENDIAN
00141         enum {
00142                 MCAST_VALID_MASK = 0xF0000000,
00143                 MCAST_VALID_VALUE = 0xE0000000
00144         };
00145 #else
00146         enum { 
00147                 MCAST_VALID_MASK = 0x000000F0,
00148                 MCAST_VALID_VALUE = 0x000000E0 
00149         };
00150 #endif
00151 };
00152 
00167 class __EXPORT IPV4Address
00168 {
00169 private:
00170         // The validator given to an IPV4Address object must not be a
00171         // transient object, but that must exist at least until the
00172         // last address object of its kind is deleted. This is an
00173         // artifact to be able to do specific checks for derived
00174         // classes inside constructors.
00175         const InetAddrValidator *validator;
00176 
00177 protected:
00178         struct in_addr * ipaddr;
00179         size_t addr_count;
00180         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00181 #if defined(WIN32)
00182         static MutexCounter counter;
00183 #else
00184         static Mutex mutex;
00185 #endif
00186 
00193         bool setIPAddress(const char *host);
00194 
00201         void setAddress(const char *host);
00202 
00203 public:
00211         IPV4Address(const InetAddrValidator *validator = NULL);
00212 
00221         IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
00222 
00233         IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
00234 
00238         IPV4Address(const IPV4Address &rhs);
00239 
00243         virtual ~IPV4Address();
00244 
00251         const char *getHostname(void) const;
00252 
00260         bool isInetAddress(void) const;
00261 
00269         struct in_addr getAddress(void) const;
00270 
00282         struct in_addr getAddress(size_t i) const;
00283 
00289         size_t getAddressCount() const { return addr_count; }
00290 
00291         IPV4Address &operator=(const char *str);
00292         IPV4Address &operator=(struct in_addr addr);
00293         IPV4Address &operator=(const IPV4Address &rhs);
00294 
00299         IPV4Address &operator=(unsigned long addr);
00300 
00301         inline IPV4Address &operator=(unsigned int addr)
00302                 {return *this = (unsigned long) addr; }
00303 
00304         inline bool operator!() const
00305                 {return !isInetAddress();};
00306 
00315         bool operator==(const IPV4Address &a) const;
00316 
00324         bool operator!=(const IPV4Address &a) const;
00325 };      
00326 
00339 class __EXPORT IPV4Mask : public IPV4Address
00340 {
00341 public:
00348         IPV4Mask(const char *mask);
00349 
00360         friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 
00361                                          const IPV4Mask &mask);
00362 
00367         IPV4Address &operator=(unsigned long addr) 
00368                 { return IPV4Address::operator =(addr); }
00369 };
00370 
00378 class __EXPORT IPV4Host : public IPV4Address
00379 {
00380 public: 
00393         IPV4Host(const char *host = NULL);
00394 
00402         IPV4Host(struct in_addr addr);
00403 
00408         IPV4Address &operator=(unsigned long addr) 
00409         { return IPV4Address::operator =(addr); }
00410 
00415         IPV4Host &operator&=(const IPV4Mask &mask);
00416 
00417         friend class __EXPORT IPV4Mask;
00418         friend __EXPORT IPV4Host operator&(const IPV4Host &addr, 
00419                                          const IPV4Mask &mask);
00420 };
00421 
00426 class __EXPORT IPV4Broadcast : public IPV4Address
00427 {
00428 public:
00436         IPV4Broadcast(const char *net = "255.255.255.255");
00437 };
00438 
00448 class __EXPORT IPV4Multicast: public IPV4Address
00449 {
00450 public:
00455         IPV4Multicast();
00456 
00463         IPV4Multicast(const struct in_addr address);
00464 
00474         IPV4Multicast(const char *address);
00475         
00476 private:
00484         static const IPV4MulticastValidator validator;
00485 };
00486 
00487 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
00488 
00489 inline struct in_addr getaddress(const IPV4Address &ia)
00490         {return ia.getAddress();}
00491 
00492 
00493 #ifdef  CCXX_IPV6
00494 
00495 class __EXPORT IPV6Host;
00496 
00505 class __EXPORT IPV6Validator 
00506 {
00507 public:
00511         IPV6Validator() { };
00512 
00516         virtual ~IPV6Validator() {};
00517 
00522         virtual void 
00523         operator()(const in6_addr address) const = 0;
00524 };
00525 
00534 class __EXPORT IPV6MulticastValidator: public IPV6Validator
00535 {
00536 public:
00540         IPV6MulticastValidator(){};
00541 
00545         virtual ~IPV6MulticastValidator(){};
00546 
00551         void operator()(const in6_addr address) const; 
00552 };
00553 
00568 class __EXPORT IPV6Address
00569 {
00570 private:
00571         // The validator given to an IPV4Address object must not be a
00572         // transient object, but that must exist at least until the
00573         // last address object of its kind is deleted. This is an
00574         // artifact to be able to do specific checks for derived
00575         // classes inside constructors.
00576         const IPV6Validator *validator;
00577 
00578 protected:
00579         struct in6_addr * ipaddr;
00580         size_t addr_count;
00581         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00582 #if defined(WIN32)
00583         static MutexCounter counter;
00584 #else
00585         static Mutex mutex;
00586 #endif
00587 
00594         bool setIPAddress(const char *host);
00595 
00602         void setAddress(const char *host);
00603 
00604 public:
00612         IPV6Address(const IPV6Validator *validator = NULL);
00613 
00622         IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00623 
00634         IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00635 
00639         IPV6Address(const IPV6Address &rhs);
00640 
00644         virtual ~IPV6Address();
00645 
00652         const char *getHostname(void) const;
00653 
00661         bool isInetAddress(void) const;
00662 
00670         struct in6_addr getAddress(void) const;
00671 
00683         struct in6_addr getAddress(size_t i) const;
00684 
00690         size_t getAddressCount() const { return addr_count; }
00691 
00692         IPV6Address &operator=(const char *str);
00693         IPV6Address &operator=(struct in6_addr addr);
00694         IPV6Address &operator=(const IPV6Address &rhs);
00695 
00696         inline bool operator!() const
00697                 {return !isInetAddress();};
00698 
00707         bool operator==(const IPV6Address &a) const;
00708 
00716         bool operator!=(const IPV6Address &a) const;
00717 };      
00718 
00731 class __EXPORT IPV6Mask : public IPV6Address
00732 {
00733 public:
00740         IPV6Mask(const char *mask);
00741 
00752         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, 
00753                                          const IPV6Mask &mask);
00754 };
00755 
00763 class __EXPORT IPV6Host : public IPV6Address
00764 {
00765 public: 
00778         IPV6Host(const char *host = NULL);
00779 
00787         IPV6Host(struct in6_addr addr);
00788 
00793         IPV6Host &operator&=(const IPV6Mask &mask);
00794 
00795         friend class __EXPORT IPV6Mask;
00796         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00797 };
00798 
00803 class __EXPORT IPV6Broadcast : public IPV6Address
00804 {
00805 public:
00813         IPV6Broadcast(const char *net = "255.255.255.255");
00814 };
00815 
00825 class __EXPORT IPV6Multicast: public IPV6Address
00826 {
00827 public:
00832         IPV6Multicast();
00833 
00840         IPV6Multicast(const struct in6_addr address);
00841 
00851         IPV6Multicast(const char *address);
00852         
00853 private:
00861         static const IPV6MulticastValidator validator;
00862 };
00863 
00864 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
00865 
00866 inline struct in6_addr getaddress(const IPV6Address &ia)
00867         {return ia.getAddress();}
00868 
00869 
00870 #endif
00871 
00872 #ifdef  CCXX_NAMESPACES
00873 }
00874 #endif
00875 
00876 #endif
00877 

Generated on Fri Jul 22 00:15:50 2005 for GNU CommonC++ by  doxygen 1.4.3-20050530