reSIProcate/rutil  9694
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
resip::DnsStub Class Reference

#include <DnsStub.hxx>

Inheritance diagram for resip::DnsStub:
Inheritance graph
[legend]
Collaboration diagram for resip::DnsStub:
Collaboration graph
[legend]

List of all members.

Classes

class  ClearDnsCacheCommand
class  Command
class  DnsStubException
class  GetDnsCacheDumpCommand
class  LogDnsCacheCommand
class  Query
class  QueryCommand
class  ResultConverter
class  ResultConverterImpl
class  ResultTransform
class  SetEnumSuffixesCommand

Public Types

typedef RRCache::Protocol Protocol
typedef std::vector< DataDataArr
typedef std::vector
< DnsResourceRecord * > 
DnsResourceRecordsByPtr
typedef std::vector
< GenericIPAddress
NameserverList

Public Member Functions

 DnsStub (const NameserverList &additional=EmptyNameserverList, AfterSocketCreationFuncPtr socketFunc=0, AsyncProcessHandler *asyncProcessHandler=0, FdPollGrp *pollGrp=0)
 ~DnsStub ()
void setResultTransform (ResultTransform *)
void removeResultTransform ()
void setEnumSuffixes (const std::vector< Data > &suffixes)
const std::vector< Data > & getEnumSuffixes () const
void clearDnsCache ()
void logDnsCache ()
void getDnsCacheDump (std::pair< unsigned long, unsigned long > key, GetDnsCacheDumpHandler *handler)
void setDnsCacheTTL (int ttl)
void setDnsCacheSize (int size)
bool checkDnsChange ()
bool supportedType (int)
template<class QueryType >
void lookup (const Data &target, DnsResultSink *sink)
template<class QueryType >
void lookup (const Data &target, int protocol, DnsResultSink *sink)
virtual void handleDnsRaw (ExternalDnsRawResult)
virtual void process (FdSet &fdset)
virtual unsigned int getTimeTillNextProcessMS ()
virtual void buildFdSet (FdSet &fdset)
void setPollGrp (FdPollGrp *pollGrp)
void processTimers ()
template<class QueryType >
void query (const Data &target, int proto, DnsResultSink *sink)

Static Public Member Functions

static void setDnsTimeoutAndTries (int timeoutInSec, int tries)
static void enableDnsFeatures (unsigned int features)

Static Public Attributes

static NameserverList EmptyNameserverList

Protected Member Functions

void cache (const Data &key, in_addr addr)
void cache (const Data &key, const unsigned char *abuf, int alen)
void cacheTTL (const Data &key, int rrType, int status, const unsigned char *abuf, int alen)

Private Member Functions

void processFifo ()
 DnsStub (const DnsStub &)
DnsStuboperator= (const DnsStub &)
void doSetEnumSuffixes (const std::vector< Data > &suffixes)
void doClearDnsCache ()
void doLogDnsCache ()
void doGetDnsCacheDump (std::pair< unsigned long, unsigned long > key, GetDnsCacheDumpHandler *handler)
const unsigned char * skipDNSQuestion (const unsigned char *aptr, const unsigned char *abuf, int alen)
const unsigned char * createOverlay (const unsigned char *abuf, const int alen, const unsigned char *aptr, std::vector< RROverlay > &, bool discard=false)
void removeQuery (Query *)
void lookupRecords (const Data &target, unsigned short type, DnsRawSink *sink)
Data errorMessage (int status)

Private Attributes

SelectInterruptor mSelectInterruptor
FdPollItemHandle mInterruptorHandle
resip::Fifo< CommandmCommandFifo
ResultTransformmTransform
ExternalDnsmDnsProvider
FdPollGrpmPollGrp
std::set< Query * > mQueries
std::vector< DatamEnumSuffixes
AsyncProcessHandlermAsyncProcessHandler
 if this object exists, it gets notified when ApplicationMessage's get posted
RRCache mRRCache
 Dns Cache.

Static Private Attributes

static int mDnsTimeout = 0
static int mDnsTries = 0
static unsigned int mDnsFeatures = 0

Detailed Description

Definition at line 105 of file DnsStub.hxx.


Member Typedef Documentation

