]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
More cleanup in ldap_pvt_tls_destroy()
[openldap] / libraries / libldap / os-ip.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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         AC_MEMCPY( (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_int_socket(LDAP *ld, int family, int type )
99 {
100         ber_socket_t s = socket(family, type, 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_int_prepare_socket(LDAP *ld, int s, int proto )
114 {
115         osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
116
117 #ifdef TCP_NODELAY
118         if( proto == LDAP_PROTO_TCP ) {
119                 int dummy = 1;
120                 if ( setsockopt( s, 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                                 s, 0, 0);
126                 }
127         }
128 #endif
129
130         return 0;
131 }
132
133 #ifndef HAVE_WINSOCK
134
135 #undef TRACE
136 #define TRACE do { \
137         osip_debug(ld, \
138                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
139                 s, \
140                 errno, \
141                 sock_errstr(errno) ); \
142 } while( 0 )
143
144 /*
145  * check the socket for errors after select returned.
146  */
147 static int
148 ldap_pvt_is_socket_ready(LDAP *ld, int s)
149 {
150         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
151
152 #if defined( notyet ) /* && defined( SO_ERROR ) */
153 {
154         int so_errno;
155         int dummy = sizeof(so_errno);
156         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
157                 == AC_SOCKET_ERROR )
158         {
159                 return -1;
160         }
161         if ( so_errno ) {
162                 ldap_pvt_set_errno(so_errno);
163                 TRACE;
164                 return -1;
165         }
166         return 0;
167 }
168 #else
169 {
170         /* error slippery */
171         struct sockaddr_in sin;
172         char ch;
173         int dummy = sizeof(sin);
174         if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
175                 == AC_SOCKET_ERROR )
176         {
177                 /* XXX: needs to be replace with ber_stream_read() */
178                 read(s, &ch, 1);
179                 TRACE;
180                 return -1;
181         }
182         return 0;
183 }
184 #endif
185         return -1;
186 }
187 #undef TRACE
188
189 #endif /* HAVE_WINSOCK */
190
191 static int
192 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
193         struct sockaddr *sin, socklen_t addrlen,
194         int async)
195 {
196         struct timeval  tv, *opt_tv=NULL;
197         fd_set          wfds, *z=NULL;
198 #ifdef HAVE_WINSOCK
199         fd_set          efds;
200 #endif
201
202 #ifdef LDAP_CONNECTIONLESS
203         /* We could do a connect() but that would interfere with
204          * attempts to poll a broadcast address
205          */
206         if (LDAP_IS_UDP(ld)) {
207                 if (ld->ld_options.ldo_peer)
208                         ldap_memfree(ld->ld_options.ldo_peer);
209                 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
210                 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
211                 return ( 0 );
212         }
213 #endif
214         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
215                 tv.tv_usec = opt_tv->tv_usec;
216                 tv.tv_sec = opt_tv->tv_sec;
217         }
218
219         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
220                         s, opt_tv ? tv.tv_sec : -1L, async);
221
222         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
223                 return ( -1 );
224
225         if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
226         {
227                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
228                         return ( -1 );
229                 return ( 0 );
230         }
231
232 #ifdef HAVE_WINSOCK
233         ldap_pvt_set_errno( WSAGetLastError() );
234 #endif
235
236         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
237                 return ( -1 );
238         }
239         
240 #ifdef notyet
241         if ( async ) return ( -2 );
242 #endif
243
244         FD_ZERO(&wfds);
245         FD_SET(s, &wfds );
246
247 #ifdef HAVE_WINSOCK
248         FD_ZERO(&efds);
249         FD_SET(s, &efds );
250 #endif
251
252         if ( select(ldap_int_tblsize, z, &wfds,
253 #ifdef HAVE_WINSOCK
254                     &efds,
255 #else
256                     z,
257 #endif
258                     opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
259         {
260                 return ( -1 );
261         }
262
263 #ifdef HAVE_WINSOCK
264         /* This means the connection failed */
265         if ( FD_ISSET(s, &efds) ) {
266             int so_errno;
267             int dummy = sizeof(so_errno);
268             if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
269                         (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
270             {
271                 /* impossible */
272                 so_errno = WSAGetLastError();
273             }
274             ldap_pvt_set_errno(so_errno);
275             osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
276                        "errno: %d (%s)\n", s, errno, sock_errstr(errno));
277             return -1;
278         }
279 #endif
280         if ( FD_ISSET(s, &wfds) ) {
281 #ifndef HAVE_WINSOCK
282                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
283                         return ( -1 );
284 #endif
285                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
286                         return ( -1 );
287                 return ( 0 );
288         }
289         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
290         ldap_pvt_set_errno( ETIMEDOUT );
291         return ( -1 );
292 }
293
294 #ifndef HAVE_INET_ATON
295 int
296 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
297 {
298         unsigned long u = inet_addr( host );
299         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
300                 in->s_addr = u;
301                 return 1;
302         }
303         return 0;
304 }
305 #endif
306
307
308 int
309 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
310         int proto,
311         const char *host,
312         unsigned long address, int port, int async )
313 {
314         struct sockaddr_in      sin;
315         ber_socket_t            s = AC_SOCKET_INVALID;
316         int                     rc, i, use_hp = 0;
317         struct hostent          *hp = NULL;
318         char                    *ha_buf=NULL, *p, *q;
319         int                     socktype;
320
321         osip_debug(ld, "ldap_connect_to_host: %s\n",host,0,0);
322         
323         switch(proto) {
324         case LDAP_PROTO_TCP: socktype = SOCK_STREAM; break;
325         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM; break;
326         default: osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
327                                 proto, 0, 0);
328                 return -1;
329         }
330
331         if (host != NULL) {
332 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
333                 char serv[7];
334                 int err;
335                 struct addrinfo hints, *res, *sai;
336
337                 memset( &hints, '\0', sizeof(hints) );
338                 hints.ai_family = AF_UNSPEC;
339                 hints.ai_socktype = socktype;
340
341                 snprintf(serv, sizeof serv, "%d", port );
342                 if ( err = getaddrinfo(host, serv, &hints, &res) ) {
343                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
344                                 AC_GAI_STRERROR(err), 0, 0);
345                         return -1;
346                 }
347                 rc = -1;
348
349                 for( sai=res; sai != NULL; sai=sai->ai_next) {
350                         if( sai->ai_addr == NULL ) {
351                                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
352                                         "ai_addr is NULL?\n", 0, 0, 0);
353                                 continue;
354                         }
355
356                         /* we assume AF_x and PF_x are equal for all x */
357                         s = ldap_int_socket( ld, sai->ai_family, socktype );
358                         if ( s == AC_SOCKET_INVALID ) {
359                                 continue;
360                         }
361
362                         if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
363                                 ldap_pvt_close_socket(ld, s);
364                                 break;
365                         }
366
367                         switch (sai->ai_family) {
368 #ifdef LDAP_PF_INET6
369                         case AF_INET6: {
370                                 char addr[INET6_ADDRSTRLEN];
371                                 inet_ntop( AF_INET6,
372                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
373                                         addr, sizeof addr);
374                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
375                                         addr, serv, 0);
376                         } break;
377 #endif
378                         case AF_INET: {
379                                 char addr[INET_ADDRSTRLEN];
380                                 inet_ntop( AF_INET,
381                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
382                                         addr, sizeof addr);
383                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
384                                         addr, serv, 0);
385                         } break;
386                         }
387
388                         rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
389                         if ( (rc == 0) || (rc == -2) ) {
390                                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
391                                 break;
392                         }
393                         ldap_pvt_close_socket(ld, s);
394                 }
395                 freeaddrinfo(res);
396                 return rc;
397
398 #else
399                 struct in_addr in;
400                 if (! inet_aton( host, &in) ) {
401                         int local_h_errno;
402                         struct hostent he_buf;
403                         rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
404                                         &hp, &local_h_errno);
405
406                         if ( (rc < 0) || (hp == NULL) ) {
407 #ifdef HAVE_WINSOCK
408                                 ldap_pvt_set_errno( WSAGetLastError() );
409 #else
410                                 /* not exactly right, but... */
411                                 ldap_pvt_set_errno( EHOSTUNREACH );
412 #endif
413                                 if (ha_buf) LDAP_FREE(ha_buf);
414                                 return -1;
415                         }
416                         use_hp = 1;
417                 }
418                 address = in.s_addr;
419 #endif
420         }
421
422         rc = s = -1;
423         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
424                 s = ldap_int_socket( ld, PF_INET, socktype );
425                 if ( s == AC_SOCKET_INVALID ) {
426                         /* use_hp ? continue : break; */
427                         break;
428                 }
429            
430                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
431                         ldap_pvt_close_socket(ld, s);
432                         break;
433                 }
434
435                 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
436                 sin.sin_family = AF_INET;
437                 sin.sin_port = htons((short) port);
438                 p = (char *)&sin.sin_addr;
439                 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
440                 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
441
442                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
443                         inet_ntoa(sin.sin_addr),port,0);
444
445                 rc = ldap_pvt_connect(ld, s,
446                         (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
447                         async);
448    
449                 if ( (rc == 0) || (rc == -2) ) {
450                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
451                         break;
452                 }
453
454                 ldap_pvt_close_socket(ld, s);
455
456                 if (!use_hp)
457                         break;
458         }
459         if (ha_buf) LDAP_FREE(ha_buf);
460         return rc;
461 }
462
463 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
464         defined( HAVE_CYRUS_SASL )
465 char *
466 ldap_host_connected_to( Sockbuf *sb )
467 {
468         struct hostent  *hp;
469         socklen_t               len;
470         struct sockaddr sa;
471         char                    *addr;
472         char                    *host;
473
474         /* buffers for gethostbyaddr_r */
475         struct hostent  he_buf;
476         int                             local_h_errno;
477         char                    *ha_buf=NULL;
478         ber_socket_t    sd;
479
480         (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
481         len = sizeof( sa );
482
483         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
484         if ( getpeername( sd, &sa, &len ) == -1 ) {
485                 return( NULL );
486         }
487
488         /*
489          * do a reverse lookup on the addr to get the official hostname.
490          * this is necessary for kerberos to work right, since the official
491          * hostname is used as the kerberos instance.
492          */
493
494         switch (sa.sa_family) {
495 #ifdef LDAP_PF_LOCAL
496         case AF_LOCAL:
497                 return LDAP_STRDUP( ldap_int_hostname );
498 #endif
499 #ifdef LDAP_PF_INET6
500         case AF_INET6:
501                 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
502                 len = sizeof( struct in6_addr );
503                 break;
504 #endif
505         case AF_INET:
506                 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
507                 len = sizeof( struct in_addr );
508
509                 {
510                         struct sockaddr_in localhost;
511                         localhost.sin_addr.s_addr = htonl( INADDR_ANY );
512
513                         if( memcmp ( &localhost.sin_addr,
514                                 &((struct sockaddr_in *)&sa)->sin_addr,
515                                 sizeof(localhost.sin_addr) ) == 0 )
516                         {
517                                 return LDAP_STRDUP( ldap_int_hostname );
518                         }
519
520 #ifdef INADDR_LOOPBACK
521                         localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
522
523                         if( memcmp ( &localhost.sin_addr,
524                                 &((struct sockaddr_in *)&sa)->sin_addr,
525                                 sizeof(localhost.sin_addr) ) == 0 )
526                         {
527                                 return LDAP_STRDUP( ldap_int_hostname );
528                         }
529 #endif
530                 }
531                 break;
532
533         default:
534                 return( NULL );
535                 break;
536         }
537
538         host = NULL;
539         if ((ldap_pvt_gethostbyaddr_a( addr, len,
540                 sa.sa_family, &he_buf, &ha_buf,
541                 &hp,&local_h_errno ) == 0 ) &&
542                 (hp != NULL) && ( hp->h_name != NULL ) )
543         {
544                 host = LDAP_STRDUP( hp->h_name );   
545         }
546
547         LDAP_FREE( ha_buf );
548         return host;
549 }
550 #endif
551
552
553 /* for UNIX */
554 struct selectinfo {
555         fd_set  si_readfds;
556         fd_set  si_writefds;
557         fd_set  si_use_readfds;
558         fd_set  si_use_writefds;
559 };
560
561
562 void
563 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
564 {
565         struct selectinfo       *sip;
566         ber_socket_t            sd;
567
568         sip = (struct selectinfo *)ld->ld_selectinfo;
569         
570         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
571         if ( !FD_ISSET( sd, &sip->si_writefds )) {
572                 FD_SET( sd, &sip->si_writefds );
573         }
574 }
575
576
577 void
578 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
579 {
580         struct selectinfo       *sip;
581         ber_socket_t            sd;
582
583         sip = (struct selectinfo *)ld->ld_selectinfo;
584
585         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
586         if ( !FD_ISSET( sd, &sip->si_readfds )) {
587                 FD_SET( sd, &sip->si_readfds );
588         }
589 }
590
591
592 void
593 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
594 {
595         struct selectinfo       *sip;
596         ber_socket_t            sd;
597
598         sip = (struct selectinfo *)ld->ld_selectinfo;
599
600         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
601         FD_CLR( sd, &sip->si_writefds );
602         FD_CLR( sd, &sip->si_readfds );
603 }
604
605
606 int
607 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
608 {
609         struct selectinfo       *sip;
610         ber_socket_t            sd;
611
612         sip = (struct selectinfo *)ld->ld_selectinfo;
613
614         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
615         return( FD_ISSET( sd, &sip->si_use_writefds ));
616 }
617
618
619 int
620 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
621 {
622         struct selectinfo       *sip;
623         ber_socket_t            sd;
624
625         sip = (struct selectinfo *)ld->ld_selectinfo;
626
627         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
628         return( FD_ISSET( sd, &sip->si_use_readfds ));
629 }
630
631
632 void *
633 ldap_new_select_info( void )
634 {
635         struct selectinfo       *sip;
636
637         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
638             sizeof( struct selectinfo ))) != NULL ) {
639                 FD_ZERO( &sip->si_readfds );
640                 FD_ZERO( &sip->si_writefds );
641         }
642
643         return( (void *)sip );
644 }
645
646
647 void
648 ldap_free_select_info( void *sip )
649 {
650         LDAP_FREE( sip );
651 }
652
653
654 void
655 ldap_int_ip_init( void )
656 {
657         int tblsize;
658 #if defined( HAVE_SYSCONF )
659         tblsize = sysconf( _SC_OPEN_MAX );
660 #elif defined( HAVE_GETDTABLESIZE )
661         tblsize = getdtablesize();
662 #else
663         tblsize = FD_SETSIZE;
664 #endif /* !USE_SYSCONF */
665
666 #ifdef FD_SETSIZE
667         if( tblsize > FD_SETSIZE )
668                 tblsize = FD_SETSIZE;
669 #endif  /* FD_SETSIZE*/
670         ldap_int_tblsize = tblsize;
671 }
672
673
674 int
675 ldap_int_select( LDAP *ld, struct timeval *timeout )
676 {
677         struct selectinfo       *sip;
678
679         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
680
681         if ( ldap_int_tblsize == 0 )
682                 ldap_int_ip_init();
683
684         sip = (struct selectinfo *)ld->ld_selectinfo;
685         sip->si_use_readfds = sip->si_readfds;
686         sip->si_use_writefds = sip->si_writefds;
687         
688         return( select( ldap_int_tblsize,
689                         &sip->si_use_readfds, &sip->si_use_writefds,
690                         NULL, timeout ));
691 }