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 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;
58 address = inet_addr( host );
59 /* This was just a test for -1 until OSF1 let inet_addr return
60 unsigned int, which is narrower than 'unsigned long address' */
61 if ( address == 0xffffffff || address == (unsigned long) -1 ) {
62 if ( (hp = gethostbyname( host )) == NULL ) {
64 errno = WSAGetLastError();
66 errno = EHOSTUNREACH; /* not exactly right, but... */
75 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
76 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
80 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
82 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
83 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
86 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
88 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
89 sin.sin_family = AF_INET;
91 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
92 ( use_hp ? (char *) hp->h_addr_list[ i ] :
93 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
95 if ( connect( s, (struct sockaddr *)&sin,
96 sizeof( struct sockaddr_in )) >= 0 ) {
102 errno = WSAGetLastError();
105 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
107 if ( errno == EINPROGRESS || errno == EAGAIN ) {
109 if ( errno == EINPROGRESS ) {
111 Debug( LDAP_DEBUG_TRACE,
112 "connect would block...\n", 0, 0, 0 );
116 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
120 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
121 perror( (char *)inet_ntoa( sin.sin_addr ));
135 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
137 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
138 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
141 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
144 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
145 s, (char *) inet_ntoa( sin.sin_addr ), 0 );
153 ldap_close_connection( Sockbuf *sb )
155 tcp_close( sb->sb_sd );
161 ldap_host_connected_to( Sockbuf *sb )
166 struct sockaddr_in sin;
168 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
170 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
175 * do a reverse lookup on the addr to get the official hostname.
176 * this is necessary for kerberos to work right, since the official
177 * hostname is used as the kerberos instance.
179 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
180 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
181 if ( hp->h_name != NULL ) {
182 return( ldap_strdup( hp->h_name ));
188 #endif /* HAVE_KERBEROS */
191 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
196 fd_set si_use_readfds;
197 fd_set si_use_writefds;
202 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
204 struct selectinfo *sip;
206 sip = (struct selectinfo *)ld->ld_selectinfo;
208 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
209 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
215 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
217 struct selectinfo *sip;
219 sip = (struct selectinfo *)ld->ld_selectinfo;
221 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
222 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
228 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
230 struct selectinfo *sip;
232 sip = (struct selectinfo *)ld->ld_selectinfo;
234 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
235 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
240 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
242 struct selectinfo *sip;
244 sip = (struct selectinfo *)ld->ld_selectinfo;
246 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
251 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
253 struct selectinfo *sip;
255 sip = (struct selectinfo *)ld->ld_selectinfo;
257 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
262 ldap_new_select_info( void )
264 struct selectinfo *sip;
266 if (( sip = (struct selectinfo *)calloc( 1,
267 sizeof( struct selectinfo ))) != NULL ) {
268 FD_ZERO( &sip->si_readfds );
269 FD_ZERO( &sip->si_writefds );
272 return( (void *)sip );
277 ldap_free_select_info( void *sip )
284 do_ldap_select( LDAP *ld, struct timeval *timeout )
286 struct selectinfo *sip;
289 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
291 if ( tblsize == 0 ) {
292 #if defined( HAVE_SYSCONF )
293 tblsize = sysconf( _SC_OPEN_MAX );
294 #elif defined( HAVE_GETDTABLESIZE )
295 tblsize = getdtablesize();
297 tblsize = FD_SETSIZE;
298 #endif /* !USE_SYSCONF */
301 if( tblsize > FD_SETSIZE ) {
302 tblsize = FD_SETSIZE;
304 #endif /* FD_SETSIZE*/
307 sip = (struct selectinfo *)ld->ld_selectinfo;
308 sip->si_use_readfds = sip->si_readfds;
309 sip->si_use_writefds = sip->si_writefds;
311 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
314 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */