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