typedef std::vector<Data> resip::DnsStub::DataArr

Definition at line 109 of file DnsStub.hxx.

Definition at line 110 of file DnsStub.hxx.

Definition at line 111 of file DnsStub.hxx.

Definition at line 108 of file DnsStub.hxx.


Constructor & Destructor Documentation

DnsStub::DnsStub ( const NameserverList additional = EmptyNameserverList,
AfterSocketCreationFuncPtr  socketFunc = 0,
AsyncProcessHandler asyncProcessHandler = 0,
FdPollGrp pollGrp = 0 
)

Definition at line 82 of file DnsStub.cxx.

References resip::ExternalDns::BuildMismatch, ErrLog, resip::ExternalDns::errorMessage(), resip::ExternalDns::init(), mDnsFeatures, mDnsProvider, mDnsTimeout, mDnsTries, setPollGrp(), resip::ExternalDns::Success, and resip::Data::Take.

                                     :
   mInterruptorHandle(0),
   mCommandFifo(&mSelectInterruptor),
   mTransform(0),
   mDnsProvider(ExternalDnsFactory::createExternalDns()),
   mPollGrp(0),
   mAsyncProcessHandler(asyncProcessHandler)
{
   setPollGrp(pollGrp);

   int retCode = mDnsProvider->init(additional, socketFunc, mDnsTimeout, mDnsTries, mDnsFeatures);
   if (retCode != ExternalDns::Success)
   {
      if (retCode == ExternalDns::BuildMismatch)
      {
         assert(0);
         throw DnsStubException("Library was not build w/ required capabilities(probably USE_IPV6 resip/ares mismatch",
                                __FILE__,__LINE__);
      }

      Data err(Data::Take, mDnsProvider->errorMessage(retCode));
      ErrLog (<< "Failed to initialize async dns library: " << err);

      throw DnsStubException("Failed to initialize async dns library " + err, __FILE__,__LINE__);
   }
}

Here is the call graph for this function:

DnsStub::~DnsStub ( )

Definition at line 112 of file DnsStub.cxx.

References mDnsProvider, mQueries, and setPollGrp().

{
   for (set<Query*>::iterator it = mQueries.begin(); it != mQueries.end(); ++it)
   {
      delete *it;
   }

   setPollGrp(0);
   delete mDnsProvider;
}

Here is the call graph for this function:

resip::DnsStub::DnsStub ( const DnsStub ) [private]

Member Function Documentation

void DnsStub::buildFdSet ( FdSet fdset) [virtual]
void DnsStub::cache ( const Data key,
in_addr  addr 
) [protected]

Definition at line 165 of file DnsStub.cxx.

References mRRCache, and resip::RRCache::updateCacheFromHostFile().

{
   DnsHostRecord record(key, addr);
   mRRCache.updateCacheFromHostFile(record);
}

Here is the call graph for this function:

void DnsStub::cache ( const Data key,
const unsigned char *  abuf,
int  alen 
) [protected]

Definition at line 173 of file DnsStub.cxx.

References createOverlay(), mRRCache, skipDNSQuestion(), and resip::RRCache::updateCache().

{

   vector<RROverlay> overlays;

   // skip header
   const unsigned char* aptr = abuf + HFIXEDSZ;

   int qdcount = DNS_HEADER_QDCOUNT(abuf); // questions.
   for (int i = 0; i < qdcount && aptr; ++i)
   {
      aptr = skipDNSQuestion(aptr, abuf, alen);
   }

   // answers.
   int ancount = DNS_HEADER_ANCOUNT(abuf);
   for (int i = 0; i < ancount; i++)
   {
      aptr = createOverlay(abuf, alen, aptr, overlays);
   }

   // name server records.
   int nscount = DNS_HEADER_NSCOUNT(abuf);
   for (int i = 0; i < nscount; i++)
   {
      aptr = createOverlay(abuf, alen, aptr, overlays, true);
   }

   // additional records.
   int arcount = DNS_HEADER_ARCOUNT(abuf);
   for (int i = 0; i < arcount; i++)
   {
      aptr = createOverlay(abuf, alen, aptr, overlays);
   }

   // sort overlays by type.
   sort(overlays.begin(), overlays.end());

   vector<RROverlay>::iterator itLow = lower_bound(overlays.begin(), overlays.end(), *overlays.begin());
   vector<RROverlay>::iterator itHigh = upper_bound(overlays.begin(), overlays.end(), *overlays.begin());
   while (itLow != overlays.end())
   {
      mRRCache.updateCache(key, (*itLow).type(), itLow, itHigh);
      itLow = itHigh;
      if (itHigh != overlays.end())
      {
         itHigh = upper_bound(itLow, overlays.end(), *itLow);
      }
   }
}

