address.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2008 David Sugar, Tycho Softworks.
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free software
00019 // library without restriction.  Specifically, if other files instantiate
00020 // templates or use macros or inline functions from this file, or you compile
00021 // this file and link it with other files to produce an executable, this
00022 // file does not by itself cause the resulting executable to be covered by
00023 // the GNU General Public License.  This exception does not however
00024 // invalidate any other reasons why the executable file might be covered by
00025 // the GNU General Public License.
00026 //
00027 // This exception applies only to the code released under the name GNU
00028 // Common C++.  If you copy code from other releases into a copy of GNU
00029 // Common C++, as the General Public License permits, the exception does
00030 // not apply to the code that you add in this way.  To avoid misleading
00031 // anyone as to the status of such modified files, you must delete
00032 // this exception notice from them.
00033 //
00034 // If you write modifications of your own for GNU Common C++, it is your choice
00035 // whether to permit this exception to apply to your modifications.
00036 // If you do not wish that, delete this exception notice.
00037 //
00038 
00044 #ifndef CCXX_ADDRESS_H_
00045 #define CCXX_ADDRESS_H_
00046 
00047 #ifndef CCXX_CONFIG_H_
00048 #include <cc++/config.h>
00049 #endif
00050 
00051 #ifndef CCXX_MISSING_H_
00052 #include <cc++/missing.h>
00053 #endif
00054 
00055 #ifndef CCXX_THREAD_H_
00056 #include <cc++/thread.h>
00057 #endif
00058 
00059 #ifndef CCXX_EXCEPTION_H_
00060 #include <cc++/exception.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 INET_IPV4_ADDRESS_SIZE  16
00070 #define CIDR_IPV4_ADDRESS_SIZE  32
00071 #define INET_IPV6_ADDRESS_SIZE  40
00072 #define CIDR_IPV6_ADDRESS_SIZE  45
00073 
00074 #define CIDR            IPV4Cidr
00075 #define InetAddress     IPV4Address
00076 #define InetHostAddress IPV4Host
00077 #define InetMaskAddress IPV4Mask
00078 #define InetMcastAddress IPV4Multicast
00079 #define InetMcastAddressValidator IPV4MulticastValidator
00080 #define InetAddrValidator IPV4Validator
00081 #define BroadcastAddress IPV4Broadcast
00082 
00086 typedef unsigned short tpport_t;
00087 
00088 class __EXPORT IPV4Host;
00089 
00098 class __EXPORT IPV4Validator
00099 {
00100 public:
00104         IPV4Validator() { };
00105 
00109         virtual ~IPV4Validator() {};
00110 
00115         virtual void
00116         operator()(const in_addr address) const = 0;
00117 };
00118 
00127 class __EXPORT IPV4MulticastValidator: public IPV4Validator
00128 {
00129 public:
00133         IPV4MulticastValidator(){};
00134 
00138         virtual ~IPV4MulticastValidator(){};
00139 
00144         void operator()(const in_addr address) const;
00145 private:
00146 #if __BYTE_ORDER == __BIG_ENDIAN
00147         enum {
00148                 MCAST_VALID_MASK = 0xF0000000,
00149                 MCAST_VALID_VALUE = 0xE0000000
00150         };
00151 #else
00152         enum {
00153                 MCAST_VALID_MASK = 0x000000F0,
00154                 MCAST_VALID_VALUE = 0x000000E0
00155         };
00156 #endif
00157 };
00158 
00166 class __EXPORT IPV4Cidr
00167 {
00168 protected:
00169         struct in_addr netmask, network;
00170 
00171         unsigned getMask(const char *cp) const;
00172 public:
00178         inline struct in_addr getNetwork(void) const
00179                 {return network;};
00180 
00186         inline struct in_addr getNetmask(void) const
00187                 {return netmask;};
00188 
00194         struct in_addr getBroadcast(void) const;
00195 
00202         void set(const char *cidr);
00203 
00209         IPV4Cidr(const char *cidr);
00210 
00214         IPV4Cidr();
00215 
00221         IPV4Cidr(IPV4Cidr &);
00222 
00229         bool isMember(const struct sockaddr *saddr) const;
00230 
00237         bool isMember(const struct in_addr &inaddr) const;
00238 
00239         inline bool operator==(const struct sockaddr *a) const
00240                 {return isMember(a);};
00241 
00242         inline bool operator==(const struct in_addr &a) const
00243                 {return isMember(a);};
00244 };
00245 
00246 #ifdef  CCXX_IPV6
00247 
00254 class __EXPORT IPV6Cidr
00255 {
00256 protected:
00257         struct in6_addr netmask, network;
00258 
00259         unsigned getMask(const char *cp) const;
00260 public:
00266         inline struct in6_addr getNetwork(void) const
00267                 {return network;};
00268 
00274         inline struct in6_addr getNetmask(void) const
00275                 {return netmask;};
00276 
00282         struct in6_addr getBroadcast(void) const;
00283 
00290         void set(const char *cidr);
00291 
00297         IPV6Cidr(const char *cidr);
00298 
00302         IPV6Cidr();
00303 
00309         IPV6Cidr(IPV6Cidr &);
00310 
00317         bool isMember(const struct sockaddr *saddr) const;
00318 
00325         bool isMember(const struct in6_addr &inaddr) const;
00326 
00327         inline bool operator==(const struct sockaddr *sa) const
00328                 {return isMember(sa);};
00329 
00330         inline bool operator==(const struct in6_addr &a) const
00331                 {return isMember(a);};
00332 };
00333 
00334 #endif
00335 
00350 class __EXPORT IPV4Address
00351 {
00352 private:
00353         // The validator given to an IPV4Address object must not be a
00354         // transient object, but that must exist at least until the
00355         // last address object of its kind is deleted. This is an
00356         // artifact to be able to do specific checks for derived
00357         // classes inside constructors.
00358         const InetAddrValidator *validator;
00359 
00360 protected:
00361         struct in_addr * ipaddr;
00362         size_t addr_count;
00363         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00364 #if defined(WIN32)
00365         static MutexCounter counter;
00366 #else
00367         static Mutex mutex;
00368 #endif
00369 
00376         bool setIPAddress(const char *host);
00377 
00384         void setAddress(const char *host);
00385 
00386 public:
00394         IPV4Address(const InetAddrValidator *validator = NULL);
00395 
00404         IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
00405 
00416         IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
00417 
00421         IPV4Address(const IPV4Address &rhs);
00422 
00426         virtual ~IPV4Address();
00427 
00434         const char *getHostname(void) const;
00435 
00443         bool isInetAddress(void) const;
00444 
00452         struct in_addr getAddress(void) const;
00453 
00465         struct in_addr getAddress(size_t i) const;
00466 
00472         size_t getAddressCount() const { return addr_count; }
00473 
00474         IPV4Address &operator=(const char *str);
00475         IPV4Address &operator=(struct in_addr addr);
00476         IPV4Address &operator=(const IPV4Address &rhs);
00477 
00482         IPV4Address &operator=(unsigned long addr);
00483 
00484         inline IPV4Address &operator=(unsigned int addr)
00485                 {return *this = (unsigned long) addr; }
00486 
00487         inline bool operator!() const
00488                 {return !isInetAddress();};
00489 
00498         bool operator==(const IPV4Address &a) const;
00499 
00507         bool operator!=(const IPV4Address &a) const;
00508 };
00509 
00522 class __EXPORT IPV4Mask : public IPV4Address
00523 {
00524 public:
00531         IPV4Mask(const char *mask);
00532 
00543         friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00544                                          const IPV4Mask &mask);
00545 
00550         IPV4Address &operator=(unsigned long addr)
00551                 { return IPV4Address::operator =(addr); }
00552 };
00553 
00561 class __EXPORT IPV4Host : public IPV4Address
00562 {
00563 private:
00564         static IPV4Host _host_;
00565 
00566 public:
00579         IPV4Host(const char *host = NULL);
00580 
00588         IPV4Host(struct in_addr addr);
00589 
00594         IPV4Address &operator=(unsigned long addr)
00595         { return IPV4Address::operator =(addr); }
00596 
00601         IPV4Host &operator&=(const IPV4Mask &mask);
00602 
00603         friend class __EXPORT IPV4Mask;
00604         friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
00605                                          const IPV4Mask &mask);
00606 };
00607 
00612 class __EXPORT IPV4Broadcast : public IPV4Address
00613 {
00614 public:
00622         IPV4Broadcast(const char *net = "255.255.255.255");
00623 };
00624 
00634 class __EXPORT IPV4Multicast: public IPV4Address
00635 {
00636 public:
00641         IPV4Multicast();
00642 
00649         IPV4Multicast(const struct in_addr address);
00650 
00660         IPV4Multicast(const char *address);
00661 
00662 private:
00670         static const IPV4MulticastValidator validator;
00671 };
00672 
00673 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
00674 
00675 inline struct in_addr getaddress(const IPV4Address &ia)
00676         {return ia.getAddress();}
00677 
00678 
00679 #ifdef  CCXX_IPV6
00680 
00681 class __EXPORT IPV6Host;
00682 
00691 class __EXPORT IPV6Validator
00692 {
00693 public:
00697         IPV6Validator() { };
00698 
00702         virtual ~IPV6Validator() {};
00703 
00708         virtual void
00709         operator()(const in6_addr address) const = 0;
00710 };
00711 
00720 class __EXPORT IPV6MulticastValidator: public IPV6Validator
00721 {
00722 public:
00726         IPV6MulticastValidator(){};
00727 
00731         virtual ~IPV6MulticastValidator(){};
00732 
00737         void operator()(const in6_addr address) const;
00738 };
00739 
00754 class __EXPORT IPV6Address
00755 {
00756 private:
00757         // The validator given to an IPV4Address object must not be a
00758         // transient object, but that must exist at least until the
00759         // last address object of its kind is deleted. This is an
00760         // artifact to be able to do specific checks for derived
00761         // classes inside constructors.
00762         const IPV6Validator *validator;
00763 
00764 protected:
00765         struct in6_addr * ipaddr;
00766         size_t addr_count;
00767         mutable char* hostname;  // hostname for ipaddr[0]. Used by getHostname
00768 #if defined(WIN32)
00769         static MutexCounter counter;
00770 #else
00771         static Mutex mutex;
00772 #endif
00773 
00780         bool setIPAddress(const char *host);
00781 
00788         void setAddress(const char *host);
00789 
00790 public:
00798         IPV6Address(const IPV6Validator *validator = NULL);
00799 
00808         IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
00809 
00820         IPV6Address(const char *address, const IPV6Validator *validator = NULL);
00821 
00825         IPV6Address(const IPV6Address &rhs);
00826 
00830         virtual ~IPV6Address();
00831 
00838         const char *getHostname(void) const;
00839 
00847         bool isInetAddress(void) const;
00848 
00856         struct in6_addr getAddress(void) const;
00857 
00869         struct in6_addr getAddress(size_t i) const;
00870 
00876         size_t getAddressCount() const { return addr_count; }
00877 
00878         IPV6Address &operator=(const char *str);
00879         IPV6Address &operator=(struct in6_addr addr);
00880         IPV6Address &operator=(const IPV6Address &rhs);
00881 
00882         inline bool operator!() const
00883                 {return !isInetAddress();};
00884 
00893         bool operator==(const IPV6Address &a) const;
00894 
00902         bool operator!=(const IPV6Address &a) const;
00903 };
00904 
00917 class __EXPORT IPV6Mask : public IPV6Address
00918 {
00919 public:
00926         IPV6Mask(const char *mask);
00927 
00938         friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
00939                                          const IPV6Mask &mask);
00940 };
00941 
00949 class __EXPORT IPV6Host : public IPV6Address
00950 {
00951 public:
00964         IPV6Host(const char *host = NULL);
00965 
00973         IPV6Host(struct in6_addr addr);
00974 
00979         IPV6Host &operator&=(const IPV6Mask &mask);
00980 
00981         friend class __EXPORT IPV6Mask;
00982         friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
00983 };
00984 
00989 class __EXPORT IPV6Broadcast : public IPV6Address
00990 {
00991 public:
00999         IPV6Broadcast(const char *net = "255.255.255.255");
01000 };
01001 
01011 class __EXPORT IPV6Multicast: public IPV6Address
01012 {
01013 public:
01018         IPV6Multicast();
01019 
01026         IPV6Multicast(const struct in6_addr address);
01027 
01037         IPV6Multicast(const char *address);
01038 
01039 private:
01047         static const IPV6MulticastValidator validator;
01048 };
01049 
01050 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
01051 
01052 inline struct in6_addr getaddress(const IPV6Address &ia)
01053         {return ia.getAddress();}
01054 
01055 
01056 #endif
01057 
01058 #ifdef  CCXX_NAMESPACES
01059 }
01060 #endif
01061 
01062 #endif
01063 

Generated on Tue Apr 7 05:42:01 2009 for GNU CommonC++ by  doxygen 1.4.7