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