1 /* Copyright (C) 1996-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Parts of this file are plain copies of the file `getnetnamadr.c' from
19 the bind package and it has the following copyright. */
21 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
22 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
24 * Permission to use, copy, modify, and distribute this software for any
25 * purpose with or without fee is hereby granted, provided that the above
26 * copyright notice and this permission notice appear in all copies.
29 * Copyright (c) 1983, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
70 #include <resolv/resolv-internal.h>
71 #include <resolv/resolv_context.h>
73 /* Maximum number of aliases we allow. */
74 #define MAX_NR_ALIASES 48
78 # define MAXPACKET PACKETSZ
80 # define MAXPACKET 65536
91 /* We need this time later. */
92 typedef union querybuf
95 u_char buf
[MAXPACKET
];
98 /* Prototypes for local functions. */
99 static enum nss_status
getanswer_r (const querybuf
*answer
, int anslen
,
100 struct netent
*result
, char *buffer
,
101 size_t buflen
, int *errnop
, int *h_errnop
,
102 lookup_method net_i
);
106 _nss_dns_getnetbyname_r (const char *name
, struct netent
*result
,
107 char *buffer
, size_t buflen
, int *errnop
,
110 /* Return entry for network with NAME. */
116 querybuf
*orig_net_buffer
;
118 enum nss_status status
;
120 struct resolv_context
*ctx
= __resolv_context_get ();
124 *herrnop
= NETDB_INTERNAL
;
125 return NSS_STATUS_UNAVAIL
;
128 net_buffer
.buf
= orig_net_buffer
= (querybuf
*) alloca (1024);
130 anslen
= __res_context_search
131 (ctx
, name
, C_IN
, T_PTR
, net_buffer
.buf
->buf
,
132 1024, &net_buffer
.ptr
, NULL
, NULL
, NULL
, NULL
);
137 if (net_buffer
.buf
!= orig_net_buffer
)
138 free (net_buffer
.buf
);
139 __resolv_context_put (ctx
);
140 return (errno
== ECONNREFUSED
141 || errno
== EPFNOSUPPORT
142 || errno
== EAFNOSUPPORT
)
143 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
146 status
= getanswer_r (net_buffer
.buf
, anslen
, result
, buffer
, buflen
,
147 errnop
, herrnop
, BYNAME
);
148 if (net_buffer
.buf
!= orig_net_buffer
)
149 free (net_buffer
.buf
);
150 __resolv_context_put (ctx
);
153 libc_hidden_def (_nss_dns_getnetbyname_r
)
156 _nss_dns_getnetbyaddr_r (uint32_t net
, int type
, struct netent
*result
,
157 char *buffer
, size_t buflen
, int *errnop
,
160 /* Return entry for network with NAME. */
161 enum nss_status status
;
167 querybuf
*orig_net_buffer
;
168 unsigned int net_bytes
[4];
174 /* No net address lookup for IPv6 yet. */
176 return NSS_STATUS_UNAVAIL
;
178 struct resolv_context
*ctx
= __resolv_context_get ();
182 *herrnop
= NETDB_INTERNAL
;
183 return NSS_STATUS_UNAVAIL
;
186 net2
= (uint32_t) net
;
187 for (cnt
= 4; net2
!= 0; net2
>>= 8)
188 net_bytes
[--cnt
] = net2
& 0xff;
193 /* Class A network. */
194 sprintf (qbuf
, "0.0.0.%u.in-addr.arpa", net_bytes
[3]);
197 /* Class B network. */
198 sprintf (qbuf
, "0.0.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2]);
201 /* Class C network. */
202 sprintf (qbuf
, "0.%u.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2],
206 /* Class D - E network. */
207 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2],
208 net_bytes
[1], net_bytes
[0]);
212 net_buffer
.buf
= orig_net_buffer
= (querybuf
*) alloca (1024);
214 anslen
= __res_context_query (ctx
, qbuf
, C_IN
, T_PTR
, net_buffer
.buf
->buf
,
215 1024, &net_buffer
.ptr
, NULL
, NULL
, NULL
, NULL
);
220 __set_errno (olderr
);
221 if (net_buffer
.buf
!= orig_net_buffer
)
222 free (net_buffer
.buf
);
223 __resolv_context_put (ctx
);
224 return (err
== ECONNREFUSED
225 || err
== EPFNOSUPPORT
226 || err
== EAFNOSUPPORT
)
227 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
230 status
= getanswer_r (net_buffer
.buf
, anslen
, result
, buffer
, buflen
,
231 errnop
, herrnop
, BYADDR
);
232 if (net_buffer
.buf
!= orig_net_buffer
)
233 free (net_buffer
.buf
);
234 if (status
== NSS_STATUS_SUCCESS
)
236 /* Strip trailing zeros. */
237 unsigned int u_net
= net
; /* Maybe net should be unsigned? */
239 while ((u_net
& 0xff) == 0 && u_net
!= 0)
241 result
->n_net
= u_net
;
244 __resolv_context_put (ctx
);
247 libc_hidden_def (_nss_dns_getnetbyaddr_r
)
249 static enum nss_status
250 getanswer_r (const querybuf
*answer
, int anslen
, struct netent
*result
,
251 char *buffer
, size_t buflen
, int *errnop
, int *h_errnop
,
255 * Find first satisfactory answer
257 * answer --> +------------+ ( MESSAGE )
260 * | Question | the question for the name server
262 * | Answer | RRs answering the question
264 * | Authority | RRs pointing toward an authority
265 * | Additional | RRs holding additional information
270 char *aliases
[MAX_NR_ALIASES
];
274 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct net_data
);
277 if (__glibc_unlikely (buflen
< sizeof (*net_data
) + pad
))
279 /* The buffer is too small. */
282 *h_errnop
= NETDB_INTERNAL
;
283 return NSS_STATUS_TRYAGAIN
;
287 net_data
= (struct net_data
*) buffer
;
288 int linebuflen
= buflen
- offsetof (struct net_data
, linebuffer
);
289 if (buflen
- offsetof (struct net_data
, linebuffer
) != linebuflen
)
290 linebuflen
= INT_MAX
;
291 const unsigned char *end_of_message
= &answer
->buf
[anslen
];
292 const HEADER
*header_pointer
= &answer
->hdr
;
293 /* #/records in the answer section. */
294 int answer_count
= ntohs (header_pointer
->ancount
);
295 /* #/entries in the question section. */
296 int question_count
= ntohs (header_pointer
->qdcount
);
297 char *bp
= net_data
->linebuffer
;
298 const unsigned char *cp
= &answer
->buf
[HFIXEDSZ
];
299 char **alias_pointer
;
301 u_char packtmp
[NS_MAXCDNAME
];
303 if (question_count
== 0)
305 /* FIXME: the Sun version uses for host name lookup an additional
306 parameter for pointing to h_errno. this is missing here.
307 OSF/1 has a per-thread h_errno variable. */
308 if (header_pointer
->aa
!= 0)
310 __set_h_errno (HOST_NOT_FOUND
);
311 return NSS_STATUS_NOTFOUND
;
315 __set_h_errno (TRY_AGAIN
);
316 return NSS_STATUS_TRYAGAIN
;
320 /* Skip the question part. */
321 while (question_count
-- > 0)
323 int n
= __libc_dn_skipname (cp
, end_of_message
);
324 if (n
< 0 || end_of_message
- (cp
+ n
) < QFIXEDSZ
)
326 __set_h_errno (NO_RECOVERY
);
327 return NSS_STATUS_UNAVAIL
;
332 alias_pointer
= result
->n_aliases
= &net_data
->aliases
[0];
333 *alias_pointer
= NULL
;
336 while (--answer_count
>= 0 && cp
< end_of_message
)
338 int n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
339 packtmp
, sizeof packtmp
);
340 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
342 if (errno
== EMSGSIZE
)
348 if (n
< 0 || __libc_res_dnok (bp
) == 0)
352 if (end_of_message
- cp
< 10)
354 __set_h_errno (NO_RECOVERY
);
355 return NSS_STATUS_UNAVAIL
;
360 GETSHORT (class, cp
);
361 cp
+= INT32SZ
; /* TTL */
363 GETSHORT (rdatalen
, cp
);
364 if (end_of_message
- cp
< rdatalen
)
366 __set_h_errno (NO_RECOVERY
);
367 return NSS_STATUS_UNAVAIL
;
370 if (class == C_IN
&& type
== T_PTR
)
372 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
373 packtmp
, sizeof packtmp
);
374 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
376 if (errno
== EMSGSIZE
)
382 if (n
< 0 || !__libc_res_hnok (bp
))
384 /* XXX What does this mean? The original form from bind
385 returns NULL. Incrementing cp has no effect in any case.
386 What should I return here. ??? */
388 return NSS_STATUS_UNAVAIL
;
391 if (alias_pointer
+ 2 < &net_data
->aliases
[MAX_NR_ALIASES
])
393 *alias_pointer
++ = bp
;
397 result
->n_addrtype
= class == C_IN
? AF_INET
: AF_UNSPEC
;
402 /* Skip over unknown record data. */
408 *alias_pointer
= NULL
;
412 result
->n_name
= *result
->n_aliases
++;
414 return NSS_STATUS_SUCCESS
;
419 for (ap
= result
->n_aliases
; *ap
!= NULL
; ++ap
)
421 /* Check each alias name for being of the forms:
422 4.3.2.1.in-addr.arpa = net 1.2.3.4
423 3.2.1.in-addr.arpa = net 0.1.2.3
424 2.1.in-addr.arpa = net 0.0.1.2
425 1.in-addr.arpa = net 0.0.0.1
427 uint32_t val
= 0; /* Accumulator for n_net value. */
428 unsigned int shift
= 0; /* Which part we are parsing now. */
429 const char *p
= *ap
; /* Consuming the string. */
432 /* Match the leading 0 or 0[xX] base indicator. */
433 unsigned int base
= 10;
434 if (*p
== '0' && p
[1] != '.')
438 if (*p
== 'x' || *p
== 'X')
443 break; /* No digit here. Give up on alias. */
449 uint32_t part
= 0; /* Accumulates this part's number. */
452 if (isdigit (*p
) && (*p
- '0' < base
))
453 part
= (part
* base
) + (*p
- '0');
454 else if (base
== 16 && isxdigit (*p
))
455 part
= (part
<< 4) + 10 + (tolower (*p
) - 'a');
457 } while (*p
!= '\0' && *p
!= '.');
460 break; /* Bad form. Give up on this name. */
462 /* Install this as the next more significant byte. */
463 val
|= part
<< shift
;
467 /* If we are out of digits now, there are two cases:
468 1. We are done with digits and now see "in-addr.arpa".
469 2. This is not the droid we are looking for. */
470 if (!isdigit (*p
) && !__strcasecmp (p
, "in-addr.arpa"))
473 return NSS_STATUS_SUCCESS
;
476 /* Keep going when we have seen fewer than 4 parts. */
477 } while (shift
< 32);
484 __set_h_errno (TRY_AGAIN
);
485 return NSS_STATUS_TRYAGAIN
;