(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / resolv / nss_dns / dns-network.c
blob6ff60f311975221f191b2ffe69275d84bf17ec3b
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Parts of this file are plain copies of the file `getnetnamadr.c' from
22 the bind package and it has the following copyright. */
24 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
25 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
32 * Copyright (c) 1983, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
60 #include <ctype.h>
61 #include <errno.h>
62 #include <netdb.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
67 #include "nsswitch.h"
68 #include <arpa/inet.h>
70 /* Maximum number of aliases we allow. */
71 #define MAX_NR_ALIASES 48
74 #if PACKETSZ > 65536
75 # define MAXPACKET PACKETSZ
76 #else
77 # define MAXPACKET 65536
78 #endif
81 typedef enum
83 BYADDR,
84 BYNAME
85 } lookup_method;
88 /* We need this time later. */
89 typedef union querybuf
91 HEADER hdr;
92 u_char buf[MAXPACKET];
93 } querybuf;
95 /* These functions are defined in res_comp.c. */
96 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
97 extern int __ns_name_ntop (const u_char *, char *, size_t) __THROW;
98 extern int __ns_name_unpack (const u_char *, const u_char *,
99 const u_char *, u_char *, size_t) __THROW;
102 /* Prototypes for local functions. */
103 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
104 struct netent *result, char *buffer,
105 size_t buflen, lookup_method net_i);
108 enum nss_status
109 _nss_dns_getnetbyname_r (const char *name, struct netent *result,
110 char *buffer, size_t buflen, int *errnop,
111 int *herrnop)
113 /* Return entry for network with NAME. */
114 union
116 querybuf *buf;
117 u_char *ptr;
118 } net_buffer;
119 querybuf *orig_net_buffer;
120 int anslen;
121 char *qbuf;
122 enum nss_status status;
124 if (__res_maybe_init (&_res, 0) == -1)
125 return NSS_STATUS_UNAVAIL;
127 qbuf = strdupa (name);
129 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
131 anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
132 1024, &net_buffer.ptr);
133 if (anslen < 0)
135 /* Nothing found. */
136 *errnop = errno;
137 if (net_buffer.buf != orig_net_buffer)
138 free (net_buffer.buf);
139 return (errno == ECONNREFUSED
140 || errno == EPFNOSUPPORT
141 || errno == EAFNOSUPPORT)
142 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
145 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, BYNAME);
146 if (net_buffer.buf != orig_net_buffer)
147 free (net_buffer.buf);
148 return status;
152 enum nss_status
153 _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
154 char *buffer, size_t buflen, int *errnop,
155 int *herrnop)
157 /* Return entry for network with NAME. */
158 enum nss_status status;
159 union
161 querybuf *buf;
162 u_char *ptr;
163 } net_buffer;
164 querybuf *orig_net_buffer;
165 unsigned int net_bytes[4];
166 char qbuf[MAXDNAME];
167 int cnt, anslen;
168 u_int32_t net2;
169 int olderr = errno;
171 /* No net address lookup for IPv6 yet. */
172 if (type != AF_INET)
173 return NSS_STATUS_UNAVAIL;
175 if (__res_maybe_init (&_res, 0) == -1)
176 return NSS_STATUS_UNAVAIL;
178 net2 = (u_int32_t) net;
179 for (cnt = 4; net2 != 0; net2 >>= 8)
180 net_bytes[--cnt] = net2 & 0xff;
182 switch (cnt)
184 case 3:
185 /* Class A network. */
186 sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]);
187 break;
188 case 2:
189 /* Class B network. */
190 sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]);
191 break;
192 case 1:
193 /* Class C network. */
194 sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
195 net_bytes[1]);
196 break;
197 case 0:
198 /* Class D - E network. */
199 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
200 net_bytes[1], net_bytes[0]);
201 break;
204 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
206 anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
207 1024, &net_buffer.ptr);
208 if (anslen < 0)
210 /* Nothing found. */
211 int err = errno;
212 __set_errno (olderr);
213 if (net_buffer.buf != orig_net_buffer)
214 free (net_buffer.buf);
215 return (err == ECONNREFUSED
216 || err == EPFNOSUPPORT
217 || err == EAFNOSUPPORT)
218 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
221 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, BYADDR);
222 if (net_buffer.buf != orig_net_buffer)
223 free (net_buffer.buf);
224 if (status == NSS_STATUS_SUCCESS)
226 /* Strip trailing zeros. */
227 unsigned int u_net = net; /* Maybe net should be unsigned? */
229 while ((u_net & 0xff) == 0 && u_net != 0)
230 u_net >>= 8;
231 result->n_net = u_net;
234 return status;
238 #undef offsetof
239 #define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member)
241 static enum nss_status
242 getanswer_r (const querybuf *answer, int anslen, struct netent *result,
243 char *buffer, size_t buflen, lookup_method net_i)
246 * Find first satisfactory answer
248 * answer --> +------------+ ( MESSAGE )
249 * | Header |
250 * +------------+
251 * | Question | the question for the name server
252 * +------------+
253 * | Answer | RRs answering the question
254 * +------------+
255 * | Authority | RRs pointing toward an authority
256 * | Additional | RRs holding additional information
257 * +------------+
259 struct net_data
261 char *aliases[MAX_NR_ALIASES];
262 char linebuffer[0];
263 } *net_data = (struct net_data *) buffer;
264 int linebuflen = buflen - offsetof (struct net_data, linebuffer);
265 const char *end_of_message = &answer->buf[anslen];
266 const HEADER *header_pointer = &answer->hdr;
267 /* #/records in the answer section. */
268 int answer_count = ntohs (header_pointer->ancount);
269 /* #/entries in the question section. */
270 int question_count = ntohs (header_pointer->qdcount);
271 char *bp = net_data->linebuffer;
272 const char *cp = &answer->buf[HFIXEDSZ];
273 char **alias_pointer;
274 int have_answer;
275 char *ans;
276 u_char packtmp[NS_MAXCDNAME];
278 if (question_count == 0)
280 /* FIXME: the Sun version uses for host name lookup an additional
281 parameter for pointing to h_errno. this is missing here.
282 OSF/1 has a per-thread h_errno variable. */
283 if (header_pointer->aa != 0)
285 __set_h_errno (HOST_NOT_FOUND);
286 return NSS_STATUS_NOTFOUND;
288 else
290 __set_h_errno (TRY_AGAIN);
291 return NSS_STATUS_TRYAGAIN;
295 /* Skip the question part. */
296 while (question_count-- > 0)
298 int n = __dn_skipname (cp, end_of_message);
299 if (n < 0 || end_of_message - (cp + n) < QFIXEDSZ)
301 __set_h_errno (NO_RECOVERY);
302 return NSS_STATUS_UNAVAIL;
304 cp += n + QFIXEDSZ;
307 alias_pointer = result->n_aliases = &net_data->aliases[0];
308 *alias_pointer = NULL;
309 have_answer = 0;
310 ans = NULL;
312 while (--answer_count >= 0 && cp < end_of_message)
314 int n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
315 int type, class;
317 n = __ns_name_unpack (answer->buf, end_of_message, cp,
318 packtmp, sizeof packtmp);
319 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
321 if (errno == EMSGSIZE)
323 errno = ERANGE;
324 return NSS_STATUS_TRYAGAIN;
327 n = -1;
330 if (n > 0 && bp[0] == '.')
331 bp[0] = '\0';
333 if (n < 0 || res_dnok (bp) == 0)
334 break;
335 cp += n;
336 ans = strdupa (bp);
337 GETSHORT (type, cp);
338 GETSHORT (class, cp);
339 cp += INT32SZ; /* TTL */
340 GETSHORT (n, cp);
342 if (class == C_IN && type == T_PTR)
344 n = __ns_name_unpack (answer->buf, end_of_message, cp,
345 packtmp, sizeof packtmp);
346 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
348 if (errno == EMSGSIZE)
350 errno = ERANGE;
351 return NSS_STATUS_TRYAGAIN;
354 n = -1;
357 if (n < 0 || !res_hnok (bp))
359 /* XXX What does this mean? The original form from bind
360 returns NULL. Incrementing cp has no effect in any case.
361 What should I return here. ??? */
362 cp += n;
363 return NSS_STATUS_UNAVAIL;
365 cp += n;
366 if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES])
368 *alias_pointer++ = bp;
369 n = strlen (bp) + 1;
370 bp += n;
371 linebuflen -= n;
372 result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC;
373 ++have_answer;
378 if (have_answer)
380 *alias_pointer = NULL;
381 switch (net_i)
383 case BYADDR:
384 result->n_name = *result->n_aliases++;
385 result->n_net = 0L;
386 return NSS_STATUS_SUCCESS;
388 case BYNAME:
390 char **ap = result->n_aliases++;
391 while (*ap != NULL)
393 /* Check each alias name for being of the forms:
394 4.3.2.1.in-addr.arpa = net 1.2.3.4
395 3.2.1.in-addr.arpa = net 0.1.2.3
396 2.1.in-addr.arpa = net 0.0.1.2
397 1.in-addr.arpa = net 0.0.0.1
399 uint32_t val = 0; /* Accumulator for n_net value. */
400 unsigned int shift = 0; /* Which part we are parsing now. */
401 const char *p = *ap; /* Consuming the string. */
404 /* Match the leading 0 or 0[xX] base indicator. */
405 unsigned int base = 10;
406 if (*p == '0' && p[1] != '.')
408 base = 8;
409 ++p;
410 if (*p == 'x' || *p == 'X')
412 base = 16;
413 ++p;
414 if (*p == '.')
415 break; /* No digit here. Give up on alias. */
417 if (*p == '\0')
418 break;
421 uint32_t part = 0; /* Accumulates this part's number. */
424 if (isdigit (*p) && (*p - '0' < base))
425 part = (part * base) + (*p - '0');
426 else if (base == 16 && isxdigit (*p))
427 part = (part << 4) + 10 + (tolower (*p) - 'a');
428 ++p;
429 } while (*p != '\0' && *p != '.');
431 if (*p != '.')
432 break; /* Bad form. Give up on this name. */
434 /* Install this as the next more significant byte. */
435 val |= part << shift;
436 shift += 8;
437 ++p;
439 /* If we are out of digits now, there are two cases:
440 1. We are done with digits and now see "in-addr.arpa".
441 2. This is not the droid we are looking for. */
442 if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa"))
444 result->n_net = val;
445 return NSS_STATUS_SUCCESS;
448 /* Keep going when we have seen fewer than 4 parts. */
449 } while (shift < 32);
452 break;
456 __set_h_errno (TRY_AGAIN);
457 return NSS_STATUS_TRYAGAIN;