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>
35 # if !defined( HAVE_REENTRANT_FUNCTIONS )
36 # if !defined( HAVE_CTIME_R )
37 # define LDAP_INT_CTIME_MUTEX 1
39 # if !defined( HAVE_GETHOSTBYNAME_R )
40 # define LDAP_INT_GETHOSTBYNAME_MUTEX 1
42 # if !defined( HAVE_GETHOSTBYADDR_R )
43 # define LDAP_INT_GETHOSTBYADDR_MUTEX 1
45 # endif /* defined( HAVE_REENTRANT_FUNCTIONS ) */
47 #if defined( LDAP_INT_CTIME_MUTEX )
48 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
50 #if defined( LDAP_INT_GETHOSTBYNAME_MUTEX )
51 static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
53 #if defined( LDAP_INT_GETHOSTBYADDR_MUTEX )
54 static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
56 #else /* LDAP_R_COMPILE */
57 #undef HAVE_REENTRANT_FUNCTIONS
60 char *ldap_pvt_ctime( const time_t *tp, char *buf )
62 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
63 && defined( CTIME_R_NARGS )
64 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
65 choke me! nargs should have 2 or 3
66 # elif CTIME_R_NARGS > 2
67 return ctime_r(tp,buf,26);
69 return ctime_r(tp,buf);
72 # if defined( LDAP_INT_CTIME_MUTEX )
73 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
75 memcpy( buf, ctime(tp), 26 );
76 # if defined( LDAP_INT_CTIME_MUTEX )
77 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
84 #define BUFMAX (32*1024)
86 static char *safe_realloc( char **buf, int len );
87 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
89 int ldap_pvt_gethostbyname_a(
91 struct hostent *resbuf,
93 struct hostent **result,
96 #if defined( HAVE_GETHOSTBYNAME_R ) || defined( HAVE_REENTRANT_FUNCTIONS )
97 # define NEED_SAFE_REALLOC 1
101 for(;buflen<BUFMAX;) {
102 if (safe_realloc( buf, buflen )==NULL)
104 r = gethostbyname_r( name, resbuf, *buf,
105 buflen, result, herrno_ptr );
106 #ifdef NETDB_INTERNAL
108 (*herrno_ptr==NETDB_INTERNAL) &&
118 #elif defined( LDAP_INT_GETHOSTBYNAME_MUTEX )
119 # define NEED_COPY_HOSTENT
123 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
125 he = gethostbyname( name );
128 *herrno_ptr = h_errno;
130 } else if (copy_hostent( resbuf, buf, he )<0) {
138 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
142 *result = gethostbyname( name );
148 *herrno_ptr = h_errno;
154 int ldap_pvt_gethostbyaddr_a(
158 struct hostent *resbuf,
160 struct hostent **result,
163 #if defined( HAVE_GETHOSTBYADDR_R ) || defined( HAVE_REENTRANT_FUNCTIONS )
164 # undef NEED_SAFE_REALLOC
165 # define NEED_SAFE_REALLOC
169 for(;buflen<BUFMAX;) {
170 if (safe_realloc( buf, buflen )==NULL)
172 r = gethostbyaddr_r( addr, len, type,
173 resbuf, *buf, buflen,
174 result, herrno_ptr );
175 #ifdef NETDB_INTERNAL
177 (*herrno_ptr==NETDB_INTERNAL) &&
187 #elif defined( LDAP_INT_GETHOSTBYADDR_MUTEX )
188 # undef NEED_COPY_HOSTENT
189 # define NEED_COPY_HOSTENT
193 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
195 he = gethostbyaddr( addr, len, type );
198 *herrno_ptr = h_errno;
200 } else if (copy_hostent( resbuf, buf, he )<0) {
208 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
211 #else /* gethostbyaddr() */
212 *result = gethostbyaddr( addr, len, type );
221 * ldap_pvt_init_utils() should be called before any other function.
224 void ldap_pvt_init_utils( void )
230 #if defined( LDAP_INT_CTIME_MUTEX )
231 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex, NULL );
233 #if defined( LDAP_INT_GETHOSTBYNAME_MUTEX )
234 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex, NULL );
236 #if defined( LDAP_INT_GETHOSTBYADDR_MUTEX )
237 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex, NULL );
239 #if defined( LDAP_R_COMPILE )
240 /* call other module init functions here... */
244 #if defined( NEED_COPY_HOSTENT )
245 # undef NEED_SAFE_REALLOC
246 #define NEED_SAFE_REALLOC
248 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
252 for( ; (*src) ; src++ ) {
253 len = strlen( *src ) + 1;
254 memcpy( buf, *src, len );
262 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
265 for( ; (*src) ; src++ ) {
266 memcpy( buf, *src, len );
274 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
286 /* calculate the size needed for the buffer */
287 name_len = strlen( src->h_name ) + 1;
289 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
290 total_alias_len += strlen( *p ) + 1;
294 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
297 total_addr_len = n_addr * src->h_length;
299 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
300 total_addr_len + total_alias_len + name_len;
302 if (safe_realloc( buf, total_len )) {
304 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
305 memcpy( res, src, sizeof( struct hostent ) );
306 /* first the name... */
307 memcpy( tbuf, src->h_name, name_len );
308 res->h_name = tbuf; tbuf+=name_len;
309 /* now the aliases */
311 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
313 /* finally the addresses */
314 res->h_addr_list = tp;
315 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
323 #if defined( NEED_SAFE_REALLOC )
324 static char *safe_realloc( char **buf, int len )
327 tmpbuf = realloc( *buf, len );