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>
37 ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
40 * if host == NULL, connect using address
41 * "address" and "port" must be in network byte order
42 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
43 * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
44 * XXX async is not used yet!
48 ber_socket_t s = AC_SOCKET_INVALID;
49 int connected, use_hp;
50 struct sockaddr_in sin;
51 struct hostent *hp = NULL;
53 ioctl_t status; /* for ioctl call */
56 /* buffers for ldap_pvt_gethostbyname_a */
57 struct hostent he_buf;
60 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
62 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
63 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
65 connected = use_hp = 0;
68 address = inet_addr( host );
69 /* This was just a test for -1 until OSF1 let inet_addr return
70 unsigned int, which is narrower than 'unsigned long address' */
71 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
72 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
73 &hp, &local_h_errno) < 0) || (hp==NULL))
76 errno = WSAGetLastError();
78 errno = EHOSTUNREACH; /* not exactly right, but... */
87 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
88 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
93 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
94 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
98 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
99 sin.sin_family = AF_INET;
101 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
102 ( use_hp ? (char *) hp->h_addr_list[ i ] :
103 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
105 if ( connect( s, (struct sockaddr *)&sin,
106 sizeof( struct sockaddr_in )) >= 0 ) {
112 errno = WSAGetLastError();
116 if ( errno == EINPROGRESS || errno == EAGAIN ) {
118 if ( errno == EINPROGRESS ) {
120 Debug( LDAP_DEBUG_TRACE,
121 "connect would block...\n", 0, 0, 0 );
128 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
129 perror( (char *)inet_ntoa( sin.sin_addr ));
139 ber_pvt_sb_set_desc( sb, s );
145 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
146 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
151 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
152 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
162 ldap_close_connection( Sockbuf *sb )
164 ber_pvt_sb_close( sb );
168 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
170 ldap_host_connected_to( Sockbuf *sb )
175 struct sockaddr_in sin;
177 /* buffers for gethostbyaddr_r */
178 struct hostent he_buf;
181 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
183 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
186 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
191 * do a reverse lookup on the addr to get the official hostname.
192 * this is necessary for kerberos to work right, since the official
193 * hostname is used as the kerberos instance.
195 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
196 sizeof( sin.sin_addr ),
197 AF_INET, &he_buf, &ha_buf,
198 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
200 if ( hp->h_name != NULL ) {
201 char *host = LDAP_STRDUP( hp->h_name );
210 #endif /* HAVE_KERBEROS || HAVE_TLS */
217 fd_set si_use_readfds;
218 fd_set si_use_writefds;
223 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
225 struct selectinfo *sip;
227 sip = (struct selectinfo *)ld->ld_selectinfo;
229 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
230 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
236 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
238 struct selectinfo *sip;
240 sip = (struct selectinfo *)ld->ld_selectinfo;
242 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
243 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
249 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
251 struct selectinfo *sip;
253 sip = (struct selectinfo *)ld->ld_selectinfo;
255 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
256 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
261 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
263 struct selectinfo *sip;
265 sip = (struct selectinfo *)ld->ld_selectinfo;
267 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
272 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
274 struct selectinfo *sip;
276 sip = (struct selectinfo *)ld->ld_selectinfo;
278 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
283 ldap_new_select_info( void )
285 struct selectinfo *sip;
287 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
288 sizeof( struct selectinfo ))) != NULL ) {
289 FD_ZERO( &sip->si_readfds );
290 FD_ZERO( &sip->si_writefds );
293 return( (void *)sip );
298 ldap_free_select_info( void *sip )
305 do_ldap_select( LDAP *ld, struct timeval *timeout )
307 struct selectinfo *sip;
310 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
312 if ( tblsize == 0 ) {
313 #if defined( HAVE_SYSCONF )
314 tblsize = sysconf( _SC_OPEN_MAX );
315 #elif defined( HAVE_GETDTABLESIZE )
316 tblsize = getdtablesize();
318 tblsize = FD_SETSIZE;
319 #endif /* !USE_SYSCONF */
322 if( tblsize > FD_SETSIZE ) {
323 tblsize = FD_SETSIZE;
325 #endif /* FD_SETSIZE*/
328 sip = (struct selectinfo *)ld->ld_selectinfo;
329 sip->si_use_readfds = sip->si_readfds;
330 sip->si_use_writefds = sip->si_writefds;
332 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,