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 #if defined ( HAVE_STRSPN )
61 #define int_strspn strspn
63 static int int_strspn( const char *str, const char *delim )
67 for( pos=0; (*str) ; pos++,str++) {
69 for( p=delim; (*p) ; p++ ) {
80 #if defined( HAVE_STRPBRK )
81 #define int_strpbrk strpbrk
83 static char *int_strpbrk( const char *str, const char *accept )
86 for( ; (*str) ; str++ ) {
87 for( p=accept; (*p) ; p++) {
96 char *ldap_pvt_strtok( char *str, const char *delim, char **pos )
98 #if defined( HAVE_STRTOK_R ) || defined( HAVE_REENTRANT_FUNCTIONS )
99 return strtok_r(str, delim, pos);
110 /* skip any initial delimiters */
111 str += int_strspn( str, delim );
114 p = int_strpbrk( str, delim );
125 char *ldap_pvt_ctime( const time_t *tp, char *buf )
127 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
128 && defined( CTIME_R_NARGS )
129 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
130 choke me! nargs should have 2 or 3
131 # elif CTIME_R_NARGS > 2
132 return ctime_r(tp,buf,26);
134 return ctime_r(tp,buf);
137 # if defined( LDAP_INT_CTIME_MUTEX )
138 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
140 memcpy( buf, ctime(tp), 26 );
141 # if defined( LDAP_INT_CTIME_MUTEX )
142 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
148 #define BUFSTART 1024
149 #define BUFMAX (32*1024)
151 static char *safe_realloc( char **buf, int len );
152 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
154 int ldap_pvt_gethostbyname_a(
156 struct hostent *resbuf,
158 struct hostent **result,
161 #if defined( HAVE_GETHOSTBYNAME_R ) || defined( HAVE_REENTRANT_FUNCTIONS )
162 # define NEED_SAFE_REALLOC 1
166 for(;buflen<BUFMAX;) {
167 if (safe_realloc( buf, buflen )==NULL)
169 r = gethostbyname_r( name, resbuf, *buf,
170 buflen, result, herrno_ptr );
171 #ifdef NETDB_INTERNAL
173 (*herrno_ptr==NETDB_INTERNAL) &&
183 #elif defined( LDAP_INT_GETHOSTBYNAME_MUTEX )
184 # define NEED_COPY_HOSTENT
188 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
190 he = gethostbyname( name );
193 *herrno_ptr = h_errno;
195 } else if (copy_hostent( resbuf, buf, he )<0) {
203 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
207 *result = gethostbyname( name );
213 *herrno_ptr = h_errno;
219 int ldap_pvt_gethostbyaddr_a(
223 struct hostent *resbuf,
225 struct hostent **result,
228 #if defined( HAVE_GETHOSTBYADDR_R ) || defined( HAVE_REENTRANT_FUNCTIONS )
229 # undef NEED_SAFE_REALLOC
230 # define NEED_SAFE_REALLOC
234 for(;buflen<BUFMAX;) {
235 if (safe_realloc( buf, buflen )==NULL)
237 r = gethostbyaddr_r( addr, len, type,
238 resbuf, *buf, buflen,
239 result, herrno_ptr );
240 #ifdef NETDB_INTERNAL
242 (*herrno_ptr==NETDB_INTERNAL) &&
252 #elif defined( LDAP_INT_GETHOSTBYADDR_MUTEX )
253 # undef NEED_COPY_HOSTENT
254 # define NEED_COPY_HOSTENT
258 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
260 he = gethostbyaddr( addr, len, type );
263 *herrno_ptr = h_errno;
265 } else if (copy_hostent( resbuf, buf, he )<0) {
273 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
276 #else /* gethostbyaddr() */
277 *result = gethostbyaddr( addr, len, type );
286 * ldap_pvt_init_utils() should be called before any other function.
289 void ldap_pvt_init_utils( void )
295 #if defined( LDAP_INT_CTIME_MUTEX )
296 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex, NULL );
298 #if defined( LDAP_INT_GETHOSTBYNAME_MUTEX )
299 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex, NULL );
301 #if defined( LDAP_INT_GETHOSTBYADDR_MUTEX )
302 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex, NULL );
304 #if defined( LDAP_R_COMPILE )
305 /* call other module init functions here... */
309 #if defined( NEED_COPY_HOSTENT )
310 # undef NEED_SAFE_REALLOC
311 #define NEED_SAFE_REALLOC
313 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
317 for( ; (*src) ; src++ ) {
318 len = strlen( *src ) + 1;
319 memcpy( buf, *src, len );
327 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
330 for( ; (*src) ; src++ ) {
331 memcpy( buf, *src, len );
339 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
351 /* calculate the size needed for the buffer */
352 name_len = strlen( src->h_name ) + 1;
354 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
355 total_alias_len += strlen( *p ) + 1;
359 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
362 total_addr_len = n_addr * src->h_length;
364 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
365 total_addr_len + total_alias_len + name_len;
367 if (safe_realloc( buf, total_len )) {
369 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
370 memcpy( res, src, sizeof( struct hostent ) );
371 /* first the name... */
372 memcpy( tbuf, src->h_name, name_len );
373 res->h_name = tbuf; tbuf+=name_len;
374 /* now the aliases */
376 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
378 /* finally the addresses */
379 res->h_addr_list = tp;
380 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
388 #if defined( NEED_SAFE_REALLOC )
389 static char *safe_realloc( char **buf, int len )
392 tmpbuf = realloc( *buf, len );