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, 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 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
52 int status; /* for ioctl call */
53 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
56 /* buffers for ldap_int_gethostbyname_a */
57 struct hostent he_buf;
60 #define DO_RETURN(x) if (ha_buf) 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_int_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 ) {
92 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
94 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
95 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
98 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
100 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
101 sin.sin_family = AF_INET;
103 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
104 ( use_hp ? (char *) hp->h_addr_list[ i ] :
105 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
107 if ( connect( s, (struct sockaddr *)&sin,
108 sizeof( struct sockaddr_in )) >= 0 ) {
114 errno = WSAGetLastError();
117 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
119 if ( errno == EINPROGRESS || errno == EAGAIN ) {
121 if ( errno == EINPROGRESS ) {
123 Debug( LDAP_DEBUG_TRACE,
124 "connect would block...\n", 0, 0, 0 );
128 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
132 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
133 perror( (char *)inet_ntoa( sin.sin_addr ));
147 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
149 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
150 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
153 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
156 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
157 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
169 ldap_close_connection( Sockbuf *sb )
171 tcp_close( sb->sb_sd );
177 ldap_host_connected_to( Sockbuf *sb )
182 struct sockaddr_in sin;
184 /* buffers for gethostbyaddr_r */
185 struct hostent he_buf;
188 #define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
190 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
192 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
197 * do a reverse lookup on the addr to get the official hostname.
198 * this is necessary for kerberos to work right, since the official
199 * hostname is used as the kerberos instance.
201 if ((ldap_int_gethostbyaddr_a( (char *) &sin.sin_addr,
202 sizeof( sin.sin_addr ),
203 AF_INET, &he_buf, &ha_buf,
204 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
206 if ( hp->h_name != NULL ) {
207 DO_RETURN( ldap_strdup( hp->h_name ));
215 #endif /* HAVE_KERBEROS */
218 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
223 fd_set si_use_readfds;
224 fd_set si_use_writefds;
229 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
231 struct selectinfo *sip;
233 sip = (struct selectinfo *)ld->ld_selectinfo;
235 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
236 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
242 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
244 struct selectinfo *sip;
246 sip = (struct selectinfo *)ld->ld_selectinfo;
248 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
249 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
255 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
257 struct selectinfo *sip;
259 sip = (struct selectinfo *)ld->ld_selectinfo;
261 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
262 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
267 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
269 struct selectinfo *sip;
271 sip = (struct selectinfo *)ld->ld_selectinfo;
273 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
278 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
280 struct selectinfo *sip;
282 sip = (struct selectinfo *)ld->ld_selectinfo;
284 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
289 ldap_new_select_info( void )
291 struct selectinfo *sip;
293 if (( sip = (struct selectinfo *)calloc( 1,
294 sizeof( struct selectinfo ))) != NULL ) {
295 FD_ZERO( &sip->si_readfds );
296 FD_ZERO( &sip->si_writefds );
299 return( (void *)sip );
304 ldap_free_select_info( void *sip )
311 do_ldap_select( LDAP *ld, struct timeval *timeout )
313 struct selectinfo *sip;
316 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
318 if ( tblsize == 0 ) {
319 #if defined( HAVE_SYSCONF )
320 tblsize = sysconf( _SC_OPEN_MAX );
321 #elif defined( HAVE_GETDTABLESIZE )
322 tblsize = getdtablesize();
324 tblsize = FD_SETSIZE;
325 #endif /* !USE_SYSCONF */
328 if( tblsize > FD_SETSIZE ) {
329 tblsize = FD_SETSIZE;
331 #endif /* FD_SETSIZE*/
334 sip = (struct selectinfo *)ld->ld_selectinfo;
335 sip->si_use_readfds = sip->si_readfds;
336 sip->si_use_writefds = sip->si_writefds;
338 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
341 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */