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