3 * Copyright 1998-2002 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 );
95 #define BUFSTART (1024-32)
96 #define BUFMAX (32*1024-32)
98 static char *safe_realloc( char **buf, int len );
100 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
101 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
104 int ldap_pvt_gethostbyname_a(
106 struct hostent *resbuf,
108 struct hostent **result,
111 #if defined( HAVE_GETHOSTBYNAME_R )
113 # define NEED_SAFE_REALLOC 1
117 for(;buflen<BUFMAX;) {
118 if (safe_realloc( buf, buflen )==NULL)
121 #if (GETHOSTBYNAME_R_NARGS < 6)
122 r = ((*result=gethostbyname_r( name, resbuf, *buf,
123 buflen, herrno_ptr ))== NULL) ?
126 r = gethostbyname_r( name, resbuf, *buf,
127 buflen, result, herrno_ptr );
130 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
133 #ifdef NETDB_INTERNAL
135 (*herrno_ptr==NETDB_INTERNAL) &&
145 #elif defined( LDAP_R_COMPILE )
146 # define NEED_COPY_HOSTENT
151 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
153 he = gethostbyname( name );
156 *herrno_ptr = h_errno;
158 } else if (copy_hostent( resbuf, buf, he )<0) {
166 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
171 *result = gethostbyname( name );
177 *herrno_ptr = h_errno;
183 int ldap_pvt_gethostbyaddr_a(
187 struct hostent *resbuf,
189 struct hostent **result,
192 #if defined( HAVE_GETHOSTBYADDR_R )
194 # undef NEED_SAFE_REALLOC
195 # define NEED_SAFE_REALLOC
199 for(;buflen<BUFMAX;) {
200 if (safe_realloc( buf, buflen )==NULL)
202 #if (GETHOSTBYADDR_R_NARGS < 8)
203 r = ((*result=gethostbyaddr_r( addr, len, type,
204 resbuf, *buf, buflen,
205 herrno_ptr )) == NULL) ?
208 r = gethostbyaddr_r( addr, len, type,
209 resbuf, *buf, buflen,
210 result, herrno_ptr );
213 #ifdef NETDB_INTERNAL
215 (*herrno_ptr==NETDB_INTERNAL) &&
225 #elif defined( LDAP_R_COMPILE )
226 # undef NEED_COPY_HOSTENT
227 # define NEED_COPY_HOSTENT
232 ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
234 he = gethostbyaddr( addr, len, type );
237 *herrno_ptr = h_errno;
239 } else if (copy_hostent( resbuf, buf, he )<0) {
247 ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
250 #else /* gethostbyaddr() */
252 *result = gethostbyaddr( addr, len, type );
261 * ldap_int_utils_init() should be called before any other function.
264 void ldap_int_utils_init( void )
271 #ifdef LDAP_R_COMPILE
273 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
274 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
277 #if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
278 ldap_pvt_thread_mutex_init( &ldap_int_gethostby_mutex );
281 #ifdef HAVE_RES_QUERY
282 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
285 /* call other module init functions here... */
289 #if defined( NEED_COPY_HOSTENT )
290 # undef NEED_SAFE_REALLOC
291 #define NEED_SAFE_REALLOC
293 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
297 for( ; (*src) ; src++ ) {
298 len = strlen( *src ) + 1;
299 AC_MEMCPY( buf, *src, len );
307 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
310 for( ; (*src) ; src++ ) {
311 AC_MEMCPY( buf, *src, len );
319 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
326 int total_alias_len=0;
331 /* calculate the size needed for the buffer */
332 name_len = strlen( src->h_name ) + 1;
334 if( src->h_aliases != NULL ) {
335 for( p = src->h_aliases; (*p) != NULL; p++ ) {
336 total_alias_len += strlen( *p ) + 1;
341 if( src->h_addr_list != NULL ) {
342 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
345 total_addr_len = n_addr * src->h_length;
348 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
349 total_addr_len + total_alias_len + name_len;
351 if (safe_realloc( buf, total_len )) {
353 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
354 AC_MEMCPY( res, src, sizeof( struct hostent ) );
355 /* first the name... */
356 AC_MEMCPY( tbuf, src->h_name, name_len );
357 res->h_name = tbuf; tbuf+=name_len;
358 /* now the aliases */
360 if ( src->h_aliases != NULL ) {
361 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
364 /* finally the addresses */
365 res->h_addr_list = tp;
366 if ( src->h_addr_list != NULL ) {
367 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
376 #if defined( NEED_SAFE_REALLOC )
377 static char *safe_realloc( char **buf, int len )
380 tmpbuf = LDAP_REALLOC( *buf, len );
388 char * ldap_pvt_get_fqdn( char *name )
391 char hostbuf[MAXHOSTNAMELEN+1];
392 struct hostent *hp, he_buf;
393 int rc, local_h_errno;
396 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
397 hostbuf[MAXHOSTNAMELEN] = '\0';
404 rc = ldap_pvt_gethostbyname_a( name,
405 &he_buf, &ha_buf, &hp, &local_h_errno );
407 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
408 fqdn = LDAP_STRDUP( name );
410 fqdn = LDAP_STRDUP( hp->h_name );
417 #if defined( HAVE_GETADDRINFO ) && !defined( HAVE_GAI_STRERROR )
418 char *ldap_pvt_gai_strerror (int code) {
423 #ifdef EAI_ADDRFAMILY
424 { EAI_ADDRFAMILY, "Address family for hostname not supported" },
426 { EAI_AGAIN, "Temporary failure in name resolution" },
427 { EAI_BADFLAGS, "Bad value for ai_flags" },
428 { EAI_FAIL, "Non-recoverable failure in name resolution" },
429 { EAI_FAMILY, "ai_family not supported" },
430 { EAI_MEMORY, "Memory allocation failure" },
432 { EAI_NODATA, "No address associated with hostname" },
434 { EAI_NONAME, "Name or service not known" },
435 { EAI_SERVICE, "Servname not supported for ai_socktype" },
436 { EAI_SOCKTYPE, "ai_socktype not supported" },
437 { EAI_SYSTEM, "System error" },
443 for ( i = 0; values[i].msg != NULL; i++ ) {
444 if ( values[i].code == code ) {
445 return (char *) values[i].msg;
449 return "Unknown error";