]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
4a908d05755ed4ff62adb235a31024223975f455
[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,
121                 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
122         {
123                 osip_debug(ld, "ldap_prepare_socket: "
124                         "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
125                         fd, 0, 0);
126         }
127 }
128 #endif
129         return 0;
130 }
131
132 #undef TRACE
133 #define TRACE do { \
134         osip_debug(ld, \
135                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
136                 s, \
137                 errno, \
138                 strerror(errno) ); \
139 } while( 0 )
140
141 /*
142  * check the socket for errors after select returned.
143  */
144 static int
145 ldap_pvt_is_socket_ready(LDAP *ld, int s)
146 {
147         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
148
149 #if defined( notyet ) /* && defined( SO_ERROR ) */
150 {
151         int so_errno;
152         int dummy = sizeof(so_errno);
153         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy ) == -1 ) {
154                 return -1;
155         }
156         if ( so_errno ) {
157                 ldap_pvt_set_errno(so_errno);
158                 TRACE;
159                 return -1;
160         }
161         return 0;
162 }
163 #else
164 {
165         /* error slippery */
166         struct sockaddr_in sin;
167         char ch;
168         int dummy = sizeof(sin);
169         if ( getpeername( s, (struct sockaddr *) &sin, &dummy ) == -1 ) {
170                 /* XXX: needs to be replace with ber_stream_read() */
171                 read(s, &ch, 1);
172 #ifdef HAVE_WINSOCK
173                 ldap_pvt_set_errno( WSAGetLastError() );
174 #endif
175                 TRACE;
176                 return -1;
177         }
178         return 0;
179 }
180 #endif
181         return -1;
182 }
183 #undef TRACE
184
185 static int
186 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_in *sin, int async)
187 {
188         struct timeval  tv, *opt_tv=NULL;
189         fd_set          wfds, *z=NULL;
190
191         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
192                 tv.tv_usec = opt_tv->tv_usec;
193                 tv.tv_sec = opt_tv->tv_sec;
194         }
195
196         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
197                         s, opt_tv ? tv.tv_sec : -1L, async);
198
199         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
200                 return ( -1 );
201
202         if ( connect(s, (struct sockaddr *) sin, sizeof(struct sockaddr_in)) == 0 )
203         {
204                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
205                         return ( -1 );
206                 return ( 0 );
207         }
208
209 #ifdef HAVE_WINSOCK
210         ldap_pvt_set_errno( WSAGetLastError() );
211 #endif
212
213         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
214                 return ( -1 );
215         }
216         
217 #ifdef notyet
218         if ( async ) return ( -2 );
219 #endif
220
221         FD_ZERO(&wfds);
222         FD_SET(s, &wfds );
223
224         if ( select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL) == -1)
225                 return ( -1 );
226
227         if ( FD_ISSET(s, &wfds) ) {
228                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
229                         return ( -1 );
230                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
231                         return ( -1 );
232                 return ( 0 );
233         }
234         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
235         ldap_pvt_set_errno( ETIMEDOUT );
236         return ( -1 );
237 }
238
239 #ifndef HAVE_INET_ATON
240 int
241 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
242 {
243         unsigned long u = inet_addr( host );
244         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
245                 in->s_addr = u;
246                 return 1;
247         }
248         return 0;
249 }
250 #endif
251
252
253 int
254 ldap_connect_to_host(LDAP *ld, Sockbuf *sb, const char *host,
255                 unsigned long address, int port, int async)
256 {
257         struct sockaddr_in      sin;
258         struct in_addr          in;
259         ber_socket_t            s = AC_SOCKET_INVALID;
260         int                     rc, i, use_hp = 0;
261         struct hostent          *hp, he_buf;
262         int                     local_h_errno;
263         char                    *ha_buf=NULL, *p, *q;
264
265         osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
266         
267         if (host != NULL) {
268                 if (! inet_aton( host, &in) ) {
269                         rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
270                                         &hp, &local_h_errno);
271
272                         if ( rc < 0 )
273                                 ; /*XXX NO MEMORY? */
274
275                         if ( (rc < 0) || (hp == NULL) ) {
276 #ifdef HAVE_WINSOCK
277                                 ldap_pvt_set_errno( WSAGetLastError() );
278 #else
279                                 /* not exactly right, but... */
280                                 ldap_pvt_set_errno( EHOSTUNREACH );
281 #endif
282                                 if (ha_buf) LDAP_FREE(ha_buf);
283                                 return -1;
284                         }
285                         use_hp = 1;
286                 }
287                 address = in.s_addr;
288         }
289
290         rc = s = -1;
291         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
292
293                 if ( (s = ldap_pvt_socket( ld )) == -1 )
294                         /* use_hp ? continue : break; */
295                         break;
296            
297                 if ( ldap_pvt_prepare_socket(ld, s) == -1 ) {
298                         ldap_pvt_close_socket(ld, s);
299                         /* use_hp ? continue : break; */
300                         break;
301                 }
302
303                 (void)memset((char *)&sin, 0, sizeof(struct sockaddr_in));
304                 sin.sin_family = AF_INET;
305                 sin.sin_port = port;
306                 p = (char *)&sin.sin_addr;
307                 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
308                 SAFEMEMCPY(p, q, sizeof(sin.sin_addr) );
309
310                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
311                                 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
312
313                 rc = ldap_pvt_connect(ld, s, &sin, async);
314    
315                 if ( (rc == 0) || (rc == -2) ) {
316                         ber_pvt_sb_set_desc( sb, s );
317                         break;
318                 }
319
320                 ldap_pvt_close_socket(ld, s);
321
322                 if (!use_hp)
323                         break;
324         }
325         if (ha_buf) LDAP_FREE(ha_buf);
326         return rc;
327 }
328
329 void
330 ldap_close_connection( Sockbuf *sb )
331 {
332         ber_pvt_sb_close( sb );
333 }
334
335
336 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
337 char *
338 ldap_host_connected_to( Sockbuf *sb )
339 {
340         struct hostent          *hp;
341         socklen_t               len;
342         struct sockaddr_in      sin;
343
344         /* buffers for gethostbyaddr_r */
345         struct hostent          he_buf;
346         int                     local_h_errno;
347         char                    *ha_buf=NULL;
348 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
349    
350         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
351         len = sizeof( sin );
352
353         if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
354                 return( NULL );
355         }
356
357         /*
358          * do a reverse lookup on the addr to get the official hostname.
359          * this is necessary for kerberos to work right, since the official
360          * hostname is used as the kerberos instance.
361          */
362         if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
363                 sizeof( sin.sin_addr ), 
364                 AF_INET, &he_buf, &ha_buf,
365                 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
366         {
367                 if ( hp->h_name != NULL ) {
368                         char *host = LDAP_STRDUP( hp->h_name );   
369                         DO_RETURN( host );
370                 }
371         }
372
373         DO_RETURN( NULL );
374 }
375 #undef DO_RETURN   
376    
377 #endif /* HAVE_KERBEROS || HAVE_TLS */
378
379
380 /* for UNIX */
381 struct selectinfo {
382         fd_set  si_readfds;
383         fd_set  si_writefds;
384         fd_set  si_use_readfds;
385         fd_set  si_use_writefds;
386 };
387
388
389 void
390 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
391 {
392         struct selectinfo       *sip;
393
394         sip = (struct selectinfo *)ld->ld_selectinfo;
395         
396         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
397                 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
398         }
399 }
400
401
402 void
403 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
404 {
405         struct selectinfo       *sip;
406
407         sip = (struct selectinfo *)ld->ld_selectinfo;
408
409         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
410                 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
411         }
412 }
413
414
415 void
416 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
417 {
418         struct selectinfo       *sip;
419
420         sip = (struct selectinfo *)ld->ld_selectinfo;
421
422         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
423         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
424 }
425
426
427 int
428 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
429 {
430         struct selectinfo       *sip;
431
432         sip = (struct selectinfo *)ld->ld_selectinfo;
433
434         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
435 }
436
437
438 int
439 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
440 {
441         struct selectinfo       *sip;
442
443         sip = (struct selectinfo *)ld->ld_selectinfo;
444
445         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
446 }
447
448
449 void *
450 ldap_new_select_info( void )
451 {
452         struct selectinfo       *sip;
453
454         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
455             sizeof( struct selectinfo ))) != NULL ) {
456                 FD_ZERO( &sip->si_readfds );
457                 FD_ZERO( &sip->si_writefds );
458         }
459
460         return( (void *)sip );
461 }
462
463
464 void
465 ldap_free_select_info( void *sip )
466 {
467         LDAP_FREE( sip );
468 }
469
470
471 void
472 ldap_int_ip_init( void )
473 {
474         int tblsize;
475 #if defined( HAVE_SYSCONF )
476         tblsize = sysconf( _SC_OPEN_MAX );
477 #elif defined( HAVE_GETDTABLESIZE )
478         tblsize = getdtablesize();
479 #else
480         tblsize = FD_SETSIZE;
481 #endif /* !USE_SYSCONF */
482
483 #ifdef FD_SETSIZE
484         if( tblsize > FD_SETSIZE )
485                 tblsize = FD_SETSIZE;
486 #endif  /* FD_SETSIZE*/
487         ldap_int_tblsize = tblsize;
488 }
489
490
491 int
492 do_ldap_select( LDAP *ld, struct timeval *timeout )
493 {
494         struct selectinfo       *sip;
495
496         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
497
498         if ( ldap_int_tblsize == 0 )
499                 ldap_int_ip_init();
500
501         sip = (struct selectinfo *)ld->ld_selectinfo;
502         sip->si_use_readfds = sip->si_readfds;
503         sip->si_use_writefds = sip->si_writefds;
504         
505         return( select( ldap_int_tblsize,
506                         &sip->si_use_readfds, &sip->si_use_writefds,
507                         NULL, timeout ));
508 }