2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * util-int.c Various functions to replace missing threadsafe ones.
7 * Without the real *_r funcs, things will work, but won't be
10 * Written by Bart Hartgers.
12 * Copyright 1998, A. Hartgers, All rights reserved.
13 * This software is not subject to any license of Eindhoven University of
14 * Technology, since it was written in my spare time.
16 * Redistribution and use in source and binary forms are permitted only
17 * as authorized by the OpenLDAP Public License. A copy of this
18 * license is available at http://www.OpenLDAP.org/license.html or
19 * in file LICENSE in the top-level directory of the distribution.
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #if defined( LDAP_R_COMPILE )
34 # include <ldap_pvt_thread.h>
36 # undef HAVE_REENTRANT_FUNCTIONS
39 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
40 && defined( CTIME_R_NARGS )
44 #ifdef LDAP_COMPILING_R
46 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
48 # ifndef HAVE_GETHOSTBYNAME_R
49 static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
51 # ifndef HAVE_GETHOSTBYADDR_R
52 static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
54 #endif /* LDAP_R_COMPILE */
56 char *ldap_pvt_ctime( const time_t *tp, char *buf )
59 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
60 choke me! nargs should have 2 or 3
61 # elif CTIME_R_NARGS > 2
62 return ctime_r(tp,buf,26);
64 return ctime_r(tp,buf);
68 # ifdef LDAP_COMPILNG_R
69 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
71 memcpy( buf, ctime(tp), 26 );
72 # ifdef LDAP_COMPILNG_R
73 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
80 #define BUFMAX (32*1024)
82 static char *safe_realloc( char **buf, int len );
83 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
85 int ldap_pvt_gethostbyname_a(
87 struct hostent *resbuf,
89 struct hostent **result,
92 #if defined( HAVE_GETHOSTBYNAME_R )
93 # define NEED_SAFE_REALLOC 1
97 for(;buflen<BUFMAX;) {
98 if (safe_realloc( buf, buflen )==NULL)
100 r = gethostbyname_r( name, resbuf, *buf,
101 buflen, result, herrno_ptr );
102 #ifdef NETDB_INTERNAL
104 (*herrno_ptr==NETDB_INTERNAL) &&
114 #elif defined( LDAP_COMPILING_R )
115 # define NEED_COPY_HOSTENT
119 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
121 he = gethostbyname( name );
124 *herrno_ptr = h_errno;
126 } else if (copy_hostent( resbuf, buf, he )<0) {
134 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
138 *result = gethostbyname( name );
144 *herrno_ptr = h_errno;
150 int ldap_pvt_gethostbyaddr_a(
154 struct hostent *resbuf,
156 struct hostent **result,
159 #if defined( HAVE_GETHOSTBYADDR_R )
160 # undef NEED_SAFE_REALLOC
161 # define NEED_SAFE_REALLOC
165 for(;buflen<BUFMAX;) {
166 if (safe_realloc( buf, buflen )==NULL)
168 r = gethostbyaddr_r( addr, len, type,
169 resbuf, *buf, buflen,
170 result, herrno_ptr );
171 #ifdef NETDB_INTERNAL
173 (*herrno_ptr==NETDB_INTERNAL) &&
183 #elif defined( LDAP_COMPILING_R )
184 # undef NEED_COPY_HOSTENT
185 # define NEED_COPY_HOSTENT
189 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
191 he = gethostbyaddr( addr, len, type );
194 *herrno_ptr = h_errno;
196 } else if (copy_hostent( resbuf, buf, he )<0) {
204 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
207 #else /* gethostbyaddr() */
208 *result = gethostbyaddr( addr, len, type );
217 * ldap_pvt_init_utils() should be called before any other function.
220 void ldap_pvt_init_utils( void )
227 #ifdef LDAP_COMPILING_R
229 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
230 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex, NULL );
233 #if !defined( HAVE_GETHOSTBYNAME_R )
234 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex, NULL );
237 #if !defined( HAVE_GETHOSTBYADDR_R )
238 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex, NULL );
241 /* call other module init functions here... */
245 #if defined( NEED_COPY_HOSTENT )
246 # undef NEED_SAFE_REALLOC
247 #define NEED_SAFE_REALLOC
249 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
253 for( ; (*src) ; src++ ) {
254 len = strlen( *src ) + 1;
255 memcpy( buf, *src, len );
263 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
266 for( ; (*src) ; src++ ) {
267 memcpy( buf, *src, len );
275 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
287 /* calculate the size needed for the buffer */
288 name_len = strlen( src->h_name ) + 1;
290 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
291 total_alias_len += strlen( *p ) + 1;
295 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
298 total_addr_len = n_addr * src->h_length;
300 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
301 total_addr_len + total_alias_len + name_len;
303 if (safe_realloc( buf, total_len )) {
305 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
306 memcpy( res, src, sizeof( struct hostent ) );
307 /* first the name... */
308 memcpy( tbuf, src->h_name, name_len );
309 res->h_name = tbuf; tbuf+=name_len;
310 /* now the aliases */
312 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
314 /* finally the addresses */
315 res->h_addr_list = tp;
316 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
324 #if defined( NEED_SAFE_REALLOC )
325 static char *safe_realloc( char **buf, int len )
328 tmpbuf = realloc( *buf, len );