/[resiprocate]/main/resip/stack/test/testDnsCache.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testDnsCache.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9485 - (show annotations) (download)
Sat Apr 7 00:34:03 2012 UTC (7 years, 7 months ago) by dpocock
File MIME type: text/plain
File size: 9367 byte(s)
Switch over to autotools build system from dpocock-autotools branch
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #if defined (HAVE_POPT_H)
6 #include <popt.h>
7 #else
8 #ifndef WIN32
9 #warning "will not work very well without libpopt"
10 #endif
11 #endif
12
13 #include <sys/types.h>
14 #include <iostream>
15 #include <memory>
16
17 #include <fstream>
18
19 #include "rutil/socket.hxx"
20 #include "rutil/Data.hxx"
21 #include "rutil/DnsUtil.hxx"
22 #include "resip/stack/DnsInterface.hxx"
23 #include "rutil/dns/QueryTypes.hxx"
24 #include "rutil/dns/RROverlay.hxx"
25 #include "rutil/dns/RRList.hxx"
26 #include "rutil/dns/RRCache.hxx"
27 #include "rutil/dns/DnsStub.hxx"
28
29 using namespace resip;
30 using namespace std;
31
32 class MyDnsSink : public DnsResultSink
33 {
34 void onDnsResult(const DNSResult<DnsHostRecord>&);
35 #ifdef USE_IPV6
36 void onDnsResult(const DNSResult<DnsAAAARecord>&);
37 #endif
38 void onDnsResult(const DNSResult<DnsSrvRecord>&);
39 void onDnsResult(const DNSResult<DnsNaptrRecord>&) {}
40 void onDnsResult(const DNSResult<DnsCnameRecord>&);
41 };
42
43 void MyDnsSink::onDnsResult(const DNSResult<DnsHostRecord>& result)
44 {
45 cout << "A records" << endl;
46 cout << "Status: " << result.status << endl;
47 cout << "Domain: " << result.domain << endl;
48 if (result.status == 0)
49 {
50 for (vector<DnsHostRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
51 {
52 cout << (*it).host() << endl;
53 }
54 }
55 cout << endl;
56 }
57
58 void MyDnsSink::onDnsResult(const DNSResult<DnsCnameRecord>& result)
59 {
60 cout << "CNAME records" << endl;
61 cout << "Status: " << result.status << endl;
62 cout << "Domain: " << result.domain << endl;
63 if (result.status == 0)
64 {
65 for (vector<DnsCnameRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
66 {
67 cout << (*it).cname() << endl;
68 }
69 }
70 cout << endl;
71 }
72
73 void MyDnsSink::onDnsResult(const DNSResult<DnsSrvRecord>& result)
74 {
75 cout << "SRV records" << endl;
76 cout << "Status: " << result.status << endl;
77 cout << "Domain: " << result.domain << endl;
78 if (result.status == 0)
79 {
80 for (vector<DnsSrvRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
81 {
82 cout << "Name: " << (*it).name() << endl;
83 cout << "Priority: " << (*it).priority() << endl;
84 cout << "Weight: " << (*it).weight() << endl;
85 cout << "Port: " << (*it).port() << endl;
86 cout << "Target: " << (*it).target() << endl;
87 }
88 }
89 cout << endl;
90 }
91
92
93 // adopted from DnsUtil.
94 const int NS_INT16SZ = 2;
95 const int NS_INADDRSZ = 4;
96 const int NS_IN6ADDRSZ = 16;
97
98 static const char*
99 MyInet_ntop4(const u_char *src, char *dst, size_t size)
100 {
101 static const char fmt[] = "%u.%u.%u.%u";
102 #ifdef WIN32
103 if ( _snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]) < 0)
104 #else
105 if ( snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]) < 0)
106 #endif
107 {
108 errno = ENOSPC;
109 dst[size-1] = 0;
110 return NULL;
111 }
112 return (dst);
113 }
114
115 static const char *
116 MyInet_ntop6(const u_char *src, char *dst, size_t size)
117 {
118 /*
119 * Note that int32_t and int16_t need only be "at least" large enough
120 * to contain a value of the specified size. On some systems, like
121 * Crays, there is no such thing as an integer variable with 16 bits.
122 * Keep this in mind if you think this function should have been coded
123 * to use pointer overlays. All the world's not a VAX.
124 */
125 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
126 struct { int base, len; } best, cur;
127 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
128 int i;
129
130 /*
131 * Preprocess:
132 * Copy the input (bytewise) array into a wordwise array.
133 * Find the longest run of 0x00's in src[] for :: shorthanding.
134 */
135 memset(words, '\0', sizeof words);
136 for (i = 0; i < NS_IN6ADDRSZ; i++)
137 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
138 best.base = -1;
139 cur.base = -1;
140 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
141 if (words[i] == 0) {
142 if (cur.base == -1)
143 cur.base = i, cur.len = 1;
144 else
145 cur.len++;
146 } else {
147 if (cur.base != -1) {
148 if (best.base == -1 || cur.len > best.len)
149 best = cur;
150 cur.base = -1;
151 }
152 }
153 }
154 if (cur.base != -1) {
155 if (best.base == -1 || cur.len > best.len)
156 best = cur;
157 }
158 if (best.base != -1 && best.len < 2)
159 best.base = -1;
160
161 /*
162 * Format the result.
163 */
164 tp = tmp;
165 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
166 /* Are we inside the best run of 0x00's? */
167 if (best.base != -1 && i >= best.base &&
168 i < (best.base + best.len)) {
169 if (i == best.base)
170 *tp++ = ':';
171 continue;
172 }
173 /* Are we following an initial run of 0x00s or any real hex? */
174 if (i != 0)
175 *tp++ = ':';
176 /* Is this address an encapsulated IPv4? */
177 if (i == 6 && best.base == 0 &&
178 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
179 if (!MyInet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
180 return (NULL);
181 tp += strlen(tp);
182 break;
183 }
184 tp += sprintf(tp, "%x", words[i]);
185 }
186 /* Was it a trailing run of 0x00's? */
187 if (best.base != -1 && (best.base + best.len) ==
188 (NS_IN6ADDRSZ / NS_INT16SZ))
189 *tp++ = ':';
190 *tp++ = '\0';
191
192 /*
193 * Check for overflow, copy, and we're done.
194 */
195 if ((size_t)(tp - tmp) > size) {
196 errno = ENOSPC;
197 return (NULL);
198 }
199 strcpy(dst, tmp);
200 return (dst);
201 }
202
203 #ifdef USE_IPV6
204 void MyDnsSink::onDnsResult(const DNSResult<DnsAAAARecord>& result)
205 {
206 cout << "AAAA records" << endl;
207 cout << "Status: " << result.status << endl;
208 cout << "Domain: " << result.domain << endl;
209 if (result.status == 0)
210 {
211 for (vector<DnsAAAARecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
212 {
213 char str[256];
214 cout << MyInet_ntop6((const u_char*)&(*it).v6Address(), str, sizeof(str)) << endl;
215 }
216 }
217 cout << endl;
218 }
219 #endif
220
221
222 // NOTE: In order to run this test, you need to uncomment out the USE_LOCAL_DNS define in
223 // ExternalDnsFactory.cxx.
224 main(int argc, char* argv[])
225 {
226 {
227 const char* const key = "yahoo.com";
228 MyDnsSink sink;
229 DnsStub stub;
230 DnsInterface dns(stub);
231 stub.lookup<RR_A>(key, Protocol::Sip, &sink);
232 }
233
234 {
235 const char* const key = "_ldap._tcp.openldap.org";
236 MyDnsSink sink;
237 DnsStub stub;
238 DnsInterface dns(stub);
239 stub.lookup<RR_SRV>(key, Protocol::Sip, &sink);
240 }
241
242 {
243 #ifdef USE_IPV6
244 const char* const key = "quartz";
245 MyDnsSink sink;
246 DnsStub stub;
247 DnsInterface dns(stub);
248 stub.lookup<RR_AAAA>(key, Protocol::Sip, &sink);
249 #endif
250 }
251
252 {
253 const char* const key = "www.google.com";
254 MyDnsSink sink;
255 DnsStub stub;
256 DnsInterface dns(stub);
257 stub.lookup<RR_CNAME>(key, Protocol::Sip, &sink);
258 }
259
260 return 0;
261 }
262 /* ====================================================================
263 * The Vovida Software License, Version 1.0
264 *
265 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
266 *
267 * Redistribution and use in source and binary forms, with or without
268 * modification, are permitted provided that the following conditions
269 * are met:
270 *
271 * 1. Redistributions of source code must retain the above copyright
272 * notice, this list of conditions and the following disclaimer.
273 *
274 * 2. Redistributions in binary form must reproduce the above copyright
275 * notice, this list of conditions and the following disclaimer in
276 * the documentation and/or other materials provided with the
277 * distribution.
278 *
279 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
280 * and "Vovida Open Communication Application Library (VOCAL)" must
281 * not be used to endorse or promote products derived from this
282 * software without prior written permission. For written
283 * permission, please contact vocal@vovida.org.
284 *
285 * 4. Products derived from this software may not be called "VOCAL", nor
286 * may "VOCAL" appear in their name, without prior written
287 * permission of Vovida Networks, Inc.
288 *
289 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
290 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
291 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
292 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
293 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
294 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
295 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
296 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
297 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
298 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
299 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
300 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
301 * DAMAGE.
302 *
303 * ====================================================================
304 *
305 * This software consists of voluntary contributions made by Vovida
306 * Networks, Inc. and many individuals on behalf of Vovida Networks,
307 * Inc. For more information on Vovida Networks, Inc., please see
308 * <http://www.vovida.org/>.
309 *
310 */
311

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27