]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/res_query.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / BsdSocketLib / res_query.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1988, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * Portions copyright (c) 1999, 2000\r
6 * Intel Corporation.\r
7 * All rights reserved.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 *\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 *\r
16 * 2. Redistributions in binary form must reproduce the above copyright\r
17 * notice, this list of conditions and the following disclaimer in the\r
18 * documentation and/or other materials provided with the distribution.\r
19 *\r
20 * 3. All advertising materials mentioning features or use of this software\r
21 * must display the following acknowledgement:\r
22 *\r
23 * This product includes software developed by the University of\r
24 * California, Berkeley, Intel Corporation, and its contributors.\r
25 *\r
26 * 4. Neither the name of University, Intel Corporation, or their respective\r
27 * contributors may be used to endorse or promote products derived from\r
28 * this software without specific prior written permission.\r
29 *\r
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
41 *\r
42 */\r
43\r
44/*\r
45 * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
46 *\r
47 * Permission to use, copy, modify, and distribute this software for any\r
48 * purpose with or without fee is hereby granted, provided that the above\r
49 * copyright notice and this permission notice appear in all copies, and that\r
50 * the name of Digital Equipment Corporation not be used in advertising or\r
51 * publicity pertaining to distribution of the document or software without\r
52 * specific, written prior permission.\r
53 *\r
54 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
55 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
57 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
61 * SOFTWARE.\r
62 */\r
63\r
64/*\r
65 * Portions Copyright (c) 1996 by Internet Software Consortium.\r
66 *\r
67 * Permission to use, copy, modify, and distribute this software for any\r
68 * purpose with or without fee is hereby granted, provided that the above\r
69 * copyright notice and this permission notice appear in all copies.\r
70 *\r
71 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
72 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
73 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
74 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
75 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
76 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
77 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
78 * SOFTWARE.\r
79 */\r
80\r
81#if defined(LIBC_SCCS) && !defined(lint)\r
82static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";\r
83static char orig_rcsid = "From: Id: res_query.c,v 8.14 1997/06/09 17:47:05 halley Exp $";\r
84static char rcsid[] = "$Id: res_query.c,v 1.1.1.1 2003/11/19 01:51:38 kyu3 Exp $";\r
85#endif /* LIBC_SCCS and not lint */\r
86\r
87#include <sys/types.h>\r
88#include <sys/param.h>\r
89#include <netinet/in.h>\r
90#include <arpa/inet.h>\r
91#include <arpa/nameser.h>\r
92#include <ctype.h>\r
93#include <errno.h>\r
94#include <netdb.h>\r
95#include <resolv.h>\r
96#include <stdio.h>\r
97#include <stdlib.h>\r
98#include <string.h>\r
99\r
100#include "res_config.h"\r
101\r
102#if PACKETSZ > 1024\r
103#define MAXPACKET PACKETSZ\r
104#else\r
105#define MAXPACKET 1024\r
106#endif\r
107\r
108/*\r
109 * Formulate a normal query, send, and await answer.\r
110 * Returned answer is placed in supplied buffer "answer".\r
111 * Perform preliminary check of answer, returning success only\r
112 * if no error is indicated and the answer count is nonzero.\r
113 * Return the size of the response on success, -1 on error.\r
114 * Error number is left in h_errno.\r
115 *\r
116 * Caller must parse answer and determine whether it answers the question.\r
117 */\r
118int\r
119res_query(\r
120 const char *name, /* domain name */\r
121 int class, /* class of query */\r
122 int type, /* type of query */\r
123 u_char *answer, /* buffer to put answer */\r
124 int anslen /* size of answer buffer */\r
125 )\r
126{\r
127 u_char buf[MAXPACKET];\r
128 HEADER *hp = (HEADER *) answer;\r
129 int n;\r
130\r
131 hp->rcode = NOERROR; /* default */\r
132\r
133 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
134 h_errno = NETDB_INTERNAL;\r
135 return (-1);\r
136 }\r
137#ifdef DEBUG\r
138 if (_res.options & RES_DEBUG)\r
139 printf(";; res_query(%s, %d, %d)\n", name, class, type);\r
140#endif\r
141\r
142 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,\r
143 buf, sizeof(buf));\r
144 if (n <= 0) {\r
145#ifdef DEBUG\r
146 if (_res.options & RES_DEBUG)\r
147 printf(";; res_query: mkquery failed\n");\r
148#endif\r
149 h_errno = NO_RECOVERY;\r
150 return (n);\r
151 }\r
152 n = res_send(buf, n, answer, anslen);\r
153 if (n < 0) {\r
154#ifdef DEBUG\r
155 if (_res.options & RES_DEBUG)\r
156 printf(";; res_query: send error\n");\r
157#endif\r
158 h_errno = TRY_AGAIN;\r
159 return (n);\r
160 }\r
161\r
162 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {\r
163#ifdef DEBUG\r
164 if (_res.options & RES_DEBUG)\r
165 printf(";; rcode = %d, ancount=%d\n", hp->rcode,\r
166 ntohs(hp->ancount));\r
167#endif\r
168 switch (hp->rcode) {\r
169 case NXDOMAIN:\r
170 h_errno = HOST_NOT_FOUND;\r
171 break;\r
172 case SERVFAIL:\r
173 h_errno = TRY_AGAIN;\r
174 break;\r
175 case NOERROR:\r
176 h_errno = NO_DATA;\r
177 break;\r
178 case FORMERR:\r
179 case NOTIMP:\r
180 case REFUSED:\r
181 default:\r
182 h_errno = NO_RECOVERY;\r
183 break;\r
184 }\r
185 return (-1);\r
186 }\r
187 return (n);\r
188}\r
189\r
190/*\r
191 * Formulate a normal query, send, and retrieve answer in supplied buffer.\r
192 * Return the size of the response on success, -1 on error.\r
193 * If enabled, implement search rules until answer or unrecoverable failure\r
194 * is detected. Error code, if any, is left in h_errno.\r
195 */\r
196int\r
197res_search(\r
198 const char *name, /* domain name */\r
199 int class, /* class of query */\r
200 int type, /* type of query */\r
201 u_char *answer, /* buffer to put answer */\r
202 int anslen /* size of answer */\r
203 )\r
204{\r
205 const char *cp, * const *domain;\r
206 HEADER *hp = (HEADER *) answer;\r
207 u_int dots;\r
208 int trailing_dot, ret, saved_herrno;\r
209 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;\r
210\r
211 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
212 h_errno = NETDB_INTERNAL;\r
213 return (-1);\r
214 }\r
215 errno = 0;\r
216 h_errno = HOST_NOT_FOUND; /* default, if we never query */\r
217 dots = 0;\r
218 for (cp = name; *cp; cp++)\r
219 dots += (*cp == '.');\r
220 trailing_dot = 0;\r
221 if (cp > name && *--cp == '.')\r
222 trailing_dot++;\r
223\r
224 /* If there aren't any dots, it could be a user-level alias */\r
225 if (!dots && (cp = hostalias(name)) != NULL)\r
226 return (res_query(cp, class, type, answer, anslen));\r
227\r
228 /*\r
229 * If there are dots in the name already, let's just give it a try\r
230 * 'as is'. The threshold can be set with the "ndots" option.\r
231 */\r
232 saved_herrno = -1;\r
233 if (dots >= _res.ndots) {\r
234 ret = res_querydomain(name, NULL, class, type, answer, anslen);\r
235 if (ret > 0)\r
236 return (ret);\r
237 saved_herrno = h_errno;\r
238 tried_as_is++;\r
239 }\r
240\r
241 /*\r
242 * We do at least one level of search if\r
243 * - there is no dot and RES_DEFNAME is set, or\r
244 * - there is at least one dot, there is no trailing dot,\r
245 * and RES_DNSRCH is set.\r
246 */\r
247 if ((!dots && (_res.options & RES_DEFNAMES)) ||\r
248 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {\r
249 int done = 0;\r
250\r
251 for (domain = (const char * const *)_res.dnsrch;\r
252 *domain && !done;\r
253 domain++) {\r
254\r
255 ret = res_querydomain(name, *domain, class, type,\r
256 answer, anslen);\r
257 if (ret > 0)\r
258 return (ret);\r
259\r
260 /*\r
261 * If no server present, give up.\r
262 * If name isn't found in this domain,\r
263 * keep trying higher domains in the search list\r
264 * (if that's enabled).\r
265 * On a NO_DATA error, keep trying, otherwise\r
266 * a wildcard entry of another type could keep us\r
267 * from finding this entry higher in the domain.\r
268 * If we get some other error (negative answer or\r
269 * server failure), then stop searching up,\r
270 * but try the input name below in case it's\r
271 * fully-qualified.\r
272 */\r
273 if (errno == ECONNREFUSED) {\r
274 h_errno = TRY_AGAIN;\r
275 return (-1);\r
276 }\r
277\r
278 switch (h_errno) {\r
279 case NO_DATA:\r
280 got_nodata++;\r
281 /* FALLTHROUGH */\r
282 case HOST_NOT_FOUND:\r
283 /* keep trying */\r
284 break;\r
285 case TRY_AGAIN:\r
286 if (hp->rcode == SERVFAIL) {\r
287 /* try next search element, if any */\r
288 got_servfail++;\r
289 break;\r
290 }\r
291 /* FALLTHROUGH */\r
292 default:\r
293 /* anything else implies that we're done */\r
294 done++;\r
295 }\r
296\r
297 /* if we got here for some reason other than DNSRCH,\r
298 * we only wanted one iteration of the loop, so stop.\r
299 */\r
300 if (!(_res.options & RES_DNSRCH))\r
301 done++;\r
302 }\r
303 }\r
304\r
305 /*\r
306 * If we have not already tried the name "as is", do that now.\r
307 * note that we do this regardless of how many dots were in the\r
308 * name or whether it ends with a dot unless NOTLDQUERY is set.\r
309 */\r
310 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {\r
311 ret = res_querydomain(name, NULL, class, type, answer, anslen);\r
312 if (ret > 0)\r
313 return (ret);\r
314 }\r
315\r
316 /* if we got here, we didn't satisfy the search.\r
317 * if we did an initial full query, return that query's h_errno\r
318 * (note that we wouldn't be here if that query had succeeded).\r
319 * else if we ever got a nodata, send that back as the reason.\r
320 * else send back meaningless h_errno, that being the one from\r
321 * the last DNSRCH we did.\r
322 */\r
323 if (saved_herrno != -1)\r
324 h_errno = saved_herrno;\r
325 else if (got_nodata)\r
326 h_errno = NO_DATA;\r
327 else if (got_servfail)\r
328 h_errno = TRY_AGAIN;\r
329 return (-1);\r
330}\r
331\r
332/*\r
333 * Perform a call on res_query on the concatenation of name and domain,\r
334 * removing a trailing dot from name if domain is NULL.\r
335 */\r
336int\r
337res_querydomain(\r
338 const char *name,\r
339 const char *domain,\r
340 int class, /* class of query */\r
341 int type, /* type of query */\r
342 u_char *answer, /* buffer to put answer */\r
343 int anslen /* size of answer */\r
344 )\r
345{\r
346 char nbuf[MAXDNAME];\r
347 const char *longname = nbuf;\r
348 int n, d;\r
349\r
350 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
351 h_errno = NETDB_INTERNAL;\r
352 return (-1);\r
353 }\r
354#ifdef DEBUG\r
355 if (_res.options & RES_DEBUG)\r
356 printf(";; res_querydomain(%s, %s, %d, %d)\n",\r
357 name, domain?domain:"<Nil>", class, type);\r
358#endif\r
359 if (domain == NULL) {\r
360 /*\r
361 * Check for trailing '.';\r
362 * copy without '.' if present.\r
363 */\r
364 n = (int)strlen(name);\r
365 if (n >= MAXDNAME) {\r
366 h_errno = NO_RECOVERY;\r
367 return (-1);\r
368 }\r
369 n--;\r
370 if (n >= 0 && name[n] == '.') {\r
371 strncpy(nbuf, name, n);\r
372 nbuf[n] = '\0';\r
373 } else\r
374 longname = name;\r
375 } else {\r
376 n = (int)strlen(name);\r
377 d = (int)strlen(domain);\r
378 if (n + d + 1 >= MAXDNAME) {\r
379 h_errno = NO_RECOVERY;\r
380 return (-1);\r
381 }\r
382 sprintf(nbuf, "%s.%s", name, domain);\r
383 }\r
384 return (res_query(longname, class, type, answer, anslen));\r
385}\r
386\r
387const char *\r
388hostalias(\r
389 const char *name\r
390 )\r
391{\r
392 register char *cp1, *cp2;\r
393 FILE *fp;\r
394 char *file;\r
395 char buf[BUFSIZ];\r
396 static char abuf[MAXDNAME];\r
397\r
398 if (_res.options & RES_NOALIASES)\r
399 return (NULL);\r
400#ifdef _ORG_FREEBSD_\r
401 if (issetugid())\r
402 return (NULL);\r
403#endif\r
404 file = getenv("HOSTALIASES");\r
405 if (file == NULL || (fp = fopen(file, "r")) == NULL)\r
406 return (NULL);\r
407 setbuf(fp, NULL);\r
408 buf[sizeof(buf) - 1] = '\0';\r
409 while (fgets(buf, sizeof(buf), fp)) {\r
410 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)\r
411 ;\r
412 if (!*cp1)\r
413 break;\r
414 *cp1 = '\0';\r
415 if (!strcasecmp(buf, name)) {\r
416 while (isspace(*++cp1))\r
417 ;\r
418 if (!*cp1)\r
419 break;\r
420 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)\r
421 ;\r
422 abuf[sizeof(abuf) - 1] = *cp2 = '\0';\r
423 strncpy(abuf, cp1, sizeof(abuf) - 1);\r
424 fclose(fp);\r
425 return (abuf);\r
426 }\r
427 }\r
428 fclose(fp);\r
429 return (NULL);\r
430}\r