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
59namespace 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
80typedef in_port_t tpport_t;
81
82class IPV4Host;
83
92class __EXPORT IPV4Validator
93{
94private:
95 __DELETE_COPY(IPV4Validator);
96
97public:
102
106 virtual ~IPV4Validator() {}
107
112 virtual void
113 operator()(const in_addr address) const = 0;
114};
115
125{
126private:
127 __DELETE_COPY(IPV4MulticastValidator);
128
129public:
134
139
144 void operator()(const in_addr address) const __OVERRIDE;
145};
146
154class __EXPORT IPV4Cidr
155{
156protected:
157 struct in_addr netmask, network;
158
159 unsigned getMask(const char *cp) const;
160public:
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
205
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
254class __EXPORT IPV6Cidr
255{
256protected:
257 struct in6_addr netmask, network;
258
259 unsigned getMask(const char *cp) const;
260public:
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
305
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
362class __EXPORT IPV4Address
363{
364private:
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
372protected:
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
388 bool setIPAddress(const char *host);
389
396 void setAddress(const char *host);
397
398public:
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
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
536class __EXPORT IPV4Mask : public IPV4Address
537{
538private:
539 __DELETE_COPY(IPV4Mask);
540
541public:
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
578class __EXPORT IPV4Host : public IPV4Address
579{
580private:
581 static IPV4Host _host_;
582
583public:
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
620
621 friend class IPV4Mask;
622 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask);
623};
624
629class __EXPORT IPV4Broadcast : public IPV4Address
630{
631public:
639 IPV4Broadcast(const char *net = "255.255.255.255");
640};
641
651class __EXPORT IPV4Multicast: public IPV4Address
652{
653public:
659
666 IPV4Multicast(const struct in_addr address);
667
677 IPV4Multicast(const char *address);
678
679private:
687 static const IPV4MulticastValidator validator;
688};
689
690extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV4Address &ia);
691
692inline struct in_addr getaddress(const IPV4Address &ia) {
693 return ia.getAddress();
694}
695
696
697#ifdef CCXX_IPV6
698
699class IPV6Host;
700
709class __EXPORT IPV6Validator
710{
711private:
712 __DELETE_COPY(IPV6Validator);
713
714public:
719
723 virtual ~IPV6Validator() {}
724
729 virtual void operator()(const in6_addr address) const = 0;
730};
731
741{
742private:
743 __DELETE_COPY(IPV6MulticastValidator);
744
745public:
750
755
760 void operator()(const in6_addr address) const __OVERRIDE;
761};
762
777class __EXPORT IPV6Address
778{
779private:
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
787protected:
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
803 bool setIPAddress(const char *host);
804
811 void setAddress(const char *host);
812
813public:
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
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
947class __EXPORT IPV6Mask : public IPV6Address
948{
949private:
950 __DELETE_COPY(IPV6Mask);
951
952public:
959 IPV6Mask(const char *mask);
960
971 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
972};
973
981class __EXPORT IPV6Host : public IPV6Address
982{
983public:
996 IPV6Host(const char *host = NULL);
997
1005 IPV6Host(struct in6_addr addr);
1006
1012
1013 friend class IPV6Mask;
1014 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
1015};
1016
1021class __EXPORT IPV6Broadcast : public IPV6Address
1022{
1023public:
1031 IPV6Broadcast(const char *net = "255.255.255.255");
1032};
1033
1043class __EXPORT IPV6Multicast: public IPV6Address
1044{
1045public:
1051
1058 IPV6Multicast(const struct in6_addr address);
1059
1069 IPV6Multicast(const char *address);
1070
1071private:
1079 static const IPV6MulticastValidator validator;
1080};
1081
1082extern __EXPORT std::ostream& operator<<(std::ostream &os, const IPV6Address &ia);
1083
1084inline struct in6_addr getaddress(const IPV6Address &ia) {
1085 return ia.getAddress();
1086}
1087
1088#endif
1089
1090} // namespace ost
1091
1092#endif
GNU Common C++ exception model base classes.
in_port_t tpport_t
Transport Protocol Ports.
Definition address.h:80
Classes derived from IPV4Address would require an specific validator to pass to the IPV4Address const...
Definition address.h:93
virtual ~IPV4Validator()
keeps compilers happy.
Definition address.h:106
virtual void operator()(const in_addr address) const =0
Pure virtual application operator.
IPV4Validator()
Constructor.
Definition address.h:101
Class for the function object that validates multicast addresses.
Definition address.h:125
virtual ~IPV4MulticastValidator()
Keeps compilers happy.
Definition address.h:138
IPV4MulticastValidator()
Constructor.
Definition address.h:133
void operator()(const in_addr address) const
Application operator.
The CIDR class is used to support routing tables and validate address policies.
Definition address.h:155
IPV4Cidr(IPV4Cidr &)
Construct a copy of a cidr.
void set(const char *cidr)
Set the cidr from a full or partial hostname, or from an address/mask, or a host/bits specification.
IPV4Cidr()
Construct an empty cidr.
bool isMember(const struct sockaddr *saddr) const
See if a socket address is a member of this cidr's network.
IPV4Cidr(const char *cidr)
Construct a new cidr from a string.
bool isMember(const struct in_addr &inaddr) const
See if a low level address object is a member of this cidr's net.
The CIDR class is used to support routing tables and validate address policies.
Definition address.h:255
bool isMember(const struct in6_addr &inaddr) const
See if a low level address object is a member of this cidr's net.
void set(const char *cidr)
Set the cidr from a full or partial hostname, or from a host/bits specification.
bool isMember(const struct sockaddr *saddr) const
See if a socket address is a member of this cidr's network.
IPV6Cidr()
Construct an empty cidr.
IPV6Cidr(const char *cidr)
Construct a new cidr from a string.
IPV6Cidr(IPV6Cidr &)
Construct a copy of a cidr.
The network name and address objects are all derived from a common IPV4Address base class.
Definition address.h:363
IPV4Address(const IPV4Validator *validator=NULL)
Create an Internet Address object with an empty (0.0.0.0) address.
IPV4Address(struct in_addr addr, const IPV4Validator *validator=NULL)
Convert the system internet address data type (struct in_addr) into a Common C++ IPV4Address object.
void setAddress(const char *host)
Used to specify a host name or numeric internet address.
const char * getHostname(void) const
Provide a string representation of the value (Internet Address) held in the IPV4Address object.
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
bool operator!=(const IPV4Address &a) const
Compare two internet addresses to see if they are not equal (if they each refer to unique and differe...
bool isInetAddress(void) const
May be used to verify if a given IPV4Address returned by another function contains a "valid" address,...
bool setIPAddress(const char *host)
Sets the IP address from a string representation of the numeric address, ie "127.0....
bool operator==(const IPV4Address &a) const
Compare two internet addresses to see if they are equal (if they specify the physical address of the ...
IPV4Address(const char *address, const IPV4Validator *validator=NULL)
Convert a null terminated ASCII host address string (example: "127.0.0.1") or host address name (exam...
size_t getAddressCount() const
Returns the number of internet addresses that an IPV4Address object contains.
Definition address.h:484
virtual ~IPV4Address()
Destructor.
IPV4Address(const IPV4Address &rhs)
Copy constructor.
Internet addresses used specifically as masking addresses (such as "255.255.255.0") are held in the I...
Definition address.h:537
friend IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask)
Masks are usually used to coerce host addresses into a specific router or class domain.
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition address.h:566
IPV4Mask(const char *mask)
Create the mask from a null terminated ASCII string such as "255.255.255.128".
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition address.h:579
IPV4Host & operator&=(const IPV4Mask &mask)
Mask the internet host address object with a network mask address.
IPV4Host(struct in_addr addr)
Convert a system socket binary address such as may be returned through the accept() call or getsockpe...
IPV4Host(const char *host=NULL)
Create a new host address for a specific internet host.
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition address.h:611
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition address.h:630
IPV4Broadcast(const char *net="255.255.255.255")
Specify the physical broadcast address to use and create a new broadcast address object based on a nu...
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition address.h:652
IPV4Multicast(const char *address)
Convert a null terminated ASCII multicast address string (example: "224.0.0.1") or multicast name str...
IPV4Multicast()
Create an Internet Multicast Address object with an empty (0.0.0.0) address.
IPV4Multicast(const struct in_addr address)
Convert the system internet address data type (struct in_addr) into a Common C++ IPV4Multicast object...
Classes derived from IPV6Address would require an specific validator to pass to the IPV6Address const...
Definition address.h:710
virtual ~IPV6Validator()
Keeps compilers happy.
Definition address.h:723
IPV6Validator()
Constructor.
Definition address.h:718
virtual void operator()(const in6_addr address) const =0
Pure virtual application operator.
Class for the function object that validates multicast addresses.
Definition address.h:741
void operator()(const in6_addr address) const
Application operator.
virtual ~IPV6MulticastValidator()
Keeps compilers happy...
Definition address.h:754
IPV6MulticastValidator()
Constructor.
Definition address.h:749
The network name and address objects are all derived from a common IPV6Address base class.
Definition address.h:778
bool operator==(const IPV6Address &a) const
Compare two internet addresses to see if they are equal (if they specify the physical address of the ...
bool operator!=(const IPV6Address &a) const
Compare two internet addresses to see if they are not equal (if they each refer to unique and differe...
IPV6Address(const IPV6Validator *validator=NULL)
Create an Internet Address object with an empty (0.0.0.0) address.
bool isInetAddress(void) const
May be used to verify if a given IPV6Address returned by another function contains a "valid" address,...
const char * getHostname(void) const
Provide a string representation of the value (Internet Address) held in the IPV6Address object.
void setAddress(const char *host)
Used to specify a host name or numeric internet address.
IPV6Address(struct in6_addr addr, const IPV6Validator *validator=NULL)
Convert the system internet address data type (struct in_addr) into a Common C++ IPV6Address object.
virtual ~IPV6Address()
Destructor.
size_t getAddressCount() const
Returns the number of internet addresses that an IPV6Address object contains.
Definition address.h:899
IPV6Address(const IPV6Address &rhs)
Copy constructor.
IPV6Address(const char *address, const IPV6Validator *validator=NULL)
Convert a null terminated ASCII host address string (example: "127.0.0.1") or host address name (exam...
bool setIPAddress(const char *host)
Sets the IP address from a string representation of the numeric address, ie "127.0....
Internet addresses used specifically as masking addresses (such as "255.255.255.0") are held in the I...
Definition address.h:948
IPV6Mask(const char *mask)
Create the mask from a null terminated ASCII string such as "255.255.255.128".
friend IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask)
Masks are usually used to coerce host addresses into a specific router or class domain.
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition address.h:982
IPV6Host(const char *host=NULL)
Create a new host address for a specific internet host.
IPV6Host(struct in6_addr addr)
Convert a system socket binary address such as may be returned through the accept() call or getsockpe...
IPV6Host & operator&=(const IPV6Mask &mask)
Mask the internet host address object with a network mask address.
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition address.h:1022
IPV6Broadcast(const char *net="255.255.255.255")
Specify the physical broadcast address to use and create a new broadcast address object based on a nu...
A specialization of IPV6Address that provides address validation for multicast addresses.
Definition address.h:1044
IPV6Multicast(const char *address)
Convert a null terminated ASCII multicast address string (example: "224.0.0.1") or multicast name str...
IPV6Multicast(const struct in6_addr address)
Convert the system internet address data type (struct in_addr) into a Common C++ IPV4Multicast object...
IPV6Multicast()
Create an Internet Multicast Address object with an empty (0.0.0.0) address.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition thread.h:105
Common C++ thread class and sychronization objects.