2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * os-ip.c -- platform-specific TCP & UDP related code
16 #include <ac/stdlib.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
22 #include <ac/unistd.h>
26 #endif /* HAVE_IO_H */
28 #if defined( HAVE_FCNTL_H )
31 #define O_NONBLOCK O_NDELAY
33 #endif /* HAVE_FCNTL_H */
35 #if defined( HAVE_SYS_FILIO_H )
36 #include <sys/filio.h>
37 #elif defined( HAVE_SYS_IOCTL_H )
38 #include <sys/ioctl.h>
43 int ldap_int_tblsize = 0;
46 * nonblock connect code
47 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
49 * Copyright 1999, Lars Uffmann, All rights reserved.
50 * This software is not subject to any license of my employer
53 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
55 * Read about the rationale in ldap_connect_timeout:
56 * ftp://koobera.math.uic.edu/www/docs/connect.html.
59 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
61 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
65 ldap_pvt_set_errno(int err)
71 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
75 assert( dest != NULL );
82 new = (struct timeval *) malloc(sizeof(struct timeval));
89 SAFEMEMCPY( (char *) new, (char *) src, sizeof(struct timeval));
96 ldap_pvt_ndelay_on(LDAP *ld, int fd)
98 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
100 /* #if defined( HAVE_FCNTL_H ) */
101 return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NONBLOCK);
105 return ioctl( fd, FIONBIO, (caddr_t)&status );
112 ldap_pvt_ndelay_off(LDAP *ld, int fd)
114 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
116 /* #if defined( HAVE_FCNTL_H ) */
117 return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NONBLOCK);
121 return ioctl( fd, FIONBIO, (caddr_t)&status );
127 ldap_pvt_socket(LDAP *ld)
129 ber_socket_t s = socket(AF_INET, SOCK_STREAM, 0);
130 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
135 ldap_pvt_close_socket(LDAP *ld, int s)
137 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
142 ldap_pvt_prepare_socket(LDAP *ld, int fd)
144 osip_debug(ld, "ldap_prepare_socket: %d\n",fd,0,0);
149 if ( setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,&dummy,sizeof(dummy)) == -1 )
157 * check the socket for errors after select returned.
160 ldap_pvt_is_socket_ready(LDAP *ld, int s)
162 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
167 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
174 /* #ifdef SO_ERROR */
177 int dummy = sizeof(so_errno);
178 if ( getsockopt(s,SOL_SOCKET,SO_ERROR,&so_errno,&dummy) == -1 )
181 ldap_pvt_set_errno(so_errno);
190 struct sockaddr_in sin;
192 int dummy = sizeof(sin);
193 if ( getpeername(s, (struct sockaddr *) &sin, &dummy) == -1 ) {
196 ldap_pvt_set_errno( WSAGetLastError() );
209 ldap_pvt_connect(LDAP *ld, int s, struct sockaddr_in *sin, int async)
211 struct timeval tv, *opt_tv=NULL;
212 fd_set wfds, *z=NULL;
214 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
215 tv.tv_usec = opt_tv->tv_usec;
216 tv.tv_sec = opt_tv->tv_sec;
219 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %d async: %d\n",
220 s, opt_tv ? tv.tv_sec : -1, async);
222 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
225 if ( connect(s, (struct sockaddr *) sin, sizeof(struct sockaddr_in)) == 0 )
227 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
233 ldap_pvt_set_errno( WSAGetLastError() );
236 if ( (errno != EINPROGRESS) && (errno != EWOULDBLOCK) )
240 if ( async ) return ( -2 );
243 FD_ZERO(&wfds); FD_SET(s, &wfds );
245 if ( select(s + 1, z, &wfds, z, opt_tv ? &tv : NULL) == -1)
248 if ( FD_ISSET(s, &wfds) ) {
249 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
251 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
255 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
256 ldap_pvt_set_errno( ETIMEDOUT );
261 ldap_pvt_inet_aton( LDAP *ld, const char *host, struct in_addr *in)
264 /* #ifdef HAVE_INET_ATON */
265 return inet_aton( host, in );
268 unsigned long u = inet_addr( host );
269 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
280 ldap_connect_to_host(LDAP *ld, Sockbuf *sb, const char *host,
281 unsigned long address, int port, int async)
283 struct sockaddr_in sin;
285 ber_socket_t s = AC_SOCKET_INVALID;
286 int rc, i, use_hp = 0;
287 struct hostent *hp, he_buf;
289 char *ha_buf=NULL, *p, *q;
291 osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
294 if (! ldap_pvt_inet_aton( ld, host, &in) ) {
295 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
296 &hp, &local_h_errno);
299 ; /*XXX NO MEMORY? */
301 if ( (rc < 0) || (hp == NULL) ) {
303 ldap_pvt_set_errno( WSAGetLastError() );
305 /* not exactly right, but... */
306 ldap_pvt_set_errno( EHOSTUNREACH );
308 if (ha_buf) LDAP_FREE(ha_buf);
317 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
319 if ( (s = ldap_pvt_socket( ld )) == -1 )
320 /* use_hp ? continue : break; */
323 if ( ldap_pvt_prepare_socket(ld, s) == -1 ) {
324 ldap_pvt_close_socket(ld, s);
325 /* use_hp ? continue : break; */
329 (void)memset((char *)&sin, 0, sizeof(struct sockaddr_in));
330 sin.sin_family = AF_INET;
332 p = (char *)&sin.sin_addr.s_addr;
333 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
334 SAFEMEMCPY(p, q, sizeof(p) );
336 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
337 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
339 rc = ldap_pvt_connect(ld, s, &sin, async);
341 if ( (rc == 0) || (rc == -2) ) {
342 ber_pvt_sb_set_desc( sb, s );
346 ldap_pvt_close_socket(ld, s);
351 if (ha_buf) LDAP_FREE(ha_buf);
356 ldap_close_connection( Sockbuf *sb )
358 ber_pvt_sb_close( sb );
362 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
364 ldap_host_connected_to( Sockbuf *sb )
369 struct sockaddr_in sin;
371 /* buffers for gethostbyaddr_r */
372 struct hostent he_buf;
375 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
377 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
380 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
385 * do a reverse lookup on the addr to get the official hostname.
386 * this is necessary for kerberos to work right, since the official
387 * hostname is used as the kerberos instance.
389 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
390 sizeof( sin.sin_addr ),
391 AF_INET, &he_buf, &ha_buf,
392 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
394 if ( hp->h_name != NULL ) {
395 char *host = LDAP_STRDUP( hp->h_name );
404 #endif /* HAVE_KERBEROS || HAVE_TLS */
411 fd_set si_use_readfds;
412 fd_set si_use_writefds;
417 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
419 struct selectinfo *sip;
421 sip = (struct selectinfo *)ld->ld_selectinfo;
423 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
424 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
430 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
432 struct selectinfo *sip;
434 sip = (struct selectinfo *)ld->ld_selectinfo;
436 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
437 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
443 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
445 struct selectinfo *sip;
447 sip = (struct selectinfo *)ld->ld_selectinfo;
449 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
450 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
455 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
457 struct selectinfo *sip;
459 sip = (struct selectinfo *)ld->ld_selectinfo;
461 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
466 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
468 struct selectinfo *sip;
470 sip = (struct selectinfo *)ld->ld_selectinfo;
472 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
477 ldap_new_select_info( void )
479 struct selectinfo *sip;
481 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
482 sizeof( struct selectinfo ))) != NULL ) {
483 FD_ZERO( &sip->si_readfds );
484 FD_ZERO( &sip->si_writefds );
487 return( (void *)sip );
492 ldap_free_select_info( void *sip )
499 ldap_int_ip_init( void )
502 #if defined( HAVE_SYSCONF )
503 tblsize = sysconf( _SC_OPEN_MAX );
504 #elif defined( HAVE_GETDTABLESIZE )
505 tblsize = getdtablesize();
507 tblsize = FD_SETSIZE;
508 #endif /* !USE_SYSCONF */
511 if( tblsize > FD_SETSIZE )
512 tblsize = FD_SETSIZE;
513 #endif /* FD_SETSIZE*/
514 ldap_int_tblsize = tblsize;
519 do_ldap_select( LDAP *ld, struct timeval *timeout )
521 struct selectinfo *sip;
523 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
525 if ( ldap_int_tblsize == 0 )
528 sip = (struct selectinfo *)ld->ld_selectinfo;
529 sip->si_use_readfds = sip->si_readfds;
530 sip->si_use_writefds = sip->si_writefds;
532 return( select( ldap_int_tblsize,
533 &sip->si_use_readfds, &sip->si_use_writefds,