]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
f40082c64dec28107c3063f60df5b2d2c73e389c
[openldap] / libraries / libldap / util-int.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * util-int.c   Various functions to replace missing threadsafe ones.
7  *                                Without the real *_r funcs, things will work, but won't be
8  *                                threadsafe. 
9  * 
10  * Written by Bart Hartgers.
11  *
12  * Copyright 1998, A. Hartgers, All rights reserved.
13  * This software is not subject to any license of Eindhoven University of
14  * Technology, since it was written in my spare time.
15  *                      
16  * Redistribution and use in source and binary forms are permitted only
17  * as authorized by the OpenLDAP Public License.  A copy of this
18  * license is available at http://www.OpenLDAP.org/license.html or
19  * in file LICENSE in the top-level directory of the distribution.
20  */ 
21
22 #include "portable.h"
23
24 #include <stdlib.h>
25
26 #include <ac/errno.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "ldap-int.h"
32
33 #if defined( LDAP_R_COMPILE )
34 # include <ldap_pvt_thread.h>
35 #else
36 # undef HAVE_REENTRANT_FUNCTIONS
37 #endif
38
39 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
40         && defined( CTIME_R_NARGS )
41 #       define USE_CTIME_R
42 #endif
43
44 #ifdef LDAP_R_COMPILE
45 # ifndef USE_CTIME_R
46         static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
47 # endif
48 # ifndef HAVE_GETHOSTBYNAME_R
49         static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
50 # endif
51 # ifndef HAVE_GETHOSTBYADDR_R
52         static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
53 # endif
54 #endif /* LDAP_R_COMPILE */
55
56 char *ldap_pvt_ctime( const time_t *tp, char *buf )
57 {
58 #ifdef USE_CTIME_R
59 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
60         choke me!  nargs should have 2 or 3
61 # elif CTIME_R_NARGS > 2
62         return ctime_r(tp,buf,26);
63 # else
64         return ctime_r(tp,buf);
65 # endif   
66
67 #else
68 # ifdef LDAP_R_COMPILE
69         ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
70 # endif
71         memcpy( buf, ctime(tp), 26 );
72 # ifdef LDAP_R_COMPILE
73         ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
74 # endif
75         return buf;
76 #endif  
77 }
78
79 #define BUFSTART 1024
80 #define BUFMAX (32*1024)
81
82 static char *safe_realloc( char **buf, int len );
83 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
84
85 int ldap_pvt_gethostbyname_a(
86         const char *name, 
87         struct hostent *resbuf,
88         char **buf,
89         struct hostent **result,
90         int *herrno_ptr )
91 {
92 #if defined( HAVE_GETHOSTBYNAME_R )
93 # define NEED_SAFE_REALLOC 1   
94         int r=-1;
95         int buflen=BUFSTART;
96         *buf = NULL;
97         for(;buflen<BUFMAX;) {
98                 if (safe_realloc( buf, buflen )==NULL)
99                         return r;
100                 r = gethostbyname_r( name, resbuf, *buf,
101                         buflen, result, herrno_ptr );
102 #ifdef NETDB_INTERNAL
103                 if ((r<0) &&
104                         (*herrno_ptr==NETDB_INTERNAL) &&
105                         (errno==ERANGE))
106                 {
107                         buflen*=2;
108                         continue;
109                 }
110 #endif
111                 return r;
112         }
113         return -1;
114 #elif defined( LDAP_R_COMPILE )
115 # define NEED_COPY_HOSTENT   
116         struct hostent *he;
117         int     retval;
118         
119         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
120         
121         he = gethostbyname( name );
122         
123         if (he==NULL) {
124                 *herrno_ptr = h_errno;
125                 retval = -1;
126         } else if (copy_hostent( resbuf, buf, he )<0) {
127                 *herrno_ptr = -1;
128                 retval = -1;
129         } else {
130                 *result = resbuf;
131                 retval = 0;
132         }
133         
134         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
135         
136         return retval;
137 #else   
138         *result = gethostbyname( name );
139
140         if (*result!=NULL) {
141                 return 0;
142         }
143
144         *herrno_ptr = h_errno;
145         
146         return -1;
147 #endif  
148 }
149          
150 int ldap_pvt_gethostbyaddr_a(
151         const char *addr,
152         int len,
153         int type,
154         struct hostent *resbuf,
155         char **buf,
156         struct hostent **result,
157         int *herrno_ptr )
158 {
159 #if defined( HAVE_GETHOSTBYADDR_R )
160 # undef NEED_SAFE_REALLOC
161 # define NEED_SAFE_REALLOC   
162         int r=-1;
163         int buflen=BUFSTART;
164         *buf = NULL;   
165         for(;buflen<BUFMAX;) {
166                 if (safe_realloc( buf, buflen )==NULL)
167                         return r;
168                 r = gethostbyaddr_r( addr, len, type,
169                         resbuf, *buf, buflen, 
170                         result, herrno_ptr );
171 #ifdef NETDB_INTERNAL
172                 if ((r<0) &&
173                         (*herrno_ptr==NETDB_INTERNAL) &&
174                         (errno==ERANGE))
175                 {
176                         buflen*=2;
177                         continue;
178                 }
179 #endif
180                 return r;
181         }
182         return -1;
183 #elif defined( LDAP_R_COMPILE )
184 # undef NEED_COPY_HOSTENT
185 # define NEED_COPY_HOSTENT   
186         struct hostent *he;
187         int     retval;
188         
189         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
190         
191         he = gethostbyaddr( addr, len, type );
192         
193         if (he==NULL) {
194                 *herrno_ptr = h_errno;
195                 retval = -1;
196         } else if (copy_hostent( resbuf, buf, he )<0) {
197                 *herrno_ptr = -1;
198                 retval = -1;
199         } else {
200                 *result = resbuf;
201                 retval = 0;
202         }
203         
204         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
205         
206         return retval;   
207 #else /* gethostbyaddr() */
208         *result = gethostbyaddr( addr, len, type );
209
210         if (*result!=NULL) {
211                 return 0;
212         }
213         return -1;
214 #endif  
215 }
216 /* 
217  * ldap_pvt_init_utils() should be called before any other function.
218  */
219
220 void ldap_pvt_init_utils( void )
221 {
222         static int done=0;
223         if (done)
224           return;
225         done=1;
226
227 #ifdef LDAP_R_COMPILE
228
229 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
230         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
231 #endif
232
233 #if !defined( HAVE_GETHOSTBYNAME_R )
234         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
235 #endif
236
237 #if !defined( HAVE_GETHOSTBYADDR_R )
238         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
239 #endif
240
241         /* call other module init functions here... */
242 #endif
243 }
244
245 #if defined( NEED_COPY_HOSTENT )
246 # undef NEED_SAFE_REALLOC
247 #define NEED_SAFE_REALLOC
248
249 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
250 {
251         int len;
252         char **tgt=*tgtio;
253         for( ; (*src) ; src++ ) {
254                 len = strlen( *src ) + 1;
255                 memcpy( buf, *src, len );
256                 *tgt++=buf;
257                 buf+=len;
258         }
259         *tgtio=tgt;   
260         return buf;
261 }
262
263 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
264 {
265         char **tgt=*tgtio;
266         for( ; (*src) ; src++ ) {
267                 memcpy( buf, *src, len );
268                 *tgt++=buf;
269                 buf+=len;
270         }
271         *tgtio=tgt;      
272         return buf;
273 }
274
275 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
276 {
277         char    **p;
278         char    **tp;
279         char    *tbuf;
280         int     name_len;
281         int     n_alias;
282         int     total_alias_len;
283         int     n_addr;
284         int     total_addr_len;
285         int     total_len;
286           
287         /* calculate the size needed for the buffer */
288         name_len = strlen( src->h_name ) + 1;
289         
290         for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
291                 total_alias_len += strlen( *p ) + 1;
292                 n_alias++;
293         }
294
295         for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
296                 n_addr++;
297         }
298         total_addr_len = n_addr * src->h_length;
299         
300         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
301                 total_addr_len + total_alias_len + name_len;
302         
303         if (safe_realloc( buf, total_len )) {                    
304                 tp = (char **) *buf;
305                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
306                 memcpy( res, src, sizeof( struct hostent ) );
307                 /* first the name... */
308                 memcpy( tbuf, src->h_name, name_len );
309                 res->h_name = tbuf; tbuf+=name_len;
310                 /* now the aliases */
311                 res->h_aliases = tp;
312                 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
313                 *tp++=NULL;
314                 /* finally the addresses */
315                 res->h_addr_list = tp;
316                 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
317                 *tp++=NULL;
318                 return 0;
319         }
320         return -1;
321 }
322 #endif
323
324 #if defined( NEED_SAFE_REALLOC )
325 static char *safe_realloc( char **buf, int len )
326 {
327         char *tmpbuf;
328         tmpbuf = realloc( *buf, len );
329         if (tmpbuf) {
330                 *buf=tmpbuf;
331         } 
332         return tmpbuf;
333 }
334 #endif
335
336