Back to index

glibc  2.9
nscd_getai.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <netdb.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <not-cancel.h>
00027 
00028 #include "nscd-client.h"
00029 #include "nscd_proto.h"
00030 
00031 
00032 /* Define in nscd_gethst_r.c.  */
00033 extern int __nss_not_use_nscd_hosts;
00034 
00035 
00036 /* We use the mapping from nscd_gethst.  */
00037 libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
00038 
00039 
00040 int
00041 __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
00042 {
00043   size_t keylen = strlen (key) + 1;
00044   int gc_cycle;
00045   int nretries = 0;
00046 
00047   /* If the mapping is available, try to search there instead of
00048      communicating with the nscd.  */
00049   struct mapped_database *mapped;
00050   mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
00051                             &gc_cycle);
00052 
00053  retry:;
00054   struct nscd_ai_result *resultbuf = NULL;
00055   const char *recend = (const char *) ~UINTMAX_C (0);
00056   char *respdata = NULL;
00057   int retval = -1;
00058   int sock = -1;
00059   ai_response_header ai_resp;
00060 
00061   if (mapped != NO_MAPPING)
00062     {
00063       struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
00064                                               mapped);
00065       if (found != NULL)
00066        {
00067          respdata = (char *) (&found->data[0].aidata + 1);
00068          ai_resp = found->data[0].aidata;
00069          recend = (const char *) found->data + found->recsize;
00070          /* Now check if we can trust ai_resp fields.  If GC is
00071             in progress, it can contain anything.  */
00072          if (mapped->head->gc_cycle != gc_cycle)
00073            {
00074              retval = -2;
00075              goto out;
00076            }
00077        }
00078     }
00079 
00080   /* If we do not have the cache mapped, try to get the data over the
00081      socket.  */
00082   if (respdata == NULL)
00083     {
00084       sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
00085                              sizeof (ai_resp));
00086       if (sock == -1)
00087        {
00088          /* nscd not running or wrong version.  */
00089          __nss_not_use_nscd_hosts = 1;
00090          goto out;
00091        }
00092     }
00093 
00094   if (ai_resp.found == 1)
00095     {
00096       size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
00097 
00098       /* This check really only affects the case where the data
00099         comes from the mapped cache.  */
00100       if (respdata + datalen > recend)
00101        {
00102          assert (sock == -1);
00103          goto out;
00104        }
00105 
00106       /* Create result.  */
00107       resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
00108                                               + datalen);
00109       if (resultbuf == NULL)
00110        {
00111          *h_errnop = NETDB_INTERNAL;
00112          goto out_close;
00113        }
00114 
00115       /* Set up the data structure, including pointers.  */
00116       resultbuf->naddrs = ai_resp.naddrs;
00117       resultbuf->addrs = (char *) (resultbuf + 1);
00118       resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
00119       if (ai_resp.canonlen != 0)
00120        resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
00121       else
00122        resultbuf->canon = NULL;
00123 
00124       if (respdata == NULL)
00125        {
00126          /* Read the data from the socket.  */
00127          if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
00128            {
00129              retval = 0;
00130              *result = resultbuf;
00131            }
00132          else
00133            {
00134              free (resultbuf);
00135              *h_errnop = NETDB_INTERNAL;
00136            }
00137        }
00138       else
00139        {
00140          /* Copy the data in the block.  */
00141          memcpy (resultbuf + 1, respdata, datalen);
00142 
00143          /* Try to detect corrupt databases.  */
00144          if (resultbuf->canon != NULL
00145              && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
00146            /* We cannot use the database.  */
00147            {
00148              if (mapped->head->gc_cycle != gc_cycle)
00149               retval = -2;
00150              else
00151               free (resultbuf);
00152              goto out_close;
00153            }
00154 
00155          retval = 0;
00156          *result = resultbuf;
00157        }
00158     }
00159   else
00160     {
00161       if (__builtin_expect (ai_resp.found == -1, 0))
00162        {
00163          /* The daemon does not cache this database.  */
00164          __nss_not_use_nscd_hosts = 1;
00165          goto out_close;
00166        }
00167 
00168       /* Store the error number.  */
00169       *h_errnop = ai_resp.error;
00170 
00171       /* Set errno to 0 to indicate no error, just no found record.  */
00172       __set_errno (0);
00173       /* Even though we have not found anything, the result is zero.  */
00174       retval = 0;
00175     }
00176 
00177  out_close:
00178   if (sock != -1)
00179     close_not_cancel_no_status (sock);
00180  out:
00181   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
00182     {
00183       /* When we come here this means there has been a GC cycle while we
00184         were looking for the data.  This means the data might have been
00185         inconsistent.  Retry if possible.  */
00186       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
00187        {
00188          /* nscd is just running gc now.  Disable using the mapping.  */
00189          if (atomic_decrement_val (&mapped->counter) == 0)
00190            __nscd_unmap (mapped);
00191          mapped = NO_MAPPING;
00192        }
00193 
00194       if (retval != -1)
00195        {
00196          *result = NULL;
00197          free (resultbuf);
00198          goto retry;
00199        }
00200     }
00201 
00202   return retval;
00203 }