]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Howard convinced me that this version is slightly better.
[openldap] / libraries / libldap / os-ip.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  os-ip.c -- platform-specific TCP & UDP related code
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/errno.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/unistd.h>
24
25 #ifdef HAVE_IO_H
26 #include <io.h>
27 #endif /* HAVE_IO_H */
28
29 #include "ldap-int.h"
30
31 int ldap_int_tblsize = 0;
32
33 /*
34  * nonblock connect code
35  * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
36  *
37  * Copyright 1999, Lars Uffmann, All rights reserved.
38  * This software is not subject to any license of my employer
39  * mediaWays GmbH.
40  *
41  * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
42  *
43  * Read about the rationale in ldap_connect_timeout: 
44  * ftp://koobera.math.uic.edu/www/docs/connect.html.
45  */
46
47 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
48 do { \
49         ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
50 } while(0)
51
52 static void
53 ldap_pvt_set_errno(int err)
54 {
55         errno = err;
56 }
57
58 int
59 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
60 {
61         struct timeval *new;
62
63         assert( dest != NULL );
64
65         if (src == NULL) {
66                 *dest = NULL;
67                 return 0;
68         }
69
70         new = (struct timeval *) malloc(sizeof(struct timeval));
71
72         if( new == NULL ) {
73                 *dest = NULL;
74                 return 1;
75         }
76
77         SAFEMEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
78
79         *dest = new;
80         return 0;
81 }
82
83 static int
84 ldap_pvt_ndelay_on(LDAP *ld, int fd)
85 {
86         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
87         return ber_pvt_socket_set_nonblock( fd, 1 );
88 }
89    
90 static int
91 ldap_pvt_ndelay_off(LDAP *ld, int fd)
92 {
93         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
94         return ber_pvt_socket_set_nonblock( fd, 0 );
95 }
96
97 static ber_socket_t
98 ldap_pvt_socket(LDAP *ld)
99 {
100         ber_socket_t s = socket(AF_INET, SOCK_STREAM, 0);
101         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
102         return ( s );
103 }
104
105 static int
106 ldap_pvt_close_socket(LDAP *ld, int s)
107 {
108         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
109         return tcp_close(s);
110 }
111
112 static int
113 ldap_pvt_prepare_socket(LDAP *ld, int fd)
114 {
115         osip_debug(ld, "ldap_prepare_socket: %d\n",fd,0,0);
116
117 #ifdef TCP_NODELAY
118 {
119         int dummy = 1;
120         if ( setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (char*) &dummy, sizeof(dummy) ) == -1 )
121                 return -1;
122 }
123 #endif
124         return 0;
125 }
126
127 #undef TRACE
128 #define TRACE do { \
129         osip_debug(ld, \
130                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
131                 s, \
132                 errno, \
133                 strerror(errno) ); \
134 } while( 0 )
135
136 /*
137  * check the socket for errors after select returned.
138  */
139 static int
140 ldap_pvt_is_socket_ready(LDAP *ld, int s)
141 {
142         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
143
144 #if defined( notyet ) /* && defined( SO_ERROR ) */
145 {
146         int so_errno;
147         int dummy = sizeof(so_errno);
148         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy ) == -1 ) {
149                 return -1;
150         }
151         if ( so_errno ) {
152                 ldap_pvt_set_errno(so_errno);
153                 TRACE;
154                 return -1;
155         }
156         return 0;
157 }
158 #else
159 {
160         /* error slippery */
161         struct sockaddr_in sin;
162         char ch;
163         int dummy = sizeof(sin);
164         if ( getpeername( s, (struct sockaddr *) &sin, &dummy ) == -1 ) {
165                 /* XXX: needs to be replace with ber_stream_read() */
166                 read(s, &ch, 1);
167 #ifdef HAVE_WINSOCK
168                 ldap_pvt_set_errno( WSAGetLastError() );
169 #endif
170                 TRACE;
171                 return -1;
172         }
173         return 0;
174 }
175 #endif
176         return -1;
177 }
178 #undef TRACE
179
180 static int
181 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_in *sin, int async)
182 {
183         struct timeval  tv, *opt_tv=NULL;
184         fd_set          wfds, *z=NULL;
185
186         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
187                 tv.tv_usec = opt_tv->tv_usec;
188                 tv.tv_sec = opt_tv->tv_sec;
189         }
190
191         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
192                         s, opt_tv ? tv.tv_sec : -1L, async);
193
194         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
195                 return ( -1 );
196
197         if ( connect(s, (struct sockaddr *) sin, sizeof(struct sockaddr_in)) == 0 )
198         {
199                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
200                         return ( -1 );
201                 return ( 0 );
202         }
203
204 #ifdef HAVE_WINSOCK
205         ldap_pvt_set_errno( WSAGetLastError() );
206 #endif
207
208         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
209                 return ( -1 );
210         }
211         
212 #ifdef notyet
213         if ( async ) return ( -2 );
214 #endif
215
216         FD_ZERO(&wfds);
217         FD_SET(s, &wfds );
218
219         if ( select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL) == -1)
220                 return ( -1 );
221
222         if ( FD_ISSET(s, &wfds) ) {
223                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
224                         return ( -1 );
225                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
226                         return ( -1 );
227                 return ( 0 );
228         }
229         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
230         ldap_pvt_set_errno( ETIMEDOUT );
231         return ( -1 );
232 }
233
234 #ifndef HAVE_INET_ATON
235 int
236 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
237 {
238         unsigned long u = inet_addr( host );
239         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
240                 in->s_addr = u;
241                 return 1;
242         }
243         return 0;
244 }
245 #endif
246
247
248 int
249 ldap_connect_to_host(LDAP *ld, Sockbuf *sb, const char *host,
250                 unsigned long address, int port, int async)
251 {
252         struct sockaddr_in      sin;
253         struct in_addr          in;
254         ber_socket_t            s = AC_SOCKET_INVALID;
255         int                     rc, i, use_hp = 0;
256         struct hostent          *hp, he_buf;
257         int                     local_h_errno;
258         char                    *ha_buf=NULL, *p, *q;
259
260         osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
261         
262         if (host != NULL) {
263                 if (! inet_aton( host, &in) ) {
264                         rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
265                                         &hp, &local_h_errno);
266
267                         if ( rc < 0 )
268                                 ; /*XXX NO MEMORY? */
269
270                         if ( (rc < 0) || (hp == NULL) ) {
271 #ifdef HAVE_WINSOCK
272                                 ldap_pvt_set_errno( WSAGetLastError() );
273 #else
274                                 /* not exactly right, but... */
275                                 ldap_pvt_set_errno( EHOSTUNREACH );
276 #endif
277                                 if (ha_buf) LDAP_FREE(ha_buf);
278                                 return -1;
279                         }
280                         use_hp = 1;
281                 }
282                 address = in.s_addr;
283         }
284
285         rc = s = -1;
286         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
287
288                 if ( (s = ldap_pvt_socket( ld )) == -1 )
289                         /* use_hp ? continue : break; */
290                         break;
291            
292                 if ( ldap_pvt_prepare_socket(ld, s) == -1 ) {
293                         ldap_pvt_close_socket(ld, s);
294                         /* use_hp ? continue : break; */
295                         break;
296                 }
297
298                 (void)memset((char *)&sin, 0, sizeof(struct sockaddr_in));
299                 sin.sin_family = AF_INET;
300                 sin.sin_port = port;
301                 p = (char *)&sin.sin_addr.s_addr;
302                 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
303                 SAFEMEMCPY(p, q, sizeof(p) );
304
305                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
306                                 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
307
308                 rc = ldap_pvt_connect(ld, s, &sin, async);
309    
310                 if ( (rc == 0) || (rc == -2) ) {
311                         ber_pvt_sb_set_desc( sb, s );
312                         break;
313                 }
314
315                 ldap_pvt_close_socket(ld, s);
316
317                 if (!use_hp)
318                         break;
319         }
320         if (ha_buf) LDAP_FREE(ha_buf);
321         return rc;
322 }
323
324 void
325 ldap_close_connection( Sockbuf *sb )
326 {
327         ber_pvt_sb_close( sb );
328 }
329
330
331 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
332 char *
333 ldap_host_connected_to( Sockbuf *sb )
334 {
335         struct hostent          *hp;
336         char                    *p;
337         socklen_t               len;
338         struct sockaddr_in      sin;
339
340         /* buffers for gethostbyaddr_r */
341         struct hostent          he_buf;
342         int                     local_h_errno;
343         char                    *ha_buf=NULL;
344 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
345    
346         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
347         len = sizeof( sin );
348
349         if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
350                 return( NULL );
351         }
352
353         /*
354          * do a reverse lookup on the addr to get the official hostname.
355          * this is necessary for kerberos to work right, since the official
356          * hostname is used as the kerberos instance.
357          */
358         if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
359                 sizeof( sin.sin_addr ), 
360                 AF_INET, &he_buf, &ha_buf,
361                 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
362         {
363                 if ( hp->h_name != NULL ) {
364                         char *host = LDAP_STRDUP( hp->h_name );   
365                         DO_RETURN( host );
366                 }
367         }
368
369         DO_RETURN( NULL );
370 }
371 #undef DO_RETURN   
372    
373 #endif /* HAVE_KERBEROS || HAVE_TLS */
374
375
376 /* for UNIX */
377 struct selectinfo {
378         fd_set  si_readfds;
379         fd_set  si_writefds;
380         fd_set  si_use_readfds;
381         fd_set  si_use_writefds;
382 };
383
384
385 void
386 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
387 {
388         struct selectinfo       *sip;
389
390         sip = (struct selectinfo *)ld->ld_selectinfo;
391         
392         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
393                 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
394         }
395 }
396
397
398 void
399 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
400 {
401         struct selectinfo       *sip;
402
403         sip = (struct selectinfo *)ld->ld_selectinfo;
404
405         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
406                 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
407         }
408 }
409
410
411 void
412 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
413 {
414         struct selectinfo       *sip;
415
416         sip = (struct selectinfo *)ld->ld_selectinfo;
417
418         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
419         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
420 }
421
422
423 int
424 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
425 {
426         struct selectinfo       *sip;
427
428         sip = (struct selectinfo *)ld->ld_selectinfo;
429
430         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
431 }
432
433
434 int
435 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
436 {
437         struct selectinfo       *sip;
438
439         sip = (struct selectinfo *)ld->ld_selectinfo;
440
441         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
442 }
443
444
445 void *
446 ldap_new_select_info( void )
447 {
448         struct selectinfo       *sip;
449
450         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
451             sizeof( struct selectinfo ))) != NULL ) {
452                 FD_ZERO( &sip->si_readfds );
453                 FD_ZERO( &sip->si_writefds );
454         }
455
456         return( (void *)sip );
457 }
458
459
460 void
461 ldap_free_select_info( void *sip )
462 {
463         LDAP_FREE( sip );
464 }
465
466
467 void
468 ldap_int_ip_init( void )
469 {
470         int tblsize;
471 #if defined( HAVE_SYSCONF )
472         tblsize = sysconf( _SC_OPEN_MAX );
473 #elif defined( HAVE_GETDTABLESIZE )
474         tblsize = getdtablesize();
475 #else
476         tblsize = FD_SETSIZE;
477 #endif /* !USE_SYSCONF */
478
479 #ifdef FD_SETSIZE
480         if( tblsize > FD_SETSIZE )
481                 tblsize = FD_SETSIZE;
482 #endif  /* FD_SETSIZE*/
483         ldap_int_tblsize = tblsize;
484 }
485
486
487 int
488 do_ldap_select( LDAP *ld, struct timeval *timeout )
489 {
490         struct selectinfo       *sip;
491
492         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
493
494         if ( ldap_int_tblsize == 0 )
495                 ldap_int_ip_init();
496
497         sip = (struct selectinfo *)ld->ld_selectinfo;
498         sip->si_use_readfds = sip->si_readfds;
499         sip->si_use_writefds = sip->si_writefds;
500         
501         return( select( ldap_int_tblsize,
502                         &sip->si_use_readfds, &sip->si_use_writefds,
503                         NULL, timeout ));
504 }