2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * os-ip.c -- platform-specific TCP & UDP related code
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
21 #endif /* HAVE_IO_H */
23 #if defined( HAVE_SYS_FILIO_H )
24 #include <sys/filio.h>
25 #elif defined( HAVE_SYS_IOCTL_H )
26 #include <sys/ioctl.h>
32 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
35 * if host == NULL, connect using address
36 * "address" and "port" must be in network byte order
37 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
38 * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
39 * XXX async is not used yet!
43 int connected, use_hp;
44 struct sockaddr_in sin;
45 struct hostent *hp = NULL;
47 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
48 int status; /* for ioctl call */
49 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
52 /* buffers for ldap_int_gethostbyname_a */
53 struct hostent he_buf;
56 #define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
58 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
59 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
61 connected = use_hp = 0;
64 address = inet_addr( host );
65 /* This was just a test for -1 until OSF1 let inet_addr return
66 unsigned int, which is narrower than 'unsigned long address' */
67 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
68 if ( ( ldap_int_gethostbyname_a( host, &he_buf, &ha_buf,
69 &hp, &local_h_errno) < 0) || (hp==NULL))
72 errno = WSAGetLastError();
74 errno = EHOSTUNREACH; /* not exactly right, but... */
83 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
84 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
88 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
90 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
91 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
94 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
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();
113 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
115 if ( errno == EINPROGRESS || errno == EAGAIN ) {
117 if ( errno == EINPROGRESS ) {
119 Debug( LDAP_DEBUG_TRACE,
120 "connect would block...\n", 0, 0, 0 );
124 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
128 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
129 perror( (char *)inet_ntoa( sin.sin_addr ));
143 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
145 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
146 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
149 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
152 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
153 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
165 ldap_close_connection( Sockbuf *sb )
167 tcp_close( sb->sb_sd );
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) free(ha_buf); return (x);
186 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
188 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
193 * do a reverse lookup on the addr to get the official hostname.
194 * this is necessary for kerberos to work right, since the official
195 * hostname is used as the kerberos instance.
197 if ((ldap_int_gethostbyaddr_a( (char *) &sin.sin_addr,
198 sizeof( sin.sin_addr ),
199 AF_INET, &he_buf, &ha_buf,
200 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
202 if ( hp->h_name != NULL ) {
203 DO_RETURN( ldap_strdup( hp->h_name ));
211 #endif /* HAVE_KERBEROS */
214 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
219 fd_set si_use_readfds;
220 fd_set si_use_writefds;
225 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
227 struct selectinfo *sip;
229 sip = (struct selectinfo *)ld->ld_selectinfo;
231 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
232 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
238 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
240 struct selectinfo *sip;
242 sip = (struct selectinfo *)ld->ld_selectinfo;
244 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
245 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
251 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
253 struct selectinfo *sip;
255 sip = (struct selectinfo *)ld->ld_selectinfo;
257 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
258 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
263 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
265 struct selectinfo *sip;
267 sip = (struct selectinfo *)ld->ld_selectinfo;
269 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
274 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
276 struct selectinfo *sip;
278 sip = (struct selectinfo *)ld->ld_selectinfo;
280 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
285 ldap_new_select_info( void )
287 struct selectinfo *sip;
289 if (( sip = (struct selectinfo *)calloc( 1,
290 sizeof( struct selectinfo ))) != NULL ) {
291 FD_ZERO( &sip->si_readfds );
292 FD_ZERO( &sip->si_writefds );
295 return( (void *)sip );
300 ldap_free_select_info( void *sip )
307 do_ldap_select( LDAP *ld, struct timeval *timeout )
309 struct selectinfo *sip;
312 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
314 if ( tblsize == 0 ) {
315 #if defined( HAVE_SYSCONF )
316 tblsize = sysconf( _SC_OPEN_MAX );
317 #elif defined( HAVE_GETDTABLESIZE )
318 tblsize = getdtablesize();
320 tblsize = FD_SETSIZE;
321 #endif /* !USE_SYSCONF */
324 if( tblsize > FD_SETSIZE ) {
325 tblsize = FD_SETSIZE;
327 #endif /* FD_SETSIZE*/
330 sip = (struct selectinfo *)ld->ld_selectinfo;
331 sip->si_use_readfds = sip->si_readfds;
332 sip->si_use_writefds = sip->si_writefds;
334 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
337 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */