]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Commit preliminary fix for ldap.conf base usage.
[openldap] / libraries / libldap / os-ip.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  os-ip.c -- platform-specific TCP & UDP related code
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #ifdef HAVE_IO_H
20 #include <io.h>
21 #endif /* HAVE_IO_H */
22
23 #if defined( HAVE_SYS_FILIO_H )
24 #include <sys/filio.h>
25 #elif defined( HAVE_SYS_IOCTL_H )
26 #include <sys/ioctl.h>
27 #endif
28
29 #include "ldap-int.h"
30
31 int
32 ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
33         int port, int async )
34 /*
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!
40  */
41 {
42         int                     rc, i, s = 0;
43         int                     connected, use_hp;
44         struct sockaddr_in      sin;
45         struct hostent          *hp = NULL;
46 #ifdef notyet
47 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
48         int                     status; /* for ioctl call */
49 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
50 #endif /* notyet */
51
52         Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
53             ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
54
55         connected = use_hp = 0;
56
57         if ( host != NULL ) {
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 ) {
63 #ifdef HAVE_WINSOCK
64                         errno = WSAGetLastError();
65 #else
66                         errno = EHOSTUNREACH;   /* not exactly right, but... */
67 #endif
68                         return( -1 );
69                 }
70                 use_hp = 1;
71             }
72         }
73
74         rc = -1;
75         for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
76                 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
77                         return( -1 );
78                 }
79 #ifdef notyet
80 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
81                 status = 1;
82                 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
83                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
84                             s, 0, 0 );
85                 }
86 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
87 #endif /* notyet */
88                 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
89                 sin.sin_family = AF_INET;
90                 sin.sin_port = port;
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) );
94
95                 if ( connect( s, (struct sockaddr *)&sin,
96                     sizeof( struct sockaddr_in )) >= 0 ) {
97                         connected = 1;
98                         rc = 0;
99                         break;
100                 } else {
101 #ifdef HAVE_WINSOCK
102                         errno = WSAGetLastError();
103 #endif
104 #ifdef notyet
105 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
106 #ifdef EAGAIN
107                         if ( errno == EINPROGRESS || errno == EAGAIN ) {
108 #else /* EAGAIN */
109                         if ( errno == EINPROGRESS ) {
110 #endif /* EAGAIN */
111                                 Debug( LDAP_DEBUG_TRACE,
112                                         "connect would block...\n", 0, 0, 0 );
113                                 rc = -2;
114                                 break;
115                         }
116 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
117 #endif /* notyet */
118
119 #ifdef LDAP_DEBUG               
120                         if ( ldap_debug & LDAP_DEBUG_TRACE ) {
121                                 perror( (char *)inet_ntoa( sin.sin_addr ));
122                         }
123 #endif
124                         tcp_close( s );
125                         if ( !use_hp ) {
126                                 break;
127                         }
128                 }
129         }
130
131         sb->sb_sd = s;
132
133         if ( connected ) {
134 #ifdef notyet
135 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
136                 status = 0;
137                 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
138                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
139                             s, 0, 0 );
140                 }
141 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
142 #endif /* notyet */
143
144                 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
145                     s, (char *) inet_ntoa( sin.sin_addr ), 0 );
146         }
147
148         return( rc );
149 }
150
151
152 void
153 ldap_close_connection( Sockbuf *sb )
154 {
155     tcp_close( sb->sb_sd );
156 }
157
158
159 #ifdef HAVE_KERBEROS
160 char *
161 ldap_host_connected_to( Sockbuf *sb )
162 {
163         struct hostent          *hp;
164         char                    *p;
165         int                     len;
166         struct sockaddr_in      sin;
167
168         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
169         len = sizeof( sin );
170         if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
171                 return( NULL );
172         }
173
174         /*
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.
178          */
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 ));
183                 }
184         }
185
186         return( NULL );
187 }
188 #endif /* HAVE_KERBEROS */
189
190
191 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
192 /* for UNIX */
193 struct selectinfo {
194         fd_set  si_readfds;
195         fd_set  si_writefds;
196         fd_set  si_use_readfds;
197         fd_set  si_use_writefds;
198 };
199
200
201 void
202 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
203 {
204         struct selectinfo       *sip;
205
206         sip = (struct selectinfo *)ld->ld_selectinfo;
207
208         if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
209                 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
210         }
211 }
212
213
214 void
215 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
216 {
217         struct selectinfo       *sip;
218
219         sip = (struct selectinfo *)ld->ld_selectinfo;
220
221         if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
222                 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
223         }
224 }
225
226
227 void
228 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
229 {
230         struct selectinfo       *sip;
231
232         sip = (struct selectinfo *)ld->ld_selectinfo;
233
234         FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
235         FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
236 }
237
238
239 int
240 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
241 {
242         struct selectinfo       *sip;
243
244         sip = (struct selectinfo *)ld->ld_selectinfo;
245
246         return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
247 }
248
249
250 int
251 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
252 {
253         struct selectinfo       *sip;
254
255         sip = (struct selectinfo *)ld->ld_selectinfo;
256
257         return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
258 }
259
260
261 void *
262 ldap_new_select_info( void )
263 {
264         struct selectinfo       *sip;
265
266         if (( sip = (struct selectinfo *)calloc( 1,
267             sizeof( struct selectinfo ))) != NULL ) {
268                 FD_ZERO( &sip->si_readfds );
269                 FD_ZERO( &sip->si_writefds );
270         }
271
272         return( (void *)sip );
273 }
274
275
276 void
277 ldap_free_select_info( void *sip )
278 {
279         free( sip );
280 }
281
282
283 int
284 do_ldap_select( LDAP *ld, struct timeval *timeout )
285 {
286         struct selectinfo       *sip;
287         static int              tblsize;
288
289         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
290
291         if ( tblsize == 0 ) {
292 #if defined( HAVE_SYSCONF )
293                 tblsize = sysconf( _SC_OPEN_MAX );
294 #elif defined( HAVE_GETDTABLESIZE )
295                 tblsize = getdtablesize();
296 #else
297                 tblsize = FD_SETSIZE;
298 #endif /* !USE_SYSCONF */
299
300 #ifdef FD_SETSIZE
301                 if( tblsize > FD_SETSIZE ) {
302                         tblsize = FD_SETSIZE;
303                 }
304 #endif  /* FD_SETSIZE*/
305         }
306
307         sip = (struct selectinfo *)ld->ld_selectinfo;
308         sip->si_use_readfds = sip->si_readfds;
309         sip->si_use_writefds = sip->si_writefds;
310         
311         return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
312             NULL, timeout ));
313 }
314 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */