Back to index

openldap  2.4.31
util-int.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1998 A. Hartgers.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Bart Hartgers for inclusion in
00018  * OpenLDAP Software.
00019  */
00020 
00021 /*
00022  * util-int.c Various functions to replace missing threadsafe ones.
00023  *                          Without the real *_r funcs, things will
00024  *                          work, but might not be threadsafe. 
00025  */
00026 
00027 #include "portable.h"
00028 
00029 #include <ac/stdlib.h>
00030 
00031 #include <ac/errno.h>
00032 #include <ac/socket.h>
00033 #include <ac/string.h>
00034 #include <ac/time.h>
00035 #include <ac/unistd.h>
00036 
00037 #include "ldap-int.h"
00038 
00039 #ifndef h_errno
00040 /* newer systems declare this in <netdb.h> for you, older ones don't.
00041  * harmless to declare it again (unless defined by a macro).
00042  */
00043 extern int h_errno;
00044 #endif
00045 
00046 #ifdef HAVE_HSTRERROR
00047 # define HSTRERROR(e)       hstrerror(e)
00048 #else
00049 # define HSTRERROR(e)       hp_strerror(e)
00050 #endif
00051 
00052 #ifndef LDAP_R_COMPILE
00053 # undef HAVE_REENTRANT_FUNCTIONS
00054 # undef HAVE_CTIME_R
00055 # undef HAVE_GETHOSTBYNAME_R
00056 # undef HAVE_GETHOSTBYADDR_R
00057 
00058 #else
00059 # include <ldap_pvt_thread.h>
00060   ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
00061 
00062 # if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
00063         && defined( CTIME_R_NARGS )
00064 #   define USE_CTIME_R
00065 # else
00066        static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
00067 # endif
00068 
00069 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
00070 
00071 #ifdef LDAP_DEVEL
00072        /* to be released with 2.5 */
00073 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
00074        /* we use the same mutex for gmtime(3) and localtime(3)
00075         * because implementations may use the same buffer
00076         * for both functions */
00077        static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
00078 #endif
00079 #else /* ! LDAP_DEVEL */
00080        ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
00081 #endif /* ! LDAP_DEVEL */
00082 
00083 # if defined(HAVE_GETHOSTBYNAME_R) && \
00084        (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
00085        /* Don't know how to handle this version, pretend it's not there */
00086 #      undef HAVE_GETHOSTBYNAME_R
00087 # endif
00088 # if defined(HAVE_GETHOSTBYADDR_R) && \
00089        (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
00090        /* Don't know how to handle this version, pretend it's not there */
00091 #      undef HAVE_GETHOSTBYADDR_R
00092 # endif
00093 #endif /* LDAP_R_COMPILE */
00094 
00095 char *ldap_pvt_ctime( const time_t *tp, char *buf )
00096 {
00097 #ifdef USE_CTIME_R
00098 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
00099 #      error "CTIME_R_NARGS should be 2 or 3"
00100 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
00101        return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
00102 # elif CTIME_R_NARGS > 2
00103        return ctime_r(tp,buf,26);
00104 # else
00105        return ctime_r(tp,buf);
00106 # endif         
00107 
00108 #else
00109 
00110        LDAP_MUTEX_LOCK( &ldap_int_ctime_mutex );
00111        AC_MEMCPY( buf, ctime(tp), 26 );
00112        LDAP_MUTEX_UNLOCK( &ldap_int_ctime_mutex );
00113 
00114        return buf;
00115 #endif 
00116 }
00117 
00118 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
00119 int
00120 ldap_pvt_gmtime_lock( void )
00121 {
00122 # ifndef LDAP_R_COMPILE
00123        return 0;
00124 # else /* LDAP_R_COMPILE */
00125        return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
00126 # endif /* LDAP_R_COMPILE */
00127 }
00128 
00129 int
00130 ldap_pvt_gmtime_unlock( void )
00131 {
00132 # ifndef LDAP_R_COMPILE
00133        return 0;
00134 # else /* LDAP_R_COMPILE */
00135        return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
00136 # endif /* LDAP_R_COMPILE */
00137 }
00138 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
00139 
00140 #ifndef USE_GMTIME_R
00141 struct tm *
00142 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
00143 {
00144        struct tm *tm_ptr;
00145 
00146        LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
00147        tm_ptr = gmtime( timep );
00148        if ( tm_ptr == NULL ) {
00149               result = NULL;
00150 
00151        } else {
00152               *result = *tm_ptr;
00153        }
00154        LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
00155 
00156        return result;
00157 }
00158 #endif /* !USE_GMTIME_R */
00159 
00160 #ifndef USE_LOCALTIME_R
00161 struct tm *
00162 ldap_pvt_localtime( const time_t *timep, struct tm *result )
00163 {
00164        struct tm *tm_ptr;
00165 
00166        LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
00167        tm_ptr = localtime( timep );
00168        if ( tm_ptr == NULL ) {
00169               result = NULL;
00170 
00171        } else {
00172               *result = *tm_ptr;
00173        }
00174        LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
00175 
00176        return result;
00177 }
00178 #endif /* !USE_LOCALTIME_R */
00179 
00180 /* return a broken out time, with microseconds
00181  * Must be mutex-protected.
00182  */
00183 #ifdef _WIN32
00184 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
00185  * also need to use a high resolution timer to get microseconds.
00186  * This is pretty clunky.
00187  */
00188 void
00189 ldap_pvt_gettime( struct lutil_tm *tm )
00190 {
00191        static LARGE_INTEGER cFreq;
00192        static LARGE_INTEGER prevCount;
00193        static int subs;
00194        static int offset;
00195        LARGE_INTEGER count;
00196        SYSTEMTIME st;
00197 
00198        GetSystemTime( &st );
00199        QueryPerformanceCounter( &count );
00200 
00201        /* It shouldn't ever go backwards, but multiple CPUs might
00202         * be able to hit in the same tick.
00203         */
00204        if ( count.QuadPart <= prevCount.QuadPart ) {
00205               subs++;
00206        } else {
00207               subs = 0;
00208               prevCount = count;
00209        }
00210 
00211        /* We assume Windows has at least a vague idea of
00212         * when a second begins. So we align our microsecond count
00213         * with the Windows millisecond count using this offset.
00214         * We retain the submillisecond portion of our own count.
00215         *
00216         * Note - this also assumes that the relationship between
00217         * the PerformanceCouunter and SystemTime stays constant;
00218         * that assumption breaks if the SystemTime is adjusted by
00219         * an external action.
00220         */
00221        if ( !cFreq.QuadPart ) {
00222               long long t;
00223               int usec;
00224               QueryPerformanceFrequency( &cFreq );
00225 
00226               /* just get sub-second portion of counter */
00227               t = count.QuadPart % cFreq.QuadPart;
00228 
00229               /* convert to microseconds */
00230               t *= 1000000;
00231               usec = t / cFreq.QuadPart;
00232 
00233               offset = usec - st.wMilliseconds * 1000;
00234        }
00235 
00236        tm->tm_usub = subs;
00237 
00238        /* convert to microseconds */
00239        count.QuadPart %= cFreq.QuadPart;
00240        count.QuadPart *= 1000000;
00241        count.QuadPart /= cFreq.QuadPart;
00242        count.QuadPart -= offset;
00243 
00244        tm->tm_usec = count.QuadPart % 1000000;
00245        if ( tm->tm_usec < 0 )
00246               tm->tm_usec += 1000000;
00247 
00248        /* any difference larger than microseconds is
00249         * already reflected in st
00250         */
00251 
00252        tm->tm_sec = st.wSecond;
00253        tm->tm_min = st.wMinute;
00254        tm->tm_hour = st.wHour;
00255        tm->tm_mday = st.wDay;
00256        tm->tm_mon = st.wMonth - 1;
00257        tm->tm_year = st.wYear - 1900;
00258 }
00259 #else
00260 void
00261 ldap_pvt_gettime( struct lutil_tm *ltm )
00262 {
00263        struct timeval tv;
00264        static struct timeval prevTv;
00265        static int subs;
00266 
00267        struct tm tm;
00268        time_t t;
00269 
00270        gettimeofday( &tv, NULL );
00271        t = tv.tv_sec;
00272 
00273        if ( tv.tv_sec < prevTv.tv_sec
00274               || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
00275               subs++;
00276        } else {
00277               subs = 0;
00278               prevTv = tv;
00279        }
00280 
00281        ltm->tm_usub = subs;
00282 
00283        ldap_pvt_gmtime( &t, &tm );
00284 
00285        ltm->tm_sec = tm.tm_sec;
00286        ltm->tm_min = tm.tm_min;
00287        ltm->tm_hour = tm.tm_hour;
00288        ltm->tm_mday = tm.tm_mday;
00289        ltm->tm_mon = tm.tm_mon;
00290        ltm->tm_year = tm.tm_year;
00291        ltm->tm_usec = tv.tv_usec;
00292 }
00293 #endif
00294 
00295 size_t
00296 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
00297 {
00298        struct lutil_tm tm;
00299        int n;
00300 
00301        ldap_pvt_gettime( &tm );
00302 
00303        n = snprintf( buf, len,
00304               "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
00305               tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
00306               tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
00307 
00308        if( n < 0 ) return 0;
00309        return ( (size_t) n < len ) ? n : 0;
00310 }
00311 
00312 #define BUFSTART (1024-32)
00313 #define BUFMAX (32*1024-32)
00314 
00315 #if defined(LDAP_R_COMPILE)
00316 static char *safe_realloc( char **buf, int len );
00317 
00318 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
00319 static int copy_hostent( struct hostent *res,
00320        char **buf, struct hostent * src );
00321 #endif
00322 #endif
00323 
00324 int ldap_pvt_gethostbyname_a(
00325        const char *name, 
00326        struct hostent *resbuf,
00327        char **buf,
00328        struct hostent **result,
00329        int *herrno_ptr )
00330 {
00331 #if defined( HAVE_GETHOSTBYNAME_R )
00332 
00333 # define NEED_SAFE_REALLOC 1   
00334        int r=-1;
00335        int buflen=BUFSTART;
00336        *buf = NULL;
00337        for(;buflen<BUFMAX;) {
00338               if (safe_realloc( buf, buflen )==NULL)
00339                      return r;
00340 
00341 #if (GETHOSTBYNAME_R_NARGS < 6)
00342               *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
00343               r = (*result == NULL) ?  -1 : 0;
00344 #else
00345               r = gethostbyname_r( name, resbuf, *buf,
00346                      buflen, result, herrno_ptr );
00347 #endif
00348 
00349               Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
00350                      name, r, 0 );
00351 
00352 #ifdef NETDB_INTERNAL
00353               if ((r<0) &&
00354                      (*herrno_ptr==NETDB_INTERNAL) &&
00355                      (errno==ERANGE))
00356               {
00357                      buflen*=2;
00358                      continue;
00359               }
00360 #endif
00361               return r;
00362        }
00363        return -1;
00364 #elif defined( LDAP_R_COMPILE )
00365 # define NEED_COPY_HOSTENT   
00366        struct hostent *he;
00367        int    retval;
00368        *buf = NULL;
00369        
00370        LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
00371        
00372        he = gethostbyname( name );
00373        
00374        if (he==NULL) {
00375               *herrno_ptr = h_errno;
00376               retval = -1;
00377        } else if (copy_hostent( resbuf, buf, he )<0) {
00378               *herrno_ptr = -1;
00379               retval = -1;
00380        } else {
00381               *result = resbuf;
00382               retval = 0;
00383        }
00384        
00385        LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
00386        
00387        return retval;
00388 #else  
00389        *buf = NULL;
00390        *result = gethostbyname( name );
00391 
00392        if (*result!=NULL) {
00393               return 0;
00394        }
00395 
00396        *herrno_ptr = h_errno;
00397        
00398        return -1;
00399 #endif 
00400 }
00401 
00402 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
00403 static const char *
00404 hp_strerror( int err )
00405 {
00406        switch (err) {
00407        case HOST_NOT_FOUND: return _("Host not found (authoritative)");
00408        case TRY_AGAIN:                    return _("Host not found (server fail?)");
00409        case NO_RECOVERY:           return _("Non-recoverable failure");
00410        case NO_DATA:               return _("No data of requested type");
00411 #ifdef NETDB_INTERNAL
00412        case NETDB_INTERNAL: return STRERROR( errno );
00413 #endif
00414        }
00415        return _("Unknown resolver error");
00416 }
00417 #endif
00418 
00419 int ldap_pvt_get_hname(
00420        const struct sockaddr *sa,
00421        int len,
00422        char *name,
00423        int namelen,
00424        char **err )
00425 {
00426        int rc;
00427 #if defined( HAVE_GETNAMEINFO )
00428 
00429        LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
00430        rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
00431        LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
00432        if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
00433        return rc;
00434 
00435 #else /* !HAVE_GETNAMEINFO */
00436        char *addr;
00437        int alen;
00438        struct hostent *hp = NULL;
00439 #ifdef HAVE_GETHOSTBYADDR_R
00440        struct hostent hb;
00441        int buflen=BUFSTART, h_errno;
00442        char *buf=NULL;
00443 #endif
00444 
00445 #ifdef LDAP_PF_INET6
00446        if (sa->sa_family == AF_INET6) {
00447               struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
00448               addr = (char *)&sin->sin6_addr;
00449               alen = sizeof(sin->sin6_addr);
00450        } else
00451 #endif
00452        if (sa->sa_family == AF_INET) {
00453               struct sockaddr_in *sin = (struct sockaddr_in *)sa;
00454               addr = (char *)&sin->sin_addr;
00455               alen = sizeof(sin->sin_addr);
00456        } else {
00457               rc = NO_RECOVERY;
00458               *err = (char *)HSTRERROR( rc );
00459               return rc;
00460        }
00461 #if defined( HAVE_GETHOSTBYADDR_R )
00462        for(;buflen<BUFMAX;) {
00463               if (safe_realloc( &buf, buflen )==NULL) {
00464                      *err = (char *)STRERROR( ENOMEM );
00465                      return ENOMEM;
00466               }
00467 #if (GETHOSTBYADDR_R_NARGS < 8)
00468               hp=gethostbyaddr_r( addr, alen, sa->sa_family,
00469                      &hb, buf, buflen, &h_errno );
00470               rc = (hp == NULL) ? -1 : 0;
00471 #else
00472               rc = gethostbyaddr_r( addr, alen, sa->sa_family,
00473                      &hb, buf, buflen, 
00474                      &hp, &h_errno );
00475 #endif
00476 #ifdef NETDB_INTERNAL
00477               if ((rc<0) &&
00478                      (h_errno==NETDB_INTERNAL) &&
00479                      (errno==ERANGE))
00480               {
00481                      buflen*=2;
00482                      continue;
00483               }
00484 #endif
00485               break;
00486        }
00487        if (hp) {
00488               strncpy( name, hp->h_name, namelen );
00489        } else {
00490               *err = (char *)HSTRERROR( h_errno );
00491        }
00492        LDAP_FREE(buf);
00493 #else /* HAVE_GETHOSTBYADDR_R */
00494 
00495        LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
00496        hp = gethostbyaddr( addr, alen, sa->sa_family );
00497        if (hp) {
00498               strncpy( name, hp->h_name, namelen );
00499               rc = 0;
00500        } else {
00501               rc = h_errno;
00502               *err = (char *)HSTRERROR( h_errno );
00503        }
00504        LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
00505 
00506 #endif /* !HAVE_GETHOSTBYADDR_R */
00507        return rc;
00508 #endif /* !HAVE_GETNAMEINFO */
00509 }
00510 
00511 int ldap_pvt_gethostbyaddr_a(
00512        const char *addr,
00513        int len,
00514        int type,
00515        struct hostent *resbuf,
00516        char **buf,
00517        struct hostent **result,
00518        int *herrno_ptr )
00519 {
00520 #if defined( HAVE_GETHOSTBYADDR_R )
00521 
00522 # undef NEED_SAFE_REALLOC
00523 # define NEED_SAFE_REALLOC   
00524        int r=-1;
00525        int buflen=BUFSTART;
00526        *buf = NULL;   
00527        for(;buflen<BUFMAX;) {
00528               if (safe_realloc( buf, buflen )==NULL)
00529                      return r;
00530 #if (GETHOSTBYADDR_R_NARGS < 8)
00531               *result=gethostbyaddr_r( addr, len, type,
00532                      resbuf, *buf, buflen, herrno_ptr );
00533               r = (*result == NULL) ? -1 : 0;
00534 #else
00535               r = gethostbyaddr_r( addr, len, type,
00536                      resbuf, *buf, buflen, 
00537                      result, herrno_ptr );
00538 #endif
00539 
00540 #ifdef NETDB_INTERNAL
00541               if ((r<0) &&
00542                      (*herrno_ptr==NETDB_INTERNAL) &&
00543                      (errno==ERANGE))
00544               {
00545                      buflen*=2;
00546                      continue;
00547               }
00548 #endif
00549               return r;
00550        }
00551        return -1;
00552 #elif defined( LDAP_R_COMPILE )
00553 # undef NEED_COPY_HOSTENT
00554 # define NEED_COPY_HOSTENT   
00555        struct hostent *he;
00556        int    retval;
00557        *buf = NULL;   
00558        
00559        LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
00560        he = gethostbyaddr( addr, len, type );
00561        
00562        if (he==NULL) {
00563               *herrno_ptr = h_errno;
00564               retval = -1;
00565        } else if (copy_hostent( resbuf, buf, he )<0) {
00566               *herrno_ptr = -1;
00567               retval = -1;
00568        } else {
00569               *result = resbuf;
00570               retval = 0;
00571        }
00572        LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
00573        
00574        return retval;
00575 
00576 #else /* gethostbyaddr() */
00577        *buf = NULL;   
00578        *result = gethostbyaddr( addr, len, type );
00579 
00580        if (*result!=NULL) {
00581               return 0;
00582        }
00583        return -1;
00584 #endif 
00585 }
00586 /* 
00587  * ldap_int_utils_init() should be called before any other function.
00588  */
00589 
00590 void ldap_int_utils_init( void )
00591 {
00592        static int done=0;
00593        if (done)
00594          return;
00595        done=1;
00596 
00597 #ifdef LDAP_R_COMPILE
00598 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
00599        ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
00600 #endif
00601 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
00602        ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
00603 #endif
00604        ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
00605 
00606 #ifdef HAVE_CYRUS_SASL
00607        ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
00608 #endif
00609 #ifdef HAVE_GSSAPI
00610        ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
00611 #endif
00612 #endif
00613 
00614        /* call other module init functions here... */
00615 }
00616 
00617 #if defined( NEED_COPY_HOSTENT )
00618 # undef NEED_SAFE_REALLOC
00619 #define NEED_SAFE_REALLOC
00620 
00621 static char *cpy_aliases(
00622        char ***tgtio,
00623        char *buf,
00624        char **src )
00625 {
00626        int len;
00627        char **tgt=*tgtio;
00628        for( ; (*src) ; src++ ) {
00629               len = strlen( *src ) + 1;
00630               AC_MEMCPY( buf, *src, len );
00631               *tgt++=buf;
00632               buf+=len;
00633        }
00634        *tgtio=tgt;   
00635        return buf;
00636 }
00637 
00638 static char *cpy_addresses(
00639        char ***tgtio,
00640        char *buf,
00641        char **src,
00642        int len )
00643 {
00644        char **tgt=*tgtio;
00645        for( ; (*src) ; src++ ) {
00646               AC_MEMCPY( buf, *src, len );
00647               *tgt++=buf;
00648               buf+=len;
00649        }
00650        *tgtio=tgt;      
00651        return buf;
00652 }
00653 
00654 static int copy_hostent(
00655        struct hostent *res,
00656        char **buf,
00657        struct hostent * src )
00658 {
00659        char   **p;
00660        char   **tp;
00661        char   *tbuf;
00662        int    name_len;
00663        int    n_alias=0;
00664        int    total_alias_len=0;
00665        int    n_addr=0;
00666        int    total_addr_len=0;
00667        int    total_len;
00668          
00669        /* calculate the size needed for the buffer */
00670        name_len = strlen( src->h_name ) + 1;
00671        
00672        if( src->h_aliases != NULL ) {
00673               for( p = src->h_aliases; (*p) != NULL; p++ ) {
00674                      total_alias_len += strlen( *p ) + 1;
00675                      n_alias++; 
00676               }
00677        }
00678 
00679        if( src->h_addr_list != NULL ) {
00680               for( p = src->h_addr_list; (*p) != NULL; p++ ) {
00681                      n_addr++;
00682               }
00683               total_addr_len = n_addr * src->h_length;
00684        }
00685        
00686        total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
00687               total_addr_len + total_alias_len + name_len;
00688        
00689        if (safe_realloc( buf, total_len )) {                    
00690               tp = (char **) *buf;
00691               tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
00692               AC_MEMCPY( res, src, sizeof( struct hostent ) );
00693               /* first the name... */
00694               AC_MEMCPY( tbuf, src->h_name, name_len );
00695               res->h_name = tbuf; tbuf+=name_len;
00696               /* now the aliases */
00697               res->h_aliases = tp;
00698               if ( src->h_aliases != NULL ) {
00699                      tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
00700               }
00701               *tp++=NULL;
00702               /* finally the addresses */
00703               res->h_addr_list = tp;
00704               if ( src->h_addr_list != NULL ) {
00705                      tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
00706               }
00707               *tp++=NULL;
00708               return 0;
00709        }
00710        return -1;
00711 }
00712 #endif
00713 
00714 #if defined( NEED_SAFE_REALLOC )
00715 static char *safe_realloc( char **buf, int len )
00716 {
00717        char *tmpbuf;
00718        tmpbuf = LDAP_REALLOC( *buf, len );
00719        if (tmpbuf) {
00720               *buf=tmpbuf;
00721        } 
00722        return tmpbuf;
00723 }
00724 #endif
00725 
00726 char * ldap_pvt_get_fqdn( char *name )
00727 {
00728        char *fqdn, *ha_buf;
00729        char hostbuf[MAXHOSTNAMELEN+1];
00730        struct hostent *hp, he_buf;
00731        int rc, local_h_errno;
00732 
00733        if( name == NULL ) {
00734               if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
00735                      hostbuf[MAXHOSTNAMELEN] = '\0';
00736                      name = hostbuf;
00737               } else {
00738                      name = "localhost";
00739               }
00740        }
00741 
00742        rc = ldap_pvt_gethostbyname_a( name,
00743               &he_buf, &ha_buf, &hp, &local_h_errno );
00744 
00745        if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
00746               fqdn = LDAP_STRDUP( name );
00747        } else {
00748               fqdn = LDAP_STRDUP( hp->h_name );
00749        }
00750 
00751        LDAP_FREE( ha_buf );
00752        return fqdn;
00753 }
00754 
00755 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
00756        && !defined( HAVE_GAI_STRERROR )
00757 char *ldap_pvt_gai_strerror (int code) {
00758        static struct {
00759               int code;
00760               const char *msg;
00761        } values[] = {
00762 #ifdef EAI_ADDRFAMILY
00763               { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
00764 #endif
00765               { EAI_AGAIN, N_("Temporary failure in name resolution") },
00766               { EAI_BADFLAGS, N_("Bad value for ai_flags") },
00767               { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
00768               { EAI_FAMILY, N_("ai_family not supported") },
00769               { EAI_MEMORY, N_("Memory allocation failure") },
00770 #ifdef EAI_NODATA
00771               { EAI_NODATA, N_("No address associated with hostname") },
00772 #endif    
00773               { EAI_NONAME, N_("Name or service not known") },
00774               { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
00775               { EAI_SOCKTYPE, N_("ai_socktype not supported") },
00776               { EAI_SYSTEM, N_("System error") },
00777               { 0, NULL }
00778        };
00779 
00780        int i;
00781 
00782        for ( i = 0; values[i].msg != NULL; i++ ) {
00783               if ( values[i].code == code ) {
00784                      return (char *) _(values[i].msg);
00785               }
00786        }
00787        
00788        return _("Unknown error");
00789 }
00790 #endif