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 int connected, use_hp;
49 struct sockaddr_in sin;
50 struct hostent *hp = NULL;
52 ioctl_t status; /* for ioctl call */
55 /* buffers for ldap_pvt_gethostbyname_a */
56 struct hostent he_buf;
59 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
61 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
62 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
64 connected = use_hp = 0;
67 address = inet_addr( host );
68 /* This was just a test for -1 until OSF1 let inet_addr return
69 unsigned int, which is narrower than 'unsigned long address' */
70 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
71 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
72 &hp, &local_h_errno) < 0) || (hp==NULL))
75 errno = WSAGetLastError();
77 errno = EHOSTUNREACH; /* not exactly right, but... */
86 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
87 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
92 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
93 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
97 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
98 sin.sin_family = AF_INET;
100 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
101 ( use_hp ? (char *) hp->h_addr_list[ i ] :
102 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
104 if ( connect( s, (struct sockaddr *)&sin,
105 sizeof( struct sockaddr_in )) >= 0 ) {
111 errno = WSAGetLastError();
115 if ( errno == EINPROGRESS || errno == EAGAIN ) {
117 if ( errno == EINPROGRESS ) {
119 Debug( LDAP_DEBUG_TRACE,
120 "connect would block...\n", 0, 0, 0 );
127 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
128 perror( (char *)inet_ntoa( sin.sin_addr ));
138 ber_pvt_sb_set_desc( sb, s );
144 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
145 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
150 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
151 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
161 ldap_close_connection( Sockbuf *sb )
163 ber_pvt_sb_close( sb );
167 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
169 ldap_host_connected_to( Sockbuf *sb )
174 struct sockaddr_in sin;
176 /* buffers for gethostbyaddr_r */
177 struct hostent he_buf;
180 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
182 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
185 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
190 * do a reverse lookup on the addr to get the official hostname.
191 * this is necessary for kerberos to work right, since the official
192 * hostname is used as the kerberos instance.
194 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
195 sizeof( sin.sin_addr ),
196 AF_INET, &he_buf, &ha_buf,
197 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
199 if ( hp->h_name != NULL ) {
200 char *host = LDAP_STRDUP( hp->h_name );
209 #endif /* HAVE_KERBEROS || HAVE_TLS */
216 fd_set si_use_readfds;
217 fd_set si_use_writefds;
222 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
224 struct selectinfo *sip;
226 sip = (struct selectinfo *)ld->ld_selectinfo;
228 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
229 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
235 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
237 struct selectinfo *sip;
239 sip = (struct selectinfo *)ld->ld_selectinfo;
241 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
242 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
248 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
250 struct selectinfo *sip;
252 sip = (struct selectinfo *)ld->ld_selectinfo;
254 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
255 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
260 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
262 struct selectinfo *sip;
264 sip = (struct selectinfo *)ld->ld_selectinfo;
266 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
271 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
273 struct selectinfo *sip;
275 sip = (struct selectinfo *)ld->ld_selectinfo;
277 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
282 ldap_new_select_info( void )
284 struct selectinfo *sip;
286 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
287 sizeof( struct selectinfo ))) != NULL ) {
288 FD_ZERO( &sip->si_readfds );
289 FD_ZERO( &sip->si_writefds );
292 return( (void *)sip );
297 ldap_free_select_info( void *sip )
304 do_ldap_select( LDAP *ld, struct timeval *timeout )
306 struct selectinfo *sip;
309 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
311 if ( tblsize == 0 ) {
312 #if defined( HAVE_SYSCONF )
313 tblsize = sysconf( _SC_OPEN_MAX );
314 #elif defined( HAVE_GETDTABLESIZE )
315 tblsize = getdtablesize();
317 tblsize = FD_SETSIZE;
318 #endif /* !USE_SYSCONF */
321 if( tblsize > FD_SETSIZE ) {
322 tblsize = FD_SETSIZE;
324 #endif /* FD_SETSIZE*/
327 sip = (struct selectinfo *)ld->ld_selectinfo;
328 sip->si_use_readfds = sip->si_readfds;
329 sip->si_use_writefds = sip->si_writefds;
331 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,