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/>. */
20 #include <libc-lock.h>
23 #include <set-freeres.h>
27 /*******************************************************************\
28 |* Here we assume several symbols to be defined: *|
30 |* LOOKUP_TYPE - the return type of the function *|
32 |* FUNCTION_NAME - name of the non-reentrant function *|
34 |* DATABASE_NAME - name of the database the function accesses *|
35 |* (e.g., host, services, ...) *|
37 |* ADD_PARAMS - additional parameter, can vary in number *|
39 |* ADD_VARIABLES - names of additional parameter *|
41 |* BUFLEN - length of buffer allocated for the non *|
42 |* reentrant version *|
44 |* Optionally the following vars can be defined: *|
46 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
47 |* the global `h_errno' variable. *|
49 \*******************************************************************/
52 #ifdef HANDLE_DIGITS_DOTS
53 # include <resolv/resolv_context.h>
56 /* To make the real sources a bit prettier. */
57 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
58 #define APPEND_R(name) APPEND_R1 (name)
59 #define APPEND_R1(name) name##_r
60 #define INTERNAL(name) INTERNAL1 (name)
61 #define INTERNAL1(name) __##name
62 #define APPEND_FREEMEM_NAME1(name) __libc_##name##_freemem_ptr
63 #define APPEND_FREEMEM_NAME(name) APPEND_FREEMEM_NAME1(name)
64 #define FREEMEM_NAME APPEND_FREEMEM_NAME (FUNCTION_NAME)
66 /* Sometimes we need to store error codes in the `h_errno' variable. */
68 # define H_ERRNO_PARM , int *h_errnop
69 # define H_ERRNO_VAR , &h_errno_tmp
70 # define H_ERRNO_VAR_P &h_errno_tmp
74 # define H_ERRNO_VAR_P NULL
80 # define AF_VAL AF_INET
83 /* Prototype for reentrant version we use here. */
84 extern int INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
,
85 char *buffer
, size_t buflen
,
86 LOOKUP_TYPE
**result H_ERRNO_PARM
)
89 /* We need to protect the dynamic buffer handling. */
90 __libc_lock_define_initialized (static, lock
);
92 /* This points to the static buffer used. */
95 weak_alias (buffer
, FREEMEM_NAME
)
98 FUNCTION_NAME (ADD_PARAMS
)
100 static size_t buffer_size
;
101 static LOOKUP_TYPE resbuf
;
107 #ifdef HANDLE_DIGITS_DOTS
108 /* Wrap both __nss_hostname_digits_dots and the actual lookup
109 function call in the same context. */
110 struct resolv_context
*res_ctx
= __resolv_context_get ();
114 __set_h_errno (NETDB_INTERNAL
);
121 __libc_lock_lock (lock
);
125 buffer_size
= BUFLEN
;
126 buffer
= (char *) malloc (buffer_size
);
129 #ifdef HANDLE_DIGITS_DOTS
132 if (__nss_hostname_digits_dots_context
133 (res_ctx
, name
, &resbuf
, &buffer
, &buffer_size
, 0, &result
, NULL
,
134 AF_VAL
, H_ERRNO_VAR_P
))
139 while (buffer
!= NULL
140 && (INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, &resbuf
, buffer
,
141 buffer_size
, &result H_ERRNO_VAR
)
144 && h_errno_tmp
== NETDB_INTERNAL
150 new_buf
= (char *) realloc (buffer
, buffer_size
);
153 /* We are out of memory. Free the current buffer so that the
154 process gets a chance for a normal termination. */
156 __set_errno (ENOMEM
);
164 #ifdef HANDLE_DIGITS_DOTS
168 __libc_lock_unlock (lock
);
170 #ifdef HANDLE_DIGITS_DOTS
171 __resolv_context_put (res_ctx
);
175 if (h_errno_tmp
!= 0)
176 __set_h_errno (h_errno_tmp
);
182 nss_interface_function (FUNCTION_NAME
)