ucommon
address.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef COMMONCPP_ADDRESS_H_
45 #define COMMONCPP_ADDRESS_H_
46 
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
49 #endif
50 
51 #ifndef COMMONCPP_THREAD_H_
52 #include <commoncpp/thread.h>
53 #endif
54 
55 #ifndef COMMMONCPP_EXCEPTION_H_
56 #include <commoncpp/exception.h>
57 #endif
58 
59 NAMESPACE_COMMONCPP
60 
61 // future definition of ipv4 specific classes, now defines
62 
63 #define INET_IPV4_ADDRESS_SIZE 16
64 #define CIDR_IPV4_ADDRESS_SIZE 32
65 #define INET_IPV6_ADDRESS_SIZE 40
66 #define CIDR_IPV6_ADDRESS_SIZE 45
67 
68 #define CIDR IPV4Cidr
69 #define InetAddress IPV4Address
70 #define InetHostAddress IPV4Host
71 #define InetMaskAddress IPV4Mask
72 #define InetMcastAddress IPV4Multicast
73 #define InetMcastAddressValidator IPV4MulticastValidator
74 #define InetAddrValidator IPV4Validator
75 #define BroadcastAddress IPV4Broadcast
76 
80 typedef unsigned short tpport_t;
81 
82 class __EXPORT IPV4Host;
83 
92 class __EXPORT IPV4Validator
93 {
94 public:
99 
103  virtual ~IPV4Validator() {};
104 
109  virtual void
110  operator()(const in_addr address) const = 0;
111 };
112 
121 class __EXPORT IPV4MulticastValidator: public IPV4Validator
122 {
123 public:
128 
133 
138  void operator()(const in_addr address) const;
139 };
140 
148 class __EXPORT IPV4Cidr
149 {
150 protected:
151  struct in_addr netmask, network;
152 
153  unsigned getMask(const char *cp) const;
154 public:
160  inline struct in_addr getNetwork(void) const
161  {return network;};
162 
168  inline struct in_addr getNetmask(void) const
169  {return netmask;};
170 
176  struct in_addr getBroadcast(void) const;
177 
184  void set(const char *cidr);
185 
191  IPV4Cidr(const char *cidr);
192 
196  IPV4Cidr();
197 
203  IPV4Cidr(IPV4Cidr &);
204 
211  bool isMember(const struct sockaddr *saddr) const;
212 
219  bool isMember(const struct in_addr &inaddr) const;
220 
221  inline bool operator==(const struct sockaddr *a) const
222  {return isMember(a);};
223 
224  inline bool operator==(const struct in_addr &a) const
225  {return isMember(a);};
226 };
227 
228 #ifdef CCXX_IPV6
229 
236 class __EXPORT IPV6Cidr
237 {
238 protected:
239  struct in6_addr netmask, network;
240 
241  unsigned getMask(const char *cp) const;
242 public:
248  inline struct in6_addr getNetwork(void) const
249  {return network;};
250 
256  inline struct in6_addr getNetmask(void) const
257  {return netmask;};
258 
264  struct in6_addr getBroadcast(void) const;
265 
272  void set(const char *cidr);
273 
279  IPV6Cidr(const char *cidr);
280 
284  IPV6Cidr();
285 
291  IPV6Cidr(IPV6Cidr &);
292 
299  bool isMember(const struct sockaddr *saddr) const;
300 
307  bool isMember(const struct in6_addr &inaddr) const;
308 
309  inline bool operator==(const struct sockaddr *sa) const
310  {return isMember(sa);};
311 
312  inline bool operator==(const struct in6_addr &a) const
313  {return isMember(a);};
314 };
315 
316 #endif
317 
332 class __EXPORT IPV4Address
333 {
334 private:
335  // The validator given to an IPV4Address object must not be a
336  // transient object, but that must exist at least until the
337  // last address object of its kind is deleted. This is an
338  // artifact to be able to do specific checks for derived
339  // classes inside constructors.
340  const InetAddrValidator *validator;
341 
342 protected:
343  struct in_addr * ipaddr;
344  size_t addr_count;
345  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
346 #if defined(_MSWINDOWS_)
347  static MutexCounter counter;
348 #else
349  static Mutex mutex;
350 #endif
351 
358  bool setIPAddress(const char *host);
359 
366  void setAddress(const char *host);
367 
368 public:
376  IPV4Address(const InetAddrValidator *validator = NULL);
377 
386  IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
387 
398  IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
399 
403  IPV4Address(const IPV4Address &rhs);
404 
408  virtual ~IPV4Address();
409 
416  const char *getHostname(void) const;
417 
425  bool isInetAddress(void) const;
426 
434  struct in_addr getAddress(void) const;
435 
447  struct in_addr getAddress(size_t i) const;
448 
454  size_t getAddressCount() const { return addr_count; }
455 
456  IPV4Address &operator=(const char *str);
457  IPV4Address &operator=(struct in_addr addr);
458  IPV4Address &operator=(const IPV4Address &rhs);
459 
464  IPV4Address &operator=(unsigned long addr);
465 
466  inline IPV4Address &operator=(unsigned int addr)
467  {return *this = (unsigned long) addr; }
468 
469  inline bool operator!() const
470  {return !isInetAddress();};
471 
480  bool operator==(const IPV4Address &a) const;
481 
489  bool operator!=(const IPV4Address &a) const;
490 };
491 
504 class __EXPORT IPV4Mask : public IPV4Address
505 {
506 public:
513  IPV4Mask(const char *mask);
514 
525  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
526  const IPV4Mask &mask);
527 
532  IPV4Address &operator=(unsigned long addr)
533  { return IPV4Address::operator =(addr); }
534 };
535 
543 class __EXPORT IPV4Host : public IPV4Address
544 {
545 private:
546  static IPV4Host _host_;
547 
548 public:
561  IPV4Host(const char *host = NULL);
562 
570  IPV4Host(struct in_addr addr);
571 
576  IPV4Address &operator=(unsigned long addr)
577  { return IPV4Address::operator =(addr); }
578 
583  IPV4Host &operator&=(const IPV4Mask &mask);
584 
585  friend class IPV4Mask;
586  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
587  const IPV4Mask &mask);
588 };
589 
594 class __EXPORT IPV4Broadcast : public IPV4Address
595 {
596 public:
604  IPV4Broadcast(const char *net = "255.255.255.255");
605 };
606 
616 class __EXPORT IPV4Multicast: public IPV4Address
617 {
618 public:
623  IPV4Multicast();
624 
631  IPV4Multicast(const struct in_addr address);
632 
642  IPV4Multicast(const char *address);
643 
644 private:
652  static const IPV4MulticastValidator validator;
653 };
654 
655 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
656 
657 inline struct in_addr getaddress(const IPV4Address &ia)
658  {return ia.getAddress();}
659 
660 
661 #ifdef CCXX_IPV6
662 
663 class __EXPORT IPV6Host;
664 
673 class __EXPORT IPV6Validator
674 {
675 public:
680 
684  virtual ~IPV6Validator() {};
685 
690  virtual void operator()(const in6_addr address) const = 0;
691 };
692 
701 class __EXPORT IPV6MulticastValidator: public IPV6Validator
702 {
703 public:
708 
713 
718  void operator()(const in6_addr address) const;
719 };
720 
735 class __EXPORT IPV6Address
736 {
737 private:
738  // The validator given to an IPV4Address object must not be a
739  // transient object, but that must exist at least until the
740  // last address object of its kind is deleted. This is an
741  // artifact to be able to do specific checks for derived
742  // classes inside constructors.
743  const IPV6Validator *validator;
744 
745 protected:
746  struct in6_addr * ipaddr;
747  size_t addr_count;
748  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
749 #if defined(_MSWINDOWS_)
750  static MutexCounter counter;
751 #else
752  static Mutex mutex;
753 #endif
754 
761  bool setIPAddress(const char *host);
762 
769  void setAddress(const char *host);
770 
771 public:
779  IPV6Address(const IPV6Validator *validator = NULL);
780 
789  IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
790 
801  IPV6Address(const char *address, const IPV6Validator *validator = NULL);
802 
806  IPV6Address(const IPV6Address &rhs);
807 
811  virtual ~IPV6Address();
812 
819  const char *getHostname(void) const;
820 
828  bool isInetAddress(void) const;
829 
837  struct in6_addr getAddress(void) const;
838 
850  struct in6_addr getAddress(size_t i) const;
851 
857  size_t getAddressCount() const { return addr_count; }
858 
859  IPV6Address &operator=(const char *str);
860  IPV6Address &operator=(struct in6_addr addr);
861  IPV6Address &operator=(const IPV6Address &rhs);
862 
863  inline bool operator!() const
864  {return !isInetAddress();};
865 
874  bool operator==(const IPV6Address &a) const;
875 
883  bool operator!=(const IPV6Address &a) const;
884 };
885 
898 class __EXPORT IPV6Mask : public IPV6Address
899 {
900 public:
907  IPV6Mask(const char *mask);
908 
919  friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
920  const IPV6Mask &mask);
921 };
922 
930 class __EXPORT IPV6Host : public IPV6Address
931 {
932 public:
945  IPV6Host(const char *host = NULL);
946 
954  IPV6Host(struct in6_addr addr);
955 
960  IPV6Host &operator&=(const IPV6Mask &mask);
961 
962  friend class IPV6Mask;
963  friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
964 };
965 
970 class __EXPORT IPV6Broadcast : public IPV6Address
971 {
972 public:
980  IPV6Broadcast(const char *net = "255.255.255.255");
981 };
982 
992 class __EXPORT IPV6Multicast: public IPV6Address
993 {
994 public:
999  IPV6Multicast();
1000 
1007  IPV6Multicast(const struct in6_addr address);
1008 
1018  IPV6Multicast(const char *address);
1019 
1020 private:
1028  static const IPV6MulticastValidator validator;
1029 };
1030 
1031 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
1032 
1033 inline struct in6_addr getaddress(const IPV6Address &ia)
1034  {return ia.getAddress();}
1035 
1036 
1037 #endif
1038 
1039 END_NAMESPACE
1040 
1041 #endif