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";
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
27 #include <sys/select.h>
30 #include "ucx_select.h"
39 #endif /* USE_SYSCONF */
42 #include <sys/filio.h>
43 #else /* NEED_FILIO */
44 #include <sys/ioctl.h>
45 #endif /* NEED_FILIO */
47 #endif /* LDAP_REFERRALS */
50 #define tcp_close( s ) tcpclose( s )
54 #define tcp_close( s ) close( s )
57 #define tcp_close( s ) netclose( s ); netshut()
60 #define tcp_close( s ) closesocket( s ); WSACleanup();
63 #define tcp_close( s ) close( s )
69 connect_to_host( Sockbuf *sb, char *host, unsigned long address,
72 * if host == NULL, connect using address
73 * "address" and "port" must be in network byte order
74 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
75 * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
76 * XXX async is not used yet!
79 int rc, i, s, connected, use_hp;
80 struct sockaddr_in sin;
84 int status; /* for ioctl call */
85 #endif /* LDAP_REFERRALS */
88 Debug( LDAP_DEBUG_TRACE, "connect_to_host: %s:%d\n",
89 ( host == NULL ) ? "(by address)" : host, ntohs( port ), 0 );
91 connected = use_hp = 0;
93 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
94 if ( (hp = gethostbyname( host )) == NULL ) {
95 errno = EHOSTUNREACH; /* not exactly right, but... */
102 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
103 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
107 #ifdef LDAP_REFERRALS
109 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
110 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
113 #endif /* LDAP_REFERRALS */
115 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
116 sin.sin_family = AF_INET;
118 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
119 ( use_hp ? (char *) hp->h_addr_list[ i ] :
120 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
122 if ( connect( s, (struct sockaddr *)&sin,
123 sizeof( struct sockaddr_in )) >= 0 ) {
129 #ifdef LDAP_REFERRALS
131 if ( errno == EINPROGRESS || errno == EAGAIN ) {
133 if ( errno == EINPROGRESS ) {
135 Debug( LDAP_DEBUG_TRACE,
136 "connect would block...\n", 0, 0, 0 );
140 #endif /* LDAP_REFERRALS */
144 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
145 perror( (char *)inet_ntoa( sin.sin_addr ));
159 #ifdef LDAP_REFERRALS
161 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
162 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
165 #endif /* LDAP_REFERRALS */
168 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
169 s, inet_ntoa( sin.sin_addr ), 0 );
177 close_connection( Sockbuf *sb )
179 tcp_close( sb->sb_sd );
185 host_connected_to( Sockbuf *sb )
190 struct sockaddr_in sin;
192 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
194 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
199 * do a reverse lookup on the addr to get the official hostname.
200 * this is necessary for kerberos to work right, since the official
201 * hostname is used as the kerberos instance.
203 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
204 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
205 if ( hp->h_name != NULL ) {
206 return( strdup( hp->h_name ));
212 #endif /* KERBEROS */
215 #ifdef LDAP_REFERRALS
220 fd_set si_use_readfds;
221 fd_set si_use_writefds;
226 mark_select_write( LDAP *ld, Sockbuf *sb )
228 struct selectinfo *sip;
230 sip = (struct selectinfo *)ld->ld_selectinfo;
232 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
233 FD_SET( sb->sb_sd, &sip->si_writefds );
239 mark_select_read( LDAP *ld, Sockbuf *sb )
241 struct selectinfo *sip;
243 sip = (struct selectinfo *)ld->ld_selectinfo;
245 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
246 FD_SET( sb->sb_sd, &sip->si_readfds );
252 mark_select_clear( LDAP *ld, Sockbuf *sb )
254 struct selectinfo *sip;
256 sip = (struct selectinfo *)ld->ld_selectinfo;
258 FD_CLR( sb->sb_sd, &sip->si_writefds );
259 FD_CLR( sb->sb_sd, &sip->si_readfds );
264 is_write_ready( LDAP *ld, Sockbuf *sb )
266 struct selectinfo *sip;
268 sip = (struct selectinfo *)ld->ld_selectinfo;
270 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
275 is_read_ready( LDAP *ld, Sockbuf *sb )
277 struct selectinfo *sip;
279 sip = (struct selectinfo *)ld->ld_selectinfo;
281 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
288 struct selectinfo *sip;
290 if (( sip = (struct selectinfo *)calloc( 1,
291 sizeof( struct selectinfo ))) != NULL ) {
292 FD_ZERO( &sip->si_readfds );
293 FD_ZERO( &sip->si_writefds );
296 return( (void *)sip );
301 free_select_info( void *sip )
308 do_ldap_select( LDAP *ld, struct timeval *timeout )
310 struct selectinfo *sip;
313 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
315 if ( tblsize == 0 ) {
317 tblsize = sysconf( _SC_OPEN_MAX );
318 #else /* USE_SYSCONF */
319 tblsize = getdtablesize();
320 #endif /* USE_SYSCONF */
323 sip = (struct selectinfo *)ld->ld_selectinfo;
324 sip->si_use_readfds = sip->si_readfds;
325 sip->si_use_writefds = sip->si_writefds;
327 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
330 #endif /* LDAP_REFERRALS */