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