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-2020 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:
101 IPV4Validator() { }
102
106 virtual ~IPV4Validator() {}
107
112 virtual void
113 operator()(const in_addr address) const = 0;
114};
115
124class __EXPORT IPV4MulticastValidator: public IPV4Validator
125{
126private:
127 __DELETE_COPY(IPV4MulticastValidator);
128
129public:
133 IPV4MulticastValidator(){}
134
138 virtual ~IPV4MulticastValidator(){}
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
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
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
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
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
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
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
619 IPV4Host &operator&=(const IPV4Mask &mask);
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:
658 IPV4Multicast();
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:
718 IPV6Validator() { }
719
723 virtual ~IPV6Validator() {}
724
729 virtual void operator()(const in6_addr address) const = 0;
730};
731
740class __EXPORT IPV6MulticastValidator: public IPV6Validator
741{
742private:
743 __DELETE_COPY(IPV6MulticastValidator);
744
745public:
749 IPV6MulticastValidator(){}
750
754 virtual ~IPV6MulticastValidator(){}
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
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
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
1011 IPV6Host &operator&=(const IPV6Mask &mask);
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:
1050 IPV6Multicast();
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.
class __attribute__((visibility("default"))) IPV4Mask class __attribute__((visibility("default"))) IPV4Host class __attribute__((visibility("default"))) IPV4Broadcast class __attribute__((visibility("default"))) IPV4Multicast __attribute__((visibility("default"))) std struct in_addr getaddress(const IPV4Address &ia)
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition address.h:692
in_port_t tpport_t
Transport Protocol Ports.
Definition address.h:80
Common C++ thread class and sychronization objects.