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