NetAddressList Class Reference

#include <NetAddress.hh>

Collaboration diagram for NetAddressList:

Collaboration graph
[legend]

Public Member Functions

 NetAddressList (char const *hostname)
 NetAddressList (NetAddressList const &orig)
NetAddressListoperator= (NetAddressList const &rightSide)
virtual ~NetAddressList ()
unsigned numAddresses () const
NetAddress const * firstAddress () const

Private Member Functions

void assign (netAddressBits numAddresses, NetAddress **addressArray)
void clean ()

Private Attributes

unsigned fNumAddresses
NetAddress ** fAddressArray

Friends

class Iterator

Data Structures

class  Iterator

Detailed Description

Definition at line 62 of file NetAddress.hh.


Constructor & Destructor Documentation

NetAddressList::NetAddressList ( char const *  hostname  ) 

Definition at line 86 of file NetAddress.cpp.

References fAddressArray, fNumAddresses, INADDR_NONE, NULL, our_inet_addr(), and while().

00087   : fNumAddresses(0), fAddressArray(NULL) {
00088   // First, check whether "hostname" is an IP address string:
00089   netAddressBits addr = our_inet_addr((char*)hostname);
00090   if (addr != INADDR_NONE) {
00091     // Yes, it was an IP address string.  Return a 1-element list with this address:
00092     fNumAddresses = 1;
00093     fAddressArray = new NetAddress*[fNumAddresses];
00094     if (fAddressArray == NULL) return;
00095 
00096     fAddressArray[0] = new NetAddress((u_int8_t*)&addr, sizeof (netAddressBits));
00097     return;
00098   }
00099     
00100   // "hostname" is not an IP address string; try resolving it as a real host name instead:
00101 #if defined(USE_GETHOSTBYNAME) || defined(VXWORKS)
00102   struct hostent* host;
00103 #if defined(VXWORKS)
00104   char hostentBuf[512];
00105 
00106   host = (struct hostent*)resolvGetHostByName((char*)hostname, (char*)&hostentBuf, sizeof hostentBuf);
00107 #else
00108   host = gethostbyname((char*)hostname);
00109 #endif
00110   if (host == NULL || host->h_length != 4 || host->h_addr_list == NULL) return; // no luck
00111 
00112   u_int8_t const** const hAddrPtr = (u_int8_t const**)host->h_addr_list;
00113   // First, count the number of addresses:
00114   u_int8_t const** hAddrPtr1 = hAddrPtr;
00115   while (*hAddrPtr1 != NULL) {
00116     ++fNumAddresses;
00117     ++hAddrPtr1;
00118   }
00119 
00120   // Next, set up the list:
00121   fAddressArray = new NetAddress*[fNumAddresses];
00122   if (fAddressArray == NULL) return;
00123 
00124   for (unsigned i = 0; i < fNumAddresses; ++i) {
00125     fAddressArray[i] = new NetAddress(hAddrPtr[i], host->h_length);
00126   }
00127 #else
00128   // Use "getaddrinfo()" (rather than the older, deprecated "gethostbyname()"):
00129   struct addrinfo addrinfoHints;
00130   memset(&addrinfoHints, 0, sizeof addrinfoHints);
00131   addrinfoHints.ai_family = AF_INET; // For now, we're interested in IPv4 addresses only
00132   struct addrinfo* addrinfoResultPtr = NULL;
00133   int result = getaddrinfo(hostname, NULL, &addrinfoHints, &addrinfoResultPtr);
00134   if (result != 0 || addrinfoResultPtr == NULL) return; // no luck
00135 
00136   // First, count the number of addresses:
00137   const struct addrinfo* p = addrinfoResultPtr;
00138   while (p != NULL) {
00139     if (p->ai_addrlen < 4) continue; // sanity check: skip over addresses that are too small
00140     ++fNumAddresses;
00141     p = p->ai_next;
00142   }
00143 
00144   // Next, set up the list:
00145   fAddressArray = new NetAddress*[fNumAddresses];
00146   if (fAddressArray == NULL) return;
00147 
00148   unsigned i = 0;
00149   p = addrinfoResultPtr;
00150   while (p != NULL) {
00151     if (p->ai_addrlen < 4) continue;
00152     fAddressArray[i++] = new NetAddress((u_int8_t const*)&(((struct sockaddr_in*)p->ai_addr)->sin_addr.s_addr), 4);
00153     p = p->ai_next;
00154   }
00155 
00156   // Finally, free the data that we had allocated by calling "getaddrinfo()":
00157   freeaddrinfo(addrinfoResultPtr);
00158 #endif
00159 }

