]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
Happy New Year (belated)
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2014 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   ldap_pvt_thread_mutex_t ldap_int_hostname_mutex;
62   static ldap_pvt_thread_mutex_t ldap_int_gettime_mutex;
63
64 # if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
65          && defined( CTIME_R_NARGS )
66 #   define USE_CTIME_R
67 # else
68         static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
69 # endif
70
71 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
72
73 #ifdef LDAP_DEVEL
74         /* to be released with 2.5 */
75 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
76         /* we use the same mutex for gmtime(3) and localtime(3)
77          * because implementations may use the same buffer
78          * for both functions */
79         static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
80 #endif
81 #else /* ! LDAP_DEVEL */
82         ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
83 #endif /* ! LDAP_DEVEL */
84
85 # if defined(HAVE_GETHOSTBYNAME_R) && \
86         (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
87         /* Don't know how to handle this version, pretend it's not there */
88 #       undef HAVE_GETHOSTBYNAME_R
89 # endif
90 # if defined(HAVE_GETHOSTBYADDR_R) && \
91         (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
92         /* Don't know how to handle this version, pretend it's not there */
93 #       undef HAVE_GETHOSTBYADDR_R
94 # endif
95 #endif /* LDAP_R_COMPILE */
96
97 char *ldap_pvt_ctime( const time_t *tp, char *buf )
98 {
99 #ifdef USE_CTIME_R
100 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
101 #       error "CTIME_R_NARGS should be 2 or 3"
102 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
103         return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
104 # elif CTIME_R_NARGS > 2
105         return ctime_r(tp,buf,26);
106 # else
107         return ctime_r(tp,buf);
108 # endif   
109
110 #else
111
112         LDAP_MUTEX_LOCK( &ldap_int_ctime_mutex );
113         AC_MEMCPY( buf, ctime(tp), 26 );
114         LDAP_MUTEX_UNLOCK( &ldap_int_ctime_mutex );
115
116         return buf;
117 #endif  
118 }
119
120 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
121 int
122 ldap_pvt_gmtime_lock( void )
123 {
124 # ifndef LDAP_R_COMPILE
125         return 0;
126 # else /* LDAP_R_COMPILE */
127         return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
128 # endif /* LDAP_R_COMPILE */
129 }
130
131 int
132 ldap_pvt_gmtime_unlock( void )
133 {
134 # ifndef LDAP_R_COMPILE
135         return 0;
136 # else /* LDAP_R_COMPILE */
137         return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
138 # endif /* LDAP_R_COMPILE */
139 }
140 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
141
142 #ifndef USE_GMTIME_R
143 struct tm *
144 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
145 {
146         struct tm *tm_ptr;
147
148         LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
149         tm_ptr = gmtime( timep );
150         if ( tm_ptr == NULL ) {
151                 result = NULL;
152
153         } else {
154                 *result = *tm_ptr;
155         }
156         LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
157
158         return result;
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         LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
169         tm_ptr = localtime( timep );
170         if ( tm_ptr == NULL ) {
171                 result = NULL;
172
173         } else {
174                 *result = *tm_ptr;
175         }
176         LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
177
178         return result;
179 }
180 #endif /* !USE_LOCALTIME_R */
181
182 /* return a broken out time, with microseconds
183  */
184 #ifdef _WIN32
185 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
186  * also need to use a high resolution timer to get microseconds.
187  * This is pretty clunky.
188  */
189 void
190 ldap_pvt_gettime( struct lutil_tm *tm )
191 {
192         static LARGE_INTEGER cFreq;
193         static LARGE_INTEGER prevCount;
194         static int subs;
195         static int offset;
196         LARGE_INTEGER count;
197         SYSTEMTIME st;
198
199         GetSystemTime( &st );
200         QueryPerformanceCounter( &count );
201
202         /* It shouldn't ever go backwards, but multiple CPUs might
203          * be able to hit in the same tick.
204          */
205         LDAP_MUTEX_LOCK( &ldap_int_gettime_mutex );
206         if ( count.QuadPart <= prevCount.QuadPart ) {
207                 subs++;
208         } else {
209                 subs = 0;
210                 prevCount = count;
211         }
212         LDAP_MUTEX_UNLOCK( &ldap_int_gettime_mutex );
213
214         /* We assume Windows has at least a vague idea of
215          * when a second begins. So we align our microsecond count
216          * with the Windows millisecond count using this offset.
217          * We retain the submillisecond portion of our own count.
218          *
219          * Note - this also assumes that the relationship between
220          * the PerformanceCouunter and SystemTime stays constant;
221          * that assumption breaks if the SystemTime is adjusted by
222          * an external action.
223          */
224         if ( !cFreq.QuadPart ) {
225                 long long t;
226                 int usec;
227                 QueryPerformanceFrequency( &cFreq );
228
229                 /* just get sub-second portion of counter */
230                 t = count.QuadPart % cFreq.QuadPart;
231
232                 /* convert to microseconds */
233                 t *= 1000000;
234                 usec = t / cFreq.QuadPart;
235
236                 offset = usec - st.wMilliseconds * 1000;
237         }
238
239         tm->tm_usub = subs;
240
241         /* convert to microseconds */
242         count.QuadPart %= cFreq.QuadPart;
243         count.QuadPart *= 1000000;
244         count.QuadPart /= cFreq.QuadPart;
245         count.QuadPart -= offset;
246
247         tm->tm_usec = count.QuadPart % 1000000;
248         if ( tm->tm_usec < 0 )
249                 tm->tm_usec += 1000000;
250
251         /* any difference larger than microseconds is
252          * already reflected in st
253          */
254
255         tm->tm_sec = st.wSecond;
256         tm->tm_min = st.wMinute;
257         tm->tm_hour = st.wHour;
258         tm->tm_mday = st.wDay;
259         tm->tm_mon = st.wMonth - 1;
260         tm->tm_year = st.wYear - 1900;
261 }
262 #else
263 void
264 ldap_pvt_gettime( struct lutil_tm *ltm )
265 {
266         struct timeval tv;
267         static struct timeval prevTv;
268         static int subs;
269
270         struct tm tm;
271         time_t t;
272
273         gettimeofday( &tv, NULL );
274         t = tv.tv_sec;
275
276         LDAP_MUTEX_LOCK( &ldap_int_gettime_mutex );
277         if ( tv.tv_sec < prevTv.tv_sec
278                 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
279                 subs++;
280         } else {
281                 subs = 0;
282                 prevTv = tv;
283         }
284         LDAP_MUTEX_UNLOCK( &ldap_int_gettime_mutex );
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_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_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         LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
435         rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
436         LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
437         if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
438         return rc;
439
440 #else /* !HAVE_GETNAMEINFO */
441         char *addr;
442         int alen;
443         struct hostent *hp = NULL;
444 #ifdef HAVE_GETHOSTBYADDR_R
445         struct hostent hb;
446         int buflen=BUFSTART, h_errno;
447         char *buf=NULL;
448 #endif
449
450 #ifdef LDAP_PF_INET6
451         if (sa->sa_family == AF_INET6) {
452                 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
453                 addr = (char *)&sin->sin6_addr;
454                 alen = sizeof(sin->sin6_addr);
455         } else
456 #endif
457         if (sa->sa_family == AF_INET) {
458                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
459                 addr = (char *)&sin->sin_addr;
460                 alen = sizeof(sin->sin_addr);
461         } else {
462                 rc = NO_RECOVERY;
463                 *err = (char *)HSTRERROR( rc );
464                 return rc;
465         }
466 #if defined( HAVE_GETHOSTBYADDR_R )
467         for(;buflen<BUFMAX;) {
468                 if (safe_realloc( &buf, buflen )==NULL) {
469                         *err = (char *)STRERROR( ENOMEM );
470                         return ENOMEM;
471                 }
472 #if (GETHOSTBYADDR_R_NARGS < 8)
473                 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
474                         &hb, buf, buflen, &h_errno );
475                 rc = (hp == NULL) ? -1 : 0;
476 #else
477                 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
478                         &hb, buf, buflen, 
479                         &hp, &h_errno );
480 #endif
481 #ifdef NETDB_INTERNAL
482                 if ((rc<0) &&
483                         (h_errno==NETDB_INTERNAL) &&
484                         (errno==ERANGE))
485                 {
486                         buflen*=2;
487                         continue;
488                 }
489 #endif
490                 break;
491         }
492         if (hp) {
493                 strncpy( name, hp->h_name, namelen );
494         } else {
495                 *err = (char *)HSTRERROR( h_errno );
496         }
497         LDAP_FREE(buf);
498 #else /* HAVE_GETHOSTBYADDR_R */
499
500         LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
501         hp = gethostbyaddr( addr, alen, sa->sa_family );
502         if (hp) {
503                 strncpy( name, hp->h_name, namelen );
504                 rc = 0;
505         } else {
506                 rc = h_errno;
507                 *err = (char *)HSTRERROR( h_errno );
508         }
509         LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
510
511 #endif  /* !HAVE_GETHOSTBYADDR_R */
512         return rc;
513 #endif  /* !HAVE_GETNAMEINFO */
514 }
515
516 int ldap_pvt_gethostbyaddr_a(
517         const char *addr,
518         int len,
519         int type,
520         struct hostent *resbuf,
521         char **buf,
522         struct hostent **result,
523         int *herrno_ptr )
524 {
525 #if defined( HAVE_GETHOSTBYADDR_R )
526
527 # undef NEED_SAFE_REALLOC
528 # define NEED_SAFE_REALLOC   
529         int r=-1;
530         int buflen=BUFSTART;
531         *buf = NULL;   
532         for(;buflen<BUFMAX;) {
533                 if (safe_realloc( buf, buflen )==NULL)
534                         return r;
535 #if (GETHOSTBYADDR_R_NARGS < 8)
536                 *result=gethostbyaddr_r( addr, len, type,
537                         resbuf, *buf, buflen, herrno_ptr );
538                 r = (*result == NULL) ? -1 : 0;
539 #else
540                 r = gethostbyaddr_r( addr, len, type,
541                         resbuf, *buf, buflen, 
542                         result, herrno_ptr );
543 #endif
544
545 #ifdef NETDB_INTERNAL
546                 if ((r<0) &&
547                         (*herrno_ptr==NETDB_INTERNAL) &&
548                         (errno==ERANGE))
549                 {
550                         buflen*=2;
551                         continue;
552                 }
553 #endif
554                 return r;
555         }
556         return -1;
557 #elif defined( LDAP_R_COMPILE )
558 # undef NEED_COPY_HOSTENT
559 # define NEED_COPY_HOSTENT   
560         struct hostent *he;
561         int     retval;
562         *buf = NULL;   
563         
564         LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
565         he = gethostbyaddr( addr, len, type );
566         
567         if (he==NULL) {
568                 *herrno_ptr = h_errno;
569                 retval = -1;
570         } else if (copy_hostent( resbuf, buf, he )<0) {
571                 *herrno_ptr = -1;
572                 retval = -1;
573         } else {
574                 *result = resbuf;
575                 retval = 0;
576         }
577         LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
578         
579         return retval;
580
581 #else /* gethostbyaddr() */
582         *buf = NULL;   
583         *result = gethostbyaddr( addr, len, type );
584
585         if (*result!=NULL) {
586                 return 0;
587         }
588         return -1;
589 #endif  
590 }
591 /* 
592  * ldap_int_utils_init() should be called before any other function.
593  */
594
595 void ldap_int_utils_init( void )
596 {
597         static int done=0;
598         if (done)
599           return;
600         done=1;
601
602 #ifdef LDAP_R_COMPILE
603 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
604         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
605 #endif
606 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
607         ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
608 #endif
609         ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
610
611         ldap_pvt_thread_mutex_init( &ldap_int_hostname_mutex );
612
613         ldap_pvt_thread_mutex_init( &ldap_int_gettime_mutex );
614
615 #ifdef HAVE_GSSAPI
616         ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
617 #endif
618 #endif
619
620         /* call other module init functions here... */
621 }
622
623 #if defined( NEED_COPY_HOSTENT )
624 # undef NEED_SAFE_REALLOC
625 #define NEED_SAFE_REALLOC
626
627 static char *cpy_aliases(
628         char ***tgtio,
629         char *buf,
630         char **src )
631 {
632         int len;
633         char **tgt=*tgtio;
634         for( ; (*src) ; src++ ) {
635                 len = strlen( *src ) + 1;
636                 AC_MEMCPY( buf, *src, len );
637                 *tgt++=buf;
638                 buf+=len;
639         }
640         *tgtio=tgt;   
641         return buf;
642 }
643
644 static char *cpy_addresses(
645         char ***tgtio,
646         char *buf,
647         char **src,
648         int len )
649 {
650         char **tgt=*tgtio;
651         for( ; (*src) ; src++ ) {
652                 AC_MEMCPY( buf, *src, len );
653                 *tgt++=buf;
654                 buf+=len;
655         }
656         *tgtio=tgt;      
657         return buf;
658 }
659
660 static int copy_hostent(
661         struct hostent *res,
662         char **buf,
663         struct hostent * src )
664 {
665         char    **p;
666         char    **tp;
667         char    *tbuf;
668         int     name_len;
669         int     n_alias=0;
670         int     total_alias_len=0;
671         int     n_addr=0;
672         int     total_addr_len=0;
673         int     total_len;
674           
675         /* calculate the size needed for the buffer */
676         name_len = strlen( src->h_name ) + 1;
677         
678         if( src->h_aliases != NULL ) {
679                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
680                         total_alias_len += strlen( *p ) + 1;
681                         n_alias++; 
682                 }
683         }
684
685         if( src->h_addr_list != NULL ) {
686                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
687                         n_addr++;
688                 }
689                 total_addr_len = n_addr * src->h_length;
690         }
691         
692         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
693                 total_addr_len + total_alias_len + name_len;
694         
695         if (safe_realloc( buf, total_len )) {                    
696                 tp = (char **) *buf;
697                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
698                 AC_MEMCPY( res, src, sizeof( struct hostent ) );
699                 /* first the name... */
700                 AC_MEMCPY( tbuf, src->h_name, name_len );
701                 res->h_name = tbuf; tbuf+=name_len;
702                 /* now the aliases */
703                 res->h_aliases = tp;
704                 if ( src->h_aliases != NULL ) {
705                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
706                 }
707                 *tp++=NULL;
708                 /* finally the addresses */
709                 res->h_addr_list = tp;
710                 if ( src->h_addr_list != NULL ) {
711                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
712                 }
713                 *tp++=NULL;
714                 return 0;
715         }
716         return -1;
717 }
718 #endif
719
720 #if defined( NEED_SAFE_REALLOC )
721 static char *safe_realloc( char **buf, int len )
722 {
723         char *tmpbuf;
724         tmpbuf = LDAP_REALLOC( *buf, len );
725         if (tmpbuf) {
726                 *buf=tmpbuf;
727         } 
728         return tmpbuf;
729 }
730 #endif
731
732 char * ldap_pvt_get_fqdn( char *name )
733 {
734         char *fqdn, *ha_buf;
735         char hostbuf[MAXHOSTNAMELEN+1];
736         struct hostent *hp, he_buf;
737         int rc, local_h_errno;
738
739         if( name == NULL ) {
740                 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
741                         hostbuf[MAXHOSTNAMELEN] = '\0';
742                         name = hostbuf;
743                 } else {
744                         name = "localhost";
745                 }
746         }
747
748         rc = ldap_pvt_gethostbyname_a( name,
749                 &he_buf, &ha_buf, &hp, &local_h_errno );
750
751         if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
752                 fqdn = LDAP_STRDUP( name );
753         } else {
754                 fqdn = LDAP_STRDUP( hp->h_name );
755         }
756
757         LDAP_FREE( ha_buf );
758         return fqdn;
759 }
760
761 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
762         && !defined( HAVE_GAI_STRERROR )
763 char *ldap_pvt_gai_strerror (int code) {
764         static struct {
765                 int code;
766                 const char *msg;
767         } values[] = {
768 #ifdef EAI_ADDRFAMILY
769                 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
770 #endif
771                 { EAI_AGAIN, N_("Temporary failure in name resolution") },
772                 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
773                 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
774                 { EAI_FAMILY, N_("ai_family not supported") },
775                 { EAI_MEMORY, N_("Memory allocation failure") },
776 #ifdef EAI_NODATA
777                 { EAI_NODATA, N_("No address associated with hostname") },
778 #endif    
779                 { EAI_NONAME, N_("Name or service not known") },
780                 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
781                 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
782 #ifdef EAI_SYSTEM
783                 { EAI_SYSTEM, N_("System error") },
784 #endif
785                 { 0, NULL }
786         };
787
788         int i;
789
790         for ( i = 0; values[i].msg != NULL; i++ ) {
791                 if ( values[i].code == code ) {
792                         return (char *) _(values[i].msg);
793                 }
794         }
795         
796         return _("Unknown error");
797 }
798 #endif