]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Update to latest devel codes
[openldap] / libraries / libldap / os-ip.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  os-ip.c -- platform-specific TCP & UDP related code
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/errno.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/unistd.h>
24
25 #ifdef HAVE_IO_H
26 #include <io.h>
27 #endif /* HAVE_IO_H */
28
29 #include "ldap-int.h"
30
31 int ldap_int_tblsize = 0;
32
33 /*
34  * nonblock connect code
35  * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
36  *
37  * Copyright 1999, Lars Uffmann, All rights reserved.
38  * This software is not subject to any license of my employer
39  * mediaWays GmbH.
40  *
41  * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
42  *
43  * Read about the rationale in ldap_connect_timeout: 
44  * ftp://koobera.math.uic.edu/www/docs/connect.html.
45  */
46
47 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
48 do { \
49         ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
50 } while(0)
51
52 static void
53 ldap_pvt_set_errno(int err)
54 {
55         errno = err;
56 }
57
58 int
59 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
60 {
61         struct timeval *new;
62
63         assert( dest != NULL );
64
65         if (src == NULL) {
66                 *dest = NULL;
67                 return 0;
68         }
69
70         new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
71
72         if( new == NULL ) {
73                 *dest = NULL;
74                 return 1;
75         }
76
77         SAFEMEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
78
79         *dest = new;
80         return 0;
81 }
82
83 static int
84 ldap_pvt_ndelay_on(LDAP *ld, int fd)
85 {
86         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
87         return ber_pvt_socket_set_nonblock( fd, 1 );
88 }
89    
90 static int
91 ldap_pvt_ndelay_off(LDAP *ld, int fd)
92 {
93         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
94         return ber_pvt_socket_set_nonblock( fd, 0 );
95 }
96
97 static ber_socket_t
98 ldap_pvt_socket(LDAP *ld, int family)
99 {
100         ber_socket_t s = socket(family, SOCK_STREAM, 0);
101         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
102         return ( s );
103 }
104
105 static int
106 ldap_pvt_close_socket(LDAP *ld, int s)
107 {
108         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
109         return tcp_close(s);
110 }
111
112 static int
113 ldap_pvt_prepare_socket(LDAP *ld, int fd)
114 {
115         osip_debug(ld, "ldap_prepare_socket: %d\n",fd,0,0);
116
117 #ifdef TCP_NODELAY
118 {
119         int dummy = 1;
120         if ( setsockopt( fd, IPPROTO_TCP, TCP_NODELAY,
121                 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
122         {
123                 osip_debug(ld, "ldap_prepare_socket: "
124                         "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
125                         fd, 0, 0);
126         }
127 }
128 #endif
129         return 0;
130 }
131
132 #undef TRACE
133 #define TRACE do { \
134         osip_debug(ld, \
135                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
136                 s, \
137                 errno, \
138                 sock_errstr(errno) ); \
139 } while( 0 )
140
141 /*
142  * check the socket for errors after select returned.
143  */
144 static int
145 ldap_pvt_is_socket_ready(LDAP *ld, int s)
146 {
147         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
148
149 #if defined( notyet ) /* && defined( SO_ERROR ) */
150 {
151         int so_errno;
152         int dummy = sizeof(so_errno);
153         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy ) == -1 ) {
154                 return -1;
155         }
156         if ( so_errno ) {
157                 ldap_pvt_set_errno(so_errno);
158                 TRACE;
159                 return -1;
160         }
161         return 0;
162 }
163 #else
164 {
165         /* error slippery */
166         struct sockaddr_in sin;
167         char ch;
168         int dummy = sizeof(sin);
169         if ( getpeername( s, (struct sockaddr *) &sin, &dummy ) == -1 ) {
170                 /* XXX: needs to be replace with ber_stream_read() */
171                 read(s, &ch, 1);
172 #ifdef HAVE_WINSOCK
173                 ldap_pvt_set_errno( WSAGetLastError() );
174 #endif
175                 TRACE;
176                 return -1;
177         }
178         return 0;
179 }
180 #endif
181         return -1;
182 }
183 #undef TRACE
184
185 static int
186 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr *sin, socklen_t addrlen, int async)
187 {
188         struct timeval  tv, *opt_tv=NULL;
189         fd_set          wfds, *z=NULL;
190 #ifdef HAVE_WINSOCK
191         fd_set          efds;
192 #endif
193
194         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
195                 tv.tv_usec = opt_tv->tv_usec;
196                 tv.tv_sec = opt_tv->tv_sec;
197         }
198
199         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
200                         s, opt_tv ? tv.tv_sec : -1L, async);
201
202         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
203                 return ( -1 );
204
205         if ( connect(s, sin, addrlen) == 0 )
206         {
207                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
208                         return ( -1 );
209                 return ( 0 );
210         }
211
212 #ifdef HAVE_WINSOCK
213         ldap_pvt_set_errno( WSAGetLastError() );
214 #endif
215
216         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
217                 return ( -1 );
218         }
219         
220 #ifdef notyet
221         if ( async ) return ( -2 );
222 #endif
223
224         FD_ZERO(&wfds);
225         FD_SET(s, &wfds );
226
227 #ifdef HAVE_WINSOCK
228         FD_ZERO(&efds);
229         FD_SET(s, &efds );
230 #endif
231
232         if ( select(ldap_int_tblsize, z, &wfds,
233 #ifdef HAVE_WINSOCK
234                     &efds,
235 #else
236                     z,
237 #endif
238                     opt_tv ? &tv : NULL) == -1)
239                 return ( -1 );
240
241 #ifdef HAVE_WINSOCK
242         /* This means the connection failed */
243         if (FD_ISSET(s, &efds))
244         {
245             ldap_pvt_set_errno(WSAECONNREFUSED);
246             osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
247                        "errno: %d (%s)\n", s, errno, sock_errstr(errno));
248             return -1;
249         }
250 #endif
251         if ( FD_ISSET(s, &wfds) ) {
252                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
253                         return ( -1 );
254                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
255                         return ( -1 );
256                 return ( 0 );
257         }
258         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
259         ldap_pvt_set_errno( ETIMEDOUT );
260         return ( -1 );
261 }
262
263 #ifndef HAVE_INET_ATON
264 int
265 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
266 {
267         unsigned long u = inet_addr( host );
268         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
269                 in->s_addr = u;
270                 return 1;
271         }
272         return 0;
273 }
274 #endif
275
276
277 int
278 ldap_connect_to_host(LDAP *ld, Sockbuf *sb, const char *host,
279                 unsigned long address, int port, int async)
280 {
281         struct sockaddr_in      sin;
282         struct in_addr          in;
283         ber_socket_t            s = AC_SOCKET_INVALID;
284         int                     rc, i, use_hp = 0;
285         struct hostent          *hp, he_buf;
286         int                     local_h_errno;
287         char                    *ha_buf=NULL, *p, *q;
288
289         osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
290         
291         if (host != NULL) {
292 #ifdef HAVE_GETADDRINFO
293                 char serv[7];
294                 struct addrinfo hints, *res, *sai;
295
296                 memset( &hints, '\0', sizeof(hints) );
297                 hints.ai_family = AF_UNSPEC;
298                 hints.ai_socktype = SOCK_STREAM;
299
300                 snprintf(serv, sizeof serv, "%d", ntohs(port));
301                 if ( getaddrinfo(host, serv, &hints, &res) ) {
302                         osip_debug(ld, "ldap_connect_to_host:getaddrinfo failed\n",0,0,0);
303                         return -1;
304                 }
305                 sai = res;
306                 rc = -1;
307                 do {
308                         s = ldap_pvt_socket( ld, sai->ai_family );
309                         if ( s == -1 ) {
310                                 continue;
311                         }
312
313                         switch (sai->ai_family) {
314 #ifdef LDAP_PF_INET6
315                         case AF_INET6: {
316                                 char addr[INET6_ADDRSTRLEN];
317                                 inet_ntop( AF_INET6,
318                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
319                                         addr, sizeof addr);
320                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
321                                         addr, serv, 0);
322                         } break;
323 #endif
324                         case AF_INET: {
325                                 char addr[INET_ADDRSTRLEN];
326                                 inet_ntop( AF_INET,
327                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
328                                         addr, sizeof addr);
329                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
330                                         addr, serv, 0);
331                         } break;
332                         }
333                         rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
334                         if ( (rc == 0) || (rc == -2) ) {
335                                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
336                                 break;
337                         }
338                         ldap_pvt_close_socket(ld, s);
339                 } while ((sai = sai->ai_next) != NULL);
340                 freeaddrinfo(res);
341                 return rc;
342 #else
343                 if (! inet_aton( host, &in) ) {
344                         rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
345                                         &hp, &local_h_errno);
346
347                         if ( (rc < 0) || (hp == NULL) ) {
348 #ifdef HAVE_WINSOCK
349                                 ldap_pvt_set_errno( WSAGetLastError() );
350 #else
351                                 /* not exactly right, but... */
352                                 ldap_pvt_set_errno( EHOSTUNREACH );
353 #endif
354                                 if (ha_buf) LDAP_FREE(ha_buf);
355                                 return -1;
356                         }
357                         use_hp = 1;
358                 }
359                 address = in.s_addr;
360 #endif
361         }
362
363         rc = s = -1;
364         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
365
366                 s = ldap_pvt_socket( ld, AF_INET );
367                 if ( s == -1 ) {
368                         /* use_hp ? continue : break; */
369                         break;
370                 }
371            
372                 if ( ldap_pvt_prepare_socket(ld, s) == -1 ) {
373                         ldap_pvt_close_socket(ld, s);
374                         /* use_hp ? continue : break; */
375                         break;
376                 }
377
378                 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
379                 sin.sin_family = AF_INET;
380                 sin.sin_port = port;
381                 p = (char *)&sin.sin_addr;
382                 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
383                 SAFEMEMCPY(p, q, sizeof(sin.sin_addr) );
384
385                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
386                                 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
387
388                 rc = ldap_pvt_connect(ld, s, (struct sockaddr *)&sin, sizeof(struct sockaddr_in), async);
389    
390                 if ( (rc == 0) || (rc == -2) ) {
391                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
392                         break;
393                 }
394
395                 ldap_pvt_close_socket(ld, s);
396
397                 if (!use_hp)
398                         break;
399         }
400         if (ha_buf) LDAP_FREE(ha_buf);
401         return rc;
402 }
403
404 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || defined( HAVE_TLS ) || defined( HAVE_CYRUS_SASL )
405 char *
406 ldap_host_connected_to( Sockbuf *sb )
407 {
408         struct hostent          *hp;
409         socklen_t               len;
410         struct sockaddr         sa;
411         char                    *addr;
412
413         /* buffers for gethostbyaddr_r */
414         struct hostent          he_buf;
415         int                     local_h_errno;
416         char                    *ha_buf=NULL;
417         ber_socket_t            sd;
418 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
419    
420         (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
421         len = sizeof( sa );
422
423         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
424         if ( getpeername( sd, (struct sockaddr *)&sa, &len ) == -1 ) {
425                 return( NULL );
426         }
427
428         /*
429          * do a reverse lookup on the addr to get the official hostname.
430          * this is necessary for kerberos to work right, since the official
431          * hostname is used as the kerberos instance.
432          */
433
434         switch (sa.sa_family) {
435 #ifdef LDAP_PF_INET6
436         case AF_INET6:
437                 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
438                 len = sizeof( struct in6_addr );
439                 break;
440 #endif
441         case AF_INET:
442                 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
443                 len = sizeof( struct in_addr );
444                 break;
445         default:
446                 return( NULL );
447                 break;
448         }
449         if ((ldap_pvt_gethostbyaddr_a( addr, len,
450                 sa.sa_family, &he_buf, &ha_buf,
451                 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
452         {
453                 if ( hp->h_name != NULL ) {
454                         char *host = LDAP_STRDUP( hp->h_name );   
455                         DO_RETURN( host );
456                 }
457         }
458
459         DO_RETURN( NULL );
460 }
461 #undef DO_RETURN   
462    
463 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND || HAVE_TLS */
464
465
466 /* for UNIX */
467 struct selectinfo {
468         fd_set  si_readfds;
469         fd_set  si_writefds;
470         fd_set  si_use_readfds;
471         fd_set  si_use_writefds;
472 };
473
474
475 void
476 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
477 {
478         struct selectinfo       *sip;
479         ber_socket_t            sd;
480
481         sip = (struct selectinfo *)ld->ld_selectinfo;
482         
483         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
484         if ( !FD_ISSET( sd, &sip->si_writefds )) {
485                 FD_SET( sd, &sip->si_writefds );
486         }
487 }
488
489
490 void
491 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
492 {
493         struct selectinfo       *sip;
494         ber_socket_t            sd;
495
496         sip = (struct selectinfo *)ld->ld_selectinfo;
497
498         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
499         if ( !FD_ISSET( sd, &sip->si_readfds )) {
500                 FD_SET( sd, &sip->si_readfds );
501         }
502 }
503
504
505 void
506 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
507 {
508         struct selectinfo       *sip;
509         ber_socket_t            sd;
510
511         sip = (struct selectinfo *)ld->ld_selectinfo;
512
513         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
514         FD_CLR( sd, &sip->si_writefds );
515         FD_CLR( sd, &sip->si_readfds );
516 }
517
518
519 int
520 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
521 {
522         struct selectinfo       *sip;
523         ber_socket_t            sd;
524
525         sip = (struct selectinfo *)ld->ld_selectinfo;
526
527         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
528         return( FD_ISSET( sd, &sip->si_use_writefds ));
529 }
530
531
532 int
533 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
534 {
535         struct selectinfo       *sip;
536         ber_socket_t            sd;
537
538         sip = (struct selectinfo *)ld->ld_selectinfo;
539
540         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
541         return( FD_ISSET( sd, &sip->si_use_readfds ));
542 }
543
544
545 void *
546 ldap_new_select_info( void )
547 {
548         struct selectinfo       *sip;
549
550         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
551             sizeof( struct selectinfo ))) != NULL ) {
552                 FD_ZERO( &sip->si_readfds );
553                 FD_ZERO( &sip->si_writefds );
554         }
555
556         return( (void *)sip );
557 }
558
559
560 void
561 ldap_free_select_info( void *sip )
562 {
563         LDAP_FREE( sip );
564 }
565
566
567 void
568 ldap_int_ip_init( void )
569 {
570         int tblsize;
571 #if defined( HAVE_SYSCONF )
572         tblsize = sysconf( _SC_OPEN_MAX );
573 #elif defined( HAVE_GETDTABLESIZE )
574         tblsize = getdtablesize();
575 #else
576         tblsize = FD_SETSIZE;
577 #endif /* !USE_SYSCONF */
578
579 #ifdef FD_SETSIZE
580         if( tblsize > FD_SETSIZE )
581                 tblsize = FD_SETSIZE;
582 #endif  /* FD_SETSIZE*/
583         ldap_int_tblsize = tblsize;
584 }
585
586
587 int
588 do_ldap_select( LDAP *ld, struct timeval *timeout )
589 {
590         struct selectinfo       *sip;
591
592         Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
593
594         if ( ldap_int_tblsize == 0 )
595                 ldap_int_ip_init();
596
597         sip = (struct selectinfo *)ld->ld_selectinfo;
598         sip->si_use_readfds = sip->si_readfds;
599         sip->si_use_writefds = sip->si_writefds;
600         
601         return( select( ldap_int_tblsize,
602                         &sip->si_use_readfds, &sip->si_use_writefds,
603                         NULL, timeout ));
604 }