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>
34 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
37 * if host == NULL, connect using address
38 * "address" and "port" must be in network byte order
39 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
40 * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
41 * XXX async is not used yet!
45 int connected, use_hp;
46 struct sockaddr_in sin;
47 struct hostent *hp = NULL;
50 int status; /* for ioctl call */
51 #endif /* LDAP_REFERRALS */
54 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
55 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
57 connected = use_hp = 0;
59 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
60 if ( (hp = gethostbyname( host )) == NULL ) {
62 errno = WSAGetLastError();
64 errno = EHOSTUNREACH; /* not exactly right, but... */
72 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
73 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
79 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
80 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
83 #endif /* LDAP_REFERRALS */
85 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
86 sin.sin_family = AF_INET;
88 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
89 ( use_hp ? (char *) hp->h_addr_list[ i ] :
90 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
92 if ( connect( s, (struct sockaddr *)&sin,
93 sizeof( struct sockaddr_in )) >= 0 ) {
99 errno = WSAGetLastError();
102 #ifdef LDAP_REFERRALS
104 if ( errno == EINPROGRESS || errno == EAGAIN ) {
106 if ( errno == EINPROGRESS ) {
108 Debug( LDAP_DEBUG_TRACE,
109 "connect would block...\n", 0, 0, 0 );
113 #endif /* LDAP_REFERRALS */
117 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
118 perror( (char *)inet_ntoa( sin.sin_addr ));
132 #ifdef LDAP_REFERRALS
134 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
135 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
138 #endif /* LDAP_REFERRALS */
141 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
142 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
150 ldap_close_connection( Sockbuf *sb )
152 tcp_close( sb->sb_sd );
158 ldap_host_connected_to( Sockbuf *sb )
163 struct sockaddr_in sin;
165 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
167 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
172 * do a reverse lookup on the addr to get the official hostname.
173 * this is necessary for kerberos to work right, since the official
174 * hostname is used as the kerberos instance.
176 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
177 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
178 if ( hp->h_name != NULL ) {
179 return( strdup( hp->h_name ));
185 #endif /* HAVE_KERBEROS */
188 #ifdef LDAP_REFERRALS
193 fd_set si_use_readfds;
194 fd_set si_use_writefds;
199 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
201 struct selectinfo *sip;
203 sip = (struct selectinfo *)ld->ld_selectinfo;
205 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
206 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
212 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
214 struct selectinfo *sip;
216 sip = (struct selectinfo *)ld->ld_selectinfo;
218 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
219 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
225 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
227 struct selectinfo *sip;
229 sip = (struct selectinfo *)ld->ld_selectinfo;
231 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
232 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
237 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
239 struct selectinfo *sip;
241 sip = (struct selectinfo *)ld->ld_selectinfo;
243 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
248 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
250 struct selectinfo *sip;
252 sip = (struct selectinfo *)ld->ld_selectinfo;
254 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
259 ldap_new_select_info()
261 struct selectinfo *sip;
263 if (( sip = (struct selectinfo *)calloc( 1,
264 sizeof( struct selectinfo ))) != NULL ) {
265 FD_ZERO( &sip->si_readfds );
266 FD_ZERO( &sip->si_writefds );
269 return( (void *)sip );
274 ldap_free_select_info( void *sip )
281 do_ldap_select( LDAP *ld, struct timeval *timeout )
283 struct selectinfo *sip;
286 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
288 if ( tblsize == 0 ) {
289 #if defined( HAVE_SYSCONF )
290 tblsize = sysconf( _SC_OPEN_MAX );
291 #elif defined( HAVE_GETDTABLESIZE )
292 tblsize = getdtablesize();
293 #endif /* !USE_SYSCONF */
296 if( tblsize > FD_SETSIZE ) {
297 tblsize = FD_SETSIZE;
299 #endif /* FD_SETSIZE*/
302 sip = (struct selectinfo *)ld->ld_selectinfo;
303 sip->si_use_readfds = sip->si_readfds;
304 sip->si_use_writefds = sip->si_writefds;
306 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
309 #endif /* LDAP_REFERRALS */