NetAddressList::NetAddressList ( NetAddressList const &  orig  ) 

Definition at line 161 of file NetAddress.cpp.

References assign(), fAddressArray, and numAddresses().

00161                                                          {
00162   assign(orig.numAddresses(), orig.fAddressArray);
00163 }

NetAddressList::~NetAddressList (  )  [virtual]

Definition at line 173 of file NetAddress.cpp.

References clean().

00173                                 {
00174   clean();
00175 }


Member Function Documentation

NetAddressList & NetAddressList::operator= ( NetAddressList const &  rightSide  ) 

Definition at line 165 of file NetAddress.cpp.

References assign(), clean(), fAddressArray, and numAddresses().

00165                                                                          {
00166   if (&rightSide != this) {
00167     clean();
00168     assign(rightSide.numAddresses(), rightSide.fAddressArray);
00169   }
00170   return *this;
00171 }

unsigned NetAddressList::numAddresses (  )  const [inline]

Definition at line 69 of file NetAddress.hh.

References fNumAddresses.

Referenced by MediaSubsession::connectionEndpointAddress(), getSDPDescription(), main(), NetAddressList(), NetAddressList::Iterator::nextAddress(), operator=(), RTSPClient::parseRTSPURL(), SIPClient::parseSIPURL(), parseSourceFilterAttribute(), and DarwinInjector::setDestination().

00069 { return fNumAddresses; }

NetAddress const * NetAddressList::firstAddress (  )  const

Definition at line 197 of file NetAddress.cpp.

References fAddressArray, fNumAddresses, and NULL.

Referenced by MediaSubsession::connectionEndpointAddress(), getSDPDescription(), main(), RTSPClient::parseRTSPURL(), SIPClient::parseSIPURL(), parseSourceFilterAttribute(), and DarwinInjector::setDestination().

00197                                                      {
00198   if (fNumAddresses == 0) return NULL;
00199 
00200   return fAddressArray[0];
00201 }

void NetAddressList::assign ( netAddressBits  numAddresses,
NetAddress **  addressArray 
) [private]

Referenced by NetAddressList(), and operator=().

void NetAddressList::clean (  )  [private]

Definition at line 190 of file NetAddress.cpp.

References fAddressArray, fNumAddresses, and NULL.

Referenced by operator=(), and ~NetAddressList().

00190                            {
00191   while (fNumAddresses-- > 0) {
00192     delete fAddressArray[fNumAddresses];
00193   }
00194   delete[] fAddressArray; fAddressArray = NULL;
00195 }


Friends And Related Function Documentation

friend class Iterator [friend]

Definition at line 87 of file NetAddress.hh.


Field Documentation

unsigned NetAddressList::fNumAddresses [private]

Definition at line 88 of file NetAddress.hh.

Referenced by clean(), firstAddress(), NetAddressList(), and numAddresses().

NetAddress** NetAddressList::fAddressArray [private]

Definition at line 89 of file NetAddress.hh.

Referenced by clean(), firstAddress(), NetAddressList(), NetAddressList::Iterator::nextAddress(), and operator=().


The documentation for this class was generated from the following files:
Generated on Tue Mar 25 14:40:19 2014 for live by  doxygen 1.5.2