2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * os-ip.c -- platform-specific TCP & UDP related code
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
30 #include <sys/select.h>
33 #include "ucx_select.h"
43 #endif /* USE_SYSCONF */
46 #include <sys/filio.h>
47 #else /* NEED_FILIO */
48 #include <sys/ioctl.h>
49 #endif /* NEED_FILIO */
51 #endif /* LDAP_REFERRALS */
54 #define tcp_close( s ) tcpclose( s )
58 #define tcp_close( s ) close( s )
61 #define tcp_close( s ) netclose( s ); netshut()
64 #define tcp_close( s ) closesocket( s ); WSACleanup();
67 #define tcp_close( s ) close( s )
74 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
77 * if host == NULL, connect using address
78 * "address" and "port" must be in network byte order
79 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
80 * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
81 * XXX async is not used yet!
85 int connected, use_hp;
86 struct sockaddr_in sin;
87 struct hostent *hp = NULL;
90 int status; /* for ioctl call */
91 #endif /* LDAP_REFERRALS */
94 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
95 ( host == NULL ) ? "(by address)" : host, ntohs( port ), 0 );
97 connected = use_hp = 0;
99 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
100 if ( (hp = gethostbyname( host )) == NULL ) {
101 errno = EHOSTUNREACH; /* not exactly right, but... */
108 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
109 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
113 #ifdef LDAP_REFERRALS
115 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
116 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
119 #endif /* LDAP_REFERRALS */
121 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
122 sin.sin_family = AF_INET;
124 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
125 ( use_hp ? (char *) hp->h_addr_list[ i ] :
126 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
128 if ( connect( s, (struct sockaddr *)&sin,
129 sizeof( struct sockaddr_in )) >= 0 ) {
135 #ifdef LDAP_REFERRALS
137 if ( errno == EINPROGRESS || errno == EAGAIN ) {
139 if ( errno == EINPROGRESS ) {
141 Debug( LDAP_DEBUG_TRACE,
142 "connect would block...\n", 0, 0, 0 );
146 #endif /* LDAP_REFERRALS */
150 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
151 perror( (char *)inet_ntoa( sin.sin_addr ));
165 #ifdef LDAP_REFERRALS
167 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
168 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
171 #endif /* LDAP_REFERRALS */
174 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
175 s, inet_ntoa( sin.sin_addr ), 0 );
183 ldap_close_connection( Sockbuf *sb )
185 tcp_close( sb->sb_sd );
191 ldap_host_connected_to( Sockbuf *sb )
196 struct sockaddr_in sin;
198 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
200 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
205 * do a reverse lookup on the addr to get the official hostname.
206 * this is necessary for kerberos to work right, since the official
207 * hostname is used as the kerberos instance.
209 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
210 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
211 if ( hp->h_name != NULL ) {
212 return( strdup( hp->h_name ));
218 #endif /* KERBEROS */
221 #ifdef LDAP_REFERRALS
226 fd_set si_use_readfds;
227 fd_set si_use_writefds;
232 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
234 struct selectinfo *sip;
236 sip = (struct selectinfo *)ld->ld_selectinfo;
238 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
239 FD_SET( sb->sb_sd, &sip->si_writefds );
245 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
247 struct selectinfo *sip;
249 sip = (struct selectinfo *)ld->ld_selectinfo;
251 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
252 FD_SET( sb->sb_sd, &sip->si_readfds );
258 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
260 struct selectinfo *sip;
262 sip = (struct selectinfo *)ld->ld_selectinfo;
264 FD_CLR( sb->sb_sd, &sip->si_writefds );
265 FD_CLR( sb->sb_sd, &sip->si_readfds );
270 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
272 struct selectinfo *sip;
274 sip = (struct selectinfo *)ld->ld_selectinfo;
276 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
281 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
283 struct selectinfo *sip;
285 sip = (struct selectinfo *)ld->ld_selectinfo;
287 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
292 ldap_new_select_info()
294 struct selectinfo *sip;
296 if (( sip = (struct selectinfo *)calloc( 1,
297 sizeof( struct selectinfo ))) != NULL ) {
298 FD_ZERO( &sip->si_readfds );
299 FD_ZERO( &sip->si_writefds );
302 return( (void *)sip );
307 ldap_free_select_info( void *sip )
314 do_ldap_select( LDAP *ld, struct timeval *timeout )
316 struct selectinfo *sip;
319 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
321 if ( tblsize == 0 ) {
323 tblsize = sysconf( _SC_OPEN_MAX );
324 #else /* !USE_SYSCONF */
325 tblsize = getdtablesize();
326 #endif /* !USE_SYSCONF */
329 if( tblsize > FD_SETSIZE ) {
330 tblsize = FD_SETSIZE;
332 #endif /* FD_SETSIZE*/
335 sip = (struct selectinfo *)ld->ld_selectinfo;
336 sip->si_use_readfds = sip->si_readfds;
337 sip->si_use_writefds = sip->si_writefds;
339 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
342 #endif /* LDAP_REFERRALS */