Updated to fedora-glibc-20050627T0850
[glibc.git] / glibc-compat / nss_dns / dns-network.c
blobb6c7a4fdc9f742445da629438f33bb42ce62c982
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Parts of this file are plain copies of the file `getnetnamadr.c' from
21 the bind package and it has the following copyright. */
23 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
24 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
26 * Permission to use, copy, modify, and distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
31 * Copyright (c) 1983, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
63 #include <ctype.h>
64 #include <errno.h>
65 #include <glibc-compat/include/netdb.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
70 #include "nsswitch.h"
71 #include <arpa/inet.h>
73 /* Maximum number of aliases we allow. */
74 #define MAX_NR_ALIASES 48
77 #if PACKETSZ > 65536
78 #define MAXPACKET PACKETSZ
79 #else
80 #define MAXPACKET 65536
81 #endif
84 typedef enum
86 BYADDR,
87 BYNAME
88 } lookup_method;
91 /* We need this time later. */
92 typedef union querybuf
94 HEADER hdr;
95 u_char buf[MAXPACKET];
96 } querybuf;
99 /* Prototypes for local functions. */
100 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
101 struct netent *result, char *buffer,
102 size_t buflen, lookup_method net_i);
105 enum nss_status
106 _nss_dns_getnetbyname_r (const char *name, struct netent *result,
107 char *buffer, size_t buflen)
109 /* Return entry for network with NAME. */
110 union
112 querybuf *buf;
113 u_char *ptr;
114 } net_buffer;
115 querybuf *orig_net_buffer;
116 int anslen;
117 char *qbuf;
118 enum nss_status status;
120 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
121 return NSS_STATUS_UNAVAIL;
123 qbuf = strdupa (name);
125 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
127 anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
128 1024, &net_buffer.ptr);
129 if (anslen < 0)
131 if (net_buffer.buf != orig_net_buffer)
132 free (net_buffer.buf);
133 /* Nothing found. */
134 return (errno == ECONNREFUSED
135 || errno == EPFNOSUPPORT
136 || errno == EAFNOSUPPORT)
137 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
140 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, BYNAME);
141 if (net_buffer.buf != orig_net_buffer)
142 free (net_buffer.buf);
143 return status;
147 enum nss_status
148 _nss_dns_getnetbyaddr_r (long net, int type, struct netent *result,
149 char *buffer, size_t buflen)
151 /* Return entry for network with NAME. */
152 enum nss_status status;
153 union
155 querybuf *buf;
156 u_char *ptr;
157 } net_buffer;
158 querybuf *orig_net_buffer;
159 unsigned int net_bytes[4];
160 char qbuf[MAXDNAME];
161 int cnt, anslen;
162 u_int32_t net2;
164 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
165 return NSS_STATUS_UNAVAIL;
167 /* No net address lookup for IPv6 yet. */
168 if (type != AF_INET)
169 return NSS_STATUS_UNAVAIL;
171 net2 = (u_int32_t) net;
172 for (cnt = 4; net2 != 0; net2 >>= 8)
173 net_bytes[--cnt] = net2 & 0xff;
175 switch (cnt)
177 case 3:
178 /* Class A network. */
179 sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]);
180 break;
181 case 2:
182 /* Class B network. */
183 sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]);
184 break;
185 case 1:
186 /* Class C network. */
187 sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
188 net_bytes[1]);
189 break;
190 case 0:
191 /* Class D - E network. */
192 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
193 net_bytes[1], net_bytes[0]);
194 break;
197 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
199 anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
200 1024, &net_buffer.ptr);
201 if (anslen < 0)
203 if (net_buffer.buf != orig_net_buffer)
204 free (net_buffer.buf);
205 /* Nothing found. */
206 return (errno == ECONNREFUSED
207 || errno == EPFNOSUPPORT
208 || errno == EAFNOSUPPORT)
209 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
212 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, BYADDR);
213 if (net_buffer.buf != orig_net_buffer)
214 free (net_buffer.buf);
215 if (status == NSS_STATUS_SUCCESS)
217 /* Strip trailing zeros. */
218 unsigned int u_net = net; /* Maybe net should be unsigned? */
220 while ((u_net & 0xff) == 0 && u_net != 0)
221 u_net >>= 8;
222 result->n_net = u_net;
225 return status;
229 #undef offsetof
230 #define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member)
232 static enum nss_status
233 getanswer_r (const querybuf *answer, int anslen, struct netent *result,
234 char *buffer, size_t buflen, lookup_method net_i)
237 * Find first satisfactory answer
239 * answer --> +------------+ ( MESSAGE )
240 * | Header |
241 * +------------+
242 * | Question | the question for the name server
243 * +------------+
244 * | Answer | RRs answering the question
245 * +------------+
246 * | Authority | RRs pointing toward an authority
247 * | Additional | RRs holding additional information
248 * +------------+
250 struct net_data
252 char *aliases[MAX_NR_ALIASES];
253 char linebuffer[0];
254 } *net_data = (struct net_data *) buffer;
255 int linebuflen = buflen - offsetof (struct net_data, linebuffer);
256 const char *end_of_message = &answer->buf[anslen];
257 const HEADER *header_pointer = &answer->hdr;
258 /* #/records in the answer section. */
259 int answer_count = ntohs (header_pointer->ancount);
260 /* #/entries in the question section. */
261 int question_count = ntohs (header_pointer->qdcount);
262 char *bp = net_data->linebuffer;
263 const char *cp = &answer->buf[HFIXEDSZ];
264 char **alias_pointer;
265 int have_answer;
266 char *ans;
268 if (question_count == 0)
270 /* FIXME: the Sun version uses for host name lookup an additional
271 parameter for pointing to h_errno. this is missing here.
272 OSF/1 has a per-thread h_errno variable. */
273 if (header_pointer->aa != 0)
275 __set_h_errno (HOST_NOT_FOUND);
276 return NSS_STATUS_NOTFOUND;
278 else
280 __set_h_errno (TRY_AGAIN);
281 return NSS_STATUS_TRYAGAIN;
285 /* Skip the question part. */
286 while (question_count-- > 0)
288 int n = __dn_skipname (cp, end_of_message);
289 if (n < 0 || end_of_message - (cp + n) < QFIXEDSZ)
291 __set_h_errno (NO_RECOVERY);
292 return NSS_STATUS_UNAVAIL;
294 cp += n + QFIXEDSZ;
297 alias_pointer = result->n_aliases = &net_data->aliases[0];
298 *alias_pointer = NULL;
299 have_answer = 0;
300 ans = NULL;
302 while (--answer_count >= 0 && cp < end_of_message)
304 int n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
305 int type, class;
307 if (n < 0 || res_dnok (bp) == 0)
308 break;
309 cp += n;
310 ans = strdupa (bp);
311 GETSHORT (type, cp);
312 GETSHORT (class, cp);
313 cp += INT32SZ; /* TTL */
314 GETSHORT (n, cp);
316 if (class == C_IN && type == T_PTR)
318 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
319 if (n < 0 || !res_hnok (bp))
321 /* XXX What does this mean? The original form from bind
322 returns NULL. Incrementing cp has no effect in any case.
323 What should I return here. ??? */
324 cp += n;
325 return NSS_STATUS_UNAVAIL;
327 cp += n;
328 if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES])
330 *alias_pointer++ = bp;
331 n = strlen (bp) + 1;
332 bp += n;
333 linebuflen -= n;
334 result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC;
335 ++have_answer;
340 if (have_answer)
342 *alias_pointer = NULL;
343 switch (net_i)
345 case BYADDR:
346 result->n_name = *result->n_aliases++;
347 result->n_net = 0L;
348 return NSS_STATUS_SUCCESS;
350 case BYNAME:
352 char **ap = result->n_aliases++;
353 while (*ap != NULL)
355 /* Check each alias name for being of the forms:
356 4.3.2.1.in-addr.arpa = net 1.2.3.4
357 3.2.1.in-addr.arpa = net 0.1.2.3
358 2.1.in-addr.arpa = net 0.0.1.2
359 1.in-addr.arpa = net 0.0.0.1
361 uint32_t val = 0; /* Accumulator for n_net value. */
362 unsigned int shift = 0; /* Which part we are parsing now. */
363 const char *p = *ap; /* Consuming the string. */
366 /* Match the leading 0 or 0[xX] base indicator. */
367 unsigned int base = 10;
368 if (*p == '0' && p[1] != '.')
370 base = 8;
371 ++p;
372 if (*p == 'x' || *p == 'X')
374 base = 16;
375 ++p;
376 if (*p == '.')
377 break; /* No digit here. Give up on alias. */
379 if (*p == '\0')
380 break;
383 uint32_t part = 0; /* Accumulates this part's number. */
386 if (isdigit (*p) && (*p - '0' < base))
387 part = (part * base) + (*p - '0');
388 else if (base == 16 && isxdigit (*p))
389 part = (part << 4) + 10 + (tolower (*p) - 'a');
390 ++p;
391 } while (*p != '\0' && *p != '.');
393 if (*p != '.')
394 break; /* Bad form. Give up on this name. */
396 /* Install this as the next more significant byte. */
397 val |= part << shift;
398 shift += 8;
399 ++p;
401 /* If we are out of digits now, there are two cases:
402 1. We are done with digits and now see "in-addr.arpa".
403 2. This is not the droid we are looking for. */
404 if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa"))
406 result->n_net = val;
407 return NSS_STATUS_SUCCESS;
410 /* Keep going when we have seen fewer than 4 parts. */
411 } while (shift < 32);
414 break;
418 __set_h_errno (TRY_AGAIN);
419 return NSS_STATUS_TRYAGAIN;