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