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
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 #include <ac/unistd.h>
25 #endif /* HAVE_IO_H */
27 #if defined( HAVE_SYS_FILIO_H )
28 #include <sys/filio.h>
29 #elif defined( HAVE_SYS_IOCTL_H )
30 #include <sys/ioctl.h>
36 ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
39 * if host == NULL, connect using address
40 * "address" and "port" must be in network byte order
41 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
42 * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
43 * XXX async is not used yet!
47 int connected, use_hp;
48 struct sockaddr_in sin;
49 struct hostent *hp = NULL;
51 ioctl_t status; /* for ioctl call */
54 /* buffers for ldap_pvt_gethostbyname_a */
55 struct hostent he_buf;
58 #define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
60 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
61 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
63 connected = use_hp = 0;
66 address = inet_addr( host );
67 /* This was just a test for -1 until OSF1 let inet_addr return
68 unsigned int, which is narrower than 'unsigned long address' */
69 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
70 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
71 &hp, &local_h_errno) < 0) || (hp==NULL))
74 errno = WSAGetLastError();
76 errno = EHOSTUNREACH; /* not exactly right, but... */
85 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
86 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
91 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
92 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
96 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
97 sin.sin_family = AF_INET;
99 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
100 ( use_hp ? (char *) hp->h_addr_list[ i ] :
101 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
103 if ( connect( s, (struct sockaddr *)&sin,
104 sizeof( struct sockaddr_in )) >= 0 ) {
110 errno = WSAGetLastError();
114 if ( errno == EINPROGRESS || errno == EAGAIN ) {
116 if ( errno == EINPROGRESS ) {
118 Debug( LDAP_DEBUG_TRACE,
119 "connect would block...\n", 0, 0, 0 );
126 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
127 perror( (char *)inet_ntoa( sin.sin_addr ));
137 ber_pvt_sb_set_desc( sb, s );
143 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
144 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
149 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
150 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
160 ldap_close_connection( Sockbuf *sb )
162 ber_pvt_sb_close( sb );
166 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
168 ldap_host_connected_to( Sockbuf *sb )
173 struct sockaddr_in sin;
175 /* buffers for gethostbyaddr_r */
176 struct hostent he_buf;
179 #define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
181 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
184 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
189 * do a reverse lookup on the addr to get the official hostname.
190 * this is necessary for kerberos to work right, since the official
191 * hostname is used as the kerberos instance.
193 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
194 sizeof( sin.sin_addr ),
195 AF_INET, &he_buf, &ha_buf,
196 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
198 if ( hp->h_name != NULL ) {
199 char *host = strdup( hp->h_name );
208 #endif /* HAVE_KERBEROS || HAVE_TLS */
215 fd_set si_use_readfds;
216 fd_set si_use_writefds;
221 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
223 struct selectinfo *sip;
225 sip = (struct selectinfo *)ld->ld_selectinfo;
227 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
228 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
234 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
236 struct selectinfo *sip;
238 sip = (struct selectinfo *)ld->ld_selectinfo;
240 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
241 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
247 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
249 struct selectinfo *sip;
251 sip = (struct selectinfo *)ld->ld_selectinfo;
253 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
254 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
259 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
261 struct selectinfo *sip;
263 sip = (struct selectinfo *)ld->ld_selectinfo;
265 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
270 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
272 struct selectinfo *sip;
274 sip = (struct selectinfo *)ld->ld_selectinfo;
276 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
281 ldap_new_select_info( void )
283 struct selectinfo *sip;
285 if (( sip = (struct selectinfo *)calloc( 1,
286 sizeof( struct selectinfo ))) != NULL ) {
287 FD_ZERO( &sip->si_readfds );
288 FD_ZERO( &sip->si_writefds );
291 return( (void *)sip );
296 ldap_free_select_info( void *sip )
303 do_ldap_select( LDAP *ld, struct timeval *timeout )
305 struct selectinfo *sip;
308 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
310 if ( tblsize == 0 ) {
311 #if defined( HAVE_SYSCONF )
312 tblsize = sysconf( _SC_OPEN_MAX );
313 #elif defined( HAVE_GETDTABLESIZE )
314 tblsize = getdtablesize();
316 tblsize = FD_SETSIZE;
317 #endif /* !USE_SYSCONF */
320 if( tblsize > FD_SETSIZE ) {
321 tblsize = FD_SETSIZE;
323 #endif /* FD_SETSIZE*/
326 sip = (struct selectinfo *)ld->ld_selectinfo;
327 sip->si_use_readfds = sip->si_readfds;
328 sip->si_use_writefds = sip->si_writefds;
330 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,