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 7 This library is distributed in the hope that it will be useful, but WITHOUT 8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 10 more details. 11 12 You should have received a copy of the GNU Lesser General Public License 13 along with this library; if not, write to the Free Software Foundation, Inc., 14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 15 **********/ 16 // "mTunnel" multicast access service 17 // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. 18 // Network Addresses 19 // C++ header 20 21 #ifndef _NET_ADDRESS_HH 22 #define _NET_ADDRESS_HH 23 24 #ifndef _HASH_TABLE_HH 25 #include "HashTable.hh" 26 #endif 27 28 #ifndef _NET_COMMON_H 29 #include "NetCommon.h" 30 #endif 31 32 #ifndef _USAGE_ENVIRONMENT_HH 33 #include "UsageEnvironment.hh" 34 #endif 35 36 // Definition of a type representing a low-level network address. 37 // At present, this is 32-bits, for IPv4. Later, generalize it, 38 // to allow for IPv6. 39 typedef u_int32_t netAddressBits; // deprecated 40 41 typedef u_int32_t ipv4AddressBits; 42 typedef u_int8_t ipv6AddressBits[16]; // 128 bits 43 44 class NetAddress { 45 public: 46 NetAddress(u_int8_t const* data, 47 unsigned length = 4 /* default: 32 bits (for IPv4); use 16 (128 bits) for IPv6 */); 48 NetAddress(unsigned length = 4); // sets address data to all-zeros 49 NetAddress(NetAddress const& orig); 50 NetAddress& operator=(NetAddress const& rightSide); 51 virtual ~NetAddress(); 52 length() const53 unsigned length() const { return fLength; } data() const54 u_int8_t const* data() const // always in network byte order 55 { return fData; } 56 57 private: 58 void assign(u_int8_t const* data, unsigned length); 59 void clean(); 60 61 unsigned fLength; 62 u_int8_t* fData; 63 }; 64 65 class NetAddressList { 66 public: 67 NetAddressList(char const* hostname); 68 NetAddressList(NetAddressList const& orig); 69 NetAddressList& operator=(NetAddressList const& rightSide); 70 virtual ~NetAddressList(); 71 numAddresses() const72 unsigned numAddresses() const { return fNumAddresses; } 73 74 NetAddress const* firstAddress() const; 75 76 // Used to iterate through the addresses in a list: 77 class Iterator { 78 public: 79 Iterator(NetAddressList const& addressList); 80 NetAddress const* nextAddress(); // NULL iff none 81 private: 82 NetAddressList const& fAddressList; 83 unsigned fNextIndex; 84 }; 85 86 private: 87 void assign(unsigned numAddresses, NetAddress** addressArray); 88 void clean(); 89 90 friend class Iterator; 91 unsigned fNumAddresses; 92 NetAddress** fAddressArray; 93 }; 94 95 typedef u_int16_t portNumBits; 96 97 class Port { 98 public: 99 Port(portNumBits num /* in host byte order */); 100 num() const101 portNumBits num() const { return fPortNum; } // in network byte order 102 103 private: 104 portNumBits fPortNum; // stored in network byte order 105 #ifdef IRIX 106 portNumBits filler; // hack to overcome a bug in IRIX C++ compiler 107 #endif 108 }; 109 110 UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p); 111 112 113 // A generic table for looking up objects by (address1, address2, port) 114 class AddressPortLookupTable { 115 public: 116 AddressPortLookupTable(); 117 virtual ~AddressPortLookupTable(); 118 119 void* Add(netAddressBits address1, netAddressBits address2, Port port, void* value); 120 // Returns the old value if different, otherwise 0 121 Boolean Remove(netAddressBits address1, netAddressBits address2, Port port); 122 void* Lookup(netAddressBits address1, netAddressBits address2, Port port); 123 // Returns 0 if not found RemoveNext()124 void* RemoveNext() { return fTable->RemoveNext(); } 125 126 // Used to iterate through the entries in the table 127 class Iterator { 128 public: 129 Iterator(AddressPortLookupTable& table); 130 virtual ~Iterator(); 131 132 void* next(); // NULL iff none 133 134 private: 135 HashTable::Iterator* fIter; 136 }; 137 138 private: 139 friend class Iterator; 140 HashTable* fTable; 141 }; 142 143 144 Boolean IsMulticastAddress(netAddressBits address); 145 146 147 // A mechanism for displaying an IP (v4 or v6) address in ASCII. 148 // (This encapsulates the "inet_ntop()" function.) 149 class AddressString { 150 public: 151 // IPv4 input: 152 AddressString(struct sockaddr_in const& addr); 153 AddressString(struct in_addr const& addr); 154 AddressString(ipv4AddressBits const& addr); // "addr" is assumed to be in host byte order 155 156 // IPv6 input: 157 AddressString(struct sockaddr_in6 const& addr); 158 AddressString(struct in6_addr const& addr); 159 AddressString(ipv6AddressBits const& addr); // "addr" is assumed to be in host byte order 160 161 // IPv4 or IPv6 input: 162 AddressString(struct sockaddr_storage const& addr); 163 164 virtual ~AddressString(); 165 val() const166 char const* val() const { return fVal; } 167 168 private: 169 void init(ipv4AddressBits const& addr); // used to implement the IPv4 constructors 170 void init(ipv6AddressBits const& addr); // used to implement the IPv6 constructors 171 172 private: 173 char* fVal; // The result ASCII string: allocated by the constructor; deleted by the destructor 174 }; 175 176 portNumBits portNum(struct sockaddr_storage const& addr); 177 178 #endif 179