Back to index

glibc  2.9
getXXbyYY.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2001,2003, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <assert.h>
00020 #include <errno.h>
00021 #include <bits/libc-lock.h>
00022 #include <stdlib.h>
00023 #include <resolv.h>
00024 
00025 #include "nsswitch.h"
00026 
00027 /*******************************************************************\
00028 |* Here we assume several symbols to be defined:           *|
00029 |*                                                         *|
00030 |* LOOKUP_TYPE   - the return type of the function                *|
00031 |*                                                         *|
00032 |* FUNCTION_NAME - name of the non-reentrant function             *|
00033 |*                                                         *|
00034 |* DATABASE_NAME - name of the database the function accesses     *|
00035 |*               (e.g., host, services, ...)                      *|
00036 |*                                                         *|
00037 |* ADD_PARAMS    - additional parameter, can vary in number       *|
00038 |*                                                         *|
00039 |* ADD_VARIABLES - names of additional parameter           *|
00040 |*                                                         *|
00041 |* BUFLEN      - length of buffer allocated for the non    *|
00042 |*               reentrant version                         *|
00043 |*                                                         *|
00044 |* Optionally the following vars can be defined:           *|
00045 |*                                                         *|
00046 |* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
00047 |*               the global `h_errno' variable.            *|
00048 |*                                                         *|
00049 \*******************************************************************/
00050 
00051 /* To make the real sources a bit prettier.  */
00052 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
00053 #define APPEND_R(name) APPEND_R1 (name)
00054 #define APPEND_R1(name) name##_r
00055 #define INTERNAL(name) INTERNAL1 (name)
00056 #define INTERNAL1(name) __##name
00057 
00058 /* Sometimes we need to store error codes in the `h_errno' variable.  */
00059 #ifdef NEED_H_ERRNO
00060 # define H_ERRNO_PARM , int *h_errnop
00061 # define H_ERRNO_VAR , &h_errno_tmp
00062 # define H_ERRNO_VAR_P &h_errno_tmp
00063 #else
00064 # define H_ERRNO_PARM
00065 # define H_ERRNO_VAR
00066 # define H_ERRNO_VAR_P NULL
00067 #endif
00068 
00069 #ifdef HAVE_AF
00070 # define AF_VAL af
00071 #else
00072 # define AF_VAL AF_INET
00073 #endif
00074 
00075 /* Prototype for reentrant version we use here.  */
00076 extern int INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf,
00077                                   char *buffer, size_t buflen,
00078                                   LOOKUP_TYPE **result H_ERRNO_PARM);
00079 
00080 /* We need to protect the dynamic buffer handling.  */
00081 __libc_lock_define_initialized (static, lock);
00082 
00083 /* This points to the static buffer used.  */
00084 libc_freeres_ptr (static char *buffer);
00085 
00086 
00087 LOOKUP_TYPE *
00088 FUNCTION_NAME (ADD_PARAMS)
00089 {
00090   static size_t buffer_size;
00091   static LOOKUP_TYPE resbuf;
00092   LOOKUP_TYPE *result;
00093 #ifdef NEED_H_ERRNO
00094   int h_errno_tmp = 0;
00095 #endif
00096 
00097   /* Get lock.  */
00098   __libc_lock_lock (lock);
00099 
00100   if (buffer == NULL)
00101     {
00102       buffer_size = BUFLEN;
00103       buffer = (char *) malloc (buffer_size);
00104     }
00105 
00106 #ifdef HANDLE_DIGITS_DOTS
00107   if (buffer != NULL)
00108     {
00109       if (__nss_hostname_digits_dots (name, &resbuf, &buffer,
00110                                   &buffer_size, 0, &result, NULL, AF_VAL,
00111                                   H_ERRNO_VAR_P))
00112        goto done;
00113     }
00114 #endif
00115 
00116   while (buffer != NULL
00117         && (INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, &resbuf, buffer,
00118                                    buffer_size, &result H_ERRNO_VAR)
00119             == ERANGE)
00120 #ifdef NEED_H_ERRNO
00121         && h_errno_tmp == NETDB_INTERNAL
00122 #endif
00123         )
00124     {
00125       char *new_buf;
00126       buffer_size *= 2;
00127       new_buf = (char *) realloc (buffer, buffer_size);
00128       if (new_buf == NULL)
00129        {
00130          /* We are out of memory.  Free the current buffer so that the
00131             process gets a chance for a normal termination.  */
00132          free (buffer);
00133          __set_errno (ENOMEM);
00134        }
00135       buffer = new_buf;
00136     }
00137 
00138   if (buffer == NULL)
00139     result = NULL;
00140 
00141 #ifdef HANDLE_DIGITS_DOTS
00142 done:
00143 #endif
00144   /* Release lock.  */
00145   __libc_lock_unlock (lock);
00146 
00147 #ifdef NEED_H_ERRNO
00148   if (h_errno_tmp != 0)
00149     __set_h_errno (h_errno_tmp);
00150 #endif
00151 
00152   return result;
00153 }
00154 
00155 static_link_warning (FUNCTION_NAME)