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 ) {
96 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
97 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
101 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
102 sin.sin_family = AF_INET;
104 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
105 ( use_hp ? (char *) hp->h_addr_list[ i ] :
106 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
108 if ( connect( s, (struct sockaddr *)&sin,
109 sizeof( struct sockaddr_in )) >= 0 ) {
115 errno = WSAGetLastError();
119 if ( errno == EINPROGRESS || errno == EAGAIN ) {
121 if ( errno == EINPROGRESS ) {
123 Debug( LDAP_DEBUG_TRACE,
124 "connect would block...\n", 0, 0, 0 );
131 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
132 perror( (char *)inet_ntoa( sin.sin_addr ));
142 ber_pvt_sb_set_desc( sb, s );
148 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
149 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
154 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
155 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
165 ldap_close_connection( Sockbuf *sb )
167 ber_pvt_sb_close( sb );
171 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
173 ldap_host_connected_to( Sockbuf *sb )
178 struct sockaddr_in sin;
180 /* buffers for gethostbyaddr_r */
181 struct hostent he_buf;
184 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
186 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
189 if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
194 * do a reverse lookup on the addr to get the official hostname.
195 * this is necessary for kerberos to work right, since the official
196 * hostname is used as the kerberos instance.
198 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
199 sizeof( sin.sin_addr ),
200 AF_INET, &he_buf, &ha_buf,
201 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
203 if ( hp->h_name != NULL ) {
204 char *host = LDAP_STRDUP( hp->h_name );
213 #endif /* HAVE_KERBEROS || HAVE_TLS */
220 fd_set si_use_readfds;
221 fd_set si_use_writefds;
226 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
228 struct selectinfo *sip;
230 sip = (struct selectinfo *)ld->ld_selectinfo;
232 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
233 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
239 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
241 struct selectinfo *sip;
243 sip = (struct selectinfo *)ld->ld_selectinfo;
245 if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
246 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
252 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
254 struct selectinfo *sip;
256 sip = (struct selectinfo *)ld->ld_selectinfo;
258 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
259 FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
264 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
266 struct selectinfo *sip;
268 sip = (struct selectinfo *)ld->ld_selectinfo;
270 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
275 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
277 struct selectinfo *sip;
279 sip = (struct selectinfo *)ld->ld_selectinfo;
281 return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
286 ldap_new_select_info( void )
288 struct selectinfo *sip;
290 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
291 sizeof( struct selectinfo ))) != NULL ) {
292 FD_ZERO( &sip->si_readfds );
293 FD_ZERO( &sip->si_writefds );
296 return( (void *)sip );
301 ldap_free_select_info( void *sip )
308 ldap_int_ip_init( void )
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;
322 #endif /* FD_SETSIZE*/
323 ldap_int_tblsize = tblsize;
328 do_ldap_select( LDAP *ld, struct timeval *timeout )
330 struct selectinfo *sip;
332 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
334 if ( ldap_int_tblsize == 0 )
337 sip = (struct selectinfo *)ld->ld_selectinfo;
338 sip->si_use_readfds = sip->si_readfds;
339 sip->si_use_writefds = sip->si_writefds;
341 return( select( ldap_int_tblsize,
342 &sip->si_use_readfds, &sip->si_use_writefds,