2009-05-16 12:17:08 +08:00
|
|
|
/* Copyright (C) 1998-2005, 2006, 2007, 2008, 2009
|
|
|
|
Free Software Foundation, Inc.
|
1998-10-18 23:16:22 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/nameser.h>
|
2004-06-06 14:06:02 +08:00
|
|
|
#include <not-cancel.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
|
1998-10-19 08:40:00 +08:00
|
|
|
#include "nscd-client.h"
|
1998-10-18 23:16:22 +08:00
|
|
|
#include "nscd_proto.h"
|
|
|
|
|
|
|
|
int __nss_not_use_nscd_hosts;
|
|
|
|
|
|
|
|
static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
|
|
|
|
struct hostent *resultbuf, char *buffer,
|
2004-02-27 08:55:39 +08:00
|
|
|
size_t buflen, struct hostent **result,
|
|
|
|
int *h_errnop) internal_function;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
__nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
|
2004-02-27 08:55:39 +08:00
|
|
|
char *buffer, size_t buflen, struct hostent **result,
|
|
|
|
int *h_errnop)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
request_type reqtype;
|
|
|
|
|
|
|
|
reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
|
|
|
|
|
|
|
|
return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
|
2004-02-27 08:55:39 +08:00
|
|
|
buffer, buflen, result, h_errnop);
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
__nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
|
2004-02-27 08:55:39 +08:00
|
|
|
char *buffer, size_t buflen, struct hostent **result,
|
|
|
|
int *h_errnop)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
request_type reqtype;
|
|
|
|
|
|
|
|
reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
|
|
|
|
|
|
|
|
return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
|
2004-02-27 08:55:39 +08:00
|
|
|
buffer, buflen, result, h_errnop);
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
Update.
* conform/conformtest.pl (@headers): Add sys/utsname.h, sys/un.h,
sys/socket.h, spawn.h, netinet/tcp.h, netinet/in.h, net/if.h, and
arpa/inet.h.
* conform/data/fcntl.h-data: Add F_GETOWN, F_SETOWN,
SEEK_SET, SEEK_CUR, SEEK_END, S_IFMT, S_IFBLK, S_IFCHR, S_IFIFO,
S_IFREG, S_IFDIR, S_IFLNK, S_IFSOCK, S_IRWXU, S_IRUSR S_IWUSR,
S_IXUSR, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH,
S_IWOTH, S_IXOTH, S_ISUID, S_ISGID, S_ISVTX, POSIX_FADV_NORMAL,
POSIX_FADV_SEQUENTIAL, POSIX_FADV_RANDOM, POSIX_FADV_WILLNEED,
POSIX_FADV_DONTNEED, POSIX_FADV_NOREUSE, posix_fadvise,
posix_fallocate, and posix_madvise.
* conform/data/netdb.h-data: New file.
* conform/data/pthread.h-data: Add pthread_barrier_t,
pthread_barrierattr_t, pthread_barrier_destroy,
pthread_barrier_init, pthread_barrier_wait,
pthread_barrierattr_destroy, pthread_barrierattr_getpshared,
pthread_barrierattr_init, pthread_barrierattr_setpshared,
pthread_condattr_getclock, pthread_condattr_setclock,
pthread_getcpuclockid, pthread_mutex_timedlock,
pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock,
pthread_sigmask, pthread_spin_destroy, pthread_spin_init,
pthread_spin_lock, pthread_spin_trylock, and pthread_spin_unlock.
* conform/data/sched.h-data: Add sched_ss_low_priority,
sched_ss_repl_period, sched_ss_init_budget, sched_ss_max_repl, and
SCHED_SPORADIC.
* conform/data/semaphore.h-data: Add sem_timedwait.
* conform/data/signal.h-data: Add SIGIO.
* conform/data/spawn.h-data: New file.
* conform/data/stdlib.h-data: Add posix_memalign, setenv, and unsetenv.
* conform/data/time.h-data: Add CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID, clock_getcpuclockid, and clock_nanosleep.
Fix typo in gmtime_r definition.
* conform/data/unistd.h-data: Add _POSIX_ADVISORY_INFO,
_POSIX_ASYNCHRONOUS_IO, _POSIX_BARRIERS, _POSIX_MONOTONIC_CLOCK,
_POSIX_READER_WRITER_LOCKS, _POSIX_SHELL, _POSIX_SPAWN,
_POSIX_SPIN_LOCKS, _POSIX_SPORADIC_SERVER, _POSIX_THREAD_CPUTIME,
_POSIX_TYPED_MEMORY_OBJECTS, _POSIX_THREAD_SPORADIC_SERVER,
_XOPEN_STREAMS, _POSIX_TIMEOUTS, _POSIX2_PBS,
_POSIX2_PBS_ACCOUNTING, _POSIX2_PBS_CHECKPOINT,
_POSIX2_PBS_LOCATE, _POSIX2_PBS_MESSAGE, _POSIX2_PBS_TRACK,
_SC_2_PBS, _SC_2_PBS_ACCOUNTING, _SC_2_PBS_CHECKPOINT,
_SC_2_PBS_LOCATE, _SC_2_PBS_MESSAGE, _SC_2_PBS_TRACK,
_SC_BARRIERS, _SC_BASE, _SC_CLOCK_SELECTION, _SC_DEVICE_IO,
_SC_DEVICE_SPECIFIC, _SC_DEVICE_SPECIFIC_R, _SC_FD_MGMT, _SC_FIFO,
_SC_FILE_ATTRIBUTES, _SC_FILE_LOCKING, _SC_FILE_SYSTEM,
_SC_MEMORY_PROTECTION, _SC_MESSAGE_PASSING, _SC_MONOTONIC_CLOCK,
_SC_MESSAGE_PASSING, _SC_MONOTONIC_CLOCK, _SC_MULTIPLE_PROCESS,
_SC_NETWORKING, _SC_PIPE, _SC_READER_WRITER_LOCKS, _SC_REGEXP,
_SC_SHELL, _SC_SIGNALS, _SC_SINGLE_PROCESS, _SC_SPIN_LOCKS,
_SC_TYPED_MEMORY_OBJECTS, _SC_USER_GROUPS, _SC_USER_GROUPS_R,
_SC_STREAMS, _PC_REC_INCR_XFER_SIZE, _PC_REC_MAX_XFER_SIZE,
_PC_REC_MIN_XFER_SIZE, _PC_REC_XFER_ALIGN, setegid, seteuid. Fix
typo in off_t definition.
* conform/data/net/if.h-data: New file.
* conform/data/netinet/in.h-data: New file.
* conform/data/netinet/tcp.h-data: New file.
* conform/data/sys/mman.h-data: Add POSIX_MADV_NORMAL,
POSIX_MADV_SEQUENTIAL, POSIX_MADV_RANDOM, POSIX_MADV_WILLNEED,
POSIX_MADV_DONTNEED, POSIX_TYPED_MEM_ALLOCATE,
POSIX_TYPED_MEM_ALLOCATE_CONTIG, POSIX_TYPED_MEM_MAP_ALLOCATABLE,
posix_typedmem_info, posix_mem_offset, posix_typed_mem_get_info,
and posix_typed_mem_open.
* conform/data/sys/socket.h-data: New file.
* conform/data/sys/stat.h-data: Add S_TYPEISTMO and isfdtype.
* conform/data/sys/types.h-data: Add pthread_barrier_t,
pthread_barrierattr_t, and pthread_spinlock_t.
* conform/data/sys/un.h-data: New file.
* conform/data/sys/utsname.h-data: New file.
* include/netdb.h (__gethostbyaddr_r): Fix types of first and second
parameter.
* inet/gethstbyad.c (gethostbyname): Likewise.
* inet/gethstbyad_t.c (gethostbyname_r): Likewise.
* inet/getipnodebyad.c (getipnodebyad): Likewise.
* nis/nss_nis/nis-hosts.c (_nss_nis_gethostbyaddr_r): Likewise.
* nis/nss_nisplus/nisplus-hosts.c (_nss_nisplus_gethostbyaddr_r):
Likewise.
* nscd/gethstbyad_r.c (gethostbyaddr_r): Likewise.
* nscd/nscd_gethst_r.c (__nscd_gethostbyaddr_r): Likewise.
* nscd/nscd_proto.h (__nscd_gethostbyaddr_r): Likewise.
* nss/nss_files/files-hosts.c (_nss_files_gethostbyaddr_r): Likewise.
* resolv/gethnamaddr.c (gethostbyname): Likewise.
* resolv/netdb.h (struct hostent): Fix type of h_length member.
(gethostbyaddr, getipnodebyaddr, gethostbyaddr_r): Fix types of first
and second parameter.
2000-04-29 Andreas Jaeger <aj@suse.de>
* conform/data/arpa/inet.h-data: New file.
2000-04-30 12:17:35 +08:00
|
|
|
__nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
|
1998-10-18 23:16:22 +08:00
|
|
|
struct hostent *resultbuf, char *buffer, size_t buflen,
|
2004-02-27 08:55:39 +08:00
|
|
|
struct hostent **result, int *h_errnop)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
request_type reqtype;
|
|
|
|
|
|
|
|
if (!((len == INADDRSZ && type == AF_INET)
|
|
|
|
|| (len == IN6ADDRSZ && type == AF_INET6)))
|
|
|
|
/* LEN and TYPE do not match. */
|
1999-06-17 20:33:08 +08:00
|
|
|
return -1;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
|
|
|
|
|
2004-02-27 08:55:39 +08:00
|
|
|
return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
|
1998-10-18 23:16:22 +08:00
|
|
|
h_errnop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-16 04:46:12 +08:00
|
|
|
libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
|
2004-09-08 23:46:42 +08:00
|
|
|
/* Note that we only free the structure if necessary. The memory
|
|
|
|
mapping is not removed since it is not visible to the malloc
|
|
|
|
handling. */
|
2004-11-10 15:54:40 +08:00
|
|
|
libc_freeres_fn (hst_map_free)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-11-10 15:54:40 +08:00
|
|
|
if (__hst_map_handle.mapped != NO_MAPPING)
|
|
|
|
{
|
|
|
|
void *p = __hst_map_handle.mapped;
|
|
|
|
__hst_map_handle.mapped = NO_MAPPING;
|
|
|
|
free (p);
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-30 04:56:13 +08:00
|
|
|
int __nss_have_localdomain attribute_hidden;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
static int
|
1999-02-03 00:27:57 +08:00
|
|
|
internal_function
|
1998-10-18 23:16:22 +08:00
|
|
|
nscd_gethst_r (const char *key, size_t keylen, request_type type,
|
|
|
|
struct hostent *resultbuf, char *buffer, size_t buflen,
|
2004-02-27 08:55:39 +08:00
|
|
|
struct hostent **result, int *h_errnop)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2008-12-30 04:56:13 +08:00
|
|
|
if (__builtin_expect (__nss_have_localdomain >= 0, 0))
|
|
|
|
{
|
|
|
|
if (__nss_have_localdomain == 0)
|
|
|
|
__nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
|
|
|
|
if (__nss_have_localdomain > 0)
|
|
|
|
{
|
|
|
|
__nss_not_use_nscd_hosts = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-30 16:08:40 +08:00
|
|
|
int gc_cycle;
|
|
|
|
int nretries = 0;
|
|
|
|
|
|
|
|
/* If the mapping is available, try to search there instead of
|
|
|
|
communicating with the nscd. */
|
|
|
|
struct mapped_database *mapped;
|
2004-11-10 15:54:40 +08:00
|
|
|
mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
|
|
|
|
&gc_cycle);
|
2004-09-30 16:08:40 +08:00
|
|
|
|
|
|
|
retry:;
|
2004-09-08 23:46:42 +08:00
|
|
|
const char *h_name = NULL;
|
|
|
|
const uint32_t *aliases_len = NULL;
|
|
|
|
const char *addr_list = NULL;
|
|
|
|
size_t addr_list_len = 0;
|
|
|
|
int retval = -1;
|
|
|
|
const char *recend = (const char *) ~UINTMAX_C (0);
|
|
|
|
int sock = -1;
|
2007-01-31 17:14:21 +08:00
|
|
|
hst_response_header hst_resp;
|
2004-09-30 13:16:40 +08:00
|
|
|
if (mapped != NO_MAPPING)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
/* No const qualifier, as it can change during garbage collection. */
|
2009-05-16 12:17:08 +08:00
|
|
|
struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
|
|
|
|
sizeof hst_resp);
|
2004-09-08 23:46:42 +08:00
|
|
|
if (found != NULL)
|
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
h_name = (char *) (&found->data[0].hstdata + 1);
|
|
|
|
hst_resp = found->data[0].hstdata;
|
|
|
|
aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
|
2004-09-08 23:46:42 +08:00
|
|
|
addr_list = ((char *) aliases_len
|
2007-01-31 17:14:21 +08:00
|
|
|
+ hst_resp.h_aliases_cnt * sizeof (uint32_t));
|
|
|
|
addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
|
|
|
|
recend = (const char *) found->data + found->recsize;
|
|
|
|
/* Now check if we can trust hst_resp fields. If GC is
|
|
|
|
in progress, it can contain anything. */
|
|
|
|
if (mapped->head->gc_cycle != gc_cycle)
|
|
|
|
{
|
|
|
|
retval = -2;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
#ifndef _STRING_ARCH_unaligned
|
|
|
|
/* The aliases_len array in the mapped database might very
|
|
|
|
well be unaligned. We will access it word-wise so on
|
|
|
|
platforms which do not tolerate unaligned accesses we
|
|
|
|
need to make an aligned copy. */
|
|
|
|
if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
|
|
|
|
!= 0)
|
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
|
2004-09-08 23:46:42 +08:00
|
|
|
* sizeof (uint32_t));
|
|
|
|
aliases_len = memcpy (tmp, aliases_len,
|
2007-01-31 17:14:21 +08:00
|
|
|
hst_resp.h_aliases_cnt
|
2004-09-08 23:46:42 +08:00
|
|
|
* sizeof (uint32_t));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
|
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
if (hst_resp.h_length == INADDRSZ)
|
2004-09-08 23:46:42 +08:00
|
|
|
addr_list += addr_list_len;
|
2007-01-31 17:14:21 +08:00
|
|
|
addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
|
2004-09-08 23:46:42 +08:00
|
|
|
}
|
|
|
|
if (__builtin_expect ((const char *) addr_list + addr_list_len
|
|
|
|
> recend, 0))
|
2007-01-31 17:14:21 +08:00
|
|
|
goto out;
|
2004-09-08 23:46:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
if (h_name == NULL)
|
2004-09-08 23:46:42 +08:00
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
sock = __nscd_open_socket (key, keylen, type, &hst_resp,
|
|
|
|
sizeof (hst_resp));
|
2004-09-08 23:46:42 +08:00
|
|
|
if (sock == -1)
|
|
|
|
{
|
|
|
|
__nss_not_use_nscd_hosts = 1;
|
2007-01-13 15:30:26 +08:00
|
|
|
goto out;
|
2004-09-08 23:46:42 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2004-03-02 06:35:28 +08:00
|
|
|
/* No value found so far. */
|
|
|
|
*result = NULL;
|
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
if (__builtin_expect (hst_resp.found == -1, 0))
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
/* The daemon does not cache this database. */
|
|
|
|
__nss_not_use_nscd_hosts = 1;
|
2004-09-08 23:46:42 +08:00
|
|
|
goto out_close;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
if (hst_resp.found == 1)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
char *cp = buffer;
|
1999-06-02 19:05:06 +08:00
|
|
|
uintptr_t align1;
|
|
|
|
uintptr_t align2;
|
1998-10-18 23:16:22 +08:00
|
|
|
size_t total_len;
|
|
|
|
ssize_t cnt;
|
|
|
|
char *ignore;
|
|
|
|
int n;
|
|
|
|
|
2000-02-22 17:00:35 +08:00
|
|
|
/* A first check whether the buffer is sufficiently large is possible. */
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Now allocate the buffer the array for the group members. We must
|
1999-06-02 19:05:06 +08:00
|
|
|
align the pointer and the base of the h_addr_list pointers. */
|
|
|
|
align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
|
|
|
|
& (__alignof__ (char *) - 1));
|
2007-01-31 17:14:21 +08:00
|
|
|
align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
|
1999-06-02 19:05:06 +08:00
|
|
|
- ((char *) 0)))
|
|
|
|
& (__alignof__ (char *) - 1));
|
2007-01-31 17:14:21 +08:00
|
|
|
if (buflen < (align1 + hst_resp.h_name_len + align2
|
|
|
|
+ ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
|
2004-09-08 23:46:42 +08:00
|
|
|
+ 2)
|
1998-10-18 23:16:22 +08:00
|
|
|
* sizeof (char *))
|
2007-01-31 17:14:21 +08:00
|
|
|
+ hst_resp.h_addr_list_cnt * (type == AF_INET
|
|
|
|
? INADDRSZ : IN6ADDRSZ)))
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
no_room:
|
2005-02-22 07:43:21 +08:00
|
|
|
*h_errnop = NETDB_INTERNAL;
|
1998-10-18 23:16:22 +08:00
|
|
|
__set_errno (ERANGE);
|
2004-02-27 08:55:39 +08:00
|
|
|
retval = ERANGE;
|
2004-09-08 23:46:42 +08:00
|
|
|
goto out_close;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
1999-06-02 19:05:06 +08:00
|
|
|
cp += align1;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* Prepare the result as far as we can. */
|
|
|
|
resultbuf->h_aliases = (char **) cp;
|
2007-01-31 17:14:21 +08:00
|
|
|
cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
|
1998-10-18 23:16:22 +08:00
|
|
|
resultbuf->h_addr_list = (char **) cp;
|
2007-01-31 17:14:21 +08:00
|
|
|
cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
resultbuf->h_name = cp;
|
2007-01-31 17:14:21 +08:00
|
|
|
cp += hst_resp.h_name_len + align2;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
|
|
|
|
{
|
|
|
|
resultbuf->h_addrtype = AF_INET;
|
|
|
|
resultbuf->h_length = INADDRSZ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-09-08 23:46:42 +08:00
|
|
|
resultbuf->h_addrtype = AF_INET6;
|
|
|
|
resultbuf->h_length = IN6ADDRSZ;
|
|
|
|
}
|
2007-01-31 17:14:21 +08:00
|
|
|
for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
|
2004-09-08 23:46:42 +08:00
|
|
|
{
|
|
|
|
resultbuf->h_addr_list[cnt] = cp;
|
|
|
|
cp += resultbuf->h_length;
|
|
|
|
}
|
|
|
|
resultbuf->h_addr_list[cnt] = NULL;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
if (h_name == NULL)
|
|
|
|
{
|
2007-01-13 15:30:26 +08:00
|
|
|
struct iovec vec[4];
|
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
vec[0].iov_base = resultbuf->h_name;
|
2007-01-31 17:14:21 +08:00
|
|
|
vec[0].iov_len = hst_resp.h_name_len;
|
|
|
|
total_len = hst_resp.h_name_len;
|
2004-09-08 23:46:42 +08:00
|
|
|
n = 1;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
if (hst_resp.h_aliases_cnt > 0)
|
2004-09-08 23:46:42 +08:00
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
aliases_len = alloca (hst_resp.h_aliases_cnt
|
2004-09-08 23:46:42 +08:00
|
|
|
* sizeof (uint32_t));
|
|
|
|
vec[n].iov_base = (void *) aliases_len;
|
2007-01-31 17:14:21 +08:00
|
|
|
vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
|
2004-09-08 23:46:42 +08:00
|
|
|
++n;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-09-08 23:46:42 +08:00
|
|
|
vec[n].iov_base = resultbuf->h_addr_list[0];
|
2007-01-31 17:14:21 +08:00
|
|
|
vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
|
2004-09-08 23:46:42 +08:00
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
++n;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2004-09-08 23:46:42 +08:00
|
|
|
else
|
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
if (hst_resp.h_length == INADDRSZ)
|
2004-09-08 23:46:42 +08:00
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
|
2004-09-08 23:46:42 +08:00
|
|
|
vec[n].iov_base = ignore;
|
2007-01-31 17:14:21 +08:00
|
|
|
vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
++n;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
vec[n].iov_base = resultbuf->h_addr_list[0];
|
2007-01-31 17:14:21 +08:00
|
|
|
vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
2005-02-23 06:58:32 +08:00
|
|
|
if ((size_t) __readvall (sock, vec, n) != total_len)
|
2004-09-08 23:46:42 +08:00
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-31 17:14:21 +08:00
|
|
|
memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
|
2004-09-08 23:46:42 +08:00
|
|
|
memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* Now we also can read the aliases. */
|
|
|
|
total_len = 0;
|
2007-01-31 17:14:21 +08:00
|
|
|
for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
resultbuf->h_aliases[cnt] = cp;
|
|
|
|
cp += aliases_len[cnt];
|
|
|
|
total_len += aliases_len[cnt];
|
|
|
|
}
|
|
|
|
resultbuf->h_aliases[cnt] = NULL;
|
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
if (__builtin_expect ((const char *) addr_list + addr_list_len
|
|
|
|
+ total_len > recend, 0))
|
2007-01-31 17:14:21 +08:00
|
|
|
{
|
|
|
|
/* aliases_len array might contain garbage during nscd GC cycle,
|
|
|
|
retry rather than fail in that case. */
|
|
|
|
if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
|
|
|
|
retval = -2;
|
|
|
|
goto out_close;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
/* See whether this would exceed the buffer capacity. */
|
2004-09-08 23:46:42 +08:00
|
|
|
if (__builtin_expect (cp > buffer + buflen, 0))
|
2007-01-31 17:14:21 +08:00
|
|
|
{
|
|
|
|
/* aliases_len array might contain garbage during nscd GC cycle,
|
|
|
|
retry rather than fail in that case. */
|
|
|
|
if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
|
|
|
|
{
|
|
|
|
retval = -2;
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
goto no_room;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* And finally read the aliases. */
|
2004-09-08 23:46:42 +08:00
|
|
|
if (addr_list == NULL)
|
|
|
|
{
|
2005-11-20 01:09:03 +08:00
|
|
|
if (total_len == 0
|
|
|
|
|| ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
|
|
|
|
== total_len))
|
2004-09-08 23:46:42 +08:00
|
|
|
{
|
|
|
|
retval = 0;
|
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-02-27 08:55:39 +08:00
|
|
|
{
|
2004-09-08 23:46:42 +08:00
|
|
|
memcpy (resultbuf->h_aliases[0],
|
|
|
|
(const char *) addr_list + addr_list_len, total_len);
|
|
|
|
|
2004-11-10 15:50:03 +08:00
|
|
|
/* Try to detect corrupt databases. */
|
2007-01-31 17:14:21 +08:00
|
|
|
if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
|
|
|
|
|| ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
|
2004-11-10 15:50:03 +08:00
|
|
|
if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
|
|
|
|
!= '\0')
|
|
|
|
break;
|
2007-01-31 17:14:21 +08:00
|
|
|
cnt < hst_resp.h_aliases_cnt; }))
|
|
|
|
{
|
|
|
|
/* We cannot use the database. */
|
|
|
|
if (mapped->head->gc_cycle != gc_cycle)
|
|
|
|
retval = -2;
|
|
|
|
goto out_close;
|
|
|
|
}
|
2004-11-10 15:50:03 +08:00
|
|
|
|
2004-02-27 08:55:39 +08:00
|
|
|
retval = 0;
|
|
|
|
*result = resultbuf;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Store the error number. */
|
2007-01-31 17:14:21 +08:00
|
|
|
*h_errnop = hst_resp.error;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2007-10-14 07:04:40 +08:00
|
|
|
/* Set errno to 0 to indicate no error, just no found record. */
|
|
|
|
__set_errno (0);
|
2004-02-27 08:55:39 +08:00
|
|
|
/* Even though we have not found anything, the result is zero. */
|
|
|
|
retval = 0;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2003-04-28 16:17:29 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
out_close:
|
|
|
|
if (sock != -1)
|
|
|
|
close_not_cancel_no_status (sock);
|
2003-04-28 16:17:29 +08:00
|
|
|
out:
|
2007-01-31 17:14:21 +08:00
|
|
|
if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
|
2004-09-30 16:08:40 +08:00
|
|
|
{
|
|
|
|
/* When we come here this means there has been a GC cycle while we
|
|
|
|
were looking for the data. This means the data might have been
|
|
|
|
inconsistent. Retry if possible. */
|
2007-01-31 17:14:21 +08:00
|
|
|
if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
|
2004-09-30 16:08:40 +08:00
|
|
|
{
|
|
|
|
/* nscd is just running gc now. Disable using the mapping. */
|
2007-01-31 17:14:21 +08:00
|
|
|
if (atomic_decrement_val (&mapped->counter) == 0)
|
|
|
|
__nscd_unmap (mapped);
|
2004-09-30 16:08:40 +08:00
|
|
|
mapped = NO_MAPPING;
|
|
|
|
}
|
|
|
|
|
2007-01-31 17:14:21 +08:00
|
|
|
if (retval != -1)
|
|
|
|
goto retry;
|
2004-09-30 16:08:40 +08:00
|
|
|
}
|
2003-04-28 16:17:29 +08:00
|
|
|
|
2004-02-27 08:55:39 +08:00
|
|
|
return retval;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|