GNU CommonC++
address.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_ADDRESS_H_
44 #define CCXX_ADDRESS_H_
45 
46 #ifndef CCXX_CONFIG_H_
47 #include <cc++/config.h>
48 #endif
49 
50 #ifndef CCXX_MISSING_H_
51 #include <cc++/missing.h>
52 #endif
53 
54 #ifndef CCXX_THREAD_H_
55 #include <cc++/thread.h>
56 #endif
57 
58 #ifndef CCXX_EXCEPTION_H_
59 #include <cc++/exception.h>
60 #endif
61 
62 #ifdef CCXX_NAMESPACES
63 namespace ost {
64 #endif
65 
66 // future definition of ipv4 specific classes, now defines
67 
68 #define INET_IPV4_ADDRESS_SIZE 16
69 #define CIDR_IPV4_ADDRESS_SIZE 32
70 #define INET_IPV6_ADDRESS_SIZE 40
71 #define CIDR_IPV6_ADDRESS_SIZE 45
72 
73 #define CIDR IPV4Cidr
74 #define InetAddress IPV4Address
75 #define InetHostAddress IPV4Host
76 #define InetMaskAddress IPV4Mask
77 #define InetMcastAddress IPV4Multicast
78 #define InetMcastAddressValidator IPV4MulticastValidator
79 #define InetAddrValidator IPV4Validator
80 #define BroadcastAddress IPV4Broadcast
81 
85 typedef unsigned short tpport_t;
86 
88 
98 {
99 public:
104 
108  virtual ~IPV4Validator() {};
109 
114  virtual void
115  operator()(const in_addr address) const = 0;
116 };
117 
127 {
128 public:
133 
138 
143  void operator()(const in_addr address) const;
144 private:
145 #if __BYTE_ORDER == __BIG_ENDIAN
146  enum {
147  MCAST_VALID_MASK = 0xF0000000,
148  MCAST_VALID_VALUE = 0xE0000000
149  };
150 #else
151  enum {
152  MCAST_VALID_MASK = 0x000000F0,
153  MCAST_VALID_VALUE = 0x000000E0
154  };
155 #endif
156 };
157 
166 {
167 protected:
168  struct in_addr netmask, network;
169 
170  unsigned getMask(const char *cp) const;
171 public:
177  inline struct in_addr getNetwork(void) const
178  {return network;};
179 
185  inline struct in_addr getNetmask(void) const
186  {return netmask;};
187 
193  struct in_addr getBroadcast(void) const;
194 
201  void set(const char *cidr);
202 
208  IPV4Cidr(const char *cidr);
209 
213  IPV4Cidr();
214 
220  IPV4Cidr(IPV4Cidr &);
221 
228  bool isMember(const struct sockaddr *saddr) const;
229 
236  bool isMember(const struct in_addr &inaddr) const;
237 
238  inline bool operator==(const struct sockaddr *a) const
239  {return isMember(a);};
240 
241  inline bool operator==(const struct in_addr &a) const
242  {return isMember(a);};
243 };
244 
245 #ifdef CCXX_IPV6
246 
254 {
255 protected:
256  struct in6_addr netmask, network;
257 
258  unsigned getMask(const char *cp) const;
259 public:
265  inline struct in6_addr getNetwork(void) const
266  {return network;};
267 
273  inline struct in6_addr getNetmask(void) const
274  {return netmask;};
275 
281  struct in6_addr getBroadcast(void) const;
282 
289  void set(const char *cidr);
290 
296  IPV6Cidr(const char *cidr);
297 
301  IPV6Cidr();
302 
308  IPV6Cidr(IPV6Cidr &);
309 
316  bool isMember(const struct sockaddr *saddr) const;
317 
324  bool isMember(const struct in6_addr &inaddr) const;
325 
326  inline bool operator==(const struct sockaddr *sa) const
327  {return isMember(sa);};
328 
329  inline bool operator==(const struct in6_addr &a) const
330  {return isMember(a);};
331 };
332 
333 #endif
334 
350 {
351 private:
352  // The validator given to an IPV4Address object must not be a
353  // transient object, but that must exist at least until the
354  // last address object of its kind is deleted. This is an
355  // artifact to be able to do specific checks for derived
356  // classes inside constructors.
357  const InetAddrValidator *validator;
358 
359 protected:
360  struct in_addr * ipaddr;
361  size_t addr_count;
362  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
363 #if defined(WIN32)
364  static MutexCounter counter;
365 #else
366  static Mutex mutex;
367 #endif
368 
375  bool setIPAddress(const char *host);
376 
383  void setAddress(const char *host);
384 
385 public:
393  IPV4Address(const InetAddrValidator *validator = NULL);
394 
403  IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
404 
415  IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
416 
420  IPV4Address(const IPV4Address &rhs);
421 
425  virtual ~IPV4Address();
426 
433  const char *getHostname(void) const;
434 
442  bool isInetAddress(void) const;
443 
451  struct in_addr getAddress(void) const;
452 
464  struct in_addr getAddress(size_t i) const;
465 
471  size_t getAddressCount() const { return addr_count; }
472 
473  IPV4Address &operator=(const char *str);
474  IPV4Address &operator=(struct in_addr addr);
475  IPV4Address &operator=(const IPV4Address &rhs);
476 
481  IPV4Address &operator=(unsigned long addr);
482 
483  inline IPV4Address &operator=(unsigned int addr)
484  {return *this = (unsigned long) addr; }
485 
486  inline bool operator!() const
487  {return !isInetAddress();};
488 
497  bool operator==(const IPV4Address &a) const;
498 
506  bool operator!=(const IPV4Address &a) const;
507 };
508 
522 {
523 public:
530  IPV4Mask(const char *mask);
531 
542  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
543  const IPV4Mask &mask);
544 
549  IPV4Address &operator=(unsigned long addr)
550  { return IPV4Address::operator =(addr); }
551 };
552 
561 {
562 private:
563  static IPV4Host _host_;
564 
565 public:
578  IPV4Host(const char *host = NULL);
579 
587  IPV4Host(struct in_addr addr);
588 
593  IPV4Address &operator=(unsigned long addr)
594  { return IPV4Address::operator =(addr); }
595 
600  IPV4Host &operator&=(const IPV4Mask &mask);
601 
602  friend class __EXPORT IPV4Mask;
603  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
604  const IPV4Mask &mask);
605 };
606 
612 {
613 public:
621  IPV4Broadcast(const char *net = "255.255.255.255");
622 };
623 
634 {
635 public:
640  IPV4Multicast();
641 
648  IPV4Multicast(const struct in_addr address);
649 
659  IPV4Multicast(const char *address);
660 
661 private:
669  static const IPV4MulticastValidator validator;
670 };
671 
672 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
673 
674 inline struct in_addr getaddress(const IPV4Address &ia)
675  {return ia.getAddress();}
676 
677 
678 #ifdef CCXX_IPV6
679 
681 
691 {
692 public:
697 
701  virtual ~IPV6Validator() {};
702 
707  virtual void
708  operator()(const in6_addr address) const = 0;
709 };
710 
720 {
721 public:
726 
731 
736  void operator()(const in6_addr address) const;
737 };
738 
754 {
755 private:
756  // The validator given to an IPV4Address object must not be a
757  // transient object, but that must exist at least until the
758  // last address object of its kind is deleted. This is an
759  // artifact to be able to do specific checks for derived
760  // classes inside constructors.
761  const IPV6Validator *validator;
762 
763 protected:
764  struct in6_addr * ipaddr;
765  size_t addr_count;
766  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
767 #if defined(WIN32)
768  static MutexCounter counter;
769 #else
770  static Mutex mutex;
771 #endif
772 
779  bool setIPAddress(const char *host);
780 
787  void setAddress(const char *host);
788 
789 public:
797  IPV6Address(const IPV6Validator *validator = NULL);
798 
807  IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
808 
819  IPV6Address(const char *address, const IPV6Validator *validator = NULL);
820 
824  IPV6Address(const IPV6Address &rhs);
825 
829  virtual ~IPV6Address();
830 
837  const char *getHostname(void) const;
838 
846  bool isInetAddress(void) const;
847 
855  struct in6_addr getAddress(void) const;
856 
868  struct in6_addr getAddress(size_t i) const;
869 
875  size_t getAddressCount() const { return addr_count; }
876 
877  IPV6Address &operator=(const char *str);
878  IPV6Address &operator=(struct in6_addr addr);
879  IPV6Address &operator=(const IPV6Address &rhs);
880 
881  inline bool operator!() const
882  {return !isInetAddress();};
883 
892  bool operator==(const IPV6Address &a) const;
893 
901  bool operator!=(const IPV6Address &a) const;
902 };
903 
917 {
918 public:
925  IPV6Mask(const char *mask);
926 
937  friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
938  const IPV6Mask &mask);
939 };
940 
949 {
950 public:
963  IPV6Host(const char *host = NULL);
964 
972  IPV6Host(struct in6_addr addr);
973 
978  IPV6Host &operator&=(const IPV6Mask &mask);
979 
980  friend class __EXPORT IPV6Mask;
981  friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
982 };
983 
989 {
990 public:
998  IPV6Broadcast(const char *net = "255.255.255.255");
999 };
1000 
1011 {
1012 public:
1017  IPV6Multicast();
1018 
1025  IPV6Multicast(const struct in6_addr address);
1026 
1036  IPV6Multicast(const char *address);
1037 
1038 private:
1046  static const IPV6MulticastValidator validator;
1047 };
1048 
1049 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
1050 
1051 inline struct in6_addr getaddress(const IPV6Address &ia)
1052  {return ia.getAddress();}
1053 
1054 
1055 #endif
1056 
1057 #ifdef CCXX_NAMESPACES
1058 }
1059 #endif
1060 
1061 #endif
1062