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