Here is the call graph for this function:

void DnsStub::cacheTTL ( const Data key,
int  rrType,
int  status,
const unsigned char *  abuf,
int  alen 
) [protected]

Definition at line 227 of file DnsStub.cxx.

References resip::RRCache::cacheTTL(), createOverlay(), mRRCache, and skipDNSQuestion().

{
   // skip header
   const unsigned char* aptr = abuf + HFIXEDSZ;

   int qdcount = DNS_HEADER_QDCOUNT(abuf); // questions.
   for (int i = 0; i < qdcount && aptr; ++i)
   {
      aptr = skipDNSQuestion(aptr, abuf, alen);
   }

   vector<RROverlay> overlays;

   // answers.
   int ancount = DNS_HEADER_ANCOUNT(abuf);
   if (ancount != 0) return;

   // name server records.
   int nscount = DNS_HEADER_NSCOUNT(abuf);
   if (nscount == 0) return;
   vector<RROverlay> soa;
   aptr = createOverlay(abuf, alen, aptr, soa);
   if(soa.empty())
   {
      return;
   }

   mRRCache.cacheTTL(key, rrType, status, soa[0]);
}

Here is the call graph for this function:

bool DnsStub::checkDnsChange ( )

Definition at line 292 of file DnsStub.cxx.

References resip::ExternalDns::checkDnsChange(), and mDnsProvider.

{
        return mDnsProvider ? mDnsProvider->checkDnsChange() : false;
}

Here is the call graph for this function:

void DnsStub::clearDnsCache ( )

Definition at line 800 of file DnsStub.cxx.

References resip::Fifo< Msg >::add(), resip::AsyncProcessHandler::handleProcessNotification(), mAsyncProcessHandler, and mCommandFifo.

{
   ClearDnsCacheCommand* command = new ClearDnsCacheCommand(*this);
   mCommandFifo.add(command);

   if (mAsyncProcessHandler)
   {
      mAsyncProcessHandler->handleProcessNotification();
   }
}

Here is the call graph for this function:

const unsigned char * DnsStub::createOverlay ( const unsigned char *  abuf,
const int  alen,
const unsigned char *  aptr,
std::vector< RROverlay > &  ,
bool  discard = false 
) [private]

Definition at line 321 of file DnsStub.cxx.

References RRFIXEDSZ, and supportedType().

Referenced by cache(), and cacheTTL().

{
   const unsigned char* rptr = aptr;
   char* name = 0;
   long len = 0;

   int status = ares_expand_name(aptr, abuf, alen, &name, &len);
   if (ARES_SUCCESS != status)
   {
      throw DnsStubException("Failed overlay creation", __FILE__, __LINE__);
   }
   free(name);
   aptr += len;
   int type = DNS_RR_TYPE(aptr);
   int dlen = DNS_RR_LEN(aptr);
   if (!supportedType(type))
   {
      aptr += RRFIXEDSZ;
      aptr += dlen;
      return aptr;
   }
   // rewind before handing it off to overlay.
   aptr -= len;
   if (!discard)
   {
      RROverlay overlay(aptr, abuf, alen);
      overlays.push_back(overlay);
   }
   return rptr + len + RRFIXEDSZ + dlen;
}

Here is the call graph for this function:

void DnsStub::doClearDnsCache ( ) [private]

Definition at line 812 of file DnsStub.cxx.

References resip::RRCache::clearCache(), and mRRCache.

Referenced by resip::DnsStub::ClearDnsCacheCommand::execute().

Here is the call graph for this function:

void DnsStub::doGetDnsCacheDump ( std::pair< unsigned long, unsigned long >  key,
GetDnsCacheDumpHandler handler 
) [private]

