]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Merge remote branch 'origin/mdb.master'
[openldap] / libraries / libldap / os-ip.c
1 /* os-ip.c -- platform-specific TCP & UDP related code */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2011 The OpenLDAP Foundation.
6  * Portions Copyright 1999 Lars Uffmann.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
18  * All rights reserved.
19  */
20 /* Significant additional contributors include:
21  *    Lars Uffman
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/errno.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
33 #include <ac/time.h>
34 #include <ac/unistd.h>
35
36 #ifdef HAVE_IO_H
37 #include <io.h>
38 #endif /* HAVE_IO_H */
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42
43 #include "ldap-int.h"
44
45 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
46 #  ifdef LDAP_PF_INET6
47 int ldap_int_inet4or6 = AF_UNSPEC;
48 #  else
49 int ldap_int_inet4or6 = AF_INET;
50 #  endif
51 #endif
52
53 #ifdef LDAP_DEBUG
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 #else
61
62 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
63
64 #endif /* LDAP_DEBUG */
65
66 static void
67 ldap_pvt_set_errno(int err)
68 {
69         sock_errset(err);
70 }
71
72 int
73 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
74 {
75         struct timeval *new;
76
77         assert( dest != NULL );
78
79         if (src == NULL) {
80                 *dest = NULL;
81                 return 0;
82         }
83
84         new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
85
86         if( new == NULL ) {
87                 *dest = NULL;
88                 return 1;
89         }
90
91         AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
92
93         *dest = new;
94         return 0;
95 }
96
97 static int
98 ldap_pvt_ndelay_on(LDAP *ld, int fd)
99 {
100         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
101         return ber_pvt_socket_set_nonblock( fd, 1 );
102 }
103    
104 static int
105 ldap_pvt_ndelay_off(LDAP *ld, int fd)
106 {
107         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
108         return ber_pvt_socket_set_nonblock( fd, 0 );
109 }
110
111 static ber_socket_t
112 ldap_int_socket(LDAP *ld, int family, int type )
113 {
114         ber_socket_t s = socket(family, type, 0);
115         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
116 #ifdef FD_CLOEXEC
117         fcntl(s, F_SETFD, FD_CLOEXEC);
118 #endif
119         return ( s );
120 }
121
122 static int
123 ldap_pvt_close_socket(LDAP *ld, int s)
124 {
125         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
126         return tcp_close(s);
127 }
128
129 static int
130 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
131 {
132         osip_debug( ld, "ldap_prepare_socket: %d\n", s, 0, 0 );
133
134 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
135         if ( proto == LDAP_PROTO_TCP ) {
136                 int dummy = 1;
137 #ifdef SO_KEEPALIVE
138                 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
139                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
140                 {
141                         osip_debug( ld, "ldap_prepare_socket: "
142                                 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
143                                 s, 0, 0 );
144                 }
145                 if ( ld->ld_options.ldo_keepalive_idle > 0 )
146                 {
147 #ifdef TCP_KEEPIDLE
148                         if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPIDLE,
149                                         (void*) &ld->ld_options.ldo_keepalive_idle,
150                                         sizeof(ld->ld_options.ldo_keepalive_idle) ) == AC_SOCKET_ERROR )
151                         {
152                                 osip_debug( ld, "ldap_prepare_socket: "
153                                         "setsockopt(%d, TCP_KEEPIDLE) failed (ignored).\n",
154                                         s, 0, 0 );
155                         }
156 #else
157                         osip_debug( ld, "ldap_prepare_socket: "
158                                         "sockopt TCP_KEEPIDLE not supported on this system.\n", 
159                                         0, 0, 0 );
160 #endif /* TCP_KEEPIDLE */
161                 }
162                 if ( ld->ld_options.ldo_keepalive_probes > 0 )
163                 {
164 #ifdef TCP_KEEPCNT
165                         if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPCNT,
166                                         (void*) &ld->ld_options.ldo_keepalive_probes,
167                                         sizeof(ld->ld_options.ldo_keepalive_probes) ) == AC_SOCKET_ERROR )
168                         {
169                                 osip_debug( ld, "ldap_prepare_socket: "
170                                         "setsockopt(%d, TCP_KEEPCNT) failed (ignored).\n",
171                                         s, 0, 0 );
172                         }
173 #else
174                         osip_debug( ld, "ldap_prepare_socket: "
175                                         "sockopt TCP_KEEPCNT not supported on this system.\n", 
176                                         0, 0, 0 );
177 #endif /* TCP_KEEPCNT */
178                 }
179                 if ( ld->ld_options.ldo_keepalive_interval > 0 )
180                 {
181 #ifdef TCP_KEEPINTVL
182                         if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPINTVL,
183                                         (void*) &ld->ld_options.ldo_keepalive_interval,
184                                         sizeof(ld->ld_options.ldo_keepalive_interval) ) == AC_SOCKET_ERROR )
185                         {
186                                 osip_debug( ld, "ldap_prepare_socket: "
187                                         "setsockopt(%d, TCP_KEEPINTVL) failed (ignored).\n",
188                                         s, 0, 0 );
189                         } 
190 #else
191                         osip_debug( ld, "ldap_prepare_socket: "
192                                         "sockopt TCP_KEEPINTVL not supported on this system.\n", 
193                                         0, 0, 0 );
194 #endif /* TCP_KEEPINTVL */
195                 }
196 #endif /* SO_KEEPALIVE */
197 #ifdef TCP_NODELAY
198                 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
199                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
200                 {
201                         osip_debug( ld, "ldap_prepare_socket: "
202                                 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
203                                 s, 0, 0 );
204                 }
205 #endif /* TCP_NODELAY */
206         }
207 #endif /* SO_KEEPALIVE || TCP_NODELAY */
208
209         return 0;
210 }
211
212 #ifndef HAVE_WINSOCK
213
214 #undef TRACE
215 #define TRACE do { \
216         osip_debug(ld, \
217                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
218                 s, \
219                 errno, \
220                 sock_errstr(errno) ); \
221 } while( 0 )
222
223 /*
224  * check the socket for errors after select returned.
225  */
226 static int
227 ldap_pvt_is_socket_ready(LDAP *ld, int s)
228 {
229         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
230
231 #if defined( notyet ) /* && defined( SO_ERROR ) */
232 {
233         int so_errno;
234         ber_socklen_t dummy = sizeof(so_errno);
235         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
236                 == AC_SOCKET_ERROR )
237         {
238                 return -1;
239         }
240         if ( so_errno ) {
241                 ldap_pvt_set_errno(so_errno);
242                 TRACE;
243                 return -1;
244         }
245         return 0;
246 }
247 #else
248 {
249         /* error slippery */
250 #ifdef LDAP_PF_INET6
251         struct sockaddr_storage sin;
252 #else
253         struct sockaddr_in sin;
254 #endif
255         char ch;
256         ber_socklen_t dummy = sizeof(sin);
257         if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
258                 == AC_SOCKET_ERROR )
259         {
260                 /* XXX: needs to be replace with ber_stream_read() */
261                 (void)read(s, &ch, 1);
262                 TRACE;
263                 return -1;
264         }
265         return 0;
266 }
267 #endif
268         return -1;
269 }
270 #undef TRACE
271
272 #endif /* HAVE_WINSOCK */
273
274 /* NOTE: this is identical to analogous code in os-local.c */
275 int
276 ldap_int_poll(
277         LDAP *ld,
278         ber_socket_t s,
279         struct timeval *tvp )
280 {
281         int             rc;
282                 
283
284         osip_debug(ld, "ldap_int_poll: fd: %d tm: %ld\n",
285                 s, tvp ? tvp->tv_sec : -1L, 0);
286
287 #ifdef HAVE_POLL
288         {
289                 struct pollfd fd;
290                 int timeout = INFTIM;
291
292                 fd.fd = s;
293                 fd.events = POLL_WRITE;
294
295                 if ( tvp != NULL ) {
296                         timeout = TV2MILLISEC( tvp );
297                 }
298                 do {
299                         fd.revents = 0;
300                         rc = poll( &fd, 1, timeout );
301                 
302                 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
303                         LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
304
305                 if ( rc == AC_SOCKET_ERROR ) {
306                         return rc;
307                 }
308
309                 if ( timeout == 0 && rc == 0 ) {
310                         return -2;
311                 }
312
313                 if ( fd.revents & POLL_WRITE ) {
314                         if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
315                                 return -1;
316                         }
317
318                         if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
319                                 return -1;
320                         }
321                         return 0;
322                 }
323         }
324 #else
325         {
326                 fd_set          wfds, *z = NULL;
327 #ifdef HAVE_WINSOCK
328                 fd_set          efds;
329 #endif
330                 struct timeval  tv = { 0 };
331
332 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
333                 if ( s >= FD_SETSIZE ) {
334                         rc = AC_SOCKET_ERROR;
335                         tcp_close( s );
336                         ldap_pvt_set_errno( EMFILE );
337                         return rc;
338                 }
339 #endif
340
341                 if ( tvp != NULL ) {
342                         tv = *tvp;
343                 }
344
345                 do {
346                         FD_ZERO(&wfds);
347                         FD_SET(s, &wfds );
348
349 #ifdef HAVE_WINSOCK
350                         FD_ZERO(&efds);
351                         FD_SET(s, &efds );
352 #endif
353
354                         rc = select( ldap_int_tblsize, z, &wfds,
355 #ifdef HAVE_WINSOCK
356                                 &efds,
357 #else
358                                 z,
359 #endif
360                                 tvp ? &tv : NULL );
361                 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
362                         LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
363
364                 if ( rc == AC_SOCKET_ERROR ) {
365                         return rc;
366                 }
367
368                 if ( rc == 0 && tvp && tvp->tv_sec == 0 && tvp->tv_usec == 0 ) {
369                         return -2;
370                 }
371
372 #ifdef HAVE_WINSOCK
373                 /* This means the connection failed */
374                 if ( FD_ISSET(s, &efds) ) {
375                         int so_errno;
376                         ber_socklen_t dummy = sizeof(so_errno);
377                         if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
378                                 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
379                         {
380                                 /* impossible */
381                                 so_errno = WSAGetLastError();
382                         }
383                         ldap_pvt_set_errno( so_errno );
384                         osip_debug(ld, "ldap_int_poll: error on socket %d: "
385                                "errno: %d (%s)\n", s, errno, sock_errstr( errno ));
386                         return -1;
387                 }
388 #endif
389                 if ( FD_ISSET(s, &wfds) ) {
390 #ifndef HAVE_WINSOCK
391                         if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
392                                 return -1;
393                         }
394 #endif
395                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
396                                 return -1;
397                         }
398                         return 0;
399                 }
400         }
401 #endif
402
403         osip_debug(ld, "ldap_int_poll: timed out\n",0,0,0);
404         ldap_pvt_set_errno( ETIMEDOUT );
405         return -1;
406 }
407
408 static int
409 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
410         struct sockaddr *sin, ber_socklen_t addrlen,
411         int async)
412 {
413         int rc, err;
414         struct timeval  tv, *opt_tv = NULL;
415
416 #ifdef LDAP_CONNECTIONLESS
417         /* We could do a connect() but that would interfere with
418          * attempts to poll a broadcast address
419          */
420         if (LDAP_IS_UDP(ld)) {
421                 if (ld->ld_options.ldo_peer)
422                         ldap_memfree(ld->ld_options.ldo_peer);
423                 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
424                 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
425                 return ( 0 );
426         }
427 #endif
428         if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
429                 tv = ld->ld_options.ldo_tm_net;
430                 opt_tv = &tv;
431         }
432
433         osip_debug(ld, "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
434                         s, opt_tv ? tv.tv_sec : -1L, async);
435
436         if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
437                 return ( -1 );
438
439         if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
440                 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
441                         return ( -1 );
442                 return ( 0 );
443         }
444
445         err = sock_errno();
446         if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
447                 return ( -1 );
448         }
449         
450         if ( async ) {
451                 /* caller will call ldap_int_poll() as appropriate? */
452                 return ( -2 );
453         }
454
455         rc = ldap_int_poll( ld, s, opt_tv );
456
457         osip_debug(ld, "ldap_pvt_connect: %d\n", rc, 0, 0);
458
459         return rc;
460 }
461
462 #ifndef HAVE_INET_ATON
463 int
464 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
465 {
466         unsigned long u = inet_addr( host );
467
468 #ifdef INADDR_NONE
469         if ( u == INADDR_NONE ) return 0;
470 #endif
471         if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
472
473         in->s_addr = u;
474         return 1;
475 }
476 #endif
477
478 int
479 ldap_int_connect_cbs(LDAP *ld, Sockbuf *sb, ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr)
480 {
481         struct ldapoptions *lo;
482         ldaplist *ll;
483         ldap_conncb *cb;
484         int rc;
485
486         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, s );
487
488         /* Invoke all handle-specific callbacks first */
489         lo = &ld->ld_options;
490         for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
491                 cb = ll->ll_data;
492                 rc = cb->lc_add( ld, sb, srv, addr, cb );
493                 /* on any failure, call the teardown functions for anything
494                  * that previously succeeded
495                  */
496                 if ( rc ) {
497                         ldaplist *l2;
498                         for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
499                                 cb = l2->ll_data;
500                                 cb->lc_del( ld, sb, cb );
501                         }
502                         /* a failure might have implicitly closed the fd */
503                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
504                         return rc;
505                 }
506         }
507         lo = LDAP_INT_GLOBAL_OPT();
508         for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
509                 cb = ll->ll_data;
510                 rc = cb->lc_add( ld, sb, srv, addr, cb );
511                 if ( rc ) {
512                         ldaplist *l2;
513                         for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
514                                 cb = l2->ll_data;
515                                 cb->lc_del( ld, sb, cb );
516                         }
517                         lo = &ld->ld_options;
518                         for (l2 = lo->ldo_conn_cbs; l2; l2 = l2->ll_next) {
519                                 cb = l2->ll_data;
520                                 cb->lc_del( ld, sb, cb );
521                         }
522                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
523                         return rc;
524                 }
525         }
526         return 0;
527 }
528
529 int
530 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
531         int proto, LDAPURLDesc *srv,
532         int async )
533 {
534         int     rc;
535         int     socktype, port;
536         ber_socket_t            s = AC_SOCKET_INVALID;
537         char *host;
538
539 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
540         char serv[7];
541         int err;
542         struct addrinfo hints, *res, *sai;
543 #else
544         int i;
545         int use_hp = 0;
546         struct hostent *hp = NULL;
547         struct hostent he_buf;
548         struct in_addr in;
549         char *ha_buf=NULL;
550 #endif
551
552         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
553                 host = "localhost";
554         } else {
555                 host = srv->lud_host;
556         }
557
558         port = srv->lud_port;
559
560         if( !port ) {
561                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
562                         port = LDAPS_PORT;
563                 } else {
564                         port = LDAP_PORT;
565                 }
566         }
567
568         switch(proto) {
569         case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
570                 osip_debug( ld,
571                         "ldap_connect_to_host: TCP %s:%d\n",
572                         host, port, 0);
573                 break;
574         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
575                 osip_debug( ld,
576                         "ldap_connect_to_host: UDP %s:%d\n",
577                         host, port, 0);
578                 break;
579         default:
580                 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
581                         proto, 0, 0 );
582                 return -1;
583         }
584
585 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
586         memset( &hints, '\0', sizeof(hints) );
587 #ifdef USE_AI_ADDRCONFIG /* FIXME: configure test needed */
588         /* Use AI_ADDRCONFIG only on systems where its known to be needed. */
589         hints.ai_flags = AI_ADDRCONFIG;
590 #endif
591         hints.ai_family = ldap_int_inet4or6;
592         hints.ai_socktype = socktype;
593         snprintf(serv, sizeof serv, "%d", port );
594
595         /* most getaddrinfo(3) use non-threadsafe resolver libraries */
596         LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
597
598         err = getaddrinfo( host, serv, &hints, &res );
599
600         LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
601
602         if ( err != 0 ) {
603                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
604                         AC_GAI_STRERROR(err), 0, 0);
605                 return -1;
606         }
607         rc = -1;
608
609         for( sai=res; sai != NULL; sai=sai->ai_next) {
610                 if( sai->ai_addr == NULL ) {
611                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
612                                 "ai_addr is NULL?\n", 0, 0, 0);
613                         continue;
614                 }
615
616                 /* we assume AF_x and PF_x are equal for all x */
617                 s = ldap_int_socket( ld, sai->ai_family, socktype );
618                 if ( s == AC_SOCKET_INVALID ) {
619                         continue;
620                 }
621
622                 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
623                         ldap_pvt_close_socket(ld, s);
624                         break;
625                 }
626
627                 switch (sai->ai_family) {
628 #ifdef LDAP_PF_INET6
629                         case AF_INET6: {
630                                 char addr[INET6_ADDRSTRLEN];
631                                 inet_ntop( AF_INET6,
632                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
633                                         addr, sizeof addr);
634                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
635                                         addr, serv, 0);
636                         } break;
637 #endif
638                         case AF_INET: {
639                                 char addr[INET_ADDRSTRLEN];
640                                 inet_ntop( AF_INET,
641                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
642                                         addr, sizeof addr);
643                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
644                                         addr, serv, 0);
645                         } break;
646                 }
647
648                 rc = ldap_pvt_connect( ld, s,
649                         sai->ai_addr, sai->ai_addrlen, async );
650                 if ( rc == 0 || rc == -2 ) {
651                         err = ldap_int_connect_cbs( ld, sb, &s, srv, sai->ai_addr );
652                         if ( err )
653                                 rc = err;
654                         else
655                                 break;
656                 }
657                 ldap_pvt_close_socket(ld, s);
658         }
659         freeaddrinfo(res);
660
661 #else
662         if (! inet_aton( host, &in ) ) {
663                 int local_h_errno;
664                 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
665                         &hp, &local_h_errno );
666
667                 if ( (rc < 0) || (hp == NULL) ) {
668 #ifdef HAVE_WINSOCK
669                         ldap_pvt_set_errno( WSAGetLastError() );
670 #else
671                         /* not exactly right, but... */
672                         ldap_pvt_set_errno( EHOSTUNREACH );
673 #endif
674                         if (ha_buf) LDAP_FREE(ha_buf);
675                         return -1;
676                 }
677
678                 use_hp = 1;
679         }
680
681         rc = s = -1;
682         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
683                 struct sockaddr_in      sin;
684
685                 s = ldap_int_socket( ld, PF_INET, socktype );
686                 if ( s == AC_SOCKET_INVALID ) {
687                         /* use_hp ? continue : break; */
688                         break;
689                 }
690            
691                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
692                         ldap_pvt_close_socket(ld, s);
693                         break;
694                 }
695
696                 (void)memset((char *)&sin, '\0', sizeof sin);
697                 sin.sin_family = AF_INET;
698                 sin.sin_port = htons((unsigned short) port);
699
700                 if( use_hp ) {
701                         AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
702                                 sizeof(sin.sin_addr) );
703                 } else {
704                         AC_MEMCPY( &sin.sin_addr, &in.s_addr,
705                                 sizeof(sin.sin_addr) );
706                 }
707
708 #ifdef HAVE_INET_NTOA_B
709                 {
710                         /* for VxWorks */
711                         char address[INET_ADDR_LEN];
712                         inet_ntoa_b(sin.sin_address, address);
713                         osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
714                                 address, port, 0);
715                 }
716 #else
717                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
718                         inet_ntoa(sin.sin_addr), port, 0);
719 #endif
720
721                 rc = ldap_pvt_connect(ld, s,
722                         (struct sockaddr *)&sin, sizeof(sin),
723                         async);
724    
725                 if ( (rc == 0) || (rc == -2) ) {
726                         int err = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&sin );
727                         if ( err )
728                                 rc = err;
729                         else
730                                 break;
731                 }
732
733                 ldap_pvt_close_socket(ld, s);
734
735                 if (!use_hp) break;
736         }
737         if (ha_buf) LDAP_FREE(ha_buf);
738 #endif
739
740         return rc;
741 }
742
743 #if defined( HAVE_CYRUS_SASL )
744 char *
745 ldap_host_connected_to( Sockbuf *sb, const char *host )
746 {
747         ber_socklen_t   len;
748 #ifdef LDAP_PF_INET6
749         struct sockaddr_storage sabuf;
750 #else
751         struct sockaddr sabuf;
752 #endif
753         struct sockaddr *sa = (struct sockaddr *) &sabuf;
754         ber_socket_t    sd;
755
756         (void)memset( (char *)sa, '\0', sizeof sabuf );
757         len = sizeof sabuf;
758
759         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
760         if ( getpeername( sd, sa, &len ) == -1 ) {
761                 return( NULL );
762         }
763
764         /*
765          * do a reverse lookup on the addr to get the official hostname.
766          * this is necessary for kerberos to work right, since the official
767          * hostname is used as the kerberos instance.
768          */
769
770         switch (sa->sa_family) {
771 #ifdef LDAP_PF_LOCAL
772         case AF_LOCAL:
773                 return LDAP_STRDUP( ldap_int_hostname );
774 #endif
775 #ifdef LDAP_PF_INET6
776         case AF_INET6:
777                 {
778                         struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
779                         if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
780                                 &localhost, sizeof(localhost)) == 0 )
781                         {
782                                 return LDAP_STRDUP( ldap_int_hostname );
783                         }
784                 }
785                 break;
786 #endif
787         case AF_INET:
788                 {
789                         struct in_addr localhost;
790                         localhost.s_addr = htonl( INADDR_ANY );
791
792                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
793                                 &localhost, sizeof(localhost) ) == 0 )
794                         {
795                                 return LDAP_STRDUP( ldap_int_hostname );
796                         }
797
798 #ifdef INADDR_LOOPBACK
799                         localhost.s_addr = htonl( INADDR_LOOPBACK );
800
801                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
802                                 &localhost, sizeof(localhost) ) == 0 )
803                         {
804                                 return LDAP_STRDUP( ldap_int_hostname );
805                         }
806 #endif
807                 }
808                 break;
809
810         default:
811                 return( NULL );
812                 break;
813         }
814
815         {
816                 char *herr;
817 #ifdef NI_MAXHOST
818                 char hbuf[NI_MAXHOST];
819 #elif defined( MAXHOSTNAMELEN )
820                 char hbuf[MAXHOSTNAMELEN];
821 #else
822                 char hbuf[256];
823 #endif
824                 hbuf[0] = 0;
825
826                 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
827                         && hbuf[0] ) 
828                 {
829                         return LDAP_STRDUP( hbuf );   
830                 }
831         }
832
833         return host ? LDAP_STRDUP( host ) : NULL;
834 }
835 #endif
836
837
838 struct selectinfo {
839 #ifdef HAVE_POLL
840         /* for UNIX poll(2) */
841         int si_maxfd;
842         struct pollfd si_fds[FD_SETSIZE];
843 #else
844         /* for UNIX select(2) */
845         fd_set  si_readfds;
846         fd_set  si_writefds;
847         fd_set  si_use_readfds;
848         fd_set  si_use_writefds;
849 #endif
850 };
851
852 void
853 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
854 {
855         struct selectinfo       *sip;
856         ber_socket_t            sd;
857
858         sip = (struct selectinfo *)ld->ld_selectinfo;
859         
860         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
861
862 #ifdef HAVE_POLL
863         /* for UNIX poll(2) */
864         {
865                 int empty=-1;
866                 int i;
867                 for(i=0; i < sip->si_maxfd; i++) {
868                         if( sip->si_fds[i].fd == sd ) {
869                                 sip->si_fds[i].events |= POLL_WRITE;
870                                 return;
871                         }
872                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
873                                 empty=i;
874                         }
875                 }
876
877                 if( empty == -1 ) {
878                         if( sip->si_maxfd >= FD_SETSIZE ) {
879                                 /* FIXME */
880                                 return;
881                         }
882                         empty = sip->si_maxfd++;
883                 }
884
885                 sip->si_fds[empty].fd = sd;
886                 sip->si_fds[empty].events = POLL_WRITE;
887         }
888 #else
889         /* for UNIX select(2) */
890         if ( !FD_ISSET( sd, &sip->si_writefds )) {
891                 FD_SET( sd, &sip->si_writefds );
892         }
893 #endif
894 }
895
896
897 void
898 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
899 {
900         struct selectinfo       *sip;
901         ber_socket_t            sd;
902
903         sip = (struct selectinfo *)ld->ld_selectinfo;
904
905         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
906
907 #ifdef HAVE_POLL
908         /* for UNIX poll(2) */
909         {
910                 int empty=-1;
911                 int i;
912                 for(i=0; i < sip->si_maxfd; i++) {
913                         if( sip->si_fds[i].fd == sd ) {
914                                 sip->si_fds[i].events |= POLL_READ;
915                                 return;
916                         }
917                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
918                                 empty=i;
919                         }
920                 }
921
922                 if( empty == -1 ) {
923                         if( sip->si_maxfd >= FD_SETSIZE ) {
924                                 /* FIXME */
925                                 return;
926                         }
927                         empty = sip->si_maxfd++;
928                 }
929
930                 sip->si_fds[empty].fd = sd;
931                 sip->si_fds[empty].events = POLL_READ;
932         }
933 #else
934         /* for UNIX select(2) */
935         if ( !FD_ISSET( sd, &sip->si_readfds )) {
936                 FD_SET( sd, &sip->si_readfds );
937         }
938 #endif
939 }
940
941
942 void
943 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
944 {
945         struct selectinfo       *sip;
946         ber_socket_t            sd;
947
948         sip = (struct selectinfo *)ld->ld_selectinfo;
949
950         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
951
952 #ifdef HAVE_POLL
953         /* for UNIX poll(2) */
954         {
955                 int i;
956                 for(i=0; i < sip->si_maxfd; i++) {
957                         if( sip->si_fds[i].fd == sd ) {
958                                 sip->si_fds[i].fd = -1;
959                         }
960                 }
961         }
962 #else
963         /* for UNIX select(2) */
964         FD_CLR( sd, &sip->si_writefds );
965         FD_CLR( sd, &sip->si_readfds );
966 #endif
967 }
968
969
970 int
971 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
972 {
973         struct selectinfo       *sip;
974         ber_socket_t            sd;
975
976         sip = (struct selectinfo *)ld->ld_selectinfo;
977
978         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
979
980 #ifdef HAVE_POLL
981         /* for UNIX poll(2) */
982         {
983                 int i;
984                 for(i=0; i < sip->si_maxfd; i++) {
985                         if( sip->si_fds[i].fd == sd ) {
986                                 return sip->si_fds[i].revents & POLL_WRITE;
987                         }
988                 }
989
990                 return 0;
991         }
992 #else
993         /* for UNIX select(2) */
994         return( FD_ISSET( sd, &sip->si_use_writefds ));
995 #endif
996 }
997
998
999 int
1000 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
1001 {
1002         struct selectinfo       *sip;
1003         ber_socket_t            sd;
1004
1005         sip = (struct selectinfo *)ld->ld_selectinfo;
1006
1007         if (ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ))
1008                 return 1;
1009
1010         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1011
1012 #ifdef HAVE_POLL
1013         /* for UNIX poll(2) */
1014         {
1015                 int i;
1016                 for(i=0; i < sip->si_maxfd; i++) {
1017                         if( sip->si_fds[i].fd == sd ) {
1018                                 return sip->si_fds[i].revents & POLL_READ;
1019                         }
1020                 }
1021
1022                 return 0;
1023         }
1024 #else
1025         /* for UNIX select(2) */
1026         return( FD_ISSET( sd, &sip->si_use_readfds ));
1027 #endif
1028 }
1029
1030
1031 void *
1032 ldap_new_select_info( void )
1033 {
1034         struct selectinfo       *sip;
1035
1036         sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
1037
1038         if ( sip == NULL ) return NULL;
1039
1040 #ifdef HAVE_POLL
1041         /* for UNIX poll(2) */
1042         /* sip->si_maxfd=0 */
1043 #else
1044         /* for UNIX select(2) */
1045         FD_ZERO( &sip->si_readfds );
1046         FD_ZERO( &sip->si_writefds );
1047 #endif
1048
1049         return( (void *)sip );
1050 }
1051
1052
1053 void
1054 ldap_free_select_info( void *sip )
1055 {
1056         LDAP_FREE( sip );
1057 }
1058
1059
1060 #ifndef HAVE_POLL
1061 int ldap_int_tblsize = 0;
1062
1063 void
1064 ldap_int_ip_init( void )
1065 {
1066 #if defined( HAVE_SYSCONF )
1067         long tblsize = sysconf( _SC_OPEN_MAX );
1068         if( tblsize > INT_MAX ) tblsize = INT_MAX;
1069
1070 #elif defined( HAVE_GETDTABLESIZE )
1071         int tblsize = getdtablesize();
1072 #else
1073         int tblsize = FD_SETSIZE;
1074 #endif /* !USE_SYSCONF */
1075
1076 #ifdef FD_SETSIZE
1077         if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1078 #endif  /* FD_SETSIZE */
1079
1080         ldap_int_tblsize = tblsize;
1081 }
1082 #endif
1083
1084
1085 int
1086 ldap_int_select( LDAP *ld, struct timeval *timeout )
1087 {
1088         int rc;
1089         struct selectinfo       *sip;
1090
1091         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1092
1093 #ifndef HAVE_POLL
1094         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1095 #endif
1096
1097         sip = (struct selectinfo *)ld->ld_selectinfo;
1098         assert( sip != NULL );
1099
1100 #ifdef HAVE_POLL
1101         {
1102                 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1103                 rc = poll( sip->si_fds, sip->si_maxfd, to );
1104         }
1105 #else
1106         sip->si_use_readfds = sip->si_readfds;
1107         sip->si_use_writefds = sip->si_writefds;
1108         
1109         rc = select( ldap_int_tblsize,
1110                 &sip->si_use_readfds, &sip->si_use_writefds,
1111                 NULL, timeout );
1112 #endif
1113
1114         return rc;
1115 }