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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Description: Socket/Naming manipulation functions
* Based on: Various Jserv files
*/
/**
* @package jk_connect
* @author Gal Shachor <shachor@il.ibm.com>
* @author Mladen Turk <mturk@apache.org>
* @version $Revision: 1841818 $
*/
#include "jk_connect.h"
#include "jk_util.h"
#ifdef HAVE_APR
#include "apr_network_io.h"
#include "apr_errno.h"
#include "apr_general.h"
#include "apr_pools.h"
static apr_pool_t *jk_apr_pool = NULL;
#endif
#ifdef HAVE_SYS_FILIO_H
/* FIONREAD on Solaris et al. */
#include <sys/filio.h>
#endif
#ifdef HAVE_POLL_H
/* Use poll instead select */
#include <poll.h>
#endif
#if defined(WIN32)
#define JK_IS_SOCKET_ERROR(x) ((x) == SOCKET_ERROR)
#define JK_GET_SOCKET_ERRNO() errno = WSAGetLastError() - WSABASEERR
#else
#define JK_IS_SOCKET_ERROR(x) ((x) == -1)
#define JK_GET_SOCKET_ERRNO() ((void)0)
#endif /* WIN32 */
#ifndef INET6_ADDRSTRLEN
/* Maximum size of an IPv6 address in ASCII */
#define INET6_ADDRSTRLEN 46
#endif
/* 2 IPv6 adresses of length (INET6_ADDRSTRLEN-1)
* each suffixed with a ":" and a port (5 digits)
* plus " -> " plus terminating "\0"
*/
#define DUMP_SINFO_BUF_SZ (2 * (INET6_ADDRSTRLEN - 1 + 1 + 5) + 4 + 1)
#ifndef IN6ADDRSZ
#define IN6ADDRSZ 16
#endif
#ifndef INT16SZ
#define INT16SZ sizeof(short)
#endif
#if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#endif
/** Set socket to blocking
* @param sd socket to manipulate
* @return errno: fcntl returns -1 (!WIN32)
* pseudo errno: ioctlsocket returns SOCKET_ERROR (WIN32)
* 0: success
*/
static int soblock(jk_sock_t sd)
{
/* BeOS uses setsockopt at present for non blocking... */
#if defined (WIN32)
u_long on = 0;
if (JK_IS_SOCKET_ERROR(ioctlsocket(sd, FIONBIO, &on))) {
JK_GET_SOCKET_ERRNO();
return errno;
}
#else
int fd_flags;
fd_flags = fcntl(sd, F_GETFL, 0);
#if defined(O_NONBLOCK)
fd_flags &= ~O_NONBLOCK;
#elif defined(O_NDELAY)
fd_flags &= ~O_NDELAY;
#elif defined(FNDELAY)
fd_flags &= ~FNDELAY;
#else
#error Please teach JK how to make sockets blocking on your platform.
#endif
if (fcntl(sd, F_SETFL, fd_flags) == -1) {
return errno;
}
#endif /* WIN32 */
return 0;
}
/** Set socket to non-blocking
* @param sd socket to manipulate
* @return errno: fcntl returns -1 (!WIN32)
* pseudo errno: ioctlsocket returns SOCKET_ERROR (WIN32)
* 0: success
*/
static int sononblock(jk_sock_t sd)
{
#if defined (WIN32)
u_long on = 1;
if (JK_IS_SOCKET_ERROR(ioctlsocket(sd, FIONBIO, &on))) {
JK_GET_SOCKET_ERRNO();
return errno;
}
#else
int fd_flags;
fd_flags = fcntl(sd, F_GETFL, 0);
#if defined(O_NONBLOCK)
fd_flags |= O_NONBLOCK;
#elif defined(O_NDELAY)
fd_flags |= O_NDELAY;
#elif defined(FNDELAY)
fd_flags |= FNDELAY;
#else
#error Please teach JK how to make sockets non-blocking on your platform.
#endif
if (fcntl(sd, F_SETFL, fd_flags) == -1) {
return errno;
}
#endif /* WIN32 */
return 0;
}
#if defined (WIN32)
/* WIN32 implementation */
/** Non-blocking socket connect
* @param sd socket to connect
* @param addr address to connect to
* @param source optional source address
* @param timeout connect timeout in seconds
* (<=0: no timeout=blocking)
* @param l logger
* @return -1: some kind of error occured
* SOCKET_ERROR: no timeout given and error
* during blocking connect
* 0: success
*/
static int nb_connect(jk_sock_t sd, jk_sockaddr_t *addr, jk_sockaddr_t *source,
int timeout, jk_logger_t *l)
{
int rc;
char buf[64];
JK_TRACE_ENTER(l);
if (source != NULL) {
if (bind(sd, (const struct sockaddr *)&source->sa.sin, source->salen)) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"error during source bind on socket %d [%s] (errno=%d)",
sd, jk_dump_hinfo(source, buf, sizeof(buf)), errno);
}
}
if (timeout <= 0) {
rc = connect(sd, (const struct sockaddr *)&addr->sa.sin, addr->salen);
JK_TRACE_EXIT(l);
return rc;
}
if ((rc = sononblock(sd))) {
JK_TRACE_EXIT(l);
return -1;
}
if (JK_IS_SOCKET_ERROR(connect(sd, (const struct sockaddr *)&addr->sa.sin, addr->salen))) {
struct timeval tv;
fd_set wfdset, efdset;
if ((rc = WSAGetLastError()) != WSAEWOULDBLOCK) {
soblock(sd);
WSASetLastError(rc);
JK_TRACE_EXIT(l);
return -1;
}
/* wait for the connect to complete or timeout */
FD_ZERO(&wfdset);
FD_SET(sd, &wfdset);
FD_ZERO(&efdset);
FD_SET(sd, &efdset);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
rc = select((int)sd + 1, NULL, &wfdset, &efdset, &tv);
if (JK_IS_SOCKET_ERROR(rc) || rc == 0) {
rc = WSAGetLastError();
soblock(sd);
WSASetLastError(rc);
JK_TRACE_EXIT(l);
return -1;
}
/* Evaluate the efdset */
if (FD_ISSET(sd, &efdset)) {
/* The connect failed. */
int rclen = (int)sizeof(rc);
if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (char*) &rc, &rclen))
rc = 0;
soblock(sd);
if (rc)
WSASetLastError(rc);
JK_TRACE_EXIT(l);
return -1;
}
}
soblock(sd);
JK_TRACE_EXIT(l);
return 0;
}
#elif defined(HAVE_POLL_H)
/* POSIX implementation using poll(2) */
/** Non-blocking socket connect
* @param sd socket to connect
* @param addr address to connect to
* @param source optional source address
* @param timeout connect timeout in seconds
* (<=0: no timeout=blocking)
* @param l logger
* @return -1: some kind of error occured
* 0: success
*/
static int nb_connect(jk_sock_t sd, jk_sockaddr_t *addr, jk_sockaddr_t *source,
int timeout, jk_logger_t *l) {
int rc = 0;
char buf[64];
JK_TRACE_ENTER(l);
if (source != NULL) {
if (bind(sd, (const struct sockaddr *)&source->sa.sin, source->salen)) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"error during source bind on socket %d [%s] (errno=%d)", sd,
jk_dump_hinfo(source, buf, sizeof(buf)), errno);
}
}
if (timeout > 0) {
if (sononblock(sd)) {
JK_TRACE_EXIT(l);
return -1;
}
}
do {
rc = connect(sd, (const struct sockaddr *)&addr->sa.sin, addr->salen);
} while (rc == -1 && errno == EINTR);
if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
&& (timeout > 0)) {
struct pollfd pfd;
socklen_t rclen = (socklen_t)sizeof(rc);
pfd.fd = sd;
pfd.events = POLLOUT;
rc = poll(&pfd, 1, timeout);
if (rc <= 0) {
/* Save errno */
int err = errno;
soblock(sd);
errno = err;
JK_TRACE_EXIT(l);
return -1;
}
rc = 0;
#ifdef SO_ERROR
if (getsockopt(sd, SOL_SOCKET, SO_ERROR,
(char *)&rc, &rclen) < 0 || rc) {
if (rc)
errno = rc;
rc = -1;
}
#endif
}
soblock(sd);
JK_TRACE_EXIT(l);
return rc;
}
#else
/* POSIX implementation using select(2) */
/** Non-blocking socket connect
* @param sd socket to connect
* @param addr address to connect to
* @param source optional source address
* @param timeout connect timeout in seconds
* (<=0: no timeout=blocking)
* @param l logger
* @return -1: some kind of error occured
* 0: success
*/
static int nb_connect(jk_sock_t sd, jk_sockaddr_t *addr, jk_sockaddr_t *source,
int timeout, jk_logger_t *l)
{
int rc = 0;
char buf[64];
JK_TRACE_ENTER(l);
if (source != NULL) {
if (bind(sd, (const struct sockaddr *)&source->sa.sin, source->salen)) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"error during source bind on socket %d [%s] (errno=%d)",
sd, jk_dump_hinfo(source, buf, sizeof(buf)), errno);
}
}
if (timeout > 0) {
if (sononblock(sd)) {
JK_TRACE_EXIT(l);
return -1;
}
}
do {
rc = connect(sd, (const struct sockaddr *)&addr->sa.sin, addr->salen);
} while (rc == -1 && errno == EINTR);
if ((rc == -1) && (errno == EINPROGRESS || errno == EALREADY)
&& (timeout > 0)) {
fd_set wfdset;
struct timeval tv;
socklen_t rclen = (socklen_t)sizeof(rc);
FD_ZERO(&wfdset);
FD_SET(sd, &wfdset);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
rc = select(sd + 1, NULL, &wfdset, NULL, &tv);
if (rc <= 0) {
/* Save errno */
int err = errno;
soblock(sd);
errno = err;
JK_TRACE_EXIT(l);
return -1;
}
rc = 0;
#ifdef SO_ERROR
if (!FD_ISSET(sd, &wfdset) ||
(getsockopt(sd, SOL_SOCKET, SO_ERROR,
(char *)&rc, &rclen) < 0) || rc) {
if (rc)
errno = rc;
rc = -1;
}
#endif /* SO_ERROR */
}
/* Not sure we can be already connected */
if (rc == -1 && errno == EISCONN)
rc = 0;
soblock(sd);
JK_TRACE_EXIT(l);
return rc;
}
#endif
/** Clone a jk_sockaddr_t
* @param out The source structure
* @param in The target structure
*/
void jk_clone_sockaddr(jk_sockaddr_t *out, jk_sockaddr_t *in)
{
memcpy(out, in, sizeof(*in));
/* The ipaddr_ptr member points to memory inside the struct.
* Do not copy the pointer but use the same offset relative
* to the struct start
*/
out->ipaddr_ptr = (char *)out + ((char *)in->ipaddr_ptr - (char *)in);
}
/** Resolve the host IP
* @param host host or ip address
* @param port port
* @param rc return value pointer
* @param l logger
* @return JK_FALSE: some kind of error occured
* JK_TRUE: success
*/
int jk_resolve(const char *host, int port, jk_sockaddr_t *saddr,
void *pool, int prefer_ipv6, jk_logger_t *l)
{
int family = JK_INET;
struct in_addr iaddr;
JK_TRACE_ENTER(l);
memset(saddr, 0, sizeof(jk_sockaddr_t));
if (*host >= '0' && *host <= '9' && strspn(host, "0123456789.") == strlen(host)) {
/* If we found only digits we use inet_addr() */
iaddr.s_addr = jk_inet_addr(host);
memcpy(&(saddr->sa.sin.sin_addr), &iaddr, sizeof(struct in_addr));
}
else {
#ifdef HAVE_APR
apr_sockaddr_t *remote_sa, *temp_sa;
if (!jk_apr_pool) {
if (apr_pool_create(&jk_apr_pool, (apr_pool_t *)pool) != APR_SUCCESS) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
apr_pool_clear(jk_apr_pool);
if (apr_sockaddr_info_get(&remote_sa, host, APR_UNSPEC, (apr_port_t)port,
0, jk_apr_pool) != APR_SUCCESS) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
/* Check if we have multiple address matches
*/
if (remote_sa->next) {
/* Since we are only handling JK_INET (IPV4) address (in_addr_t) */
/* make sure we find one of those. */
temp_sa = remote_sa;
#if APR_HAVE_IPV6
if (prefer_ipv6) {
while ((NULL != temp_sa) && (APR_INET6 != temp_sa->family))
temp_sa = temp_sa->next;
}
#endif
if (NULL != temp_sa) {
remote_sa = temp_sa;
}
else {
while ((NULL != temp_sa) && (APR_INET != temp_sa->family))
temp_sa = temp_sa->next;
#if APR_HAVE_IPV6
if (NULL == temp_sa) {
temp_sa = remote_sa;
while ((NULL != temp_sa) && (APR_INET6 != temp_sa->family))
temp_sa = temp_sa->next;
}
#endif
}
/* if temp_sa is set, we have a valid address otherwise, just return */
if (NULL != temp_sa) {
remote_sa = temp_sa;
}
else {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
}
if (remote_sa->family == APR_INET) {
saddr->sa.sin = remote_sa->sa.sin;
family = JK_INET;
}
#if APR_HAVE_IPV6
else {
saddr->sa.sin6 = remote_sa->sa.sin6;
family = JK_INET6;
}
#endif
#else /* HAVE_APR */
/* Without APR go the classic way.
*/
#if defined(HAVE_GETADDRINFO)
/* TODO:
* 1. Check for numeric IPV6 addresses
* 2. Do we need to set service name for getaddrinfo?
*/
struct addrinfo hints, *ai_list, *ai = NULL;
int error;
char pbuf[12];
char *pbufptr = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
#if JK_HAVE_IPV6
if (strchr(host, ':')) {
/* If host name contains colon this must be IPV6 address.
* Set prefer_ipv6 flag in this case if it wasn't set already
*/
prefer_ipv6 = 1;
}
if (prefer_ipv6)
hints.ai_family = JK_INET6;
else
#endif
hints.ai_family = JK_INET;
if (port > 0) {
snprintf(pbuf, sizeof(pbuf), "%d", port);
pbufptr = pbuf;
}
error = getaddrinfo(host, pbufptr, &hints, &ai_list);
#if JK_HAVE_IPV6
/* XXX:
* Is the check for EAI_FAMILY/WSAEAFNOSUPPORT correct
* way to retry the IPv4 address?
*/
if (error == EAI_FAMILY && prefer_ipv6) {
hints.ai_family = JK_INET;
error = getaddrinfo(host, pbufptr, &hints, &ai_list);
}
#endif
if (error) {
JK_TRACE_EXIT(l);
errno = error;
return JK_FALSE;
}
#if JK_HAVE_IPV6
if (prefer_ipv6) {
ai = ai_list;
while (ai) {
if (ai->ai_family == JK_INET6) {
/* ignore elements without required address info */
if((ai->ai_addr != NULL) && (ai->ai_addrlen > 0)) {
family = JK_INET6;
break;
}
}
ai = ai->ai_next;
}
}
#endif
if (ai == NULL) {
ai = ai_list;
while (ai) {
if (ai->ai_family == JK_INET) {
/* ignore elements without required address info */
if((ai->ai_addr != NULL) && (ai->ai_addrlen > 0)) {
family = JK_INET;
break;
}
}
ai = ai->ai_next;
}
}
if (ai == NULL) {
/* No address found
* XXX: Use better error code?
*/
freeaddrinfo(ai_list);
JK_TRACE_EXIT(l);
errno = ENOENT;
return JK_FALSE;
}
memcpy(&(saddr->sa), ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai_list);
#else /* HAVE_GETADDRINFO */
struct hostent *hoste;
/* XXX : WARNING : We should really use gethostbyname_r in multi-threaded env */
/* Fortunatly when APR is available, ie under Apache 2.0, we use it */
hoste = gethostbyname(host);
if (!hoste) {
JK_TRACE_EXIT(l);
return JK_FALSE;
}
iaddr = *((struct in_addr *)hoste->h_addr_list[0]);
memcpy(&(saddr->sa.sin.sin_addr), &iaddr, sizeof(struct in_addr));
#endif /* HAVE_GETADDRINFO */
#endif /* HAVE_APR */
}
if (family == JK_INET) {
saddr->ipaddr_ptr = &(saddr->sa.sin.sin_addr);
saddr->ipaddr_len = (int)sizeof(struct in_addr);
saddr->salen = (int)sizeof(struct sockaddr_in);
}
#if JK_HAVE_IPV6
else {
saddr->ipaddr_ptr = &(saddr->sa.sin6.sin6_addr);
saddr->ipaddr_len = (int)sizeof(struct in6_addr);
saddr->salen = (int)sizeof(struct sockaddr_in6);
}
#endif
saddr->sa.sin.sin_family = family;
/* XXX IPv6: assumes sin_port and sin6_port at same offset */
saddr->sa.sin.sin_port = htons(port);
saddr->port = port;
saddr->family = family;
JK_TRACE_EXIT(l);
return JK_TRUE;
}
/** Connect to Tomcat
* @param addr address to connect to
* @param keepalive should we set SO_KEEPALIVE (if !=0)
* @param timeout connect timeout in seconds
* (<=0: no timeout=blocking)
* @param sock_buf size of send and recv buffer
* (<=0: use default)
* @param l logger
* @return JK_INVALID_SOCKET: some kind of error occured
* created socket: success
* @remark Cares about errno
*/
jk_sock_t jk_open_socket(jk_sockaddr_t *addr, jk_sockaddr_t *source,
int keepalive,
int timeout, int connect_timeout,
int sock_buf, jk_logger_t *l)
{
char buf[DUMP_SINFO_BUF_SZ];
jk_sock_t sd;
int set = 1;
int ret = 0;
int flags = 0;
#ifdef SO_LINGER
struct linger li;
#endif
JK_TRACE_ENTER(l);
errno = 0;
#if defined(SOCK_CLOEXEC) && defined(USE_SOCK_CLOEXEC)
flags |= SOCK_CLOEXEC;
#endif
sd = socket(addr->family, SOCK_STREAM | flags, 0);
if (!IS_VALID_SOCKET(sd)) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"socket() failed (errno=%d)", errno);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
#if defined(FD_CLOEXEC) && !defined(USE_SOCK_CLOEXEC)
if ((flags = fcntl(sd, F_GETFD)) == -1) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"fcntl() failed (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
flags |= FD_CLOEXEC;
if (fcntl(sd, F_SETFD, flags) == -1) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"fcntl() failed (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
#endif
/* Disable Nagle algorithm */
if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (const char*)&set,
sizeof(set))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting TCP_NODELAY (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"socket TCP_NODELAY set to On");
if (keepalive) {
#if defined(WIN32)
DWORD dw;
struct tcp_keepalive ka = { 0 }, ks = { 0 };
if (timeout)
ka.keepalivetime = timeout * 10000;
else
ka.keepalivetime = 60 * 10000; /* 10 minutes */
ka.keepaliveinterval = 1000;
ka.onoff = 1;
if (WSAIoctl(sd, SIO_KEEPALIVE_VALS, &ka, sizeof(ka),
&ks, sizeof(ks), &dw, NULL, NULL)) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SIO_KEEPALIVE_VALS (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"socket SO_KEEPALIVE set to %d seconds",
ka.keepalivetime / 1000);
#else
set = 1;
if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (const char*)&set,
sizeof(set))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SO_KEEPALIVE (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"socket SO_KEEPALIVE set to On");
#endif
}
if (sock_buf > 0) {
set = sock_buf;
/* Set socket send buffer size */
if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (const char*)&set,
sizeof(set))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SO_SNDBUF (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
set = sock_buf;
/* Set socket receive buffer size */
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (const char*)&set,
sizeof(set))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SO_RCVBUF (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"socket SO_SNDBUF and SO_RCVBUF set to %d",
sock_buf);
}
if (timeout > 0) {
#if defined(WIN32)
int tmout = timeout * 1000;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO,
(const char *) &tmout, sizeof(int));
setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO,
(const char *) &tmout, sizeof(int));
JK_GET_SOCKET_ERRNO();
#elif defined(SO_RCVTIMEO) && defined(USE_SO_RCVTIMEO) && defined(SO_SNDTIMEO) && defined(USE_SO_SNDTIMEO)
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO,
(const void *) &tv, sizeof(tv));
setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO,
(const void *) &tv, sizeof(tv));
#endif
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"timeout %d set for socket=%d",
timeout, sd);
}
#ifdef SO_NOSIGPIPE
/* The preferred method on Mac OS X (10.2 and later) to prevent SIGPIPEs when
* sending data to a dead peer. Possibly also existing and in use on other BSD
* systems?
*/
set = 1;
if (setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set,
sizeof(int))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SO_NOSIGPIPE (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
#endif
#ifdef SO_LINGER
/* Make hard closesocket by disabling lingering */
li.l_linger = li.l_onoff = 0;
if (setsockopt(sd, SOL_SOCKET, SO_LINGER, (const char*)&li,
sizeof(li))) {
JK_GET_SOCKET_ERRNO();
jk_log(l, JK_LOG_ERROR,
"failed setting SO_LINGER (errno=%d)", errno);
jk_close_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_INVALID_SOCKET;
}
#endif
/* Tries to connect to Tomcat (continues trying while error is EINTR) */
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"trying to connect socket %d to %s", sd,
jk_dump_hinfo(addr, buf, sizeof(buf)));
/* Need more infos for BSD 4.4 and Unix 98 defines, for now only
iSeries when Unix98 is required at compile time */
#if (_XOPEN_SOURCE >= 520) && defined(AS400)
((struct sockaddr *)addr)->sa.sin.sa_len = sizeof(struct sockaddr_in);
#endif
ret = nb_connect(sd, addr, source, connect_timeout, l);
#if defined(WIN32)
if (JK_IS_SOCKET_ERROR(ret)) {
JK_GET_SOCKET_ERRNO();
}
#endif /* WIN32 */
/* Check if we are connected */
if (ret) {
jk_log(l, JK_LOG_INFO,
"connect to %s failed (errno=%d)",
jk_dump_hinfo(addr, buf, sizeof(buf)), errno);
jk_close_socket(sd, l);
sd = JK_INVALID_SOCKET;
}
else {
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG, "socket %d [%s] connected",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)));
}
JK_TRACE_EXIT(l);
return sd;
}
/** Close the socket
* @param sd socket to close
* @param l logger
* @return -1: some kind of error occured (!WIN32)
* SOCKET_ERROR: some kind of error occured (WIN32)
* 0: success
* @remark Does not change errno
*/
int jk_close_socket(jk_sock_t sd, jk_logger_t *l)
{
int rc;
int save_errno;
JK_TRACE_ENTER(l);
if (!IS_VALID_SOCKET(sd)) {
JK_TRACE_EXIT(l);
return -1;
}
save_errno = errno;
#if defined(WIN32)
rc = closesocket(sd) ? -1 : 0;
#else
do {
rc = close(sd);
} while (JK_IS_SOCKET_ERROR(rc) && (errno == EINTR || errno == EAGAIN));
#endif
JK_TRACE_EXIT(l);
errno = save_errno;
return rc;
}
#ifndef MAX_SECS_TO_LINGER
#define MAX_SECS_TO_LINGER 2
#endif
#ifndef MS_TO_LINGER
#define MS_TO_LINGER 100
#endif
#ifndef MS_TO_LINGER_LAST
#define MS_TO_LINGER_LAST 20
#endif
#ifndef MAX_READ_RETRY
#define MAX_READ_RETRY 10
#endif
#ifndef MAX_LINGER_BYTES
#define MAX_LINGER_BYTES 32768
#endif
#ifndef SHUT_WR
#ifdef SD_SEND
#define SHUT_WR SD_SEND
#else
#define SHUT_WR 0x01
#endif
#endif
#ifndef SHUT_RD
#ifdef SD_RECEIVE
#define SHUT_RD SD_RECEIVE
#else
#define SHUT_RD 0x00
#endif
#endif
/** Drain and close the socket
* @param sd socket to close
* @param l logger
* @return -1: socket to close is invalid
* -1: some kind of error occured (!WIN32)
* SOCKET_ERROR: some kind of error occured (WIN32)
* 0: success
* @remark Does not change errno
*/
int jk_shutdown_socket(jk_sock_t sd, jk_logger_t *l)
{
char dummy[512];
char buf[DUMP_SINFO_BUF_SZ];
char *sb = NULL;
int rc = 0;
size_t rd = 0;
size_t rp = 0;
int save_errno;
int timeout = MS_TO_LINGER;
time_t start = time(NULL);
JK_TRACE_ENTER(l);
if (!IS_VALID_SOCKET(sd)) {
JK_TRACE_EXIT(l);
return -1;
}
save_errno = errno;
if (JK_IS_DEBUG_LEVEL(l)) {
sb = jk_dump_sinfo(sd, buf, sizeof(buf));
jk_log(l, JK_LOG_DEBUG, "About to shutdown socket %d [%s]",
sd, sb);
}
/* Shut down the socket for write, which will send a FIN
* to the peer.
*/
if (shutdown(sd, SHUT_WR)) {
rc = jk_close_socket(sd, l);
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"Failed sending SHUT_WR for socket %d [%s]",
sd, sb);
errno = save_errno;
JK_TRACE_EXIT(l);
return rc;
}
do {
rp = 0;
if (jk_is_input_event(sd, timeout, l)) {
/* Do a restartable read on the socket
* draining out all the data currently in the socket buffer.
*/
int num = 0;
do {
num++;
#if defined(WIN32)
rc = recv(sd, &dummy[0], sizeof(dummy), 0);
if (JK_IS_SOCKET_ERROR(rc))
JK_GET_SOCKET_ERRNO();
#else
rc = read(sd, &dummy[0], sizeof(dummy));
#endif
if (rc > 0)
rp += rc;
} while (JK_IS_SOCKET_ERROR(rc) && (errno == EINTR || errno == EAGAIN) && num < MAX_READ_RETRY);
if (rc < 0) {
/* Read failed.
* Bail out from the loop.
*/
break;
}
}
else {
/* Error or timeout (reason is logged within jk_is_input_event)
* Exit the drain loop
*/
break;
}
rd += rp;
if (rp < sizeof(dummy)) {
if (timeout > MS_TO_LINGER_LAST) {
/* Try one last time with a short timeout
*/
timeout = MS_TO_LINGER_LAST;
continue;
}
/* We have read less then size of buffer
* It's a good chance there will be no more data
* to read.
*/
if ((rc = sononblock(sd))) {
rc = jk_close_socket(sd, l);
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"error setting socket %d [%s] to nonblocking",
sd, sb);
errno = save_errno;
JK_TRACE_EXIT(l);
return rc;
}
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"shutting down the read side of socket %d [%s]",
sd, sb);
shutdown(sd, SHUT_RD);
break;
}
timeout = MS_TO_LINGER;
} while ((rd < MAX_LINGER_BYTES) && (difftime(time(NULL), start) < MAX_SECS_TO_LINGER));
rc = jk_close_socket(sd, l);
if (JK_IS_DEBUG_LEVEL(l))
jk_log(l, JK_LOG_DEBUG,
"Shutdown socket %d [%s] and read %d lingering bytes in %d sec.",
sd, sb, rd, (int)difftime(time(NULL), start));
errno = save_errno;
JK_TRACE_EXIT(l);
return rc;
}
/** send a message
* @param sd socket to use
* @param b buffer containing the data
* @param len length to send
* @param l logger
* @return negative errno: write returns a fatal -1 (!WIN32)
* negative pseudo errno: send returns SOCKET_ERROR (WIN32)
* JK_SOCKET_EOF: no bytes could be sent
* >0: success, provided number of bytes send
* @remark Always closes socket in case of error
* @remark Cares about errno
* @bug this fails on Unixes if len is too big for the underlying
* protocol
*/
int jk_tcp_socket_sendfull(jk_sock_t sd, const unsigned char *b, int len, jk_logger_t *l)
{
int sent = 0;
int wr;
JK_TRACE_ENTER(l);
errno = 0;
while (sent < len) {
do {
#if defined(WIN32)
wr = send(sd, (const char*)(b + sent),
len - sent, 0);
if (JK_IS_SOCKET_ERROR(wr))
JK_GET_SOCKET_ERRNO();
#else
wr = write(sd, b + sent, len - sent);
#endif
} while (JK_IS_SOCKET_ERROR(wr) && (errno == EINTR || errno == EAGAIN));
if (JK_IS_SOCKET_ERROR(wr)) {
int err;
jk_shutdown_socket(sd, l);
err = (errno > 0) ? -errno : errno;
JK_TRACE_EXIT(l);
return err;
}
else if (wr == 0) {
jk_shutdown_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_SOCKET_EOF;
}
sent += wr;
}
JK_TRACE_EXIT(l);
return sent;
}
/** receive a message
* @param sd socket to use
* @param b buffer to store the data
* @param len length to receive
* @param l logger
* @return negative errno: read returns a fatal -1 (!WIN32)
* negative pseudo errno: recv returns SOCKET_ERROR (WIN32)
* JK_SOCKET_EOF: no bytes could be read
* >0: success, requested number of bytes received
* @remark Always closes socket in case of error
* @remark Cares about errno
*/
int jk_tcp_socket_recvfull(jk_sock_t sd, unsigned char *b, int len, jk_logger_t *l)
{
int rdlen = 0;
int rd;
JK_TRACE_ENTER(l);
errno = 0;
while (rdlen < len) {
do {
#if defined(WIN32)
rd = recv(sd, (char *)b + rdlen,
len - rdlen, 0);
if (JK_IS_SOCKET_ERROR(rd))
JK_GET_SOCKET_ERRNO();
#else
rd = read(sd, (char *)b + rdlen, len - rdlen);
#endif
} while (JK_IS_SOCKET_ERROR(rd) && errno == EINTR);
if (JK_IS_SOCKET_ERROR(rd)) {
int err = (errno > 0) ? -errno : errno;
jk_shutdown_socket(sd, l);
JK_TRACE_EXIT(l);
return (err == 0) ? JK_SOCKET_EOF : err;
}
else if (rd == 0) {
jk_shutdown_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_SOCKET_EOF;
}
rdlen += rd;
}
JK_TRACE_EXIT(l);
return rdlen;
}
/* const char *
* inet_ntop4(src, dst, size)
* format an IPv4 address, more or less like inet_ntoa()
* return:
* `dst' (as a const)
* notes:
* (1) uses no statics
* (2) takes a u_char* not an in_addr as input
* author:
* Paul Vixie, 1996.
*/
static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size)
{
const size_t MIN_SIZE = 16; /* space for 255.255.255.255\0 */
int n = 0;
char *next = dst;
if (size < MIN_SIZE) {
errno = ENOSPC;
return NULL;
}
do {
unsigned char u = *src++;
if (u > 99) {
*next++ = '0' + u/100;
u %= 100;
*next++ = '0' + u/10;
u %= 10;
}
else if (u > 9) {
*next++ = '0' + u/10;
u %= 10;
}
*next++ = '0' + u;
*next++ = '.';
n++;
} while (n < 4);
*--next = '\0';
return dst;
}
#if JK_HAVE_IPV6
/* const char *
* inet_ntop6(src, dst, size)
* convert IPv6 binary address into presentation (printable) format
* author:
* Paul Vixie, 1996.
*/
static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
char tmp[INET6_ADDRSTRLEN], *tp;
struct { int base, len; } best = {-1, 0}, cur = {-1, 0};
unsigned int words[IN6ADDRSZ / INT16SZ];
int i;
const unsigned char *next_src, *src_end;
unsigned int *next_dest;
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
next_src = src;
src_end = src + IN6ADDRSZ;
next_dest = words;
i = 0;
do {
unsigned int next_word = (unsigned int)*next_src++;
next_word <<= 8;
next_word |= (unsigned int)*next_src++;
*next_dest++ = next_word;
if (next_word == 0) {
if (cur.base == -1) {
cur.base = i;
cur.len = 1;
}
else {
cur.len++;
}
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len) {
best = cur;
}
cur.base = -1;
}
}
i++;
} while (next_src < src_end);
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len) {
best = cur;
}
}
if (best.base != -1 && best.len < 2) {
best.base = -1;
}
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (IN6ADDRSZ / INT16SZ);) {
/* Are we inside the best run of 0x00's? */
if (i == best.base) {
*tp++ = ':';
i += best.len;
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0) {
*tp++ = ':';
}
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) {
return (NULL);
}
tp += strlen(tp);
break;
}
tp += sprintf(tp, "%x", words[i]);
i++;
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
*tp++ = ':';
}
*tp++ = '\0';
/*
* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size) {
errno = ENOSPC;
return NULL;
}
strcpy(dst, tmp);
return dst;
}
#endif
/**
* dump a jk_sockaddr_t in A.B.C.D:P in ASCII buffer
*
*/
char *jk_dump_hinfo(jk_sockaddr_t *saddr, char *buf, size_t size)
{
char pb[8];
if (saddr->ipaddr_ptr == NULL) {
strcpy(buf, "UnresolvedIP");
} else {
if (saddr->family == JK_INET) {
inet_ntop4(saddr->ipaddr_ptr, buf, size);
}
#if JK_HAVE_IPV6
else {
inet_ntop6(saddr->ipaddr_ptr, buf, size);
}
#endif
}
sprintf(pb, ":%d", saddr->port);
strncat(buf, pb, size - strlen(buf) - 1);
return buf;
}
char *jk_dump_sinfo(jk_sock_t sd, char *buf, size_t size)
{
struct sockaddr rsaddr;
struct sockaddr lsaddr;
socklen_t salen;
salen = sizeof(struct sockaddr);
if (getsockname(sd, &lsaddr, &salen) == 0) {
salen = sizeof(struct sockaddr);
if (getpeername(sd, &rsaddr, &salen) == 0) {
char pb[8];
size_t ps;
if (lsaddr.sa_family == JK_INET) {
struct sockaddr_in *sa = (struct sockaddr_in *)&lsaddr;
inet_ntop4((unsigned char *)&sa->sin_addr, buf, size);
sprintf(pb, ":%d", (unsigned int)htons(sa->sin_port));
}
#if JK_HAVE_IPV6
else {
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&lsaddr;
inet_ntop6((unsigned char *)&sa->sin6_addr, buf, size);
sprintf(pb, ":%d", (unsigned int)htons(sa->sin6_port));
}
#endif
ps = strlen(buf);
strncat(buf, pb, size - ps - 1);
ps = strlen(buf);
strncat(buf, " -> ", size - ps - 1);
ps = strlen(buf);
if (rsaddr.sa_family == JK_INET) {
struct sockaddr_in *sa = (struct sockaddr_in *)&rsaddr;
inet_ntop4((unsigned char *)&sa->sin_addr, buf + ps, size - ps);
sprintf(pb, ":%d", (unsigned int)htons(sa->sin_port));
}
#if JK_HAVE_IPV6
else {
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&rsaddr;
inet_ntop6((unsigned char *)&sa->sin6_addr, buf + ps, size - ps);
sprintf(pb, ":%d", (unsigned int)htons(sa->sin6_port));
}
#endif
strncat(buf, pb, size - strlen(buf) - 1);
return buf;
}
}
snprintf(buf, size, "errno=%d", errno);
return buf;
}
/** Wait for input event on socket until timeout
* @param sd socket to use
* @param timeout wait timeout in milliseconds
* @param l logger
* @return JK_FALSE: Timeout expired without something to read
* JK_FALSE: Error during waiting
* JK_TRUE: success
* @remark Does not close socket in case of error
* to allow for iterative waiting
* @remark Cares about errno
*/
#ifdef HAVE_POLL
int jk_is_input_event(jk_sock_t sd, int timeout, jk_logger_t *l)
{
struct pollfd fds;
int rc;
int save_errno;
char buf[DUMP_SINFO_BUF_SZ];
JK_TRACE_ENTER(l);
errno = 0;
fds.fd = sd;
fds.events = POLLIN;
fds.revents = 0;
do {
rc = poll(&fds, 1, timeout);
} while (rc < 0 && errno == EINTR);
if (rc == 0) {
if (JK_IS_DEBUG_LEVEL(l)) {
jk_log(l, JK_LOG_DEBUG,
"timeout during poll on socket %d [%s] (timeout=%d)",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)), timeout);
}
/* Timeout. Set the errno to timeout */
errno = ETIMEDOUT;
JK_TRACE_EXIT(l);
return JK_FALSE;
}
else if (rc < 0) {
save_errno = errno;
if (JK_IS_DEBUG_LEVEL(l)) {
jk_log(l, JK_LOG_DEBUG,
"error during poll on socket %d [%s] (errno=%d)",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)), errno);
}
errno = save_errno;
JK_TRACE_EXIT(l);
return JK_FALSE;
}
if ((fds.revents & (POLLERR | POLLHUP))) {
save_errno = fds.revents & (POLLERR | POLLHUP);
if (JK_IS_DEBUG_LEVEL(l)) {
jk_log(l, JK_LOG_DEBUG,
"error event during poll on socket %d [%s] (event=%d)",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)), save_errno);
}
errno = save_errno;
JK_TRACE_EXIT(l);
return JK_FALSE;
}
errno = 0;
JK_TRACE_EXIT(l);
return JK_TRUE;
}
#else
int jk_is_input_event(jk_sock_t sd, int timeout, jk_logger_t *l)
{
fd_set rset;
struct timeval tv;
int rc;
int save_errno;
char buf[DUMP_SINFO_BUF_SZ];
JK_TRACE_ENTER(l);
errno = 0;
FD_ZERO(&rset);
FD_SET(sd, &rset);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
do {
rc = select((int)sd + 1, &rset, NULL, NULL, &tv);
} while (rc < 0 && errno == EINTR);
if (rc == 0) {
if (JK_IS_DEBUG_LEVEL(l)) {
jk_log(l, JK_LOG_DEBUG,
"timeout during select on socket %d [%s] (timeout=%d)",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)), timeout);
}
/* Timeout. Set the errno to timeout */
#if defined(WIN32)
errno = WSAETIMEDOUT - WSABASEERR;
#else
errno = ETIMEDOUT;
#endif
JK_TRACE_EXIT(l);
return JK_FALSE;
}
else if (rc < 0) {
save_errno = errno;
if (JK_IS_DEBUG_LEVEL(l)) {
jk_log(l, JK_LOG_DEBUG,
"error during select on socket %d [%s] (errno=%d)",
sd, jk_dump_sinfo(sd, buf, sizeof(buf)), errno);
}
errno = save_errno;
JK_TRACE_EXIT(l);
return JK_FALSE;
}
errno = 0;
JK_TRACE_EXIT(l);
return JK_TRUE;
}
#endif
/** Test if a socket is still connected
* @param sd socket to use
* @param l logger
* @return JK_FALSE: failure
* JK_TRUE: success
* @remark Always closes socket in case of error
* @remark Cares about errno
*/
#ifdef HAVE_POLL
int jk_is_socket_connected(jk_sock_t sd, jk_logger_t *l)
{
struct pollfd fds;
int rc;
JK_TRACE_ENTER(l);
errno = 0;
fds.fd = sd;
fds.events = POLLIN;
do {
rc = poll(&fds, 1, 0);
} while (rc < 0 && errno == EINTR);
if (rc == 0) {
/* If we get a timeout, then we are still connected */
JK_TRACE_EXIT(l);
return JK_TRUE;
}
else if (rc == 1 && fds.revents == POLLIN) {
char buf;
do {
rc = (int)recvfrom(sd, &buf, 1, MSG_PEEK, NULL, NULL);
} while (rc < 0 && errno == EINTR);
if (rc == 1) {
/* There is at least one byte to read. */
JK_TRACE_EXIT(l);
return JK_TRUE;
}
}
jk_shutdown_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
#else
int jk_is_socket_connected(jk_sock_t sd, jk_logger_t *l)
{
fd_set fd;
struct timeval tv;
int rc;
JK_TRACE_ENTER(l);
errno = 0;
FD_ZERO(&fd);
FD_SET(sd, &fd);
/* Initially test the socket without any blocking.
*/
tv.tv_sec = 0;
tv.tv_usec = 0;
do {
rc = select((int)sd + 1, &fd, NULL, NULL, &tv);
JK_GET_SOCKET_ERRNO();
/* Wait one microsecond on next select, if EINTR */
tv.tv_sec = 0;
tv.tv_usec = 1;
} while (JK_IS_SOCKET_ERROR(rc) && errno == EINTR);
errno = 0;
if (rc == 0) {
/* If we get a timeout, then we are still connected */
JK_TRACE_EXIT(l);
return JK_TRUE;
}
else if (rc == 1) {
#if defined(WIN32)
u_long nr;
rc = ioctlsocket(sd, FIONREAD, &nr);
if (rc == 0) {
if (WSAGetLastError() == 0)
errno = 0;
else
JK_GET_SOCKET_ERRNO();
}
#else
int nr;
rc = ioctl(sd, FIONREAD, (void*)&nr);
#endif
if (rc == 0 && nr != 0) {
JK_TRACE_EXIT(l);
return JK_TRUE;
}
JK_GET_SOCKET_ERRNO();
}
jk_shutdown_socket(sd, l);
JK_TRACE_EXIT(l);
return JK_FALSE;
}
#endif
|