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 )
47 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
49 # ifndef HAVE_GETHOSTBYNAME_R
50 static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
52 # ifndef HAVE_GETHOSTBYADDR_R
53 static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
55 #endif /* LDAP_R_COMPILE */
57 char *ldap_pvt_ctime( const time_t *tp, char *buf )
60 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
61 choke me! nargs should have 2 or 3
62 # elif CTIME_R_NARGS > 2
63 return ctime_r(tp,buf,26);
65 return ctime_r(tp,buf);
69 # ifdef LDAP_R_COMPILE
70 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
72 memcpy( buf, ctime(tp), 26 );
73 # ifdef LDAP_R_COMPILE
74 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
81 #define BUFMAX (32*1024)
83 static char *safe_realloc( char **buf, int len );
84 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
86 int ldap_pvt_gethostbyname_a(
88 struct hostent *resbuf,
90 struct hostent **result,
93 #if defined( HAVE_GETHOSTBYNAME_R )
95 # if (GETHOSTBYNAME_R_NARGS > 6) || (GETHOSTBYNAME_R_NARGS < 5)
96 Ouch! gethostbyname_r() must have either 5 or 6 args
99 # define NEED_SAFE_REALLOC 1
103 for(;buflen<BUFMAX;) {
104 if (safe_realloc( buf, buflen )==NULL)
107 #if (GETHOSTBYNAME_R_NARGS < 6)
108 r = ((*result=gethostbyname_r( name, resbuf, *buf,\
109 buflen, herrno_ptr ))== NULL) ?\
112 r = gethostbyname_r( name, resbuf, *buf,
113 buflen, result, herrno_ptr );
116 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",\
119 #ifdef NETDB_INTERNAL
121 (*herrno_ptr==NETDB_INTERNAL) &&
131 #elif defined( LDAP_R_COMPILE )
132 # define NEED_COPY_HOSTENT
136 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
138 he = gethostbyname( name );
141 *herrno_ptr = h_errno;
143 } else if (copy_hostent( resbuf, buf, he )<0) {
151 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
155 *result = gethostbyname( name );
161 *herrno_ptr = h_errno;
167 int ldap_pvt_gethostbyaddr_a(
171 struct hostent *resbuf,
173 struct hostent **result,
176 #if defined( HAVE_GETHOSTBYADDR_R )
178 #if (GETHOSTBYADDR_R_NARGS > 8) || (GETHOSTBYADDR_R_NARGS < 7)
179 Ouch! gethostbyaddr_r() must have either 7 or 8 args
182 # undef NEED_SAFE_REALLOC
183 # define NEED_SAFE_REALLOC
187 for(;buflen<BUFMAX;) {
188 if (safe_realloc( buf, buflen )==NULL)
190 #if (GETHOSTBYADDR_R_NARGS < 8)
191 r = ((*result=gethostbyaddr_r( addr, len, type,
192 resbuf, *buf, buflen,
193 herrno_ptr )) == NULL) ?\
196 r = gethostbyaddr_r( addr, len, type,
197 resbuf, *buf, buflen,
198 result, herrno_ptr );
201 #ifdef NETDB_INTERNAL
203 (*herrno_ptr==NETDB_INTERNAL) &&
213 #elif defined( LDAP_R_COMPILE )
214 # undef NEED_COPY_HOSTENT
215 # define NEED_COPY_HOSTENT
219 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
221 he = gethostbyaddr( addr, len, type );
224 *herrno_ptr = h_errno;
226 } else if (copy_hostent( resbuf, buf, he )<0) {
234 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
237 #else /* gethostbyaddr() */
238 *result = gethostbyaddr( addr, len, type );
247 * ldap_pvt_init_utils() should be called before any other function.
250 void ldap_pvt_init_utils( void )
257 #ifdef LDAP_R_COMPILE
259 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
260 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
263 #if !defined( HAVE_GETHOSTBYNAME_R )
264 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
267 #if !defined( HAVE_GETHOSTBYADDR_R )
268 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
271 /* call other module init functions here... */
275 #if defined( NEED_COPY_HOSTENT )
276 # undef NEED_SAFE_REALLOC
277 #define NEED_SAFE_REALLOC
279 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
283 for( ; (*src) ; src++ ) {
284 len = strlen( *src ) + 1;
285 memcpy( buf, *src, len );
293 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
296 for( ; (*src) ; src++ ) {
297 memcpy( buf, *src, len );
305 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
317 /* calculate the size needed for the buffer */
318 name_len = strlen( src->h_name ) + 1;
320 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
321 total_alias_len += strlen( *p ) + 1;
325 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
328 total_addr_len = n_addr * src->h_length;
330 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
331 total_addr_len + total_alias_len + name_len;
333 if (safe_realloc( buf, total_len )) {
335 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
336 memcpy( res, src, sizeof( struct hostent ) );
337 /* first the name... */
338 memcpy( tbuf, src->h_name, name_len );
339 res->h_name = tbuf; tbuf+=name_len;
340 /* now the aliases */
342 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
344 /* finally the addresses */
345 res->h_addr_list = tp;
346 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
354 #if defined( NEED_SAFE_REALLOC )
355 static char *safe_realloc( char **buf, int len )
358 tmpbuf = realloc( *buf, len );