]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
LDAP C-API changes
[openldap] / libraries / libldap / util-int.c
1 /*
2  * util-int.c   Various functions to replace missing threadsafe ones.
3  *                                Without the real *_r funcs, things will work, but won't be
4  *                                threadsafe. 
5  * 
6  * Written by Bart Hartgers.
7  *
8  * Copyright 1998, A. Hartgers, All rights reserved.
9  * This software is not subject to any license of Eindhoven University of
10  * Technology, since it was written in my spare time.
11  *                      
12  * Redistribution and use in source and binary forms are permitted only
13  * as authorized by the OpenLDAP Public License.  A copy of this
14  * license is available at http://www.OpenLDAP.org/license.html or
15  * in file LICENSE in the top-level directory of the distribution.
16  */ 
17
18 #include "portable.h"
19
20 #include <stdlib.h>
21
22 #include <ac/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "ldap-int.h"
28
29 char *ldap_int_strtok( char *str, const char *delim, char **pos )
30 {
31 #ifdef HAVE_STRTOK_R
32         return strtok_r(str, delim, pos);
33 #else
34         return strtok(str, delim);
35 #endif
36 }
37
38 char *ldap_int_ctime( const time_t *tp, char *buf )
39 {
40 #ifdef HAVE_CTIME_R
41         return ctime_r(tp,buf);
42 #else
43         return ctime(tp);
44 #endif  
45 }
46
47 #define BUFSTART 1024
48 #define BUFMAX (32*1024)
49
50 static char *safe_realloc( char **buf, int len )
51 {
52         char *tmpbuf;
53         tmpbuf = realloc( *buf, len );
54         if (tmpbuf) {
55                 *buf=tmpbuf;
56         } 
57         return tmpbuf;
58 }
59  
60 int ldap_int_gethostbyname_a(
61         const char *name, 
62         struct hostent *resbuf,
63         char **buf,
64         struct hostent **result,
65         int *herrno_ptr )
66 {
67 #ifdef HAVE_GETHOSTBYNAME_R
68         int r;
69         int buflen=BUFSTART;
70
71         if (safe_realloc( buf, buflen)) {
72                 for(;buflen<BUFMAX;) {
73                         r = gethostbyname_r( name, resbuf, *buf,
74                                 buflen, result, herrno_ptr );
75 #ifdef NETDB_INTERNAL
76                         if ((r<0) &&
77                                 (*herrno_ptr==NETDB_INTERNAL) &&
78                                 (errno==ERANGE))
79                         {
80                                 if (safe_realloc( buf, buflen*=2 )) {
81                                                 continue;
82                                 }
83                         }
84 #endif
85                         return r;
86                 }
87         }
88
89 #else /* gethostbyname() */
90         *result = gethostbyname( name );
91
92         if (*result!=NULL) {
93                 return 0;
94         }
95
96         *herrno_ptr = h_errno;
97 #endif  
98
99         return -1;
100 }
101          
102 int ldap_int_gethostbyaddr_a(
103         const char *addr,
104         int len,
105         int type,
106         struct hostent *resbuf,
107         char **buf,
108         struct hostent **result,
109         int *herrno_ptr )
110 {
111 #ifdef HAVE_GETHOSTBYADDR_R
112         int r;
113         int buflen=BUFSTART;
114         if (safe_realloc( buf, buflen)) {
115                 for(;buflen<BUFMAX;) {
116                         r = gethostbyaddr_r( addr, len, type,
117                                 resbuf, *buf, buflen, 
118                                 result, herrno_ptr );
119 #ifdef NETDB_INTERNAL
120                         if ((r<0) &&
121                                 (*herrno_ptr==NETDB_INTERNAL) &&
122                                 (errno==ERANGE))
123                         {
124                                 if (safe_realloc( buf, buflen*=2))
125                                         continue;
126                         }
127 #endif
128                         return r;
129                 }
130         }
131
132 #else /* gethostbyaddr() */
133         *result = gethostbyaddr( addr, len, type );
134
135         if (*result!=NULL) {
136                 return 0;
137         }
138 #endif  
139
140         return -1;
141 }