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_SYS_FILIO_H )
29 #include <sys/filio.h>
30 #elif defined( HAVE_SYS_IOCTL_H )
31 #include <sys/ioctl.h>
36 int ldap_int_tblsize = 0;
40 ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
43 * if host == NULL, connect using address
44 * "address" and "port" must be in network byte order
45 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
46 * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
47 * XXX async is not used yet!
51 ber_socket_t s = AC_SOCKET_INVALID;
52 int connected, use_hp;
53 struct sockaddr_in sin;
54 struct hostent *hp = NULL;
56 ioctl_t status; /* for ioctl call */
59 /* buffers for ldap_pvt_gethostbyname_a */
60 struct hostent he_buf;
63 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
65 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
66 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
68 connected = use_hp = 0;
71 address = inet_addr( host );
72 /* This was just a test for -1 until OSF1 let inet_addr return
73 unsigned int, which is narrower than 'unsigned long address' */
74 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
75 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
76 &hp, &local_h_errno) < 0) || (hp==NULL))
79 errno = WSAGetLastError();
81 errno = EHOSTUNREACH; /* not exactly right, but... */
90 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
91 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
97 if( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
98 (char *) &tmp, sizeof(tmp) ) == -1 )
100 Debug( LDAP_DEBUG_ANY,
101 "setsockopt(TCP_NODELAY failed on %d\n",
108 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
109 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
113 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
114 sin.sin_family = AF_INET;
116 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
117 ( use_hp ? (char *) hp->h_addr_list[ i ] :
118 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
120 if ( connect( s, (struct sockaddr *)&sin,
121 sizeof( struct sockaddr_in )) >= 0 ) {
127 errno = WSAGetLastError();
131 if ( errno == EINPROGRESS || errno == EAGAIN ) {
133 if ( errno == EINPROGRESS ) {
135 Debug( LDAP_DEBUG_TRACE,
136 "connect would block...\n", 0, 0, 0 );
143 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
144 perror( (char *)inet_ntoa( sin.sin_addr ));
154 ber_pvt_sb_set_desc( sb, s );
160 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
161 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
166 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
167 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
177 ldap_close_connection( Sockbuf *sb )
179 ber_pvt_sb_close( sb );
183 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
185 ldap_host_connected_to( Sockbuf *sb )
190 struct sockaddr_in sin;
192 /* buffers for gethostbyaddr_r */
193 struct hostent he_buf;
196 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
198 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
201 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
206 * do a reverse lookup on the addr to get the official hostname.
207 * this is necessary for kerberos to work right, since the official
208 * hostname is used as the kerberos instance.
210 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
211 sizeof( sin.sin_addr ),
212 AF_INET, &he_buf, &ha_buf,
213 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
215 if ( hp->h_name != NULL ) {
216 char *host = LDAP_STRDUP( hp->h_name );
225 #endif /* HAVE_KERBEROS || HAVE_TLS */
232 fd_set si_use_readfds;
233 fd_set si_use_writefds;
238 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
240 struct selectinfo *sip;
242 sip = (struct selectinfo *)ld->ld_selectinfo;
244 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
245 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
251 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
253 struct selectinfo *sip;
255 sip = (struct selectinfo *)ld->ld_selectinfo;
257 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
258 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
264 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
266 struct selectinfo *sip;
268 sip = (struct selectinfo *)ld->ld_selectinfo;
270 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
271 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
276 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
278 struct selectinfo *sip;
280 sip = (struct selectinfo *)ld->ld_selectinfo;
282 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
287 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
289 struct selectinfo *sip;
291 sip = (struct selectinfo *)ld->ld_selectinfo;
293 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
298 ldap_new_select_info( void )
300 struct selectinfo *sip;
302 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
303 sizeof( struct selectinfo ))) != NULL ) {
304 FD_ZERO( &sip->si_readfds );
305 FD_ZERO( &sip->si_writefds );
308 return( (void *)sip );
313 ldap_free_select_info( void *sip )
320 ldap_int_ip_init( void )
323 #if defined( HAVE_SYSCONF )
324 tblsize = sysconf( _SC_OPEN_MAX );
325 #elif defined( HAVE_GETDTABLESIZE )
326 tblsize = getdtablesize();
328 tblsize = FD_SETSIZE;
329 #endif /* !USE_SYSCONF */
332 if( tblsize > FD_SETSIZE )
333 tblsize = FD_SETSIZE;
334 #endif /* FD_SETSIZE*/
335 ldap_int_tblsize = tblsize;
340 do_ldap_select( LDAP *ld, struct timeval *timeout )
342 struct selectinfo *sip;
344 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
346 if ( ldap_int_tblsize == 0 )
349 sip = (struct selectinfo *)ld->ld_selectinfo;
350 sip->si_use_readfds = sip->si_readfds;
351 sip->si_use_writefds = sip->si_writefds;
353 return( select( ldap_int_tblsize,
354 &sip->si_use_readfds, &sip->si_use_writefds,