Definition at line 848 of file DnsStub.cxx.

References resip::RRCache::getCacheDump(), mRRCache, and resip::GetDnsCacheDumpHandler::onDnsCacheDumpRetrieved().

Referenced by resip::DnsStub::GetDnsCacheDumpCommand::execute().

{
   assert(handler != 0);
   Data dnsCacheDump;
   mRRCache.getCacheDump(dnsCacheDump);
   handler->onDnsCacheDumpRetrieved(key, dnsCacheDump);
}

Here is the call graph for this function:

void DnsStub::doLogDnsCache ( ) [private]

Definition at line 830 of file DnsStub.cxx.

References resip::RRCache::logCache(), and mRRCache.

Referenced by resip::DnsStub::LogDnsCacheCommand::execute().

Here is the call graph for this function:

void DnsStub::doSetEnumSuffixes ( const std::vector< Data > &  suffixes) [private]

Definition at line 794 of file DnsStub.cxx.

References mEnumSuffixes.

Referenced by resip::DnsStub::SetEnumSuffixesCommand::execute().

{
   mEnumSuffixes = suffixes;
}
static void resip::DnsStub::enableDnsFeatures ( unsigned int  features) [inline, static]

Definition at line 146 of file DnsStub.hxx.

References mDnsFeatures.

{mDnsFeatures |= features;} // bit mask of ExternalDns::Features
Data DnsStub::errorMessage ( int  status) [private]

Definition at line 757 of file DnsStub.cxx.

References resip::ExternalDns::errorMessage(), mDnsProvider, and resip::Data::Take.

{
   return Data(Data::Take, mDnsProvider->errorMessage(status));
}

Here is the call graph for this function:

void DnsStub::getDnsCacheDump ( std::pair< unsigned long, unsigned long >  key,
GetDnsCacheDumpHandler handler 
)

Definition at line 836 of file DnsStub.cxx.

References resip::Fifo< Msg >::add(), resip::AsyncProcessHandler::handleProcessNotification(), mAsyncProcessHandler, and mCommandFifo.

{
   GetDnsCacheDumpCommand* command = new GetDnsCacheDumpCommand(*this, key, handler);
   mCommandFifo.add(command);

   if (mAsyncProcessHandler)
   {
      mAsyncProcessHandler->handleProcessNotification();
   }
}

Here is the call graph for this function:

const std::vector< Data > & DnsStub::getEnumSuffixes ( ) const

Definition at line 788 of file DnsStub.cxx.

References mEnumSuffixes.

{
   return mEnumSuffixes;
}
unsigned int DnsStub::getTimeTillNextProcessMS ( ) [virtual]

Definition at line 124 of file DnsStub.cxx.

References resip::ExternalDns::getTimeTillNextProcessMS(), mCommandFifo, mDnsProvider, and resip::AbstractFifo< T >::size().

{
    if(mCommandFifo.size() > 0) return 0;
    return mDnsProvider->getTimeTillNextProcessMS();
}

Here is the call graph for this function:

void DnsStub::handleDnsRaw ( ExternalDnsRawResult  res) [virtual]

Implements resip::ExternalDnsHandler.

Definition at line 769 of file DnsStub.cxx.

References resip::ExternalDnsRawResult::abuf, resip::ExternalDnsRawResult::alen, resip::AsyncResult::errorCode(), resip::ExternalDns::freeResult(), mDnsProvider, and resip::ExternalDnsResult::userData.

{
   reinterpret_cast<DnsRawSink*>(res.userData)->onDnsRaw(res.errorCode(), res.abuf, res.alen);
   mDnsProvider->freeResult(res);
}

Here is the call graph for this function:

void DnsStub::logDnsCache ( )

Definition at line 818 of file DnsStub.cxx.

References resip::Fifo< Msg >::add(), resip::AsyncProcessHandler::handleProcessNotification(), mAsyncProcessHandler, and mCommandFifo.

{
   LogDnsCacheCommand* command = new LogDnsCacheCommand(*this);
   mCommandFifo.add(command);

   if (mAsyncProcessHandler)
   {
      mAsyncProcessHandler->handleProcessNotification();
   }
}

