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 ) {
61 errno = EHOSTUNREACH; /* not exactly right, but... */
68 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
69 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
75 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
76 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
79 #endif /* LDAP_REFERRALS */
81 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
82 sin.sin_family = AF_INET;
84 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
85 ( use_hp ? (char *) hp->h_addr_list[ i ] :
86 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
88 if ( connect( s, (struct sockaddr *)&sin,
89 sizeof( struct sockaddr_in )) >= 0 ) {
97 if ( errno == EINPROGRESS || errno == EAGAIN ) {
99 if ( errno == EINPROGRESS ) {
101 Debug( LDAP_DEBUG_TRACE,
102 "connect would block...\n", 0, 0, 0 );
106 #endif /* LDAP_REFERRALS */
110 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
111 perror( (char *)inet_ntoa( sin.sin_addr ));
125 #ifdef LDAP_REFERRALS
127 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
128 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
131 #endif /* LDAP_REFERRALS */
134 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
135 s, inet_ntoa( sin.sin_addr ), 0 );
143 ldap_close_connection( Sockbuf *sb )
145 tcp_close( sb->sb_sd );
151 ldap_host_connected_to( Sockbuf *sb )
156 struct sockaddr_in sin;
158 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
160 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
165 * do a reverse lookup on the addr to get the official hostname.
166 * this is necessary for kerberos to work right, since the official
167 * hostname is used as the kerberos instance.
169 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
170 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
171 if ( hp->h_name != NULL ) {
172 return( strdup( hp->h_name ));
178 #endif /* HAVE_KERBEROS */
181 #ifdef LDAP_REFERRALS
186 fd_set si_use_readfds;
187 fd_set si_use_writefds;
192 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
194 struct selectinfo *sip;
196 sip = (struct selectinfo *)ld->ld_selectinfo;
198 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
199 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
205 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
207 struct selectinfo *sip;
209 sip = (struct selectinfo *)ld->ld_selectinfo;
211 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
212 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
218 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
220 struct selectinfo *sip;
222 sip = (struct selectinfo *)ld->ld_selectinfo;
224 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
225 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
230 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
232 struct selectinfo *sip;
234 sip = (struct selectinfo *)ld->ld_selectinfo;
236 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
241 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
243 struct selectinfo *sip;
245 sip = (struct selectinfo *)ld->ld_selectinfo;
247 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
252 ldap_new_select_info()
254 struct selectinfo *sip;
256 if (( sip = (struct selectinfo *)calloc( 1,
257 sizeof( struct selectinfo ))) != NULL ) {
258 FD_ZERO( &sip->si_readfds );
259 FD_ZERO( &sip->si_writefds );
262 return( (void *)sip );
267 ldap_free_select_info( void *sip )
274 do_ldap_select( LDAP *ld, struct timeval *timeout )
276 struct selectinfo *sip;
279 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
281 if ( tblsize == 0 ) {
282 #if defined( HAVE_SYSCONF )
283 tblsize = sysconf( _SC_OPEN_MAX );
284 #elif defined( HAVE_GETDTABLESIZE )
285 tblsize = getdtablesize();
286 #endif /* !USE_SYSCONF */
289 if( tblsize > FD_SETSIZE ) {
290 tblsize = FD_SETSIZE;
292 #endif /* FD_SETSIZE*/
295 sip = (struct selectinfo *)ld->ld_selectinfo;
296 sip->si_use_readfds = sip->si_readfds;
297 sip->si_use_writefds = sip->si_writefds;
299 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
302 #endif /* LDAP_REFERRALS */