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

Contents of /main/resip/stack/test/testDnsStub.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: 8594 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 <iostream>
14 #include <list>
15
16 #include "rutil/Lock.hxx"
17 #include "rutil/Mutex.hxx"
18 #include "rutil/Socket.hxx"
19 #include "rutil/Logger.hxx"
20 #include "rutil/ThreadIf.hxx"
21 #include "rutil/DnsUtil.hxx"
22 #include "resip/stack/DnsResult.hxx"
23 #include "resip/stack/SipStack.hxx"
24 #include "rutil/dns/RRVip.hxx"
25 #include "rutil/dns/QueryTypes.hxx"
26 #include "rutil/dns/DnsStub.hxx"
27
28 using namespace std;
29
30
31 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
32
33 const char bf[] = "\033[01;34m";
34 const char gf[] = "\033[01;32m";
35 const char rf[] = "\033[01;31m";
36 const char ub[] = "\033[01;00m";
37
38 bool gComplete = false;
39
40 #ifdef WIN32
41 #define usleep(x) Sleep(x/1000)
42 #define sleep(x) Sleep(x)
43 #endif
44
45 namespace resip
46 {
47
48 class MyDnsSink : public DnsResultSink
49 {
50 public:
51 void onDnsResult(const DNSResult<DnsHostRecord>&);
52 #ifdef USE_IPV6
53 void onDnsResult(const DNSResult<DnsAAAARecord>&);
54 #endif
55 void onDnsResult(const DNSResult<DnsSrvRecord>&);
56 void onDnsResult(const DNSResult<DnsNaptrRecord>&);
57 void onDnsResult(const DNSResult<DnsCnameRecord>&);
58 };
59
60 void MyDnsSink::onDnsResult(const DNSResult<DnsHostRecord>& result)
61 {
62 cout << "A records" << endl;
63 cout << "Status: " << result.status << endl;
64 cout << "Domain: " << result.domain << endl;
65 if (result.status == 0)
66 {
67 for (vector<DnsHostRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
68 {
69 cout << (*it).host() << endl;
70 }
71 }
72 else
73 {
74 cout << "Dns look up failed:" << result.msg << endl;
75 }
76 gComplete = true;
77 }
78
79 void MyDnsSink::onDnsResult(const DNSResult<DnsNaptrRecord>& result)
80 {
81 cout << "Naptr records" << endl;
82 cout << "Status: " << result.status << endl;
83 cout << "Domain: " << result.domain << endl;
84 if (result.status == 0)
85 {
86 for (vector<DnsNaptrRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
87 {
88 cout << (*it).name() << endl;
89 }
90 }
91 else
92 {
93 cout << "Dns look up failed:" << result.msg << endl;
94 }
95 gComplete = true;
96 }
97
98 void MyDnsSink::onDnsResult(const DNSResult<DnsCnameRecord>& result)
99 {
100 cout << "CNAME records" << endl;
101 cout << "Status: " << result.status << endl;
102 cout << "Domain: " << result.domain << endl;
103 if (result.status == 0)
104 {
105 for (vector<DnsCnameRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
106 {
107 cout << (*it).cname() << endl;
108 }
109 }
110 else
111 {
112 cout << "Dns look up failed:" << result.msg << endl;
113 }
114 gComplete = true;
115 }
116
117 void MyDnsSink::onDnsResult(const DNSResult<DnsSrvRecord>& result)
118 {
119 cout << "SRV records" << endl;
120 cout << "Status: " << result.status << endl;
121 cout << "Domain: " << result.domain << endl;
122 if (result.status == 0)
123 {
124 for (vector<DnsSrvRecord>::const_iterator it = result.records.begin(); it != result.records.end(); ++it)
125 {
126 cout << "Name: " << (*it).name() << endl;
127 cout << "Priority: " << (*it).priority() << endl;
128 cout << "Weight: " << (*it).weight() << endl;
129 cout << "Port: " << (*it).port() << endl;
130 cout << "Target: " << (*it).target() << endl;
131 }
132 }
133 else
134 {
135 cout << "Dns look up failed:" << result.msg << endl;
136 }
137 gComplete = true;
138 }
139
140 #ifdef USE_IPV6
141 void MyDnsSink::onDnsResult(const DNSResult<DnsAAAARecord>& result)
142 {
143 gComplete = true;
144 }
145 #endif
146
147 class TestDns : public ThreadIf, public DnsStub
148 {
149 public:
150 TestDns(const DnsStub::NameserverList& additional) : DnsStub(additional)
151 {
152 }
153
154 void thread()
155 {
156 while (!waitForShutdown(100))
157 {
158 FdSet fdset;
159 buildFdSet(fdset);
160 fdset.selectMilliSeconds(1);
161 process(fdset);
162 }
163 }
164 };
165
166 }
167
168 using namespace resip;
169
170 int
171 main(int argc, const char** argv)
172 {
173 if (argc < 3)
174 {
175 cout << "usage: " << argv[0] << " target" << " type" << endl;
176 cout << "Valid type values: " << endl;
177 cout << "A Record - 1" << endl;
178 cout << "CNAME - 5" << endl;
179 cout << "SRV - 33" << endl;
180 cout << "NAPTR - 35" << endl;
181 cout << "AAAA - 28" << endl;
182 return 0;
183 }
184
185 DnsStub::NameserverList nameServerList = DnsStub::EmptyNameserverList;
186 if(argc == 4)
187 {
188 Tuple tuple(Data(argv[3]), 0, V4);
189 nameServerList.push_back(tuple.toGenericIPAddress());
190 }
191
192 char* logType = "cout";
193 //char* logLevel = "STACK";
194 char* logLevel = "DEBUG";
195
196 #if defined(HAVE_POPT_H)
197 struct poptOption table[] = {
198 {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
199 {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
200 POPT_AUTOHELP
201 { NULL, 0, 0, NULL, 0 }
202 };
203
204 poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
205 poptGetNextOpt(context);
206 #endif
207
208 Log::initialize(logType, logLevel, argv[0]);
209 initNetwork();
210 TestDns dns(nameServerList);
211 dns.run();
212 cerr << "Starting" << endl;
213 #if defined(HAVE_POPT_H)
214 const char** args = poptGetArgs(context);
215 #else
216 const char** args = argv;
217 #endif
218
219 MyDnsSink sink;
220
221 switch (atoi(argv[2]))
222 {
223 case 1:
224 dns.lookup<RR_A>(argv[1], Protocol::Sip, &sink);
225 break;
226 case 5:
227 dns.lookup<RR_CNAME>(argv[1], Protocol::Sip, &sink);
228 break;
229 #ifdef USE_IPV6
230 case 28:
231 dns.lookup<RR_AAAA>(argv[1], Protocol::Sip, &sink);
232 break;
233 #endif
234 case 33:
235 dns.lookup<RR_SRV>(argv[1], Protocol::Sip, &sink);
236 break;
237 case 35:
238 dns.lookup<RR_NAPTR>(argv[1], Protocol::Sip, &sink);
239 break;
240 default:
241 cout << "Invalid Dns type" << endl;
242 return 0;
243 }
244
245 while (!gComplete)
246 {
247 #ifdef WIN32
248 sleep(100);
249 #else
250 sleep(1);
251 #endif
252 }
253
254 dns.shutdown();
255 dns.join();
256
257 return 0;
258 }
259
260
261 /* ====================================================================
262 * The Vovida Software License, Version 1.0
263 *
264 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
265 *
266 * Redistribution and use in source and binary forms, with or without
267 * modification, are permitted provided that the following conditions
268 * are met:
269 *
270 * 1. Redistributions of source code must retain the above copyright
271 * notice, this list of conditions and the following disclaimer.
272 *
273 * 2. Redistributions in binary form must reproduce the above copyright
274 * notice, this list of conditions and the following disclaimer in
275 * the documentation and/or other materials provided with the
276 * distribution.
277 *
278 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
279 * and "Vovida Open Communication Application Library (VOCAL)" must
280 * not be used to endorse or promote products derived from this
281 * software without prior written permission. For written
282 * permission, please contact vocal@vovida.org.
283 *
284 * 4. Products derived from this software may not be called "VOCAL", nor
285 * may "VOCAL" appear in their name, without prior written
286 * permission of Vovida Networks, Inc.
287 *
288 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
289 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
290 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
291 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
292 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
293 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
294 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
295 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
296 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
297 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
298 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
299 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
300 * DAMAGE.
301 *
302 * ====================================================================
303 *
304 * This software consists of voluntary contributions made by Vovida
305 * Networks, Inc. and many individuals on behalf of Vovida Networks,
306 * Inc. For more information on Vovida Networks, Inc., please see
307 * <http://www.vovida.org/>.
308 *
309 */

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