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 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 ost {
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 in_port_t tpport_t;
81 
82 class IPV4Host;
83 
92 class __EXPORT IPV4Validator
93 {
94 private:
95  __DELETE_COPY(IPV4Validator);
96 
97 public:
102 
106  virtual ~IPV4Validator() {}
107 
112  virtual void
113  operator()(const in_addr address) const = 0;
114 };
115 
124 class __EXPORT IPV4MulticastValidator: public IPV4Validator
125 {
126 private:
127  __DELETE_COPY(IPV4MulticastValidator);
128 
129 public:
134 
139 
144  void operator()(const in_addr address) const __OVERRIDE;
145 };
146 
154 class __EXPORT IPV4Cidr
155 {
156 protected:
157  struct in_addr netmask, network;
158 
159  unsigned getMask(const char *cp) const;
160 public:
166  inline struct in_addr getNetwork(void) const {
167  return network;
168  }
169 
175  inline struct in_addr getNetmask(void) const {
176  return netmask;
177  }
178 
184  struct in_addr getBroadcast(void) const;
185 
192  void set(const char *cidr);
193 
199  IPV4Cidr(const char *cidr);
200 
204  IPV4Cidr();
205 
211  IPV4Cidr(IPV4Cidr &);
212 
219  bool isMember(const struct sockaddr *saddr) const;
220 
227  bool isMember(const struct in_addr &inaddr) const;
228 
229  inline bool operator==(const struct sockaddr *a) const {
230  return isMember(a);
231  }
232 
233  inline bool operator==(const struct in_addr &a) const {
234  return isMember(a);
235  }
236 
237  inline bool operator!=(const struct sockaddr *a) const {
238  return !isMember(a);
239  }
240 
241  inline bool operator!=(const struct in_addr &a) const {
242  return !isMember(a);
243  }
244 };
245 
246 #ifdef CCXX_IPV6
247 
254 class __EXPORT IPV6Cidr
255 {
256 protected:
257  struct in6_addr netmask, network;
258 
259  unsigned getMask(const char *cp) const;
260 public:
266  inline struct in6_addr getNetwork(void) const {
267  return network;
268  }
269 
275  inline struct in6_addr getNetmask(void) const {
276  return netmask;
277  }
278 
284  struct in6_addr getBroadcast(void) const;
285 
292  void set(const char *cidr);
293 
299  IPV6Cidr(const char *cidr);
300 
304  IPV6Cidr();
305 
311  IPV6Cidr(IPV6Cidr &);
312 
319  bool isMember(const struct sockaddr *saddr) const;
320 
327  bool isMember(const struct in6_addr &inaddr) const;
328 
329  inline bool operator==(const struct sockaddr *sa) const {
330  return isMember(sa);
331  }
332 
333  inline bool operator==(const struct in6_addr &a) const {
334  return isMember(a);
335  }
336 
337  inline bool operator!=(const struct sockaddr *sa) const {
338  return !isMember(sa);
339  }
340 
341  inline bool operator!=(const struct in6_addr &a) const {
342  return !isMember(a);
343  }
344 };
345 
346 #endif
347 
362 class __EXPORT IPV4Address
363 {
364 private:
365  // The validator given to an IPV4Address object must not be a
366  // transient object, but that must exist at least until the
367  // last address object of its kind is deleted. This is an
368  // artifact to be able to do specific checks for derived
369  // classes inside constructors.
370  const InetAddrValidator *validator;
371 
372 protected:
373  struct in_addr * ipaddr;
374  size_t addr_count;
375  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
376 #if defined(_MSWINDOWS_)
377  static MutexCounter counter;
378 #else
379  static Mutex mutex;
380 #endif
381 
388  bool setIPAddress(const char *host);
389 
396  void setAddress(const char *host);
397 
398 public:
406  IPV4Address(const InetAddrValidator *validator = NULL);
407 
416  IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
417 
428  IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
429 
433  IPV4Address(const IPV4Address &rhs);
434 
438  virtual ~IPV4Address();
439 
446  const char *getHostname(void) const;
447 
455  bool isInetAddress(void) const;
456 
464  struct in_addr getAddress(void) const;
465 
477  struct in_addr getAddress(size_t i) const;
478 
484  size_t getAddressCount() const { return addr_count; }
485 
486  IPV4Address &operator=(const char *str);
487  IPV4Address &operator=(struct in_addr addr);
488  IPV4Address &operator=(const IPV4Address &rhs);
489 
494  IPV4Address &operator=(in_addr_t addr);
495 
496  inline operator bool() const {
497  return isInetAddress();
498  }
499 
500  inline bool operator!() const {
501  return !isInetAddress();
502  }
503 
512  bool operator==(const IPV4Address &a) const;
513 
521  bool operator!=(const IPV4Address &a) const;
522 };
523 
536 class __EXPORT IPV4Mask : public IPV4Address
537 {
538 private:
539  __DELETE_COPY(IPV4Mask);
540 
541 public:
548  IPV4Mask(const char *mask);
549 
560  friend __EXPORT IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask);
561 
566  IPV4Address &operator=(in_addr_t addr) {
567  return IPV4Address::operator =(addr);
568  }
569 };
570 
578 class __EXPORT IPV4Host : public IPV4Address
579 {
580 private:
581  static IPV4Host _host_;
582 
583 public:
596  IPV4Host(const char *host = NULL);
597 
605  IPV4Host(struct in_addr addr);
606 
611  IPV4Address &operator=(in_addr_t addr) {
612  return IPV4Address::operator =(addr);
613  }
614 
619  IPV4Host &operator&=(const IPV4Mask &mask);
620 
621  friend class IPV4Mask;
622  friend __EXPORT IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask);
623 };
624 
629 class __EXPORT IPV4Broadcast : public IPV4Address
630 {
631 public:
639  IPV4Broadcast(const char *net = "255.255.255.255");
640 };
641 
651 class __EXPORT IPV4Multicast: public IPV4Address
652 {
653 public:
658  IPV4Multicast();
659 
666  IPV4Multicast(const struct in_addr address);
667 
677  IPV4Multicast(const char *address);
678 
679 private:
687  static const IPV4MulticastValidator validator;
688 };
689 
690 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
691 
692 inline struct in_addr getaddress(const IPV4Address &ia) {
693  return ia.getAddress();
694 }
695 
696 
697 #ifdef CCXX_IPV6
698 
699 class IPV6Host;
700 
709 class __EXPORT IPV6Validator
710 {
711 private:
712  __DELETE_COPY(IPV6Validator);
713 
714 public:
719 
723  virtual ~IPV6Validator() {}
724 
729  virtual void operator()(const in6_addr address) const = 0;
730 };
731 
740 class __EXPORT IPV6MulticastValidator: public IPV6Validator
741 {
742 private:
743  __DELETE_COPY(IPV6MulticastValidator);
744 
745 public:
750 
755 
760  void operator()(const in6_addr address) const __OVERRIDE;
761 };
762 
777 class __EXPORT IPV6Address
778 {
779 private:
780  // The validator given to an IPV4Address object must not be a
781  // transient object, but that must exist at least until the
782  // last address object of its kind is deleted. This is an
783  // artifact to be able to do specific checks for derived
784  // classes inside constructors.
785  const IPV6Validator *validator;
786 
787 protected:
788  struct in6_addr * ipaddr;
789  size_t addr_count;
790  mutable char* hostname; // hostname for ipaddr[0]. Used by getHostname
791 #if defined(_MSWINDOWS_)
792  static MutexCounter counter;
793 #else
794  static Mutex mutex;
795 #endif
796 
803  bool setIPAddress(const char *host);
804 
811  void setAddress(const char *host);
812 
813 public:
821  IPV6Address(const IPV6Validator *validator = NULL);
822 
831  IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
832 
843  IPV6Address(const char *address, const IPV6Validator *validator = NULL);
844 
848  IPV6Address(const IPV6Address &rhs);
849 
853  virtual ~IPV6Address();
854 
861  const char *getHostname(void) const;
862 
870  bool isInetAddress(void) const;
871 
879  struct in6_addr getAddress(void) const;
880 
892  struct in6_addr getAddress(size_t i) const;
893 
899  size_t getAddressCount() const {
900  return addr_count;
901  }
902 
903  IPV6Address &operator=(const char *str);
904  IPV6Address &operator=(struct in6_addr addr);
905  IPV6Address &operator=(const IPV6Address &rhs);
906 
907  inline operator bool () const {
908  return isInetAddress();
909  }
910 
911  inline bool operator!() const {
912  return !isInetAddress();
913  }
914 
923  bool operator==(const IPV6Address &a) const;
924 
932  bool operator!=(const IPV6Address &a) const;
933 };
934 
947 class __EXPORT IPV6Mask : public IPV6Address
948 {
949 private:
950  __DELETE_COPY(IPV6Mask);
951 
952 public:
959  IPV6Mask(const char *mask);
960 
971  friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
972 };
973 
981 class __EXPORT IPV6Host : public IPV6Address
982 {
983 public:
996  IPV6Host(const char *host = NULL);
997 
1005  IPV6Host(struct in6_addr addr);
1006 
1011  IPV6Host &operator&=(const IPV6Mask &mask);
1012 
1013  friend class IPV6Mask;
1014  friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
1015 };
1016 
1021 class __EXPORT IPV6Broadcast : public IPV6Address
1022 {
1023 public:
1031  IPV6Broadcast(const char *net = "255.255.255.255");
1032 };
1033 
1043 class __EXPORT IPV6Multicast: public IPV6Address
1044 {
1045 public:
1050  IPV6Multicast();
1051 
1058  IPV6Multicast(const struct in6_addr address);
1059 
1069  IPV6Multicast(const char *address);
1070 
1071 private:
1079  static const IPV6MulticastValidator validator;
1080 };
1081 
1082 extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
1083 
1084 inline struct in6_addr getaddress(const IPV6Address &ia) {
1085  return ia.getAddress();
1086 }
1087 
1088 #endif
1089 
1090 } // namespace ost
1091 
1092 #endif
Class for the function object that validates multicast addresses.
Definition: address.h:124
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition: address.h:1021
IPV4Validator()
Constructor.
Definition: address.h:101
virtual ~IPV4Validator()
keeps compilers happy.
Definition: address.h:106
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:578
The CIDR class is used to support routing tables and validate address policies.
Definition: address.h:254
Classes derived from IPV4Address would require an specific validator to pass to the IPV4Address const...
Definition: address.h:92
The network name and address objects are all derived from a common IPV6Address base class.
Definition: address.h:777
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:651
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition: address.h:536
Common C++ thread class and sychronization objects.
virtual ~IPV6MulticastValidator()
Keeps compilers happy...
Definition: address.h:754
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:981
Class for the function object that validates multicast addresses.
Definition: address.h:740
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:611
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition: address.h:1043
in_port_t tpport_t
Transport Protocol Ports.
Definition: address.h:80
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
size_t getAddressCount() const
Returns the number of internet addresses that an IPV4Address object contains.
Definition: address.h:484
IPV6MulticastValidator()
Constructor.
Definition: address.h:749
virtual ~IPV6Validator()
Keeps compilers happy.
Definition: address.h:723
size_t getAddressCount() const
Returns the number of internet addresses that an IPV6Address object contains.
Definition: address.h:899
virtual ~IPV4MulticastValidator()
Keeps compilers happy.
Definition: address.h:138
IPV4MulticastValidator()
Constructor.
Definition: address.h:133
The CIDR class is used to support routing tables and validate address policies.
Definition: address.h:154
IPV6Validator()
Constructor.
Definition: address.h:718
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:566
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition: address.h:629
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:362
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition: thread.h:104
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition: address.h:947
Classes derived from IPV6Address would require an specific validator to pass to the IPV6Address const...
Definition: address.h:709
GNU Common C++ exception model base classes.