NetAddress.hh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "mTunnel" multicast access service
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // Network Addresses
  17. // C++ header
  18. #ifndef _NET_ADDRESS_HH
  19. #define _NET_ADDRESS_HH
  20. #ifndef _HASH_TABLE_HH
  21. #include "HashTable.hh"
  22. #endif
  23. #ifndef _NET_COMMON_H
  24. #include "NetCommon.h"
  25. #endif
  26. #ifndef _USAGE_ENVIRONMENT_HH
  27. #include "UsageEnvironment.hh"
  28. #endif
  29. // Definition of a type representing a low-level network address.
  30. // At present, this is 32-bits, for IPv4. Later, generalize it,
  31. // to allow for IPv6.
  32. typedef u_int32_t netAddressBits;
  33. class NetAddress {
  34. public:
  35. NetAddress(u_int8_t const* data,
  36. unsigned length = 4 /* default: 32 bits */);
  37. NetAddress(unsigned length = 4); // sets address data to all-zeros
  38. NetAddress(NetAddress const& orig);
  39. NetAddress& operator=(NetAddress const& rightSide);
  40. virtual ~NetAddress();
  41. unsigned length() const { return fLength; }
  42. u_int8_t const* data() const // always in network byte order
  43. { return fData; }
  44. private:
  45. void assign(u_int8_t const* data, unsigned length);
  46. void clean();
  47. unsigned fLength;
  48. u_int8_t* fData;
  49. };
  50. class NetAddressList {
  51. public:
  52. NetAddressList(char const* hostname);
  53. NetAddressList(NetAddressList const& orig);
  54. NetAddressList& operator=(NetAddressList const& rightSide);
  55. virtual ~NetAddressList();
  56. unsigned numAddresses() const { return fNumAddresses; }
  57. NetAddress const* firstAddress() const;
  58. // Used to iterate through the addresses in a list:
  59. class Iterator {
  60. public:
  61. Iterator(NetAddressList const& addressList);
  62. NetAddress const* nextAddress(); // NULL iff none
  63. private:
  64. NetAddressList const& fAddressList;
  65. unsigned fNextIndex;
  66. };
  67. private:
  68. void assign(netAddressBits numAddresses, NetAddress** addressArray);
  69. void clean();
  70. friend class Iterator;
  71. unsigned fNumAddresses;
  72. NetAddress** fAddressArray;
  73. };
  74. typedef u_int16_t portNumBits;
  75. class Port {
  76. public:
  77. Port(portNumBits num /* in host byte order */);
  78. portNumBits num() const { return fPortNum; } // in network byte order
  79. private:
  80. portNumBits fPortNum; // stored in network byte order
  81. #ifdef IRIX
  82. portNumBits filler; // hack to overcome a bug in IRIX C++ compiler
  83. #endif
  84. };
  85. UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p);
  86. // A generic table for looking up objects by (address1, address2, port)
  87. class AddressPortLookupTable {
  88. public:
  89. AddressPortLookupTable();
  90. virtual ~AddressPortLookupTable();
  91. void* Add(netAddressBits address1, netAddressBits address2, Port port, void* value);
  92. // Returns the old value if different, otherwise 0
  93. Boolean Remove(netAddressBits address1, netAddressBits address2, Port port);
  94. void* Lookup(netAddressBits address1, netAddressBits address2, Port port);
  95. // Returns 0 if not found
  96. void* RemoveNext() { return fTable->RemoveNext(); }
  97. // Used to iterate through the entries in the table
  98. class Iterator {
  99. public:
  100. Iterator(AddressPortLookupTable& table);
  101. virtual ~Iterator();
  102. void* next(); // NULL iff none
  103. private:
  104. HashTable::Iterator* fIter;
  105. };
  106. private:
  107. friend class Iterator;
  108. HashTable* fTable;
  109. };
  110. Boolean IsMulticastAddress(netAddressBits address);
  111. // A mechanism for displaying an IPv4 address in ASCII. This is intended to replace "inet_ntoa()", which is not thread-safe.
  112. class AddressString {
  113. public:
  114. AddressString(struct sockaddr_in const& addr);
  115. AddressString(struct in_addr const& addr);
  116. AddressString(netAddressBits addr); // "addr" is assumed to be in host byte order here
  117. virtual ~AddressString();
  118. char const* val() const { return fVal; }
  119. private:
  120. void init(netAddressBits addr); // used to implement each of the constructors
  121. private:
  122. char* fVal; // The result ASCII string: allocated by the constructor; deleted by the destructor
  123. };
  124. #endif