Here is the call graph for this function:

template<class QueryType >
void resip::DnsStub::lookup ( const Data target,
DnsResultSink sink 
) [inline]

Definition at line 160 of file DnsStub.hxx.

References resip::RRList::Protocol::Reserved.

      {
         lookup<QueryType>(target, Protocol::Reserved, sink);
      }
template<class QueryType >
void resip::DnsStub::lookup ( const Data target,
int  protocol,
DnsResultSink sink 
) [inline]

Definition at line 170 of file DnsStub.hxx.

References resip::Fifo< Msg >::add(), resip::AsyncProcessHandler::handleProcessNotification(), mAsyncProcessHandler, and mCommandFifo.

      {
         QueryCommand<QueryType>* command = new QueryCommand<QueryType>(target, protocol, sink, *this);
         mCommandFifo.add(command);

         if (mAsyncProcessHandler)
         {
            mAsyncProcessHandler->handleProcessNotification();
         }
      }

Here is the call graph for this function:

void DnsStub::lookupRecords ( const Data target,
unsigned short  type,
DnsRawSink sink 
) [private]

Definition at line 763 of file DnsStub.cxx.

References resip::Data::c_str(), resip::ExternalDns::lookup(), and mDnsProvider.

{
   mDnsProvider->lookup(target.c_str(), type, this, sink);
}

Here is the call graph for this function:

DnsStub& resip::DnsStub::operator= ( const DnsStub ) [private]
void DnsStub::process ( FdSet fdset) [virtual]
void DnsStub::processFifo ( ) [private]

Definition at line 138 of file DnsStub.cxx.

References resip::DnsStub::Command::execute(), resip::Fifo< Msg >::getNext(), mCommandFifo, and resip::AbstractFifo< T >::messageAvailable().

Referenced by process(), and processTimers().

{
   while (mCommandFifo.messageAvailable())
   {
      Command* command = mCommandFifo.getNext();
      command->execute();
      delete command;
   }
}

Here is the call graph for this function:

void DnsStub::processTimers ( )

Definition at line 157 of file DnsStub.cxx.

References mDnsProvider, processFifo(), and resip::ExternalDns::processTimers().

Referenced by resip::DnsThread::thread().

{
   // the fifo is captures as a timer within getTimeTill... above
   processFifo();
   mDnsProvider->processTimers();
}

Here is the call graph for this function:

template<class QueryType >
void resip::DnsStub::query ( const Data target,
int  proto,
DnsResultSink sink 
) [inline]

Definition at line 269 of file DnsStub.hxx.

References resip::DnsStub::Query::go(), mQueries, and mTransform.

Referenced by resip::DnsStub::QueryCommand< QueryType >::execute().

      {
         Query* query = new Query(*this, mTransform, 
                                  new ResultConverterImpl<QueryType>(), 
                                  target, QueryType::getRRType(),
                                  QueryType::SupportsCName, proto, sink);
         mQueries.insert(query);
         query->go();
      }

Here is the call graph for this function:

void DnsStub::removeQuery ( Query query) [private]

Definition at line 357 of file DnsStub.cxx.

References mQueries.

{
   set<Query*>::iterator it = mQueries.find(query);
   if (it != mQueries.end())
   {
      mQueries.erase(it);
   }
}
void DnsStub::removeResultTransform ( )

Definition at line 373 of file DnsStub.cxx.

References mTransform.

{
   mTransform = 0;
}
void DnsStub::setDnsCacheSize ( int  size)

Definition at line 863 of file DnsStub.cxx.

References mRRCache, and resip::RRCache::setSize().

{
   mRRCache.setSize(size);
}

Here is the call graph for this function:

void DnsStub::setDnsCacheTTL ( int  ttl)

Definition at line 857 of file DnsStub.cxx.

References mRRCache, and resip::RRCache::setTTL().

{
   mRRCache.setTTL(ttl);
}

Here is the call graph for this function:

static void resip::DnsStub::setDnsTimeoutAndTries ( int  timeoutInSec,
int  tries 
) [inline, static]

Definition at line 141 of file DnsStub.hxx.

