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