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
8 * work, but might not be threadsafe.
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.
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #if defined( LDAP_R_COMPILE )
35 # include <ldap_pvt_thread.h>
37 # undef HAVE_REENTRANT_FUNCTIONS
40 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
41 && defined( CTIME_R_NARGS )
45 #if defined(HAVE_GETHOSTBYNAME_R) && \
46 (GETHOSTBYNAME_R_NARGS > 6 || GETHOSTBYNAME_R_NARGS < 5)
47 /* Don't know how to handle this version, pretend it's not there */
48 # undef HAVE_GETHOSTBYNAME_R
50 #if defined(HAVE_GETHOSTBYADDR_R) && \
51 (GETHOSTBYADDR_R_NARGS > 8 || GETHOSTBYADDR_R_NARGS < 7)
52 /* Don't know how to handle this version, pretend it's not there */
53 # undef HAVE_GETHOSTBYADDR_R
58 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
60 # ifndef HAVE_GETHOSTBYNAME_R
61 static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
63 # ifndef HAVE_GETHOSTBYADDR_R
64 static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
66 #endif /* LDAP_R_COMPILE */
68 char *ldap_pvt_ctime( const time_t *tp, char *buf )
71 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
72 choke me! nargs should have 2 or 3
73 # elif CTIME_R_NARGS > 2
74 return ctime_r(tp,buf,26);
76 return ctime_r(tp,buf);
80 # ifdef LDAP_R_COMPILE
81 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
83 memcpy( buf, ctime(tp), 26 );
84 # ifdef LDAP_R_COMPILE
85 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
92 #define BUFMAX (32*1024)
94 static char *safe_realloc( char **buf, int len );
95 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
97 int ldap_pvt_gethostbyname_a(
99 struct hostent *resbuf,
101 struct hostent **result,
104 #if defined( HAVE_GETHOSTBYNAME_R )
106 # define NEED_SAFE_REALLOC 1
110 for(;buflen<BUFMAX;) {
111 if (safe_realloc( buf, buflen )==NULL)
114 #if (GETHOSTBYNAME_R_NARGS < 6)
115 r = ((*result=gethostbyname_r( name, resbuf, *buf,\
116 buflen, herrno_ptr ))== NULL) ?\
119 r = gethostbyname_r( name, resbuf, *buf,
120 buflen, result, herrno_ptr );
123 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",\
126 #ifdef NETDB_INTERNAL
128 (*herrno_ptr==NETDB_INTERNAL) &&
138 #elif defined( LDAP_R_COMPILE )
139 # define NEED_COPY_HOSTENT
143 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
145 he = gethostbyname( name );
148 *herrno_ptr = h_errno;
150 } else if (copy_hostent( resbuf, buf, he )<0) {
158 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
162 *result = gethostbyname( name );
168 *herrno_ptr = h_errno;
174 int ldap_pvt_gethostbyaddr_a(
178 struct hostent *resbuf,
180 struct hostent **result,
183 #if defined( HAVE_GETHOSTBYADDR_R )
185 # undef NEED_SAFE_REALLOC
186 # define NEED_SAFE_REALLOC
190 for(;buflen<BUFMAX;) {
191 if (safe_realloc( buf, buflen )==NULL)
193 #if (GETHOSTBYADDR_R_NARGS < 8)
194 r = ((*result=gethostbyaddr_r( addr, len, type,
195 resbuf, *buf, buflen,
196 herrno_ptr )) == NULL) ?\
199 r = gethostbyaddr_r( addr, len, type,
200 resbuf, *buf, buflen,
201 result, herrno_ptr );
204 #ifdef NETDB_INTERNAL
206 (*herrno_ptr==NETDB_INTERNAL) &&
216 #elif defined( LDAP_R_COMPILE )
217 # undef NEED_COPY_HOSTENT
218 # define NEED_COPY_HOSTENT
222 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
224 he = gethostbyaddr( addr, len, type );
227 *herrno_ptr = h_errno;
229 } else if (copy_hostent( resbuf, buf, he )<0) {
237 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
240 #else /* gethostbyaddr() */
241 *result = gethostbyaddr( addr, len, type );
250 * ldap_pvt_init_utils() should be called before any other function.
253 void ldap_pvt_init_utils( void )
260 #ifdef LDAP_R_COMPILE
262 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
263 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
266 #if !defined( HAVE_GETHOSTBYNAME_R )
267 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
270 #if !defined( HAVE_GETHOSTBYADDR_R )
271 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
274 /* call other module init functions here... */
278 #if defined( NEED_COPY_HOSTENT )
279 # undef NEED_SAFE_REALLOC
280 #define NEED_SAFE_REALLOC
282 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
286 for( ; (*src) ; src++ ) {
287 len = strlen( *src ) + 1;
288 memcpy( buf, *src, len );
296 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
299 for( ; (*src) ; src++ ) {
300 memcpy( buf, *src, len );
308 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
320 /* calculate the size needed for the buffer */
321 name_len = strlen( src->h_name ) + 1;
323 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
324 total_alias_len += strlen( *p ) + 1;
328 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
331 total_addr_len = n_addr * src->h_length;
333 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
334 total_addr_len + total_alias_len + name_len;
336 if (safe_realloc( buf, total_len )) {
338 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
339 memcpy( res, src, sizeof( struct hostent ) );
340 /* first the name... */
341 memcpy( tbuf, src->h_name, name_len );
342 res->h_name = tbuf; tbuf+=name_len;
343 /* now the aliases */
345 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
347 /* finally the addresses */
348 res->h_addr_list = tp;
349 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
357 #if defined( NEED_SAFE_REALLOC )
358 static char *safe_realloc( char **buf, int len )
361 tmpbuf = realloc( *buf, len );