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>
16 #include <ac/unistd.h>
20 #endif /* HAVE_IO_H */
22 #if defined( HAVE_SYS_FILIO_H )
23 #include <sys/filio.h>
24 #elif defined( HAVE_SYS_IOCTL_H )
25 #include <sys/ioctl.h>
33 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
36 * if host == NULL, connect using address
37 * "address" and "port" must be in network byte order
38 * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
39 * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
40 * XXX async is not used yet!
44 int connected, use_hp;
45 struct sockaddr_in sin;
46 struct hostent *hp = NULL;
49 int status; /* for ioctl call */
50 #endif /* LDAP_REFERRALS */
53 Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
54 ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
56 connected = use_hp = 0;
58 if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
59 if ( (hp = gethostbyname( host )) == NULL ) {
60 errno = EHOSTUNREACH; /* not exactly right, but... */
67 for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
68 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
74 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
75 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
78 #endif /* LDAP_REFERRALS */
80 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
81 sin.sin_family = AF_INET;
83 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
84 ( use_hp ? (char *) hp->h_addr_list[ i ] :
85 (char *) &address ), sizeof( sin.sin_addr.s_addr) );
87 if ( connect( s, (struct sockaddr *)&sin,
88 sizeof( struct sockaddr_in )) >= 0 ) {
96 if ( errno == EINPROGRESS || errno == EAGAIN ) {
98 if ( errno == EINPROGRESS ) {
100 Debug( LDAP_DEBUG_TRACE,
101 "connect would block...\n", 0, 0, 0 );
105 #endif /* LDAP_REFERRALS */
109 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
110 perror( (char *)inet_ntoa( sin.sin_addr ));
124 #ifdef LDAP_REFERRALS
126 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
127 Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
130 #endif /* LDAP_REFERRALS */
133 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
134 s, inet_ntoa( sin.sin_addr ), 0 );
142 ldap_close_connection( Sockbuf *sb )
144 tcp_close( sb->sb_sd );
150 ldap_host_connected_to( Sockbuf *sb )
155 struct sockaddr_in sin;
157 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
159 if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
164 * do a reverse lookup on the addr to get the official hostname.
165 * this is necessary for kerberos to work right, since the official
166 * hostname is used as the kerberos instance.
168 if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
169 sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
170 if ( hp->h_name != NULL ) {
171 return( strdup( hp->h_name ));
177 #endif /* KERBEROS */
180 #ifdef LDAP_REFERRALS
185 fd_set si_use_readfds;
186 fd_set si_use_writefds;
191 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
193 struct selectinfo *sip;
195 sip = (struct selectinfo *)ld->ld_selectinfo;
197 if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
198 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
204 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
206 struct selectinfo *sip;
208 sip = (struct selectinfo *)ld->ld_selectinfo;
210 if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
211 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
217 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
219 struct selectinfo *sip;
221 sip = (struct selectinfo *)ld->ld_selectinfo;
223 FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
224 FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
229 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
231 struct selectinfo *sip;
233 sip = (struct selectinfo *)ld->ld_selectinfo;
235 return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
240 ldap_is_read_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_readfds ));
251 ldap_new_select_info()
253 struct selectinfo *sip;
255 if (( sip = (struct selectinfo *)calloc( 1,
256 sizeof( struct selectinfo ))) != NULL ) {
257 FD_ZERO( &sip->si_readfds );
258 FD_ZERO( &sip->si_writefds );
261 return( (void *)sip );
266 ldap_free_select_info( void *sip )
273 do_ldap_select( LDAP *ld, struct timeval *timeout )
275 struct selectinfo *sip;
278 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
280 if ( tblsize == 0 ) {
281 #if defined( HAVE_SYSCONF )
282 tblsize = sysconf( _SC_OPEN_MAX );
283 #elif defined( HAVE_GETDTABLESIZE )
284 tblsize = getdtablesize();
285 #endif /* !USE_SYSCONF */
288 if( tblsize > FD_SETSIZE ) {
289 tblsize = FD_SETSIZE;
291 #endif /* FD_SETSIZE*/
294 sip = (struct selectinfo *)ld->ld_selectinfo;
295 sip->si_use_readfds = sip->si_readfds;
296 sip->si_use_writefds = sip->si_writefds;
298 return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
301 #endif /* LDAP_REFERRALS */