3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * util-int.c Various functions to replace missing threadsafe ones.
8 * Without the real *_r funcs, things will
9 * work, but might not be threadsafe.
11 * Written by Bart Hartgers.
13 * Copyright 1998, A. Hartgers, All rights reserved.
14 * This software is not subject to any license of Eindhoven University of
15 * Technology, since it was written in my spare time.
17 * Redistribution and use in source and binary forms are permitted only
18 * as authorized by the OpenLDAP Public License. A copy of this
19 * license is available at http://www.OpenLDAP.org/license.html or
20 * in file LICENSE in the top-level directory of the distribution.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
35 #if defined( LDAP_R_COMPILE )
36 # include <ldap_pvt_thread.h>
38 # undef HAVE_REENTRANT_FUNCTIONS
41 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
42 && defined( CTIME_R_NARGS )
46 #if defined(HAVE_GETHOSTBYNAME_R) && \
47 (GETHOSTBYNAME_R_NARGS > 6 || GETHOSTBYNAME_R_NARGS < 5)
48 /* Don't know how to handle this version, pretend it's not there */
49 # undef HAVE_GETHOSTBYNAME_R
51 #if defined(HAVE_GETHOSTBYADDR_R) && \
52 (GETHOSTBYADDR_R_NARGS > 8 || GETHOSTBYADDR_R_NARGS < 7)
53 /* Don't know how to handle this version, pretend it's not there */
54 # undef HAVE_GETHOSTBYADDR_R
59 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
61 # if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
62 static ldap_pvt_thread_mutex_t ldap_int_gethostby_mutex;
64 # ifdef HAVE_RES_QUERY
65 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
67 #endif /* LDAP_R_COMPILE */
69 char *ldap_pvt_ctime( const time_t *tp, char *buf )
72 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
73 # error "CTIME_R_NARGS should be 2 or 3"
74 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
75 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
76 # elif CTIME_R_NARGS > 2
77 return ctime_r(tp,buf,26);
79 return ctime_r(tp,buf);
83 # ifdef LDAP_R_COMPILE
84 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
86 AC_MEMCPY( buf, ctime(tp), 26 );
87 # ifdef LDAP_R_COMPILE
88 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
95 #define BUFMAX (32*1024)
97 static char *safe_realloc( char **buf, int len );
98 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
100 int ldap_pvt_gethostbyname_a(
102 struct hostent *resbuf,
104 struct hostent **result,
107 #if defined( HAVE_GETHOSTBYNAME_R )
109 # define NEED_SAFE_REALLOC 1
113 for(;buflen<BUFMAX;) {
114 if (safe_realloc( buf, buflen )==NULL)
117 #if (GETHOSTBYNAME_R_NARGS < 6)
118 r = ((*result=gethostbyname_r( name, resbuf, *buf,
119 buflen, herrno_ptr ))== NULL) ?
122 r = gethostbyname_r( name, resbuf, *buf,
123 buflen, result, herrno_ptr );
126 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
129 #ifdef NETDB_INTERNAL
131 (*herrno_ptr==NETDB_INTERNAL) &&
141 #elif defined( LDAP_R_COMPILE )
142 # define NEED_COPY_HOSTENT
146 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
148 he = gethostbyname( name );
151 *herrno_ptr = h_errno;
153 } else if (copy_hostent( resbuf, buf, he )<0) {
161 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
165 *result = gethostbyname( name );
171 *herrno_ptr = h_errno;
177 int ldap_pvt_gethostbyaddr_a(
181 struct hostent *resbuf,
183 struct hostent **result,
186 #if defined( HAVE_GETHOSTBYADDR_R )
188 # undef NEED_SAFE_REALLOC
189 # define NEED_SAFE_REALLOC
193 for(;buflen<BUFMAX;) {
194 if (safe_realloc( buf, buflen )==NULL)
196 #if (GETHOSTBYADDR_R_NARGS < 8)
197 r = ((*result=gethostbyaddr_r( addr, len, type,
198 resbuf, *buf, buflen,
199 herrno_ptr )) == NULL) ?
202 r = gethostbyaddr_r( addr, len, type,
203 resbuf, *buf, buflen,
204 result, herrno_ptr );
207 #ifdef NETDB_INTERNAL
209 (*herrno_ptr==NETDB_INTERNAL) &&
219 #elif defined( LDAP_R_COMPILE )
220 # undef NEED_COPY_HOSTENT
221 # define NEED_COPY_HOSTENT
225 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
227 he = gethostbyaddr( addr, len, type );
230 *herrno_ptr = h_errno;
232 } else if (copy_hostent( resbuf, buf, he )<0) {
240 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
243 #else /* gethostbyaddr() */
244 *result = gethostbyaddr( addr, len, type );
253 * ldap_int_utils_init() should be called before any other function.
256 void ldap_int_utils_init( void )
263 #ifdef LDAP_R_COMPILE
265 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
266 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
269 #if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
270 ldap_pvt_thread_mutex_init( &ldap_int_gethostby_mutex );
273 #ifdef HAVE_RES_QUERY
274 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
277 /* call other module init functions here... */
281 #if defined( NEED_COPY_HOSTENT )
282 # undef NEED_SAFE_REALLOC
283 #define NEED_SAFE_REALLOC
285 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
289 for( ; (*src) ; src++ ) {
290 len = strlen( *src ) + 1;
291 AC_MEMCPY( buf, *src, len );
299 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
302 for( ; (*src) ; src++ ) {
303 AC_MEMCPY( buf, *src, len );
311 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
318 int total_alias_len=0;
323 /* calculate the size needed for the buffer */
324 name_len = strlen( src->h_name ) + 1;
326 if( src->h_aliases != NULL ) {
327 for( p = src->h_aliases; (*p) != NULL; p++ ) {
328 total_alias_len += strlen( *p ) + 1;
333 if( src->h_addr_list != NULL ) {
334 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
337 total_addr_len = n_addr * src->h_length;
340 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
341 total_addr_len + total_alias_len + name_len;
343 if (safe_realloc( buf, total_len )) {
345 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
346 AC_MEMCPY( res, src, sizeof( struct hostent ) );
347 /* first the name... */
348 AC_MEMCPY( tbuf, src->h_name, name_len );
349 res->h_name = tbuf; tbuf+=name_len;
350 /* now the aliases */
352 if ( src->h_aliases != NULL ) {
353 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
356 /* finally the addresses */
357 res->h_addr_list = tp;
358 if ( src->h_addr_list != NULL ) {
359 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
368 #if defined( NEED_SAFE_REALLOC )
369 static char *safe_realloc( char **buf, int len )
372 tmpbuf = LDAP_REALLOC( *buf, len );