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