3 * Copyright 1998-2003 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>
37 /* newer systems declare this in <netdb.h> for you, older ones don't.
38 * harmless to declare it again (unless defined by a macro).
43 #ifndef LDAP_R_COMPILE
44 # undef HAVE_REENTRANT_FUNCTIONS
46 # undef HAVE_GETHOSTBYNAME_R
47 # undef HAVE_GETHOSTBYADDR_R
50 # include <ldap_pvt_thread.h>
51 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
53 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
54 && defined( CTIME_R_NARGS )
57 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
60 # if defined(HAVE_GETHOSTBYNAME_R) && \
61 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
62 /* Don't know how to handle this version, pretend it's not there */
63 # undef HAVE_GETHOSTBYNAME_R
65 # if defined(HAVE_GETHOSTBYADDR_R) && \
66 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
67 /* Don't know how to handle this version, pretend it's not there */
68 # undef HAVE_GETHOSTBYADDR_R
70 #endif /* LDAP_R_COMPILE */
72 char *ldap_pvt_ctime( const time_t *tp, char *buf )
75 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
76 # error "CTIME_R_NARGS should be 2 or 3"
77 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
78 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
79 # elif CTIME_R_NARGS > 2
80 return ctime_r(tp,buf,26);
82 return ctime_r(tp,buf);
87 # ifdef LDAP_R_COMPILE
88 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
91 AC_MEMCPY( buf, ctime(tp), 26 );
93 # ifdef LDAP_R_COMPILE
94 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
101 #define BUFSTART (1024-32)
102 #define BUFMAX (32*1024-32)
104 static char *safe_realloc( char **buf, int len );
106 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
107 static int copy_hostent( struct hostent *res,
108 char **buf, struct hostent * src );
111 int ldap_pvt_gethostbyname_a(
113 struct hostent *resbuf,
115 struct hostent **result,
118 #if defined( HAVE_GETHOSTBYNAME_R )
120 # define NEED_SAFE_REALLOC 1
124 for(;buflen<BUFMAX;) {
125 if (safe_realloc( buf, buflen )==NULL)
128 #if (GETHOSTBYNAME_R_NARGS < 6)
129 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
130 r = (*result == NULL) ? -1 : 0;
132 r = gethostbyname_r( name, resbuf, *buf,
133 buflen, result, herrno_ptr );
136 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
139 #ifdef NETDB_INTERNAL
141 (*herrno_ptr==NETDB_INTERNAL) &&
151 #elif defined( LDAP_R_COMPILE )
152 # define NEED_COPY_HOSTENT
157 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
159 he = gethostbyname( name );
162 *herrno_ptr = h_errno;
164 } else if (copy_hostent( resbuf, buf, he )<0) {
172 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
177 *result = gethostbyname( name );
183 *herrno_ptr = h_errno;
191 hp_strerror( int err )
194 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
195 case TRY_AGAIN: return _("Host not found (server fail?)");
196 case NO_RECOVERY: return _("Non-recoverable failure");
197 case NO_DATA: return _("No data of requested type");
198 #ifdef NETDB_INTERNAL
199 case NETDB_INTERNAL: return STRERROR( errno );
202 return _("Unknown resolver error");
206 int ldap_pvt_get_hname(
207 const struct sockaddr *sa,
214 #if defined( HAVE_GETNAMEINFO )
216 #if defined( LDAP_R_COMPILE )
217 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
219 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
220 #if defined( LDAP_R_COMPILE )
221 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
223 if ( rc ) *err = AC_GAI_STRERROR( rc );
226 #else /* !HAVE_GETNAMEINFO */
229 struct hostent *hp = NULL;
230 #ifdef HAVE_GETHOSTBYADDR_R
232 int buflen=BUFSTART, h_errno;
237 if (sa->sa_family == AF_INET6) {
238 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
239 addr = (char *)&sin->sin6_addr;
240 alen = sizeof(sin->sin6_addr);
243 if (sa->sa_family == AF_INET) {
244 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
245 addr = (char *)&sin->sin_addr;
246 alen = sizeof(sin->sin_addr);
249 *err = (char *)hp_strerror( rc );
252 #if defined( HAVE_GETHOSTBYADDR_R )
253 for(;buflen<BUFMAX;) {
254 if (safe_realloc( &buf, buflen )==NULL) {
255 *err = (char *)STRERROR( ENOMEM );
258 #if (GETHOSTBYADDR_R_NARGS < 8)
259 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
260 &hb, buf, buflen, &h_errno );
261 rc = (hp == NULL) ? -1 : 0;
263 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
267 #ifdef NETDB_INTERNAL
269 (h_errno==NETDB_INTERNAL) &&
279 strncpy( name, hp->h_name, namelen );
281 *err = (char *)hp_strerror( h_errno );
284 #else /* HAVE_GETHOSTBYADDR_R */
286 #if defined( LDAP_R_COMPILE )
287 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
289 hp = gethostbyaddr( addr, alen, sa->sa_family );
291 strncpy( name, hp->h_name, namelen );
295 *err = (char *)hp_strerror( h_errno );
297 #if defined( LDAP_R_COMPILE )
298 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
301 #endif /* !HAVE_GETHOSTBYADDR_R */
303 #endif /* !HAVE_GETNAMEINFO */
306 int ldap_pvt_gethostbyaddr_a(
310 struct hostent *resbuf,
312 struct hostent **result,
315 #if defined( HAVE_GETHOSTBYADDR_R )
317 # undef NEED_SAFE_REALLOC
318 # define NEED_SAFE_REALLOC
322 for(;buflen<BUFMAX;) {
323 if (safe_realloc( buf, buflen )==NULL)
325 #if (GETHOSTBYADDR_R_NARGS < 8)
326 *result=gethostbyaddr_r( addr, len, type,
327 resbuf, *buf, buflen, herrno_ptr );
328 r = (*result == NULL) ? -1 : 0;
330 r = gethostbyaddr_r( addr, len, type,
331 resbuf, *buf, buflen,
332 result, herrno_ptr );
335 #ifdef NETDB_INTERNAL
337 (*herrno_ptr==NETDB_INTERNAL) &&
347 #elif defined( LDAP_R_COMPILE )
348 # undef NEED_COPY_HOSTENT
349 # define NEED_COPY_HOSTENT
354 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
356 he = gethostbyaddr( addr, len, type );
359 *herrno_ptr = h_errno;
361 } else if (copy_hostent( resbuf, buf, he )<0) {
369 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
373 #else /* gethostbyaddr() */
375 *result = gethostbyaddr( addr, len, type );
384 * ldap_int_utils_init() should be called before any other function.
387 void ldap_int_utils_init( void )
394 #ifdef LDAP_R_COMPILE
395 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
396 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
398 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
401 /* call other module init functions here... */
404 #if defined( NEED_COPY_HOSTENT )
405 # undef NEED_SAFE_REALLOC
406 #define NEED_SAFE_REALLOC
408 static char *cpy_aliases(
415 for( ; (*src) ; src++ ) {
416 len = strlen( *src ) + 1;
417 AC_MEMCPY( buf, *src, len );
425 static char *cpy_addresses(
432 for( ; (*src) ; src++ ) {
433 AC_MEMCPY( buf, *src, len );
441 static int copy_hostent(
444 struct hostent * src )
451 int total_alias_len=0;
456 /* calculate the size needed for the buffer */
457 name_len = strlen( src->h_name ) + 1;
459 if( src->h_aliases != NULL ) {
460 for( p = src->h_aliases; (*p) != NULL; p++ ) {
461 total_alias_len += strlen( *p ) + 1;
466 if( src->h_addr_list != NULL ) {
467 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
470 total_addr_len = n_addr * src->h_length;
473 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
474 total_addr_len + total_alias_len + name_len;
476 if (safe_realloc( buf, total_len )) {
478 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
479 AC_MEMCPY( res, src, sizeof( struct hostent ) );
480 /* first the name... */
481 AC_MEMCPY( tbuf, src->h_name, name_len );
482 res->h_name = tbuf; tbuf+=name_len;
483 /* now the aliases */
485 if ( src->h_aliases != NULL ) {
486 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
489 /* finally the addresses */
490 res->h_addr_list = tp;
491 if ( src->h_addr_list != NULL ) {
492 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
501 #if defined( NEED_SAFE_REALLOC )
502 static char *safe_realloc( char **buf, int len )
505 tmpbuf = LDAP_REALLOC( *buf, len );
513 char * ldap_pvt_get_fqdn( char *name )
516 char hostbuf[MAXHOSTNAMELEN+1];
517 struct hostent *hp, he_buf;
518 int rc, local_h_errno;
521 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
522 hostbuf[MAXHOSTNAMELEN] = '\0';
529 rc = ldap_pvt_gethostbyname_a( name,
530 &he_buf, &ha_buf, &hp, &local_h_errno );
532 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
533 fqdn = LDAP_STRDUP( name );
535 fqdn = LDAP_STRDUP( hp->h_name );
542 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
543 && !defined( HAVE_GAI_STRERROR )
544 char *ldap_pvt_gai_strerror (int code) {
549 #ifdef EAI_ADDRFAMILY
550 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
552 { EAI_AGAIN, N_("Temporary failure in name resolution") },
553 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
554 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
555 { EAI_FAMILY, N_("ai_family not supported") },
556 { EAI_MEMORY, N_("Memory allocation failure") },
558 { EAI_NODATA, N_("No address associated with hostname") },
560 { EAI_NONAME, N_("Name or service not known") },
561 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
562 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
563 { EAI_SYSTEM, N_("System error") },
569 for ( i = 0; values[i].msg != NULL; i++ ) {
570 if ( values[i].code == code ) {
571 return (char *) _(values[i].msg);
575 return _("Unknown error");