reSIProcate/rutil  9694
LocalDns.cxx
Go to the documentation of this file.
00001 #include <fstream>
00002 
00003 #include "rutil/dns/LocalDns.hxx"
00004 #include "rutil/WinLeakCheck.hxx"
00005 
00006 #include "ares.h"
00007 #include "ares_dns.h"
00008 #ifdef WIN32
00009 #undef write  // Note:  ares.h defines write to be _write for WIN32 - we don't want that here, since we use fdset.write and stream write
00010 #endif
00011 
00012 #include "AresCompat.hxx"
00013 
00014 #if !defined(WIN32)
00015 #include <arpa/nameser.h>
00016 #endif
00017 
00018 using namespace resip;
00019 using namespace std;
00020 
00021 std::map<Data, Data> LocalDns::files;
00022 Data LocalDns::mTarget;
00023 
00024 int 
00025 LocalDns::init()
00026 {
00027    int status;
00028    if ((status = ares_init(&mChannel)) != ARES_SUCCESS)
00029    {
00030       return status;
00031    }
00032    else
00033    {
00034       return 0;
00035    }
00036 }
00037 
00038 LocalDns::LocalDns()
00039 {
00040    files["yahoo.com"] = "yahoo.dns";
00041    files["demo.xten.com"] = "demo.naptr";
00042    files["_ldap._tcp.openldap.org"] = "openldap.srv";
00043    files["quartz"] = "quartz.aaaa";
00044    files["crystal"] = "crystal.aaaa";
00045    files["www.google.com"] = "google.cname";
00046 }
00047 
00048 LocalDns::~LocalDns()
00049 {
00050 }
00051 
00052 ExternalDnsHandler* 
00053 LocalDns::getHandler(void* arg)
00054 {
00055    Payload* p = reinterpret_cast<Payload*>(arg);
00056    ExternalDnsHandler *thisp = reinterpret_cast<ExternalDnsHandler*>(p->first);
00057    return thisp;
00058 }
00059 
00060 ExternalDnsRawResult 
00061 LocalDns::makeRawResult(void *arg, int status, unsigned char *abuf, int alen)
00062 {
00063    Payload* p = reinterpret_cast<Payload*>(arg);
00064    void* userArg = reinterpret_cast<void*>(p->second);
00065    
00066    if (status != ARES_SUCCESS)
00067    {
00068       return ExternalDnsRawResult(status, abuf, alen, userArg);
00069    }
00070    else
00071    {
00072       return ExternalDnsRawResult(abuf, alen, userArg);
00073    }
00074 }
00075       
00076 unsigned int
00077 LocalDns::getTimeTillNextProcessMS()
00078 {
00079    return 20; 
00080 }
00081 
00082 void 
00083 LocalDns::buildFdSet(fd_set& read, fd_set& write, int& size)
00084 {
00085 }
00086 
00087 void 
00088 LocalDns::process(fd_set& read, fd_set& write)
00089 {
00090 }
00091 
00092 void
00093 LocalDns::lookup(const char* target, unsigned short type, ExternalDnsHandler* handler, void* userData)
00094 {
00095    mTarget = target;
00096    ares_query(mChannel, target, C_IN, type, LocalDns::localCallback, new Payload(handler, userData));
00097 }
00098 
00099 void LocalDns::message(const char* file, unsigned char* buf, int& len)
00100 {
00101    len = 0;
00102    ifstream fs;
00103    fs.open(file, ios_base::binary | ios_base::in);
00104    assert(fs.is_open());
00105    
00106    unsigned char* p = buf;
00107    
00108    while (!fs.eof())
00109    {
00110       unsigned char c = fs.get();
00111       if (c != char_traits<char>::eof())
00112       {
00113          *p++ = c;
00114          len++;
00115       }
00116    }
00117    
00118    fs.close();
00119 }
00120 
00121 void
00122 LocalDns::localCallback(void *arg, int status, unsigned char *abuf, int alen)
00123 {
00124    unsigned char msg[1024];
00125    int len = 0;
00126    map<Data, Data>::iterator it = files.find(mTarget);
00127    assert(it != files.end());
00128    message(it->second.c_str(), msg, len);   
00129    assert(0 != len);
00130    getHandler(arg)->handleDnsRaw(makeRawResult(arg, 0, msg, len));
00131    Payload* p = reinterpret_cast<Payload*>(arg);
00132    delete p;
00133 }
00134 
00135 /* ====================================================================
00136  * The Vovida Software License, Version 1.0 
00137  * 
00138  * Copyright (c) 2000-2005 Vovida Networks, Inc.  All rights reserved.
00139  * 
00140  * Redistribution and use in source and binary forms, with or without
00141  * modification, are permitted provided that the following conditions
00142  * are met:
00143  * 
00144  * 1. Redistributions of source code must retain the above copyright
00145  *    notice, this list of conditions and the following disclaimer.
00146  * 
00147  * 2. Redistributions in binary form must reproduce the above copyright
00148  *    notice, this list of conditions and the following disclaimer in
00149  *    the documentation and/or other materials provided with the
00150  *    distribution.
00151  * 
00152  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00153  *    and "Vovida Open Communication Application Library (VOCAL)" must
00154  *    not be used to endorse or promote products derived from this
00155  *    software without prior written permission. For written
00156  *    permission, please contact vocal@vovida.org.
00157  *
00158  * 4. Products derived from this software may not be called "VOCAL", nor
00159  *    may "VOCAL" appear in their name, without prior written
00160  *    permission of Vovida Networks, Inc.
00161  * 
00162  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00163  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00164  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00165  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00166  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00167  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00168  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00169  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00170  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00171  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00172  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00173  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00174  * DAMAGE.
00175  * 
00176  * ====================================================================
00177  * 
00178  * This software consists of voluntary contributions made by Vovida
00179  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00180  * Inc.  For more information on Vovida Networks, Inc., please see
00181  * <http://www.vovida.org/>.
00182  *
00183  */