groupsock/NetAddress.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "mTunnel" multicast access service
00017 // Copyright (c) 1996-2010 Live Networks, Inc.  All rights reserved.
00018 // Network Addresses
00019 // Implementation
00020 
00021 #include "NetAddress.hh"
00022 #include "GroupsockHelper.hh"
00023 
00024 #include <stddef.h>
00025 #if defined(__WIN32__) || defined(_WIN32)
00026 #else
00027 #ifndef INADDR_NONE
00028 #define INADDR_NONE 0xFFFFFFFF
00029 #endif
00030 #endif
00031 
00033 
00034 NetAddress::NetAddress(u_int8_t const* data, unsigned length) {
00035   assign(data, length);
00036 }
00037 
00038 NetAddress::NetAddress(unsigned length) {
00039   fData = new u_int8_t[length];
00040   if (fData == NULL) {
00041     fLength = 0;
00042     return;
00043   }
00044 
00045   for (unsigned i = 0; i < length; ++i) fData[i] = 0;
00046   fLength = length;
00047 }
00048 
00049 NetAddress::NetAddress(NetAddress const& orig) {
00050   assign(orig.data(), orig.length());
00051 }
00052 
00053 NetAddress& NetAddress::operator=(NetAddress const& rightSide) {
00054   if (&rightSide != this) {
00055     clean();
00056     assign(rightSide.data(), rightSide.length());
00057   }
00058   return *this;
00059 }
00060 
00061 NetAddress::~NetAddress() {
00062   clean();
00063 }
00064 
00065 void NetAddress::assign(u_int8_t const* data, unsigned length) {
00066   fData = new u_int8_t[length];
00067   if (fData == NULL) {
00068     fLength = 0;
00069     return;
00070   }
00071 
00072   for (unsigned i = 0; i < length; ++i) fData[i] = data[i];
00073   fLength = length;
00074 }
00075 
00076 void NetAddress::clean() {
00077   delete[] fData; fData = NULL;
00078   fLength = 0;
00079 }
00080 
00081 
00083 
00084 NetAddressList::NetAddressList(char const* hostname)
00085   : fNumAddresses(0), fAddressArray(NULL) {
00086     struct hostent* host;
00087 
00088     // Check first whether "hostname" is an IP address string:
00089     netAddressBits addr = our_inet_addr((char*)hostname);
00090     if (addr != INADDR_NONE) { // yes it was an IP address string
00091       //##### host = gethostbyaddr((char*)&addr, sizeof (netAddressBits), AF_INET);
00092       host = NULL; // don't bother calling gethostbyaddr(); we only want 1 addr
00093 
00094       if (host == NULL) {
00095         // For some unknown reason, gethostbyaddr() failed, so just
00096         // return a 1-element list with the address we were given:
00097         fNumAddresses = 1;
00098         fAddressArray = new NetAddress*[fNumAddresses];
00099         if (fAddressArray == NULL) return;
00100 
00101         fAddressArray[0] = new NetAddress((u_int8_t*)&addr,
00102                                           sizeof (netAddressBits));
00103         return;
00104       }
00105     } else { // Try resolving "hostname" as a real host name
00106 
00107 #if defined(VXWORKS)
00108       char hostentBuf[512];
00109       host = (struct hostent*)resolvGetHostByName((char*)hostname,(char*)&hostentBuf,sizeof hostentBuf);
00110 #else
00111       host = our_gethostbyname((char*)hostname);
00112 #endif
00113 
00114       if (host == NULL) {
00115         // It was a host name, and we couldn't resolve it.  We're SOL.
00116         return;
00117       }
00118     }
00119 
00120     u_int8_t const** const hAddrPtr
00121       = (u_int8_t const**)host->h_addr_list;
00122     if (hAddrPtr != NULL) {
00123       // First, count the number of addresses:
00124       u_int8_t const** hAddrPtr1 = hAddrPtr;
00125       while (*hAddrPtr1 != NULL) {
00126         ++fNumAddresses;
00127         ++hAddrPtr1;
00128       }
00129 
00130       // Next, set up the list:
00131       fAddressArray = new NetAddress*[fNumAddresses];
00132       if (fAddressArray == NULL) return;
00133 
00134       for (unsigned i = 0; i < fNumAddresses; ++i) {
00135         fAddressArray[i]
00136           = new NetAddress(hAddrPtr[i], host->h_length);
00137       }
00138     }
00139 }
00140 
00141 NetAddressList::NetAddressList(NetAddressList const& orig) {
00142   assign(orig.numAddresses(), orig.fAddressArray);
00143 }
00144 
00145 NetAddressList& NetAddressList::operator=(NetAddressList const& rightSide) {
00146   if (&rightSide != this) {
00147     clean();
00148     assign(rightSide.numAddresses(), rightSide.fAddressArray);
00149   }
00150   return *this;
00151 }
00152 
00153 NetAddressList::~NetAddressList() {
00154   clean();
00155 }
00156 
00157 void NetAddressList::assign(unsigned numAddresses, NetAddress** addressArray) {
00158   fAddressArray = new NetAddress*[numAddresses];
00159   if (fAddressArray == NULL) {
00160     fNumAddresses = 0;
00161     return;
00162   }
00163 
00164   for (unsigned i = 0; i < numAddresses; ++i) {
00165     fAddressArray[i] = new NetAddress(*addressArray[i]);
00166   }
00167   fNumAddresses = numAddresses;
00168 }
00169 
00170 void NetAddressList::clean() {
00171   while (fNumAddresses-- > 0) {
00172     delete fAddressArray[fNumAddresses];
00173   }
00174   delete[] fAddressArray; fAddressArray = NULL;
00175 }
00176 
00177 NetAddress const* NetAddressList::firstAddress() const {
00178   if (fNumAddresses == 0) return NULL;
00179 
00180   return fAddressArray[0];
00181 }
00182 
00184 NetAddressList::Iterator::Iterator(NetAddressList const& addressList)
00185   : fAddressList(addressList), fNextIndex(0) {}
00186 
00187 NetAddress const* NetAddressList::Iterator::nextAddress() {
00188   if (fNextIndex >= fAddressList.numAddresses()) return NULL; // no more
00189   return fAddressList.fAddressArray[fNextIndex++];
00190 }
00191 
00192 
00194 
00195 Port::Port(portNumBits num /* in host byte order */) {
00196   fPortNum = htons(num);
00197 }
00198 
00199 UsageEnvironment& operator<<(UsageEnvironment& s, const Port& p) {
00200   return s << ntohs(p.num());
00201 }
00202 
00203 
00205 
00206 AddressPortLookupTable::AddressPortLookupTable()
00207   : fTable(HashTable::create(3)) { // three-word keys are used
00208 }
00209 
00210 AddressPortLookupTable::~AddressPortLookupTable() {
00211   delete fTable;
00212 }
00213 
00214 void* AddressPortLookupTable::Add(netAddressBits address1,
00215                                   netAddressBits address2,
00216                                   Port port, void* value) {
00217   int key[3];
00218   key[0] = (int)address1;
00219   key[1] = (int)address2;
00220   key[2] = (int)port.num();
00221   return fTable->Add((char*)key, value);
00222 }
00223 
00224 void* AddressPortLookupTable::Lookup(netAddressBits address1,
00225                                      netAddressBits address2,
00226                                      Port port) {
00227   int key[3];
00228   key[0] = (int)address1;
00229   key[1] = (int)address2;
00230   key[2] = (int)port.num();
00231   return fTable->Lookup((char*)key);
00232 }
00233 
00234 Boolean AddressPortLookupTable::Remove(netAddressBits address1,
00235                                        netAddressBits address2,
00236                                        Port port) {
00237   int key[3];
00238   key[0] = (int)address1;
00239   key[1] = (int)address2;
00240   key[2] = (int)port.num();
00241   return fTable->Remove((char*)key);
00242 }
00243 
00244 AddressPortLookupTable::Iterator::Iterator(AddressPortLookupTable& table)
00245   : fIter(HashTable::Iterator::create(*(table.fTable))) {
00246 }
00247 
00248 AddressPortLookupTable::Iterator::~Iterator() {
00249   delete fIter;
00250 }
00251 
00252 void* AddressPortLookupTable::Iterator::next() {
00253   char const* key; // dummy
00254   return fIter->next(key);
00255 }
00256 
00258 
00259 Boolean IsMulticastAddress(netAddressBits address) {
00260   // Note: We return False for addresses in the range 224.0.0.0
00261   // through 224.0.0.255, because these are non-routable
00262   // Note: IPv4-specific #####
00263   netAddressBits addressInHostOrder = ntohl(address);
00264   return addressInHostOrder >  0xE00000FF &&
00265          addressInHostOrder <= 0xEFFFFFFF;
00266 }

Generated on Fri Sep 3 02:35:40 2010 for live by  doxygen 1.5.2