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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5948 - (show annotations) (download)
Fri Feb 17 15:30:16 2006 UTC (13 years, 8 months ago) by dworley
File MIME type: text/plain
File size: 7791 byte(s)
Setting more svn: properties, and adding EOLs to the ends of files
that need it.

1 #ifndef WIN32
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <arpa/inet.h>
5 #include <netdb.h>
6 #else
7 #include <sys/types.h>
8 #endif
9
10 #include <stdio.h>
11 #include <errno.h>
12
13 #include "rutil/Socket.hxx"
14
15 #include "resip/stack/Symbols.hxx"
16 #include "resip/stack/Uri.hxx"
17
18 #include "rutil/Socket.hxx"
19 #include "rutil/Data.hxx"
20 #include "rutil/Logger.hxx"
21 #include "rutil/ParseBuffer.hxx"
22
23 #include "Resolver.hxx"
24
25 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::SIP
26
27 using namespace resip;
28
29 Resolver::Resolver(const Uri& uri) :
30 mHost(uri.host()),
31 mPort(uri.port() ? uri.port() : 5060)
32 {
33 bool isNumeric = isIpAddress(mHost);
34 if (!uri.exists(p_transport) )
35 {
36 if (isNumeric)
37 {
38 if (uri.scheme() == Symbols::Sip)
39 {
40 mTransport = UDP;
41 }
42 else if (uri.scheme() == Symbols::Sips)
43 {
44 mTransport = TCP;
45 }
46 }
47 else // not numeric
48 {
49 if (1) // uri.portSpecified()) // !jf!
50 {
51 if (uri.scheme() == Symbols::Sip)
52 {
53 mTransport = UDP;
54 }
55 else if (uri.scheme() == Symbols::Sips)
56 {
57 mTransport = TCP;
58 }
59 }
60 else // NAPTR query - yuck!
61 {
62 // Rohan, step up to the plate buddy.
63 mTransport = UDP; // !jf! not done yet
64 }
65 }
66 }
67
68 if (!isNumeric)
69 {
70 if (1) // !jf! uri.portSpecified())
71 {
72 lookupARecords();
73 // do an A or AAAA DNS lookup
74
75 }
76 else
77 {
78 // do SRV lookup on result of NAPTR lookup
79
80 // if no NAPTR lookup, do SRV lookup on _sips for sips and _sip for sip
81
82 // if no result on SRV lookup, do an A or AAAA lookup
83
84 }
85 }
86 else
87 {
88 Tuple tuple;
89 if (inet_pton(AF_INET, mHost.c_str(), &tuple.ipv4.s_addr) <= 0)
90 {
91 DebugLog( << "inet_pton failed to parse address: " << mHost << " " << strerror(errno));
92 assert(0);
93 }
94 tuple.port = mPort;
95 tuple.transportType = mTransport;
96
97 mNextHops.push_back(tuple);
98 }
99
100 }
101
102
103 Resolver::Resolver(const Data& host, int port, TransportType transport)
104 : mTransport(transport),
105 mHost(host),
106 mPort(port)
107 {
108 bool isNumeric = isIpAddress(mHost);
109 if (!isNumeric)
110 {
111 if (1) // !jf! uri.portSpecified())
112 {
113 lookupARecords();
114 // do an A or AAAA DNS lookup
115
116 }
117 else
118 {
119 // do SRV lookup on result of NAPTR lookup
120
121 // if no NAPTR lookup, do SRV lookup on _sips for sips and _sip for sip
122
123 // if no result on SRV lookup, do an A or AAAA lookup
124
125 }
126 }
127 else
128 {
129 Tuple tuple;
130 if (inet_pton(AF_INET, mHost.c_str(), &tuple.ipv4.s_addr) <= 0)
131 {
132 DebugLog( << "inet_pton failed to parse address: " << mHost << " " << strerror(errno));
133 assert(0);
134 }
135 tuple.port = mPort;
136 tuple.transportType = mTransport;
137
138 mNextHops.push_back(tuple);
139 }
140 }
141
142
143 void
144 Resolver::lookupARecords()
145 {
146 struct hostent hostbuf;
147 struct hostent* result=0;
148
149 int herrno=0;
150 char buffer[8192];
151 #ifdef __QNX__
152 result = gethostbyname_r (mHost.c_str(), &hostbuf, buffer, sizeof(buffer), &herrno);
153 if (result == 0)
154 {
155 #else
156
157 #if defined( WIN32 ) || defined( __APPLE__ ) || defined (__SUNPRO_CC) || defined(__FreeBSD__)
158 assert(0); // !cj!
159 int ret = -1;
160 #else
161 int ret = gethostbyname_r (mHost.c_str(), &hostbuf, buffer, sizeof(buffer), &result, &herrno);
162 #endif
163 assert (ret != ERANGE);
164
165 if (ret != 0)
166 {
167 #endif
168 switch (herrno)
169 {
170 case HOST_NOT_FOUND:
171 InfoLog ( << "host not found: " << mHost);
172 break;
173 case NO_DATA:
174 InfoLog ( << "no data found for: " << mHost);
175 break;
176 case NO_RECOVERY:
177 InfoLog ( << "no recovery lookup up: " << mHost);
178 break;
179 case TRY_AGAIN:
180 InfoLog ( << "try again: " << mHost);
181 break;
182 }
183 }
184 else
185 {
186 assert(result);
187 assert(result->h_length == 4);
188
189 DebugLog (<< "DNS lookup of " << mHost << ": canonical name: " << result->h_name);
190 char str[256];
191 for (char** pptr = result->h_addr_list; *pptr != 0; pptr++)
192 {
193 Tuple tuple;
194 tuple.ipv4.s_addr = *((u_int32_t*)(*pptr));
195 tuple.port = mPort;
196 tuple.transportType = mTransport;
197
198 mNextHops.push_back(tuple);
199 #ifndef WIN32
200 DebugLog (<< inet_ntop(AF_INET, &tuple.ipv4.s_addr, str, sizeof(str)));
201 #endif
202
203 }
204 }
205 }
206
207 bool
208 Resolver::isIpAddress(const Data& data)
209 {
210 // ok, this is fairly monstrous but it works.
211 unsigned int p1,p2,p3,p4;
212 int count=0;
213 int result = sscanf( data.c_str(),
214 "%u.%u.%u.%u%n",
215 &p1, &p2, &p3, &p4, &count );
216
217 if ( (result == 4) && (p1 <= 255) && (p2 <= 255) && (p3 <= 255) && (p4 <= 255) && (count == int(data.size())) )
218 {
219 return true;
220 }
221 else
222 {
223 return false;
224 }
225 }
226
227
228 Data
229 Resolver::getHostName()
230 {
231 char buffer[255];
232 if (gethostname(buffer, sizeof(buffer)) < 0)
233 {
234 InfoLog (<< "Failed gethostname() " << strerror(errno));
235 return "localhost";
236 }
237 else
238 {
239 return Data(buffer);
240 }
241 }
242 /* ====================================================================
243 * The Vovida Software License, Version 1.0
244 *
245 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
246 *
247 * Redistribution and use in source and binary forms, with or without
248 * modification, are permitted provided that the following conditions
249 * are met:
250 *
251 * 1. Redistributions of source code must retain the above copyright
252 * notice, this list of conditions and the following disclaimer.
253 *
254 * 2. Redistributions in binary form must reproduce the above copyright
255 * notice, this list of conditions and the following disclaimer in
256 * the documentation and/or other materials provided with the
257 * distribution.
258 *
259 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
260 * and "Vovida Open Communication Application Library (VOCAL)" must
261 * not be used to endorse or promote products derived from this
262 * software without prior written permission. For written
263 * permission, please contact vocal@vovida.org.
264 *
265 * 4. Products derived from this software may not be called "VOCAL", nor
266 * may "VOCAL" appear in their name, without prior written
267 * permission of Vovida Networks, Inc.
268 *
269 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
270 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
271 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
272 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
273 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
274 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
275 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
276 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
277 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
278 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
280 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
281 * DAMAGE.
282 *
283 * ====================================================================
284 *
285 * This software consists of voluntary contributions made by Vovida
286 * Networks, Inc. and many individuals on behalf of Vovida Networks,
287 * Inc. For more information on Vovida Networks, Inc., please see
288 * <http://www.vovida.org/>.
289 *
290 */

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