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