1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**********/
// "mTunnel" multicast access service
// Copyright (c) 1996-2012 Live Networks, Inc. All rights reserved.
// Network Addresses
// Implementation
#include "NetAddress.hh"
#include "GroupsockHelper.hh"
#include <stddef.h>
#include <stdio.h>
#if defined(__WIN32__) || defined(_WIN32)
#define USE_GETHOSTBYNAME 1 /*because at least some Windows don't have getaddrinfo()*/
#else
#ifndef INADDR_NONE
#define INADDR_NONE 0xFFFFFFFF
#endif
#endif
////////// NetAddress //////////
NetAddress::NetAddress(u_int8_t const* data, unsigned length) {
assign(data, length);
}
NetAddress::NetAddress(unsigned length) {
fData = new u_int8_t[length];
if (fData == NULL) {
fLength = 0;
return;
}
for (unsigned i = 0; i < length; ++i) fData[i] = 0;
fLength = length;
}
NetAddress::NetAddress(NetAddress const& orig) {
assign(orig.data(), orig.length());
}
NetAddress& NetAddress::operator=(NetAddress const& rightSide) {
if (&rightSide != this) {
clean();
assign(rightSide.data(), rightSide.length());
}
return *this;
}
NetAddress::~NetAddress() {
clean();
}
void NetAddress::assign(u_int8_t const* data, unsigned length) {
fData = new u_int8_t[length];
if (fData == NULL) {
fLength = 0;
return;
}
for (unsigned i = 0; i < length; ++i) fData[i] = data[i];
fLength = length;
}
void NetAddress::clean() {
delete[] fData; fData = NULL;
fLength = 0;
}
////////// NetAddressList //////////
NetAddressList::NetAddressList(char const* hostname)
: fNumAddresses(0), fAddressArray(NULL) {
// First, check whether "hostname" is an IP address string:
netAddressBits addr = our_inet_addr((char*)hostname);
if (addr != INADDR_NONE) {
// Yes, it was an IP address string. Return a 1-element list with this address:
fNumAddresses = 1;
fAddressArray = new NetAddress*[fNumAddresses];
if (fAddressArray == NULL) return;
fAddressArray[0] = new NetAddress((u_int8_t*)&addr, sizeof (netAddressBits));
return;
}
// "hostname" is not an IP address string; try resolving it as a real host name instead:
#if defined(USE_GETHOSTBYNAME) || defined(VXWORKS)
struct hostent* host;
#if defined(VXWORKS)
char hostentBuf[512];
host = (struct hostent*)resolvGetHostByName((char*)hostname, (char*)&hostentBuf, sizeof hostentBuf);
#else
host = gethostbyname((char*)hostname);
#endif
if (host == NULL || host->h_length != 4 || host->h_addr_list == NULL) return; // no luck
u_int8_t const** const hAddrPtr = (u_int8_t const**)host->h_addr_list;
// First, count the number of addresses:
u_int8_t const** hAddrPtr1 = hAddrPtr;
while (*hAddrPtr1 != NULL) {
++fNumAddresses;
++hAddrPtr1;
}
// Next, set up the list:
fAddressArray = new NetAddress*[fNumAddresses];
if (fAddressArray == NULL) return;
for (unsigned i = 0; i < fNumAddresses; ++i) {
fAddressArray[i] = new NetAddress(hAddrPtr[i], host->h_length);
}
#else
// Use "getaddrinfo()" (rather than the older, deprecated "gethostbyname()"):
struct addrinfo addrinfoHints;
memset(&addrinfoHints, 0, sizeof addrinfoHints);
addrinfoHints.ai_family = AF_INET; // For now, we're interested in IPv4 addresses only
struct addrinfo* addrinfoResultPtr = NULL;
int result = getaddrinfo(hostname, NULL, &addrinfoHints, &addrinfoResultPtr);
if (result != 0 || addrinfoResultPtr == NULL) return; // no luck
// First, count the number of addresses:
const struct addrinfo* p = addrinfoResultPtr;
while (p != NULL) {
if (p->ai_addrlen < 4) continue; // sanity check: skip over addresses that are too small
++fNumAddresses;
p = p->ai_next;
}
// Next, set up the list:
fAddressArray = new NetAddress*[fNumAddresses];
if (fAddressArray == NULL) return;
unsigned i = 0;
p = addrinfoResultPtr;
while (p != NULL) {
if (p->ai_addrlen < 4) continue;
fAddressArray[i++] = new NetAddress((u_int8_t const*)&(((struct sockaddr_in*)p->ai_addr)->sin_addr.s_addr), 4);
p = p->ai_next;
}
// Finally, free the data that we had allocated by calling "getaddrinfo()":
freeaddrinfo(addrinfoResultPtr);
#endif
}
NetAddressList::NetAddressList(NetAddressList const& orig) {
assign(orig.numAddresses(), orig.fAddressArray);
}
NetAddressList& NetAddressList::operator=(NetAddressList const& rightSide) {
if (&rightSide != this) {
clean();
assign(rightSide.numAddresses(), rightSide.fAddressArray);
}
return *this;
}
NetAddressList::~NetAddressList() {
clean();
}
void NetAddressList::assign(unsigned numAddresses, NetAddress** addressArray) {
fAddressArray = new NetAddress*[numAddresses];
if (fAddressArray == NULL) {
fNumAddresses = 0;
return;
}
for (unsigned i = 0; i < numAddresses; ++i) {
fAddressArray[i] = new NetAddress(*addressArray[i]);
}
fNumAddresses = numAddresses;
}
void NetAddressList::clean() {
while (fNumAddresses-- > 0) {
delete fAddressArray[fNumAddresses];
}
delete[] fAddressArray; fAddressArray = NULL;
}
NetAddress const* NetAddressList::firstAddress() const {
if (fNumAddresses == 0) return NULL;
return fAddressArray[0];
}
////////// NetAddressList::Iterator //////////
NetAddressList::Iterator::Iterator(NetAddressList const& addressList)
: fAddressList(addressList), fNextIndex(0) {}
NetAddress const* NetAddressList::Iterator::nextAddress() {
if (fNextIndex >= fAddressList.numAddresses()) return NULL; // no more
return fAddressList.fAddressArray[fNextIndex++];
}
////////// Port //////////
Port::Port(portNumBits num /* in host byte order */) {
fPortNum = htons(num);
}
UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p) {
return s << ntohs(p.num());
}
////////// AddressPortLookupTable //////////
AddressPortLookupTable::AddressPortLookupTable()
: fTable(HashTable::create(3)) { // three-word keys are used
}
AddressPortLookupTable::~AddressPortLookupTable() {
delete fTable;
}
void* AddressPortLookupTable::Add(netAddressBits address1,
netAddressBits address2,
Port port, void* value) {
int key[3];
key[0] = (int)address1;
key[1] = (int)address2;
key[2] = (int)port.num();
return fTable->Add((char*)key, value);
}
void* AddressPortLookupTable::Lookup(netAddressBits address1,
netAddressBits address2,
Port port) {
int key[3];
key[0] = (int)address1;
key[1] = (int)address2;
key[2] = (int)port.num();
return fTable->Lookup((char*)key);
}
Boolean AddressPortLookupTable::Remove(netAddressBits address1,
netAddressBits address2,
Port port) {
int key[3];
key[0] = (int)address1;
key[1] = (int)address2;
key[2] = (int)port.num();
return fTable->Remove((char*)key);
}
AddressPortLookupTable::Iterator::Iterator(AddressPortLookupTable& table)
: fIter(HashTable::Iterator::create(*(table.fTable))) {
}
AddressPortLookupTable::Iterator::~Iterator() {
delete fIter;
}
void* AddressPortLookupTable::Iterator::next() {
char const* key; // dummy
return fIter->next(key);
}
////////// isMulticastAddress() implementation //////////
Boolean IsMulticastAddress(netAddressBits address) {
// Note: We return False for addresses in the range 224.0.0.0
// through 224.0.0.255, because these are non-routable
// Note: IPv4-specific #####
netAddressBits addressInNetworkOrder = htonl(address);
return addressInNetworkOrder > 0xE00000FF &&
addressInNetworkOrder <= 0xEFFFFFFF;
}
////////// AddressString implementation //////////
AddressString::AddressString(struct sockaddr_in const& addr) {
init(addr.sin_addr.s_addr);
}
AddressString::AddressString(struct in_addr const& addr) {
init(addr.s_addr);
}
AddressString::AddressString(netAddressBits addr) {
init(addr);
}
void AddressString::init(netAddressBits addr) {
fVal = new char[16]; // large enough for "abc.def.ghi.jkl"
netAddressBits addrNBO = htonl(addr); // make sure we have a value in a known byte order: big endian
sprintf(fVal, "%u.%u.%u.%u", (addrNBO>>24)&0xFF, (addrNBO>>16)&0xFF, (addrNBO>>8)&0xFF, addrNBO&0xFF);
}
AddressString::~AddressString() {
delete[] fVal;
}
|