References mDnsTimeout, and mDnsTries.

      {
         mDnsTimeout = timeoutInSec;
         mDnsTries = tries;
      }
void DnsStub::setEnumSuffixes ( const std::vector< Data > &  suffixes)

Definition at line 776 of file DnsStub.cxx.

References resip::Fifo< Msg >::add(), resip::AsyncProcessHandler::handleProcessNotification(), mAsyncProcessHandler, and mCommandFifo.

{
   SetEnumSuffixesCommand* command = new SetEnumSuffixesCommand(*this, suffixes);
   mCommandFifo.add(command);

   if (mAsyncProcessHandler)
   {
      mAsyncProcessHandler->handleProcessNotification();
   }
}

Here is the call graph for this function:

void DnsStub::setPollGrp ( FdPollGrp pollGrp)
void DnsStub::setResultTransform ( ResultTransform transform)

Definition at line 367 of file DnsStub.cxx.

References mTransform.

{
   mTransform = transform;
}
const unsigned char * DnsStub::skipDNSQuestion ( const unsigned char *  aptr,
const unsigned char *  abuf,
int  alen 
) [private]

Definition at line 262 of file DnsStub.cxx.

Referenced by cache(), and cacheTTL().

{
   char *name=0;
   int status=0;
   long len = 0;

   // Parse the question name.
   status = ares_expand_name(aptr, abuf, alen, &name, &len);
   if (status != ARES_SUCCESS)
   {
      throw DnsStubException("Failed DNS preparse", __FILE__, __LINE__);
   }
   aptr += len;

   // Make sure there's enough data after the name for the fixed part
   // of the question.
   if (aptr + QFIXEDSZ > abuf + alen)
   {
      free(name);
      throw DnsStubException("Failed DNS preparse", __FILE__, __LINE__);
   }

   aptr += QFIXEDSZ;
   free(name);
   return aptr;
}
bool DnsStub::supportedType ( int  type)

Definition at line 298 of file DnsStub.cxx.

References resip::ExternalDns::hostFileLookupLookupOnlyMode(), mDnsProvider, T_A, T_AAAA, T_NAPTR, and T_SRV.

Referenced by createOverlay().

{
   if(mDnsProvider && mDnsProvider->hostFileLookupLookupOnlyMode())
   {
      return (T_A == type);
   }
#ifdef USE_IPV6
   return (T_A == type ||
           T_AAAA == type ||
           T_NAPTR == type ||
           T_SRV == type ||
           T_CNAME == type ||
           T_SOA == type);
#else
   return (T_A == type ||
           T_NAPTR == type ||
           T_SRV == type ||
           T_CNAME == type ||
           T_SOA == type);
#endif
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 113 of file DnsStub.hxx.

if this object exists, it gets notified when ApplicationMessage's get posted

Definition at line 423 of file DnsStub.hxx.

Referenced by clearDnsCache(), getDnsCacheDump(), logDnsCache(), lookup(), and setEnumSuffixes().

unsigned int DnsStub::mDnsFeatures = 0 [static, private]

Definition at line 420 of file DnsStub.hxx.

Referenced by DnsStub(), and enableDnsFeatures().

int DnsStub::mDnsTimeout = 0 [static, private]

Definition at line 418 of file DnsStub.hxx.

Referenced by DnsStub(), and setDnsTimeoutAndTries().

int DnsStub::mDnsTries = 0 [static, private]

Definition at line 419 of file DnsStub.hxx.

Referenced by DnsStub(), and setDnsTimeoutAndTries().

std::vector<Data> resip::DnsStub::mEnumSuffixes [private]

Definition at line 416 of file DnsStub.hxx.

Referenced by doSetEnumSuffixes(), and getEnumSuffixes().

Definition at line 395 of file DnsStub.hxx.

Referenced by setPollGrp().

Definition at line 413 of file DnsStub.hxx.

Referenced by setPollGrp().

std::set<Query*> resip::DnsStub::mQueries [private]

Definition at line 414 of file DnsStub.hxx.

Referenced by query(), removeQuery(), and ~DnsStub().

Definition at line 394 of file DnsStub.hxx.

Referenced by buildFdSet(), process(), and setPollGrp().


The documentation for this class was generated from the following files: