3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * util-int.c Various functions to replace missing threadsafe ones.
8 * Without the real *_r funcs, things will
9 * work, but might not be threadsafe.
11 * Written by Bart Hartgers.
13 * Copyright 1998, A. Hartgers, All rights reserved.
14 * This software is not subject to any license of Eindhoven University of
15 * Technology, since it was written in my spare time.
17 * Redistribution and use in source and binary forms are permitted only
18 * as authorized by the OpenLDAP Public License. A copy of this
19 * license is available at http://www.OpenLDAP.org/license.html or
20 * in file LICENSE in the top-level directory of the distribution.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include <ac/unistd.h>
36 #if defined( LDAP_R_COMPILE )
37 # include <ldap_pvt_thread.h>
39 # undef HAVE_REENTRANT_FUNCTIONS
42 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
43 && defined( CTIME_R_NARGS )
47 #if defined(HAVE_GETHOSTBYNAME_R) && \
48 (GETHOSTBYNAME_R_NARGS > 6 || GETHOSTBYNAME_R_NARGS < 5)
49 /* Don't know how to handle this version, pretend it's not there */
50 # undef HAVE_GETHOSTBYNAME_R
52 #if defined(HAVE_GETHOSTBYADDR_R) && \
53 (GETHOSTBYADDR_R_NARGS > 8 || GETHOSTBYADDR_R_NARGS < 7)
54 /* Don't know how to handle this version, pretend it's not there */
55 # undef HAVE_GETHOSTBYADDR_R
60 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
62 # if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
63 static ldap_pvt_thread_mutex_t ldap_int_gethostby_mutex;
65 # ifdef HAVE_RES_QUERY
66 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
68 #endif /* LDAP_R_COMPILE */
70 char *ldap_pvt_ctime( const time_t *tp, char *buf )
73 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
74 # error "CTIME_R_NARGS should be 2 or 3"
75 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
76 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
77 # elif CTIME_R_NARGS > 2
78 return ctime_r(tp,buf,26);
80 return ctime_r(tp,buf);
84 # ifdef LDAP_R_COMPILE
85 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
87 AC_MEMCPY( buf, ctime(tp), 26 );
88 # ifdef LDAP_R_COMPILE
89 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
96 #define BUFMAX (32*1024)
98 static char *safe_realloc( char **buf, int len );
100 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
101 # define NEED_COPY_HOSTENT
102 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
105 int ldap_pvt_gethostbyname_a(
107 struct hostent *resbuf,
109 struct hostent **result,
112 #if defined( HAVE_GETHOSTBYNAME_R )
114 # define NEED_SAFE_REALLOC 1
118 for(;buflen<BUFMAX;) {
119 if (safe_realloc( buf, buflen )==NULL)
122 #if (GETHOSTBYNAME_R_NARGS < 6)
123 r = ((*result=gethostbyname_r( name, resbuf, *buf,
124 buflen, herrno_ptr ))== NULL) ?
127 r = gethostbyname_r( name, resbuf, *buf,
128 buflen, result, herrno_ptr );
131 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
134 #ifdef NETDB_INTERNAL
136 (*herrno_ptr==NETDB_INTERNAL) &&
146 #elif defined( LDAP_R_COMPILE )
147 # define NEED_COPY_HOSTENT
152 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
154 he = gethostbyname( name );
157 *herrno_ptr = h_errno;
159 } else if (copy_hostent( resbuf, buf, he )<0) {
167 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
172 *result = gethostbyname( name );
178 *herrno_ptr = h_errno;
184 int ldap_pvt_gethostbyaddr_a(
188 struct hostent *resbuf,
190 struct hostent **result,
193 #if defined( HAVE_GETHOSTBYADDR_R )
195 # undef NEED_SAFE_REALLOC
196 # define NEED_SAFE_REALLOC
200 for(;buflen<BUFMAX;) {
201 if (safe_realloc( buf, buflen )==NULL)
203 #if (GETHOSTBYADDR_R_NARGS < 8)
204 r = ((*result=gethostbyaddr_r( addr, len, type,
205 resbuf, *buf, buflen,
206 herrno_ptr )) == NULL) ?
209 r = gethostbyaddr_r( addr, len, type,
210 resbuf, *buf, buflen,
211 result, herrno_ptr );
214 #ifdef NETDB_INTERNAL
216 (*herrno_ptr==NETDB_INTERNAL) &&
226 #elif defined( LDAP_R_COMPILE )
227 # undef NEED_COPY_HOSTENT
228 # define NEED_COPY_HOSTENT
233 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
235 he = gethostbyaddr( addr, len, type );
238 *herrno_ptr = h_errno;
240 } else if (copy_hostent( resbuf, buf, he )<0) {
248 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
251 #else /* gethostbyaddr() */
253 *result = gethostbyaddr( addr, len, type );
262 * ldap_int_utils_init() should be called before any other function.
265 void ldap_int_utils_init( void )
272 #ifdef LDAP_R_COMPILE
274 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
275 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
278 #if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
279 ldap_pvt_thread_mutex_init( &ldap_int_gethostby_mutex );
282 #ifdef HAVE_RES_QUERY
283 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
286 /* call other module init functions here... */
290 #if defined( NEED_COPY_HOSTENT )
291 # undef NEED_SAFE_REALLOC
292 #define NEED_SAFE_REALLOC
294 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
298 for( ; (*src) ; src++ ) {
299 len = strlen( *src ) + 1;
300 AC_MEMCPY( buf, *src, len );
308 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
311 for( ; (*src) ; src++ ) {
312 AC_MEMCPY( buf, *src, len );
320 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
327 int total_alias_len=0;
332 /* calculate the size needed for the buffer */
333 name_len = strlen( src->h_name ) + 1;
335 if( src->h_aliases != NULL ) {
336 for( p = src->h_aliases; (*p) != NULL; p++ ) {
337 total_alias_len += strlen( *p ) + 1;
342 if( src->h_addr_list != NULL ) {
343 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
346 total_addr_len = n_addr * src->h_length;
349 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
350 total_addr_len + total_alias_len + name_len;
352 if (safe_realloc( buf, total_len )) {
354 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
355 AC_MEMCPY( res, src, sizeof( struct hostent ) );
356 /* first the name... */
357 AC_MEMCPY( tbuf, src->h_name, name_len );
358 res->h_name = tbuf; tbuf+=name_len;
359 /* now the aliases */
361 if ( src->h_aliases != NULL ) {
362 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
365 /* finally the addresses */
366 res->h_addr_list = tp;
367 if ( src->h_addr_list != NULL ) {
368 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
377 #if defined( NEED_SAFE_REALLOC )
378 static char *safe_realloc( char **buf, int len )
381 tmpbuf = LDAP_REALLOC( *buf, len );
389 char * ldap_pvt_get_fqdn( char *name )
392 char hostbuf[MAXHOSTNAMELEN+1];
393 struct hostent *hp, he_buf;
394 int rc, local_h_errno;
397 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
398 hostbuf[MAXHOSTNAMELEN] = '\0';
405 rc = ldap_pvt_gethostbyname_a( name,
406 &he_buf, &ha_buf, &hp, &local_h_errno );
408 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
409 fqdn = LDAP_STRDUP( name );
411 fqdn = LDAP_STRDUP( hp->h_name );