reSIProcate/stack  9694
Classes | Functions | Variables
testDnsCache.cxx File Reference
#include <sys/types.h>
#include <iostream>
#include <memory>
#include <fstream>
#include "rutil/socket.hxx"
#include "rutil/Data.hxx"
#include "rutil/DnsUtil.hxx"
#include "resip/stack/DnsInterface.hxx"
#include "rutil/dns/QueryTypes.hxx"
#include "rutil/dns/RROverlay.hxx"
#include "rutil/dns/RRList.hxx"
#include "rutil/dns/RRCache.hxx"
#include "rutil/dns/DnsStub.hxx"
Include dependency graph for testDnsCache.cxx:

Go to the source code of this file.

Classes

class  MyDnsSink

Functions

static const char * MyInet_ntop4 (const u_char *src, char *dst, size_t size)
static const char * MyInet_ntop6 (const u_char *src, char *dst, size_t size)
 main (int argc, char *argv[])

Variables

const int NS_INT16SZ = 2
const int NS_INADDRSZ = 4
const int NS_IN6ADDRSZ = 16

Function Documentation

main ( int  argc,
char *  argv[] 
)

Definition at line 224 of file testDnsCache.cxx.

References resip::DnsStub::lookup(), and resip::RRList::Protocol::Sip.

{
   {
      const char* const key = "yahoo.com";
      MyDnsSink sink;
      DnsStub stub;
      DnsInterface dns(stub);
      stub.lookup<RR_A>(key, Protocol::Sip, &sink);
   }

   {
      const char* const key = "_ldap._tcp.openldap.org";
      MyDnsSink sink;
      DnsStub stub;
      DnsInterface dns(stub);
      stub.lookup<RR_SRV>(key, Protocol::Sip, &sink);
   }

   {
#ifdef USE_IPV6
      const char* const key = "quartz";
      MyDnsSink sink;
      DnsStub stub;
      DnsInterface dns(stub);
      stub.lookup<RR_AAAA>(key, Protocol::Sip, &sink);
#endif
   }

   {
      const char* const key = "www.google.com";
      MyDnsSink sink;
      DnsStub stub;
      DnsInterface dns(stub);
      stub.lookup<RR_CNAME>(key, Protocol::Sip, &sink);
   }

   return 0;
}

Here is the call graph for this function:

static const char* MyInet_ntop4 ( const u_char *  src,
char *  dst,
size_t  size 
) [static]

Definition at line 99 of file testDnsCache.cxx.

References errno.

Referenced by MyInet_ntop6().

{
   static const char fmt[] = "%u.%u.%u.%u";
#ifdef WIN32
   if ( _snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]) < 0)
#else
   if ( snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]) < 0)
#endif
   {
      errno = ENOSPC;
      dst[size-1] = 0;
      return NULL;
   }
   return (dst);
}
static const char* MyInet_ntop6 ( const u_char *  src,
char *  dst,
size_t  size 
) [static]

Definition at line 116 of file testDnsCache.cxx.

References errno, len, MyInet_ntop4(), NS_IN6ADDRSZ, and NS_INT16SZ.

{
   /*
    * Note that int32_t and int16_t need only be "at least" large enough
    * to contain a value of the specified size.  On some systems, like
    * Crays, there is no such thing as an integer variable with 16 bits.
    * Keep this in mind if you think this function should have been coded
    * to use pointer overlays.  All the world's not a VAX.
    */
   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
   struct { int base, len; } best, cur;
   u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
   int i;

   /*
    * Preprocess:
    *   Copy the input (bytewise) array into a wordwise array.
    *   Find the longest run of 0x00's in src[] for :: shorthanding.
    */
   memset(words, '\0', sizeof words);
   for (i = 0; i < NS_IN6ADDRSZ; i++)
      words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
   best.base = -1;
   cur.base = -1;
   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
      if (words[i] == 0) {
         if (cur.base == -1)
            cur.base = i, cur.len = 1;
         else
            cur.len++;
      } else {
         if (cur.base != -1) {
            if (best.base == -1 || cur.len > best.len)
               best = cur;
            cur.base = -1;
         }
      }
   }
   if (cur.base != -1) {
      if (best.base == -1 || cur.len > best.len)
         best = cur;
   }
   if (best.base != -1 && best.len < 2)
      best.base = -1;

   /*
    * Format the result.
    */
   tp = tmp;
   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
      /* Are we inside the best run of 0x00's? */
      if (best.base != -1 && i >= best.base &&
          i < (best.base + best.len)) {
         if (i == best.base)
            *tp++ = ':';
         continue;
      }
      /* Are we following an initial run of 0x00s or any real hex? */
      if (i != 0)
         *tp++ = ':';
      /* Is this address an encapsulated IPv4? */
      if (i == 6 && best.base == 0 &&
          (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
         if (!MyInet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
            return (NULL);
         tp += strlen(tp);
         break;
      }
      tp += sprintf(tp, "%x", words[i]);
   }
   /* Was it a trailing run of 0x00's? */
   if (best.base != -1 && (best.base + best.len) ==
       (NS_IN6ADDRSZ / NS_INT16SZ))
      *tp++ = ':';
   *tp++ = '\0';

   /*
    * Check for overflow, copy, and we're done.
    */
   if ((size_t)(tp - tmp) > size) {
      errno = ENOSPC;
      return (NULL);
   }
   strcpy(dst, tmp);
   return (dst);
}

Here is the call graph for this function:


Variable Documentation

const int NS_IN6ADDRSZ = 16

Definition at line 96 of file testDnsCache.cxx.

Referenced by MyInet_ntop6().

const int NS_INADDRSZ = 4

Definition at line 95 of file testDnsCache.cxx.

const int NS_INT16SZ = 2

Definition at line 94 of file testDnsCache.cxx.

Referenced by MyInet_ntop6().