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