]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Removed numerous memory leaks detected by Mark Meredith.
[openldap] / libraries / libldap / os-ip.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1995 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  os-ip.c -- platform-specific TCP & UDP related code
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/stdlib.h>
17
18 #include <ac/errno.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22 #include <ac/unistd.h>
23
24 #ifdef HAVE_IO_H
25 #include <io.h>
26 #endif /* HAVE_IO_H */
27
28 #if defined( HAVE_SYS_FILIO_H )
29 #include <sys/filio.h>
30 #elif defined( HAVE_SYS_IOCTL_H )
31 #include <sys/ioctl.h>
32 #endif
33
34 #include "ldap-int.h"
35
36 int
37 ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
38         int port, int async )
39 /*
40  * if host == NULL, connect using address
41  * "address" and "port" must be in network byte order
42  * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
43  * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
44  * XXX async is not used yet!
45  */
46 {
47         int                     rc, i;
48         ber_socket_t s = AC_SOCKET_INVALID;
49         int                     connected, use_hp;
50         struct sockaddr_in      sin;
51         struct hostent          *hp = NULL;
52 #ifdef notyet
53         ioctl_t                 status; /* for ioctl call */
54 #endif /* notyet */
55    
56         /* buffers for ldap_pvt_gethostbyname_a */
57         struct hostent          he_buf;
58         int                     local_h_errno;
59         char                    *ha_buf=NULL;
60 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
61    
62         Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
63             ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
64
65         connected = use_hp = 0;
66
67         if ( host != NULL ) {
68             address = inet_addr( host );
69             /* This was just a test for -1 until OSF1 let inet_addr return
70                unsigned int, which is narrower than 'unsigned long address' */
71             if ( address == 0xffffffff || address == (unsigned long) -1 ) {
72                 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
73                         &hp, &local_h_errno) < 0) || (hp==NULL))
74                 {
75 #ifdef HAVE_WINSOCK
76                         errno = WSAGetLastError();
77 #else
78                         errno = EHOSTUNREACH;   /* not exactly right, but... */
79 #endif
80                         DO_RETURN( -1 );
81                 }
82                 use_hp = 1;
83             }
84         }
85
86         rc = -1;
87         for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
88                 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
89                         DO_RETURN( -1 );
90                 }
91 #ifdef notyet
92                 status = 1;
93                 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
94                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
95                             s, 0, 0 );
96                 }
97 #endif /* notyet */
98                 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
99                 sin.sin_family = AF_INET;
100                 sin.sin_port = port;
101                 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
102                     ( use_hp ? (char *) hp->h_addr_list[ i ] :
103                     (char *) &address ), sizeof( sin.sin_addr.s_addr) );
104
105                 if ( connect( s, (struct sockaddr *)&sin,
106                     sizeof( struct sockaddr_in )) >= 0 ) {
107                         connected = 1;
108                         rc = 0;
109                         break;
110                 } else {
111 #ifdef HAVE_WINSOCK
112                         errno = WSAGetLastError();
113 #endif
114 #ifdef notyet
115 #ifdef EAGAIN
116                         if ( errno == EINPROGRESS || errno == EAGAIN ) {
117 #else /* EAGAIN */
118                         if ( errno == EINPROGRESS ) {
119 #endif /* EAGAIN */
120                                 Debug( LDAP_DEBUG_TRACE,
121                                         "connect would block...\n", 0, 0, 0 );
122                                 rc = -2;
123                                 break;
124                         }
125 #endif /* notyet */
126
127 #ifdef LDAP_DEBUG               
128                         if ( ldap_debug & LDAP_DEBUG_TRACE ) {
129                                 perror( (char *)inet_ntoa( sin.sin_addr ));
130                         }
131 #endif
132                         tcp_close( s );
133                         if ( !use_hp ) {
134                                 break;
135                         }
136                 }
137         }
138
139         ber_pvt_sb_set_desc( sb, s );           
140
141         if ( connected ) {
142            
143 #ifdef notyet
144                 status = 0;
145                 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
146                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
147                             s, 0, 0 );
148                 }
149 #endif /* notyet */
150
151                 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
152                     s, (char *) inet_ntoa( sin.sin_addr ), 0 );
153         }
154
155         DO_RETURN( rc );
156 }
157    
158 #undef DO_RETURN
159
160
161 void
162 ldap_close_connection( Sockbuf *sb )
163 {
164         ber_pvt_sb_close( sb );
165 }
166
167
168 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
169 char *
170 ldap_host_connected_to( Sockbuf *sb )
171 {
172         struct hostent          *hp;
173         char                    *p;
174         int                     len;
175         struct sockaddr_in      sin;
176
177         /* buffers for gethostbyaddr_r */
178         struct hostent          he_buf;
179         int                     local_h_errno;
180         char                    *ha_buf=NULL;
181 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
182    
183         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
184         len = sizeof( sin );
185
186         if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
187                 return( NULL );
188         }
189
190         /*
191          * do a reverse lookup on the addr to get the official hostname.
192          * this is necessary for kerberos to work right, since the official
193          * hostname is used as the kerberos instance.
194          */
195         if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
196                 sizeof( sin.sin_addr ), 
197                 AF_INET, &he_buf, &ha_buf,
198                 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
199         {
200                 if ( hp->h_name != NULL ) {
201                         char *host = LDAP_STRDUP( hp->h_name );   
202                         DO_RETURN( host );
203                 }
204         }
205
206         DO_RETURN( NULL );
207 }
208 #undef DO_RETURN   
209    
210 #endif /* HAVE_KERBEROS || HAVE_TLS */
211
212
213 /* for UNIX */
214 struct selectinfo {
215         fd_set  si_readfds;
216         fd_set  si_writefds;
217         fd_set  si_use_readfds;
218         fd_set  si_use_writefds;
219 };
220
221
222 void
223 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
224 {
225         struct selectinfo       *sip;
226
227         sip = (struct selectinfo *)ld->ld_selectinfo;
228         
229         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
230                 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
231         }
232 }
233
234
235 void
236 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
237 {
238         struct selectinfo       *sip;
239
240         sip = (struct selectinfo *)ld->ld_selectinfo;
241
242         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
243                 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
244         }
245 }
246
247
248 void
249 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
250 {
251         struct selectinfo       *sip;
252
253         sip = (struct selectinfo *)ld->ld_selectinfo;
254
255         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
256         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
257 }
258
259
260 int
261 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
262 {
263         struct selectinfo       *sip;
264
265         sip = (struct selectinfo *)ld->ld_selectinfo;
266
267         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
268 }
269
270
271 int
272 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
273 {
274         struct selectinfo       *sip;
275
276         sip = (struct selectinfo *)ld->ld_selectinfo;
277
278         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
279 }
280
281
282 void *
283 ldap_new_select_info( void )
284 {
285         struct selectinfo       *sip;
286
287         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
288             sizeof( struct selectinfo ))) != NULL ) {
289                 FD_ZERO( &sip->si_readfds );
290                 FD_ZERO( &sip->si_writefds );
291         }
292
293         return( (void *)sip );
294 }
295
296
297 void
298 ldap_free_select_info( void *sip )
299 {
300         LDAP_FREE( sip );
301 }
302
303
304 int
305 do_ldap_select( LDAP *ld, struct timeval *timeout )
306 {
307         struct selectinfo       *sip;
308         static int              tblsize;
309
310         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
311
312         if ( tblsize == 0 ) {
313 #if defined( HAVE_SYSCONF )
314                 tblsize = sysconf( _SC_OPEN_MAX );
315 #elif defined( HAVE_GETDTABLESIZE )
316                 tblsize = getdtablesize();
317 #else
318                 tblsize = FD_SETSIZE;
319 #endif /* !USE_SYSCONF */
320
321 #ifdef FD_SETSIZE
322                 if( tblsize > FD_SETSIZE ) {
323                         tblsize = FD_SETSIZE;
324                 }
325 #endif  /* FD_SETSIZE*/
326         }
327
328         sip = (struct selectinfo *)ld->ld_selectinfo;
329         sip->si_use_readfds = sip->si_readfds;
330         sip->si_use_writefds = sip->si_writefds;
331         
332         return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
333             NULL, timeout ));
334 }