ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
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
39/**
40 * @file commoncpp/address.h
41 * @short Network addresses and sockets related classes.
42 **/
43
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
77/**
78 * Transport Protocol Ports.
79 */
80typedef in_port_t tpport_t;
81
82class IPV4Host;
83
84/**
85 * Classes derived from IPV4Address would require an specific
86 * validator to pass to the IPV4Address constructor. This is a base
87 * class for classes of function objects used by such derived classes.
88 *
89 * @author Federico Montesino <p5087@quintero.fie.us.es>
90 * @short Abstract base class for derived inet addresses validators.
91 */
93{
94private:
96
97public:
98 /**
99 * Constructor. Does not deal with any state.
100 */
102
103 /**
104 * keeps compilers happy.
105 */
106 virtual ~IPV4Validator() {}
107
108 /**
109 * Pure virtual application operator. Apply the validation
110 * algorithm specific to derived classes.
111 */
112 virtual void
113 operator()(const in_addr address) const = 0;
114};
115
116/**
117 * Class for the function object that validates multicast addresses.
118 * Implements a specific application operator to validate multicast
119 * addresses.
120 *
121 * @author Federico Montesino <p5087@quintero.fie.us.es>
122 * @short Validating class specialized for multicast addresses.
123 */
125{
126private:
128
129public:
130 /**
131 * Constructor. Does not deal with any state.
132 */
134
135 /**
136 * Keeps compilers happy.
137 */
139
140 /**
141 * Application operator. Apply the validation algorithm
142 * specific to multicast addresses
143 */
144 void operator()(const in_addr address) const __OVERRIDE;
145};
146
147/**
148 * The CIDR class is used to support routing tables and validate address
149 * policies.
150 *
151 * @author David Sugar <dyfet@gnutelephony.org>
152 * @short Classless Internet Domain Routing
153 */
155{
156protected:
157 struct in_addr netmask, network;
158
159 unsigned getMask(const char *cp) const;
160public:
161 /**
162 * Get network address associated with this cidr.
163 *
164 * @return system binary coded address.
165 */
166 inline struct in_addr getNetwork(void) const {
167 return network;
168 }
169
170 /**
171 * Get network mask associated with this cidr.
172 *
173 * @return system binary coded network mask.
174 */
175 inline struct in_addr getNetmask(void) const {
176 return netmask;
177 }
178
179 /**
180 * Compute the broadcast address associated with this cidr.
181 *
182 * @return system binary coded network address.
183 */
184 struct in_addr getBroadcast(void) const;
185
186 /**
187 * Set the cidr from a full or partial hostname, or from an
188 * address/mask, or a host/bits specification.
189 *
190 * @param cidr string to use.
191 */
192 void set(const char *cidr);
193
194 /**
195 * Construct a new cidr from a string.
196 *
197 * @param cidr string to use.
198 */
199 IPV4Cidr(const char *cidr);
200
201 /**
202 * Construct an empty cidr.
203 */
204 IPV4Cidr();
205
206 /**
207 * Construct a copy of a cidr.
208 *
209 * @param cidr to copy from.
210 */
212
213 /**
214 * See if a socket address is a member of this cidr's network.
215 *
216 * @param saddr pointer to test.
217 * @return true if member of cidr.
218 */
219 bool isMember(const struct sockaddr *saddr) const;
220
221 /**
222 * See if a low level address object is a member of this cidr's net.
223 *
224 * @param inaddr object to test.
225 * @return true if member of cidr.
226 */
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/**
248 * The CIDR class is used to support routing tables and validate address
249 * policies.
250 *
251 * @author David Sugar <dyfet@gnutelephony.org>
252 * @short Classless Internet Domain Routing
253 */
254class __EXPORT IPV6Cidr
255{
256protected:
257 struct in6_addr netmask, network;
258
259 unsigned getMask(const char *cp) const;
260public:
261 /**
262 * Get network address associated with this cidr.
263 *
264 * @return system binary coded address.
265 */
266 inline struct in6_addr getNetwork(void) const {
267 return network;
268 }
269
270 /**
271 * Get network mask associated with this cidr.
272 *
273 * @return system binary coded network mask.
274 */
275 inline struct in6_addr getNetmask(void) const {
276 return netmask;
277 }
278
279 /**
280 * Compute the broadcast address associated with this cidr.
281 *
282 * @return system binary coded network address.
283 */
284 struct in6_addr getBroadcast(void) const;
285
286 /**
287 * Set the cidr from a full or partial hostname, or from a
288 * host/bits specification.
289 *
290 * @param cidr string to use.
291 */
292 void set(const char *cidr);
293
294 /**
295 * Construct a new cidr from a string.
296 *
297 * @param cidr string to use.
298 */
299 IPV6Cidr(const char *cidr);
300
301 /**
302 * Construct an empty cidr.
303 */
304 IPV6Cidr();
305
306 /**
307 * Construct a copy of a cidr.
308 *
309 * @param cidr to copy from.
310 */
311 IPV6Cidr(IPV6Cidr &);
312
313 /**
314 * See if a socket address is a member of this cidr's network.
315 *
316 * @param saddr pointer to test.
317 * @return true if member of cidr.
318 */
319 bool isMember(const struct sockaddr *saddr) const;
320
321 /**
322 * See if a low level address object is a member of this cidr's net.
323 *
324 * @param inaddr object to test.
325 * @return true if member of cidr.
326 */
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
348/**
349 * The network name and address objects are all derived from a common
350 * IPV4Address base class. Specific classes, such as IPV4Host,
351 * IPV4Mask, etc, are defined from IPV4Address entirely so that the
352 * manner a network address is being used can easily be documented and
353 * understood from the code and to avoid common errors and accidental misuse
354 * of the wrong address object. For example, a "connection" to something
355 * that is declared as a "IPV4Host" can be kept type-safe from a
356 * "connection" accidently being made to something that was declared a
357 * "IPV4Broadcast".
358 *
359 * @author David Sugar <dyfet@ostel.com>
360 * @short Internet Address binary data type.
361 */
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.
371
372protected:
373 struct in_addr * ipaddr;
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 /**
382 * Sets the IP address from a string representation of the
383 * numeric address, ie "127.0.0.1"
384 *
385 * @param host The string representation of the IP address
386 * @return true if successful
387 */
388 bool setIPAddress(const char *host);
389
390 /**
391 * Used to specify a host name or numeric internet address.
392 *
393 * @param host The string representation of the IP address or
394 * a hostname, , if NULL, it will default to INADDR_ANY
395 */
396 void setAddress(const char *host);
397
398public:
399 /**
400 * Create an Internet Address object with an empty (0.0.0.0)
401 * address.
402 *
403 * @param validator optional validator function object, intended for
404 * derived classes.
405 */
406 IPV4Address(const InetAddrValidator *validator = NULL);
407
408 /**
409 * Convert the system internet address data type (struct in_addr)
410 * into a Common C++ IPV4Address object.
411 *
412 * @param addr struct of system used binary internet address.
413 * @param validator optional validator function object, intended for
414 * derived classes.
415 */
416 IPV4Address(struct in_addr addr, const InetAddrValidator *validator = NULL);
417
418 /**
419 * Convert a null terminated ASCII host address string
420 * (example: "127.0.0.1") or host address name (example:
421 * "www.voxilla.org") directly into a Common C++ IPV4Address
422 * object.
423 *
424 * @param address null terminated C string.
425 * @param validator optional validator function object, intended for
426 * derived classes.
427 */
428 IPV4Address(const char *address, const InetAddrValidator *validator = NULL);
429
430 /**
431 * Copy constructor
432 */
433 IPV4Address(const IPV4Address &rhs);
434
435 /**
436 * Destructor
437 */
438 virtual ~IPV4Address();
439
440 /**
441 * Provide a string representation of the value (Internet Address)
442 * held in the IPV4Address object.
443 *
444 * @return string representation of IPV4Address.
445 */
446 const char *getHostname(void) const;
447
448 /**
449 * May be used to verify if a given IPV4Address returned
450 * by another function contains a "valid" address, or "0.0.0.0"
451 * which is often used to mark "invalid" IPV4Address values.
452 *
453 * @return true if address != 0.0.0.0.
454 */
455 bool isInetAddress(void) const;
456
457 /**
458 * Provide a low level system usable struct in_addr object from
459 * the contents of IPV4Address. This is needed for services such
460 * as bind() and connect().
461 *
462 * @return system binary coded internet address.
463 */
464 struct in_addr getAddress(void) const;
465
466 /**
467 * Provide a low level system usable struct in_addr object from
468 * the contents of IPV4Address. This is needed for services such
469 * as bind() and connect().
470 *
471 * @param i for IPV4Addresses with multiple addresses, returns the
472 * address at this index. User should call getAddressCount()
473 * to determine the number of address the object contains.
474 * @return system binary coded internet address. If parameter i is
475 * out of range, the first address is returned.
476 */
477 struct in_addr getAddress(size_t i) const;
478
479 /**
480 * Returns the number of internet addresses that an IPV4Address object
481 * contains. This usually only happens with IPV4Host objects
482 * where multiple IP addresses are returned for a DNS lookup
483 */
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
490 /**
491 * Allows assignment from the return of functions like
492 * inet_addr() or htonl()
493 */
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
504 /**
505 * Compare two internet addresses to see if they are equal
506 * (if they specify the physical address of the same internet host).
507 *
508 * If there is more than one IP address in either IPV4Address object,
509 * this will return true if all of the IP addresses in the smaller
510 * are in the larger in any order.
511 */
512 bool operator==(const IPV4Address &a) const;
513
514 /**
515 * Compare two internet addresses to see if they are not
516 * equal (if they each refer to unique and different physical
517 * ip addresses).
518 *
519 * This is implimented in terms of operator==
520 */
521 bool operator!=(const IPV4Address &a) const;
522};
523
524/**
525 * Internet addresses used specifically as masking addresses (such as "
526 * 255.255.255.0") are held in the IPV4Mask derived object. The
527 * seperate class is used so that C++ type casting can automatically
528 * determine when an IPV4Address object is really a mask address object
529 * rather than simply using the base class. This also allows manipulative
530 * operators for address masking to operate only when presented with a
531 * Masked address as well as providing cleaner and safer source.
532 *
533 * @author David Sugar <dyfet@ostel.com>
534 * @short Internet Address Mask such as subnet masks.
535 */
537{
538private:
540
541public:
542 /**
543 * Create the mask from a null terminated ASCII string such as
544 * "255.255.255.128".
545 *
546 * @param mask null terminated ASCII mask string.
547 */
548 IPV4Mask(const char *mask);
549
550 /**
551 * Masks are usually used to coerce host addresses into a specific
552 * router or class domain. This can be done by taking the Inet
553 * Host Address object and "and"ing it with an address mask. This
554 * operation can be directly expressed in C++ through the & operator.
555 *
556 * @return a internet host address that has been masked.
557 * @param addr host address to be masked by subnet.
558 * @param mask inetnet mask address object to mask by.
559 */
560 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask);
561
562 /**
563 * Allows assignment from the return of functions like
564 * inet_addr() or htonl()
565 */
566 IPV4Address &operator=(in_addr_t addr) {
567 return IPV4Address::operator =(addr);
568 }
569};
570
571/**
572 * This object is used to hold the actual and valid internet address of a
573 * specific host machine that will be accessed through a socket.
574 *
575 * @author David Sugar <dyfet@ostel.com>
576 * @short Address of a specific Internet host machine.
577 */
579{
580private:
582
583public:
584 /**
585 * Create a new host address for a specific internet host. The
586 * internet host can be specified in a null terminated ASCII
587 * string and include either the physical host address or the
588 * DNS name of a host machine. Hence, an IPV4Host
589 * ("www.voxilla.org") can be directly declaired in this manner.
590 *
591 * Defaults to the IP address that represents the interface matching
592 * "gethostname()".
593 *
594 * @param host dns or physical address of an Internet host.
595 */
596 IPV4Host(const char *host = NULL);
597
598 /**
599 * Convert a system socket binary address such as may be
600 * returned through the accept() call or getsockpeer() into
601 * an internet host address object.
602 *
603 * @param addr binary address of internet host.
604 */
605 IPV4Host(struct in_addr addr);
606
607 /**
608 * Allows assignment from the return of functions like
609 * inet_addr() or htonl()
610 */
611 IPV4Address &operator=(in_addr_t addr) {
612 return IPV4Address::operator =(addr);
613 }
614
615 /**
616 * Mask the internet host address object with a network mask address.
617 * This is commonly used to coerce an address by subnet.
618 */
619 IPV4Host &operator&=(const IPV4Mask &mask);
620
621 friend class IPV4Mask;
622 friend __EXPORT IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask);
623};
624
625/**
626 * The broadcast address object is used to store the broadcast address for
627 * a specific subnet. This is commonly used for UDP broadcast operations.
628 */
630{
631public:
632 /**
633 * Specify the physical broadcast address to use and create a new
634 * broadcast address object based on a null terminated ASCII
635 * string.
636 *
637 * @param net null terminated ASCII network address.
638 */
639 IPV4Broadcast(const char *net = "255.255.255.255");
640};
641
642/**
643 * A specialization of IPV4Address that provides address validation
644 * for multicast addresses. Whenever its value changes the new value
645 * is checked to be in the range from 224.0.0.1 through
646 * 239.255.255.255. If it is not, an exception is thrown.
647 *
648 * @short A multicast network address.
649 * @author Federico Montesino <p5087@quintero.fie.us.es>
650 */
652{
653public:
654 /**
655 * Create an Internet Multicast Address object with an empty
656 * (0.0.0.0) address.
657 */
659
660 /**
661 * Convert the system internet address data type (struct in_addr)
662 * into a Common C++ IPV4Multicast object.
663 *
664 * @param address struct of system used binary internet address.
665 */
666 IPV4Multicast(const struct in_addr address);
667
668 /**
669 * Convert a null terminated ASCII multicast address string
670 * (example: "224.0.0.1") or multicast name string (example:
671 * "sap.mcast.net") directly into a Common C++
672 * IPV4Multicast object. Works like IPV4Address(const
673 * char*).
674 *
675 * @param address null terminated C string.
676 */
677 IPV4Multicast(const char *address);
678
679private:
680 /**
681 * Check the address in <code>addr<code> is a valid multicast
682 * address. In case not, throws an exception.
683 *
684 * @param address a system network address
685 * @return true if validation succeeded
686 */
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
701/**
702 * Classes derived from IPV6Address would require an specific
703 * validator to pass to the IPV6Address constructor. This is a base
704 * class for classes of function objects used by such derived classes.
705 *
706 * @author Federico Montesino <p5087@quintero.fie.us.es>
707 * @short Abstract base class for derived inet addresses validators.
708 */
709class __EXPORT IPV6Validator
710{
711private:
712 __DELETE_COPY(IPV6Validator);
713
714public:
715 /**
716 * Constructor. Does not deal with any state.
717 */
718 IPV6Validator() { }
719
720 /**
721 * Keeps compilers happy.
722 */
723 virtual ~IPV6Validator() {}
724
725 /**
726 * Pure virtual application operator. Apply the validation
727 * algorithm specific to derived classes.
728 */
729 virtual void operator()(const in6_addr address) const = 0;
730};
731
732/**
733 * Class for the function object that validates multicast addresses.
734 * Implements a specific application operator to validate multicast
735 * addresses.
736 *
737 * @author Federico Montesino <p5087@quintero.fie.us.es>
738 * @short Validating class specialized for multicast addresses.
739 */
740class __EXPORT IPV6MulticastValidator: public IPV6Validator
741{
742private:
743 __DELETE_COPY(IPV6MulticastValidator);
744
745public:
746 /**
747 * Constructor. Does not deal with any state.
748 */
749 IPV6MulticastValidator(){}
750
751 /**
752 * Keeps compilers happy...
753 */
754 virtual ~IPV6MulticastValidator(){}
755
756 /**
757 * Application operator. Apply the validation algorithm
758 * specific to multicast addresses
759 */
760 void operator()(const in6_addr address) const __OVERRIDE;
761};
762
763/**
764 * The network name and address objects are all derived from a common
765 * IPV6Address base class. Specific classes, such as IPV4Host,
766 * IPV6Mask, etc, are defined from IPV6Address entirely so that the
767 * manner a network address is being used can easily be documented and
768 * understood from the code and to avoid common errors and accidental misuse
769 * of the wrong address object. For example, a "connection" to something
770 * that is declared as a "IPV6Host" can be kept type-safe from a
771 * "connection" accidently being made to something that was declared a
772 * "IPV6Broadcast".
773 *
774 * @author David Sugar <dyfet@ostel.com>
775 * @short Internet Address binary data type.
776 */
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
796 /**
797 * Sets the IP address from a string representation of the
798 * numeric address, ie "127.0.0.1"
799 *
800 * @param host The string representation of the IP address
801 * @return true if successful
802 */
803 bool setIPAddress(const char *host);
804
805 /**
806 * Used to specify a host name or numeric internet address.
807 *
808 * @param host The string representation of the IP address or
809 * a hostname, , if NULL, it will default to INADDR_ANY
810 */
811 void setAddress(const char *host);
812
813public:
814 /**
815 * Create an Internet Address object with an empty (0.0.0.0)
816 * address.
817 *
818 * @param validator optional validator function object, intended for
819 * derived classes.
820 */
821 IPV6Address(const IPV6Validator *validator = NULL);
822
823 /**
824 * Convert the system internet address data type (struct in_addr)
825 * into a Common C++ IPV6Address object.
826 *
827 * @param addr struct of system used binary internet address.
828 * @param validator optional validator function object, intended for
829 * derived classes.
830 */
831 IPV6Address(struct in6_addr addr, const IPV6Validator *validator = NULL);
832
833 /**
834 * Convert a null terminated ASCII host address string
835 * (example: "127.0.0.1") or host address name (example:
836 * "www.voxilla.org") directly into a Common C++ IPV6Address
837 * object.
838 *
839 * @param address null terminated C string.
840 * @param validator optional validator function object, intended for
841 * derived classes.
842 */
843 IPV6Address(const char *address, const IPV6Validator *validator = NULL);
844
845 /**
846 * Copy constructor
847 */
848 IPV6Address(const IPV6Address &rhs);
849
850 /**
851 * Destructor
852 */
853 virtual ~IPV6Address();
854
855 /**
856 * Provide a string representation of the value (Internet Address)
857 * held in the IPV6Address object.
858 *
859 * @return string representation of IPV6Address.
860 */
861 const char *getHostname(void) const;
862
863 /**
864 * May be used to verify if a given IPV6Address returned
865 * by another function contains a "valid" address, or "0.0.0.0"
866 * which is often used to mark "invalid" IPV6Address values.
867 *
868 * @return true if address != 0.0.0.0.
869 */
870 bool isInetAddress(void) const;
871
872 /**
873 * Provide a low level system usable struct in_addr object from
874 * the contents of IPV6Address. This is needed for services such
875 * as bind() and connect().
876 *
877 * @return system binary coded internet address.
878 */
879 struct in6_addr getAddress(void) const;
880
881 /**
882 * Provide a low level system usable struct in_addr object from
883 * the contents of IPV6Address. This is needed for services such
884 * as bind() and connect().
885 *
886 * @param i for IPV6Addresses with multiple addresses, returns the
887 * address at this index. User should call getAddressCount()
888 * to determine the number of address the object contains.
889 * @return system binary coded internet address. If parameter i is
890 * out of range, the first address is returned.
891 */
892 struct in6_addr getAddress(size_t i) const;
893
894 /**
895 * Returns the number of internet addresses that an IPV6Address object
896 * contains. This usually only happens with IPV6Host objects
897 * where multiple IP addresses are returned for a DNS lookup
898 */
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
915 /**
916 * Compare two internet addresses to see if they are equal
917 * (if they specify the physical address of the same internet host).
918 *
919 * If there is more than one IP address in either IPV6Address object,
920 * this will return true if all of the IP addresses in the smaller
921 * are in the larger in any order.
922 */
923 bool operator==(const IPV6Address &a) const;
924
925 /**
926 * Compare two internet addresses to see if they are not
927 * equal (if they each refer to unique and different physical
928 * ip addresses).
929 *
930 * This is implimented in terms of operator==
931 */
932 bool operator!=(const IPV6Address &a) const;
933};
934
935/**
936 * Internet addresses used specifically as masking addresses (such as "
937 * 255.255.255.0") are held in the IPV6Mask derived object. The
938 * seperate class is used so that C++ type casting can automatically
939 * determine when an IPV6Address object is really a mask address object
940 * rather than simply using the base class. This also allows manipulative
941 * operators for address masking to operate only when presented with a
942 * Masked address as well as providing cleaner and safer source.
943 *
944 * @author David Sugar <dyfet@ostel.com>
945 * @short Internet Address Mask such as subnet masks.
946 */
947class __EXPORT IPV6Mask : public IPV6Address
948{
949private:
950 __DELETE_COPY(IPV6Mask);
951
952public:
953 /**
954 * Create the mask from a null terminated ASCII string such as
955 * "255.255.255.128".
956 *
957 * @param mask null terminated ASCII mask string.
958 */
959 IPV6Mask(const char *mask);
960
961 /**
962 * Masks are usually used to coerce host addresses into a specific
963 * router or class domain. This can be done by taking the Inet
964 * Host Address object and "and"ing it with an address mask. This
965 * operation can be directly expressed in C++ through the & operator.
966 *
967 * @return a internet host address that has been masked.
968 * @param addr host address to be masked by subnet.
969 * @param mask inetnet mask address object to mask by.
970 */
971 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
972};
973
974/**
975 * This object is used to hold the actual and valid internet address of a
976 * specific host machine that will be accessed through a socket.
977 *
978 * @author David Sugar <dyfet@ostel.com>
979 * @short Address of a specific Internet host machine.
980 */
981class __EXPORT IPV6Host : public IPV6Address
982{
983public:
984 /**
985 * Create a new host address for a specific internet host. The
986 * internet host can be specified in a null terminated ASCII
987 * string and include either the physical host address or the
988 * DNS name of a host machine. Hence, an IPV6Host
989 * ("www.voxilla.org") can be directly declaired in this manner.
990 *
991 * Defaults to the IP address that represents the interface matching
992 * "gethostname()".
993 *
994 * @param host dns or physical address of an Internet host.
995 */
996 IPV6Host(const char *host = NULL);
997
998 /**
999 * Convert a system socket binary address such as may be
1000 * returned through the accept() call or getsockpeer() into
1001 * an internet host address object.
1002 *
1003 * @param addr binary address of internet host.
1004 */
1005 IPV6Host(struct in6_addr addr);
1006
1007 /**
1008 * Mask the internet host address object with a network mask address.
1009 * This is commonly used to coerce an address by subnet.
1010 */
1011 IPV6Host &operator&=(const IPV6Mask &mask);
1012
1013 friend class IPV6Mask;
1014 friend __EXPORT IPV6Host operator&(const IPV6Host &addr, const IPV6Mask &mask);
1015};
1016
1017/**
1018 * The broadcast address object is used to store the broadcast address for
1019 * a specific subnet. This is commonly used for UDP broadcast operations.
1020 */
1021class __EXPORT IPV6Broadcast : public IPV6Address
1022{
1023public:
1024 /**
1025 * Specify the physical broadcast address to use and create a new
1026 * broadcast address object based on a null terminated ASCII
1027 * string.
1028 *
1029 * @param net null terminated ASCII network address.
1030 */
1031 IPV6Broadcast(const char *net = "255.255.255.255");
1032};
1033
1034/**
1035 * A specialization of IPV6Address that provides address validation
1036 * for multicast addresses. Whenever its value changes the new value
1037 * is checked to be in the range from 224.0.0.1 through
1038 * 239.255.255.255. If it is not, an exception is thrown.
1039 *
1040 * @short A multicast network address.
1041 * @author Federico Montesino <p5087@quintero.fie.us.es>
1042 */
1043class __EXPORT IPV6Multicast: public IPV6Address
1044{
1045public:
1046 /**
1047 * Create an Internet Multicast Address object with an empty
1048 * (0.0.0.0) address.
1049 */
1050 IPV6Multicast();
1051
1052 /**
1053 * Convert the system internet address data type (struct in_addr)
1054 * into a Common C++ IPV4Multicast object.
1055 *
1056 * @param address struct of system used binary internet address.
1057 */
1058 IPV6Multicast(const struct in6_addr address);
1059
1060 /**
1061 * Convert a null terminated ASCII multicast address string
1062 * (example: "224.0.0.1") or multicast name string (example:
1063 * "sap.mcast.net") directly into a Common C++
1064 * IPV6Multicast object. Works like IPV6Address(const
1065 * char*).
1066 *
1067 * @param address null terminated C string.
1068 */
1069 IPV6Multicast(const char *address);
1070
1071private:
1072 /**
1073 * Check the address in <code>addr<code> is a valid multicast
1074 * address. In case not, throws an exception.
1075 *
1076 * @param address a system network address
1077 * @return true if validation succeeded
1078 */
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
#define InetAddrValidator
Definition: address.h:74
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:363
const IPV4Validator * validator
Definition: address.h:370
size_t addr_count
Definition: address.h:374
struct in_addr * ipaddr
Definition: address.h:373
static Mutex mutex
Definition: address.h:379
char * hostname
Definition: address.h:375
size_t getAddressCount() const
Returns the number of internet addresses that an IPV4Address object contains.
Definition: address.h:484
bool operator!() const
Definition: address.h:500
The broadcast address object is used to store the broadcast address for a specific subnet.
Definition: address.h:630
The CIDR class is used to support routing tables and validate address policies.
Definition: address.h:155
bool operator!=(const struct sockaddr *a) const
Definition: address.h:237
bool operator==(const struct in_addr &a) const
Definition: address.h:233
bool operator==(const struct sockaddr *a) const
Definition: address.h:229
bool operator!=(const struct in_addr &a) const
Definition: address.h:241
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:579
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:611
static IPV4Host _host_
Definition: address.h:581
Internet addresses used specifically as masking addresses (such as " 255.255.255.0") are held in the ...
Definition: address.h:537
IPV4Address & operator=(in_addr_t addr)
Allows assignment from the return of functions like inet_addr() or htonl()
Definition: address.h:566
__DELETE_COPY(IPV4Mask)
Class for the function object that validates multicast addresses.
Definition: address.h:125
virtual ~IPV4MulticastValidator()
Keeps compilers happy.
Definition: address.h:138
__DELETE_COPY(IPV4MulticastValidator)
IPV4MulticastValidator()
Constructor.
Definition: address.h:133
A specialization of IPV4Address that provides address validation for multicast addresses.
Definition: address.h:652
static const IPV4MulticastValidator validator
Check the address in addr is a valid multicast address.
Definition: address.h:687
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
__DELETE_COPY(IPV4Validator)
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition: thread.h:105
A class to hold internet segment routing rules.
Definition: socket.h:168
Automatic integer counting class.
Definition: counter.h:44
Common C++ thread class and sychronization objects.
#define __EXPORT
Definition: config.h:49
GNU Common C++ exception model base classes.
Definition: address.cpp:63
struct in_addr getaddress(const IPV4Address &ia)
Definition: address.h:692
in_port_t tpport_t
Transport Protocol Ports.
Definition: address.h:80
IPV4Host operator&(const IPV4Host &addr, const IPV4Mask &mask)
Definition: address.cpp:461
ostream & operator<<(ostream &os, const IPV4Address &ia)
Definition: address.cpp:908
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Definition: socket.h:2089
#define __OVERRIDE
Definition: platform.h:158
#define __DELETE_COPY(x)
Definition: platform.h:160