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 )
70 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
73 * if host == NULL, connect using address
74 * "address" and "port" must be in network byte order
75 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
76 * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
77 * XXX async is not used yet!
80 int rc, i, s, connected, use_hp;
81 struct sockaddr_in sin;
85 int status; /* for ioctl call */
86 #endif /* LDAP_REFERRALS */
89 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
90 ( host == NULL ) ? "(by address)" : host, ntohs( port ), 0 );
92 connected = use_hp = 0;
94 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
95 if ( (hp = gethostbyname( host )) == NULL ) {
96 errno = EHOSTUNREACH; /* not exactly right, but... */
103 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
104 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
108 #ifdef LDAP_REFERRALS
110 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
111 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
114 #endif /* LDAP_REFERRALS */
116 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
117 sin.sin_family = AF_INET;
119 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
120 ( use_hp ? (char *) hp->h_addr_list[ i ] :
121 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
123 if ( connect( s, (struct sockaddr *)&sin,
124 sizeof( struct sockaddr_in )) >= 0 ) {
130 #ifdef LDAP_REFERRALS
132 if ( errno == EINPROGRESS || errno == EAGAIN ) {
134 if ( errno == EINPROGRESS ) {
136 Debug( LDAP_DEBUG_TRACE,
137 "connect would block...\n", 0, 0, 0 );
141 #endif /* LDAP_REFERRALS */
145 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
146 perror( (char *)inet_ntoa( sin.sin_addr ));
160 #ifdef LDAP_REFERRALS
162 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
163 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
166 #endif /* LDAP_REFERRALS */
169 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
170 s, inet_ntoa( sin.sin_addr ), 0 );
178 ldap_close_connection( Sockbuf *sb )
180 tcp_close( sb->sb_sd );
186 ldap_host_connected_to( Sockbuf *sb )
191 struct sockaddr_in sin;
193 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
195 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
200 * do a reverse lookup on the addr to get the official hostname.
201 * this is necessary for kerberos to work right, since the official
202 * hostname is used as the kerberos instance.
204 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
205 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
206 if ( hp->h_name != NULL ) {
207 return( strdup( hp->h_name ));
213 #endif /* KERBEROS */
216 #ifdef LDAP_REFERRALS
221 fd_set si_use_readfds;
222 fd_set si_use_writefds;
227 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
229 struct selectinfo *sip;
231 sip = (struct selectinfo *)ld->ld_selectinfo;
233 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
234 FD_SET( sb->sb_sd, &sip->si_writefds );
240 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
242 struct selectinfo *sip;
244 sip = (struct selectinfo *)ld->ld_selectinfo;
246 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
247 FD_SET( sb->sb_sd, &sip->si_readfds );
253 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
255 struct selectinfo *sip;
257 sip = (struct selectinfo *)ld->ld_selectinfo;
259 FD_CLR( sb->sb_sd, &sip->si_writefds );
260 FD_CLR( sb->sb_sd, &sip->si_readfds );
265 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
267 struct selectinfo *sip;
269 sip = (struct selectinfo *)ld->ld_selectinfo;
271 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
276 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
278 struct selectinfo *sip;
280 sip = (struct selectinfo *)ld->ld_selectinfo;
282 return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
287 ldap_new_select_info()
289 struct selectinfo *sip;
291 if (( sip = (struct selectinfo *)calloc( 1,
292 sizeof( struct selectinfo ))) != NULL ) {
293 FD_ZERO( &sip->si_readfds );
294 FD_ZERO( &sip->si_writefds );
297 return( (void *)sip );
302 ldap_free_select_info( void *sip )
309 do_ldap_select( LDAP *ld, struct timeval *timeout )
311 struct selectinfo *sip;
314 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
316 if ( tblsize == 0 ) {
319 * It is invalid to use a set size in excess of the type
320 * scope, as defined for the fd_set in sys/types.h. This
321 * is true for any OS.
323 tblsize = FD_SETSIZE;
324 #else /* !FD_SETSIZE*/
326 tblsize = sysconf( _SC_OPEN_MAX );
327 #else /* USE_SYSCONF */
328 tblsize = getdtablesize();
329 #endif /* USE_SYSCONF */
330 #endif /* !FD_SETSIZE*/
333 sip = (struct selectinfo *)ld->ld_selectinfo;
334 sip->si_use_readfds = sip->si_readfds;
335 sip->si_use_writefds = sip->si_writefds;
337 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
340 #endif /* LDAP_REFERRALS */