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
|
/*
* testcode/delayer.c - debug program that delays queries to a server.
*
* Copyright (c) 2008, NLnet Labs. All rights reserved.
*
* This software is open source.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the NLNET LABS nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
*
* This program delays queries made. It performs as a proxy to another
* server and delays queries to it.
*/
#include "config.h"
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <sys/time.h>
#include "util/net_help.h"
#include "util/config_file.h"
#include <signal.h>
/** number of reads per select for delayer */
#define TRIES_PER_SELECT 100
/**
* The ring buffer
*/
struct ringbuf {
/** base of buffer */
uint8_t* buf;
/** size of buffer */
size_t size;
/** low mark, items start here */
size_t low;
/** high mark, items end here */
size_t high;
};
/**
* List of proxy fds that return replies from the server to our clients.
*/
struct proxy {
/** the fd to listen for replies from server */
int s;
/** last time this was used */
struct timeval lastuse;
/** remote address */
struct sockaddr_storage addr;
/** length of addr */
socklen_t addr_len;
/** number of queries waiting (in total) */
size_t numwait;
/** number of queries sent to server (in total) */
size_t numsent;
/** numberof answers returned to client (in total) */
size_t numreturn;
/** how many times repurposed */
size_t numreuse;
/** next in proxylist */
struct proxy* next;
};
/**
* An item that has to be TCP relayed
*/
struct tcp_send_list {
/** the data item */
uint8_t* item;
/** size of item */
size_t len;
/** time when the item can be transmitted on */
struct timeval wait;
/** how much of the item has already been transmitted */
size_t done;
/** next in list */
struct tcp_send_list* next;
};
/**
* List of TCP proxy fd pairs to TCP connect client to server
*/
struct tcp_proxy {
/** the fd to listen for client query */
int client_s;
/** the fd to listen for server answer */
int server_s;
/** remote client address */
struct sockaddr_storage addr;
/** length of address */
socklen_t addr_len;
/** timeout on this entry */
struct timeval timeout;
/** list of query items to send to server */
struct tcp_send_list* querylist;
/** last in query list */
struct tcp_send_list* querylast;
/** list of answer items to send to client */
struct tcp_send_list* answerlist;
/** last in answerlist */
struct tcp_send_list* answerlast;
/** next in list */
struct tcp_proxy* next;
};
/** usage information for delayer */
static void usage(char* argv[])
{
printf("usage: %s [options]\n", argv[0]);
printf(" -f addr : use addr, forward to that server, @port.\n");
printf(" -b addr : bind to this address to listen.\n");
printf(" -p port : bind to this port (use 0 for random).\n");
printf(" -m mem : use this much memory for waiting queries.\n");
printf(" -d delay: UDP queries are delayed n milliseconds.\n");
printf(" TCP is delayed twice (on send, on recv).\n");
printf(" -h : this help message\n");
exit(1);
}
/** timeval compare, t1 < t2 */
static int
dl_tv_smaller(struct timeval* t1, const struct timeval* t2)
{
#ifndef S_SPLINT_S
if(t1->tv_sec < t2->tv_sec)
return 1;
if(t1->tv_sec == t2->tv_sec &&
t1->tv_usec < t2->tv_usec)
return 1;
#endif
return 0;
}
/** timeval add, t1 += t2 */
static void
dl_tv_add(struct timeval* t1, const struct timeval* t2)
{
#ifndef S_SPLINT_S
t1->tv_sec += t2->tv_sec;
t1->tv_usec += t2->tv_usec;
while(t1->tv_usec > 1000000) {
t1->tv_usec -= 1000000;
t1->tv_sec++;
}
#endif
}
/** timeval subtract, t1 -= t2 */
static void
dl_tv_subtract(struct timeval* t1, const struct timeval* t2)
{
#ifndef S_SPLINT_S
t1->tv_sec -= t2->tv_sec;
if(t1->tv_usec >= t2->tv_usec) {
t1->tv_usec -= t2->tv_usec;
} else {
t1->tv_sec--;
t1->tv_usec = 1000000-(t2->tv_usec-t1->tv_usec);
}
#endif
}
/** create new ring buffer */
static struct ringbuf*
ring_create(size_t sz)
{
struct ringbuf* r = (struct ringbuf*)calloc(1, sizeof(*r));
if(!r) fatal_exit("out of memory");
r->buf = (uint8_t*)malloc(sz);
if(!r->buf) fatal_exit("out of memory");
r->size = sz;
r->low = 0;
r->high = 0;
return r;
}
/** delete ring buffer */
static void
ring_delete(struct ringbuf* r)
{
if(!r) return;
free(r->buf);
free(r);
}
/** add entry to ringbuffer */
static void
ring_add(struct ringbuf* r, ldns_buffer* pkt, struct timeval* now,
struct timeval* delay, struct proxy* p)
{
/* time -- proxy* -- 16bitlen -- message */
uint16_t len = (uint16_t)ldns_buffer_limit(pkt);
struct timeval when;
size_t needed;
uint8_t* where = NULL;
log_assert(ldns_buffer_limit(pkt) <= 65535);
needed = sizeof(when) + sizeof(p) + sizeof(len) + len;
/* put item into ringbuffer */
if(r->low < r->high) {
/* used part is in the middle */
if(r->size - r->high >= needed) {
where = r->buf + r->high;
r->high += needed;
} else if(r->low > needed) {
/* wrap around ringbuffer */
/* make sure r->low == r->high means empty */
/* so r->low == r->high cannot be used to signify
* a completely full ringbuf */
if(r->size - r->high > sizeof(when)+sizeof(p)) {
/* zero entry at end of buffer */
memset(r->buf+r->high, 0,
sizeof(when)+sizeof(p));
}
where = r->buf;
r->high = needed;
} else {
/* drop message */
log_warn("warning: mem full, dropped message");
return;
}
} else {
/* empty */
if(r->high == r->low) {
where = r->buf;
r->low = 0;
r->high = needed;
/* unused part is in the middle */
/* so ringbuffer has wrapped around */
} else if(r->low - r->high > needed) {
where = r->buf + r->high;
r->high += needed;
} else {
log_warn("warning: mem full, dropped message");
return;
}
}
when = *now;
dl_tv_add(&when, delay);
/* copy it at where part */
log_assert(where != NULL);
memmove(where, &when, sizeof(when));
memmove(where+sizeof(when), &p, sizeof(p));
memmove(where+sizeof(when)+sizeof(p), &len, sizeof(len));
memmove(where+sizeof(when)+sizeof(p)+sizeof(len),
ldns_buffer_begin(pkt), len);
}
/** see if the ringbuffer is empty */
static int
ring_empty(struct ringbuf* r)
{
return (r->low == r->high);
}
/** peek at timevalue for next item in ring */
static struct timeval*
ring_peek_time(struct ringbuf* r)
{
if(ring_empty(r))
return NULL;
return (struct timeval*)&r->buf[r->low];
}
/** get entry from ringbuffer */
static int
ring_pop(struct ringbuf* r, ldns_buffer* pkt, struct timeval* tv,
struct proxy** p)
{
/* time -- proxy* -- 16bitlen -- message */
uint16_t len;
uint8_t* where = NULL;
size_t done;
if(r->low == r->high)
return 0;
where = r->buf + r->low;
memmove(tv, where, sizeof(*tv));
memmove(p, where+sizeof(*tv), sizeof(*p));
memmove(&len, where+sizeof(*tv)+sizeof(*p), sizeof(len));
memmove(ldns_buffer_begin(pkt),
where+sizeof(*tv)+sizeof(*p)+sizeof(len), len);
ldns_buffer_set_limit(pkt, (size_t)len);
done = sizeof(*tv)+sizeof(*p)+sizeof(len)+len;
/* move lowmark */
if(r->low < r->high) {
/* used part in middle */
log_assert(r->high - r->low >= done);
r->low += done;
} else {
/* unused part in middle */
log_assert(r->size - r->low >= done);
r->low += done;
if(r->size - r->low > sizeof(*tv)+sizeof(*p)) {
/* see if it is zeroed; means end of buffer */
struct proxy* pz;
memmove(&pz, r->buf+r->low+sizeof(*tv), sizeof(pz));
if(pz == NULL)
r->low = 0;
} else r->low = 0;
}
if(r->low == r->high) {
r->low = 0; /* reset if empty */
r->high = 0;
}
return 1;
}
/** signal handler global info */
static volatile int do_quit = 0;
/** signal handler for user quit */
static RETSIGTYPE delayer_sigh(int sig)
{
printf("exit on signal %d\n", sig);
do_quit = 1;
}
/** send out waiting packets */
static void
service_send(struct ringbuf* ring, struct timeval* now, ldns_buffer* pkt,
struct sockaddr_storage* srv_addr, socklen_t srv_len)
{
struct proxy* p;
struct timeval tv;
ssize_t sent;
while(!ring_empty(ring) &&
dl_tv_smaller(ring_peek_time(ring), now)) {
/* this items needs to be sent out */
if(!ring_pop(ring, pkt, &tv, &p))
fatal_exit("ringbuf error: pop failed");
verbose(1, "send out query %d.%6.6d",
(unsigned)tv.tv_sec, (unsigned)tv.tv_usec);
log_addr(1, "from client", &p->addr, p->addr_len);
/* send it */
sent = sendto(p->s, (void*)ldns_buffer_begin(pkt),
ldns_buffer_limit(pkt), 0,
(struct sockaddr*)srv_addr, srv_len);
if(sent == -1) {
#ifndef USE_WINSOCK
log_err("sendto: %s", strerror(errno));
#else
log_err("sendto: %s", wsa_strerror(WSAGetLastError()));
#endif
} else if(sent != (ssize_t)ldns_buffer_limit(pkt)) {
log_err("sendto: partial send");
}
p->lastuse = *now;
p->numsent++;
}
}
/** do proxy for one readable client */
static void
do_proxy(struct proxy* p, int retsock, ldns_buffer* pkt)
{
int i;
ssize_t r;
for(i=0; i<TRIES_PER_SELECT; i++) {
r = recv(p->s, (void*)ldns_buffer_begin(pkt),
ldns_buffer_capacity(pkt), 0);
if(r == -1) {
#ifndef USE_WINSOCK
if(errno == EAGAIN || errno == EINTR)
return;
log_err("recv: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEINPROGRESS ||
WSAGetLastError() == WSAEWOULDBLOCK)
return;
log_err("recv: %s", wsa_strerror(WSAGetLastError()));
#endif
return;
}
ldns_buffer_set_limit(pkt, (size_t)r);
log_addr(1, "return reply to client", &p->addr, p->addr_len);
/* send reply back to the real client */
p->numreturn++;
r = sendto(retsock, (void*)ldns_buffer_begin(pkt), (size_t)r,
0, (struct sockaddr*)&p->addr, p->addr_len);
if(r == -1) {
#ifndef USE_WINSOCK
log_err("sendto: %s", strerror(errno));
#else
log_err("sendto: %s", wsa_strerror(WSAGetLastError()));
#endif
}
}
}
/** proxy return replies to clients */
static void
service_proxy(fd_set* rset, int retsock, struct proxy* proxies,
ldns_buffer* pkt, struct timeval* now)
{
struct proxy* p;
for(p = proxies; p; p = p->next) {
if(FD_ISSET(p->s, rset)) {
p->lastuse = *now;
do_proxy(p, retsock, pkt);
}
}
}
/** find or else create proxy for this remote client */
static struct proxy*
find_create_proxy(struct sockaddr_storage* from, socklen_t from_len,
fd_set* rorig, int* max, struct proxy** proxies, int serv_ip6,
struct timeval* now, struct timeval* reuse_timeout)
{
struct proxy* p;
struct timeval t;
for(p = *proxies; p; p = p->next) {
if(sockaddr_cmp(from, from_len, &p->addr, p->addr_len)==0)
return p;
}
/* possibly: reuse lapsed entries */
for(p = *proxies; p; p = p->next) {
if(p->numwait > p->numsent || p->numsent > p->numreturn)
continue;
t = *now;
dl_tv_subtract(&t, &p->lastuse);
if(dl_tv_smaller(&t, reuse_timeout))
continue;
/* yes! */
verbose(1, "reuse existing entry");
memmove(&p->addr, from, from_len);
p->addr_len = from_len;
p->numreuse++;
return p;
}
/* create new */
p = (struct proxy*)calloc(1, sizeof(*p));
if(!p) fatal_exit("out of memory");
p->s = socket(serv_ip6?AF_INET6:AF_INET, SOCK_DGRAM, 0);
if(p->s == -1) {
#ifndef USE_WINSOCK
fatal_exit("socket: %s", strerror(errno));
#else
fatal_exit("socket: %s", wsa_strerror(WSAGetLastError()));
#endif
}
fd_set_nonblock(p->s);
memmove(&p->addr, from, from_len);
p->addr_len = from_len;
p->next = *proxies;
*proxies = p;
FD_SET(FD_SET_T p->s, rorig);
if(p->s+1 > *max)
*max = p->s+1;
return p;
}
/** recv new waiting packets */
static void
service_recv(int s, struct ringbuf* ring, ldns_buffer* pkt,
fd_set* rorig, int* max, struct proxy** proxies,
struct sockaddr_storage* srv_addr, socklen_t srv_len,
struct timeval* now, struct timeval* delay, struct timeval* reuse)
{
int i;
struct sockaddr_storage from;
socklen_t from_len;
ssize_t len;
struct proxy* p;
for(i=0; i<TRIES_PER_SELECT; i++) {
from_len = (socklen_t)sizeof(from);
len = recvfrom(s, (void*)ldns_buffer_begin(pkt),
ldns_buffer_capacity(pkt), 0,
(struct sockaddr*)&from, &from_len);
if(len < 0) {
#ifndef USE_WINSOCK
if(errno == EAGAIN || errno == EINTR)
return;
fatal_exit("recvfrom: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEWOULDBLOCK ||
WSAGetLastError() == WSAEINPROGRESS)
return;
fatal_exit("recvfrom: %s",
wsa_strerror(WSAGetLastError()));
#endif
}
ldns_buffer_set_limit(pkt, (size_t)len);
/* find its proxy element */
p = find_create_proxy(&from, from_len, rorig, max, proxies,
addr_is_ip6(srv_addr, srv_len), now, reuse);
if(!p) fatal_exit("error: cannot find or create proxy");
p->lastuse = *now;
ring_add(ring, pkt, now, delay, p);
p->numwait++;
log_addr(1, "recv from client", &p->addr, p->addr_len);
}
}
/** delete tcp proxy */
static void
tcp_proxy_delete(struct tcp_proxy* p)
{
struct tcp_send_list* s, *sn;
if(!p)
return;
log_addr(1, "delete tcp proxy", &p->addr, p->addr_len);
s = p->querylist;
while(s) {
sn = s->next;
free(s->item);
free(s);
s = sn;
}
s = p->answerlist;
while(s) {
sn = s->next;
free(s->item);
free(s);
s = sn;
}
#ifndef USE_WINSOCK
close(p->client_s);
if(p->server_s != -1)
close(p->server_s);
#else
closesocket(p->client_s);
if(p->server_s != -1)
closesocket(p->server_s);
#endif
free(p);
}
/** accept new TCP connections, and set them up */
static void
service_tcp_listen(int s, fd_set* rorig, int* max, struct tcp_proxy** proxies,
struct sockaddr_storage* srv_addr, socklen_t srv_len,
struct timeval* now, struct timeval* tcp_timeout)
{
int newfd;
struct sockaddr_storage addr;
struct tcp_proxy* p;
socklen_t addr_len;
newfd = accept(s, (struct sockaddr*)&addr, &addr_len);
if(newfd == -1) {
#ifndef USE_WINSOCK
if(errno == EAGAIN || errno == EINTR)
return;
fatal_exit("accept: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEWOULDBLOCK ||
WSAGetLastError() == WSAEINPROGRESS ||
WSAGetLastError() == WSAECONNRESET)
return;
fatal_exit("accept: %s", wsa_strerror(WSAGetLastError()));
#endif
}
p = (struct tcp_proxy*)calloc(1, sizeof(*p));
if(!p) fatal_exit("out of memory");
memmove(&p->addr, &addr, addr_len);
p->addr_len = addr_len;
log_addr(1, "new tcp proxy", &p->addr, p->addr_len);
p->client_s = newfd;
p->server_s = socket(addr_is_ip6(srv_addr, srv_len)?AF_INET6:AF_INET,
SOCK_STREAM, 0);
if(p->server_s == -1) {
#ifndef USE_WINSOCK
fatal_exit("tcp socket: %s", strerror(errno));
#else
fatal_exit("tcp socket: %s", wsa_strerror(WSAGetLastError()));
#endif
}
fd_set_nonblock(p->client_s);
fd_set_nonblock(p->server_s);
if(connect(p->server_s, (struct sockaddr*)srv_addr, srv_len) == -1) {
#ifndef USE_WINSOCK
if(errno != EINPROGRESS) {
log_err("tcp connect: %s", strerror(errno));
close(p->server_s);
close(p->client_s);
#else
if(WSAGetLastError() != WSAEWOULDBLOCK &&
WSAGetLastError() != WSAEINPROGRESS) {
log_err("tcp connect: %s",
wsa_strerror(WSAGetLastError()));
closesocket(p->server_s);
closesocket(p->client_s);
#endif
free(p);
return;
}
}
p->timeout = *now;
dl_tv_add(&p->timeout, tcp_timeout);
/* listen to client and server */
FD_SET(FD_SET_T p->client_s, rorig);
FD_SET(FD_SET_T p->server_s, rorig);
if(p->client_s+1 > *max)
*max = p->client_s+1;
if(p->server_s+1 > *max)
*max = p->server_s+1;
/* add into proxy list */
p->next = *proxies;
*proxies = p;
}
/** relay TCP, read a part */
static int
tcp_relay_read(int s, struct tcp_send_list** first,
struct tcp_send_list** last, struct timeval* now,
struct timeval* delay, ldns_buffer* pkt)
{
struct tcp_send_list* item;
ssize_t r = recv(s, (void*)ldns_buffer_begin(pkt),
ldns_buffer_capacity(pkt), 0);
if(r == -1) {
#ifndef USE_WINSOCK
if(errno == EINTR || errno == EAGAIN)
return 1;
log_err("tcp read: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEINPROGRESS ||
WSAGetLastError() == WSAEWOULDBLOCK)
return 1;
log_err("tcp read: %s", wsa_strerror(WSAGetLastError()));
#endif
return 0;
} else if(r == 0) {
/* connection closed */
return 0;
}
item = (struct tcp_send_list*)malloc(sizeof(*item));
if(!item) {
log_err("out of memory");
return 0;
}
verbose(1, "read item len %d", (int)r);
item->len = (size_t)r;
item->item = memdup(ldns_buffer_begin(pkt), item->len);
if(!item->item) {
free(item);
log_err("out of memory");
return 0;
}
item->done = 0;
item->wait = *now;
dl_tv_add(&item->wait, delay);
item->next = NULL;
/* link in */
if(*first) {
(*last)->next = item;
} else {
*first = item;
}
*last = item;
return 1;
}
/** relay TCP, write a part */
static int
tcp_relay_write(int s, struct tcp_send_list** first,
struct tcp_send_list** last, struct timeval* now)
{
ssize_t r;
struct tcp_send_list* p;
while(*first) {
p = *first;
/* is the item ready? */
if(!dl_tv_smaller(&p->wait, now))
return 1;
/* write it */
r = send(s, (void*)(p->item + p->done), p->len - p->done, 0);
if(r == -1) {
#ifndef USE_WINSOCK
if(errno == EAGAIN || errno == EINTR)
return 1;
log_err("tcp write: %s", strerror(errno));
#else
if(WSAGetLastError() == WSAEWOULDBLOCK ||
WSAGetLastError() == WSAEINPROGRESS)
return 1;
log_err("tcp write: %s",
wsa_strerror(WSAGetLastError()));
#endif
return 0;
} else if(r == 0) {
/* closed */
return 0;
}
/* account it */
p->done += (size_t)r;
verbose(1, "write item %d of %d", (int)p->done, (int)p->len);
if(p->done >= p->len) {
free(p->item);
*first = p->next;
if(!*first)
*last = NULL;
free(p);
} else {
/* partial write */
return 1;
}
}
return 1;
}
/** perform TCP relaying */
static void
service_tcp_relay(struct tcp_proxy** tcp_proxies, struct timeval* now,
struct timeval* delay, struct timeval* tcp_timeout, ldns_buffer* pkt,
fd_set* rset, fd_set* rorig, fd_set* worig)
{
struct tcp_proxy* p, **prev;
struct timeval tout;
int delete_it;
p = *tcp_proxies;
prev = tcp_proxies;
tout = *now;
dl_tv_add(&tout, tcp_timeout);
while(p) {
delete_it = 0;
/* can we receive further queries? */
if(!delete_it && FD_ISSET(p->client_s, rset)) {
p->timeout = tout;
log_addr(1, "read tcp query", &p->addr, p->addr_len);
if(!tcp_relay_read(p->client_s, &p->querylist,
&p->querylast, now, delay, pkt))
delete_it = 1;
}
/* can we receive further answers? */
if(!delete_it && p->server_s != -1 &&
FD_ISSET(p->server_s, rset)) {
p->timeout = tout;
log_addr(1, "read tcp answer", &p->addr, p->addr_len);
if(!tcp_relay_read(p->server_s, &p->answerlist,
&p->answerlast, now, delay, pkt)) {
#ifndef USE_WINSOCK
close(p->server_s);
#else
closesocket(p->server_s);
#endif
FD_CLR(FD_SET_T p->server_s, worig);
FD_CLR(FD_SET_T p->server_s, rorig);
p->server_s = -1;
}
}
/* can we send on further queries */
if(!delete_it && p->querylist && p->server_s != -1) {
p->timeout = tout;
if(dl_tv_smaller(&p->querylist->wait, now))
log_addr(1, "write tcp query",
&p->addr, p->addr_len);
if(!tcp_relay_write(p->server_s, &p->querylist,
&p->querylast, now))
delete_it = 1;
if(p->querylist && p->server_s != -1 &&
dl_tv_smaller(&p->querylist->wait, now))
FD_SET(FD_SET_T p->server_s, worig);
else FD_CLR(FD_SET_T p->server_s, worig);
}
/* can we send on further answers */
if(!delete_it && p->answerlist) {
p->timeout = tout;
if(dl_tv_smaller(&p->answerlist->wait, now))
log_addr(1, "write tcp answer",
&p->addr, p->addr_len);
if(!tcp_relay_write(p->client_s, &p->answerlist,
&p->answerlast, now))
delete_it = 1;
if(p->answerlist && dl_tv_smaller(&p->answerlist->wait,
now))
FD_SET(FD_SET_T p->client_s, worig);
else FD_CLR(FD_SET_T p->client_s, worig);
if(!p->answerlist && p->server_s == -1)
delete_it = 1;
}
/* does this entry timeout? (unused too long) */
if(dl_tv_smaller(&p->timeout, now)) {
delete_it = 1;
}
if(delete_it) {
struct tcp_proxy* np = p->next;
*prev = np;
FD_CLR(FD_SET_T p->client_s, rorig);
FD_CLR(FD_SET_T p->client_s, worig);
if(p->server_s != -1) {
FD_CLR(FD_SET_T p->server_s, rorig);
FD_CLR(FD_SET_T p->server_s, worig);
}
tcp_proxy_delete(p);
p = np;
continue;
}
prev = &p->next;
p = p->next;
}
}
/** find waiting time */
static int
service_findwait(struct timeval* now, struct timeval* wait,
struct ringbuf* ring, struct tcp_proxy* tcplist)
{
/* first item is the time to wait */
struct timeval* peek = ring_peek_time(ring);
struct timeval tcv;
int have_tcpval = 0;
struct tcp_proxy* p;
/* also for TCP list the first in sendlists is the time to wait */
for(p=tcplist; p; p=p->next) {
if(!have_tcpval)
tcv = p->timeout;
have_tcpval = 1;
if(dl_tv_smaller(&p->timeout, &tcv))
tcv = p->timeout;
if(p->querylist && dl_tv_smaller(&p->querylist->wait, &tcv))
tcv = p->querylist->wait;
if(p->answerlist && dl_tv_smaller(&p->answerlist->wait, &tcv))
tcv = p->answerlist->wait;
}
if(peek) {
/* peek can be unaligned */
/* use wait as a temp variable */
memmove(wait, peek, sizeof(*wait));
if(!have_tcpval)
tcv = *wait;
else if(dl_tv_smaller(wait, &tcv))
tcv = *wait;
have_tcpval = 1;
}
if(have_tcpval) {
*wait = tcv;
dl_tv_subtract(wait, now);
return 1;
}
/* nothing, block */
return 0;
}
/** clear proxy list */
static void
proxy_list_clear(struct proxy* p)
{
char from[109];
struct proxy* np;
int i=0, port;
while(p) {
np = p->next;
port = (int)ntohs(((struct sockaddr_in*)&p->addr)->sin_port);
if(addr_is_ip6(&p->addr, p->addr_len)) {
if(inet_ntop(AF_INET6,
&((struct sockaddr_in6*)&p->addr)->sin6_addr,
from, (socklen_t)sizeof(from)) == 0)
strncpy(from, "err", sizeof(from));
} else {
if(inet_ntop(AF_INET,
&((struct sockaddr_in*)&p->addr)->sin_addr,
from, (socklen_t)sizeof(from)) == 0)
strncpy(from, "err", sizeof(from));
}
printf("client[%d]: last %s@%d of %d : %u in, %u out, "
"%u returned\n", i++, from, port, (int)p->numreuse+1,
(unsigned)p->numwait, (unsigned)p->numsent,
(unsigned)p->numreturn);
#ifndef USE_WINSOCK
close(p->s);
#else
closesocket(p->s);
#endif
free(p);
p = np;
}
}
/** clear TCP proxy list */
static void
tcp_proxy_list_clear(struct tcp_proxy* p)
{
struct tcp_proxy* np;
while(p) {
np = p->next;
tcp_proxy_delete(p);
p = np;
}
}
/** delayer service loop */
static void
service_loop(int udp_s, int listen_s, struct ringbuf* ring,
struct timeval* delay, struct timeval* reuse,
struct sockaddr_storage* srv_addr, socklen_t srv_len,
ldns_buffer* pkt)
{
fd_set rset, rorig;
fd_set wset, worig;
struct timeval now, wait;
int max, have_wait = 0;
struct proxy* proxies = NULL;
struct tcp_proxy* tcp_proxies = NULL;
struct timeval tcp_timeout;
tcp_timeout.tv_sec = 120;
tcp_timeout.tv_usec = 0;
#ifndef S_SPLINT_S
FD_ZERO(&rorig);
FD_ZERO(&worig);
FD_SET(FD_SET_T udp_s, &rorig);
FD_SET(FD_SET_T listen_s, &rorig);
#endif
max = udp_s + 1;
if(listen_s + 1 > max) max = listen_s + 1;
while(!do_quit) {
/* wait for events */
rset = rorig;
wset = worig;
if(have_wait)
verbose(1, "wait for %d.%6.6d",
(unsigned)wait.tv_sec, (unsigned)wait.tv_usec);
else verbose(1, "wait");
if(select(max, &rset, &wset, NULL, have_wait?&wait:NULL) < 0) {
if(errno == EAGAIN || errno == EINTR)
continue;
fatal_exit("select: %s", strerror(errno));
}
/* get current time */
if(gettimeofday(&now, NULL) < 0) {
if(errno == EAGAIN || errno == EINTR)
continue;
fatal_exit("gettimeofday: %s", strerror(errno));
}
verbose(1, "process at %u.%6.6u\n",
(unsigned)now.tv_sec, (unsigned)now.tv_usec);
/* sendout delayed queries to master server (frees up buffer)*/
service_send(ring, &now, pkt, srv_addr, srv_len);
/* proxy return replies */
service_proxy(&rset, udp_s, proxies, pkt, &now);
/* see what can be received to start waiting */
service_recv(udp_s, ring, pkt, &rorig, &max, &proxies,
srv_addr, srv_len, &now, delay, reuse);
/* see if there are new tcp connections */
service_tcp_listen(listen_s, &rorig, &max, &tcp_proxies,
srv_addr, srv_len, &now, &tcp_timeout);
/* service tcp connections */
service_tcp_relay(&tcp_proxies, &now, delay, &tcp_timeout,
pkt, &rset, &rorig, &worig);
/* see what next timeout is (if any) */
have_wait = service_findwait(&now, &wait, ring, tcp_proxies);
}
proxy_list_clear(proxies);
tcp_proxy_list_clear(tcp_proxies);
}
/** delayer main service routine */
static void
service(const char* bind_str, int bindport, const char* serv_str,
size_t memsize, int delay_msec)
{
struct sockaddr_storage bind_addr, srv_addr;
socklen_t bind_len, srv_len;
struct ringbuf* ring = ring_create(memsize);
struct timeval delay, reuse;
ldns_buffer* pkt;
int i, s, listen_s;
#ifndef S_SPLINT_S
delay.tv_sec = delay_msec / 1000;
delay.tv_usec = (delay_msec % 1000)*1000;
#endif
reuse = delay; /* reuse is max(4*delay, 1 second) */
dl_tv_add(&reuse, &delay);
dl_tv_add(&reuse, &delay);
dl_tv_add(&reuse, &delay);
if(reuse.tv_sec == 0)
reuse.tv_sec = 1;
if(!extstrtoaddr(serv_str, &srv_addr, &srv_len)) {
printf("cannot parse forward address: %s\n", serv_str);
exit(1);
}
pkt = ldns_buffer_new(65535);
if(!pkt)
fatal_exit("out of memory");
if( signal(SIGINT, delayer_sigh) == SIG_ERR ||
#ifdef SIGHUP
signal(SIGHUP, delayer_sigh) == SIG_ERR ||
#endif
#ifdef SIGQUIT
signal(SIGQUIT, delayer_sigh) == SIG_ERR ||
#endif
#ifdef SIGBREAK
signal(SIGBREAK, delayer_sigh) == SIG_ERR ||
#endif
#ifdef SIGALRM
signal(SIGALRM, delayer_sigh) == SIG_ERR ||
#endif
signal(SIGTERM, delayer_sigh) == SIG_ERR)
fatal_exit("could not bind to signal");
/* bind UDP port */
if((s = socket(str_is_ip6(bind_str)?AF_INET6:AF_INET,
SOCK_DGRAM, 0)) == -1) {
#ifndef USE_WINSOCK
fatal_exit("socket: %s", strerror(errno));
#else
fatal_exit("socket: %s", wsa_strerror(WSAGetLastError()));
#endif
}
i=0;
if(bindport == 0) {
bindport = 1024 + random()%64000;
i = 100;
}
while(1) {
if(!ipstrtoaddr(bind_str, bindport, &bind_addr, &bind_len)) {
printf("cannot parse listen address: %s\n", bind_str);
exit(1);
}
if(bind(s, (struct sockaddr*)&bind_addr, bind_len) == -1) {
#ifndef USE_WINSOCK
log_err("bind: %s", strerror(errno));
#else
log_err("bind: %s", wsa_strerror(WSAGetLastError()));
#endif
if(i--==0)
fatal_exit("cannot bind any port");
bindport = 1024 + random()%64000;
} else break;
}
fd_set_nonblock(s);
/* and TCP port */
if((listen_s = socket(str_is_ip6(bind_str)?AF_INET6:AF_INET,
SOCK_STREAM, 0)) == -1) {
#ifndef USE_WINSOCK
fatal_exit("tcp socket: %s", strerror(errno));
#else
fatal_exit("tcp socket: %s", wsa_strerror(WSAGetLastError()));
#endif
}
#ifdef SO_REUSEADDR
if(1) {
int on = 1;
if(setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
(socklen_t)sizeof(on)) < 0)
#ifndef USE_WINSOCK
fatal_exit("setsockopt(.. SO_REUSEADDR ..) failed: %s",
strerror(errno));
#else
fatal_exit("setsockopt(.. SO_REUSEADDR ..) failed: %s",
wsa_strerror(WSAGetLastError()));
#endif
}
#endif
if(bind(listen_s, (struct sockaddr*)&bind_addr, bind_len) == -1) {
#ifndef USE_WINSOCK
fatal_exit("tcp bind: %s", strerror(errno));
#else
fatal_exit("tcp bind: %s", wsa_strerror(WSAGetLastError()));
#endif
}
if(listen(listen_s, 5) == -1) {
#ifndef USE_WINSOCK
fatal_exit("tcp listen: %s", strerror(errno));
#else
fatal_exit("tcp listen: %s", wsa_strerror(WSAGetLastError()));
#endif
}
fd_set_nonblock(listen_s);
printf("listening on port: %d\n", bindport);
/* process loop */
do_quit = 0;
service_loop(s, listen_s, ring, &delay, &reuse, &srv_addr, srv_len,
pkt);
/* cleanup */
verbose(1, "cleanup");
#ifndef USE_WINSOCK
close(s);
close(listen_s);
#else
closesocket(s);
closesocket(listen_s);
#endif
ldns_buffer_free(pkt);
ring_delete(ring);
}
/** getopt global, in case header files fail to declare it. */
extern int optind;
/** getopt global, in case header files fail to declare it. */
extern char* optarg;
/** main program for delayer */
int main(int argc, char** argv)
{
int c; /* defaults */
const char* server = "127.0.0.1@53";
const char* bindto = "0.0.0.0";
int bindport = 0;
size_t memsize = 10*1024*1024;
int delay = 100;
verbosity = 0;
log_init(0, 0, 0);
log_ident_set("delayer");
srandom(time(NULL) ^ getpid());
if(argc == 1) usage(argv);
while( (c=getopt(argc, argv, "b:d:f:hm:p:")) != -1) {
switch(c) {
case 'b':
bindto = optarg;
break;
case 'd':
if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) {
printf("bad delay: %s\n", optarg);
return 1;
}
delay = atoi(optarg);
break;
case 'f':
server = optarg;
break;
case 'm':
if(!cfg_parse_memsize(optarg, &memsize)) {
printf("bad memsize: %s\n", optarg);
return 1;
}
break;
case 'p':
if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) {
printf("bad port nr: %s\n", optarg);
return 1;
}
bindport = atoi(optarg);
break;
case 'h':
case '?':
default:
usage(argv);
}
}
argc -= optind;
argv += optind;
if(argc != 0)
usage(argv);
printf("bind to %s @ %d and forward to %s after %d msec\n",
bindto, bindport, server, delay);
service(bindto, bindport, server, memsize, delay);
return 0;
}
|