reSIProcate/stack  9694
testDnsStub.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #if defined (HAVE_POPT_H) 
00006 #include <popt.h>
00007 #else
00008 #ifndef WIN32
00009 #warning "will not work very well without libpopt"
00010 #endif
00011 #endif
00012 
00013 #include <iostream>
00014 #include <list>
00015 
00016 #include "rutil/Lock.hxx"
00017 #include "rutil/Mutex.hxx"
00018 #include "rutil/Socket.hxx"
00019 #include "rutil/Logger.hxx"
00020 #include "rutil/ThreadIf.hxx"
00021 #include "rutil/DnsUtil.hxx"
00022 #include "resip/stack/DnsResult.hxx"
00023 #include "resip/stack/SipStack.hxx"
00024 #include "rutil/dns/RRVip.hxx"
00025 #include "rutil/dns/QueryTypes.hxx"
00026 #include "rutil/dns/DnsStub.hxx"
00027 
00028 using namespace std;
00029 
00030 
00031 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
00032 
00033 const char bf[] = "\033[01;34m";
00034 const char gf[] = "\033[01;32m";
00035 const char rf[] = "\033[01;31m";
00036 const char ub[] = "\033[01;00m";
00037 
00038 bool gComplete = false;
00039 
00040 #ifdef WIN32
00041 #define usleep(x) Sleep(x/1000)
00042 #define sleep(x) Sleep(x)
00043 #endif
00044 
00045 namespace resip
00046 {
00047 
00048 class MyDnsSink : public DnsResultSink
00049 {
00050    public:
00051       void onDnsResult(const DNSResult<DnsHostRecord>&);
00052 #ifdef USE_IPV6
00053       void onDnsResult(const DNSResult<DnsAAAARecord>&);
00054 #endif
00055       void onDnsResult(const DNSResult<DnsSrvRecord>&);
00056       void onDnsResult(const DNSResult<DnsNaptrRecord>&);
00057       void onDnsResult(const DNSResult<DnsCnameRecord>&);
00058 };
00059 
00060 void MyDnsSink::onDnsResult(const DNSResult<DnsHostRecord>& result)
00061 {
00062    cout << "A records" << endl;
00063    cout << "Status: " << result.status << endl;
00064    cout << "Domain: " << result.domain << endl;
00065    if (result.status == 0)
00066    {
00067       for (vector<DnsHostRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
00068       {
00069          cout << (*it).host() << endl;
00070       }
00071    }
00072    else
00073    {
00074       cout << "Dns look up failed:" << result.msg << endl;
00075    }
00076    gComplete = true;
00077 }
00078 
00079 void MyDnsSink::onDnsResult(const DNSResult<DnsNaptrRecord>& result)
00080 {
00081    cout << "Naptr records" << endl;
00082    cout << "Status: " << result.status << endl;
00083    cout << "Domain: " << result.domain << endl;
00084    if (result.status == 0)
00085    {
00086       for (vector<DnsNaptrRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
00087       {
00088          cout << (*it).name() << endl;
00089       }
00090    }
00091    else
00092    {
00093       cout << "Dns look up failed:" << result.msg << endl;
00094    }
00095    gComplete = true;
00096 }
00097 
00098 void MyDnsSink::onDnsResult(const DNSResult<DnsCnameRecord>& result)
00099 {
00100    cout << "CNAME records" << endl;
00101    cout << "Status: " << result.status << endl;
00102    cout << "Domain: " << result.domain << endl;
00103    if (result.status == 0)
00104    {
00105       for (vector<DnsCnameRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
00106       {
00107          cout << (*it).cname() << endl;
00108       }
00109    }
00110    else
00111    {
00112       cout << "Dns look up failed:" << result.msg << endl;
00113    }
00114    gComplete = true;
00115 }
00116 
00117 void MyDnsSink::onDnsResult(const DNSResult<DnsSrvRecord>& result)
00118 {
00119    cout << "SRV records" << endl;
00120    cout << "Status: " << result.status << endl;
00121    cout << "Domain: " << result.domain << endl;
00122    if (result.status == 0)
00123    {
00124       for (vector<DnsSrvRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
00125       {
00126          cout << "Name: " << (*it).name() << endl;
00127          cout << "Priority: " << (*it).priority() << endl;
00128          cout << "Weight: " << (*it).weight() << endl;
00129          cout << "Port: " << (*it).port() << endl;
00130          cout << "Target: " << (*it).target() << endl;
00131       }
00132    }
00133    else
00134    {
00135       cout << "Dns look up failed:" << result.msg << endl;
00136    }
00137    gComplete = true;
00138 }
00139 
00140 #ifdef USE_IPV6
00141 void MyDnsSink::onDnsResult(const DNSResult<DnsAAAARecord>& result)
00142 {
00143    gComplete = true;
00144 }
00145 #endif
00146 
00147 class TestDns : public ThreadIf, public DnsStub
00148 {
00149    public:
00150       TestDns(const DnsStub::NameserverList& additional) : DnsStub(additional)
00151       {
00152       }
00153 
00154       void thread()
00155       {
00156          while (!waitForShutdown(100))
00157          {
00158             FdSet fdset;
00159             buildFdSet(fdset);
00160             fdset.selectMilliSeconds(1);
00161             process(fdset);
00162          }
00163       }
00164 };
00165  
00166 }
00167 
00168 using namespace resip;
00169 
00170 int 
00171 main(int argc, const char** argv)
00172 {
00173    if (argc < 3) 
00174    {
00175       cout << "usage: " << argv[0] << " target" << " type" << endl;
00176       cout << "Valid type values: " << endl;
00177       cout << "A Record - 1" << endl;
00178       cout << "CNAME - 5" << endl;
00179       cout << "SRV - 33" << endl;
00180       cout << "NAPTR - 35" << endl;
00181       cout << "AAAA - 28" << endl;
00182       return 0;
00183    }
00184 
00185    DnsStub::NameserverList nameServerList = DnsStub::EmptyNameserverList;
00186    if(argc == 4)
00187    {
00188       Tuple tuple(Data(argv[3]), 0, V4);
00189       nameServerList.push_back(tuple.toGenericIPAddress());
00190    }
00191 
00192    char* logType = "cout";
00193    //char* logLevel = "STACK";
00194    char* logLevel = "DEBUG";
00195 
00196 #if defined(HAVE_POPT_H)
00197   struct poptOption table[] = {
00198       {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00199       {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00200       POPT_AUTOHELP
00201       { NULL, 0, 0, NULL, 0 }
00202    };
00203    
00204    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00205    poptGetNextOpt(context);
00206 #endif
00207 
00208    Log::initialize(logType, logLevel, argv[0]);
00209    initNetwork();
00210    TestDns dns(nameServerList);
00211    dns.run();   
00212    cerr << "Starting" << endl;   
00213 #if defined(HAVE_POPT_H)
00214    const char** args = poptGetArgs(context);
00215 #else
00216    const char** args = argv;
00217 #endif
00218 
00219    MyDnsSink sink;
00220 
00221    switch (atoi(argv[2]))
00222    {
00223       case 1:
00224          dns.lookup<RR_A>(argv[1], Protocol::Sip, &sink);
00225          break;
00226       case 5:
00227          dns.lookup<RR_CNAME>(argv[1], Protocol::Sip, &sink);
00228          break;
00229 #ifdef USE_IPV6
00230       case 28:
00231          dns.lookup<RR_AAAA>(argv[1], Protocol::Sip, &sink);
00232          break;
00233 #endif
00234       case 33:
00235          dns.lookup<RR_SRV>(argv[1], Protocol::Sip, &sink);
00236          break;
00237       case 35:
00238          dns.lookup<RR_NAPTR>(argv[1], Protocol::Sip, &sink);
00239          break;
00240       default:
00241          cout << "Invalid Dns type" << endl;
00242          return 0;
00243    }
00244 
00245    while (!gComplete)
00246    {
00247 #ifdef WIN32
00248       sleep(100);
00249 #else
00250       sleep(1);
00251 #endif
00252    }
00253 
00254    dns.shutdown();
00255    dns.join();
00256 
00257    return 0;
00258 }
00259 
00260 
00261 /* ====================================================================
00262  * The Vovida Software License, Version 1.0 
00263  * 
00264  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00265  * 
00266  * Redistribution and use in source and binary forms, with or without
00267  * modification, are permitted provided that the following conditions
00268  * are met:
00269  * 
00270  * 1. Redistributions of source code must retain the above copyright
00271  *    notice, this list of conditions and the following disclaimer.
00272  * 
00273  * 2. Redistributions in binary form must reproduce the above copyright
00274  *    notice, this list of conditions and the following disclaimer in
00275  *    the documentation and/or other materials provided with the
00276  *    distribution.
00277  * 
00278  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00279  *    and "Vovida Open Communication Application Library (VOCAL)" must
00280  *    not be used to endorse or promote products derived from this
00281  *    software without prior written permission. For written
00282  *    permission, please contact vocal@vovida.org.
00283  *
00284  * 4. Products derived from this software may not be called "VOCAL", nor
00285  *    may "VOCAL" appear in their name, without prior written
00286  *    permission of Vovida Networks, Inc.
00287  * 
00288  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00289  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00290  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00291  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00292  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00293  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00294  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00295  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00296  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00297  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00298  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00299  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00300  * DAMAGE.
00301  * 
00302  * ====================================================================
00303  * 
00304  * This software consists of voluntary contributions made by Vovida
00305  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00306  * Inc.  For more information on Vovida Networks, Inc., please see
00307  * <http://www.vovida.org/>.
00308  *
00309  */