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_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;
48 int status; /* for ioctl call */
49 #endif /* LDAP_REFERRALS */
52 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
53 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
55 connected = use_hp = 0;
57 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
58 if ( (hp = gethostbyname( host )) == NULL ) {
60 errno = WSAGetLastError();
62 errno = EHOSTUNREACH; /* not exactly right, but... */
70 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
71 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
77 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
78 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
81 #endif /* LDAP_REFERRALS */
83 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
84 sin.sin_family = AF_INET;
86 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
87 ( use_hp ? (char *) hp->h_addr_list[ i ] :
88 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
90 if ( connect( s, (struct sockaddr *)&sin,
91 sizeof( struct sockaddr_in )) >= 0 ) {
97 errno = WSAGetLastError();
100 #ifdef LDAP_REFERRALS
102 if ( errno == EINPROGRESS || errno == EAGAIN ) {
104 if ( errno == EINPROGRESS ) {
106 Debug( LDAP_DEBUG_TRACE,
107 "connect would block...\n", 0, 0, 0 );
111 #endif /* LDAP_REFERRALS */
115 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
116 perror( (char *)inet_ntoa( sin.sin_addr ));
130 #ifdef LDAP_REFERRALS
132 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
133 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
136 #endif /* LDAP_REFERRALS */
139 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
140 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
148 ldap_close_connection( Sockbuf *sb )
150 tcp_close( sb->sb_sd );
156 ldap_host_connected_to( Sockbuf *sb )
161 struct sockaddr_in sin;
163 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
165 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
170 * do a reverse lookup on the addr to get the official hostname.
171 * this is necessary for kerberos to work right, since the official
172 * hostname is used as the kerberos instance.
174 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
175 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
176 if ( hp->h_name != NULL ) {
177 return( strdup( hp->h_name ));
183 #endif /* HAVE_KERBEROS */
186 #ifdef LDAP_REFERRALS
191 fd_set si_use_readfds;
192 fd_set si_use_writefds;
197 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
199 struct selectinfo *sip;
201 sip = (struct selectinfo *)ld->ld_selectinfo;
203 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
204 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
210 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
212 struct selectinfo *sip;
214 sip = (struct selectinfo *)ld->ld_selectinfo;
216 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
217 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
223 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
225 struct selectinfo *sip;
227 sip = (struct selectinfo *)ld->ld_selectinfo;
229 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
230 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
235 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
237 struct selectinfo *sip;
239 sip = (struct selectinfo *)ld->ld_selectinfo;
241 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
246 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
248 struct selectinfo *sip;
250 sip = (struct selectinfo *)ld->ld_selectinfo;
252 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
257 ldap_new_select_info()
259 struct selectinfo *sip;
261 if (( sip = (struct selectinfo *)calloc( 1,
262 sizeof( struct selectinfo ))) != NULL ) {
263 FD_ZERO( &sip->si_readfds );
264 FD_ZERO( &sip->si_writefds );
267 return( (void *)sip );
272 ldap_free_select_info( void *sip )
279 do_ldap_select( LDAP *ld, struct timeval *timeout )
281 struct selectinfo *sip;
284 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
286 if ( tblsize == 0 ) {
287 #if defined( HAVE_SYSCONF )
288 tblsize = sysconf( _SC_OPEN_MAX );
289 #elif defined( HAVE_GETDTABLESIZE )
290 tblsize = getdtablesize();
291 #endif /* !USE_SYSCONF */
294 if( tblsize > FD_SETSIZE ) {
295 tblsize = FD_SETSIZE;
297 #endif /* FD_SETSIZE*/
300 sip = (struct selectinfo *)ld->ld_selectinfo;
301 sip->si_use_readfds = sip->si_readfds;
302 sip->si_use_writefds = sip->si_writefds;
304 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
307 #endif /* LDAP_REFERRALS */