]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
4d6e6c8db7dd255ae0bc8c4146c7f108dcc78c37
[openldap] / libraries / libldap / os-ip.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1995 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  os-ip.c -- platform-specific TCP & UDP related code
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/stdlib.h>
17
18 #include <ac/errno.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22 #include <ac/unistd.h>
23
24 #ifdef HAVE_IO_H
25 #include <io.h>
26 #endif /* HAVE_IO_H */
27
28 #if defined( HAVE_SYS_FILIO_H )
29 #include <sys/filio.h>
30 #elif defined( HAVE_SYS_IOCTL_H )
31 #include <sys/ioctl.h>
32 #endif
33
34 #include "ldap-int.h"
35
36 int ldap_int_tblsize = 0;
37
38
39 int
40 ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
41         int port, int async )
42 /*
43  * if host == NULL, connect using address
44  * "address" and "port" must be in network byte order
45  * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
46  * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
47  * XXX async is not used yet!
48  */
49 {
50         int                     rc, i;
51         ber_socket_t s = AC_SOCKET_INVALID;
52         int                     connected, use_hp;
53         struct sockaddr_in      sin;
54         struct hostent          *hp = NULL;
55 #ifdef notyet
56         ioctl_t                 status; /* for ioctl call */
57 #endif /* notyet */
58    
59         /* buffers for ldap_pvt_gethostbyname_a */
60         struct hostent          he_buf;
61         int                     local_h_errno;
62         char                    *ha_buf=NULL;
63 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
64    
65         Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
66             ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
67
68         connected = use_hp = 0;
69
70         if ( host != NULL ) {
71             address = inet_addr( host );
72             /* This was just a test for -1 until OSF1 let inet_addr return
73                unsigned int, which is narrower than 'unsigned long address' */
74             if ( address == 0xffffffff || address == (unsigned long) -1 ) {
75                 if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
76                         &hp, &local_h_errno) < 0) || (hp==NULL))
77                 {
78 #ifdef HAVE_WINSOCK
79                         errno = WSAGetLastError();
80 #else
81                         errno = EHOSTUNREACH;   /* not exactly right, but... */
82 #endif
83                         DO_RETURN( -1 );
84                 }
85                 use_hp = 1;
86             }
87         }
88
89         rc = -1;
90         for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
91                 if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
92                         DO_RETURN( -1 );
93                 }
94 #ifdef TCP_NODELAY
95                 {
96                         int tmp = 1;
97                         if( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
98                                 &tmp, sizeof(tmp) ) == -1 )
99                         {
100                                 Debug( LDAP_DEBUG_ANY,
101                                         "setsockopt(TCP_NODELAY failed on %d\n",
102                                         s, 0, 0 );
103                         }
104                 }
105 #endif
106 #ifdef notyet
107                 status = 1;
108                 if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
109                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
110                             s, 0, 0 );
111                 }
112 #endif /* notyet */
113                 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
114                 sin.sin_family = AF_INET;
115                 sin.sin_port = port;
116                 SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
117                     ( use_hp ? (char *) hp->h_addr_list[ i ] :
118                     (char *) &address ), sizeof( sin.sin_addr.s_addr) );
119
120                 if ( connect( s, (struct sockaddr *)&sin,
121                     sizeof( struct sockaddr_in )) >= 0 ) {
122                         connected = 1;
123                         rc = 0;
124                         break;
125                 } else {
126 #ifdef HAVE_WINSOCK
127                         errno = WSAGetLastError();
128 #endif
129 #ifdef notyet
130 #ifdef EAGAIN
131                         if ( errno == EINPROGRESS || errno == EAGAIN ) {
132 #else /* EAGAIN */
133                         if ( errno == EINPROGRESS ) {
134 #endif /* EAGAIN */
135                                 Debug( LDAP_DEBUG_TRACE,
136                                         "connect would block...\n", 0, 0, 0 );
137                                 rc = -2;
138                                 break;
139                         }
140 #endif /* notyet */
141
142 #ifdef LDAP_DEBUG               
143                         if ( ldap_debug & LDAP_DEBUG_TRACE ) {
144                                 perror( (char *)inet_ntoa( sin.sin_addr ));
145                         }
146 #endif
147                         tcp_close( s );
148                         if ( !use_hp ) {
149                                 break;
150                         }
151                 }
152         }
153
154         ber_pvt_sb_set_desc( sb, s );           
155
156         if ( connected ) {
157            
158 #ifdef notyet
159                 status = 0;
160                 if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
161                         Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
162                             s, 0, 0 );
163                 }
164 #endif /* notyet */
165
166                 Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
167                     s, (char *) inet_ntoa( sin.sin_addr ), 0 );
168         }
169
170         DO_RETURN( rc );
171 }
172    
173 #undef DO_RETURN
174
175
176 void
177 ldap_close_connection( Sockbuf *sb )
178 {
179         ber_pvt_sb_close( sb );
180 }
181
182
183 #if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
184 char *
185 ldap_host_connected_to( Sockbuf *sb )
186 {
187         struct hostent          *hp;
188         char                    *p;
189         socklen_t               len;
190         struct sockaddr_in      sin;
191
192         /* buffers for gethostbyaddr_r */
193         struct hostent          he_buf;
194         int                     local_h_errno;
195         char                    *ha_buf=NULL;
196 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
197    
198         (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
199         len = sizeof( sin );
200
201         if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
202                 return( NULL );
203         }
204
205         /*
206          * do a reverse lookup on the addr to get the official hostname.
207          * this is necessary for kerberos to work right, since the official
208          * hostname is used as the kerberos instance.
209          */
210         if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
211                 sizeof( sin.sin_addr ), 
212                 AF_INET, &he_buf, &ha_buf,
213                 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
214         {
215                 if ( hp->h_name != NULL ) {
216                         char *host = LDAP_STRDUP( hp->h_name );   
217                         DO_RETURN( host );
218                 }
219         }
220
221         DO_RETURN( NULL );
222 }
223 #undef DO_RETURN   
224    
225 #endif /* HAVE_KERBEROS || HAVE_TLS */
226
227
228 /* for UNIX */
229 struct selectinfo {
230         fd_set  si_readfds;
231         fd_set  si_writefds;
232         fd_set  si_use_readfds;
233         fd_set  si_use_writefds;
234 };
235
236
237 void
238 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
239 {
240         struct selectinfo       *sip;
241
242         sip = (struct selectinfo *)ld->ld_selectinfo;
243         
244         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
245                 FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
246         }
247 }
248
249
250 void
251 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
252 {
253         struct selectinfo       *sip;
254
255         sip = (struct selectinfo *)ld->ld_selectinfo;
256
257         if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
258                 FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
259         }
260 }
261
262
263 void
264 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
265 {
266         struct selectinfo       *sip;
267
268         sip = (struct selectinfo *)ld->ld_selectinfo;
269
270         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
271         FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
272 }
273
274
275 int
276 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
277 {
278         struct selectinfo       *sip;
279
280         sip = (struct selectinfo *)ld->ld_selectinfo;
281
282         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
283 }
284
285
286 int
287 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
288 {
289         struct selectinfo       *sip;
290
291         sip = (struct selectinfo *)ld->ld_selectinfo;
292
293         return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
294 }
295
296
297 void *
298 ldap_new_select_info( void )
299 {
300         struct selectinfo       *sip;
301
302         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
303             sizeof( struct selectinfo ))) != NULL ) {
304                 FD_ZERO( &sip->si_readfds );
305                 FD_ZERO( &sip->si_writefds );
306         }
307
308         return( (void *)sip );
309 }
310
311
312 void
313 ldap_free_select_info( void *sip )
314 {
315         LDAP_FREE( sip );
316 }
317
318
319 void
320 ldap_int_ip_init( void )
321 {
322         int tblsize;
323 #if defined( HAVE_SYSCONF )
324         tblsize = sysconf( _SC_OPEN_MAX );
325 #elif defined( HAVE_GETDTABLESIZE )
326         tblsize = getdtablesize();
327 #else
328         tblsize = FD_SETSIZE;
329 #endif /* !USE_SYSCONF */
330
331 #ifdef FD_SETSIZE
332         if( tblsize > FD_SETSIZE )
333                 tblsize = FD_SETSIZE;
334 #endif  /* FD_SETSIZE*/
335         ldap_int_tblsize = tblsize;
336 }
337
338
339 int
340 do_ldap_select( LDAP *ld, struct timeval *timeout )
341 {
342         struct selectinfo       *sip;
343
344         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
345
346         if ( ldap_int_tblsize == 0 )
347                 ldap_int_ip_init();
348
349         sip = (struct selectinfo *)ld->ld_selectinfo;
350         sip->si_use_readfds = sip->si_readfds;
351         sip->si_use_writefds = sip->si_writefds;
352         
353         return( select( ldap_int_tblsize,
354                         &sip->si_use_readfds, &sip->si_use_writefds,
355                         NULL, timeout ));
356 }