]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
More ITS#6262: Also define ldap_pvt_gmtime_[un]lock if localtime_r is missing
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2009 The OpenLDAP Foundation.
5  * Portions Copyright 1998 A. Hartgers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Bart Hartgers for inclusion in
18  * OpenLDAP Software.
19  */
20
21 /*
22  * util-int.c   Various functions to replace missing threadsafe ones.
23  *                              Without the real *_r funcs, things will
24  *                              work, but might not be threadsafe. 
25  */
26
27 #include "portable.h"
28
29 #include <ac/stdlib.h>
30
31 #include <ac/errno.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35 #include <ac/unistd.h>
36
37 #include "ldap-int.h"
38
39 #ifndef h_errno
40 /* newer systems declare this in <netdb.h> for you, older ones don't.
41  * harmless to declare it again (unless defined by a macro).
42  */
43 extern int h_errno;
44 #endif
45
46 #ifdef HAVE_HSTRERROR
47 # define HSTRERROR(e)   hstrerror(e)
48 #else
49 # define HSTRERROR(e)   hp_strerror(e)
50 #endif
51
52 #ifndef LDAP_R_COMPILE
53 # undef HAVE_REENTRANT_FUNCTIONS
54 # undef HAVE_CTIME_R
55 # undef HAVE_GETHOSTBYNAME_R
56 # undef HAVE_GETHOSTBYADDR_R
57
58 #else
59 # include <ldap_pvt_thread.h>
60   ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
61
62 # if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
63          && defined( CTIME_R_NARGS )
64 #   define USE_CTIME_R
65 # else
66         static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
67 # endif
68
69 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
70
71 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
72         /* we use the same mutex for gmtime(3) and localtime(3)
73          * because implementations may use the same buffer
74          * for both functions */
75         static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
76 #endif
77
78 # if defined(HAVE_GETHOSTBYNAME_R) && \
79         (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
80         /* Don't know how to handle this version, pretend it's not there */
81 #       undef HAVE_GETHOSTBYNAME_R
82 # endif
83 # if defined(HAVE_GETHOSTBYADDR_R) && \
84         (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
85         /* Don't know how to handle this version, pretend it's not there */
86 #       undef HAVE_GETHOSTBYADDR_R
87 # endif
88 #endif /* LDAP_R_COMPILE */
89
90 char *ldap_pvt_ctime( const time_t *tp, char *buf )
91 {
92 #ifdef USE_CTIME_R
93 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
94 #       error "CTIME_R_NARGS should be 2 or 3"
95 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
96         return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
97 # elif CTIME_R_NARGS > 2
98         return ctime_r(tp,buf,26);
99 # else
100         return ctime_r(tp,buf);
101 # endif   
102
103 #else
104
105 # ifdef LDAP_R_COMPILE
106         ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
107 # endif
108
109         AC_MEMCPY( buf, ctime(tp), 26 );
110
111 # ifdef LDAP_R_COMPILE
112         ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
113 # endif
114
115         return buf;
116 #endif  
117 }
118
119 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
120 int
121 ldap_pvt_gmtime_lock( void )
122 {
123 # ifndef LDAP_R_COMPILE
124         return 0;
125 # else /* LDAP_R_COMPILE */
126         return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
127 # endif /* LDAP_R_COMPILE */
128 }
129
130 int
131 ldap_pvt_gmtime_unlock( void )
132 {
133 # ifndef LDAP_R_COMPILE
134         return 0;
135 # else /* LDAP_R_COMPILE */
136         return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
137 # endif /* LDAP_R_COMPILE */
138 }
139 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
140
141 #ifndef USE_GMTIME_R
142 struct tm *
143 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
144 {
145         struct tm *tm_ptr;
146
147 # ifdef LDAP_R_COMPILE
148         ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
149 # endif /* LDAP_R_COMPILE */
150
151         tm_ptr = gmtime( timep );
152         if ( tm_ptr == NULL ) {
153                 result = NULL;
154
155         } else {
156                 *result = *tm_ptr;
157         }
158
159 # ifdef LDAP_R_COMPILE
160         ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
161 # endif /* LDAP_R_COMPILE */
162
163         return result;
164 }
165 #endif /* !USE_GMTIME_R */
166
167 #ifndef USE_LOCALTIME_R
168 struct tm *
169 ldap_pvt_localtime( const time_t *timep, struct tm *result )
170 {
171         struct tm *tm_ptr;
172
173 # ifdef LDAP_R_COMPILE
174         ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
175 # endif /* LDAP_R_COMPILE */
176
177         tm_ptr = localtime( timep );
178         if ( tm_ptr == NULL ) {
179                 result = NULL;
180
181         } else {
182                 *result = *tm_ptr;
183         }
184
185 # ifdef LDAP_R_COMPILE
186         ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
187 # endif /* LDAP_R_COMPILE */
188
189         return result;
190 }
191 #endif /* !USE_LOCALTIME_R */
192
193 /* return a broken out time, with microseconds
194  * Must be mutex-protected.
195  */
196 #ifdef _WIN32
197 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
198  * also need to use a high resolution timer to get microseconds.
199  * This is pretty clunky.
200  */
201 void
202 ldap_pvt_gettime( struct lutil_tm *tm )
203 {
204         static LARGE_INTEGER cFreq;
205         static LARGE_INTEGER prevCount;
206         static int subs;
207         static int offset;
208         LARGE_INTEGER count;
209         SYSTEMTIME st;
210
211         GetSystemTime( &st );
212         QueryPerformanceCounter( &count );
213
214         /* It shouldn't ever go backwards, but multiple CPUs might
215          * be able to hit in the same tick.
216          */
217         if ( count.QuadPart <= prevCount.QuadPart ) {
218                 subs++;
219         } else {
220                 subs = 0;
221                 prevCount = count;
222         }
223
224         /* We assume Windows has at least a vague idea of
225          * when a second begins. So we align our microsecond count
226          * with the Windows millisecond count using this offset.
227          * We retain the submillisecond portion of our own count.
228          *
229          * Note - this also assumes that the relationship between
230          * the PerformanceCouunter and SystemTime stays constant;
231          * that assumption breaks if the SystemTime is adjusted by
232          * an external action.
233          */
234         if ( !cFreq.QuadPart ) {
235                 long long t;
236                 int usec;
237                 QueryPerformanceFrequency( &cFreq );
238
239                 /* just get sub-second portion of counter */
240                 t = count.QuadPart % cFreq.QuadPart;
241
242                 /* convert to microseconds */
243                 t *= 1000000;
244                 usec = t / cFreq.QuadPart;
245
246                 offset = usec - st.wMilliseconds * 1000;
247         }
248
249         tm->tm_usub = subs;
250
251         /* convert to microseconds */
252         count.QuadPart %= cFreq.QuadPart;
253         count.QuadPart *= 1000000;
254         count.QuadPart /= cFreq.QuadPart;
255         count.QuadPart -= offset;
256
257         tm->tm_usec = count.QuadPart % 1000000;
258         if ( tm->tm_usec < 0 )
259                 tm->tm_usec += 1000000;
260
261         /* any difference larger than microseconds is
262          * already reflected in st
263          */
264
265         tm->tm_sec = st.wSecond;
266         tm->tm_min = st.wMinute;
267         tm->tm_hour = st.wHour;
268         tm->tm_mday = st.wDay;
269         tm->tm_mon = st.wMonth - 1;
270         tm->tm_year = st.wYear - 1900;
271 }
272 #else
273 void
274 ldap_pvt_gettime( struct lutil_tm *ltm )
275 {
276         struct timeval tv;
277         static struct timeval prevTv;
278         static int subs;
279
280         struct tm tm;
281         time_t t;
282
283         gettimeofday( &tv, NULL );
284         t = tv.tv_sec;
285
286         if ( tv.tv_sec < prevTv.tv_sec
287                 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec == prevTv.tv_usec )) {
288                 subs++;
289         } else {
290                 subs = 0;
291                 prevTv = tv;
292         }
293
294         ltm->tm_usub = subs;
295
296         ldap_pvt_gmtime( &t, &tm );
297
298         ltm->tm_sec = tm.tm_sec;
299         ltm->tm_min = tm.tm_min;
300         ltm->tm_hour = tm.tm_hour;
301         ltm->tm_mday = tm.tm_mday;
302         ltm->tm_mon = tm.tm_mon;
303         ltm->tm_year = tm.tm_year;
304         ltm->tm_usec = tv.tv_usec;
305 }
306 #endif
307
308 size_t
309 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
310 {
311         struct lutil_tm tm;
312         int n;
313
314         ldap_pvt_gettime( &tm );
315
316         n = snprintf( buf, len,
317                 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
318                 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
319                 tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
320
321         if( n < 0 ) return 0;
322         return ( (size_t) n < len ) ? n : 0;
323 }
324
325 #define BUFSTART (1024-32)
326 #define BUFMAX (32*1024-32)
327
328 #if defined(LDAP_R_COMPILE)
329 static char *safe_realloc( char **buf, int len );
330
331 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
332 static int copy_hostent( struct hostent *res,
333         char **buf, struct hostent * src );
334 #endif
335 #endif
336
337 int ldap_pvt_gethostbyname_a(
338         const char *name, 
339         struct hostent *resbuf,
340         char **buf,
341         struct hostent **result,
342         int *herrno_ptr )
343 {
344 #if defined( HAVE_GETHOSTBYNAME_R )
345
346 # define NEED_SAFE_REALLOC 1   
347         int r=-1;
348         int buflen=BUFSTART;
349         *buf = NULL;
350         for(;buflen<BUFMAX;) {
351                 if (safe_realloc( buf, buflen )==NULL)
352                         return r;
353
354 #if (GETHOSTBYNAME_R_NARGS < 6)
355                 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
356                 r = (*result == NULL) ?  -1 : 0;
357 #else
358                 r = gethostbyname_r( name, resbuf, *buf,
359                         buflen, result, herrno_ptr );
360 #endif
361
362                 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
363                        name, r, 0 );
364
365 #ifdef NETDB_INTERNAL
366                 if ((r<0) &&
367                         (*herrno_ptr==NETDB_INTERNAL) &&
368                         (errno==ERANGE))
369                 {
370                         buflen*=2;
371                         continue;
372                 }
373 #endif
374                 return r;
375         }
376         return -1;
377 #elif defined( LDAP_R_COMPILE )
378 # define NEED_COPY_HOSTENT   
379         struct hostent *he;
380         int     retval;
381         *buf = NULL;
382         
383         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
384         
385         he = gethostbyname( name );
386         
387         if (he==NULL) {
388                 *herrno_ptr = h_errno;
389                 retval = -1;
390         } else if (copy_hostent( resbuf, buf, he )<0) {
391                 *herrno_ptr = -1;
392                 retval = -1;
393         } else {
394                 *result = resbuf;
395                 retval = 0;
396         }
397         
398         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
399         
400         return retval;
401 #else   
402         *buf = NULL;
403         *result = gethostbyname( name );
404
405         if (*result!=NULL) {
406                 return 0;
407         }
408
409         *herrno_ptr = h_errno;
410         
411         return -1;
412 #endif  
413 }
414
415 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
416 static const char *
417 hp_strerror( int err )
418 {
419         switch (err) {
420         case HOST_NOT_FOUND:    return _("Host not found (authoritative)");
421         case TRY_AGAIN:                 return _("Host not found (server fail?)");
422         case NO_RECOVERY:               return _("Non-recoverable failure");
423         case NO_DATA:                   return _("No data of requested type");
424 #ifdef NETDB_INTERNAL
425         case NETDB_INTERNAL:    return STRERROR( errno );
426 #endif
427         }
428         return _("Unknown resolver error");
429 }
430 #endif
431
432 int ldap_pvt_get_hname(
433         const struct sockaddr *sa,
434         int len,
435         char *name,
436         int namelen,
437         char **err )
438 {
439         int rc;
440 #if defined( HAVE_GETNAMEINFO )
441
442 #if defined( LDAP_R_COMPILE )
443         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
444 #endif
445         rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
446 #if defined( LDAP_R_COMPILE )
447         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
448 #endif
449         if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
450         return rc;
451
452 #else /* !HAVE_GETNAMEINFO */
453         char *addr;
454         int alen;
455         struct hostent *hp = NULL;
456 #ifdef HAVE_GETHOSTBYADDR_R
457         struct hostent hb;
458         int buflen=BUFSTART, h_errno;
459         char *buf=NULL;
460 #endif
461
462 #ifdef LDAP_PF_INET6
463         if (sa->sa_family == AF_INET6) {
464                 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
465                 addr = (char *)&sin->sin6_addr;
466                 alen = sizeof(sin->sin6_addr);
467         } else
468 #endif
469         if (sa->sa_family == AF_INET) {
470                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
471                 addr = (char *)&sin->sin_addr;
472                 alen = sizeof(sin->sin_addr);
473         } else {
474                 rc = NO_RECOVERY;
475                 *err = (char *)HSTRERROR( rc );
476                 return rc;
477         }
478 #if defined( HAVE_GETHOSTBYADDR_R )
479         for(;buflen<BUFMAX;) {
480                 if (safe_realloc( &buf, buflen )==NULL) {
481                         *err = (char *)STRERROR( ENOMEM );
482                         return ENOMEM;
483                 }
484 #if (GETHOSTBYADDR_R_NARGS < 8)
485                 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
486                         &hb, buf, buflen, &h_errno );
487                 rc = (hp == NULL) ? -1 : 0;
488 #else
489                 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
490                         &hb, buf, buflen, 
491                         &hp, &h_errno );
492 #endif
493 #ifdef NETDB_INTERNAL
494                 if ((rc<0) &&
495                         (h_errno==NETDB_INTERNAL) &&
496                         (errno==ERANGE))
497                 {
498                         buflen*=2;
499                         continue;
500                 }
501 #endif
502                 break;
503         }
504         if (hp) {
505                 strncpy( name, hp->h_name, namelen );
506         } else {
507                 *err = (char *)HSTRERROR( h_errno );
508         }
509         LDAP_FREE(buf);
510 #else /* HAVE_GETHOSTBYADDR_R */
511
512 #if defined( LDAP_R_COMPILE )
513         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
514 #endif
515         hp = gethostbyaddr( addr, alen, sa->sa_family );
516         if (hp) {
517                 strncpy( name, hp->h_name, namelen );
518                 rc = 0;
519         } else {
520                 rc = h_errno;
521                 *err = (char *)HSTRERROR( h_errno );
522         }
523 #if defined( LDAP_R_COMPILE )
524         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
525 #endif
526
527 #endif  /* !HAVE_GETHOSTBYADDR_R */
528         return rc;
529 #endif  /* !HAVE_GETNAMEINFO */
530 }
531
532 int ldap_pvt_gethostbyaddr_a(
533         const char *addr,
534         int len,
535         int type,
536         struct hostent *resbuf,
537         char **buf,
538         struct hostent **result,
539         int *herrno_ptr )
540 {
541 #if defined( HAVE_GETHOSTBYADDR_R )
542
543 # undef NEED_SAFE_REALLOC
544 # define NEED_SAFE_REALLOC   
545         int r=-1;
546         int buflen=BUFSTART;
547         *buf = NULL;   
548         for(;buflen<BUFMAX;) {
549                 if (safe_realloc( buf, buflen )==NULL)
550                         return r;
551 #if (GETHOSTBYADDR_R_NARGS < 8)
552                 *result=gethostbyaddr_r( addr, len, type,
553                         resbuf, *buf, buflen, herrno_ptr );
554                 r = (*result == NULL) ? -1 : 0;
555 #else
556                 r = gethostbyaddr_r( addr, len, type,
557                         resbuf, *buf, buflen, 
558                         result, herrno_ptr );
559 #endif
560
561 #ifdef NETDB_INTERNAL
562                 if ((r<0) &&
563                         (*herrno_ptr==NETDB_INTERNAL) &&
564                         (errno==ERANGE))
565                 {
566                         buflen*=2;
567                         continue;
568                 }
569 #endif
570                 return r;
571         }
572         return -1;
573 #elif defined( LDAP_R_COMPILE )
574 # undef NEED_COPY_HOSTENT
575 # define NEED_COPY_HOSTENT   
576         struct hostent *he;
577         int     retval;
578         *buf = NULL;   
579         
580         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
581         
582         he = gethostbyaddr( addr, len, type );
583         
584         if (he==NULL) {
585                 *herrno_ptr = h_errno;
586                 retval = -1;
587         } else if (copy_hostent( resbuf, buf, he )<0) {
588                 *herrno_ptr = -1;
589                 retval = -1;
590         } else {
591                 *result = resbuf;
592                 retval = 0;
593         }
594         
595         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
596         
597         return retval;
598
599 #else /* gethostbyaddr() */
600         *buf = NULL;   
601         *result = gethostbyaddr( addr, len, type );
602
603         if (*result!=NULL) {
604                 return 0;
605         }
606         return -1;
607 #endif  
608 }
609 /* 
610  * ldap_int_utils_init() should be called before any other function.
611  */
612
613 void ldap_int_utils_init( void )
614 {
615         static int done=0;
616         if (done)
617           return;
618         done=1;
619
620 #ifdef LDAP_R_COMPILE
621 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
622         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
623 #endif
624 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
625         ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
626 #endif
627         ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
628
629 #ifdef HAVE_CYRUS_SASL
630         ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
631 #endif
632 #ifdef HAVE_GSSAPI
633         ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
634 #endif
635 #endif
636
637         /* call other module init functions here... */
638 }
639
640 #if defined( NEED_COPY_HOSTENT )
641 # undef NEED_SAFE_REALLOC
642 #define NEED_SAFE_REALLOC
643
644 static char *cpy_aliases(
645         char ***tgtio,
646         char *buf,
647         char **src )
648 {
649         int len;
650         char **tgt=*tgtio;
651         for( ; (*src) ; src++ ) {
652                 len = strlen( *src ) + 1;
653                 AC_MEMCPY( buf, *src, len );
654                 *tgt++=buf;
655                 buf+=len;
656         }
657         *tgtio=tgt;   
658         return buf;
659 }
660
661 static char *cpy_addresses(
662         char ***tgtio,
663         char *buf,
664         char **src,
665         int len )
666 {
667         char **tgt=*tgtio;
668         for( ; (*src) ; src++ ) {
669                 AC_MEMCPY( buf, *src, len );
670                 *tgt++=buf;
671                 buf+=len;
672         }
673         *tgtio=tgt;      
674         return buf;
675 }
676
677 static int copy_hostent(
678         struct hostent *res,
679         char **buf,
680         struct hostent * src )
681 {
682         char    **p;
683         char    **tp;
684         char    *tbuf;
685         int     name_len;
686         int     n_alias=0;
687         int     total_alias_len=0;
688         int     n_addr=0;
689         int     total_addr_len=0;
690         int     total_len;
691           
692         /* calculate the size needed for the buffer */
693         name_len = strlen( src->h_name ) + 1;
694         
695         if( src->h_aliases != NULL ) {
696                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
697                         total_alias_len += strlen( *p ) + 1;
698                         n_alias++; 
699                 }
700         }
701
702         if( src->h_addr_list != NULL ) {
703                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
704                         n_addr++;
705                 }
706                 total_addr_len = n_addr * src->h_length;
707         }
708         
709         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
710                 total_addr_len + total_alias_len + name_len;
711         
712         if (safe_realloc( buf, total_len )) {                    
713                 tp = (char **) *buf;
714                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
715                 AC_MEMCPY( res, src, sizeof( struct hostent ) );
716                 /* first the name... */
717                 AC_MEMCPY( tbuf, src->h_name, name_len );
718                 res->h_name = tbuf; tbuf+=name_len;
719                 /* now the aliases */
720                 res->h_aliases = tp;
721                 if ( src->h_aliases != NULL ) {
722                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
723                 }
724                 *tp++=NULL;
725                 /* finally the addresses */
726                 res->h_addr_list = tp;
727                 if ( src->h_addr_list != NULL ) {
728                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
729                 }
730                 *tp++=NULL;
731                 return 0;
732         }
733         return -1;
734 }
735 #endif
736
737 #if defined( NEED_SAFE_REALLOC )
738 static char *safe_realloc( char **buf, int len )
739 {
740         char *tmpbuf;
741         tmpbuf = LDAP_REALLOC( *buf, len );
742         if (tmpbuf) {
743                 *buf=tmpbuf;
744         } 
745         return tmpbuf;
746 }
747 #endif
748
749 char * ldap_pvt_get_fqdn( char *name )
750 {
751         char *fqdn, *ha_buf;
752         char hostbuf[MAXHOSTNAMELEN+1];
753         struct hostent *hp, he_buf;
754         int rc, local_h_errno;
755
756         if( name == NULL ) {
757                 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
758                         hostbuf[MAXHOSTNAMELEN] = '\0';
759                         name = hostbuf;
760                 } else {
761                         name = "localhost";
762                 }
763         }
764
765         rc = ldap_pvt_gethostbyname_a( name,
766                 &he_buf, &ha_buf, &hp, &local_h_errno );
767
768         if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
769                 fqdn = LDAP_STRDUP( name );
770         } else {
771                 fqdn = LDAP_STRDUP( hp->h_name );
772         }
773
774         LDAP_FREE( ha_buf );
775         return fqdn;
776 }
777
778 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
779         && !defined( HAVE_GAI_STRERROR )
780 char *ldap_pvt_gai_strerror (int code) {
781         static struct {
782                 int code;
783                 const char *msg;
784         } values[] = {
785 #ifdef EAI_ADDRFAMILY
786                 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
787 #endif
788                 { EAI_AGAIN, N_("Temporary failure in name resolution") },
789                 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
790                 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
791                 { EAI_FAMILY, N_("ai_family not supported") },
792                 { EAI_MEMORY, N_("Memory allocation failure") },
793 #ifdef EAI_NODATA
794                 { EAI_NODATA, N_("No address associated with hostname") },
795 #endif    
796                 { EAI_NONAME, N_("Name or service not known") },
797                 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
798                 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
799                 { EAI_SYSTEM, N_("System error") },
800                 { 0, NULL }
801         };
802
803         int i;
804
805         for ( i = 0; values[i].msg != NULL; i++ ) {
806                 if ( values[i].code == code ) {
807                         return (char *) _(values[i].msg);
808                 }
809         }
810         
811         return _("Unknown error");
812 }
813 #endif