(libc): Add GLIBC_2.3.1. (libpthread): Add GLIBC_2.3.1.
[glibc.git] / resolv / nss_dns / dns-network.c
blob774c868d2f6d3eaf7d8008b89e922d9781294ea5
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
59 #include <ctype.h>
60 #include <errno.h>
61 #include <netdb.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
66 #include "nsswitch.h"
67 #include <arpa/inet.h>
69 /* Maximum number of aliases we allow. */
70 #define MAX_NR_ALIASES 48
73 #if PACKETSZ > 65536
74 # define MAXPACKET PACKETSZ
75 #else
76 # define MAXPACKET 65536
77 #endif
80 typedef enum
82 BYADDR,
83 BYNAME
84 } lookup_method;
87 /* We need this time later. */
88 typedef union querybuf
90 HEADER hdr;
91 u_char buf[MAXPACKET];
92 } querybuf;
94 /* These functions are defined in res_comp.c. */
95 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
96 extern int __ns_name_ntop __P ((const u_char *, char *, size_t));
97 extern int __ns_name_unpack __P ((const u_char *, const u_char *,
98 const u_char *, u_char *, size_t));
101 /* Prototypes for local functions. */
102 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
103 struct netent *result, char *buffer,
104 size_t buflen, lookup_method net_i);
107 enum nss_status
108 _nss_dns_getnetbyname_r (const char *name, struct netent *result,
109 char *buffer, size_t buflen, int *errnop,
110 int *herrnop)
112 /* Return entry for network with NAME. */
113 querybuf net_buffer;
114 int anslen;
115 char *qbuf;
117 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
118 return NSS_STATUS_UNAVAIL;
120 qbuf = strdupa (name);
121 anslen = res_nsearch (&_res, qbuf, C_IN, T_PTR, (u_char *) &net_buffer,
122 sizeof (querybuf));
123 if (anslen < 0)
125 /* Nothing found. */
126 *errnop = errno;
127 return (errno == ECONNREFUSED
128 || errno == EPFNOSUPPORT
129 || errno == EAFNOSUPPORT)
130 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
133 return getanswer_r (&net_buffer, anslen, result, buffer, buflen, BYNAME);
137 enum nss_status
138 _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
139 char *buffer, size_t buflen, int *errnop,
140 int *herrnop)
142 /* Return entry for network with NAME. */
143 enum nss_status status;
144 querybuf net_buffer;
145 unsigned int net_bytes[4];
146 char qbuf[MAXDNAME];
147 int cnt, anslen;
148 u_int32_t net2;
149 int olderr = errno;
151 /* No net address lookup for IPv6 yet. */
152 if (type != AF_INET)
153 return NSS_STATUS_UNAVAIL;
155 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
156 return NSS_STATUS_UNAVAIL;
158 net2 = (u_int32_t) net;
159 for (cnt = 4; net2 != 0; net2 >>= 8)
160 net_bytes[--cnt] = net2 & 0xff;
162 switch (cnt)
164 case 3:
165 /* Class A network. */
166 sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]);
167 break;
168 case 2:
169 /* Class B network. */
170 sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]);
171 break;
172 case 1:
173 /* Class C network. */
174 sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
175 net_bytes[1]);
176 break;
177 case 0:
178 /* Class D - E network. */
179 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
180 net_bytes[1], net_bytes[0]);
181 break;
184 anslen = res_nquery (&_res, qbuf, C_IN, T_PTR, (u_char *) &net_buffer,
185 sizeof (querybuf));
186 if (anslen < 0)
188 /* Nothing found. */
189 int err = errno;
190 __set_errno (olderr);
191 return (err == ECONNREFUSED
192 || err == EPFNOSUPPORT
193 || err == EAFNOSUPPORT)
194 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
197 status = getanswer_r (&net_buffer, anslen, result, buffer, buflen, BYADDR);
198 if (status == NSS_STATUS_SUCCESS)
200 /* Strip trailing zeros. */
201 unsigned int u_net = net; /* Maybe net should be unsigned? */
203 while ((u_net & 0xff) == 0 && u_net != 0)
204 u_net >>= 8;
205 result->n_net = u_net;
208 return status;
212 #undef offsetof
213 #define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member)
215 static enum nss_status
216 getanswer_r (const querybuf *answer, int anslen, struct netent *result,
217 char *buffer, size_t buflen, lookup_method net_i)
220 * Find first satisfactory answer
222 * answer --> +------------+ ( MESSAGE )
223 * | Header |
224 * +------------+
225 * | Question | the question for the name server
226 * +------------+
227 * | Answer | RRs answering the question
228 * +------------+
229 * | Authority | RRs pointing toward an authority
230 * | Additional | RRs holding additional information
231 * +------------+
233 struct net_data
235 char *aliases[MAX_NR_ALIASES];
236 char linebuffer[0];
237 } *net_data = (struct net_data *) buffer;
238 int linebuflen = buflen - offsetof (struct net_data, linebuffer);
239 const char *end_of_message = &answer->buf[anslen];
240 const HEADER *header_pointer = &answer->hdr;
241 /* #/records in the answer section. */
242 int answer_count = ntohs (header_pointer->ancount);
243 /* #/entries in the question section. */
244 int question_count = ntohs (header_pointer->qdcount);
245 char *bp = net_data->linebuffer;
246 const char *cp = &answer->buf[HFIXEDSZ];
247 char **alias_pointer;
248 int have_answer;
249 char *ans;
250 u_char packtmp[NS_MAXCDNAME];
252 if (question_count == 0)
254 /* FIXME: the Sun version uses for host name lookup an additional
255 parameter for pointing to h_errno. this is missing here.
256 OSF/1 has a per-thread h_errno variable. */
257 if (header_pointer->aa != 0)
259 __set_h_errno (HOST_NOT_FOUND);
260 return NSS_STATUS_NOTFOUND;
262 else
264 __set_h_errno (TRY_AGAIN);
265 return NSS_STATUS_TRYAGAIN;
269 /* Skip the question part. */
270 while (question_count-- > 0)
271 cp += __dn_skipname (cp, end_of_message) + QFIXEDSZ;
273 alias_pointer = result->n_aliases = &net_data->aliases[0];
274 *alias_pointer = NULL;
275 have_answer = 0;
276 ans = NULL;
278 while (--answer_count >= 0 && cp < end_of_message)
280 int n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
281 int type, class;
283 n = __ns_name_unpack (answer->buf, end_of_message, cp,
284 packtmp, sizeof packtmp);
285 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
287 if (errno == EMSGSIZE)
289 errno = ERANGE;
290 return NSS_STATUS_TRYAGAIN;
293 n = -1;
296 if (n > 0 && bp[0] == '.')
297 bp[0] = '\0';
299 if (n < 0 || res_dnok (bp) == 0)
300 break;
301 cp += n;
302 ans = strdupa (bp);
303 GETSHORT (type, cp);
304 GETSHORT (class, cp);
305 cp += INT32SZ; /* TTL */
306 GETSHORT (n, cp);
308 if (class == C_IN && type == T_PTR)
310 n = __ns_name_unpack (answer->buf, end_of_message, cp,
311 packtmp, sizeof packtmp);
312 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
314 if (errno == EMSGSIZE)
316 errno = ERANGE;
317 return NSS_STATUS_TRYAGAIN;
320 n = -1;
323 if (n < 0 || !res_hnok (bp))
325 /* XXX What does this mean? The original form from bind
326 returns NULL. Incrementing cp has no effect in any case.
327 What should I return here. ??? */
328 cp += n;
329 return NSS_STATUS_UNAVAIL;
331 cp += n;
332 *alias_pointer++ = bp;
333 n = strlen (bp) + 1;
334 bp += n;
335 linebuflen -= n;
336 result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC;
337 ++have_answer;
341 if (have_answer)
343 char *tmp;
344 int len;
345 char *in, *cp, *rp, *wp;
346 int cnt, first_flag;
348 *alias_pointer = NULL;
349 switch (net_i)
351 case BYADDR:
352 result->n_name = result->n_aliases[0];
353 result->n_net = 0L;
354 break;
355 case BYNAME:
356 len = strlen (result->n_aliases[0]);
357 tmp = (char *) alloca (len + 1);
358 tmp[len] = 0;
359 wp = &tmp[len - 1];
361 rp = in = result->n_aliases[0];
362 result->n_name = ans;
364 first_flag = 1;
365 for (cnt = 0; cnt < 4; ++cnt)
367 char *startp;
369 startp = rp;
370 while (*rp != '.')
371 ++rp;
372 if (rp - startp > 1 || *startp != '0' || !first_flag)
374 first_flag = 0;
375 if (cnt > 0)
376 *wp-- = '.';
377 cp = rp;
378 while (cp > startp)
379 *wp-- = *--cp;
381 in = rp + 1;
384 result->n_net = inet_network (wp);
385 break;
388 ++result->n_aliases;
389 return NSS_STATUS_SUCCESS;
392 __set_h_errno (TRY_AGAIN);
393 return NSS_STATUS_TRYAGAIN;