ucommon
address.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 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 Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 COMMONCPP_ADDRESS_H_
44 #define COMMONCPP_ADDRESS_H_
45 
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
48 #endif
49 
50 #ifndef COMMONCPP_THREAD_H_
51 #include <commoncpp/thread.h>
52 #endif
53 
54 #ifndef COMMMONCPP_EXCEPTION_H_
55 #include <commoncpp/exception.h>
56 #endif
57 
58 NAMESPACE_COMMONCPP
59 
60 // future definition of ipv4 specific classes, now defines
61 
62 #define INET_IPV4_ADDRESS_SIZE 16
63 #define CIDR_IPV4_ADDRESS_SIZE 32
64 #define INET_IPV6_ADDRESS_SIZE 40
65 #define CIDR_IPV6_ADDRESS_SIZE 45
66 
67 #define CIDR IPV4Cidr
68 #define InetAddress IPV4Address
69 #define InetHostAddress IPV4Host
70 #define InetMaskAddress IPV4Mask
71 #define InetMcastAddress IPV4Multicast
72 #define InetMcastAddressValidator IPV4MulticastValidator
73 #define InetAddrValidator IPV4Validator
74 #define BroadcastAddress IPV4Broadcast
75 
79 typedef unsigned short tpport_t;
80 
81 class IPV4Host;
82 
91 class __EXPORT IPV4Validator
92 {
93 public:
98 
102  virtual ~IPV4Validator() {};
103 
108  virtual void
109  operator()(const in_addr address) const = 0;
110 };
111 
120 class __EXPORT IPV4MulticastValidator: public IPV4Validator
121 {
122 public:
127 
132 
137  void operator()(const in_addr address) const;
138 };
139 
147 class __EXPORT IPV4Cidr
148 {
149 protected:
150  struct in_addr netmask, network;
151 
152  unsigned getMask(const char *cp) const;
153 public:
159  inline struct in_addr getNetwork(void) const
160  {return network;};
161 
167  inline struct in_addr getNetmask(void) const
168  {return netmask;};
169 
175  struct in_addr getBroadcast(void) const;
176 
183  void set(const char *cidr);
184 
190  IPV4Cidr(const char *cidr);
191 
195  IPV4Cidr();
196 
202  IPV4Cidr(IPV4Cidr &);
203 
210  bool isMember(const struct sockaddr *saddr) const;
211 
218  bool isMember(const struct in_addr &inaddr) const;
219 
220  inline bool operator==(const struct sockaddr *a) const
221  {return isMember(a);};
222 
223  inline bool operator==(const struct in_addr &a) const
224  {return isMember(a);};
225 };
226 
227 #ifdef CCXX_IPV6
228 
235 class __EXPORT IPV6Cidr
236 {
237 protected:
238  struct in6_addr netmask, network;
239 
240  unsigned getMask(const char *cp) const;
241 public:
247  inline struct in6_addr getNetwork(void) const
248  {return network;};
249 
255  inline struct in6_addr getNetmask(void) const
256  {return netmask;};
257 
263  struct in6_addr getBroadcast(void) const;
264 
271  void set(const char *cidr);
272 
278  IPV6Cidr(const char *cidr);
279 
283  IPV6Cidr();
284 
290  IPV6Cidr(IPV6Cidr &);
291 
298  bool isMember(const struct sockaddr *saddr) const;
299 
306  bool isMember(const struct in6_addr &inaddr) const;
307 
308  inline bool operator==(const struct sockaddr *sa) const
309  {return isMember(sa);};
310 
311  inline bool operator==(const struct in6_addr &a) const
312  {return isMember(a);};
313 };
314 
315 #endif
316 
331 class __EXPORT IPV4Address
332 {
333 private:
334  // The validator given to an IPV4Address object must not be a
335  // transient object, but that must exist at least until the
336  // last address object of its kind is deleted. This is an
337  // artifact to be able to do specific checks for derived
338  // classes inside constructors.
339  const InetAddrValidator *validator;
340 
341 protected:
342  struct in_addr * ipaddr;
343  size_t addr_count;
344  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
345 #if defined(_MSWINDOWS_)
346  static MutexCounter counter;
347 #else
348  static Mutex mutex;
349 #endif
350 
357  bool setIPAddress(const char *host);
358 
365  void setAddress(const char *host);
366 
367 public:
375  IPV4Address(const InetAddrValidator *validator = NULL);
376 
385  IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
386 
397  IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
398 
402  IPV4Address(const IPV4Address &rhs);
403 
407  virtual ~IPV4Address();
408 
415  const char *getHostname(void) const;
416 
424  bool isInetAddress(void) const;
425 
433  struct in_addr getAddress(void) const;
434 
446  struct in_addr getAddress(size_t i) const;
447 
453  size_t getAddressCount() const { return addr_count; }
454 
455  IPV4Address &operator=(const char *str);
456  IPV4Address &operator=(struct in_addr addr);
457  IPV4Address &operator=(const IPV4Address &rhs);
458 
463  IPV4Address &operator=(unsigned long addr);
464 
465  inline IPV4Address &operator=(unsigned int addr)
466  {return *this = (unsigned long) addr; }
467 
468  inline bool operator!() const
469  {return !isInetAddress();};
470 
479  bool operator==(const IPV4Address &a) const;
480 
488  bool operator!=(const IPV4Address &a) const;
489 };
490 
503 class __EXPORT IPV4Mask : public IPV4Address
504 {
505 public:
512  IPV4Mask(const char *mask);
513 
524  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
525  const IPV4Mask &mask);
526 
531  IPV4Address &operator=(unsigned long addr)
532  { return IPV4Address::operator =(addr); }
533 };
534 
542 class __EXPORT IPV4Host : public IPV4Address
543 {
544 private:
545  static IPV4Host _host_;
546 
547 public:
560  IPV4Host(const char *host = NULL);
561 
569  IPV4Host(struct in_addr addr);
570 
575  IPV4Address &operator=(unsigned long addr)
576  { return IPV4Address::operator =(addr); }
577 
582  IPV4Host &operator&=(const IPV4Mask &mask);
583 
584  friend class IPV4Mask;
585  friend __EXPORT IPV4Host operator&(const IPV4Host &addr,
586  const IPV4Mask &mask);
587 };
588 
593 class __EXPORT IPV4Broadcast : public IPV4Address
594 {
595 public:
603  IPV4Broadcast(const char *net = "255.255.255.255");
604 };
605 
615 class __EXPORT IPV4Multicast: public IPV4Address
616 {
617 public:
622  IPV4Multicast();
623 
630  IPV4Multicast(const struct in_addr address);
631 
641  IPV4Multicast(const char *address);
642 
643 private:
651  static const IPV4MulticastValidator validator;
652 };
653 
654 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
655 
656 inline struct in_addr getaddress(const IPV4Address &ia)
657  {return ia.getAddress();}
658 
659 
660 #ifdef CCXX_IPV6
661 
662 class IPV6Host;
663 
672 class __EXPORT IPV6Validator
673 {
674 public:
679 
683  virtual ~IPV6Validator() {};
684 
689  virtual void operator()(const in6_addr address) const = 0;
690 };
691 
700 class __EXPORT IPV6MulticastValidator: public IPV6Validator
701 {
702 public:
707 
712 
717  void operator()(const in6_addr address) const;
718 };
719 
734 class __EXPORT IPV6Address
735 {
736 private:
737  // The validator given to an IPV4Address object must not be a
738  // transient object, but that must exist at least until the
739  // last address object of its kind is deleted. This is an
740  // artifact to be able to do specific checks for derived
741  // classes inside constructors.
742  const IPV6Validator *validator;
743 
744 protected:
745  struct in6_addr * ipaddr;
746  size_t addr_count;
747  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
748 #if defined(_MSWINDOWS_)
749  static MutexCounter counter;
750 #else
751  static Mutex mutex;
752 #endif
753 
760  bool setIPAddress(const char *host);
761 
768  void setAddress(const char *host);
769 
770 public:
778  IPV6Address(const IPV6Validator *validator = NULL);
779 
788  IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
789 
800  IPV6Address(const char *address, const IPV6Validator *validator = NULL);
801 
805  IPV6Address(const IPV6Address &rhs);
806 
810  virtual ~IPV6Address();
811 
818  const char *getHostname(void) const;
819 
827  bool isInetAddress(void) const;
828 
836  struct in6_addr getAddress(void) const;
837 
849  struct in6_addr getAddress(size_t i) const;
850 
856  size_t getAddressCount() const { return addr_count; }
857 
858  IPV6Address &operator=(const char *str);
859  IPV6Address &operator=(struct in6_addr addr);
860  IPV6Address &operator=(const IPV6Address &rhs);
861 
862  inline bool operator!() const
863  {return !isInetAddress();};
864 
873  bool operator==(const IPV6Address &a) const;
874 
882  bool operator!=(const IPV6Address &a) const;
883 };
884 
897 class __EXPORT IPV6Mask : public IPV6Address
898 {
899 public:
906  IPV6Mask(const char *mask);
907 
918  friend __EXPORT IPV6Host operator&(const IPV6Host &addr,
919  const IPV6Mask &mask);
920 };
921 
929 class __EXPORT IPV6Host : public IPV6Address
930 {
931 public:
944  IPV6Host(const char *host = NULL);
945 
953  IPV6Host(struct in6_addr addr);
954 
959  IPV6Host &operator&=(const IPV6Mask &mask);
960 
961  friend class IPV6Mask;
962  friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
963 };
964 
969 class __EXPORT IPV6Broadcast : public IPV6Address
970 {
971 public:
979  IPV6Broadcast(const char *net = "255.255.255.255");
980 };
981 
991 class __EXPORT IPV6Multicast: public IPV6Address
992 {
993 public:
998  IPV6Multicast();
999 
1006  IPV6Multicast(const struct in6_addr address);
1007 
1017  IPV6Multicast(const char *address);
1018 
1019 private:
1027  static const IPV6MulticastValidator validator;
1028 };
1029 
1030 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
1031 
1032 inline struct in6_addr getaddress(const IPV6Address &ia)
1033  {return ia.getAddress();}
1034 
1035 
1036 #endif
1037 
1038 END_NAMESPACE
1039 
1040 #endif
IPV4Validator()
Constructor.
Definition: address.h:97
size_t getAddressCount() const
Returns the number of internet addresses that an IPV4Address object contains.
Definition: address.h:453
PersistEngine & operator<<(PersistEngine &ar, PersistObject const &ob)
Definition: persist.h:319
The CIDR class is used to support routing tables and validate address policies.
Definition: address.h:147
IPV6MulticastValidator()
Constructor.
Definition: address.h:706
Classes derived from IPV4Address would require an specific validator to pass to the IPV4Address const...
Definition: address.h:91
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:615
virtual ~IPV4Validator()
keeps compilers happy.
Definition: address.h:102
The CIDR class is used to support routing tables and validate address policies.
Definition: address.h:235
IPV4Address & operator=(unsigned long addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:531
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition: thread.h:93
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:991
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:1796
Class for the function object that validates multicast addresses.
Definition: address.h:120
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition: address.h:503
GNU Common C++ exception model base classes.
size_t getAddressCount() const
Returns the number of internet addresses that an IPV6Address object contains.
Definition: address.h:856
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:593
IPV4Address & operator=(unsigned long addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:575
Common C++ thread class and sychronization objects.
virtual void operator()(const in_addr address) const =0
Pure virtual application operator.
IPV4MulticastValidator()
Constructor.
Definition: address.h:126
The broadcast address object is used to store the broadcast address for a specific subnet...
Definition: address.h:969
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition: address.h:897
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:331
Classes derived from IPV6Address would require an specific validator to pass to the IPV6Address const...
Definition: address.h:672
virtual void operator()(const in6_addr address) const =0
Pure virtual application operator.
IPV6Validator()
Constructor.
Definition: address.h:678
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:542
The network name and address objects are all derived from a common IPV6Address base class...
Definition: address.h:734
virtual ~IPV6Validator()
Keeps compilers happy.
Definition: address.h:683
virtual ~IPV6MulticastValidator()
Keeps compilers happy...
Definition: address.h:711
Class for the function object that validates multicast addresses.
Definition: address.h:700
virtual ~IPV4MulticastValidator()
Keeps compilers happy.
Definition: address.h:131
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:929