reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
LocalDns.cxx
Go to the documentation of this file.
1#include <fstream>
2
5
6#include "ares.h"
7#include "ares_dns.h"
8#ifdef WIN32
9#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
10#endif
11
12#include "AresCompat.hxx"
13
14#if !defined(WIN32)
15#include <arpa/nameser.h>
16#endif
17
18using namespace resip;
19using namespace std;
20
21std::map<Data, Data> LocalDns::files;
22Data LocalDns::mTarget;
23
24int
26{
27 int status;
28 if ((status = ares_init(&mChannel)) != ARES_SUCCESS)
29 {
30 return status;
31 }
32 else
33 {
34 return 0;
35 }
36}
37
39{
40 files["yahoo.com"] = "yahoo.dns";
41 files["demo.xten.com"] = "demo.naptr";
42 files["_ldap._tcp.openldap.org"] = "openldap.srv";
43 files["quartz"] = "quartz.aaaa";
44 files["crystal"] = "crystal.aaaa";
45 files["www.google.com"] = "google.cname";
46}
47
51
53LocalDns::getHandler(void* arg)
54{
55 Payload* p = reinterpret_cast<Payload*>(arg);
56 ExternalDnsHandler *thisp = reinterpret_cast<ExternalDnsHandler*>(p->first);
57 return thisp;
58}
59
61LocalDns::makeRawResult(void *arg, int status, unsigned char *abuf, int alen)
62{
63 Payload* p = reinterpret_cast<Payload*>(arg);
64 void* userArg = reinterpret_cast<void*>(p->second);
65
66 if (status != ARES_SUCCESS)
67 {
68 return ExternalDnsRawResult(status, abuf, alen, userArg);
69 }
70 else
71 {
72 return ExternalDnsRawResult(abuf, alen, userArg);
73 }
74}
75
76unsigned int
78{
79 return 20;
80}
81
82void
83LocalDns::buildFdSet(fd_set& read, fd_set& write, int& size)
84{
85}
86
87void
88LocalDns::process(fd_set& read, fd_set& write)
89{
90}
91
92void
93LocalDns::lookup(const char* target, unsigned short type, ExternalDnsHandler* handler, void* userData)
94{
95 mTarget = target;
96 ares_query(mChannel, target, C_IN, type, LocalDns::localCallback, new Payload(handler, userData));
97}
98
99void LocalDns::message(const char* file, unsigned char* buf, int& len)
100{
101 len = 0;
102 ifstream fs;
103 fs.open(file, ios_base::binary | ios_base::in);
104 resip_assert(fs.is_open());
105
106 unsigned char* p = buf;
107
108 while (!fs.eof())
109 {
110 unsigned char c = fs.get();
111 if (c != char_traits<char>::eof())
112 {
113 *p++ = c;
114 len++;
115 }
116 }
117
118 fs.close();
119}
120
121void
122LocalDns::localCallback(void *arg, int status, unsigned char *abuf, int alen)
123{
124 unsigned char msg[1024];
125 int len = 0;
126 map<Data, Data>::iterator it = files.find(mTarget);
127 resip_assert(it != files.end());
128 message(it->second.c_str(), msg, len);
129 resip_assert(0 != len);
130 getHandler(arg)->handleDnsRaw(makeRawResult(arg, 0, msg, len));
131 Payload* p = reinterpret_cast<Payload*>(arg);
132 delete p;
133}
134
135/* ====================================================================
136 * The Vovida Software License, Version 1.0
137 *
138 * Copyright (c) 2000-2005 Vovida Networks, Inc. All rights reserved.
139 *
140 * Redistribution and use in source and binary forms, with or without
141 * modification, are permitted provided that the following conditions
142 * are met:
143 *
144 * 1. Redistributions of source code must retain the above copyright
145 * notice, this list of conditions and the following disclaimer.
146 *
147 * 2. Redistributions in binary form must reproduce the above copyright
148 * notice, this list of conditions and the following disclaimer in
149 * the documentation and/or other materials provided with the
150 * distribution.
151 *
152 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
153 * and "Vovida Open Communication Application Library (VOCAL)" must
154 * not be used to endorse or promote products derived from this
155 * software without prior written permission. For written
156 * permission, please contact vocal@vovida.org.
157 *
158 * 4. Products derived from this software may not be called "VOCAL", nor
159 * may "VOCAL" appear in their name, without prior written
160 * permission of Vovida Networks, Inc.
161 *
162 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
163 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
164 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
165 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
166 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
167 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
168 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
169 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
170 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
171 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
172 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
173 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
174 * DAMAGE.
175 *
176 * ====================================================================
177 *
178 * This software consists of voluntary contributions made by Vovida
179 * Networks, Inc. and many individuals on behalf of Vovida Networks,
180 * Inc. For more information on Vovida Networks, Inc., please see
181 * <http://www.vovida.org/>.
182 *
183 */
#define resip_assert(x)
This file needs to work for both C and C++ code.
#define ARES_SUCCESS
Definition ares.h:79
int ares_init(ares_channel *channelptr)
Definition ares_init.c:104
void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition ares_query.c:38
#define C_IN
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
virtual void handleDnsRaw(ExternalDnsRawResult res)=0
virtual ~LocalDns()
Definition LocalDns.cxx:48
virtual void buildFdSet(fd_set &read, fd_set &write, int &size)
Definition LocalDns.cxx:83
virtual void process(fd_set &read, fd_set &write)
Definition LocalDns.cxx:88
virtual unsigned int getTimeTillNextProcessMS()
Definition LocalDns.cxx:77
virtual int init()
Definition LocalDns.cxx:25
void lookup(const char *target, unsigned short type, ExternalDnsHandler *handler, void *userData)
Definition LocalDns.cxx:93
dcm! – add UnusedChecking(_enum) below;