1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1995 Regents of the University of Michigan.
* All rights reserved.
*/
/*
* os-ip.c -- platform-specific TCP & UDP related code
*/
#if 0
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#endif
/*
* On platforms where poll() does not exist, we use select().
* Therefore, we should increase the number of file descriptors
* we can use by #defining FD_SETSIZE to a large number before
* we include <sys/select.h> or its equivalent. We do not need
* to do this for Windows, because on that platform there is no
* relationship between FD_SETSIZE and the highest numbered file
* descriptor we can use. See the document fdsetsize.txt in
* this directory for a description of the setting of FD_SETSIZE
* for the OS'es we care about.
*/
#ifndef NSLDAPI_HAVE_POLL
/* XXX value for BSDI? */
/* XXX value for macintosh (if applicable)? */
#endif
#include "ldap-int.h"
#ifdef NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED
#include <signal.h>
#endif
#ifdef NSLDAPI_HAVE_POLL
#include <poll.h>
#endif
#ifdef _WINDOWS
#define NSLDAPI_INVALID_OS_SOCKET( s ) ((s) == INVALID_SOCKET)
#else
#define NSLDAPI_INVALID_OS_SOCKET( s ) ((s) < 0 )
#endif
#define NSLDAPI_POLL_ARRAY_GROWTH 5 /* grow arrays 5 elements at a time */
/*
* Structures and union for tracking status of network sockets
*/
#ifdef NSLDAPI_HAVE_POLL
struct nsldapi_os_statusinfo { /* used with native OS poll() */
struct pollfd *ossi_pollfds;
int ossi_pollfds_size;
};
#else /* NSLDAPI_HAVE_POLL */
struct nsldapi_os_statusinfo { /* used with native OS select() */
fd_set ossi_readfds;
fd_set ossi_writefds;
fd_set ossi_use_readfds;
fd_set ossi_use_writefds;
};
#endif /* else NSLDAPI_HAVE_POLL */
struct nsldapi_cb_statusinfo { /* used with ext. I/O poll() callback */
LDAP_X_PollFD *cbsi_pollfds;
int cbsi_pollfds_size;
};
/*
* NSLDAPI_CB_POLL_MATCH() evaluates to non-zero (true) if the Sockbuf *sdp
* matches the LDAP_X_PollFD pollfd.
*/
#ifdef _WINDOWS
#define NSLDAPI_CB_POLL_SD_CAST (unsigned int)
#else
#define NSLDAPI_CB_POLL_SD_CAST
#endif
#define NSLDAPI_CB_POLL_MATCH( sbp, pollfd ) \
((sbp)->sb_sd == NSLDAPI_CB_POLL_SD_CAST ((pollfd).lpoll_fd) && \
(sbp)->sb_ext_io_fns.lbextiofn_socket_arg == (pollfd).lpoll_socketarg)
struct nsldapi_iostatus_info {
int ios_type;
#define NSLDAPI_IOSTATUS_TYPE_OSNATIVE 1 /* poll() or select() */
#define NSLDAPI_IOSTATUS_TYPE_CALLBACK 2 /* poll()-like */
int ios_read_count;
int ios_write_count;
union {
struct nsldapi_os_statusinfo ios_osinfo;
struct nsldapi_cb_statusinfo ios_cbinfo;
} ios_status;
};
#ifndef NSLDAPI_AVOID_OS_SOCKETS
#ifdef NSLDAPI_HAVE_POLL
static int nsldapi_add_to_os_pollfds( int fd,
struct nsldapi_os_statusinfo *pip, short events );
static int nsldapi_clear_from_os_pollfds( int fd,
struct nsldapi_os_statusinfo *pip, short events );
static int nsldapi_find_in_os_pollfds( int fd,
struct nsldapi_os_statusinfo *pip, short revents );
#endif /* NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
static int nsldapi_iostatus_init_nolock( LDAP *ld );
static int nsldapi_add_to_cb_pollfds( Sockbuf *sb,
struct nsldapi_cb_statusinfo *pip, short events );
static int nsldapi_clear_from_cb_pollfds( Sockbuf *sb,
struct nsldapi_cb_statusinfo *pip, short events );
static int nsldapi_find_in_cb_pollfds( Sockbuf *sb,
struct nsldapi_cb_statusinfo *pip, short revents );
#ifdef irix
#ifndef _PR_THREADS
/*
* XXXmcs: on IRIX NSPR's poll() and select() wrappers will crash if NSPR
* has not been initialized. We work around the problem by bypassing
* the NSPR wrapper functions and going directly to the OS' functions.
*/
#define NSLDAPI_POLL _poll
#define NSLDAPI_SELECT _select
extern int _poll(struct pollfd *fds, unsigned long nfds, int timeout);
extern int _select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
#else /* _PR_THREADS */
#define NSLDAPI_POLL poll
#define NSLDAPI_SELECT select
#endif /* else _PR_THREADS */
#else /* irix */
#define NSLDAPI_POLL poll
#define NSLDAPI_SELECT select
#endif /* else irix */
static LBER_SOCKET nsldapi_os_socket( LDAP *ld, int secure, int domain,
int type, int protocol );
static int nsldapi_os_ioctl( LBER_SOCKET s, int option, int *statusp );
static int nsldapi_os_connect_with_to( LBER_SOCKET s, struct sockaddr *name,
int namelen, int msec_timeout );
#if defined(KERBEROS)
char * nsldapi_host_connected_to( LDAP *ld, Sockbuf *sb );
#endif
/*
* Function typedefs used by nsldapi_try_each_host()
*/
typedef LBER_SOCKET (NSLDAPI_SOCKET_FN)( LDAP *ld, int secure, int domain,
int type, int protocol );
typedef int (NSLDAPI_IOCTL_FN)( LBER_SOCKET s, int option, int *statusp );
typedef int (NSLDAPI_CONNECT_WITH_TO_FN )( LBER_SOCKET s, struct sockaddr *name,
int namelen, int msec_timeout );
typedef int (NSLDAPI_CONNECT_FN )( LBER_SOCKET s, struct sockaddr *name,
int namelen );
typedef int (NSLDAPI_CLOSE_FN )( LBER_SOCKET s );
static int nsldapi_try_each_host( LDAP *ld, const char *hostlist, int defport,
int secure, NSLDAPI_SOCKET_FN *socketfn, NSLDAPI_IOCTL_FN *ioctlfn,
NSLDAPI_CONNECT_WITH_TO_FN *connectwithtofn,
NSLDAPI_CONNECT_FN *connectfn, NSLDAPI_CLOSE_FN *closefn );
static int
nsldapi_os_closesocket( LBER_SOCKET s )
{
int rc;
#ifdef NSLDAPI_AVOID_OS_SOCKETS
rc = -1;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef _WINDOWS
rc = closesocket( s );
#else /* _WINDOWS */
rc = close( s );
#endif /* _WINDOWS */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
return( rc );
}
static LBER_SOCKET
nsldapi_os_socket( LDAP *ld, int secure, int domain, int type, int protocol )
{
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return -1;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
int s, invalid_socket;
char *errmsg = NULL;
if ( secure ) {
LDAP_SET_LDERRNO( ld, LDAP_LOCAL_ERROR, NULL,
nsldapi_strdup( "secure mode not supported" ));
return( -1 );
}
s = socket( domain, type, protocol );
/*
* if the socket() call failed or it returned a socket larger
* than we can deal with, return a "local error."
*/
if ( NSLDAPI_INVALID_OS_SOCKET( s )) {
errmsg = "unable to create a socket";
invalid_socket = 1;
} else { /* valid socket -- check for overflow */
invalid_socket = 0;
#if !defined(NSLDAPI_HAVE_POLL) && !defined(_WINDOWS)
/* not on Windows and do not have poll() */
if ( s >= FD_SETSIZE ) {
errmsg = "can't use socket >= FD_SETSIZE";
}
#endif
}
if ( errmsg != NULL ) { /* local socket error */
if ( !invalid_socket ) {
nsldapi_os_closesocket( s );
}
errmsg = nsldapi_strdup( errmsg );
LDAP_SET_LDERRNO( ld, LDAP_LOCAL_ERROR, NULL, errmsg );
return( -1 );
}
return( s );
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
}
/*
* Non-blocking connect call function
*/
static int
nsldapi_os_connect_with_to(LBER_SOCKET sockfd, struct sockaddr *saptr,
int salen, int msec)
{
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return -1;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
int n, error;
int len;
#if defined(_WINDOWS) || defined(XP_OS2)
int nonblock = 1;
int block = 0;
#else
int flags;
#endif /* _WINDOWS */
#ifdef NSLDAPI_HAVE_POLL
struct pollfd pfd;
#else
struct timeval tval;
fd_set rset, wset;
#ifdef _WINDOWS
fd_set eset;
#endif
#endif /* NSLDAPI_HAVE_POLL */
LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_connect_nonblock timeout: %d (msec)\n",
msec, 0, 0);
#ifdef _WINDOWS
ioctlsocket(sockfd, FIONBIO, &nonblock);
#elif defined(XP_OS2)
ioctl( sockfd, FIONBIO, &nonblock, sizeof(nonblock) );
#else
flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
#endif /* _WINDOWS */
error = 0;
if ((n = connect(sockfd, saptr, salen)) < 0)
#ifdef _WINDOWS
if ((n != SOCKET_ERROR) && (WSAGetLastError() != WSAEWOULDBLOCK)) {
#else
if (errno != EINPROGRESS) {
#endif /* _WINDOWS */
#ifdef LDAP_DEBUG
if ( ldap_debug & LDAP_DEBUG_TRACE ) {
perror("connect");
}
#endif
return (-1);
}
/* success */
if (n == 0)
goto done;
#ifdef NSLDAPI_HAVE_POLL
pfd.fd = sockfd;
pfd.events = POLLOUT;
#else
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
wset = rset;
#ifdef _WINDOWS
eset = rset;
#endif /* _WINDOWS */
#endif /* NSLDAPI_HAVE_POLL */
if (msec < 0 && msec != LDAP_X_IO_TIMEOUT_NO_TIMEOUT) {
LDAPDebug( LDAP_DEBUG_TRACE, "Invalid timeout value detected.."
"resetting connect timeout to default value "
"(LDAP_X_IO_TIMEOUT_NO_TIMEOUT\n", 0, 0, 0);
msec = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
#ifndef NSLDAPI_HAVE_POLL
} else {
if (msec != 0) {
tval.tv_sec = msec / 1000;
tval.tv_usec = 1000 * ( msec % 1000 );
} else {
tval.tv_sec = 0;
tval.tv_usec = 0;
}
#endif /* NSLDAPI_HAVE_POLL */
}
#ifdef NSLDAPI_HAVE_POLL
if ((n = poll(&pfd, 1,
(msec != LDAP_X_IO_TIMEOUT_NO_TIMEOUT) ? msec : -1)) == 0) {
errno = ETIMEDOUT;
return (-1);
}
if (pfd.revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char *)&error, &len)
< 0)
return (-1);
#ifdef LDAP_DEBUG
} else if ( ldap_debug & LDAP_DEBUG_TRACE ) {
perror("poll error: sockfd not set");
#endif
}
#else /* NSLDAPI_HAVE_POLL */
/* if timeval structure == NULL, select will block indefinitely */
/* != NULL, and value == 0, select will */
/* not block */
/* Windows is a bit quirky on how it behaves w.r.t nonblocking */
/* connects. If the connect fails, the exception fd, eset, is */
/* set to show the failure. The first argument in select is */
/* ignored */
#ifdef _WINDOWS
if ((n = select(sockfd +1, &rset, &wset, &eset,
(msec != LDAP_X_IO_TIMEOUT_NO_TIMEOUT) ? &tval : NULL)) == 0) {
errno = WSAETIMEDOUT;
return (-1);
}
/* if wset is set, the connect worked */
if (FD_ISSET(sockfd, &wset) || FD_ISSET(sockfd, &rset)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char *)&error, &len)
< 0)
return (-1);
goto done;
}
/* if eset is set, the connect failed */
if (FD_ISSET(sockfd, &eset)) {
return (-1);
}
/* failure on select call */
if (n == SOCKET_ERROR) {
perror("select error: SOCKET_ERROR returned");
return (-1);
}
#else
if ((n = select(sockfd +1, &rset, &wset, NULL,
(msec != LDAP_X_IO_TIMEOUT_NO_TIMEOUT) ? &tval : NULL)) == 0) {
errno = ETIMEDOUT;
return (-1);
}
if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char *)&error, &len)
< 0)
return (-1);
#ifdef LDAP_DEBUG
} else if ( ldap_debug & LDAP_DEBUG_TRACE ) {
perror("select error: sockfd not set");
#endif
}
#endif /* _WINDOWS */
#endif /* NSLDAPI_HAVE_POLL */
done:
#ifdef _WINDOWS
ioctlsocket(sockfd, FIONBIO, &block);
#elif defined(XP_OS2)
ioctl( sockfd, FIONBIO, &nonblock, sizeof(block) );
#else
fcntl(sockfd, F_SETFL, flags);
#endif /* _WINDOWS */
if (error) {
errno = error;
return (-1);
}
return (0);
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
}
static int
nsldapi_os_ioctl( LBER_SOCKET s, int option, int *statusp )
{
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return -1;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
int err;
#if defined(_WINDOWS) || defined(XP_OS2)
u_long iostatus;
#endif
if ( FIONBIO != option ) {
return( -1 );
}
#ifdef _WINDOWS
iostatus = *(u_long *)statusp;
err = ioctlsocket( s, FIONBIO, &iostatus );
#else
#ifdef XP_OS2
err = ioctl( s, FIONBIO, (caddr_t)&iostatus, sizeof(iostatus) );
#else
err = ioctl( s, FIONBIO, (caddr_t)statusp );
#endif
#endif
return( err );
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
}
int
nsldapi_connect_to_host( LDAP *ld, Sockbuf *sb, const char *hostlist,
int defport, int secure, char **krbinstancep )
/*
* "defport" must be in host byte order
* zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
* if -1 is returned, ld_errno is set
*/
{
int s;
LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_connect_to_host: %s, port: %d\n",
NULL == hostlist ? "NULL" : hostlist, defport, 0 );
/*
* If an extended I/O connect callback has been defined, just use it.
*/
if ( NULL != ld->ld_extconnect_fn ) {
unsigned long connect_opts = 0;
if ( ld->ld_options & LDAP_BITOPT_ASYNC) {
connect_opts |= LDAP_X_EXTIOF_OPT_NONBLOCKING;
}
if ( secure ) {
connect_opts |= LDAP_X_EXTIOF_OPT_SECURE;
}
s = ld->ld_extconnect_fn( hostlist, defport,
ld->ld_connect_timeout, connect_opts,
ld->ld_ext_session_arg,
&sb->sb_ext_io_fns.lbextiofn_socket_arg );
} else {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return( -1 );
#else /* NSLDAPI_AVOID_OS_SOCKETS */
s = nsldapi_try_each_host( ld, hostlist,
defport, secure, nsldapi_os_socket,
nsldapi_os_ioctl, nsldapi_os_connect_with_to,
NULL, nsldapi_os_closesocket );
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
}
if ( s < 0 ) {
LDAP_SET_LDERRNO( ld, LDAP_CONNECT_ERROR, NULL, NULL );
return( -1 );
}
sb->sb_sd = s;
/*
* Set krbinstancep (canonical name of host for use by Kerberos).
*/
#ifdef KERBEROS
char *p;
if (( *krbinstancep = nsldapi_host_connected_to( sb )) != NULL
&& ( p = strchr( *krbinstancep, '.' )) != NULL ) {
*p = '\0';
}
#else /* KERBEROS */
*krbinstancep = NULL;
#endif /* KERBEROS */
return( 0 );
}
/*
* Returns a socket number if successful and -1 if an error occurs.
*/
static int
nsldapi_try_each_host( LDAP *ld, const char *hostlist,
int defport, int secure, NSLDAPI_SOCKET_FN *socketfn,
NSLDAPI_IOCTL_FN *ioctlfn, NSLDAPI_CONNECT_WITH_TO_FN *connectwithtofn,
NSLDAPI_CONNECT_FN *connectfn, NSLDAPI_CLOSE_FN *closefn )
{
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return -1;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
int rc = -1;
int s = 0;
int i, err, connected, use_hp;
int parse_err, port;
struct sockaddr_in sin;
nsldapi_in_addr_t address;
char **addrlist, *ldhpbuf, *ldhpbuf_allocd = NULL;
char *host;
LDAPHostEnt ldhent, *ldhp;
struct hostent *hp;
struct ldap_x_hostlist_status *status;
#ifdef GETHOSTBYNAME_BUF_T
GETHOSTBYNAME_BUF_T hbuf;
struct hostent hent;
#endif /* GETHOSTBYNAME_BUF_T */
connected = 0;
parse_err = ldap_x_hostlist_first( hostlist, defport, &host, &port,
&status );
while ( !connected && LDAP_SUCCESS == parse_err && host != NULL ) {
ldhpbuf_allocd = NULL;
ldhp = NULL;
hp = NULL;
s = 0;
use_hp = 0;
addrlist = NULL;
if (( address = inet_addr( host )) == -1 ) {
if ( ld->ld_dns_gethostbyname_fn == NULL ) {
#ifdef GETHOSTBYNAME_R_RETURNS_INT
(void)GETHOSTBYNAME( host, &hent, hbuf,
sizeof(hbuf), &hp, &err );
#else
hp = GETHOSTBYNAME( host, &hent, hbuf,
sizeof(hbuf), &err );
#endif
if ( hp != NULL ) {
addrlist = hp->h_addr_list;
}
} else {
/*
* DNS callback installed... use it.
*/
#ifdef GETHOSTBYNAME_buf_t
/* avoid allocation by using hbuf if large enough */
if ( sizeof( hbuf ) < ld->ld_dns_bufsize ) {
ldhpbuf = ldhpbuf_allocd
= NSLDAPI_MALLOC( ld->ld_dns_bufsize );
} else {
ldhpbuf = (char *)hbuf;
}
#else /* GETHOSTBYNAME_buf_t */
ldhpbuf = ldhpbuf_allocd = NSLDAPI_MALLOC(
ld->ld_dns_bufsize );
#endif /* else GETHOSTBYNAME_buf_t */
if ( ldhpbuf == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY,
NULL, NULL );
ldap_memfree( host );
ldap_x_hostlist_statusfree( status );
return( -1 );
}
if (( ldhp = ld->ld_dns_gethostbyname_fn( host,
&ldhent, ldhpbuf, ld->ld_dns_bufsize, &err,
ld->ld_dns_extradata )) != NULL ) {
addrlist = ldhp->ldaphe_addr_list;
}
}
if ( addrlist == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_CONNECT_ERROR, NULL, NULL );
LDAP_SET_ERRNO( ld, EHOSTUNREACH ); /* close enough */
if ( ldhpbuf_allocd != NULL ) {
NSLDAPI_FREE( ldhpbuf_allocd );
}
ldap_memfree( host );
ldap_x_hostlist_statusfree( status );
return( -1 );
}
use_hp = 1;
}
rc = -1;
for ( i = 0; !use_hp || ( addrlist[ i ] != 0 ); i++ ) {
if ( -1 == ( s = (*socketfn)( ld, secure, AF_INET,
SOCK_STREAM, 0 ))) {
if ( ldhpbuf_allocd != NULL ) {
NSLDAPI_FREE( ldhpbuf_allocd );
}
ldap_memfree( host );
ldap_x_hostlist_statusfree( status );
return( -1 );
}
if ( ld->ld_options & LDAP_BITOPT_ASYNC ) {
int iostatus = 1;
err = (*ioctlfn)( s, FIONBIO, &iostatus );
if ( err == -1 ) {
LDAPDebug( LDAP_DEBUG_ANY,
"FIONBIO ioctl failed on %d\n",
s, 0, 0 );
}
}
(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)port );
SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
( use_hp ? (char *) addrlist[ i ] :
(char *) &address ), sizeof( sin.sin_addr.s_addr) );
{
#ifdef NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED
/*
* Block all of the signals that might interrupt connect() since
* there is an OS bug that causes connect() to fail if it is restarted.
* Look in ../../include/portable.h for the definition of
* NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED.
*/
sigset_t ints_off, oldset;
sigemptyset( &ints_off );
sigaddset( &ints_off, SIGALRM );
sigaddset( &ints_off, SIGIO );
sigaddset( &ints_off, SIGCLD );
NSLDAPI_MT_SAFE_SIGPROCMASK( SIG_BLOCK, &ints_off, &oldset );
#endif /* NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED */
if ( NULL != connectwithtofn ) {
err = (*connectwithtofn)(s,
(struct sockaddr *)&sin,
sizeof(struct sockaddr_in),
ld->ld_connect_timeout);
} else {
err = (*connectfn)(s,
(struct sockaddr *)&sin,
sizeof(struct sockaddr_in));
}
#ifdef NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED
/*
* restore original signal mask
*/
NSLDAPI_MT_SAFE_SIGPROCMASK( SIG_SETMASK, &oldset, 0 );
#endif /* NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED */
}
if ( err >= 0 ) {
connected = 1;
rc = 0;
break;
} else {
if ( ld->ld_options & LDAP_BITOPT_ASYNC) {
#ifdef _WINDOWS
if (err == -1 && WSAGetLastError() == WSAEWOULDBLOCK)
LDAP_SET_ERRNO( ld, EWOULDBLOCK );
#endif /* _WINDOWS */
err = LDAP_GET_ERRNO( ld );
if ( NSLDAPI_ERRNO_IO_INPROGRESS( err )) {
LDAPDebug( LDAP_DEBUG_TRACE, "connect would block...\n",
0, 0, 0 );
rc = -2;
break;
}
}
#ifdef LDAP_DEBUG
if ( ldap_debug & LDAP_DEBUG_TRACE ) {
perror( (char *)inet_ntoa( sin.sin_addr ));
}
#endif
(*closefn)( s );
if ( !use_hp ) {
break;
}
}
}
ldap_memfree( host );
parse_err = ldap_x_hostlist_next( &host, &port, status );
}
if ( ldhpbuf_allocd != NULL ) {
NSLDAPI_FREE( ldhpbuf_allocd );
}
ldap_memfree( host );
ldap_x_hostlist_statusfree( status );
if ( connected ) {
LDAPDebug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
s, inet_ntoa( sin.sin_addr ), 0 );
}
return( rc == 0 ? s : -1 );
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
}
void
nsldapi_close_connection( LDAP *ld, Sockbuf *sb )
{
if ( ld->ld_extclose_fn == NULL ) {
#ifndef NSLDAPI_AVOID_OS_SOCKETS
nsldapi_os_closesocket( sb->sb_sd );
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else {
ld->ld_extclose_fn( sb->sb_sd,
sb->sb_ext_io_fns.lbextiofn_socket_arg );
}
}
#ifdef KERBEROS
char *
nsldapi_host_connected_to( LDAP *ld, Sockbuf *sb )
{
struct hostent *hp;
char *p;
int len;
struct sockaddr_in sin;
(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
len = sizeof( sin );
if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
return( NULL );
}
/*
* do a reverse lookup on the addr to get the official hostname.
* this is necessary for kerberos to work right, since the official
* hostname is used as the kerberos instance.
*/
#error XXXmcs: need to use DNS callbacks here
if (( hp = (struct hostent *)gethostbyaddr( (char *) &sin.sin_addr,
sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
if ( hp->h_name != NULL ) {
return( nsldapi_strdup( hp->h_name ));
}
}
return( NULL );
}
#endif /* KERBEROS */
/*
* Returns 0 if all goes well and -1 if an error occurs (error code set in ld)
* Also allocates initializes ld->ld_iostatus if needed..
*/
int
nsldapi_iostatus_interest_write( LDAP *ld, Sockbuf *sb )
{
int rc = 0;
NSLDAPIIOStatus *iosp;
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
if ( ld->ld_iostatus == NULL
&& nsldapi_iostatus_init_nolock( ld ) < 0 ) {
rc = -1;
goto unlock_and_return;
}
iosp = ld->ld_iostatus;
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
rc = -1;
goto unlock_and_return;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef NSLDAPI_HAVE_POLL
if ( nsldapi_add_to_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, POLLOUT )) {
++iosp->ios_write_count;
}
#else /* NSLDAPI_HAVE_POLL */
if ( !FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_writefds )) {
FD_SET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_writefds );
++iosp->ios_write_count;
}
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
if ( nsldapi_add_to_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, LDAP_X_POLLOUT )) {
++iosp->ios_write_count;
}
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_interest_write: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
}
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
/*
* Returns 0 if all goes well and -1 if an error occurs (error code set in ld)
* Also allocates initializes ld->ld_iostatus if needed..
*/
int
nsldapi_iostatus_interest_read( LDAP *ld, Sockbuf *sb )
{
int rc = 0;
NSLDAPIIOStatus *iosp;
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
if ( ld->ld_iostatus == NULL
&& nsldapi_iostatus_init_nolock( ld ) < 0 ) {
rc = -1;
goto unlock_and_return;
}
iosp = ld->ld_iostatus;
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
rc = -1;
goto unlock_and_return;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef NSLDAPI_HAVE_POLL
if ( nsldapi_add_to_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, POLLIN )) {
++iosp->ios_read_count;
}
#else /* NSLDAPI_HAVE_POLL */
if ( !FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_readfds )) {
FD_SET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_readfds );
++iosp->ios_read_count;
}
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
if ( nsldapi_add_to_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, LDAP_X_POLLIN )) {
++iosp->ios_read_count;
}
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_interest_read: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
}
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
/*
* Returns 0 if all goes well and -1 if an error occurs (error code set in ld)
* Also allocates initializes ld->ld_iostatus if needed..
*/
int
nsldapi_iostatus_interest_clear( LDAP *ld, Sockbuf *sb )
{
int rc = 0;
NSLDAPIIOStatus *iosp;
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
if ( ld->ld_iostatus == NULL
&& nsldapi_iostatus_init_nolock( ld ) < 0 ) {
rc = -1;
goto unlock_and_return;
}
iosp = ld->ld_iostatus;
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
rc = -1;
goto unlock_and_return;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef NSLDAPI_HAVE_POLL
if ( nsldapi_clear_from_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, POLLOUT )) {
--iosp->ios_write_count;
}
if ( nsldapi_clear_from_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, POLLIN )) {
--iosp->ios_read_count;
}
#else /* NSLDAPI_HAVE_POLL */
if ( FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_writefds )) {
FD_CLR( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_writefds );
--iosp->ios_write_count;
}
if ( FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_readfds )) {
FD_CLR( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_readfds );
--iosp->ios_read_count;
}
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
if ( nsldapi_clear_from_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, LDAP_X_POLLOUT )) {
--iosp->ios_write_count;
}
if ( nsldapi_clear_from_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, LDAP_X_POLLIN )) {
--iosp->ios_read_count;
}
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_interest_clear: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
}
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
/*
* Return a non-zero value if sb is ready for write.
*/
int
nsldapi_iostatus_is_write_ready( LDAP *ld, Sockbuf *sb )
{
int rc = 0;
NSLDAPIIOStatus *iosp;
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
iosp = ld->ld_iostatus;
if ( iosp == NULL ) {
goto unlock_and_return;
}
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
goto unlock_and_return;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef NSLDAPI_HAVE_POLL
/*
* if we are using poll() we do something a little tricky: if
* any bits in the socket's returned events field other than
* POLLIN (ready for read) are set, we return true. This
* is done so we notice when a server closes a connection
* or when another error occurs. The actual error will be
* noticed later when we call write() or send().
*/
rc = nsldapi_find_in_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, ~POLLIN );
#else /* NSLDAPI_HAVE_POLL */
rc = FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_use_writefds );
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
rc = nsldapi_find_in_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, ~LDAP_X_POLLIN );
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_is_write_ready: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
rc = 0;
}
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
/*
* Return a non-zero value if sb is ready for read.
*/
int
nsldapi_iostatus_is_read_ready( LDAP *ld, Sockbuf *sb )
{
int rc = 0;
NSLDAPIIOStatus *iosp;
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
iosp = ld->ld_iostatus;
if ( iosp == NULL ) {
goto unlock_and_return;
}
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
goto unlock_and_return;
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifdef NSLDAPI_HAVE_POLL
/*
* if we are using poll() we do something a little tricky: if
* any bits in the socket's returned events field other than
* POLLOUT (ready for write) are set, we return true. This
* is done so we notice when a server closes a connection
* or when another error occurs. The actual error will be
* noticed later when we call read() or recv().
*/
rc = nsldapi_find_in_os_pollfds( sb->sb_sd,
&iosp->ios_status.ios_osinfo, ~POLLOUT );
#else /* NSLDAPI_HAVE_POLL */
rc = FD_ISSET( sb->sb_sd,
&iosp->ios_status.ios_osinfo.ossi_use_readfds );
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
rc = nsldapi_find_in_cb_pollfds( sb,
&iosp->ios_status.ios_cbinfo, ~LDAP_X_POLLOUT );
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_is_read_ready: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
rc = 0;
}
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
/*
* Allocate and initialize ld->ld_iostatus if not already done.
* Should be called with LDAP_IOSTATUS_LOCK locked.
* Returns 0 if all goes well and -1 if not (sets error in ld)
*/
static int
nsldapi_iostatus_init_nolock( LDAP *ld )
{
NSLDAPIIOStatus *iosp;
if ( ld->ld_iostatus != NULL ) {
return( 0 );
}
if (( iosp = (NSLDAPIIOStatus *)NSLDAPI_CALLOC( 1,
sizeof( NSLDAPIIOStatus ))) == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
return( -1 );
}
if ( ld->ld_extpoll_fn == NULL ) {
iosp->ios_type = NSLDAPI_IOSTATUS_TYPE_OSNATIVE;
#ifdef NSLDAPI_AVOID_OS_SOCKETS
return( -1 );
#else /* NSLDAPI_AVOID_OS_SOCKETS */
#ifndef NSLDAPI_HAVE_POLL
FD_ZERO( &iosp->ios_status.ios_osinfo.ossi_readfds );
FD_ZERO( &iosp->ios_status.ios_osinfo.ossi_writefds );
#endif /* !NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else {
iosp->ios_type = NSLDAPI_IOSTATUS_TYPE_CALLBACK;
}
ld->ld_iostatus = iosp;
return( 0 );
}
void
nsldapi_iostatus_free( LDAP *ld )
{
if ( ld == NULL ) {
return;
}
/* clean up classic I/O compatibility glue */
if ( ld->ld_io_fns_ptr != NULL ) {
if ( ld->ld_ext_session_arg != NULL ) {
NSLDAPI_FREE( ld->ld_ext_session_arg );
}
NSLDAPI_FREE( ld->ld_io_fns_ptr );
}
/* clean up I/O status tracking info. */
if ( ld->ld_iostatus != NULL ) {
NSLDAPIIOStatus *iosp = ld->ld_iostatus;
if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifdef NSLDAPI_HAVE_POLL
if ( iosp->ios_status.ios_osinfo.ossi_pollfds
!= NULL ) {
NSLDAPI_FREE(
iosp->ios_status.ios_osinfo.ossi_pollfds );
}
#endif /* NSLDAPI_HAVE_POLL */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
if ( iosp->ios_status.ios_cbinfo.cbsi_pollfds
!= NULL ) {
NSLDAPI_FREE(
iosp->ios_status.ios_cbinfo.cbsi_pollfds );
}
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_free: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
}
NSLDAPI_FREE( iosp );
}
}
#if !defined(NSLDAPI_HAVE_POLL) && !defined(NSLDAPI_AVOID_OS_SOCKETS)
static int
nsldapi_get_select_table_size( void )
{
static int tblsize = 0; /* static */
if ( tblsize == 0 ) {
#if defined(_WINDOWS) || defined(XP_OS2)
tblsize = FOPEN_MAX; /* ANSI spec. */
#else
#ifdef USE_SYSCONF
tblsize = sysconf( _SC_OPEN_MAX );
#else /* USE_SYSCONF */
tblsize = getdtablesize();
#endif /* else USE_SYSCONF */
#endif /* else _WINDOWS */
if ( tblsize >= FD_SETSIZE ) {
/*
* clamp value so we don't overrun the fd_set structure
*/
tblsize = FD_SETSIZE - 1;
}
}
return( tblsize );
}
#endif /* !defined(NSLDAPI_HAVE_POLL) && !defined(NSLDAPI_AVOID_OS_SOCKETS) */
static int
nsldapi_tv2ms( struct timeval *tv )
{
if ( tv == NULL ) {
return( -1 ); /* infinite timout for poll() */
}
return( tv->tv_sec * 1000 + tv->tv_usec / 1000 );
}
int
nsldapi_iostatus_poll( LDAP *ld, struct timeval *timeout )
{
int rc;
NSLDAPIIOStatus *iosp;
LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_iostatus_poll\n", 0, 0, 0 );
LDAP_MUTEX_LOCK( ld, LDAP_IOSTATUS_LOCK );
iosp = ld->ld_iostatus;
if ( iosp == NULL ||
( iosp->ios_read_count <= 0 && iosp->ios_write_count <= 0 )) {
rc = 0; /* simulate a timeout */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_OSNATIVE ) {
#ifndef NSLDAPI_AVOID_OS_SOCKETS
#ifdef NSLDAPI_HAVE_POLL
rc = NSLDAPI_POLL( iosp->ios_status.ios_osinfo.ossi_pollfds,
iosp->ios_status.ios_osinfo.ossi_pollfds_size,
nsldapi_tv2ms( timeout ));
#else /* NSLDAPI_HAVE_POLL */
/* two (potentially large) struct copies */
iosp->ios_status.ios_osinfo.ossi_use_readfds
= iosp->ios_status.ios_osinfo.ossi_readfds;
iosp->ios_status.ios_osinfo.ossi_use_writefds
= iosp->ios_status.ios_osinfo.ossi_writefds;
#ifdef HPUX9
rc = NSLDAPI_SELECT( nsldapi_get_select_table_size(),
(int *)&iosp->ios_status.ios_osinfo.ossi_use_readfds
(int *)&iosp->ios_status.ios_osinfo.ossi_use_writefds,
NULL, timeout );
#else
rc = NSLDAPI_SELECT( nsldapi_get_select_table_size(),
&iosp->ios_status.ios_osinfo.ossi_use_readfds,
&iosp->ios_status.ios_osinfo.ossi_use_writefds,
NULL, timeout );
#endif /* else HPUX9 */
#endif /* else NSLDAPI_HAVE_POLL */
#endif /* NSLDAPI_AVOID_OS_SOCKETS */
} else if ( iosp->ios_type == NSLDAPI_IOSTATUS_TYPE_CALLBACK ) {
/*
* We always pass the session extended I/O argument to
* the extended poll() callback.
*/
rc = ld->ld_extpoll_fn(
iosp->ios_status.ios_cbinfo.cbsi_pollfds,
iosp->ios_status.ios_cbinfo.cbsi_pollfds_size,
nsldapi_tv2ms( timeout ), ld->ld_ext_session_arg );
} else {
LDAPDebug( LDAP_DEBUG_ANY,
"nsldapi_iostatus_poll: unknown I/O type %d\n",
iosp->ios_type, 0, 0 );
rc = 0; /* simulate a timeout (what else to do?) */
}
LDAP_MUTEX_UNLOCK( ld, LDAP_IOSTATUS_LOCK );
return( rc );
}
#if defined(NSLDAPI_HAVE_POLL) && !defined(NSLDAPI_AVOID_OS_SOCKETS)
/*
* returns 1 if "fd" was added to pollfds.
* returns 1 if some of the bits in "events" were added to pollfds.
* returns 0 if no changes were made.
*/
static int
nsldapi_add_to_os_pollfds( int fd, struct nsldapi_os_statusinfo *pip,
short events )
{
int i, openslot;
/* first we check to see if "fd" is already in our pollfds */
openslot = -1;
for ( i = 0; i < pip->ossi_pollfds_size; ++i ) {
if ( pip->ossi_pollfds[ i ].fd == fd ) {
if (( pip->ossi_pollfds[ i ].events & events )
!= events ) {
pip->ossi_pollfds[ i ].events |= events;
return( 1 );
} else {
return( 0 );
}
}
if ( pip->ossi_pollfds[ i ].fd == -1 && openslot == -1 ) {
openslot = i; /* remember for later */
}
}
/*
* "fd" is not currently being poll'd on -- add to array.
* if we need to expand the pollfds array, we do it in increments of
* NSLDAPI_POLL_ARRAY_GROWTH (#define near the top of this file).
*/
if ( openslot == -1 ) {
struct pollfd *newpollfds;
if ( pip->ossi_pollfds_size == 0 ) {
newpollfds = (struct pollfd *)NSLDAPI_MALLOC(
NSLDAPI_POLL_ARRAY_GROWTH
* sizeof( struct pollfd ));
} else {
newpollfds = (struct pollfd *)NSLDAPI_REALLOC(
pip->ossi_pollfds, (NSLDAPI_POLL_ARRAY_GROWTH
+ pip->ossi_pollfds_size)
* sizeof( struct pollfd ));
}
if ( newpollfds == NULL ) { /* XXXmcs: no way to return err! */
return( 0 );
}
pip->ossi_pollfds = newpollfds;
openslot = pip->ossi_pollfds_size;
pip->ossi_pollfds_size += NSLDAPI_POLL_ARRAY_GROWTH;
for ( i = openslot + 1; i < pip->ossi_pollfds_size; ++i ) {
pip->ossi_pollfds[ i ].fd = -1;
pip->ossi_pollfds[ i ].events =
pip->ossi_pollfds[ i ].revents = 0;
}
}
pip->ossi_pollfds[ openslot ].fd = fd;
pip->ossi_pollfds[ openslot ].events = events;
pip->ossi_pollfds[ openslot ].revents = 0;
return( 1 );
}
/*
* returns 1 if any "events" from "fd" were removed from pollfds
* returns 0 of "fd" wasn't in pollfds or if events did not overlap.
*/
static int
nsldapi_clear_from_os_pollfds( int fd, struct nsldapi_os_statusinfo *pip,
short events )
{
int i;
for ( i = 0; i < pip->ossi_pollfds_size; ++i ) {
if ( pip->ossi_pollfds[i].fd == fd ) {
if (( pip->ossi_pollfds[ i ].events & events ) != 0 ) {
pip->ossi_pollfds[ i ].events &= ~events;
if ( pip->ossi_pollfds[ i ].events == 0 ) {
pip->ossi_pollfds[i].fd = -1;
}
return( 1 ); /* events overlap */
} else {
return( 0 ); /* events do not overlap */
}
}
}
return( 0 ); /* "fd" was not found */
}
/*
* returns 1 if any "revents" from "fd" were set in pollfds revents field.
* returns 0 if not.
*/
static int
nsldapi_find_in_os_pollfds( int fd, struct nsldapi_os_statusinfo *pip,
short revents )
{
int i;
for ( i = 0; i < pip->ossi_pollfds_size; ++i ) {
if ( pip->ossi_pollfds[i].fd == fd ) {
if (( pip->ossi_pollfds[ i ].revents & revents ) != 0 ) {
return( 1 ); /* revents overlap */
} else {
return( 0 ); /* revents do not overlap */
}
}
}
return( 0 ); /* "fd" was not found */
}
#endif /* !defined(NSLDAPI_HAVE_POLL) && !defined(NSLDAPI_AVOID_OS_SOCKETS) */
/*
* returns 1 if "sb" was added to pollfds.
* returns 1 if some of the bits in "events" were added to pollfds.
* returns 0 if no changes were made.
*/
static int
nsldapi_add_to_cb_pollfds( Sockbuf *sb, struct nsldapi_cb_statusinfo *pip,
short events )
{
int i, openslot;
/* first we check to see if "sb" is already in our pollfds */
openslot = -1;
for ( i = 0; i < pip->cbsi_pollfds_size; ++i ) {
if ( NSLDAPI_CB_POLL_MATCH( sb, pip->cbsi_pollfds[ i ] )) {
if (( pip->cbsi_pollfds[ i ].lpoll_events & events )
!= events ) {
pip->cbsi_pollfds[ i ].lpoll_events |= events;
return( 1 );
} else {
return( 0 );
}
}
if ( pip->cbsi_pollfds[ i ].lpoll_fd == -1 && openslot == -1 ) {
openslot = i; /* remember for later */
}
}
/*
* "sb" is not currently being poll'd on -- add to array.
* if we need to expand the pollfds array, we do it in increments of
* NSLDAPI_POLL_ARRAY_GROWTH (#define near the top of this file).
*/
if ( openslot == -1 ) {
LDAP_X_PollFD *newpollfds;
if ( pip->cbsi_pollfds_size == 0 ) {
newpollfds = (LDAP_X_PollFD *)NSLDAPI_MALLOC(
NSLDAPI_POLL_ARRAY_GROWTH
* sizeof( LDAP_X_PollFD ));
} else {
newpollfds = (LDAP_X_PollFD *)NSLDAPI_REALLOC(
pip->cbsi_pollfds, (NSLDAPI_POLL_ARRAY_GROWTH
+ pip->cbsi_pollfds_size)
* sizeof( LDAP_X_PollFD ));
}
if ( newpollfds == NULL ) { /* XXXmcs: no way to return err! */
return( 0 );
}
pip->cbsi_pollfds = newpollfds;
openslot = pip->cbsi_pollfds_size;
pip->cbsi_pollfds_size += NSLDAPI_POLL_ARRAY_GROWTH;
for ( i = openslot + 1; i < pip->cbsi_pollfds_size; ++i ) {
pip->cbsi_pollfds[ i ].lpoll_fd = -1;
pip->cbsi_pollfds[ i ].lpoll_socketarg = NULL;
pip->cbsi_pollfds[ i ].lpoll_events =
pip->cbsi_pollfds[ i ].lpoll_revents = 0;
}
}
pip->cbsi_pollfds[ openslot ].lpoll_fd = sb->sb_sd;
pip->cbsi_pollfds[ openslot ].lpoll_socketarg =
sb->sb_ext_io_fns.lbextiofn_socket_arg;
pip->cbsi_pollfds[ openslot ].lpoll_events = events;
pip->cbsi_pollfds[ openslot ].lpoll_revents = 0;
return( 1 );
}
/*
* returns 1 if any "events" from "sb" were removed from pollfds
* returns 0 of "sb" wasn't in pollfds or if events did not overlap.
*/
static int
nsldapi_clear_from_cb_pollfds( Sockbuf *sb,
struct nsldapi_cb_statusinfo *pip, short events )
{
int i;
for ( i = 0; i < pip->cbsi_pollfds_size; ++i ) {
if ( NSLDAPI_CB_POLL_MATCH( sb, pip->cbsi_pollfds[ i ] )) {
if (( pip->cbsi_pollfds[ i ].lpoll_events
& events ) != 0 ) {
pip->cbsi_pollfds[ i ].lpoll_events &= ~events;
if ( pip->cbsi_pollfds[ i ].lpoll_events
== 0 ) {
pip->cbsi_pollfds[i].lpoll_fd = -1;
}
return( 1 ); /* events overlap */
} else {
return( 0 ); /* events do not overlap */
}
}
}
return( 0 ); /* "sb" was not found */
}
/*
* returns 1 if any "revents" from "sb" were set in pollfds revents field.
* returns 0 if not.
*/
static int
nsldapi_find_in_cb_pollfds( Sockbuf *sb, struct nsldapi_cb_statusinfo *pip,
short revents )
{
int i;
for ( i = 0; i < pip->cbsi_pollfds_size; ++i ) {
if ( NSLDAPI_CB_POLL_MATCH( sb, pip->cbsi_pollfds[ i ] )) {
if (( pip->cbsi_pollfds[ i ].lpoll_revents
& revents ) != 0 ) {
return( 1 ); /* revents overlap */
} else {
return( 0 ); /* revents do not overlap */
}
}
}
return( 0 ); /* "sb" was not found */
}
/*
* Install read and write functions into lber layer / sb
*/
int
nsldapi_install_lber_extiofns( LDAP *ld, Sockbuf *sb )
{
struct lber_x_ext_io_fns lberiofns;
if ( NULL != sb ) {
lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
lberiofns.lbextiofn_read = ld->ld_extread_fn;
lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
lberiofns.lbextiofn_socket_arg = ld->ld_ext_session_arg;
if ( ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_EXT_IO_FNS,
&lberiofns ) != 0 ) {
return( LDAP_LOCAL_ERROR );
}
}
return( LDAP_SUCCESS );
}
/*
******************************************************************************
* One struct and several functions to bridge the gap between new extended
* I/O functions that are installed using ldap_set_option( ...,
* LDAP_OPT_EXTIO_FN_PTRS, ... ) and the original "classic" I/O functions
* (installed using LDAP_OPT_IO_FN_PTRS) follow.
*
* Our basic strategy is to use the new extended arg to hold a pointer to a
* structure that contains a pointer to the LDAP * (which contains pointers
* to the old functions so we can call them) as well as a pointer to an
* LBER_SOCKET to hold the socket used by the classic functions (the new
* functions use a simple int for the socket).
*/
typedef struct nsldapi_compat_socket_info {
LBER_SOCKET csi_socket;
LDAP *csi_ld;
} NSLDAPICompatSocketInfo;
static int LDAP_CALLBACK
nsldapi_ext_compat_read( int s, void *buf, int len,
struct lextiof_socket_private *arg )
{
NSLDAPICompatSocketInfo *csip = (NSLDAPICompatSocketInfo *)arg;
struct ldap_io_fns *iofns = csip->csi_ld->ld_io_fns_ptr;
return( iofns->liof_read( csip->csi_socket, buf, len ));
}
static int LDAP_CALLBACK
nsldapi_ext_compat_write( int s, const void *buf, int len,
struct lextiof_socket_private *arg )
{
NSLDAPICompatSocketInfo *csip = (NSLDAPICompatSocketInfo *)arg;
struct ldap_io_fns *iofns = csip->csi_ld->ld_io_fns_ptr;
return( iofns->liof_write( csip->csi_socket, buf, len ));
}
static int LDAP_CALLBACK
nsldapi_ext_compat_poll( LDAP_X_PollFD fds[], int nfds, int timeout,
struct lextiof_session_private *arg )
{
NSLDAPICompatSocketInfo *csip = (NSLDAPICompatSocketInfo *)arg;
struct ldap_io_fns *iofns = csip->csi_ld->ld_io_fns_ptr;
fd_set readfds, writefds;
int i, rc, maxfd = 0;
struct timeval tv, *tvp;
/*
* Prepare fd_sets for select()
*/
FD_ZERO( &readfds );
FD_ZERO( &writefds );
for ( i = 0; i < nfds; ++i ) {
if ( fds[ i ].lpoll_fd < 0 ) {
continue;
}
if ( fds[ i ].lpoll_fd >= FD_SETSIZE ) {
LDAP_SET_ERRNO( csip->csi_ld, EINVAL );
return( -1 );
}
if ( 0 != ( fds[i].lpoll_events & LDAP_X_POLLIN )) {
FD_SET( fds[i].lpoll_fd, &readfds );
}
if ( 0 != ( fds[i].lpoll_events & LDAP_X_POLLOUT )) {
FD_SET( fds[i].lpoll_fd, &writefds );
}
fds[i].lpoll_revents = 0; /* clear revents */
if ( fds[i].lpoll_fd >= maxfd ) {
maxfd = fds[i].lpoll_fd;
}
}
/*
* select() using callback.
*/
++maxfd;
if ( timeout == -1 ) {
tvp = NULL;
} else {
tv.tv_sec = timeout / 1000;
tv.tv_usec = 1000 * ( timeout - tv.tv_sec * 1000 );
tvp = &tv;
}
rc = iofns->liof_select( maxfd, &readfds, &writefds, NULL, tvp );
if ( rc <= 0 ) { /* timeout or fatal error */
return( rc );
}
/*
* Use info. in fd_sets to populate poll() revents.
*/
for ( i = 0; i < nfds; ++i ) {
if ( fds[ i ].lpoll_fd < 0 ) {
continue;
}
if ( 0 != ( fds[i].lpoll_events & LDAP_X_POLLIN )
&& FD_ISSET( fds[i].lpoll_fd, &readfds )) {
fds[i].lpoll_revents |= LDAP_X_POLLIN;
}
if ( 0 != ( fds[i].lpoll_events & LDAP_X_POLLOUT )
&& FD_ISSET( fds[i].lpoll_fd, &writefds )) {
fds[i].lpoll_revents |= LDAP_X_POLLOUT;
}
/* XXXmcs: any other cases to deal with? LDAP_X_POLLERR? */
}
return( rc );
}
static LBER_SOCKET
nsldapi_compat_socket( LDAP *ld, int secure, int domain, int type,
int protocol )
{
int s;
s = ld->ld_io_fns_ptr->liof_socket( domain, type, protocol );
if ( s >= 0 ) {
char *errmsg = NULL;
#ifdef NSLDAPI_HAVE_POLL
if ( ld->ld_io_fns_ptr->liof_select != NULL
&& s >= FD_SETSIZE ) {
errmsg = "can't use socket >= FD_SETSIZE";
}
#elif !defined(_WINDOWS) /* not on Windows and do not have poll() */
if ( s >= FD_SETSIZE ) {
errmsg = "can't use socket >= FD_SETSIZE";
}
#endif
if ( NULL == errmsg && secure &&
ld->ld_io_fns_ptr->liof_ssl_enable( s ) < 0 ) {
errmsg = "failed to enable secure mode";
}
if ( NULL != errmsg ) {
if ( NULL == ld->ld_io_fns_ptr->liof_close ) {
nsldapi_os_closesocket( s );
} else {
ld->ld_io_fns_ptr->liof_close( s );
}
LDAP_SET_LDERRNO( ld, LDAP_LOCAL_ERROR, NULL,
nsldapi_strdup( errmsg ));
return( -1 );
}
}
return( s );
}
/*
* Note: timeout is ignored because we have no way to pass it via
* the old I/O callback interface.
*/
static int LDAP_CALLBACK
nsldapi_ext_compat_connect( const char *hostlist, int defport, int timeout,
unsigned long options, struct lextiof_session_private *sessionarg,
struct lextiof_socket_private **socketargp )
{
NSLDAPICompatSocketInfo *defcsip;
struct ldap_io_fns *iofns;
int s, secure;
NSLDAPI_SOCKET_FN *socketfn;
NSLDAPI_IOCTL_FN *ioctlfn;
NSLDAPI_CONNECT_WITH_TO_FN *connectwithtofn;
NSLDAPI_CONNECT_FN *connectfn;
NSLDAPI_CLOSE_FN *closefn;
defcsip = (NSLDAPICompatSocketInfo *)sessionarg;
iofns = defcsip->csi_ld->ld_io_fns_ptr;
if ( 0 != ( options & LDAP_X_EXTIOF_OPT_SECURE )) {
if ( NULL == iofns->liof_ssl_enable ) {
LDAP_SET_ERRNO( defcsip->csi_ld, EINVAL );
return( -1 );
}
secure = 1;
} else {
secure = 0;
}
socketfn = ( iofns->liof_socket == NULL ) ?
nsldapi_os_socket : nsldapi_compat_socket;
ioctlfn = ( iofns->liof_ioctl == NULL ) ?
nsldapi_os_ioctl : (NSLDAPI_IOCTL_FN *)(iofns->liof_ioctl);
if ( NULL == iofns->liof_connect ) {
connectwithtofn = nsldapi_os_connect_with_to;
connectfn = NULL;
} else {
connectwithtofn = NULL;
connectfn = iofns->liof_connect;
}
closefn = ( iofns->liof_close == NULL ) ?
nsldapi_os_closesocket : iofns->liof_close;
s = nsldapi_try_each_host( defcsip->csi_ld, hostlist, defport,
secure, socketfn, ioctlfn, connectwithtofn,
connectfn, closefn );
if ( s >= 0 ) {
NSLDAPICompatSocketInfo *csip;
if (( csip = (NSLDAPICompatSocketInfo *)NSLDAPI_CALLOC( 1,
sizeof( NSLDAPICompatSocketInfo ))) == NULL ) {
(*closefn)( s );
LDAP_SET_LDERRNO( defcsip->csi_ld, LDAP_NO_MEMORY,
NULL, NULL );
return( -1 );
}
csip->csi_socket = s;
csip->csi_ld = defcsip->csi_ld;
*socketargp = (void *)csip;
/*
* We always return 1, which is a valid but not unique socket
* (file descriptor) number. The extended I/O functions only
* require that the combination of the void *arg and the int
* socket be unique. Since we allocate the
* NSLDAPICompatSocketInfo that we assign to arg, we meet
* that requirement.
*/
s = 1;
}
return( s );
}
static int LDAP_CALLBACK
nsldapi_ext_compat_close( int s, struct lextiof_socket_private *arg )
{
NSLDAPICompatSocketInfo *csip = (NSLDAPICompatSocketInfo *)arg;
struct ldap_io_fns *iofns = csip->csi_ld->ld_io_fns_ptr;
int rc;
rc = iofns->liof_close( csip->csi_socket );
NSLDAPI_FREE( csip );
return( rc );
}
/*
* Install the I/O functions.
* Return an LDAP error code (LDAP_SUCCESS if all goes well).
*/
int
nsldapi_install_compat_io_fns( LDAP *ld, struct ldap_io_fns *iofns )
{
NSLDAPICompatSocketInfo *defcsip;
if (( defcsip = (NSLDAPICompatSocketInfo *)NSLDAPI_CALLOC( 1,
sizeof( NSLDAPICompatSocketInfo ))) == NULL ) {
return( LDAP_NO_MEMORY );
}
defcsip->csi_socket = -1;
defcsip->csi_ld = ld;
if ( ld->ld_io_fns_ptr != NULL ) {
(void)memset( (char *)ld->ld_io_fns_ptr, 0,
sizeof( struct ldap_io_fns ));
} else if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_CALLOC(
1, sizeof( struct ldap_io_fns ))) == NULL ) {
NSLDAPI_FREE( defcsip );
return( LDAP_NO_MEMORY );
}
/* struct copy */
*(ld->ld_io_fns_ptr) = *iofns;
ld->ld_extio_size = LBER_X_EXTIO_FNS_SIZE;
ld->ld_ext_session_arg = defcsip;
ld->ld_extread_fn = nsldapi_ext_compat_read;
ld->ld_extwrite_fn = nsldapi_ext_compat_write;
ld->ld_extpoll_fn = nsldapi_ext_compat_poll;
ld->ld_extconnect_fn = nsldapi_ext_compat_connect;
ld->ld_extclose_fn = nsldapi_ext_compat_close;
return( nsldapi_install_lber_extiofns( ld, ld->ld_sbp ));
}
/*
* end of compat I/O functions
******************************************************************************
*/
|