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