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 && defined(CTIME_R_RETURNS_INT)
74 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
75 # elif CTIME_R_NARGS > 2
76 return ctime_r(tp,buf,26);
78 return ctime_r(tp,buf);
82 # ifdef LDAP_R_COMPILE
83 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
85 memcpy( buf, ctime(tp), 26 );
86 # ifdef LDAP_R_COMPILE
87 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
94 #define BUFMAX (32*1024)
96 static char *safe_realloc( char **buf, int len );
97 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
99 int ldap_pvt_gethostbyname_a(
101 struct hostent *resbuf,
103 struct hostent **result,
106 #if defined( HAVE_GETHOSTBYNAME_R )
108 # define NEED_SAFE_REALLOC 1
112 for(;buflen<BUFMAX;) {
113 if (safe_realloc( buf, buflen )==NULL)
116 #if (GETHOSTBYNAME_R_NARGS < 6)
117 r = ((*result=gethostbyname_r( name, resbuf, *buf,\
118 buflen, herrno_ptr ))== NULL) ?\
121 r = gethostbyname_r( name, resbuf, *buf,
122 buflen, result, herrno_ptr );
125 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",\
128 #ifdef NETDB_INTERNAL
130 (*herrno_ptr==NETDB_INTERNAL) &&
140 #elif defined( LDAP_R_COMPILE )
141 # define NEED_COPY_HOSTENT
145 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
147 he = gethostbyname( name );
150 *herrno_ptr = h_errno;
152 } else if (copy_hostent( resbuf, buf, he )<0) {
160 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
164 *result = gethostbyname( name );
170 *herrno_ptr = h_errno;
176 int ldap_pvt_gethostbyaddr_a(
180 struct hostent *resbuf,
182 struct hostent **result,
185 #if defined( HAVE_GETHOSTBYADDR_R )
187 # undef NEED_SAFE_REALLOC
188 # define NEED_SAFE_REALLOC
192 for(;buflen<BUFMAX;) {
193 if (safe_realloc( buf, buflen )==NULL)
195 #if (GETHOSTBYADDR_R_NARGS < 8)
196 r = ((*result=gethostbyaddr_r( addr, len, type,
197 resbuf, *buf, buflen,
198 herrno_ptr )) == NULL) ?\
201 r = gethostbyaddr_r( addr, len, type,
202 resbuf, *buf, buflen,
203 result, herrno_ptr );
206 #ifdef NETDB_INTERNAL
208 (*herrno_ptr==NETDB_INTERNAL) &&
218 #elif defined( LDAP_R_COMPILE )
219 # undef NEED_COPY_HOSTENT
220 # define NEED_COPY_HOSTENT
224 ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
226 he = gethostbyaddr( addr, len, type );
229 *herrno_ptr = h_errno;
231 } else if (copy_hostent( resbuf, buf, he )<0) {
239 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
242 #else /* gethostbyaddr() */
243 *result = gethostbyaddr( addr, len, type );
252 * ldap_pvt_init_utils() should be called before any other function.
255 void ldap_pvt_init_utils( void )
262 #ifdef LDAP_R_COMPILE
264 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
265 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
268 #if !defined( HAVE_GETHOSTBYNAME_R )
269 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
272 #if !defined( HAVE_GETHOSTBYADDR_R )
273 ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
276 /* call other module init functions here... */
280 #if defined( NEED_COPY_HOSTENT )
281 # undef NEED_SAFE_REALLOC
282 #define NEED_SAFE_REALLOC
284 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
288 for( ; (*src) ; src++ ) {
289 len = strlen( *src ) + 1;
290 memcpy( buf, *src, len );
298 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
301 for( ; (*src) ; src++ ) {
302 memcpy( buf, *src, len );
310 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
322 /* calculate the size needed for the buffer */
323 name_len = strlen( src->h_name ) + 1;
325 for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
326 total_alias_len += strlen( *p ) + 1;
330 for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
333 total_addr_len = n_addr * src->h_length;
335 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
336 total_addr_len + total_alias_len + name_len;
338 if (safe_realloc( buf, total_len )) {
340 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
341 memcpy( res, src, sizeof( struct hostent ) );
342 /* first the name... */
343 memcpy( tbuf, src->h_name, name_len );
344 res->h_name = tbuf; tbuf+=name_len;
345 /* now the aliases */
347 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
349 /* finally the addresses */
350 res->h_addr_list = tp;
351 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
359 #if defined( NEED_SAFE_REALLOC )
360 static char *safe_realloc( char **buf, int len )
363 tmpbuf = realloc( *buf, len );