1 /* 2 * util/netevent.c - event notification 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains event notification functions. 40 */ 41 #include "config.h" 42 #include "util/netevent.h" 43 #include "util/ub_event.h" 44 #include "util/log.h" 45 #include "util/net_help.h" 46 #include "util/tcp_conn_limit.h" 47 #include "util/fptr_wlist.h" 48 #include "util/proxy_protocol.h" 49 #include "util/timeval_func.h" 50 #include "sldns/pkthdr.h" 51 #include "sldns/sbuffer.h" 52 #include "sldns/str2wire.h" 53 #include "dnstap/dnstap.h" 54 #include "dnscrypt/dnscrypt.h" 55 #include "services/listen_dnsport.h" 56 #ifdef HAVE_SYS_TYPES_H 57 #include <sys/types.h> 58 #endif 59 #ifdef HAVE_SYS_SOCKET_H 60 #include <sys/socket.h> 61 #endif 62 #ifdef HAVE_NETDB_H 63 #include <netdb.h> 64 #endif 65 #ifdef HAVE_POLL_H 66 #include <poll.h> 67 #endif 68 69 #ifdef HAVE_OPENSSL_SSL_H 70 #include <openssl/ssl.h> 71 #endif 72 #ifdef HAVE_OPENSSL_ERR_H 73 #include <openssl/err.h> 74 #endif 75 #ifdef HAVE_LINUX_NET_TSTAMP_H 76 #include <linux/net_tstamp.h> 77 #endif 78 /* -------- Start of local definitions -------- */ 79 /** if CMSG_ALIGN is not defined on this platform, a workaround */ 80 #ifndef CMSG_ALIGN 81 # ifdef __CMSG_ALIGN 82 # define CMSG_ALIGN(n) __CMSG_ALIGN(n) 83 # elif defined(CMSG_DATA_ALIGN) 84 # define CMSG_ALIGN _CMSG_DATA_ALIGN 85 # else 86 # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1)) 87 # endif 88 #endif 89 90 /** if CMSG_LEN is not defined on this platform, a workaround */ 91 #ifndef CMSG_LEN 92 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len)) 93 #endif 94 95 /** if CMSG_SPACE is not defined on this platform, a workaround */ 96 #ifndef CMSG_SPACE 97 # ifdef _CMSG_HDR_ALIGN 98 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr))) 99 # else 100 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr))) 101 # endif 102 #endif 103 104 /** The TCP writing query timeout in milliseconds */ 105 #define TCP_QUERY_TIMEOUT 120000 106 /** The minimum actual TCP timeout to use, regardless of what we advertise, 107 * in msec */ 108 #define TCP_QUERY_TIMEOUT_MINIMUM 200 109 110 #ifndef NONBLOCKING_IS_BROKEN 111 /** number of UDP reads to perform per read indication from select */ 112 #define NUM_UDP_PER_SELECT 100 113 #else 114 #define NUM_UDP_PER_SELECT 1 115 #endif 116 117 /** timeout in millisec to wait for write to unblock, packets dropped after.*/ 118 #define SEND_BLOCKED_WAIT_TIMEOUT 200 119 /** max number of times to wait for write to unblock, packets dropped after.*/ 120 #define SEND_BLOCKED_MAX_RETRY 5 121 122 /** Let's make timestamping code cleaner and redefine SO_TIMESTAMP* */ 123 #ifndef SO_TIMESTAMP 124 #define SO_TIMESTAMP 29 125 #endif 126 #ifndef SO_TIMESTAMPNS 127 #define SO_TIMESTAMPNS 35 128 #endif 129 #ifndef SO_TIMESTAMPING 130 #define SO_TIMESTAMPING 37 131 #endif 132 /** 133 * The internal event structure for keeping ub_event info for the event. 134 * Possibly other structures (list, tree) this is part of. 135 */ 136 struct internal_event { 137 /** the comm base */ 138 struct comm_base* base; 139 /** ub_event event type */ 140 struct ub_event* ev; 141 }; 142 143 /** 144 * Internal base structure, so that every thread has its own events. 145 */ 146 struct internal_base { 147 /** ub_event event_base type. */ 148 struct ub_event_base* base; 149 /** seconds time pointer points here */ 150 time_t secs; 151 /** timeval with current time */ 152 struct timeval now; 153 /** the event used for slow_accept timeouts */ 154 struct ub_event* slow_accept; 155 /** true if slow_accept is enabled */ 156 int slow_accept_enabled; 157 /** last log time for slow logging of file descriptor errors */ 158 time_t last_slow_log; 159 /** last log time for slow logging of write wait failures */ 160 time_t last_writewait_log; 161 }; 162 163 /** 164 * Internal timer structure, to store timer event in. 165 */ 166 struct internal_timer { 167 /** the super struct from which derived */ 168 struct comm_timer super; 169 /** the comm base */ 170 struct comm_base* base; 171 /** ub_event event type */ 172 struct ub_event* ev; 173 /** is timer enabled */ 174 uint8_t enabled; 175 }; 176 177 /** 178 * Internal signal structure, to store signal event in. 179 */ 180 struct internal_signal { 181 /** ub_event event type */ 182 struct ub_event* ev; 183 /** next in signal list */ 184 struct internal_signal* next; 185 }; 186 187 /** create a tcp handler with a parent */ 188 static struct comm_point* comm_point_create_tcp_handler( 189 struct comm_base *base, struct comm_point* parent, size_t bufsize, 190 struct sldns_buffer* spoolbuf, comm_point_callback_type* callback, 191 void* callback_arg, struct unbound_socket* socket); 192 193 /* -------- End of local definitions -------- */ 194 195 struct comm_base* 196 comm_base_create(int sigs) 197 { 198 struct comm_base* b = (struct comm_base*)calloc(1, 199 sizeof(struct comm_base)); 200 const char *evnm="event", *evsys="", *evmethod=""; 201 202 if(!b) 203 return NULL; 204 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base)); 205 if(!b->eb) { 206 free(b); 207 return NULL; 208 } 209 b->eb->base = ub_default_event_base(sigs, &b->eb->secs, &b->eb->now); 210 if(!b->eb->base) { 211 free(b->eb); 212 free(b); 213 return NULL; 214 } 215 ub_comm_base_now(b); 216 ub_get_event_sys(b->eb->base, &evnm, &evsys, &evmethod); 217 verbose(VERB_ALGO, "%s %s uses %s method.", evnm, evsys, evmethod); 218 return b; 219 } 220 221 struct comm_base* 222 comm_base_create_event(struct ub_event_base* base) 223 { 224 struct comm_base* b = (struct comm_base*)calloc(1, 225 sizeof(struct comm_base)); 226 if(!b) 227 return NULL; 228 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base)); 229 if(!b->eb) { 230 free(b); 231 return NULL; 232 } 233 b->eb->base = base; 234 ub_comm_base_now(b); 235 return b; 236 } 237 238 void 239 comm_base_delete(struct comm_base* b) 240 { 241 if(!b) 242 return; 243 if(b->eb->slow_accept_enabled) { 244 if(ub_event_del(b->eb->slow_accept) != 0) { 245 log_err("could not event_del slow_accept"); 246 } 247 ub_event_free(b->eb->slow_accept); 248 } 249 ub_event_base_free(b->eb->base); 250 b->eb->base = NULL; 251 free(b->eb); 252 free(b); 253 } 254 255 void 256 comm_base_delete_no_base(struct comm_base* b) 257 { 258 if(!b) 259 return; 260 if(b->eb->slow_accept_enabled) { 261 if(ub_event_del(b->eb->slow_accept) != 0) { 262 log_err("could not event_del slow_accept"); 263 } 264 ub_event_free(b->eb->slow_accept); 265 } 266 b->eb->base = NULL; 267 free(b->eb); 268 free(b); 269 } 270 271 void 272 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv) 273 { 274 *tt = &b->eb->secs; 275 *tv = &b->eb->now; 276 } 277 278 void 279 comm_base_dispatch(struct comm_base* b) 280 { 281 int retval; 282 retval = ub_event_base_dispatch(b->eb->base); 283 if(retval < 0) { 284 fatal_exit("event_dispatch returned error %d, " 285 "errno is %s", retval, strerror(errno)); 286 } 287 } 288 289 void comm_base_exit(struct comm_base* b) 290 { 291 if(ub_event_base_loopexit(b->eb->base) != 0) { 292 log_err("Could not loopexit"); 293 } 294 } 295 296 void comm_base_set_slow_accept_handlers(struct comm_base* b, 297 void (*stop_acc)(void*), void (*start_acc)(void*), void* arg) 298 { 299 b->stop_accept = stop_acc; 300 b->start_accept = start_acc; 301 b->cb_arg = arg; 302 } 303 304 struct ub_event_base* comm_base_internal(struct comm_base* b) 305 { 306 return b->eb->base; 307 } 308 309 /** see if errno for udp has to be logged or not uses globals */ 310 static int 311 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen) 312 { 313 /* do not log transient errors (unless high verbosity) */ 314 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN) 315 switch(errno) { 316 # ifdef ENETUNREACH 317 case ENETUNREACH: 318 # endif 319 # ifdef EHOSTDOWN 320 case EHOSTDOWN: 321 # endif 322 # ifdef EHOSTUNREACH 323 case EHOSTUNREACH: 324 # endif 325 # ifdef ENETDOWN 326 case ENETDOWN: 327 # endif 328 case EPERM: 329 case EACCES: 330 if(verbosity < VERB_ALGO) 331 return 0; 332 default: 333 break; 334 } 335 #endif 336 /* permission denied is gotten for every send if the 337 * network is disconnected (on some OS), squelch it */ 338 if( ((errno == EPERM) 339 # ifdef EADDRNOTAVAIL 340 /* 'Cannot assign requested address' also when disconnected */ 341 || (errno == EADDRNOTAVAIL) 342 # endif 343 ) && verbosity < VERB_ALGO) 344 return 0; 345 # ifdef EADDRINUSE 346 /* If SO_REUSEADDR is set, we could try to connect to the same server 347 * from the same source port twice. */ 348 if(errno == EADDRINUSE && verbosity < VERB_DETAIL) 349 return 0; 350 # endif 351 /* squelch errors where people deploy AAAA ::ffff:bla for 352 * authority servers, which we try for intranets. */ 353 if(errno == EINVAL && addr_is_ip4mapped( 354 (struct sockaddr_storage*)addr, addrlen) && 355 verbosity < VERB_DETAIL) 356 return 0; 357 /* SO_BROADCAST sockopt can give access to 255.255.255.255, 358 * but a dns cache does not need it. */ 359 if(errno == EACCES && addr_is_broadcast( 360 (struct sockaddr_storage*)addr, addrlen) && 361 verbosity < VERB_DETAIL) 362 return 0; 363 return 1; 364 } 365 366 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen) 367 { 368 return udp_send_errno_needs_log(addr, addrlen); 369 } 370 371 /* send a UDP reply */ 372 int 373 comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet, 374 struct sockaddr* addr, socklen_t addrlen, int is_connected) 375 { 376 ssize_t sent; 377 log_assert(c->fd != -1); 378 #ifdef UNBOUND_DEBUG 379 if(sldns_buffer_remaining(packet) == 0) 380 log_err("error: send empty UDP packet"); 381 #endif 382 log_assert(addr && addrlen > 0); 383 if(!is_connected) { 384 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), 385 sldns_buffer_remaining(packet), 0, 386 addr, addrlen); 387 } else { 388 sent = send(c->fd, (void*)sldns_buffer_begin(packet), 389 sldns_buffer_remaining(packet), 0); 390 } 391 if(sent == -1) { 392 /* try again and block, waiting for IO to complete, 393 * we want to send the answer, and we will wait for 394 * the ethernet interface buffer to have space. */ 395 #ifndef USE_WINSOCK 396 if(errno == EAGAIN || errno == EINTR || 397 # ifdef EWOULDBLOCK 398 errno == EWOULDBLOCK || 399 # endif 400 errno == ENOBUFS) { 401 #else 402 if(WSAGetLastError() == WSAEINPROGRESS || 403 WSAGetLastError() == WSAEINTR || 404 WSAGetLastError() == WSAENOBUFS || 405 WSAGetLastError() == WSAEWOULDBLOCK) { 406 #endif 407 int retries = 0; 408 /* if we set the fd blocking, other threads suddenly 409 * have a blocking fd that they operate on */ 410 while(sent == -1 && retries < SEND_BLOCKED_MAX_RETRY && ( 411 #ifndef USE_WINSOCK 412 errno == EAGAIN || errno == EINTR || 413 # ifdef EWOULDBLOCK 414 errno == EWOULDBLOCK || 415 # endif 416 errno == ENOBUFS 417 #else 418 WSAGetLastError() == WSAEINPROGRESS || 419 WSAGetLastError() == WSAEINTR || 420 WSAGetLastError() == WSAENOBUFS || 421 WSAGetLastError() == WSAEWOULDBLOCK 422 #endif 423 )) { 424 #if defined(HAVE_POLL) || defined(USE_WINSOCK) 425 int send_nobufs = ( 426 #ifndef USE_WINSOCK 427 errno == ENOBUFS 428 #else 429 WSAGetLastError() == WSAENOBUFS 430 #endif 431 ); 432 struct pollfd p; 433 int pret; 434 memset(&p, 0, sizeof(p)); 435 p.fd = c->fd; 436 p.events = POLLOUT | POLLERR | POLLHUP; 437 # ifndef USE_WINSOCK 438 pret = poll(&p, 1, SEND_BLOCKED_WAIT_TIMEOUT); 439 # else 440 pret = WSAPoll(&p, 1, 441 SEND_BLOCKED_WAIT_TIMEOUT); 442 # endif 443 if(pret == 0) { 444 /* timer expired */ 445 struct comm_base* b = c->ev->base; 446 if(b->eb->last_writewait_log+SLOW_LOG_TIME <= 447 b->eb->secs) { 448 b->eb->last_writewait_log = b->eb->secs; 449 verbose(VERB_OPS, "send udp blocked " 450 "for long, dropping packet."); 451 } 452 return 0; 453 } else if(pret < 0 && 454 #ifndef USE_WINSOCK 455 errno != EAGAIN && errno != EINTR && 456 # ifdef EWOULDBLOCK 457 errno != EWOULDBLOCK && 458 # endif 459 errno != ENOBUFS 460 #else 461 WSAGetLastError() != WSAEINPROGRESS && 462 WSAGetLastError() != WSAEINTR && 463 WSAGetLastError() != WSAENOBUFS && 464 WSAGetLastError() != WSAEWOULDBLOCK 465 #endif 466 ) { 467 log_err("poll udp out failed: %s", 468 sock_strerror(errno)); 469 return 0; 470 } else if((pret < 0 && 471 #ifndef USE_WINSOCK 472 errno == ENOBUFS 473 #else 474 WSAGetLastError() == WSAENOBUFS 475 #endif 476 ) || (send_nobufs && retries > 0)) { 477 /* ENOBUFS, and poll returned without 478 * a timeout. Or the retried send call 479 * returned ENOBUFS. It is good to 480 * wait a bit for the error to clear. */ 481 /* The timeout is 20*(2^(retries+1)), 482 * it increases exponentially, starting 483 * at 40 msec. After 5 tries, 1240 msec 484 * have passed in total, when poll 485 * returned the error, and 1200 msec 486 * when send returned the errors. */ 487 #ifndef USE_WINSOCK 488 pret = poll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1)); 489 #else 490 pret = WSAPoll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1)); 491 #endif 492 if(pret < 0 && 493 #ifndef USE_WINSOCK 494 errno != EAGAIN && errno != EINTR && 495 # ifdef EWOULDBLOCK 496 errno != EWOULDBLOCK && 497 # endif 498 errno != ENOBUFS 499 #else 500 WSAGetLastError() != WSAEINPROGRESS && 501 WSAGetLastError() != WSAEINTR && 502 WSAGetLastError() != WSAENOBUFS && 503 WSAGetLastError() != WSAEWOULDBLOCK 504 #endif 505 ) { 506 log_err("poll udp out timer failed: %s", 507 sock_strerror(errno)); 508 } 509 } 510 #endif /* defined(HAVE_POLL) || defined(USE_WINSOCK) */ 511 retries++; 512 if (!is_connected) { 513 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), 514 sldns_buffer_remaining(packet), 0, 515 addr, addrlen); 516 } else { 517 sent = send(c->fd, (void*)sldns_buffer_begin(packet), 518 sldns_buffer_remaining(packet), 0); 519 } 520 } 521 } 522 } 523 if(sent == -1) { 524 if(!udp_send_errno_needs_log(addr, addrlen)) 525 return 0; 526 if (!is_connected) { 527 verbose(VERB_OPS, "sendto failed: %s", sock_strerror(errno)); 528 } else { 529 verbose(VERB_OPS, "send failed: %s", sock_strerror(errno)); 530 } 531 if(addr) 532 log_addr(VERB_OPS, "remote address is", 533 (struct sockaddr_storage*)addr, addrlen); 534 return 0; 535 } else if((size_t)sent != sldns_buffer_remaining(packet)) { 536 log_err("sent %d in place of %d bytes", 537 (int)sent, (int)sldns_buffer_remaining(packet)); 538 return 0; 539 } 540 return 1; 541 } 542 543 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG)) 544 /** print debug ancillary info */ 545 static void p_ancil(const char* str, struct comm_reply* r) 546 { 547 if(r->srctype != 4 && r->srctype != 6) { 548 log_info("%s: unknown srctype %d", str, r->srctype); 549 return; 550 } 551 552 if(r->srctype == 6) { 553 #ifdef IPV6_PKTINFO 554 char buf[1024]; 555 if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr, 556 buf, (socklen_t)sizeof(buf)) == 0) { 557 (void)strlcpy(buf, "(inet_ntop error)", sizeof(buf)); 558 } 559 buf[sizeof(buf)-1]=0; 560 log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex); 561 #endif 562 } else if(r->srctype == 4) { 563 #ifdef IP_PKTINFO 564 char buf1[1024], buf2[1024]; 565 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr, 566 buf1, (socklen_t)sizeof(buf1)) == 0) { 567 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1)); 568 } 569 buf1[sizeof(buf1)-1]=0; 570 #ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST 571 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst, 572 buf2, (socklen_t)sizeof(buf2)) == 0) { 573 (void)strlcpy(buf2, "(inet_ntop error)", sizeof(buf2)); 574 } 575 buf2[sizeof(buf2)-1]=0; 576 #else 577 buf2[0]=0; 578 #endif 579 log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex, 580 buf1, buf2); 581 #elif defined(IP_RECVDSTADDR) 582 char buf1[1024]; 583 if(inet_ntop(AF_INET, &r->pktinfo.v4addr, 584 buf1, (socklen_t)sizeof(buf1)) == 0) { 585 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1)); 586 } 587 buf1[sizeof(buf1)-1]=0; 588 log_info("%s: %s", str, buf1); 589 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */ 590 } 591 } 592 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */ 593 594 /** send a UDP reply over specified interface*/ 595 static int 596 comm_point_send_udp_msg_if(struct comm_point *c, sldns_buffer* packet, 597 struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r) 598 { 599 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG) 600 ssize_t sent; 601 struct msghdr msg; 602 struct iovec iov[1]; 603 union { 604 struct cmsghdr hdr; 605 char buf[256]; 606 } control; 607 #ifndef S_SPLINT_S 608 struct cmsghdr *cmsg; 609 #endif /* S_SPLINT_S */ 610 611 log_assert(c->fd != -1); 612 #ifdef UNBOUND_DEBUG 613 if(sldns_buffer_remaining(packet) == 0) 614 log_err("error: send empty UDP packet"); 615 #endif 616 log_assert(addr && addrlen > 0); 617 618 msg.msg_name = addr; 619 msg.msg_namelen = addrlen; 620 iov[0].iov_base = sldns_buffer_begin(packet); 621 iov[0].iov_len = sldns_buffer_remaining(packet); 622 msg.msg_iov = iov; 623 msg.msg_iovlen = 1; 624 msg.msg_control = control.buf; 625 #ifndef S_SPLINT_S 626 msg.msg_controllen = sizeof(control.buf); 627 #endif /* S_SPLINT_S */ 628 msg.msg_flags = 0; 629 630 #ifndef S_SPLINT_S 631 cmsg = CMSG_FIRSTHDR(&msg); 632 if(r->srctype == 4) { 633 #ifdef IP_PKTINFO 634 void* cmsg_data; 635 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); 636 log_assert(msg.msg_controllen <= sizeof(control.buf)); 637 cmsg->cmsg_level = IPPROTO_IP; 638 cmsg->cmsg_type = IP_PKTINFO; 639 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info, 640 sizeof(struct in_pktinfo)); 641 /* unset the ifindex to not bypass the routing tables */ 642 cmsg_data = CMSG_DATA(cmsg); 643 ((struct in_pktinfo *) cmsg_data)->ipi_ifindex = 0; 644 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); 645 /* zero the padding bytes inserted by the CMSG_LEN */ 646 if(sizeof(struct in_pktinfo) < cmsg->cmsg_len) 647 memset(((uint8_t*)(CMSG_DATA(cmsg))) + 648 sizeof(struct in_pktinfo), 0, cmsg->cmsg_len 649 - sizeof(struct in_pktinfo)); 650 #elif defined(IP_SENDSRCADDR) 651 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr)); 652 log_assert(msg.msg_controllen <= sizeof(control.buf)); 653 cmsg->cmsg_level = IPPROTO_IP; 654 cmsg->cmsg_type = IP_SENDSRCADDR; 655 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr, 656 sizeof(struct in_addr)); 657 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 658 /* zero the padding bytes inserted by the CMSG_LEN */ 659 if(sizeof(struct in_addr) < cmsg->cmsg_len) 660 memset(((uint8_t*)(CMSG_DATA(cmsg))) + 661 sizeof(struct in_addr), 0, cmsg->cmsg_len 662 - sizeof(struct in_addr)); 663 #else 664 verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR"); 665 msg.msg_control = NULL; 666 #endif /* IP_PKTINFO or IP_SENDSRCADDR */ 667 } else if(r->srctype == 6) { 668 void* cmsg_data; 669 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); 670 log_assert(msg.msg_controllen <= sizeof(control.buf)); 671 cmsg->cmsg_level = IPPROTO_IPV6; 672 cmsg->cmsg_type = IPV6_PKTINFO; 673 memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info, 674 sizeof(struct in6_pktinfo)); 675 /* unset the ifindex to not bypass the routing tables */ 676 cmsg_data = CMSG_DATA(cmsg); 677 ((struct in6_pktinfo *) cmsg_data)->ipi6_ifindex = 0; 678 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 679 /* zero the padding bytes inserted by the CMSG_LEN */ 680 if(sizeof(struct in6_pktinfo) < cmsg->cmsg_len) 681 memset(((uint8_t*)(CMSG_DATA(cmsg))) + 682 sizeof(struct in6_pktinfo), 0, cmsg->cmsg_len 683 - sizeof(struct in6_pktinfo)); 684 } else { 685 /* try to pass all 0 to use default route */ 686 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); 687 log_assert(msg.msg_controllen <= sizeof(control.buf)); 688 cmsg->cmsg_level = IPPROTO_IPV6; 689 cmsg->cmsg_type = IPV6_PKTINFO; 690 memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo)); 691 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 692 /* zero the padding bytes inserted by the CMSG_LEN */ 693 if(sizeof(struct in6_pktinfo) < cmsg->cmsg_len) 694 memset(((uint8_t*)(CMSG_DATA(cmsg))) + 695 sizeof(struct in6_pktinfo), 0, cmsg->cmsg_len 696 - sizeof(struct in6_pktinfo)); 697 } 698 #endif /* S_SPLINT_S */ 699 if(verbosity >= VERB_ALGO && r->srctype != 0) 700 p_ancil("send_udp over interface", r); 701 sent = sendmsg(c->fd, &msg, 0); 702 if(sent == -1) { 703 /* try again and block, waiting for IO to complete, 704 * we want to send the answer, and we will wait for 705 * the ethernet interface buffer to have space. */ 706 #ifndef USE_WINSOCK 707 if(errno == EAGAIN || errno == EINTR || 708 # ifdef EWOULDBLOCK 709 errno == EWOULDBLOCK || 710 # endif 711 errno == ENOBUFS) { 712 #else 713 if(WSAGetLastError() == WSAEINPROGRESS || 714 WSAGetLastError() == WSAEINTR || 715 WSAGetLastError() == WSAENOBUFS || 716 WSAGetLastError() == WSAEWOULDBLOCK) { 717 #endif 718 int retries = 0; 719 while(sent == -1 && retries < SEND_BLOCKED_MAX_RETRY && ( 720 #ifndef USE_WINSOCK 721 errno == EAGAIN || errno == EINTR || 722 # ifdef EWOULDBLOCK 723 errno == EWOULDBLOCK || 724 # endif 725 errno == ENOBUFS 726 #else 727 WSAGetLastError() == WSAEINPROGRESS || 728 WSAGetLastError() == WSAEINTR || 729 WSAGetLastError() == WSAENOBUFS || 730 WSAGetLastError() == WSAEWOULDBLOCK 731 #endif 732 )) { 733 #if defined(HAVE_POLL) || defined(USE_WINSOCK) 734 int send_nobufs = ( 735 #ifndef USE_WINSOCK 736 errno == ENOBUFS 737 #else 738 WSAGetLastError() == WSAENOBUFS 739 #endif 740 ); 741 struct pollfd p; 742 int pret; 743 memset(&p, 0, sizeof(p)); 744 p.fd = c->fd; 745 p.events = POLLOUT | POLLERR | POLLHUP; 746 # ifndef USE_WINSOCK 747 pret = poll(&p, 1, SEND_BLOCKED_WAIT_TIMEOUT); 748 # else 749 pret = WSAPoll(&p, 1, 750 SEND_BLOCKED_WAIT_TIMEOUT); 751 # endif 752 if(pret == 0) { 753 /* timer expired */ 754 struct comm_base* b = c->ev->base; 755 if(b->eb->last_writewait_log+SLOW_LOG_TIME <= 756 b->eb->secs) { 757 b->eb->last_writewait_log = b->eb->secs; 758 verbose(VERB_OPS, "send udp blocked " 759 "for long, dropping packet."); 760 } 761 return 0; 762 } else if(pret < 0 && 763 #ifndef USE_WINSOCK 764 errno != EAGAIN && errno != EINTR && 765 # ifdef EWOULDBLOCK 766 errno != EWOULDBLOCK && 767 # endif 768 errno != ENOBUFS 769 #else 770 WSAGetLastError() != WSAEINPROGRESS && 771 WSAGetLastError() != WSAEINTR && 772 WSAGetLastError() != WSAENOBUFS && 773 WSAGetLastError() != WSAEWOULDBLOCK 774 #endif 775 ) { 776 log_err("poll udp out failed: %s", 777 sock_strerror(errno)); 778 return 0; 779 } else if((pret < 0 && 780 #ifndef USE_WINSOCK 781 errno == ENOBUFS 782 #else 783 WSAGetLastError() == WSAENOBUFS 784 #endif 785 ) || (send_nobufs && retries > 0)) { 786 /* ENOBUFS, and poll returned without 787 * a timeout. Or the retried send call 788 * returned ENOBUFS. It is good to 789 * wait a bit for the error to clear. */ 790 /* The timeout is 20*(2^(retries+1)), 791 * it increases exponentially, starting 792 * at 40 msec. After 5 tries, 1240 msec 793 * have passed in total, when poll 794 * returned the error, and 1200 msec 795 * when send returned the errors. */ 796 #ifndef USE_WINSOCK 797 pret = poll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1)); 798 #else 799 pret = WSAPoll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1)); 800 #endif 801 if(pret < 0 && 802 #ifndef USE_WINSOCK 803 errno != EAGAIN && errno != EINTR && 804 # ifdef EWOULDBLOCK 805 errno != EWOULDBLOCK && 806 # endif 807 errno != ENOBUFS 808 #else 809 WSAGetLastError() != WSAEINPROGRESS && 810 WSAGetLastError() != WSAEINTR && 811 WSAGetLastError() != WSAENOBUFS && 812 WSAGetLastError() != WSAEWOULDBLOCK 813 #endif 814 ) { 815 log_err("poll udp out timer failed: %s", 816 sock_strerror(errno)); 817 } 818 } 819 #endif /* defined(HAVE_POLL) || defined(USE_WINSOCK) */ 820 retries++; 821 sent = sendmsg(c->fd, &msg, 0); 822 } 823 } 824 } 825 if(sent == -1) { 826 if(!udp_send_errno_needs_log(addr, addrlen)) 827 return 0; 828 verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno)); 829 log_addr(VERB_OPS, "remote address is", 830 (struct sockaddr_storage*)addr, addrlen); 831 #ifdef __NetBSD__ 832 /* netbsd 7 has IP_PKTINFO for recv but not send */ 833 if(errno == EINVAL && r->srctype == 4) 834 log_err("sendmsg: No support for sendmsg(IP_PKTINFO). " 835 "Please disable interface-automatic"); 836 #endif 837 return 0; 838 } else if((size_t)sent != sldns_buffer_remaining(packet)) { 839 log_err("sent %d in place of %d bytes", 840 (int)sent, (int)sldns_buffer_remaining(packet)); 841 return 0; 842 } 843 return 1; 844 #else 845 (void)c; 846 (void)packet; 847 (void)addr; 848 (void)addrlen; 849 (void)r; 850 log_err("sendmsg: IPV6_PKTINFO not supported"); 851 return 0; 852 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */ 853 } 854 855 /** return true is UDP receive error needs to be logged */ 856 static int udp_recv_needs_log(int err) 857 { 858 switch(err) { 859 case EACCES: /* some hosts send ICMP 'Permission Denied' */ 860 #ifndef USE_WINSOCK 861 case ECONNREFUSED: 862 # ifdef ENETUNREACH 863 case ENETUNREACH: 864 # endif 865 # ifdef EHOSTDOWN 866 case EHOSTDOWN: 867 # endif 868 # ifdef EHOSTUNREACH 869 case EHOSTUNREACH: 870 # endif 871 # ifdef ENETDOWN 872 case ENETDOWN: 873 # endif 874 #else /* USE_WINSOCK */ 875 case WSAECONNREFUSED: 876 case WSAENETUNREACH: 877 case WSAEHOSTDOWN: 878 case WSAEHOSTUNREACH: 879 case WSAENETDOWN: 880 #endif 881 if(verbosity >= VERB_ALGO) 882 return 1; 883 return 0; 884 default: 885 break; 886 } 887 return 1; 888 } 889 890 /** Parses the PROXYv2 header from buf and updates the comm_reply struct. 891 * Returns 1 on success, 0 on failure. */ 892 static int consume_pp2_header(struct sldns_buffer* buf, struct comm_reply* rep, 893 int stream) { 894 size_t size; 895 struct pp2_header *header; 896 int err = pp2_read_header(sldns_buffer_begin(buf), 897 sldns_buffer_remaining(buf)); 898 if(err) return 0; 899 header = (struct pp2_header*)sldns_buffer_begin(buf); 900 size = PP2_HEADER_SIZE + ntohs(header->len); 901 if((header->ver_cmd & 0xF) == PP2_CMD_LOCAL) { 902 /* A connection from the proxy itself. 903 * No need to do anything with addresses. */ 904 goto done; 905 } 906 if(header->fam_prot == PP2_UNSPEC_UNSPEC) { 907 /* Unspecified family and protocol. This could be used for 908 * health checks by proxies. 909 * No need to do anything with addresses. */ 910 goto done; 911 } 912 /* Read the proxied address */ 913 switch(header->fam_prot) { 914 case PP2_INET_STREAM: 915 case PP2_INET_DGRAM: 916 { 917 struct sockaddr_in* addr = 918 (struct sockaddr_in*)&rep->client_addr; 919 addr->sin_family = AF_INET; 920 addr->sin_addr.s_addr = header->addr.addr4.src_addr; 921 addr->sin_port = header->addr.addr4.src_port; 922 rep->client_addrlen = (socklen_t)sizeof(struct sockaddr_in); 923 } 924 /* Ignore the destination address; it should be us. */ 925 break; 926 case PP2_INET6_STREAM: 927 case PP2_INET6_DGRAM: 928 { 929 struct sockaddr_in6* addr = 930 (struct sockaddr_in6*)&rep->client_addr; 931 memset(addr, 0, sizeof(*addr)); 932 addr->sin6_family = AF_INET6; 933 memcpy(&addr->sin6_addr, 934 header->addr.addr6.src_addr, 16); 935 addr->sin6_port = header->addr.addr6.src_port; 936 rep->client_addrlen = (socklen_t)sizeof(struct sockaddr_in6); 937 } 938 /* Ignore the destination address; it should be us. */ 939 break; 940 default: 941 log_err("proxy_protocol: unsupported family and " 942 "protocol 0x%x", (int)header->fam_prot); 943 return 0; 944 } 945 rep->is_proxied = 1; 946 done: 947 if(!stream) { 948 /* We are reading a whole packet; 949 * Move the rest of the data to overwrite the PROXYv2 header */ 950 /* XXX can we do better to avoid memmove? */ 951 memmove(header, ((char*)header)+size, 952 sldns_buffer_limit(buf)-size); 953 sldns_buffer_set_limit(buf, sldns_buffer_limit(buf)-size); 954 } 955 return 1; 956 } 957 958 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG) 959 void 960 comm_point_udp_ancil_callback(int fd, short event, void* arg) 961 { 962 struct comm_reply rep; 963 struct msghdr msg; 964 struct iovec iov[1]; 965 ssize_t rcv; 966 union { 967 struct cmsghdr hdr; 968 char buf[256]; 969 } ancil; 970 int i; 971 #ifndef S_SPLINT_S 972 struct cmsghdr* cmsg; 973 #endif /* S_SPLINT_S */ 974 #ifdef HAVE_LINUX_NET_TSTAMP_H 975 struct timespec *ts; 976 #endif /* HAVE_LINUX_NET_TSTAMP_H */ 977 978 rep.c = (struct comm_point*)arg; 979 log_assert(rep.c->type == comm_udp); 980 981 if(!(event&UB_EV_READ)) 982 return; 983 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd); 984 ub_comm_base_now(rep.c->ev->base); 985 for(i=0; i<NUM_UDP_PER_SELECT; i++) { 986 sldns_buffer_clear(rep.c->buffer); 987 timeval_clear(&rep.c->recv_tv); 988 rep.remote_addrlen = (socklen_t)sizeof(rep.remote_addr); 989 log_assert(fd != -1); 990 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0); 991 msg.msg_name = &rep.remote_addr; 992 msg.msg_namelen = (socklen_t)sizeof(rep.remote_addr); 993 iov[0].iov_base = sldns_buffer_begin(rep.c->buffer); 994 iov[0].iov_len = sldns_buffer_remaining(rep.c->buffer); 995 msg.msg_iov = iov; 996 msg.msg_iovlen = 1; 997 msg.msg_control = ancil.buf; 998 #ifndef S_SPLINT_S 999 msg.msg_controllen = sizeof(ancil.buf); 1000 #endif /* S_SPLINT_S */ 1001 msg.msg_flags = 0; 1002 rcv = recvmsg(fd, &msg, MSG_DONTWAIT); 1003 if(rcv == -1) { 1004 if(errno != EAGAIN && errno != EINTR 1005 && udp_recv_needs_log(errno)) { 1006 log_err("recvmsg failed: %s", strerror(errno)); 1007 } 1008 return; 1009 } 1010 rep.remote_addrlen = msg.msg_namelen; 1011 sldns_buffer_skip(rep.c->buffer, rcv); 1012 sldns_buffer_flip(rep.c->buffer); 1013 rep.srctype = 0; 1014 rep.is_proxied = 0; 1015 #ifndef S_SPLINT_S 1016 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 1017 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 1018 if( cmsg->cmsg_level == IPPROTO_IPV6 && 1019 cmsg->cmsg_type == IPV6_PKTINFO) { 1020 rep.srctype = 6; 1021 memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg), 1022 sizeof(struct in6_pktinfo)); 1023 break; 1024 #ifdef IP_PKTINFO 1025 } else if( cmsg->cmsg_level == IPPROTO_IP && 1026 cmsg->cmsg_type == IP_PKTINFO) { 1027 rep.srctype = 4; 1028 memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg), 1029 sizeof(struct in_pktinfo)); 1030 break; 1031 #elif defined(IP_RECVDSTADDR) 1032 } else if( cmsg->cmsg_level == IPPROTO_IP && 1033 cmsg->cmsg_type == IP_RECVDSTADDR) { 1034 rep.srctype = 4; 1035 memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg), 1036 sizeof(struct in_addr)); 1037 break; 1038 #endif /* IP_PKTINFO or IP_RECVDSTADDR */ 1039 #ifdef HAVE_LINUX_NET_TSTAMP_H 1040 } else if( cmsg->cmsg_level == SOL_SOCKET && 1041 cmsg->cmsg_type == SO_TIMESTAMPNS) { 1042 ts = (struct timespec *)CMSG_DATA(cmsg); 1043 TIMESPEC_TO_TIMEVAL(&rep.c->recv_tv, ts); 1044 } else if( cmsg->cmsg_level == SOL_SOCKET && 1045 cmsg->cmsg_type == SO_TIMESTAMPING) { 1046 ts = (struct timespec *)CMSG_DATA(cmsg); 1047 TIMESPEC_TO_TIMEVAL(&rep.c->recv_tv, ts); 1048 } else if( cmsg->cmsg_level == SOL_SOCKET && 1049 cmsg->cmsg_type == SO_TIMESTAMP) { 1050 memmove(&rep.c->recv_tv, CMSG_DATA(cmsg), sizeof(struct timeval)); 1051 #endif /* HAVE_LINUX_NET_TSTAMP_H */ 1052 } 1053 } 1054 1055 if(verbosity >= VERB_ALGO && rep.srctype != 0) 1056 p_ancil("receive_udp on interface", &rep); 1057 #endif /* S_SPLINT_S */ 1058 1059 if(rep.c->pp2_enabled && !consume_pp2_header(rep.c->buffer, 1060 &rep, 0)) { 1061 log_err("proxy_protocol: could not consume PROXYv2 header"); 1062 return; 1063 } 1064 if(!rep.is_proxied) { 1065 rep.client_addrlen = rep.remote_addrlen; 1066 memmove(&rep.client_addr, &rep.remote_addr, 1067 rep.remote_addrlen); 1068 } 1069 1070 fptr_ok(fptr_whitelist_comm_point(rep.c->callback)); 1071 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) { 1072 /* send back immediate reply */ 1073 struct sldns_buffer *buffer; 1074 #ifdef USE_DNSCRYPT 1075 buffer = rep.c->dnscrypt_buffer; 1076 #else 1077 buffer = rep.c->buffer; 1078 #endif 1079 (void)comm_point_send_udp_msg_if(rep.c, buffer, 1080 (struct sockaddr*)&rep.remote_addr, 1081 rep.remote_addrlen, &rep); 1082 } 1083 if(!rep.c || rep.c->fd == -1) /* commpoint closed */ 1084 break; 1085 } 1086 } 1087 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */ 1088 1089 void 1090 comm_point_udp_callback(int fd, short event, void* arg) 1091 { 1092 struct comm_reply rep; 1093 ssize_t rcv; 1094 int i; 1095 struct sldns_buffer *buffer; 1096 1097 rep.c = (struct comm_point*)arg; 1098 log_assert(rep.c->type == comm_udp); 1099 1100 if(!(event&UB_EV_READ)) 1101 return; 1102 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd); 1103 ub_comm_base_now(rep.c->ev->base); 1104 for(i=0; i<NUM_UDP_PER_SELECT; i++) { 1105 sldns_buffer_clear(rep.c->buffer); 1106 rep.remote_addrlen = (socklen_t)sizeof(rep.remote_addr); 1107 log_assert(fd != -1); 1108 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0); 1109 rcv = recvfrom(fd, (void*)sldns_buffer_begin(rep.c->buffer), 1110 sldns_buffer_remaining(rep.c->buffer), MSG_DONTWAIT, 1111 (struct sockaddr*)&rep.remote_addr, &rep.remote_addrlen); 1112 if(rcv == -1) { 1113 #ifndef USE_WINSOCK 1114 if(errno != EAGAIN && errno != EINTR 1115 && udp_recv_needs_log(errno)) 1116 log_err("recvfrom %d failed: %s", 1117 fd, strerror(errno)); 1118 #else 1119 if(WSAGetLastError() != WSAEINPROGRESS && 1120 WSAGetLastError() != WSAECONNRESET && 1121 WSAGetLastError()!= WSAEWOULDBLOCK && 1122 udp_recv_needs_log(WSAGetLastError())) 1123 log_err("recvfrom failed: %s", 1124 wsa_strerror(WSAGetLastError())); 1125 #endif 1126 return; 1127 } 1128 sldns_buffer_skip(rep.c->buffer, rcv); 1129 sldns_buffer_flip(rep.c->buffer); 1130 rep.srctype = 0; 1131 rep.is_proxied = 0; 1132 1133 if(rep.c->pp2_enabled && !consume_pp2_header(rep.c->buffer, 1134 &rep, 0)) { 1135 log_err("proxy_protocol: could not consume PROXYv2 header"); 1136 return; 1137 } 1138 if(!rep.is_proxied) { 1139 rep.client_addrlen = rep.remote_addrlen; 1140 memmove(&rep.client_addr, &rep.remote_addr, 1141 rep.remote_addrlen); 1142 } 1143 1144 fptr_ok(fptr_whitelist_comm_point(rep.c->callback)); 1145 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) { 1146 /* send back immediate reply */ 1147 #ifdef USE_DNSCRYPT 1148 buffer = rep.c->dnscrypt_buffer; 1149 #else 1150 buffer = rep.c->buffer; 1151 #endif 1152 (void)comm_point_send_udp_msg(rep.c, buffer, 1153 (struct sockaddr*)&rep.remote_addr, 1154 rep.remote_addrlen, 0); 1155 } 1156 if(!rep.c || rep.c->fd != fd) /* commpoint closed to -1 or reused for 1157 another UDP port. Note rep.c cannot be reused with TCP fd. */ 1158 break; 1159 } 1160 } 1161 1162 int adjusted_tcp_timeout(struct comm_point* c) 1163 { 1164 if(c->tcp_timeout_msec < TCP_QUERY_TIMEOUT_MINIMUM) 1165 return TCP_QUERY_TIMEOUT_MINIMUM; 1166 return c->tcp_timeout_msec; 1167 } 1168 1169 /** Use a new tcp handler for new query fd, set to read query */ 1170 static void 1171 setup_tcp_handler(struct comm_point* c, int fd, int cur, int max) 1172 { 1173 int handler_usage; 1174 log_assert(c->type == comm_tcp || c->type == comm_http); 1175 log_assert(c->fd == -1); 1176 sldns_buffer_clear(c->buffer); 1177 #ifdef USE_DNSCRYPT 1178 if (c->dnscrypt) 1179 sldns_buffer_clear(c->dnscrypt_buffer); 1180 #endif 1181 c->tcp_is_reading = 1; 1182 c->tcp_byte_count = 0; 1183 c->tcp_keepalive = 0; 1184 /* if more than half the tcp handlers are in use, use a shorter 1185 * timeout for this TCP connection, we need to make space for 1186 * other connections to be able to get attention */ 1187 /* If > 50% TCP handler structures in use, set timeout to 1/100th 1188 * configured value. 1189 * If > 65%TCP handler structures in use, set to 1/500th configured 1190 * value. 1191 * If > 80% TCP handler structures in use, set to 0. 1192 * 1193 * If the timeout to use falls below 200 milliseconds, an actual 1194 * timeout of 200ms is used. 1195 */ 1196 handler_usage = (cur * 100) / max; 1197 if(handler_usage > 50 && handler_usage <= 65) 1198 c->tcp_timeout_msec /= 100; 1199 else if (handler_usage > 65 && handler_usage <= 80) 1200 c->tcp_timeout_msec /= 500; 1201 else if (handler_usage > 80) 1202 c->tcp_timeout_msec = 0; 1203 comm_point_start_listening(c, fd, adjusted_tcp_timeout(c)); 1204 } 1205 1206 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd), 1207 short ATTR_UNUSED(event), void* arg) 1208 { 1209 struct comm_base* b = (struct comm_base*)arg; 1210 /* timeout for the slow accept, re-enable accepts again */ 1211 if(b->start_accept) { 1212 verbose(VERB_ALGO, "wait is over, slow accept disabled"); 1213 fptr_ok(fptr_whitelist_start_accept(b->start_accept)); 1214 (*b->start_accept)(b->cb_arg); 1215 b->eb->slow_accept_enabled = 0; 1216 } 1217 } 1218 1219 int comm_point_perform_accept(struct comm_point* c, 1220 struct sockaddr_storage* addr, socklen_t* addrlen) 1221 { 1222 int new_fd; 1223 *addrlen = (socklen_t)sizeof(*addr); 1224 #ifndef HAVE_ACCEPT4 1225 new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen); 1226 #else 1227 /* SOCK_NONBLOCK saves extra calls to fcntl for the same result */ 1228 new_fd = accept4(c->fd, (struct sockaddr*)addr, addrlen, SOCK_NONBLOCK); 1229 #endif 1230 if(new_fd == -1) { 1231 #ifndef USE_WINSOCK 1232 /* EINTR is signal interrupt. others are closed connection. */ 1233 if( errno == EINTR || errno == EAGAIN 1234 #ifdef EWOULDBLOCK 1235 || errno == EWOULDBLOCK 1236 #endif 1237 #ifdef ECONNABORTED 1238 || errno == ECONNABORTED 1239 #endif 1240 #ifdef EPROTO 1241 || errno == EPROTO 1242 #endif /* EPROTO */ 1243 ) 1244 return -1; 1245 #if defined(ENFILE) && defined(EMFILE) 1246 if(errno == ENFILE || errno == EMFILE) { 1247 /* out of file descriptors, likely outside of our 1248 * control. stop accept() calls for some time */ 1249 if(c->ev->base->stop_accept) { 1250 struct comm_base* b = c->ev->base; 1251 struct timeval tv; 1252 verbose(VERB_ALGO, "out of file descriptors: " 1253 "slow accept"); 1254 ub_comm_base_now(b); 1255 if(b->eb->last_slow_log+SLOW_LOG_TIME <= 1256 b->eb->secs) { 1257 b->eb->last_slow_log = b->eb->secs; 1258 verbose(VERB_OPS, "accept failed, " 1259 "slow down accept for %d " 1260 "msec: %s", 1261 NETEVENT_SLOW_ACCEPT_TIME, 1262 sock_strerror(errno)); 1263 } 1264 b->eb->slow_accept_enabled = 1; 1265 fptr_ok(fptr_whitelist_stop_accept( 1266 b->stop_accept)); 1267 (*b->stop_accept)(b->cb_arg); 1268 /* set timeout, no mallocs */ 1269 tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000; 1270 tv.tv_usec = (NETEVENT_SLOW_ACCEPT_TIME%1000)*1000; 1271 b->eb->slow_accept = ub_event_new(b->eb->base, 1272 -1, UB_EV_TIMEOUT, 1273 comm_base_handle_slow_accept, b); 1274 if(b->eb->slow_accept == NULL) { 1275 /* we do not want to log here, because 1276 * that would spam the logfiles. 1277 * error: "event_base_set failed." */ 1278 } 1279 else if(ub_event_add(b->eb->slow_accept, &tv) 1280 != 0) { 1281 /* we do not want to log here, 1282 * error: "event_add failed." */ 1283 } 1284 } else { 1285 log_err("accept, with no slow down, " 1286 "failed: %s", sock_strerror(errno)); 1287 } 1288 return -1; 1289 } 1290 #endif 1291 #else /* USE_WINSOCK */ 1292 if(WSAGetLastError() == WSAEINPROGRESS || 1293 WSAGetLastError() == WSAECONNRESET) 1294 return -1; 1295 if(WSAGetLastError() == WSAEWOULDBLOCK) { 1296 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 1297 return -1; 1298 } 1299 #endif 1300 log_err_addr("accept failed", sock_strerror(errno), addr, 1301 *addrlen); 1302 return -1; 1303 } 1304 if(c->tcp_conn_limit && c->type == comm_tcp_accept) { 1305 c->tcl_addr = tcl_addr_lookup(c->tcp_conn_limit, addr, *addrlen); 1306 if(!tcl_new_connection(c->tcl_addr)) { 1307 if(verbosity >= 3) 1308 log_err_addr("accept rejected", 1309 "connection limit exceeded", addr, *addrlen); 1310 close(new_fd); 1311 return -1; 1312 } 1313 } 1314 #ifndef HAVE_ACCEPT4 1315 fd_set_nonblock(new_fd); 1316 #endif 1317 return new_fd; 1318 } 1319 1320 #ifdef USE_WINSOCK 1321 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp), 1322 #ifdef HAVE_BIO_SET_CALLBACK_EX 1323 size_t ATTR_UNUSED(len), 1324 #endif 1325 int ATTR_UNUSED(argi), long argl, 1326 #ifndef HAVE_BIO_SET_CALLBACK_EX 1327 long retvalue 1328 #else 1329 int retvalue, size_t* ATTR_UNUSED(processed) 1330 #endif 1331 ) 1332 { 1333 int wsa_err = WSAGetLastError(); /* store errcode before it is gone */ 1334 verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper, 1335 (oper&BIO_CB_RETURN)?"return":"before", 1336 (oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"), 1337 wsa_err==WSAEWOULDBLOCK?"wsawb":""); 1338 /* on windows, check if previous operation caused EWOULDBLOCK */ 1339 if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) || 1340 (oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) { 1341 if(wsa_err == WSAEWOULDBLOCK) 1342 ub_winsock_tcp_wouldblock((struct ub_event*) 1343 BIO_get_callback_arg(b), UB_EV_READ); 1344 } 1345 if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) || 1346 (oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) { 1347 if(wsa_err == WSAEWOULDBLOCK) 1348 ub_winsock_tcp_wouldblock((struct ub_event*) 1349 BIO_get_callback_arg(b), UB_EV_WRITE); 1350 } 1351 /* return original return value */ 1352 return retvalue; 1353 } 1354 1355 /** set win bio callbacks for nonblocking operations */ 1356 void 1357 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl) 1358 { 1359 SSL* ssl = (SSL*)thessl; 1360 /* set them both just in case, but usually they are the same BIO */ 1361 #ifdef HAVE_BIO_SET_CALLBACK_EX 1362 BIO_set_callback_ex(SSL_get_rbio(ssl), &win_bio_cb); 1363 #else 1364 BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb); 1365 #endif 1366 BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)c->ev->ev); 1367 #ifdef HAVE_BIO_SET_CALLBACK_EX 1368 BIO_set_callback_ex(SSL_get_wbio(ssl), &win_bio_cb); 1369 #else 1370 BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb); 1371 #endif 1372 BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)c->ev->ev); 1373 } 1374 #endif 1375 1376 #ifdef HAVE_NGHTTP2 1377 /** Create http2 session server. Per connection, after TCP accepted.*/ 1378 static int http2_session_server_create(struct http2_session* h2_session) 1379 { 1380 log_assert(h2_session->callbacks); 1381 h2_session->is_drop = 0; 1382 if(nghttp2_session_server_new(&h2_session->session, 1383 h2_session->callbacks, 1384 h2_session) == NGHTTP2_ERR_NOMEM) { 1385 log_err("failed to create nghttp2 session server"); 1386 return 0; 1387 } 1388 1389 return 1; 1390 } 1391 1392 /** Submit http2 setting to session. Once per session. */ 1393 static int http2_submit_settings(struct http2_session* h2_session) 1394 { 1395 int ret; 1396 nghttp2_settings_entry settings[1] = { 1397 {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 1398 h2_session->c->http2_max_streams}}; 1399 1400 ret = nghttp2_submit_settings(h2_session->session, NGHTTP2_FLAG_NONE, 1401 settings, 1); 1402 if(ret) { 1403 verbose(VERB_QUERY, "http2: submit_settings failed, " 1404 "error: %s", nghttp2_strerror(ret)); 1405 return 0; 1406 } 1407 return 1; 1408 } 1409 #endif /* HAVE_NGHTTP2 */ 1410 1411 1412 void 1413 comm_point_tcp_accept_callback(int fd, short event, void* arg) 1414 { 1415 struct comm_point* c = (struct comm_point*)arg, *c_hdl; 1416 int new_fd; 1417 log_assert(c->type == comm_tcp_accept); 1418 if(!(event & UB_EV_READ)) { 1419 log_info("ignoring tcp accept event %d", (int)event); 1420 return; 1421 } 1422 ub_comm_base_now(c->ev->base); 1423 /* find free tcp handler. */ 1424 if(!c->tcp_free) { 1425 log_warn("accepted too many tcp, connections full"); 1426 return; 1427 } 1428 /* accept incoming connection. */ 1429 c_hdl = c->tcp_free; 1430 /* clear leftover flags from previous use, and then set the 1431 * correct event base for the event structure for libevent */ 1432 ub_event_free(c_hdl->ev->ev); 1433 c_hdl->ev->ev = NULL; 1434 if((c_hdl->type == comm_tcp && c_hdl->tcp_req_info) || 1435 c_hdl->type == comm_local || c_hdl->type == comm_raw) 1436 c_hdl->tcp_do_toggle_rw = 0; 1437 else c_hdl->tcp_do_toggle_rw = 1; 1438 1439 if(c_hdl->type == comm_http) { 1440 #ifdef HAVE_NGHTTP2 1441 if(!c_hdl->h2_session || 1442 !http2_session_server_create(c_hdl->h2_session)) { 1443 log_warn("failed to create nghttp2"); 1444 return; 1445 } 1446 if(!c_hdl->h2_session || 1447 !http2_submit_settings(c_hdl->h2_session)) { 1448 log_warn("failed to submit http2 settings"); 1449 return; 1450 } 1451 if(!c->ssl) { 1452 c_hdl->tcp_do_toggle_rw = 0; 1453 c_hdl->use_h2 = 1; 1454 } 1455 #endif 1456 c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1, 1457 UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT, 1458 comm_point_http_handle_callback, c_hdl); 1459 } else { 1460 c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1, 1461 UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT, 1462 comm_point_tcp_handle_callback, c_hdl); 1463 } 1464 if(!c_hdl->ev->ev) { 1465 log_warn("could not ub_event_new, dropped tcp"); 1466 return; 1467 } 1468 log_assert(fd != -1); 1469 (void)fd; 1470 new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.remote_addr, 1471 &c_hdl->repinfo.remote_addrlen); 1472 if(new_fd == -1) 1473 return; 1474 /* Copy remote_address to client_address. 1475 * Simplest way/time for streams to do that. */ 1476 c_hdl->repinfo.client_addrlen = c_hdl->repinfo.remote_addrlen; 1477 memmove(&c_hdl->repinfo.client_addr, 1478 &c_hdl->repinfo.remote_addr, 1479 c_hdl->repinfo.remote_addrlen); 1480 if(c->ssl) { 1481 c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd); 1482 if(!c_hdl->ssl) { 1483 c_hdl->fd = new_fd; 1484 comm_point_close(c_hdl); 1485 return; 1486 } 1487 c_hdl->ssl_shake_state = comm_ssl_shake_read; 1488 #ifdef USE_WINSOCK 1489 comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl); 1490 #endif 1491 } 1492 1493 /* grab the tcp handler buffers */ 1494 c->cur_tcp_count++; 1495 c->tcp_free = c_hdl->tcp_free; 1496 c_hdl->tcp_free = NULL; 1497 if(!c->tcp_free) { 1498 /* stop accepting incoming queries for now. */ 1499 comm_point_stop_listening(c); 1500 } 1501 setup_tcp_handler(c_hdl, new_fd, c->cur_tcp_count, c->max_tcp_count); 1502 } 1503 1504 /** Make tcp handler free for next assignment */ 1505 static void 1506 reclaim_tcp_handler(struct comm_point* c) 1507 { 1508 log_assert(c->type == comm_tcp); 1509 if(c->ssl) { 1510 #ifdef HAVE_SSL 1511 SSL_shutdown(c->ssl); 1512 SSL_free(c->ssl); 1513 c->ssl = NULL; 1514 #endif 1515 } 1516 comm_point_close(c); 1517 if(c->tcp_parent) { 1518 if(c != c->tcp_parent->tcp_free) { 1519 c->tcp_parent->cur_tcp_count--; 1520 c->tcp_free = c->tcp_parent->tcp_free; 1521 c->tcp_parent->tcp_free = c; 1522 } 1523 if(!c->tcp_free) { 1524 /* re-enable listening on accept socket */ 1525 comm_point_start_listening(c->tcp_parent, -1, -1); 1526 } 1527 } 1528 c->tcp_more_read_again = NULL; 1529 c->tcp_more_write_again = NULL; 1530 c->tcp_byte_count = 0; 1531 c->pp2_header_state = pp2_header_none; 1532 sldns_buffer_clear(c->buffer); 1533 } 1534 1535 /** do the callback when writing is done */ 1536 static void 1537 tcp_callback_writer(struct comm_point* c) 1538 { 1539 log_assert(c->type == comm_tcp); 1540 if(!c->tcp_write_and_read) { 1541 sldns_buffer_clear(c->buffer); 1542 c->tcp_byte_count = 0; 1543 } 1544 if(c->tcp_do_toggle_rw) 1545 c->tcp_is_reading = 1; 1546 /* switch from listening(write) to listening(read) */ 1547 if(c->tcp_req_info) { 1548 tcp_req_info_handle_writedone(c->tcp_req_info); 1549 } else { 1550 comm_point_stop_listening(c); 1551 if(c->tcp_write_and_read) { 1552 fptr_ok(fptr_whitelist_comm_point(c->callback)); 1553 if( (*c->callback)(c, c->cb_arg, NETEVENT_PKT_WRITTEN, 1554 &c->repinfo) ) { 1555 comm_point_start_listening(c, -1, 1556 adjusted_tcp_timeout(c)); 1557 } 1558 } else { 1559 comm_point_start_listening(c, -1, 1560 adjusted_tcp_timeout(c)); 1561 } 1562 } 1563 } 1564 1565 /** do the callback when reading is done */ 1566 static void 1567 tcp_callback_reader(struct comm_point* c) 1568 { 1569 log_assert(c->type == comm_tcp || c->type == comm_local); 1570 sldns_buffer_flip(c->buffer); 1571 if(c->tcp_do_toggle_rw) 1572 c->tcp_is_reading = 0; 1573 c->tcp_byte_count = 0; 1574 if(c->tcp_req_info) { 1575 tcp_req_info_handle_readdone(c->tcp_req_info); 1576 } else { 1577 if(c->type == comm_tcp) 1578 comm_point_stop_listening(c); 1579 fptr_ok(fptr_whitelist_comm_point(c->callback)); 1580 if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) { 1581 comm_point_start_listening(c, -1, 1582 adjusted_tcp_timeout(c)); 1583 } 1584 } 1585 } 1586 1587 #ifdef HAVE_SSL 1588 /** true if the ssl handshake error has to be squelched from the logs */ 1589 int 1590 squelch_err_ssl_handshake(unsigned long err) 1591 { 1592 if(verbosity >= VERB_QUERY) 1593 return 0; /* only squelch on low verbosity */ 1594 if(ERR_GET_LIB(err) == ERR_LIB_SSL && 1595 (ERR_GET_REASON(err) == SSL_R_HTTPS_PROXY_REQUEST || 1596 ERR_GET_REASON(err) == SSL_R_HTTP_REQUEST || 1597 ERR_GET_REASON(err) == SSL_R_WRONG_VERSION_NUMBER || 1598 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE 1599 #ifdef SSL_F_TLS_POST_PROCESS_CLIENT_HELLO 1600 || ERR_GET_REASON(err) == SSL_R_NO_SHARED_CIPHER 1601 #endif 1602 #ifdef SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO 1603 || ERR_GET_REASON(err) == SSL_R_UNKNOWN_PROTOCOL 1604 || ERR_GET_REASON(err) == SSL_R_UNSUPPORTED_PROTOCOL 1605 # ifdef SSL_R_VERSION_TOO_LOW 1606 || ERR_GET_REASON(err) == SSL_R_VERSION_TOO_LOW 1607 # endif 1608 #endif 1609 )) 1610 return 1; 1611 return 0; 1612 } 1613 #endif /* HAVE_SSL */ 1614 1615 /** continue ssl handshake */ 1616 #ifdef HAVE_SSL 1617 static int 1618 ssl_handshake(struct comm_point* c) 1619 { 1620 int r; 1621 if(c->ssl_shake_state == comm_ssl_shake_hs_read) { 1622 /* read condition satisfied back to writing */ 1623 comm_point_listen_for_rw(c, 0, 1); 1624 c->ssl_shake_state = comm_ssl_shake_none; 1625 return 1; 1626 } 1627 if(c->ssl_shake_state == comm_ssl_shake_hs_write) { 1628 /* write condition satisfied, back to reading */ 1629 comm_point_listen_for_rw(c, 1, 0); 1630 c->ssl_shake_state = comm_ssl_shake_none; 1631 return 1; 1632 } 1633 1634 ERR_clear_error(); 1635 r = SSL_do_handshake(c->ssl); 1636 if(r != 1) { 1637 int want = SSL_get_error(c->ssl, r); 1638 if(want == SSL_ERROR_WANT_READ) { 1639 if(c->ssl_shake_state == comm_ssl_shake_read) 1640 return 1; 1641 c->ssl_shake_state = comm_ssl_shake_read; 1642 comm_point_listen_for_rw(c, 1, 0); 1643 return 1; 1644 } else if(want == SSL_ERROR_WANT_WRITE) { 1645 if(c->ssl_shake_state == comm_ssl_shake_write) 1646 return 1; 1647 c->ssl_shake_state = comm_ssl_shake_write; 1648 comm_point_listen_for_rw(c, 0, 1); 1649 return 1; 1650 } else if(r == 0) { 1651 return 0; /* closed */ 1652 } else if(want == SSL_ERROR_SYSCALL) { 1653 /* SYSCALL and errno==0 means closed uncleanly */ 1654 #ifdef EPIPE 1655 if(errno == EPIPE && verbosity < 2) 1656 return 0; /* silence 'broken pipe' */ 1657 #endif 1658 #ifdef ECONNRESET 1659 if(errno == ECONNRESET && verbosity < 2) 1660 return 0; /* silence reset by peer */ 1661 #endif 1662 if(!tcp_connect_errno_needs_log( 1663 (struct sockaddr*)&c->repinfo.remote_addr, 1664 c->repinfo.remote_addrlen)) 1665 return 0; /* silence connect failures that 1666 show up because after connect this is the 1667 first system call that accesses the socket */ 1668 if(errno != 0) 1669 log_err("SSL_handshake syscall: %s", 1670 strerror(errno)); 1671 return 0; 1672 } else { 1673 unsigned long err = ERR_get_error(); 1674 if(!squelch_err_ssl_handshake(err)) { 1675 log_crypto_err_io_code("ssl handshake failed", 1676 want, err); 1677 log_addr(VERB_OPS, "ssl handshake failed", 1678 &c->repinfo.remote_addr, 1679 c->repinfo.remote_addrlen); 1680 } 1681 return 0; 1682 } 1683 } 1684 /* this is where peer verification could take place */ 1685 if((SSL_get_verify_mode(c->ssl)&SSL_VERIFY_PEER)) { 1686 /* verification */ 1687 if(SSL_get_verify_result(c->ssl) == X509_V_OK) { 1688 #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE 1689 X509* x = SSL_get1_peer_certificate(c->ssl); 1690 #else 1691 X509* x = SSL_get_peer_certificate(c->ssl); 1692 #endif 1693 if(!x) { 1694 log_addr(VERB_ALGO, "SSL connection failed: " 1695 "no certificate", 1696 &c->repinfo.remote_addr, 1697 c->repinfo.remote_addrlen); 1698 return 0; 1699 } 1700 log_cert(VERB_ALGO, "peer certificate", x); 1701 #ifdef HAVE_SSL_GET0_PEERNAME 1702 if(SSL_get0_peername(c->ssl)) { 1703 char buf[255]; 1704 snprintf(buf, sizeof(buf), "SSL connection " 1705 "to %s authenticated", 1706 SSL_get0_peername(c->ssl)); 1707 log_addr(VERB_ALGO, buf, &c->repinfo.remote_addr, 1708 c->repinfo.remote_addrlen); 1709 } else { 1710 #endif 1711 log_addr(VERB_ALGO, "SSL connection " 1712 "authenticated", &c->repinfo.remote_addr, 1713 c->repinfo.remote_addrlen); 1714 #ifdef HAVE_SSL_GET0_PEERNAME 1715 } 1716 #endif 1717 X509_free(x); 1718 } else { 1719 #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE 1720 X509* x = SSL_get1_peer_certificate(c->ssl); 1721 #else 1722 X509* x = SSL_get_peer_certificate(c->ssl); 1723 #endif 1724 if(x) { 1725 log_cert(VERB_ALGO, "peer certificate", x); 1726 X509_free(x); 1727 } 1728 log_addr(VERB_ALGO, "SSL connection failed: " 1729 "failed to authenticate", 1730 &c->repinfo.remote_addr, 1731 c->repinfo.remote_addrlen); 1732 return 0; 1733 } 1734 } else { 1735 /* unauthenticated, the verify peer flag was not set 1736 * in c->ssl when the ssl object was created from ssl_ctx */ 1737 log_addr(VERB_ALGO, "SSL connection", &c->repinfo.remote_addr, 1738 c->repinfo.remote_addrlen); 1739 } 1740 1741 #ifdef HAVE_SSL_GET0_ALPN_SELECTED 1742 /* check if http2 use is negotiated */ 1743 if(c->type == comm_http && c->h2_session) { 1744 const unsigned char *alpn; 1745 unsigned int alpnlen = 0; 1746 SSL_get0_alpn_selected(c->ssl, &alpn, &alpnlen); 1747 if(alpnlen == 2 && memcmp("h2", alpn, 2) == 0) { 1748 /* connection upgraded to HTTP2 */ 1749 c->tcp_do_toggle_rw = 0; 1750 c->use_h2 = 1; 1751 } 1752 } 1753 #endif 1754 1755 /* setup listen rw correctly */ 1756 if(c->tcp_is_reading) { 1757 if(c->ssl_shake_state != comm_ssl_shake_read) 1758 comm_point_listen_for_rw(c, 1, 0); 1759 } else { 1760 comm_point_listen_for_rw(c, 0, 1); 1761 } 1762 c->ssl_shake_state = comm_ssl_shake_none; 1763 return 1; 1764 } 1765 #endif /* HAVE_SSL */ 1766 1767 /** ssl read callback on TCP */ 1768 static int 1769 ssl_handle_read(struct comm_point* c) 1770 { 1771 #ifdef HAVE_SSL 1772 int r; 1773 if(c->ssl_shake_state != comm_ssl_shake_none) { 1774 if(!ssl_handshake(c)) 1775 return 0; 1776 if(c->ssl_shake_state != comm_ssl_shake_none) 1777 return 1; 1778 } 1779 if(c->pp2_enabled && c->pp2_header_state != pp2_header_done) { 1780 struct pp2_header* header = NULL; 1781 size_t want_read_size = 0; 1782 size_t current_read_size = 0; 1783 if(c->pp2_header_state == pp2_header_none) { 1784 want_read_size = PP2_HEADER_SIZE; 1785 if(sldns_buffer_remaining(c->buffer)<want_read_size) { 1786 log_err_addr("proxy_protocol: not enough " 1787 "buffer size to read PROXYv2 header", "", 1788 &c->repinfo.remote_addr, 1789 c->repinfo.remote_addrlen); 1790 return 0; 1791 } 1792 verbose(VERB_ALGO, "proxy_protocol: reading fixed " 1793 "part of PROXYv2 header (len %lu)", 1794 (unsigned long)want_read_size); 1795 current_read_size = want_read_size; 1796 if(c->tcp_byte_count < current_read_size) { 1797 ERR_clear_error(); 1798 if((r=SSL_read(c->ssl, (void*)sldns_buffer_at( 1799 c->buffer, c->tcp_byte_count), 1800 current_read_size - 1801 c->tcp_byte_count)) <= 0) { 1802 int want = SSL_get_error(c->ssl, r); 1803 if(want == SSL_ERROR_ZERO_RETURN) { 1804 if(c->tcp_req_info) 1805 return tcp_req_info_handle_read_close(c->tcp_req_info); 1806 return 0; /* shutdown, closed */ 1807 } else if(want == SSL_ERROR_WANT_READ) { 1808 #ifdef USE_WINSOCK 1809 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 1810 #endif 1811 return 1; /* read more later */ 1812 } else if(want == SSL_ERROR_WANT_WRITE) { 1813 c->ssl_shake_state = comm_ssl_shake_hs_write; 1814 comm_point_listen_for_rw(c, 0, 1); 1815 return 1; 1816 } else if(want == SSL_ERROR_SYSCALL) { 1817 #ifdef ECONNRESET 1818 if(errno == ECONNRESET && verbosity < 2) 1819 return 0; /* silence reset by peer */ 1820 #endif 1821 if(errno != 0) 1822 log_err("SSL_read syscall: %s", 1823 strerror(errno)); 1824 return 0; 1825 } 1826 log_crypto_err_io("could not SSL_read", 1827 want); 1828 return 0; 1829 } 1830 c->tcp_byte_count += r; 1831 sldns_buffer_skip(c->buffer, r); 1832 if(c->tcp_byte_count != current_read_size) return 1; 1833 c->pp2_header_state = pp2_header_init; 1834 } 1835 } 1836 if(c->pp2_header_state == pp2_header_init) { 1837 int err; 1838 err = pp2_read_header( 1839 sldns_buffer_begin(c->buffer), 1840 sldns_buffer_limit(c->buffer)); 1841 if(err) { 1842 log_err("proxy_protocol: could not parse " 1843 "PROXYv2 header (%s)", 1844 pp_lookup_error(err)); 1845 return 0; 1846 } 1847 header = (struct pp2_header*)sldns_buffer_begin(c->buffer); 1848 want_read_size = ntohs(header->len); 1849 if(sldns_buffer_limit(c->buffer) < 1850 PP2_HEADER_SIZE + want_read_size) { 1851 log_err_addr("proxy_protocol: not enough " 1852 "buffer size to read PROXYv2 header", "", 1853 &c->repinfo.remote_addr, 1854 c->repinfo.remote_addrlen); 1855 return 0; 1856 } 1857 verbose(VERB_ALGO, "proxy_protocol: reading variable " 1858 "part of PROXYv2 header (len %lu)", 1859 (unsigned long)want_read_size); 1860 current_read_size = PP2_HEADER_SIZE + want_read_size; 1861 if(want_read_size == 0) { 1862 /* nothing more to read; header is complete */ 1863 c->pp2_header_state = pp2_header_done; 1864 } else if(c->tcp_byte_count < current_read_size) { 1865 ERR_clear_error(); 1866 if((r=SSL_read(c->ssl, (void*)sldns_buffer_at( 1867 c->buffer, c->tcp_byte_count), 1868 current_read_size - 1869 c->tcp_byte_count)) <= 0) { 1870 int want = SSL_get_error(c->ssl, r); 1871 if(want == SSL_ERROR_ZERO_RETURN) { 1872 if(c->tcp_req_info) 1873 return tcp_req_info_handle_read_close(c->tcp_req_info); 1874 return 0; /* shutdown, closed */ 1875 } else if(want == SSL_ERROR_WANT_READ) { 1876 #ifdef USE_WINSOCK 1877 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 1878 #endif 1879 return 1; /* read more later */ 1880 } else if(want == SSL_ERROR_WANT_WRITE) { 1881 c->ssl_shake_state = comm_ssl_shake_hs_write; 1882 comm_point_listen_for_rw(c, 0, 1); 1883 return 1; 1884 } else if(want == SSL_ERROR_SYSCALL) { 1885 #ifdef ECONNRESET 1886 if(errno == ECONNRESET && verbosity < 2) 1887 return 0; /* silence reset by peer */ 1888 #endif 1889 if(errno != 0) 1890 log_err("SSL_read syscall: %s", 1891 strerror(errno)); 1892 return 0; 1893 } 1894 log_crypto_err_io("could not SSL_read", 1895 want); 1896 return 0; 1897 } 1898 c->tcp_byte_count += r; 1899 sldns_buffer_skip(c->buffer, r); 1900 if(c->tcp_byte_count != current_read_size) return 1; 1901 c->pp2_header_state = pp2_header_done; 1902 } 1903 } 1904 if(c->pp2_header_state != pp2_header_done || !header) { 1905 log_err_addr("proxy_protocol: wrong state for the " 1906 "PROXYv2 header", "", &c->repinfo.remote_addr, 1907 c->repinfo.remote_addrlen); 1908 return 0; 1909 } 1910 sldns_buffer_flip(c->buffer); 1911 if(!consume_pp2_header(c->buffer, &c->repinfo, 1)) { 1912 log_err_addr("proxy_protocol: could not consume " 1913 "PROXYv2 header", "", &c->repinfo.remote_addr, 1914 c->repinfo.remote_addrlen); 1915 return 0; 1916 } 1917 verbose(VERB_ALGO, "proxy_protocol: successful read of " 1918 "PROXYv2 header"); 1919 /* Clear and reset the buffer to read the following 1920 * DNS packet(s). */ 1921 sldns_buffer_clear(c->buffer); 1922 c->tcp_byte_count = 0; 1923 return 1; 1924 } 1925 if(c->tcp_byte_count < sizeof(uint16_t)) { 1926 /* read length bytes */ 1927 ERR_clear_error(); 1928 if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(c->buffer, 1929 c->tcp_byte_count), (int)(sizeof(uint16_t) - 1930 c->tcp_byte_count))) <= 0) { 1931 int want = SSL_get_error(c->ssl, r); 1932 if(want == SSL_ERROR_ZERO_RETURN) { 1933 if(c->tcp_req_info) 1934 return tcp_req_info_handle_read_close(c->tcp_req_info); 1935 return 0; /* shutdown, closed */ 1936 } else if(want == SSL_ERROR_WANT_READ) { 1937 #ifdef USE_WINSOCK 1938 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 1939 #endif 1940 return 1; /* read more later */ 1941 } else if(want == SSL_ERROR_WANT_WRITE) { 1942 c->ssl_shake_state = comm_ssl_shake_hs_write; 1943 comm_point_listen_for_rw(c, 0, 1); 1944 return 1; 1945 } else if(want == SSL_ERROR_SYSCALL) { 1946 #ifdef ECONNRESET 1947 if(errno == ECONNRESET && verbosity < 2) 1948 return 0; /* silence reset by peer */ 1949 #endif 1950 if(errno != 0) 1951 log_err("SSL_read syscall: %s", 1952 strerror(errno)); 1953 return 0; 1954 } 1955 log_crypto_err_io("could not SSL_read", want); 1956 return 0; 1957 } 1958 c->tcp_byte_count += r; 1959 if(c->tcp_byte_count < sizeof(uint16_t)) 1960 return 1; 1961 if(sldns_buffer_read_u16_at(c->buffer, 0) > 1962 sldns_buffer_capacity(c->buffer)) { 1963 verbose(VERB_QUERY, "ssl: dropped larger than buffer"); 1964 return 0; 1965 } 1966 sldns_buffer_set_limit(c->buffer, 1967 sldns_buffer_read_u16_at(c->buffer, 0)); 1968 if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) { 1969 verbose(VERB_QUERY, "ssl: dropped bogus too short."); 1970 return 0; 1971 } 1972 sldns_buffer_skip(c->buffer, (ssize_t)(c->tcp_byte_count-sizeof(uint16_t))); 1973 verbose(VERB_ALGO, "Reading ssl tcp query of length %d", 1974 (int)sldns_buffer_limit(c->buffer)); 1975 } 1976 if(sldns_buffer_remaining(c->buffer) > 0) { 1977 ERR_clear_error(); 1978 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer), 1979 (int)sldns_buffer_remaining(c->buffer)); 1980 if(r <= 0) { 1981 int want = SSL_get_error(c->ssl, r); 1982 if(want == SSL_ERROR_ZERO_RETURN) { 1983 if(c->tcp_req_info) 1984 return tcp_req_info_handle_read_close(c->tcp_req_info); 1985 return 0; /* shutdown, closed */ 1986 } else if(want == SSL_ERROR_WANT_READ) { 1987 #ifdef USE_WINSOCK 1988 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 1989 #endif 1990 return 1; /* read more later */ 1991 } else if(want == SSL_ERROR_WANT_WRITE) { 1992 c->ssl_shake_state = comm_ssl_shake_hs_write; 1993 comm_point_listen_for_rw(c, 0, 1); 1994 return 1; 1995 } else if(want == SSL_ERROR_SYSCALL) { 1996 #ifdef ECONNRESET 1997 if(errno == ECONNRESET && verbosity < 2) 1998 return 0; /* silence reset by peer */ 1999 #endif 2000 if(errno != 0) 2001 log_err("SSL_read syscall: %s", 2002 strerror(errno)); 2003 return 0; 2004 } 2005 log_crypto_err_io("could not SSL_read", want); 2006 return 0; 2007 } 2008 sldns_buffer_skip(c->buffer, (ssize_t)r); 2009 } 2010 if(sldns_buffer_remaining(c->buffer) <= 0) { 2011 tcp_callback_reader(c); 2012 } 2013 return 1; 2014 #else 2015 (void)c; 2016 return 0; 2017 #endif /* HAVE_SSL */ 2018 } 2019 2020 /** ssl write callback on TCP */ 2021 static int 2022 ssl_handle_write(struct comm_point* c) 2023 { 2024 #ifdef HAVE_SSL 2025 int r; 2026 if(c->ssl_shake_state != comm_ssl_shake_none) { 2027 if(!ssl_handshake(c)) 2028 return 0; 2029 if(c->ssl_shake_state != comm_ssl_shake_none) 2030 return 1; 2031 } 2032 /* ignore return, if fails we may simply block */ 2033 (void)SSL_set_mode(c->ssl, (long)SSL_MODE_ENABLE_PARTIAL_WRITE); 2034 if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) { 2035 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(c->buffer)); 2036 ERR_clear_error(); 2037 if(c->tcp_write_and_read) { 2038 if(c->tcp_write_pkt_len + 2 < LDNS_RR_BUF_SIZE) { 2039 /* combine the tcp length and the query for 2040 * write, this emulates writev */ 2041 uint8_t buf[LDNS_RR_BUF_SIZE]; 2042 memmove(buf, &len, sizeof(uint16_t)); 2043 memmove(buf+sizeof(uint16_t), 2044 c->tcp_write_pkt, 2045 c->tcp_write_pkt_len); 2046 r = SSL_write(c->ssl, 2047 (void*)(buf+c->tcp_write_byte_count), 2048 c->tcp_write_pkt_len + 2 - 2049 c->tcp_write_byte_count); 2050 } else { 2051 r = SSL_write(c->ssl, 2052 (void*)(((uint8_t*)&len)+c->tcp_write_byte_count), 2053 (int)(sizeof(uint16_t)-c->tcp_write_byte_count)); 2054 } 2055 } else if(sizeof(uint16_t)+sldns_buffer_remaining(c->buffer) < 2056 LDNS_RR_BUF_SIZE) { 2057 /* combine the tcp length and the query for write, 2058 * this emulates writev */ 2059 uint8_t buf[LDNS_RR_BUF_SIZE]; 2060 memmove(buf, &len, sizeof(uint16_t)); 2061 memmove(buf+sizeof(uint16_t), 2062 sldns_buffer_current(c->buffer), 2063 sldns_buffer_remaining(c->buffer)); 2064 r = SSL_write(c->ssl, (void*)(buf+c->tcp_byte_count), 2065 (int)(sizeof(uint16_t)+ 2066 sldns_buffer_remaining(c->buffer) 2067 - c->tcp_byte_count)); 2068 } else { 2069 r = SSL_write(c->ssl, 2070 (void*)(((uint8_t*)&len)+c->tcp_byte_count), 2071 (int)(sizeof(uint16_t)-c->tcp_byte_count)); 2072 } 2073 if(r <= 0) { 2074 int want = SSL_get_error(c->ssl, r); 2075 if(want == SSL_ERROR_ZERO_RETURN) { 2076 return 0; /* closed */ 2077 } else if(want == SSL_ERROR_WANT_READ) { 2078 c->ssl_shake_state = comm_ssl_shake_hs_read; 2079 comm_point_listen_for_rw(c, 1, 0); 2080 return 1; /* wait for read condition */ 2081 } else if(want == SSL_ERROR_WANT_WRITE) { 2082 #ifdef USE_WINSOCK 2083 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 2084 #endif 2085 return 1; /* write more later */ 2086 } else if(want == SSL_ERROR_SYSCALL) { 2087 #ifdef EPIPE 2088 if(errno == EPIPE && verbosity < 2) 2089 return 0; /* silence 'broken pipe' */ 2090 #endif 2091 if(errno != 0) 2092 log_err("SSL_write syscall: %s", 2093 strerror(errno)); 2094 return 0; 2095 } 2096 log_crypto_err_io("could not SSL_write", want); 2097 return 0; 2098 } 2099 if(c->tcp_write_and_read) { 2100 c->tcp_write_byte_count += r; 2101 if(c->tcp_write_byte_count < sizeof(uint16_t)) 2102 return 1; 2103 } else { 2104 c->tcp_byte_count += r; 2105 if(c->tcp_byte_count < sizeof(uint16_t)) 2106 return 1; 2107 sldns_buffer_set_position(c->buffer, c->tcp_byte_count - 2108 sizeof(uint16_t)); 2109 } 2110 if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) { 2111 tcp_callback_writer(c); 2112 return 1; 2113 } 2114 } 2115 log_assert(c->tcp_write_and_read || sldns_buffer_remaining(c->buffer) > 0); 2116 log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2); 2117 ERR_clear_error(); 2118 if(c->tcp_write_and_read) { 2119 r = SSL_write(c->ssl, (void*)(c->tcp_write_pkt + c->tcp_write_byte_count - 2), 2120 (int)(c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count)); 2121 } else { 2122 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer), 2123 (int)sldns_buffer_remaining(c->buffer)); 2124 } 2125 if(r <= 0) { 2126 int want = SSL_get_error(c->ssl, r); 2127 if(want == SSL_ERROR_ZERO_RETURN) { 2128 return 0; /* closed */ 2129 } else if(want == SSL_ERROR_WANT_READ) { 2130 c->ssl_shake_state = comm_ssl_shake_hs_read; 2131 comm_point_listen_for_rw(c, 1, 0); 2132 return 1; /* wait for read condition */ 2133 } else if(want == SSL_ERROR_WANT_WRITE) { 2134 #ifdef USE_WINSOCK 2135 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 2136 #endif 2137 return 1; /* write more later */ 2138 } else if(want == SSL_ERROR_SYSCALL) { 2139 #ifdef EPIPE 2140 if(errno == EPIPE && verbosity < 2) 2141 return 0; /* silence 'broken pipe' */ 2142 #endif 2143 if(errno != 0) 2144 log_err("SSL_write syscall: %s", 2145 strerror(errno)); 2146 return 0; 2147 } 2148 log_crypto_err_io("could not SSL_write", want); 2149 return 0; 2150 } 2151 if(c->tcp_write_and_read) { 2152 c->tcp_write_byte_count += r; 2153 } else { 2154 sldns_buffer_skip(c->buffer, (ssize_t)r); 2155 } 2156 2157 if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) { 2158 tcp_callback_writer(c); 2159 } 2160 return 1; 2161 #else 2162 (void)c; 2163 return 0; 2164 #endif /* HAVE_SSL */ 2165 } 2166 2167 /** handle ssl tcp connection with dns contents */ 2168 static int 2169 ssl_handle_it(struct comm_point* c, int is_write) 2170 { 2171 /* handle case where renegotiation wants read during write call 2172 * or write during read calls */ 2173 if(is_write && c->ssl_shake_state == comm_ssl_shake_hs_write) 2174 return ssl_handle_read(c); 2175 else if(!is_write && c->ssl_shake_state == comm_ssl_shake_hs_read) 2176 return ssl_handle_write(c); 2177 /* handle read events for read operation and write events for a 2178 * write operation */ 2179 else if(!is_write) 2180 return ssl_handle_read(c); 2181 return ssl_handle_write(c); 2182 } 2183 2184 /** 2185 * Handle tcp reading callback. 2186 * @param fd: file descriptor of socket. 2187 * @param c: comm point to read from into buffer. 2188 * @param short_ok: if true, very short packets are OK (for comm_local). 2189 * @return: 0 on error 2190 */ 2191 static int 2192 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok) 2193 { 2194 ssize_t r; 2195 int recv_initial = 0; 2196 log_assert(c->type == comm_tcp || c->type == comm_local); 2197 if(c->ssl) 2198 return ssl_handle_it(c, 0); 2199 if(!c->tcp_is_reading && !c->tcp_write_and_read) 2200 return 0; 2201 2202 log_assert(fd != -1); 2203 if(c->pp2_enabled && c->pp2_header_state != pp2_header_done) { 2204 struct pp2_header* header = NULL; 2205 size_t want_read_size = 0; 2206 size_t current_read_size = 0; 2207 if(c->pp2_header_state == pp2_header_none) { 2208 want_read_size = PP2_HEADER_SIZE; 2209 if(sldns_buffer_remaining(c->buffer)<want_read_size) { 2210 log_err_addr("proxy_protocol: not enough " 2211 "buffer size to read PROXYv2 header", "", 2212 &c->repinfo.remote_addr, 2213 c->repinfo.remote_addrlen); 2214 return 0; 2215 } 2216 verbose(VERB_ALGO, "proxy_protocol: reading fixed " 2217 "part of PROXYv2 header (len %lu)", 2218 (unsigned long)want_read_size); 2219 current_read_size = want_read_size; 2220 if(c->tcp_byte_count < current_read_size) { 2221 r = recv(fd, (void*)sldns_buffer_at(c->buffer, 2222 c->tcp_byte_count), 2223 current_read_size-c->tcp_byte_count, MSG_DONTWAIT); 2224 if(r == 0) { 2225 if(c->tcp_req_info) 2226 return tcp_req_info_handle_read_close(c->tcp_req_info); 2227 return 0; 2228 } else if(r == -1) { 2229 goto recv_error_initial; 2230 } 2231 c->tcp_byte_count += r; 2232 sldns_buffer_skip(c->buffer, r); 2233 if(c->tcp_byte_count != current_read_size) return 1; 2234 c->pp2_header_state = pp2_header_init; 2235 } 2236 } 2237 if(c->pp2_header_state == pp2_header_init) { 2238 int err; 2239 err = pp2_read_header( 2240 sldns_buffer_begin(c->buffer), 2241 sldns_buffer_limit(c->buffer)); 2242 if(err) { 2243 log_err("proxy_protocol: could not parse " 2244 "PROXYv2 header (%s)", 2245 pp_lookup_error(err)); 2246 return 0; 2247 } 2248 header = (struct pp2_header*)sldns_buffer_begin(c->buffer); 2249 want_read_size = ntohs(header->len); 2250 if(sldns_buffer_limit(c->buffer) < 2251 PP2_HEADER_SIZE + want_read_size) { 2252 log_err_addr("proxy_protocol: not enough " 2253 "buffer size to read PROXYv2 header", "", 2254 &c->repinfo.remote_addr, 2255 c->repinfo.remote_addrlen); 2256 return 0; 2257 } 2258 verbose(VERB_ALGO, "proxy_protocol: reading variable " 2259 "part of PROXYv2 header (len %lu)", 2260 (unsigned long)want_read_size); 2261 current_read_size = PP2_HEADER_SIZE + want_read_size; 2262 if(want_read_size == 0) { 2263 /* nothing more to read; header is complete */ 2264 c->pp2_header_state = pp2_header_done; 2265 } else if(c->tcp_byte_count < current_read_size) { 2266 r = recv(fd, (void*)sldns_buffer_at(c->buffer, 2267 c->tcp_byte_count), 2268 current_read_size-c->tcp_byte_count, MSG_DONTWAIT); 2269 if(r == 0) { 2270 if(c->tcp_req_info) 2271 return tcp_req_info_handle_read_close(c->tcp_req_info); 2272 return 0; 2273 } else if(r == -1) { 2274 goto recv_error; 2275 } 2276 c->tcp_byte_count += r; 2277 sldns_buffer_skip(c->buffer, r); 2278 if(c->tcp_byte_count != current_read_size) return 1; 2279 c->pp2_header_state = pp2_header_done; 2280 } 2281 } 2282 if(c->pp2_header_state != pp2_header_done || !header) { 2283 log_err_addr("proxy_protocol: wrong state for the " 2284 "PROXYv2 header", "", &c->repinfo.remote_addr, 2285 c->repinfo.remote_addrlen); 2286 return 0; 2287 } 2288 sldns_buffer_flip(c->buffer); 2289 if(!consume_pp2_header(c->buffer, &c->repinfo, 1)) { 2290 log_err_addr("proxy_protocol: could not consume " 2291 "PROXYv2 header", "", &c->repinfo.remote_addr, 2292 c->repinfo.remote_addrlen); 2293 return 0; 2294 } 2295 verbose(VERB_ALGO, "proxy_protocol: successful read of " 2296 "PROXYv2 header"); 2297 /* Clear and reset the buffer to read the following 2298 * DNS packet(s). */ 2299 sldns_buffer_clear(c->buffer); 2300 c->tcp_byte_count = 0; 2301 return 1; 2302 } 2303 2304 if(c->tcp_byte_count < sizeof(uint16_t)) { 2305 /* read length bytes */ 2306 r = recv(fd,(void*)sldns_buffer_at(c->buffer,c->tcp_byte_count), 2307 sizeof(uint16_t)-c->tcp_byte_count, MSG_DONTWAIT); 2308 if(r == 0) { 2309 if(c->tcp_req_info) 2310 return tcp_req_info_handle_read_close(c->tcp_req_info); 2311 return 0; 2312 } else if(r == -1) { 2313 if(c->pp2_enabled) goto recv_error; 2314 goto recv_error_initial; 2315 } 2316 c->tcp_byte_count += r; 2317 if(c->tcp_byte_count != sizeof(uint16_t)) 2318 return 1; 2319 if(sldns_buffer_read_u16_at(c->buffer, 0) > 2320 sldns_buffer_capacity(c->buffer)) { 2321 verbose(VERB_QUERY, "tcp: dropped larger than buffer"); 2322 return 0; 2323 } 2324 sldns_buffer_set_limit(c->buffer, 2325 sldns_buffer_read_u16_at(c->buffer, 0)); 2326 if(!short_ok && 2327 sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) { 2328 verbose(VERB_QUERY, "tcp: dropped bogus too short."); 2329 return 0; 2330 } 2331 verbose(VERB_ALGO, "Reading tcp query of length %d", 2332 (int)sldns_buffer_limit(c->buffer)); 2333 } 2334 2335 if(sldns_buffer_remaining(c->buffer) == 0) 2336 log_err("in comm_point_tcp_handle_read buffer_remaining is " 2337 "not > 0 as expected, continuing with (harmless) 0 " 2338 "length recv"); 2339 r = recv(fd, (void*)sldns_buffer_current(c->buffer), 2340 sldns_buffer_remaining(c->buffer), MSG_DONTWAIT); 2341 if(r == 0) { 2342 if(c->tcp_req_info) 2343 return tcp_req_info_handle_read_close(c->tcp_req_info); 2344 return 0; 2345 } else if(r == -1) { 2346 goto recv_error; 2347 } 2348 sldns_buffer_skip(c->buffer, r); 2349 if(sldns_buffer_remaining(c->buffer) <= 0) { 2350 tcp_callback_reader(c); 2351 } 2352 return 1; 2353 2354 recv_error_initial: 2355 recv_initial = 1; 2356 recv_error: 2357 #ifndef USE_WINSOCK 2358 if(errno == EINTR || errno == EAGAIN) 2359 return 1; 2360 if(recv_initial) { 2361 #ifdef ECONNRESET 2362 if(errno == ECONNRESET && verbosity < 2) 2363 return 0; /* silence reset by peer */ 2364 #endif 2365 #ifdef ECONNREFUSED 2366 if(errno == ECONNREFUSED && verbosity < 2) 2367 return 0; /* silence reset by peer */ 2368 #endif 2369 #ifdef ENETUNREACH 2370 if(errno == ENETUNREACH && verbosity < 2) 2371 return 0; /* silence it */ 2372 #endif 2373 #ifdef EHOSTDOWN 2374 if(errno == EHOSTDOWN && verbosity < 2) 2375 return 0; /* silence it */ 2376 #endif 2377 #ifdef EHOSTUNREACH 2378 if(errno == EHOSTUNREACH && verbosity < 2) 2379 return 0; /* silence it */ 2380 #endif 2381 #ifdef ENETDOWN 2382 if(errno == ENETDOWN && verbosity < 2) 2383 return 0; /* silence it */ 2384 #endif 2385 #ifdef EACCES 2386 if(errno == EACCES && verbosity < 2) 2387 return 0; /* silence it */ 2388 #endif 2389 #ifdef ENOTCONN 2390 if(errno == ENOTCONN) { 2391 log_err_addr("read (in tcp s) failed and this " 2392 "could be because TCP Fast Open is " 2393 "enabled [--disable-tfo-client " 2394 "--disable-tfo-server] but does not " 2395 "work", sock_strerror(errno), 2396 &c->repinfo.remote_addr, 2397 c->repinfo.remote_addrlen); 2398 return 0; 2399 } 2400 #endif 2401 } 2402 #else /* USE_WINSOCK */ 2403 if(recv_initial) { 2404 if(WSAGetLastError() == WSAECONNREFUSED && verbosity < 2) 2405 return 0; 2406 if(WSAGetLastError() == WSAEHOSTDOWN && verbosity < 2) 2407 return 0; 2408 if(WSAGetLastError() == WSAEHOSTUNREACH && verbosity < 2) 2409 return 0; 2410 if(WSAGetLastError() == WSAENETDOWN && verbosity < 2) 2411 return 0; 2412 if(WSAGetLastError() == WSAENETUNREACH && verbosity < 2) 2413 return 0; 2414 } 2415 if(WSAGetLastError() == WSAECONNRESET) 2416 return 0; 2417 if(WSAGetLastError() == WSAEINPROGRESS) 2418 return 1; 2419 if(WSAGetLastError() == WSAEWOULDBLOCK) { 2420 ub_winsock_tcp_wouldblock(c->ev->ev, 2421 UB_EV_READ); 2422 return 1; 2423 } 2424 #endif 2425 log_err_addr("read (in tcp s)", sock_strerror(errno), 2426 &c->repinfo.remote_addr, c->repinfo.remote_addrlen); 2427 return 0; 2428 } 2429 2430 /** 2431 * Handle tcp writing callback. 2432 * @param fd: file descriptor of socket. 2433 * @param c: comm point to write buffer out of. 2434 * @return: 0 on error 2435 */ 2436 static int 2437 comm_point_tcp_handle_write(int fd, struct comm_point* c) 2438 { 2439 ssize_t r; 2440 struct sldns_buffer *buffer; 2441 log_assert(c->type == comm_tcp); 2442 #ifdef USE_DNSCRYPT 2443 buffer = c->dnscrypt_buffer; 2444 #else 2445 buffer = c->buffer; 2446 #endif 2447 if(c->tcp_is_reading && !c->ssl && !c->tcp_write_and_read) 2448 return 0; 2449 log_assert(fd != -1); 2450 if(((!c->tcp_write_and_read && c->tcp_byte_count == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == 0)) && c->tcp_check_nb_connect) { 2451 /* check for pending error from nonblocking connect */ 2452 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/ 2453 int error = 0; 2454 socklen_t len = (socklen_t)sizeof(error); 2455 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error, 2456 &len) < 0){ 2457 #ifndef USE_WINSOCK 2458 error = errno; /* on solaris errno is error */ 2459 #else /* USE_WINSOCK */ 2460 error = WSAGetLastError(); 2461 #endif 2462 } 2463 #ifndef USE_WINSOCK 2464 #if defined(EINPROGRESS) && defined(EWOULDBLOCK) 2465 if(error == EINPROGRESS || error == EWOULDBLOCK) 2466 return 1; /* try again later */ 2467 else 2468 #endif 2469 if(error != 0 && verbosity < 2) 2470 return 0; /* silence lots of chatter in the logs */ 2471 else if(error != 0) { 2472 log_err_addr("tcp connect", strerror(error), 2473 &c->repinfo.remote_addr, 2474 c->repinfo.remote_addrlen); 2475 #else /* USE_WINSOCK */ 2476 /* examine error */ 2477 if(error == WSAEINPROGRESS) 2478 return 1; 2479 else if(error == WSAEWOULDBLOCK) { 2480 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 2481 return 1; 2482 } else if(error != 0 && verbosity < 2) 2483 return 0; 2484 else if(error != 0) { 2485 log_err_addr("tcp connect", wsa_strerror(error), 2486 &c->repinfo.remote_addr, 2487 c->repinfo.remote_addrlen); 2488 #endif /* USE_WINSOCK */ 2489 return 0; 2490 } 2491 } 2492 if(c->ssl) 2493 return ssl_handle_it(c, 1); 2494 2495 #ifdef USE_MSG_FASTOPEN 2496 /* Only try this on first use of a connection that uses tfo, 2497 otherwise fall through to normal write */ 2498 /* Also, TFO support on WINDOWS not implemented at the moment */ 2499 if(c->tcp_do_fastopen == 1) { 2500 /* this form of sendmsg() does both a connect() and send() so need to 2501 look for various flavours of error*/ 2502 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer)); 2503 struct msghdr msg; 2504 struct iovec iov[2]; 2505 c->tcp_do_fastopen = 0; 2506 memset(&msg, 0, sizeof(msg)); 2507 if(c->tcp_write_and_read) { 2508 iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count; 2509 iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count; 2510 iov[1].iov_base = c->tcp_write_pkt; 2511 iov[1].iov_len = c->tcp_write_pkt_len; 2512 } else { 2513 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count; 2514 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count; 2515 iov[1].iov_base = sldns_buffer_begin(buffer); 2516 iov[1].iov_len = sldns_buffer_limit(buffer); 2517 } 2518 log_assert(iov[0].iov_len > 0); 2519 msg.msg_name = &c->repinfo.remote_addr; 2520 msg.msg_namelen = c->repinfo.remote_addrlen; 2521 msg.msg_iov = iov; 2522 msg.msg_iovlen = 2; 2523 r = sendmsg(fd, &msg, MSG_FASTOPEN); 2524 if (r == -1) { 2525 #if defined(EINPROGRESS) && defined(EWOULDBLOCK) 2526 /* Handshake is underway, maybe because no TFO cookie available. 2527 Come back to write the message*/ 2528 if(errno == EINPROGRESS || errno == EWOULDBLOCK) 2529 return 1; 2530 #endif 2531 if(errno == EINTR || errno == EAGAIN) 2532 return 1; 2533 /* Not handling EISCONN here as shouldn't ever hit that case.*/ 2534 if(errno != EPIPE 2535 #ifdef EOPNOTSUPP 2536 /* if /proc/sys/net/ipv4/tcp_fastopen is 2537 * disabled on Linux, sendmsg may return 2538 * 'Operation not supported', if so 2539 * fallthrough to ordinary connect. */ 2540 && errno != EOPNOTSUPP 2541 #endif 2542 && errno != 0) { 2543 if(verbosity < 2) 2544 return 0; /* silence lots of chatter in the logs */ 2545 log_err_addr("tcp sendmsg", strerror(errno), 2546 &c->repinfo.remote_addr, 2547 c->repinfo.remote_addrlen); 2548 return 0; 2549 } 2550 verbose(VERB_ALGO, "tcp sendmsg for fastopen failed (with %s), try normal connect", strerror(errno)); 2551 /* fallthrough to nonFASTOPEN 2552 * (MSG_FASTOPEN on Linux 3 produces EPIPE) 2553 * we need to perform connect() */ 2554 if(connect(fd, (struct sockaddr *)&c->repinfo.remote_addr, 2555 c->repinfo.remote_addrlen) == -1) { 2556 #ifdef EINPROGRESS 2557 if(errno == EINPROGRESS) 2558 return 1; /* wait until connect done*/ 2559 #endif 2560 #ifdef USE_WINSOCK 2561 if(WSAGetLastError() == WSAEINPROGRESS || 2562 WSAGetLastError() == WSAEWOULDBLOCK) 2563 return 1; /* wait until connect done*/ 2564 #endif 2565 if(tcp_connect_errno_needs_log( 2566 (struct sockaddr *)&c->repinfo.remote_addr, 2567 c->repinfo.remote_addrlen)) { 2568 log_err_addr("outgoing tcp: connect after EPIPE for fastopen", 2569 strerror(errno), 2570 &c->repinfo.remote_addr, 2571 c->repinfo.remote_addrlen); 2572 } 2573 return 0; 2574 } 2575 2576 } else { 2577 if(c->tcp_write_and_read) { 2578 c->tcp_write_byte_count += r; 2579 if(c->tcp_write_byte_count < sizeof(uint16_t)) 2580 return 1; 2581 } else { 2582 c->tcp_byte_count += r; 2583 if(c->tcp_byte_count < sizeof(uint16_t)) 2584 return 1; 2585 sldns_buffer_set_position(buffer, c->tcp_byte_count - 2586 sizeof(uint16_t)); 2587 } 2588 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) { 2589 tcp_callback_writer(c); 2590 return 1; 2591 } 2592 } 2593 } 2594 #endif /* USE_MSG_FASTOPEN */ 2595 2596 if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) { 2597 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer)); 2598 #ifdef HAVE_WRITEV 2599 struct iovec iov[2]; 2600 if(c->tcp_write_and_read) { 2601 iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count; 2602 iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count; 2603 iov[1].iov_base = c->tcp_write_pkt; 2604 iov[1].iov_len = c->tcp_write_pkt_len; 2605 } else { 2606 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count; 2607 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count; 2608 iov[1].iov_base = sldns_buffer_begin(buffer); 2609 iov[1].iov_len = sldns_buffer_limit(buffer); 2610 } 2611 log_assert(iov[0].iov_len > 0); 2612 r = writev(fd, iov, 2); 2613 #else /* HAVE_WRITEV */ 2614 if(c->tcp_write_and_read) { 2615 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_write_byte_count), 2616 sizeof(uint16_t)-c->tcp_write_byte_count, 0); 2617 } else { 2618 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count), 2619 sizeof(uint16_t)-c->tcp_byte_count, 0); 2620 } 2621 #endif /* HAVE_WRITEV */ 2622 if(r == -1) { 2623 #ifndef USE_WINSOCK 2624 # ifdef EPIPE 2625 if(errno == EPIPE && verbosity < 2) 2626 return 0; /* silence 'broken pipe' */ 2627 #endif 2628 if(errno == EINTR || errno == EAGAIN) 2629 return 1; 2630 #ifdef ECONNRESET 2631 if(errno == ECONNRESET && verbosity < 2) 2632 return 0; /* silence reset by peer */ 2633 #endif 2634 # ifdef HAVE_WRITEV 2635 log_err_addr("tcp writev", strerror(errno), 2636 &c->repinfo.remote_addr, 2637 c->repinfo.remote_addrlen); 2638 # else /* HAVE_WRITEV */ 2639 log_err_addr("tcp send s", strerror(errno), 2640 &c->repinfo.remote_addr, 2641 c->repinfo.remote_addrlen); 2642 # endif /* HAVE_WRITEV */ 2643 #else 2644 if(WSAGetLastError() == WSAENOTCONN) 2645 return 1; 2646 if(WSAGetLastError() == WSAEINPROGRESS) 2647 return 1; 2648 if(WSAGetLastError() == WSAEWOULDBLOCK) { 2649 ub_winsock_tcp_wouldblock(c->ev->ev, 2650 UB_EV_WRITE); 2651 return 1; 2652 } 2653 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2) 2654 return 0; /* silence reset by peer */ 2655 log_err_addr("tcp send s", 2656 wsa_strerror(WSAGetLastError()), 2657 &c->repinfo.remote_addr, 2658 c->repinfo.remote_addrlen); 2659 #endif 2660 return 0; 2661 } 2662 if(c->tcp_write_and_read) { 2663 c->tcp_write_byte_count += r; 2664 if(c->tcp_write_byte_count < sizeof(uint16_t)) 2665 return 1; 2666 } else { 2667 c->tcp_byte_count += r; 2668 if(c->tcp_byte_count < sizeof(uint16_t)) 2669 return 1; 2670 sldns_buffer_set_position(buffer, c->tcp_byte_count - 2671 sizeof(uint16_t)); 2672 } 2673 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) { 2674 tcp_callback_writer(c); 2675 return 1; 2676 } 2677 } 2678 log_assert(c->tcp_write_and_read || sldns_buffer_remaining(buffer) > 0); 2679 log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2); 2680 if(c->tcp_write_and_read) { 2681 r = send(fd, (void*)(c->tcp_write_pkt + c->tcp_write_byte_count - 2), 2682 c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count, 0); 2683 } else { 2684 r = send(fd, (void*)sldns_buffer_current(buffer), 2685 sldns_buffer_remaining(buffer), 0); 2686 } 2687 if(r == -1) { 2688 #ifndef USE_WINSOCK 2689 if(errno == EINTR || errno == EAGAIN) 2690 return 1; 2691 #ifdef ECONNRESET 2692 if(errno == ECONNRESET && verbosity < 2) 2693 return 0; /* silence reset by peer */ 2694 #endif 2695 #else 2696 if(WSAGetLastError() == WSAEINPROGRESS) 2697 return 1; 2698 if(WSAGetLastError() == WSAEWOULDBLOCK) { 2699 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 2700 return 1; 2701 } 2702 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2) 2703 return 0; /* silence reset by peer */ 2704 #endif 2705 log_err_addr("tcp send r", sock_strerror(errno), 2706 &c->repinfo.remote_addr, 2707 c->repinfo.remote_addrlen); 2708 return 0; 2709 } 2710 if(c->tcp_write_and_read) { 2711 c->tcp_write_byte_count += r; 2712 } else { 2713 sldns_buffer_skip(buffer, r); 2714 } 2715 2716 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) { 2717 tcp_callback_writer(c); 2718 } 2719 2720 return 1; 2721 } 2722 2723 /** read again to drain buffers when there could be more to read, returns 0 2724 * on failure which means the comm point is closed. */ 2725 static int 2726 tcp_req_info_read_again(int fd, struct comm_point* c) 2727 { 2728 while(c->tcp_req_info->read_again) { 2729 int r; 2730 c->tcp_req_info->read_again = 0; 2731 if(c->tcp_is_reading) 2732 r = comm_point_tcp_handle_read(fd, c, 0); 2733 else r = comm_point_tcp_handle_write(fd, c); 2734 if(!r) { 2735 reclaim_tcp_handler(c); 2736 if(!c->tcp_do_close) { 2737 fptr_ok(fptr_whitelist_comm_point( 2738 c->callback)); 2739 (void)(*c->callback)(c, c->cb_arg, 2740 NETEVENT_CLOSED, NULL); 2741 } 2742 return 0; 2743 } 2744 } 2745 return 1; 2746 } 2747 2748 /** read again to drain buffers when there could be more to read */ 2749 static void 2750 tcp_more_read_again(int fd, struct comm_point* c) 2751 { 2752 /* if the packet is done, but another one could be waiting on 2753 * the connection, the callback signals this, and we try again */ 2754 /* this continues until the read routines get EAGAIN or so, 2755 * and thus does not call the callback, and the bool is 0 */ 2756 int* moreread = c->tcp_more_read_again; 2757 while(moreread && *moreread) { 2758 *moreread = 0; 2759 if(!comm_point_tcp_handle_read(fd, c, 0)) { 2760 reclaim_tcp_handler(c); 2761 if(!c->tcp_do_close) { 2762 fptr_ok(fptr_whitelist_comm_point( 2763 c->callback)); 2764 (void)(*c->callback)(c, c->cb_arg, 2765 NETEVENT_CLOSED, NULL); 2766 } 2767 return; 2768 } 2769 } 2770 } 2771 2772 /** write again to fill up when there could be more to write */ 2773 static void 2774 tcp_more_write_again(int fd, struct comm_point* c) 2775 { 2776 /* if the packet is done, but another is waiting to be written, 2777 * the callback signals it and we try again. */ 2778 /* this continues until the write routines get EAGAIN or so, 2779 * and thus does not call the callback, and the bool is 0 */ 2780 int* morewrite = c->tcp_more_write_again; 2781 while(morewrite && *morewrite) { 2782 *morewrite = 0; 2783 if(!comm_point_tcp_handle_write(fd, c)) { 2784 reclaim_tcp_handler(c); 2785 if(!c->tcp_do_close) { 2786 fptr_ok(fptr_whitelist_comm_point( 2787 c->callback)); 2788 (void)(*c->callback)(c, c->cb_arg, 2789 NETEVENT_CLOSED, NULL); 2790 } 2791 return; 2792 } 2793 } 2794 } 2795 2796 void 2797 comm_point_tcp_handle_callback(int fd, short event, void* arg) 2798 { 2799 struct comm_point* c = (struct comm_point*)arg; 2800 log_assert(c->type == comm_tcp); 2801 ub_comm_base_now(c->ev->base); 2802 2803 if(c->fd == -1 || c->fd != fd) 2804 return; /* duplicate event, but commpoint closed. */ 2805 2806 #ifdef USE_DNSCRYPT 2807 /* Initialize if this is a dnscrypt socket */ 2808 if(c->tcp_parent) { 2809 c->dnscrypt = c->tcp_parent->dnscrypt; 2810 } 2811 if(c->dnscrypt && c->dnscrypt_buffer == c->buffer) { 2812 c->dnscrypt_buffer = sldns_buffer_new(sldns_buffer_capacity(c->buffer)); 2813 if(!c->dnscrypt_buffer) { 2814 log_err("Could not allocate dnscrypt buffer"); 2815 reclaim_tcp_handler(c); 2816 if(!c->tcp_do_close) { 2817 fptr_ok(fptr_whitelist_comm_point( 2818 c->callback)); 2819 (void)(*c->callback)(c, c->cb_arg, 2820 NETEVENT_CLOSED, NULL); 2821 } 2822 return; 2823 } 2824 } 2825 #endif 2826 2827 if(event&UB_EV_TIMEOUT) { 2828 verbose(VERB_QUERY, "tcp took too long, dropped"); 2829 reclaim_tcp_handler(c); 2830 if(!c->tcp_do_close) { 2831 fptr_ok(fptr_whitelist_comm_point(c->callback)); 2832 (void)(*c->callback)(c, c->cb_arg, 2833 NETEVENT_TIMEOUT, NULL); 2834 } 2835 return; 2836 } 2837 if(event&UB_EV_READ 2838 #ifdef USE_MSG_FASTOPEN 2839 && !(c->tcp_do_fastopen && (event&UB_EV_WRITE)) 2840 #endif 2841 ) { 2842 int has_tcpq = (c->tcp_req_info != NULL); 2843 int* moreread = c->tcp_more_read_again; 2844 if(!comm_point_tcp_handle_read(fd, c, 0)) { 2845 reclaim_tcp_handler(c); 2846 if(!c->tcp_do_close) { 2847 fptr_ok(fptr_whitelist_comm_point( 2848 c->callback)); 2849 (void)(*c->callback)(c, c->cb_arg, 2850 NETEVENT_CLOSED, NULL); 2851 } 2852 return; 2853 } 2854 if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again) { 2855 if(!tcp_req_info_read_again(fd, c)) 2856 return; 2857 } 2858 if(moreread && *moreread) 2859 tcp_more_read_again(fd, c); 2860 return; 2861 } 2862 if(event&UB_EV_WRITE) { 2863 int has_tcpq = (c->tcp_req_info != NULL); 2864 int* morewrite = c->tcp_more_write_again; 2865 if(!comm_point_tcp_handle_write(fd, c)) { 2866 reclaim_tcp_handler(c); 2867 if(!c->tcp_do_close) { 2868 fptr_ok(fptr_whitelist_comm_point( 2869 c->callback)); 2870 (void)(*c->callback)(c, c->cb_arg, 2871 NETEVENT_CLOSED, NULL); 2872 } 2873 return; 2874 } 2875 if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again) { 2876 if(!tcp_req_info_read_again(fd, c)) 2877 return; 2878 } 2879 if(morewrite && *morewrite) 2880 tcp_more_write_again(fd, c); 2881 return; 2882 } 2883 log_err("Ignored event %d for tcphdl.", event); 2884 } 2885 2886 /** Make http handler free for next assignment */ 2887 static void 2888 reclaim_http_handler(struct comm_point* c) 2889 { 2890 log_assert(c->type == comm_http); 2891 if(c->ssl) { 2892 #ifdef HAVE_SSL 2893 SSL_shutdown(c->ssl); 2894 SSL_free(c->ssl); 2895 c->ssl = NULL; 2896 #endif 2897 } 2898 comm_point_close(c); 2899 if(c->tcp_parent) { 2900 if(c != c->tcp_parent->tcp_free) { 2901 c->tcp_parent->cur_tcp_count--; 2902 c->tcp_free = c->tcp_parent->tcp_free; 2903 c->tcp_parent->tcp_free = c; 2904 } 2905 if(!c->tcp_free) { 2906 /* re-enable listening on accept socket */ 2907 comm_point_start_listening(c->tcp_parent, -1, -1); 2908 } 2909 } 2910 } 2911 2912 /** read more data for http (with ssl) */ 2913 static int 2914 ssl_http_read_more(struct comm_point* c) 2915 { 2916 #ifdef HAVE_SSL 2917 int r; 2918 log_assert(sldns_buffer_remaining(c->buffer) > 0); 2919 ERR_clear_error(); 2920 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer), 2921 (int)sldns_buffer_remaining(c->buffer)); 2922 if(r <= 0) { 2923 int want = SSL_get_error(c->ssl, r); 2924 if(want == SSL_ERROR_ZERO_RETURN) { 2925 return 0; /* shutdown, closed */ 2926 } else if(want == SSL_ERROR_WANT_READ) { 2927 return 1; /* read more later */ 2928 } else if(want == SSL_ERROR_WANT_WRITE) { 2929 c->ssl_shake_state = comm_ssl_shake_hs_write; 2930 comm_point_listen_for_rw(c, 0, 1); 2931 return 1; 2932 } else if(want == SSL_ERROR_SYSCALL) { 2933 #ifdef ECONNRESET 2934 if(errno == ECONNRESET && verbosity < 2) 2935 return 0; /* silence reset by peer */ 2936 #endif 2937 if(errno != 0) 2938 log_err("SSL_read syscall: %s", 2939 strerror(errno)); 2940 return 0; 2941 } 2942 log_crypto_err_io("could not SSL_read", want); 2943 return 0; 2944 } 2945 verbose(VERB_ALGO, "ssl http read more skip to %d + %d", 2946 (int)sldns_buffer_position(c->buffer), (int)r); 2947 sldns_buffer_skip(c->buffer, (ssize_t)r); 2948 return 1; 2949 #else 2950 (void)c; 2951 return 0; 2952 #endif /* HAVE_SSL */ 2953 } 2954 2955 /** read more data for http */ 2956 static int 2957 http_read_more(int fd, struct comm_point* c) 2958 { 2959 ssize_t r; 2960 log_assert(sldns_buffer_remaining(c->buffer) > 0); 2961 r = recv(fd, (void*)sldns_buffer_current(c->buffer), 2962 sldns_buffer_remaining(c->buffer), MSG_DONTWAIT); 2963 if(r == 0) { 2964 return 0; 2965 } else if(r == -1) { 2966 #ifndef USE_WINSOCK 2967 if(errno == EINTR || errno == EAGAIN) 2968 return 1; 2969 #else /* USE_WINSOCK */ 2970 if(WSAGetLastError() == WSAECONNRESET) 2971 return 0; 2972 if(WSAGetLastError() == WSAEINPROGRESS) 2973 return 1; 2974 if(WSAGetLastError() == WSAEWOULDBLOCK) { 2975 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 2976 return 1; 2977 } 2978 #endif 2979 log_err_addr("read (in http r)", sock_strerror(errno), 2980 &c->repinfo.remote_addr, c->repinfo.remote_addrlen); 2981 return 0; 2982 } 2983 verbose(VERB_ALGO, "http read more skip to %d + %d", 2984 (int)sldns_buffer_position(c->buffer), (int)r); 2985 sldns_buffer_skip(c->buffer, r); 2986 return 1; 2987 } 2988 2989 /** return true if http header has been read (one line complete) */ 2990 static int 2991 http_header_done(sldns_buffer* buf) 2992 { 2993 size_t i; 2994 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) { 2995 /* there was a \r before the \n, but we ignore that */ 2996 if((char)sldns_buffer_read_u8_at(buf, i) == '\n') 2997 return 1; 2998 } 2999 return 0; 3000 } 3001 3002 /** return character string into buffer for header line, moves buffer 3003 * past that line and puts zero terminator into linefeed-newline */ 3004 static char* 3005 http_header_line(sldns_buffer* buf) 3006 { 3007 char* result = (char*)sldns_buffer_current(buf); 3008 size_t i; 3009 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) { 3010 /* terminate the string on the \r */ 3011 if((char)sldns_buffer_read_u8_at(buf, i) == '\r') 3012 sldns_buffer_write_u8_at(buf, i, 0); 3013 /* terminate on the \n and skip past the it and done */ 3014 if((char)sldns_buffer_read_u8_at(buf, i) == '\n') { 3015 sldns_buffer_write_u8_at(buf, i, 0); 3016 sldns_buffer_set_position(buf, i+1); 3017 return result; 3018 } 3019 } 3020 return NULL; 3021 } 3022 3023 /** move unread buffer to start and clear rest for putting the rest into it */ 3024 static void 3025 http_moveover_buffer(sldns_buffer* buf) 3026 { 3027 size_t pos = sldns_buffer_position(buf); 3028 size_t len = sldns_buffer_remaining(buf); 3029 sldns_buffer_clear(buf); 3030 memmove(sldns_buffer_begin(buf), sldns_buffer_at(buf, pos), len); 3031 sldns_buffer_set_position(buf, len); 3032 } 3033 3034 /** a http header is complete, process it */ 3035 static int 3036 http_process_initial_header(struct comm_point* c) 3037 { 3038 char* line = http_header_line(c->buffer); 3039 if(!line) return 1; 3040 verbose(VERB_ALGO, "http header: %s", line); 3041 if(strncasecmp(line, "HTTP/1.1 ", 9) == 0) { 3042 /* check returncode */ 3043 if(line[9] != '2') { 3044 verbose(VERB_ALGO, "http bad status %s", line+9); 3045 return 0; 3046 } 3047 } else if(strncasecmp(line, "Content-Length: ", 16) == 0) { 3048 if(!c->http_is_chunked) 3049 c->tcp_byte_count = (size_t)atoi(line+16); 3050 } else if(strncasecmp(line, "Transfer-Encoding: chunked", 19+7) == 0) { 3051 c->tcp_byte_count = 0; 3052 c->http_is_chunked = 1; 3053 } else if(line[0] == 0) { 3054 /* end of initial headers */ 3055 c->http_in_headers = 0; 3056 if(c->http_is_chunked) 3057 c->http_in_chunk_headers = 1; 3058 /* remove header text from front of buffer 3059 * the buffer is going to be used to return the data segment 3060 * itself and we don't want the header to get returned 3061 * prepended with it */ 3062 http_moveover_buffer(c->buffer); 3063 sldns_buffer_flip(c->buffer); 3064 return 1; 3065 } 3066 /* ignore other headers */ 3067 return 1; 3068 } 3069 3070 /** a chunk header is complete, process it, return 0=fail, 1=continue next 3071 * header line, 2=done with chunked transfer*/ 3072 static int 3073 http_process_chunk_header(struct comm_point* c) 3074 { 3075 char* line = http_header_line(c->buffer); 3076 if(!line) return 1; 3077 if(c->http_in_chunk_headers == 3) { 3078 verbose(VERB_ALGO, "http chunk trailer: %s", line); 3079 /* are we done ? */ 3080 if(line[0] == 0 && c->tcp_byte_count == 0) { 3081 /* callback of http reader when NETEVENT_DONE, 3082 * end of data, with no data in buffer */ 3083 sldns_buffer_set_position(c->buffer, 0); 3084 sldns_buffer_set_limit(c->buffer, 0); 3085 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3086 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL); 3087 /* return that we are done */ 3088 return 2; 3089 } 3090 if(line[0] == 0) { 3091 /* continue with header of the next chunk */ 3092 c->http_in_chunk_headers = 1; 3093 /* remove header text from front of buffer */ 3094 http_moveover_buffer(c->buffer); 3095 sldns_buffer_flip(c->buffer); 3096 return 1; 3097 } 3098 /* ignore further trail headers */ 3099 return 1; 3100 } 3101 verbose(VERB_ALGO, "http chunk header: %s", line); 3102 if(c->http_in_chunk_headers == 1) { 3103 /* read chunked start line */ 3104 char* end = NULL; 3105 c->tcp_byte_count = (size_t)strtol(line, &end, 16); 3106 if(end == line) 3107 return 0; 3108 c->http_in_chunk_headers = 0; 3109 /* remove header text from front of buffer */ 3110 http_moveover_buffer(c->buffer); 3111 sldns_buffer_flip(c->buffer); 3112 if(c->tcp_byte_count == 0) { 3113 /* done with chunks, process chunk_trailer lines */ 3114 c->http_in_chunk_headers = 3; 3115 } 3116 return 1; 3117 } 3118 /* ignore other headers */ 3119 return 1; 3120 } 3121 3122 /** handle nonchunked data segment, 0=fail, 1=wait */ 3123 static int 3124 http_nonchunk_segment(struct comm_point* c) 3125 { 3126 /* c->buffer at position..limit has new data we read in. 3127 * the buffer itself is full of nonchunked data. 3128 * we are looking to read tcp_byte_count more data 3129 * and then the transfer is done. */ 3130 size_t remainbufferlen; 3131 size_t got_now = sldns_buffer_limit(c->buffer); 3132 if(c->tcp_byte_count <= got_now) { 3133 /* done, this is the last data fragment */ 3134 c->http_stored = 0; 3135 sldns_buffer_set_position(c->buffer, 0); 3136 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3137 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL); 3138 return 1; 3139 } 3140 /* if we have the buffer space, 3141 * read more data collected into the buffer */ 3142 remainbufferlen = sldns_buffer_capacity(c->buffer) - 3143 sldns_buffer_limit(c->buffer); 3144 if(remainbufferlen+got_now >= c->tcp_byte_count || 3145 remainbufferlen >= (size_t)(c->ssl?16384:2048)) { 3146 size_t total = sldns_buffer_limit(c->buffer); 3147 sldns_buffer_clear(c->buffer); 3148 sldns_buffer_set_position(c->buffer, total); 3149 c->http_stored = total; 3150 /* return and wait to read more */ 3151 return 1; 3152 } 3153 /* call callback with this data amount, then 3154 * wait for more */ 3155 c->tcp_byte_count -= got_now; 3156 c->http_stored = 0; 3157 sldns_buffer_set_position(c->buffer, 0); 3158 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3159 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL); 3160 /* c->callback has to buffer_clear(c->buffer). */ 3161 /* return and wait to read more */ 3162 return 1; 3163 } 3164 3165 /** handle chunked data segment, return 0=fail, 1=wait, 2=process more */ 3166 static int 3167 http_chunked_segment(struct comm_point* c) 3168 { 3169 /* the c->buffer has from position..limit new data we read. */ 3170 /* the current chunk has length tcp_byte_count. 3171 * once we read that read more chunk headers. 3172 */ 3173 size_t remainbufferlen; 3174 size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored; 3175 verbose(VERB_ALGO, "http_chunked_segment: got now %d, tcpbytcount %d, http_stored %d, buffer pos %d, buffer limit %d", (int)got_now, (int)c->tcp_byte_count, (int)c->http_stored, (int)sldns_buffer_position(c->buffer), (int)sldns_buffer_limit(c->buffer)); 3176 if(c->tcp_byte_count <= got_now) { 3177 /* the chunk has completed (with perhaps some extra data 3178 * from next chunk header and next chunk) */ 3179 /* save too much info into temp buffer */ 3180 size_t fraglen; 3181 struct comm_reply repinfo; 3182 c->http_stored = 0; 3183 sldns_buffer_skip(c->buffer, (ssize_t)c->tcp_byte_count); 3184 sldns_buffer_clear(c->http_temp); 3185 sldns_buffer_write(c->http_temp, 3186 sldns_buffer_current(c->buffer), 3187 sldns_buffer_remaining(c->buffer)); 3188 sldns_buffer_flip(c->http_temp); 3189 3190 /* callback with this fragment */ 3191 fraglen = sldns_buffer_position(c->buffer); 3192 sldns_buffer_set_position(c->buffer, 0); 3193 sldns_buffer_set_limit(c->buffer, fraglen); 3194 repinfo = c->repinfo; 3195 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3196 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &repinfo); 3197 /* c->callback has to buffer_clear(). */ 3198 3199 /* is commpoint deleted? */ 3200 if(!repinfo.c) { 3201 return 1; 3202 } 3203 /* copy waiting info */ 3204 sldns_buffer_clear(c->buffer); 3205 sldns_buffer_write(c->buffer, 3206 sldns_buffer_begin(c->http_temp), 3207 sldns_buffer_remaining(c->http_temp)); 3208 sldns_buffer_flip(c->buffer); 3209 /* process end of chunk trailer header lines, until 3210 * an empty line */ 3211 c->http_in_chunk_headers = 3; 3212 /* process more data in buffer (if any) */ 3213 return 2; 3214 } 3215 c->tcp_byte_count -= got_now; 3216 3217 /* if we have the buffer space, 3218 * read more data collected into the buffer */ 3219 remainbufferlen = sldns_buffer_capacity(c->buffer) - 3220 sldns_buffer_limit(c->buffer); 3221 if(remainbufferlen >= c->tcp_byte_count || 3222 remainbufferlen >= 2048) { 3223 size_t total = sldns_buffer_limit(c->buffer); 3224 sldns_buffer_clear(c->buffer); 3225 sldns_buffer_set_position(c->buffer, total); 3226 c->http_stored = total; 3227 /* return and wait to read more */ 3228 return 1; 3229 } 3230 3231 /* callback of http reader for a new part of the data */ 3232 c->http_stored = 0; 3233 sldns_buffer_set_position(c->buffer, 0); 3234 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3235 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL); 3236 /* c->callback has to buffer_clear(c->buffer). */ 3237 /* return and wait to read more */ 3238 return 1; 3239 } 3240 3241 #ifdef HAVE_NGHTTP2 3242 /** Create new http2 session. Called when creating handling comm point. */ 3243 static struct http2_session* http2_session_create(struct comm_point* c) 3244 { 3245 struct http2_session* session = calloc(1, sizeof(*session)); 3246 if(!session) { 3247 log_err("malloc failure while creating http2 session"); 3248 return NULL; 3249 } 3250 session->c = c; 3251 3252 return session; 3253 } 3254 #endif 3255 3256 /** Delete http2 session. After closing connection or on error */ 3257 static void http2_session_delete(struct http2_session* h2_session) 3258 { 3259 #ifdef HAVE_NGHTTP2 3260 if(h2_session->callbacks) 3261 nghttp2_session_callbacks_del(h2_session->callbacks); 3262 free(h2_session); 3263 #else 3264 (void)h2_session; 3265 #endif 3266 } 3267 3268 #ifdef HAVE_NGHTTP2 3269 struct http2_stream* http2_stream_create(int32_t stream_id) 3270 { 3271 struct http2_stream* h2_stream = calloc(1, sizeof(*h2_stream)); 3272 if(!h2_stream) { 3273 log_err("malloc failure while creating http2 stream"); 3274 return NULL; 3275 } 3276 h2_stream->stream_id = stream_id; 3277 return h2_stream; 3278 } 3279 3280 /** Delete http2 stream. After session delete or stream close callback */ 3281 static void http2_stream_delete(struct http2_session* h2_session, 3282 struct http2_stream* h2_stream) 3283 { 3284 if(h2_stream->mesh_state) { 3285 mesh_state_remove_reply(h2_stream->mesh, h2_stream->mesh_state, 3286 h2_session->c); 3287 h2_stream->mesh_state = NULL; 3288 } 3289 http2_req_stream_clear(h2_stream); 3290 free(h2_stream); 3291 } 3292 #endif 3293 3294 void http2_stream_add_meshstate(struct http2_stream* h2_stream, 3295 struct mesh_area* mesh, struct mesh_state* m) 3296 { 3297 h2_stream->mesh = mesh; 3298 h2_stream->mesh_state = m; 3299 } 3300 3301 /** delete http2 session server. After closing connection. */ 3302 static void http2_session_server_delete(struct http2_session* h2_session) 3303 { 3304 #ifdef HAVE_NGHTTP2 3305 struct http2_stream* h2_stream, *next; 3306 nghttp2_session_del(h2_session->session); /* NULL input is fine */ 3307 h2_session->session = NULL; 3308 for(h2_stream = h2_session->first_stream; h2_stream;) { 3309 next = h2_stream->next; 3310 http2_stream_delete(h2_session, h2_stream); 3311 h2_stream = next; 3312 } 3313 h2_session->first_stream = NULL; 3314 h2_session->is_drop = 0; 3315 h2_session->postpone_drop = 0; 3316 h2_session->c->h2_stream = NULL; 3317 #endif 3318 (void)h2_session; 3319 } 3320 3321 #ifdef HAVE_NGHTTP2 3322 void http2_session_add_stream(struct http2_session* h2_session, 3323 struct http2_stream* h2_stream) 3324 { 3325 if(h2_session->first_stream) 3326 h2_session->first_stream->prev = h2_stream; 3327 h2_stream->next = h2_session->first_stream; 3328 h2_session->first_stream = h2_stream; 3329 } 3330 3331 /** remove stream from session linked list. After stream close callback or 3332 * closing connection */ 3333 static void http2_session_remove_stream(struct http2_session* h2_session, 3334 struct http2_stream* h2_stream) 3335 { 3336 if(h2_stream->prev) 3337 h2_stream->prev->next = h2_stream->next; 3338 else 3339 h2_session->first_stream = h2_stream->next; 3340 if(h2_stream->next) 3341 h2_stream->next->prev = h2_stream->prev; 3342 3343 } 3344 3345 int http2_stream_close_cb(nghttp2_session* ATTR_UNUSED(session), 3346 int32_t stream_id, uint32_t ATTR_UNUSED(error_code), void* cb_arg) 3347 { 3348 struct http2_stream* h2_stream; 3349 struct http2_session* h2_session = (struct http2_session*)cb_arg; 3350 if(!(h2_stream = nghttp2_session_get_stream_user_data( 3351 h2_session->session, stream_id))) { 3352 return 0; 3353 } 3354 http2_session_remove_stream(h2_session, h2_stream); 3355 http2_stream_delete(h2_session, h2_stream); 3356 return 0; 3357 } 3358 3359 ssize_t http2_recv_cb(nghttp2_session* ATTR_UNUSED(session), uint8_t* buf, 3360 size_t len, int ATTR_UNUSED(flags), void* cb_arg) 3361 { 3362 struct http2_session* h2_session = (struct http2_session*)cb_arg; 3363 ssize_t ret; 3364 3365 log_assert(h2_session->c->type == comm_http); 3366 log_assert(h2_session->c->h2_session); 3367 3368 #ifdef HAVE_SSL 3369 if(h2_session->c->ssl) { 3370 int r; 3371 ERR_clear_error(); 3372 r = SSL_read(h2_session->c->ssl, buf, len); 3373 if(r <= 0) { 3374 int want = SSL_get_error(h2_session->c->ssl, r); 3375 if(want == SSL_ERROR_ZERO_RETURN) { 3376 return NGHTTP2_ERR_EOF; 3377 } else if(want == SSL_ERROR_WANT_READ) { 3378 return NGHTTP2_ERR_WOULDBLOCK; 3379 } else if(want == SSL_ERROR_WANT_WRITE) { 3380 h2_session->c->ssl_shake_state = comm_ssl_shake_hs_write; 3381 comm_point_listen_for_rw(h2_session->c, 0, 1); 3382 return NGHTTP2_ERR_WOULDBLOCK; 3383 } else if(want == SSL_ERROR_SYSCALL) { 3384 #ifdef ECONNRESET 3385 if(errno == ECONNRESET && verbosity < 2) 3386 return NGHTTP2_ERR_CALLBACK_FAILURE; 3387 #endif 3388 if(errno != 0) 3389 log_err("SSL_read syscall: %s", 3390 strerror(errno)); 3391 return NGHTTP2_ERR_CALLBACK_FAILURE; 3392 } 3393 log_crypto_err_io("could not SSL_read", want); 3394 return NGHTTP2_ERR_CALLBACK_FAILURE; 3395 } 3396 return r; 3397 } 3398 #endif /* HAVE_SSL */ 3399 3400 ret = recv(h2_session->c->fd, buf, len, MSG_DONTWAIT); 3401 if(ret == 0) { 3402 return NGHTTP2_ERR_EOF; 3403 } else if(ret < 0) { 3404 #ifndef USE_WINSOCK 3405 if(errno == EINTR || errno == EAGAIN) 3406 return NGHTTP2_ERR_WOULDBLOCK; 3407 #ifdef ECONNRESET 3408 if(errno == ECONNRESET && verbosity < 2) 3409 return NGHTTP2_ERR_CALLBACK_FAILURE; 3410 #endif 3411 log_err_addr("could not http2 recv: %s", strerror(errno), 3412 &h2_session->c->repinfo.remote_addr, 3413 h2_session->c->repinfo.remote_addrlen); 3414 #else /* USE_WINSOCK */ 3415 if(WSAGetLastError() == WSAECONNRESET) 3416 return NGHTTP2_ERR_CALLBACK_FAILURE; 3417 if(WSAGetLastError() == WSAEINPROGRESS) 3418 return NGHTTP2_ERR_WOULDBLOCK; 3419 if(WSAGetLastError() == WSAEWOULDBLOCK) { 3420 ub_winsock_tcp_wouldblock(h2_session->c->ev->ev, 3421 UB_EV_READ); 3422 return NGHTTP2_ERR_WOULDBLOCK; 3423 } 3424 log_err_addr("could not http2 recv: %s", 3425 wsa_strerror(WSAGetLastError()), 3426 &h2_session->c->repinfo.remote_addr, 3427 h2_session->c->repinfo.remote_addrlen); 3428 #endif 3429 return NGHTTP2_ERR_CALLBACK_FAILURE; 3430 } 3431 return ret; 3432 } 3433 #endif /* HAVE_NGHTTP2 */ 3434 3435 /** Handle http2 read */ 3436 static int 3437 comm_point_http2_handle_read(int ATTR_UNUSED(fd), struct comm_point* c) 3438 { 3439 #ifdef HAVE_NGHTTP2 3440 int ret; 3441 log_assert(c->h2_session); 3442 3443 /* reading until recv cb returns NGHTTP2_ERR_WOULDBLOCK */ 3444 ret = nghttp2_session_recv(c->h2_session->session); 3445 if(ret) { 3446 if(ret != NGHTTP2_ERR_EOF && 3447 ret != NGHTTP2_ERR_CALLBACK_FAILURE) { 3448 char a[256]; 3449 addr_to_str(&c->repinfo.remote_addr, 3450 c->repinfo.remote_addrlen, a, sizeof(a)); 3451 verbose(VERB_QUERY, "http2: session_recv from %s failed, " 3452 "error: %s", a, nghttp2_strerror(ret)); 3453 } 3454 return 0; 3455 } 3456 if(nghttp2_session_want_write(c->h2_session->session)) { 3457 c->tcp_is_reading = 0; 3458 comm_point_stop_listening(c); 3459 comm_point_start_listening(c, -1, adjusted_tcp_timeout(c)); 3460 } else if(!nghttp2_session_want_read(c->h2_session->session)) 3461 return 0; /* connection can be closed */ 3462 return 1; 3463 #else 3464 (void)c; 3465 return 0; 3466 #endif 3467 } 3468 3469 /** 3470 * Handle http reading callback. 3471 * @param fd: file descriptor of socket. 3472 * @param c: comm point to read from into buffer. 3473 * @return: 0 on error 3474 */ 3475 static int 3476 comm_point_http_handle_read(int fd, struct comm_point* c) 3477 { 3478 log_assert(c->type == comm_http); 3479 log_assert(fd != -1); 3480 3481 /* if we are in ssl handshake, handle SSL handshake */ 3482 #ifdef HAVE_SSL 3483 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) { 3484 if(!ssl_handshake(c)) 3485 return 0; 3486 if(c->ssl_shake_state != comm_ssl_shake_none) 3487 return 1; 3488 } 3489 #endif /* HAVE_SSL */ 3490 3491 if(!c->tcp_is_reading) 3492 return 1; 3493 3494 if(c->use_h2) { 3495 return comm_point_http2_handle_read(fd, c); 3496 } 3497 3498 /* http version is <= http/1.1 */ 3499 3500 if(c->http_min_version >= http_version_2) { 3501 /* HTTP/2 failed, not allowed to use lower version. */ 3502 return 0; 3503 } 3504 3505 /* read more data */ 3506 if(c->ssl) { 3507 if(!ssl_http_read_more(c)) 3508 return 0; 3509 } else { 3510 if(!http_read_more(fd, c)) 3511 return 0; 3512 } 3513 3514 if(c->http_stored >= sldns_buffer_position(c->buffer)) { 3515 /* read did not work but we wanted more data, there is 3516 * no bytes to process now. */ 3517 return 1; 3518 } 3519 sldns_buffer_flip(c->buffer); 3520 /* if we are partway in a segment of data, position us at the point 3521 * where we left off previously */ 3522 if(c->http_stored < sldns_buffer_limit(c->buffer)) 3523 sldns_buffer_set_position(c->buffer, c->http_stored); 3524 else sldns_buffer_set_position(c->buffer, sldns_buffer_limit(c->buffer)); 3525 3526 while(sldns_buffer_remaining(c->buffer) > 0) { 3527 /* Handle HTTP/1.x data */ 3528 /* if we are reading headers, read more headers */ 3529 if(c->http_in_headers || c->http_in_chunk_headers) { 3530 /* if header is done, process the header */ 3531 if(!http_header_done(c->buffer)) { 3532 /* copy remaining data to front of buffer 3533 * and set rest for writing into it */ 3534 http_moveover_buffer(c->buffer); 3535 /* return and wait to read more */ 3536 return 1; 3537 } 3538 if(!c->http_in_chunk_headers) { 3539 /* process initial headers */ 3540 if(!http_process_initial_header(c)) 3541 return 0; 3542 } else { 3543 /* process chunk headers */ 3544 int r = http_process_chunk_header(c); 3545 if(r == 0) return 0; 3546 if(r == 2) return 1; /* done */ 3547 /* r == 1, continue */ 3548 } 3549 /* see if we have more to process */ 3550 continue; 3551 } 3552 3553 if(!c->http_is_chunked) { 3554 /* if we are reading nonchunks, process that*/ 3555 return http_nonchunk_segment(c); 3556 } else { 3557 /* if we are reading chunks, read the chunk */ 3558 int r = http_chunked_segment(c); 3559 if(r == 0) return 0; 3560 if(r == 1) return 1; 3561 continue; 3562 } 3563 } 3564 /* broke out of the loop; could not process header instead need 3565 * to read more */ 3566 /* moveover any remaining data and read more data */ 3567 http_moveover_buffer(c->buffer); 3568 /* return and wait to read more */ 3569 return 1; 3570 } 3571 3572 /** check pending connect for http */ 3573 static int 3574 http_check_connect(int fd, struct comm_point* c) 3575 { 3576 /* check for pending error from nonblocking connect */ 3577 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/ 3578 int error = 0; 3579 socklen_t len = (socklen_t)sizeof(error); 3580 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error, 3581 &len) < 0){ 3582 #ifndef USE_WINSOCK 3583 error = errno; /* on solaris errno is error */ 3584 #else /* USE_WINSOCK */ 3585 error = WSAGetLastError(); 3586 #endif 3587 } 3588 #ifndef USE_WINSOCK 3589 #if defined(EINPROGRESS) && defined(EWOULDBLOCK) 3590 if(error == EINPROGRESS || error == EWOULDBLOCK) 3591 return 1; /* try again later */ 3592 else 3593 #endif 3594 if(error != 0 && verbosity < 2) 3595 return 0; /* silence lots of chatter in the logs */ 3596 else if(error != 0) { 3597 log_err_addr("http connect", strerror(error), 3598 &c->repinfo.remote_addr, c->repinfo.remote_addrlen); 3599 #else /* USE_WINSOCK */ 3600 /* examine error */ 3601 if(error == WSAEINPROGRESS) 3602 return 1; 3603 else if(error == WSAEWOULDBLOCK) { 3604 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 3605 return 1; 3606 } else if(error != 0 && verbosity < 2) 3607 return 0; 3608 else if(error != 0) { 3609 log_err_addr("http connect", wsa_strerror(error), 3610 &c->repinfo.remote_addr, c->repinfo.remote_addrlen); 3611 #endif /* USE_WINSOCK */ 3612 return 0; 3613 } 3614 /* keep on processing this socket */ 3615 return 2; 3616 } 3617 3618 /** write more data for http (with ssl) */ 3619 static int 3620 ssl_http_write_more(struct comm_point* c) 3621 { 3622 #ifdef HAVE_SSL 3623 int r; 3624 log_assert(sldns_buffer_remaining(c->buffer) > 0); 3625 ERR_clear_error(); 3626 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer), 3627 (int)sldns_buffer_remaining(c->buffer)); 3628 if(r <= 0) { 3629 int want = SSL_get_error(c->ssl, r); 3630 if(want == SSL_ERROR_ZERO_RETURN) { 3631 return 0; /* closed */ 3632 } else if(want == SSL_ERROR_WANT_READ) { 3633 c->ssl_shake_state = comm_ssl_shake_hs_read; 3634 comm_point_listen_for_rw(c, 1, 0); 3635 return 1; /* wait for read condition */ 3636 } else if(want == SSL_ERROR_WANT_WRITE) { 3637 return 1; /* write more later */ 3638 } else if(want == SSL_ERROR_SYSCALL) { 3639 #ifdef EPIPE 3640 if(errno == EPIPE && verbosity < 2) 3641 return 0; /* silence 'broken pipe' */ 3642 #endif 3643 if(errno != 0) 3644 log_err("SSL_write syscall: %s", 3645 strerror(errno)); 3646 return 0; 3647 } 3648 log_crypto_err_io("could not SSL_write", want); 3649 return 0; 3650 } 3651 sldns_buffer_skip(c->buffer, (ssize_t)r); 3652 return 1; 3653 #else 3654 (void)c; 3655 return 0; 3656 #endif /* HAVE_SSL */ 3657 } 3658 3659 /** write more data for http */ 3660 static int 3661 http_write_more(int fd, struct comm_point* c) 3662 { 3663 ssize_t r; 3664 log_assert(sldns_buffer_remaining(c->buffer) > 0); 3665 r = send(fd, (void*)sldns_buffer_current(c->buffer), 3666 sldns_buffer_remaining(c->buffer), 0); 3667 if(r == -1) { 3668 #ifndef USE_WINSOCK 3669 if(errno == EINTR || errno == EAGAIN) 3670 return 1; 3671 #else 3672 if(WSAGetLastError() == WSAEINPROGRESS) 3673 return 1; 3674 if(WSAGetLastError() == WSAEWOULDBLOCK) { 3675 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 3676 return 1; 3677 } 3678 #endif 3679 log_err_addr("http send r", sock_strerror(errno), 3680 &c->repinfo.remote_addr, c->repinfo.remote_addrlen); 3681 return 0; 3682 } 3683 sldns_buffer_skip(c->buffer, r); 3684 return 1; 3685 } 3686 3687 #ifdef HAVE_NGHTTP2 3688 ssize_t http2_send_cb(nghttp2_session* ATTR_UNUSED(session), const uint8_t* buf, 3689 size_t len, int ATTR_UNUSED(flags), void* cb_arg) 3690 { 3691 ssize_t ret; 3692 struct http2_session* h2_session = (struct http2_session*)cb_arg; 3693 log_assert(h2_session->c->type == comm_http); 3694 log_assert(h2_session->c->h2_session); 3695 3696 #ifdef HAVE_SSL 3697 if(h2_session->c->ssl) { 3698 int r; 3699 ERR_clear_error(); 3700 r = SSL_write(h2_session->c->ssl, buf, len); 3701 if(r <= 0) { 3702 int want = SSL_get_error(h2_session->c->ssl, r); 3703 if(want == SSL_ERROR_ZERO_RETURN) { 3704 return NGHTTP2_ERR_CALLBACK_FAILURE; 3705 } else if(want == SSL_ERROR_WANT_READ) { 3706 h2_session->c->ssl_shake_state = comm_ssl_shake_hs_read; 3707 comm_point_listen_for_rw(h2_session->c, 1, 0); 3708 return NGHTTP2_ERR_WOULDBLOCK; 3709 } else if(want == SSL_ERROR_WANT_WRITE) { 3710 return NGHTTP2_ERR_WOULDBLOCK; 3711 } else if(want == SSL_ERROR_SYSCALL) { 3712 #ifdef EPIPE 3713 if(errno == EPIPE && verbosity < 2) 3714 return NGHTTP2_ERR_CALLBACK_FAILURE; 3715 #endif 3716 if(errno != 0) 3717 log_err("SSL_write syscall: %s", 3718 strerror(errno)); 3719 return NGHTTP2_ERR_CALLBACK_FAILURE; 3720 } 3721 log_crypto_err_io("could not SSL_write", want); 3722 return NGHTTP2_ERR_CALLBACK_FAILURE; 3723 } 3724 return r; 3725 } 3726 #endif /* HAVE_SSL */ 3727 3728 ret = send(h2_session->c->fd, buf, len, 0); 3729 if(ret == 0) { 3730 return NGHTTP2_ERR_CALLBACK_FAILURE; 3731 } else if(ret < 0) { 3732 #ifndef USE_WINSOCK 3733 if(errno == EINTR || errno == EAGAIN) 3734 return NGHTTP2_ERR_WOULDBLOCK; 3735 #ifdef EPIPE 3736 if(errno == EPIPE && verbosity < 2) 3737 return NGHTTP2_ERR_CALLBACK_FAILURE; 3738 #endif 3739 #ifdef ECONNRESET 3740 if(errno == ECONNRESET && verbosity < 2) 3741 return NGHTTP2_ERR_CALLBACK_FAILURE; 3742 #endif 3743 log_err_addr("could not http2 write: %s", strerror(errno), 3744 &h2_session->c->repinfo.remote_addr, 3745 h2_session->c->repinfo.remote_addrlen); 3746 #else /* USE_WINSOCK */ 3747 if(WSAGetLastError() == WSAENOTCONN) 3748 return NGHTTP2_ERR_WOULDBLOCK; 3749 if(WSAGetLastError() == WSAEINPROGRESS) 3750 return NGHTTP2_ERR_WOULDBLOCK; 3751 if(WSAGetLastError() == WSAEWOULDBLOCK) { 3752 ub_winsock_tcp_wouldblock(h2_session->c->ev->ev, 3753 UB_EV_WRITE); 3754 return NGHTTP2_ERR_WOULDBLOCK; 3755 } 3756 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2) 3757 return NGHTTP2_ERR_CALLBACK_FAILURE; 3758 log_err_addr("could not http2 write: %s", 3759 wsa_strerror(WSAGetLastError()), 3760 &h2_session->c->repinfo.remote_addr, 3761 h2_session->c->repinfo.remote_addrlen); 3762 #endif 3763 return NGHTTP2_ERR_CALLBACK_FAILURE; 3764 } 3765 return ret; 3766 } 3767 #endif /* HAVE_NGHTTP2 */ 3768 3769 /** Handle http2 writing */ 3770 static int 3771 comm_point_http2_handle_write(int ATTR_UNUSED(fd), struct comm_point* c) 3772 { 3773 #ifdef HAVE_NGHTTP2 3774 int ret; 3775 log_assert(c->h2_session); 3776 3777 ret = nghttp2_session_send(c->h2_session->session); 3778 if(ret) { 3779 verbose(VERB_QUERY, "http2: session_send failed, " 3780 "error: %s", nghttp2_strerror(ret)); 3781 return 0; 3782 } 3783 3784 if(nghttp2_session_want_read(c->h2_session->session)) { 3785 c->tcp_is_reading = 1; 3786 comm_point_stop_listening(c); 3787 comm_point_start_listening(c, -1, adjusted_tcp_timeout(c)); 3788 } else if(!nghttp2_session_want_write(c->h2_session->session)) 3789 return 0; /* connection can be closed */ 3790 return 1; 3791 #else 3792 (void)c; 3793 return 0; 3794 #endif 3795 } 3796 3797 /** 3798 * Handle http writing callback. 3799 * @param fd: file descriptor of socket. 3800 * @param c: comm point to write buffer out of. 3801 * @return: 0 on error 3802 */ 3803 static int 3804 comm_point_http_handle_write(int fd, struct comm_point* c) 3805 { 3806 log_assert(c->type == comm_http); 3807 log_assert(fd != -1); 3808 3809 /* check pending connect errors, if that fails, we wait for more, 3810 * or we can continue to write contents */ 3811 if(c->tcp_check_nb_connect) { 3812 int r = http_check_connect(fd, c); 3813 if(r == 0) return 0; 3814 if(r == 1) return 1; 3815 c->tcp_check_nb_connect = 0; 3816 } 3817 /* if we are in ssl handshake, handle SSL handshake */ 3818 #ifdef HAVE_SSL 3819 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) { 3820 if(!ssl_handshake(c)) 3821 return 0; 3822 if(c->ssl_shake_state != comm_ssl_shake_none) 3823 return 1; 3824 } 3825 #endif /* HAVE_SSL */ 3826 if(c->tcp_is_reading) 3827 return 1; 3828 3829 if(c->use_h2) { 3830 return comm_point_http2_handle_write(fd, c); 3831 } 3832 3833 /* http version is <= http/1.1 */ 3834 3835 if(c->http_min_version >= http_version_2) { 3836 /* HTTP/2 failed, not allowed to use lower version. */ 3837 return 0; 3838 } 3839 3840 /* if we are writing, write more */ 3841 if(c->ssl) { 3842 if(!ssl_http_write_more(c)) 3843 return 0; 3844 } else { 3845 if(!http_write_more(fd, c)) 3846 return 0; 3847 } 3848 3849 /* we write a single buffer contents, that can contain 3850 * the http request, and then flip to read the results */ 3851 /* see if write is done */ 3852 if(sldns_buffer_remaining(c->buffer) == 0) { 3853 sldns_buffer_clear(c->buffer); 3854 if(c->tcp_do_toggle_rw) 3855 c->tcp_is_reading = 1; 3856 c->tcp_byte_count = 0; 3857 /* switch from listening(write) to listening(read) */ 3858 comm_point_stop_listening(c); 3859 comm_point_start_listening(c, -1, -1); 3860 } 3861 return 1; 3862 } 3863 3864 void 3865 comm_point_http_handle_callback(int fd, short event, void* arg) 3866 { 3867 struct comm_point* c = (struct comm_point*)arg; 3868 log_assert(c->type == comm_http); 3869 ub_comm_base_now(c->ev->base); 3870 3871 if(event&UB_EV_TIMEOUT) { 3872 verbose(VERB_QUERY, "http took too long, dropped"); 3873 reclaim_http_handler(c); 3874 if(!c->tcp_do_close) { 3875 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3876 (void)(*c->callback)(c, c->cb_arg, 3877 NETEVENT_TIMEOUT, NULL); 3878 } 3879 return; 3880 } 3881 if(event&UB_EV_READ) { 3882 if(!comm_point_http_handle_read(fd, c)) { 3883 reclaim_http_handler(c); 3884 if(!c->tcp_do_close) { 3885 fptr_ok(fptr_whitelist_comm_point( 3886 c->callback)); 3887 (void)(*c->callback)(c, c->cb_arg, 3888 NETEVENT_CLOSED, NULL); 3889 } 3890 } 3891 return; 3892 } 3893 if(event&UB_EV_WRITE) { 3894 if(!comm_point_http_handle_write(fd, c)) { 3895 reclaim_http_handler(c); 3896 if(!c->tcp_do_close) { 3897 fptr_ok(fptr_whitelist_comm_point( 3898 c->callback)); 3899 (void)(*c->callback)(c, c->cb_arg, 3900 NETEVENT_CLOSED, NULL); 3901 } 3902 } 3903 return; 3904 } 3905 log_err("Ignored event %d for httphdl.", event); 3906 } 3907 3908 void comm_point_local_handle_callback(int fd, short event, void* arg) 3909 { 3910 struct comm_point* c = (struct comm_point*)arg; 3911 log_assert(c->type == comm_local); 3912 ub_comm_base_now(c->ev->base); 3913 3914 if(event&UB_EV_READ) { 3915 if(!comm_point_tcp_handle_read(fd, c, 1)) { 3916 fptr_ok(fptr_whitelist_comm_point(c->callback)); 3917 (void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED, 3918 NULL); 3919 } 3920 return; 3921 } 3922 log_err("Ignored event %d for localhdl.", event); 3923 } 3924 3925 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd), 3926 short event, void* arg) 3927 { 3928 struct comm_point* c = (struct comm_point*)arg; 3929 int err = NETEVENT_NOERROR; 3930 log_assert(c->type == comm_raw); 3931 ub_comm_base_now(c->ev->base); 3932 3933 if(event&UB_EV_TIMEOUT) 3934 err = NETEVENT_TIMEOUT; 3935 fptr_ok(fptr_whitelist_comm_point_raw(c->callback)); 3936 (void)(*c->callback)(c, c->cb_arg, err, NULL); 3937 } 3938 3939 struct comm_point* 3940 comm_point_create_udp(struct comm_base *base, int fd, sldns_buffer* buffer, 3941 int pp2_enabled, comm_point_callback_type* callback, 3942 void* callback_arg, struct unbound_socket* socket) 3943 { 3944 struct comm_point* c = (struct comm_point*)calloc(1, 3945 sizeof(struct comm_point)); 3946 short evbits; 3947 if(!c) 3948 return NULL; 3949 c->ev = (struct internal_event*)calloc(1, 3950 sizeof(struct internal_event)); 3951 if(!c->ev) { 3952 free(c); 3953 return NULL; 3954 } 3955 c->ev->base = base; 3956 c->fd = fd; 3957 c->buffer = buffer; 3958 c->timeout = NULL; 3959 c->tcp_is_reading = 0; 3960 c->tcp_byte_count = 0; 3961 c->tcp_parent = NULL; 3962 c->max_tcp_count = 0; 3963 c->cur_tcp_count = 0; 3964 c->tcp_handlers = NULL; 3965 c->tcp_free = NULL; 3966 c->type = comm_udp; 3967 c->tcp_do_close = 0; 3968 c->do_not_close = 0; 3969 c->tcp_do_toggle_rw = 0; 3970 c->tcp_check_nb_connect = 0; 3971 #ifdef USE_MSG_FASTOPEN 3972 c->tcp_do_fastopen = 0; 3973 #endif 3974 #ifdef USE_DNSCRYPT 3975 c->dnscrypt = 0; 3976 c->dnscrypt_buffer = buffer; 3977 #endif 3978 c->inuse = 0; 3979 c->callback = callback; 3980 c->cb_arg = callback_arg; 3981 c->socket = socket; 3982 c->pp2_enabled = pp2_enabled; 3983 c->pp2_header_state = pp2_header_none; 3984 evbits = UB_EV_READ | UB_EV_PERSIST; 3985 /* ub_event stuff */ 3986 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 3987 comm_point_udp_callback, c); 3988 if(c->ev->ev == NULL) { 3989 log_err("could not baseset udp event"); 3990 comm_point_delete(c); 3991 return NULL; 3992 } 3993 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) { 3994 log_err("could not add udp event"); 3995 comm_point_delete(c); 3996 return NULL; 3997 } 3998 c->event_added = 1; 3999 return c; 4000 } 4001 4002 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG) 4003 struct comm_point* 4004 comm_point_create_udp_ancil(struct comm_base *base, int fd, 4005 sldns_buffer* buffer, int pp2_enabled, 4006 comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket) 4007 { 4008 struct comm_point* c = (struct comm_point*)calloc(1, 4009 sizeof(struct comm_point)); 4010 short evbits; 4011 if(!c) 4012 return NULL; 4013 c->ev = (struct internal_event*)calloc(1, 4014 sizeof(struct internal_event)); 4015 if(!c->ev) { 4016 free(c); 4017 return NULL; 4018 } 4019 c->ev->base = base; 4020 c->fd = fd; 4021 c->buffer = buffer; 4022 c->timeout = NULL; 4023 c->tcp_is_reading = 0; 4024 c->tcp_byte_count = 0; 4025 c->tcp_parent = NULL; 4026 c->max_tcp_count = 0; 4027 c->cur_tcp_count = 0; 4028 c->tcp_handlers = NULL; 4029 c->tcp_free = NULL; 4030 c->type = comm_udp; 4031 c->tcp_do_close = 0; 4032 c->do_not_close = 0; 4033 #ifdef USE_DNSCRYPT 4034 c->dnscrypt = 0; 4035 c->dnscrypt_buffer = buffer; 4036 #endif 4037 c->inuse = 0; 4038 c->tcp_do_toggle_rw = 0; 4039 c->tcp_check_nb_connect = 0; 4040 #ifdef USE_MSG_FASTOPEN 4041 c->tcp_do_fastopen = 0; 4042 #endif 4043 c->callback = callback; 4044 c->cb_arg = callback_arg; 4045 c->socket = socket; 4046 c->pp2_enabled = pp2_enabled; 4047 c->pp2_header_state = pp2_header_none; 4048 evbits = UB_EV_READ | UB_EV_PERSIST; 4049 /* ub_event stuff */ 4050 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4051 comm_point_udp_ancil_callback, c); 4052 if(c->ev->ev == NULL) { 4053 log_err("could not baseset udp event"); 4054 comm_point_delete(c); 4055 return NULL; 4056 } 4057 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) { 4058 log_err("could not add udp event"); 4059 comm_point_delete(c); 4060 return NULL; 4061 } 4062 c->event_added = 1; 4063 return c; 4064 } 4065 #endif 4066 4067 static struct comm_point* 4068 comm_point_create_tcp_handler(struct comm_base *base, 4069 struct comm_point* parent, size_t bufsize, 4070 struct sldns_buffer* spoolbuf, comm_point_callback_type* callback, 4071 void* callback_arg, struct unbound_socket* socket) 4072 { 4073 struct comm_point* c = (struct comm_point*)calloc(1, 4074 sizeof(struct comm_point)); 4075 short evbits; 4076 if(!c) 4077 return NULL; 4078 c->ev = (struct internal_event*)calloc(1, 4079 sizeof(struct internal_event)); 4080 if(!c->ev) { 4081 free(c); 4082 return NULL; 4083 } 4084 c->ev->base = base; 4085 c->fd = -1; 4086 c->buffer = sldns_buffer_new(bufsize); 4087 if(!c->buffer) { 4088 free(c->ev); 4089 free(c); 4090 return NULL; 4091 } 4092 c->timeout = (struct timeval*)malloc(sizeof(struct timeval)); 4093 if(!c->timeout) { 4094 sldns_buffer_free(c->buffer); 4095 free(c->ev); 4096 free(c); 4097 return NULL; 4098 } 4099 c->tcp_is_reading = 0; 4100 c->tcp_byte_count = 0; 4101 c->tcp_parent = parent; 4102 c->tcp_timeout_msec = parent->tcp_timeout_msec; 4103 c->tcp_conn_limit = parent->tcp_conn_limit; 4104 c->tcl_addr = NULL; 4105 c->tcp_keepalive = 0; 4106 c->max_tcp_count = 0; 4107 c->cur_tcp_count = 0; 4108 c->tcp_handlers = NULL; 4109 c->tcp_free = NULL; 4110 c->type = comm_tcp; 4111 c->tcp_do_close = 0; 4112 c->do_not_close = 0; 4113 c->tcp_do_toggle_rw = 1; 4114 c->tcp_check_nb_connect = 0; 4115 #ifdef USE_MSG_FASTOPEN 4116 c->tcp_do_fastopen = 0; 4117 #endif 4118 #ifdef USE_DNSCRYPT 4119 c->dnscrypt = 0; 4120 /* We don't know just yet if this is a dnscrypt channel. Allocation 4121 * will be done when handling the callback. */ 4122 c->dnscrypt_buffer = c->buffer; 4123 #endif 4124 c->repinfo.c = c; 4125 c->callback = callback; 4126 c->cb_arg = callback_arg; 4127 c->socket = socket; 4128 c->pp2_enabled = parent->pp2_enabled; 4129 c->pp2_header_state = pp2_header_none; 4130 if(spoolbuf) { 4131 c->tcp_req_info = tcp_req_info_create(spoolbuf); 4132 if(!c->tcp_req_info) { 4133 log_err("could not create tcp commpoint"); 4134 sldns_buffer_free(c->buffer); 4135 free(c->timeout); 4136 free(c->ev); 4137 free(c); 4138 return NULL; 4139 } 4140 c->tcp_req_info->cp = c; 4141 c->tcp_do_close = 1; 4142 c->tcp_do_toggle_rw = 0; 4143 } 4144 /* add to parent free list */ 4145 c->tcp_free = parent->tcp_free; 4146 parent->tcp_free = c; 4147 /* ub_event stuff */ 4148 evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT; 4149 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4150 comm_point_tcp_handle_callback, c); 4151 if(c->ev->ev == NULL) 4152 { 4153 log_err("could not basetset tcphdl event"); 4154 parent->tcp_free = c->tcp_free; 4155 tcp_req_info_delete(c->tcp_req_info); 4156 sldns_buffer_free(c->buffer); 4157 free(c->timeout); 4158 free(c->ev); 4159 free(c); 4160 return NULL; 4161 } 4162 return c; 4163 } 4164 4165 static struct comm_point* 4166 comm_point_create_http_handler(struct comm_base *base, 4167 struct comm_point* parent, size_t bufsize, int harden_large_queries, 4168 uint32_t http_max_streams, char* http_endpoint, 4169 comm_point_callback_type* callback, void* callback_arg, 4170 struct unbound_socket* socket) 4171 { 4172 struct comm_point* c = (struct comm_point*)calloc(1, 4173 sizeof(struct comm_point)); 4174 short evbits; 4175 if(!c) 4176 return NULL; 4177 c->ev = (struct internal_event*)calloc(1, 4178 sizeof(struct internal_event)); 4179 if(!c->ev) { 4180 free(c); 4181 return NULL; 4182 } 4183 c->ev->base = base; 4184 c->fd = -1; 4185 c->buffer = sldns_buffer_new(bufsize); 4186 if(!c->buffer) { 4187 free(c->ev); 4188 free(c); 4189 return NULL; 4190 } 4191 c->timeout = (struct timeval*)malloc(sizeof(struct timeval)); 4192 if(!c->timeout) { 4193 sldns_buffer_free(c->buffer); 4194 free(c->ev); 4195 free(c); 4196 return NULL; 4197 } 4198 c->tcp_is_reading = 0; 4199 c->tcp_byte_count = 0; 4200 c->tcp_parent = parent; 4201 c->tcp_timeout_msec = parent->tcp_timeout_msec; 4202 c->tcp_conn_limit = parent->tcp_conn_limit; 4203 c->tcl_addr = NULL; 4204 c->tcp_keepalive = 0; 4205 c->max_tcp_count = 0; 4206 c->cur_tcp_count = 0; 4207 c->tcp_handlers = NULL; 4208 c->tcp_free = NULL; 4209 c->type = comm_http; 4210 c->tcp_do_close = 1; 4211 c->do_not_close = 0; 4212 c->tcp_do_toggle_rw = 1; /* will be set to 0 after http2 upgrade */ 4213 c->tcp_check_nb_connect = 0; 4214 #ifdef USE_MSG_FASTOPEN 4215 c->tcp_do_fastopen = 0; 4216 #endif 4217 #ifdef USE_DNSCRYPT 4218 c->dnscrypt = 0; 4219 c->dnscrypt_buffer = NULL; 4220 #endif 4221 c->repinfo.c = c; 4222 c->callback = callback; 4223 c->cb_arg = callback_arg; 4224 c->socket = socket; 4225 c->pp2_enabled = 0; 4226 c->pp2_header_state = pp2_header_none; 4227 4228 c->http_min_version = http_version_2; 4229 c->http2_stream_max_qbuffer_size = bufsize; 4230 if(harden_large_queries && bufsize > 512) 4231 c->http2_stream_max_qbuffer_size = 512; 4232 c->http2_max_streams = http_max_streams; 4233 if(!(c->http_endpoint = strdup(http_endpoint))) { 4234 log_err("could not strdup http_endpoint"); 4235 sldns_buffer_free(c->buffer); 4236 free(c->timeout); 4237 free(c->ev); 4238 free(c); 4239 return NULL; 4240 } 4241 c->use_h2 = 0; 4242 #ifdef HAVE_NGHTTP2 4243 if(!(c->h2_session = http2_session_create(c))) { 4244 log_err("could not create http2 session"); 4245 free(c->http_endpoint); 4246 sldns_buffer_free(c->buffer); 4247 free(c->timeout); 4248 free(c->ev); 4249 free(c); 4250 return NULL; 4251 } 4252 if(!(c->h2_session->callbacks = http2_req_callbacks_create())) { 4253 log_err("could not create http2 callbacks"); 4254 http2_session_delete(c->h2_session); 4255 free(c->http_endpoint); 4256 sldns_buffer_free(c->buffer); 4257 free(c->timeout); 4258 free(c->ev); 4259 free(c); 4260 return NULL; 4261 } 4262 #endif 4263 4264 /* add to parent free list */ 4265 c->tcp_free = parent->tcp_free; 4266 parent->tcp_free = c; 4267 /* ub_event stuff */ 4268 evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT; 4269 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4270 comm_point_http_handle_callback, c); 4271 if(c->ev->ev == NULL) 4272 { 4273 log_err("could not set http handler event"); 4274 parent->tcp_free = c->tcp_free; 4275 http2_session_delete(c->h2_session); 4276 sldns_buffer_free(c->buffer); 4277 free(c->timeout); 4278 free(c->ev); 4279 free(c); 4280 return NULL; 4281 } 4282 return c; 4283 } 4284 4285 struct comm_point* 4286 comm_point_create_tcp(struct comm_base *base, int fd, int num, 4287 int idle_timeout, int harden_large_queries, 4288 uint32_t http_max_streams, char* http_endpoint, 4289 struct tcl_list* tcp_conn_limit, size_t bufsize, 4290 struct sldns_buffer* spoolbuf, enum listen_type port_type, 4291 int pp2_enabled, comm_point_callback_type* callback, 4292 void* callback_arg, struct unbound_socket* socket) 4293 { 4294 struct comm_point* c = (struct comm_point*)calloc(1, 4295 sizeof(struct comm_point)); 4296 short evbits; 4297 int i; 4298 /* first allocate the TCP accept listener */ 4299 if(!c) 4300 return NULL; 4301 c->ev = (struct internal_event*)calloc(1, 4302 sizeof(struct internal_event)); 4303 if(!c->ev) { 4304 free(c); 4305 return NULL; 4306 } 4307 c->ev->base = base; 4308 c->fd = fd; 4309 c->buffer = NULL; 4310 c->timeout = NULL; 4311 c->tcp_is_reading = 0; 4312 c->tcp_byte_count = 0; 4313 c->tcp_timeout_msec = idle_timeout; 4314 c->tcp_conn_limit = tcp_conn_limit; 4315 c->tcl_addr = NULL; 4316 c->tcp_keepalive = 0; 4317 c->tcp_parent = NULL; 4318 c->max_tcp_count = num; 4319 c->cur_tcp_count = 0; 4320 c->tcp_handlers = (struct comm_point**)calloc((size_t)num, 4321 sizeof(struct comm_point*)); 4322 if(!c->tcp_handlers) { 4323 free(c->ev); 4324 free(c); 4325 return NULL; 4326 } 4327 c->tcp_free = NULL; 4328 c->type = comm_tcp_accept; 4329 c->tcp_do_close = 0; 4330 c->do_not_close = 0; 4331 c->tcp_do_toggle_rw = 0; 4332 c->tcp_check_nb_connect = 0; 4333 #ifdef USE_MSG_FASTOPEN 4334 c->tcp_do_fastopen = 0; 4335 #endif 4336 #ifdef USE_DNSCRYPT 4337 c->dnscrypt = 0; 4338 c->dnscrypt_buffer = NULL; 4339 #endif 4340 c->callback = NULL; 4341 c->cb_arg = NULL; 4342 c->socket = socket; 4343 c->pp2_enabled = (port_type==listen_type_http?0:pp2_enabled); 4344 c->pp2_header_state = pp2_header_none; 4345 evbits = UB_EV_READ | UB_EV_PERSIST; 4346 /* ub_event stuff */ 4347 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4348 comm_point_tcp_accept_callback, c); 4349 if(c->ev->ev == NULL) { 4350 log_err("could not baseset tcpacc event"); 4351 comm_point_delete(c); 4352 return NULL; 4353 } 4354 if (ub_event_add(c->ev->ev, c->timeout) != 0) { 4355 log_err("could not add tcpacc event"); 4356 comm_point_delete(c); 4357 return NULL; 4358 } 4359 c->event_added = 1; 4360 /* now prealloc the handlers */ 4361 for(i=0; i<num; i++) { 4362 if(port_type == listen_type_tcp || 4363 port_type == listen_type_ssl || 4364 port_type == listen_type_tcp_dnscrypt) { 4365 c->tcp_handlers[i] = comm_point_create_tcp_handler(base, 4366 c, bufsize, spoolbuf, callback, callback_arg, socket); 4367 } else if(port_type == listen_type_http) { 4368 c->tcp_handlers[i] = comm_point_create_http_handler( 4369 base, c, bufsize, harden_large_queries, 4370 http_max_streams, http_endpoint, 4371 callback, callback_arg, socket); 4372 } 4373 else { 4374 log_err("could not create tcp handler, unknown listen " 4375 "type"); 4376 return NULL; 4377 } 4378 if(!c->tcp_handlers[i]) { 4379 comm_point_delete(c); 4380 return NULL; 4381 } 4382 } 4383 4384 return c; 4385 } 4386 4387 struct comm_point* 4388 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize, 4389 comm_point_callback_type* callback, void* callback_arg) 4390 { 4391 struct comm_point* c = (struct comm_point*)calloc(1, 4392 sizeof(struct comm_point)); 4393 short evbits; 4394 if(!c) 4395 return NULL; 4396 c->ev = (struct internal_event*)calloc(1, 4397 sizeof(struct internal_event)); 4398 if(!c->ev) { 4399 free(c); 4400 return NULL; 4401 } 4402 c->ev->base = base; 4403 c->fd = -1; 4404 c->buffer = sldns_buffer_new(bufsize); 4405 if(!c->buffer) { 4406 free(c->ev); 4407 free(c); 4408 return NULL; 4409 } 4410 c->timeout = NULL; 4411 c->tcp_is_reading = 0; 4412 c->tcp_byte_count = 0; 4413 c->tcp_timeout_msec = TCP_QUERY_TIMEOUT; 4414 c->tcp_conn_limit = NULL; 4415 c->tcl_addr = NULL; 4416 c->tcp_keepalive = 0; 4417 c->tcp_parent = NULL; 4418 c->max_tcp_count = 0; 4419 c->cur_tcp_count = 0; 4420 c->tcp_handlers = NULL; 4421 c->tcp_free = NULL; 4422 c->type = comm_tcp; 4423 c->tcp_do_close = 0; 4424 c->do_not_close = 0; 4425 c->tcp_do_toggle_rw = 1; 4426 c->tcp_check_nb_connect = 1; 4427 #ifdef USE_MSG_FASTOPEN 4428 c->tcp_do_fastopen = 1; 4429 #endif 4430 #ifdef USE_DNSCRYPT 4431 c->dnscrypt = 0; 4432 c->dnscrypt_buffer = c->buffer; 4433 #endif 4434 c->repinfo.c = c; 4435 c->callback = callback; 4436 c->cb_arg = callback_arg; 4437 c->pp2_enabled = 0; 4438 c->pp2_header_state = pp2_header_none; 4439 evbits = UB_EV_PERSIST | UB_EV_WRITE; 4440 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4441 comm_point_tcp_handle_callback, c); 4442 if(c->ev->ev == NULL) 4443 { 4444 log_err("could not baseset tcpout event"); 4445 sldns_buffer_free(c->buffer); 4446 free(c->ev); 4447 free(c); 4448 return NULL; 4449 } 4450 4451 return c; 4452 } 4453 4454 struct comm_point* 4455 comm_point_create_http_out(struct comm_base *base, size_t bufsize, 4456 comm_point_callback_type* callback, void* callback_arg, 4457 sldns_buffer* temp) 4458 { 4459 struct comm_point* c = (struct comm_point*)calloc(1, 4460 sizeof(struct comm_point)); 4461 short evbits; 4462 if(!c) 4463 return NULL; 4464 c->ev = (struct internal_event*)calloc(1, 4465 sizeof(struct internal_event)); 4466 if(!c->ev) { 4467 free(c); 4468 return NULL; 4469 } 4470 c->ev->base = base; 4471 c->fd = -1; 4472 c->buffer = sldns_buffer_new(bufsize); 4473 if(!c->buffer) { 4474 free(c->ev); 4475 free(c); 4476 return NULL; 4477 } 4478 c->timeout = NULL; 4479 c->tcp_is_reading = 0; 4480 c->tcp_byte_count = 0; 4481 c->tcp_parent = NULL; 4482 c->max_tcp_count = 0; 4483 c->cur_tcp_count = 0; 4484 c->tcp_handlers = NULL; 4485 c->tcp_free = NULL; 4486 c->type = comm_http; 4487 c->tcp_do_close = 0; 4488 c->do_not_close = 0; 4489 c->tcp_do_toggle_rw = 1; 4490 c->tcp_check_nb_connect = 1; 4491 c->http_in_headers = 1; 4492 c->http_in_chunk_headers = 0; 4493 c->http_is_chunked = 0; 4494 c->http_temp = temp; 4495 #ifdef USE_MSG_FASTOPEN 4496 c->tcp_do_fastopen = 1; 4497 #endif 4498 #ifdef USE_DNSCRYPT 4499 c->dnscrypt = 0; 4500 c->dnscrypt_buffer = c->buffer; 4501 #endif 4502 c->repinfo.c = c; 4503 c->callback = callback; 4504 c->cb_arg = callback_arg; 4505 c->pp2_enabled = 0; 4506 c->pp2_header_state = pp2_header_none; 4507 evbits = UB_EV_PERSIST | UB_EV_WRITE; 4508 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4509 comm_point_http_handle_callback, c); 4510 if(c->ev->ev == NULL) 4511 { 4512 log_err("could not baseset tcpout event"); 4513 #ifdef HAVE_SSL 4514 SSL_free(c->ssl); 4515 #endif 4516 sldns_buffer_free(c->buffer); 4517 free(c->ev); 4518 free(c); 4519 return NULL; 4520 } 4521 4522 return c; 4523 } 4524 4525 struct comm_point* 4526 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize, 4527 comm_point_callback_type* callback, void* callback_arg) 4528 { 4529 struct comm_point* c = (struct comm_point*)calloc(1, 4530 sizeof(struct comm_point)); 4531 short evbits; 4532 if(!c) 4533 return NULL; 4534 c->ev = (struct internal_event*)calloc(1, 4535 sizeof(struct internal_event)); 4536 if(!c->ev) { 4537 free(c); 4538 return NULL; 4539 } 4540 c->ev->base = base; 4541 c->fd = fd; 4542 c->buffer = sldns_buffer_new(bufsize); 4543 if(!c->buffer) { 4544 free(c->ev); 4545 free(c); 4546 return NULL; 4547 } 4548 c->timeout = NULL; 4549 c->tcp_is_reading = 1; 4550 c->tcp_byte_count = 0; 4551 c->tcp_parent = NULL; 4552 c->max_tcp_count = 0; 4553 c->cur_tcp_count = 0; 4554 c->tcp_handlers = NULL; 4555 c->tcp_free = NULL; 4556 c->type = comm_local; 4557 c->tcp_do_close = 0; 4558 c->do_not_close = 1; 4559 c->tcp_do_toggle_rw = 0; 4560 c->tcp_check_nb_connect = 0; 4561 #ifdef USE_MSG_FASTOPEN 4562 c->tcp_do_fastopen = 0; 4563 #endif 4564 #ifdef USE_DNSCRYPT 4565 c->dnscrypt = 0; 4566 c->dnscrypt_buffer = c->buffer; 4567 #endif 4568 c->callback = callback; 4569 c->cb_arg = callback_arg; 4570 c->pp2_enabled = 0; 4571 c->pp2_header_state = pp2_header_none; 4572 /* ub_event stuff */ 4573 evbits = UB_EV_PERSIST | UB_EV_READ; 4574 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4575 comm_point_local_handle_callback, c); 4576 if(c->ev->ev == NULL) { 4577 log_err("could not baseset localhdl event"); 4578 free(c->ev); 4579 free(c); 4580 return NULL; 4581 } 4582 if (ub_event_add(c->ev->ev, c->timeout) != 0) { 4583 log_err("could not add localhdl event"); 4584 ub_event_free(c->ev->ev); 4585 free(c->ev); 4586 free(c); 4587 return NULL; 4588 } 4589 c->event_added = 1; 4590 return c; 4591 } 4592 4593 struct comm_point* 4594 comm_point_create_raw(struct comm_base* base, int fd, int writing, 4595 comm_point_callback_type* callback, void* callback_arg) 4596 { 4597 struct comm_point* c = (struct comm_point*)calloc(1, 4598 sizeof(struct comm_point)); 4599 short evbits; 4600 if(!c) 4601 return NULL; 4602 c->ev = (struct internal_event*)calloc(1, 4603 sizeof(struct internal_event)); 4604 if(!c->ev) { 4605 free(c); 4606 return NULL; 4607 } 4608 c->ev->base = base; 4609 c->fd = fd; 4610 c->buffer = NULL; 4611 c->timeout = NULL; 4612 c->tcp_is_reading = 0; 4613 c->tcp_byte_count = 0; 4614 c->tcp_parent = NULL; 4615 c->max_tcp_count = 0; 4616 c->cur_tcp_count = 0; 4617 c->tcp_handlers = NULL; 4618 c->tcp_free = NULL; 4619 c->type = comm_raw; 4620 c->tcp_do_close = 0; 4621 c->do_not_close = 1; 4622 c->tcp_do_toggle_rw = 0; 4623 c->tcp_check_nb_connect = 0; 4624 #ifdef USE_MSG_FASTOPEN 4625 c->tcp_do_fastopen = 0; 4626 #endif 4627 #ifdef USE_DNSCRYPT 4628 c->dnscrypt = 0; 4629 c->dnscrypt_buffer = c->buffer; 4630 #endif 4631 c->callback = callback; 4632 c->cb_arg = callback_arg; 4633 c->pp2_enabled = 0; 4634 c->pp2_header_state = pp2_header_none; 4635 /* ub_event stuff */ 4636 if(writing) 4637 evbits = UB_EV_PERSIST | UB_EV_WRITE; 4638 else evbits = UB_EV_PERSIST | UB_EV_READ; 4639 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits, 4640 comm_point_raw_handle_callback, c); 4641 if(c->ev->ev == NULL) { 4642 log_err("could not baseset rawhdl event"); 4643 free(c->ev); 4644 free(c); 4645 return NULL; 4646 } 4647 if (ub_event_add(c->ev->ev, c->timeout) != 0) { 4648 log_err("could not add rawhdl event"); 4649 ub_event_free(c->ev->ev); 4650 free(c->ev); 4651 free(c); 4652 return NULL; 4653 } 4654 c->event_added = 1; 4655 return c; 4656 } 4657 4658 void 4659 comm_point_close(struct comm_point* c) 4660 { 4661 if(!c) 4662 return; 4663 if(c->fd != -1) { 4664 verbose(5, "comm_point_close of %d: event_del", c->fd); 4665 if(c->event_added) { 4666 if(ub_event_del(c->ev->ev) != 0) { 4667 log_err("could not event_del on close"); 4668 } 4669 c->event_added = 0; 4670 } 4671 } 4672 tcl_close_connection(c->tcl_addr); 4673 if(c->tcp_req_info) 4674 tcp_req_info_clear(c->tcp_req_info); 4675 if(c->h2_session) 4676 http2_session_server_delete(c->h2_session); 4677 /* stop the comm point from reading or writing after it is closed. */ 4678 if(c->tcp_more_read_again && *c->tcp_more_read_again) 4679 *c->tcp_more_read_again = 0; 4680 if(c->tcp_more_write_again && *c->tcp_more_write_again) 4681 *c->tcp_more_write_again = 0; 4682 4683 /* close fd after removing from event lists, or epoll.. is messed up */ 4684 if(c->fd != -1 && !c->do_not_close) { 4685 #ifdef USE_WINSOCK 4686 if(c->type == comm_tcp || c->type == comm_http) { 4687 /* delete sticky events for the fd, it gets closed */ 4688 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ); 4689 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE); 4690 } 4691 #endif 4692 verbose(VERB_ALGO, "close fd %d", c->fd); 4693 sock_close(c->fd); 4694 } 4695 c->fd = -1; 4696 } 4697 4698 void 4699 comm_point_delete(struct comm_point* c) 4700 { 4701 if(!c) 4702 return; 4703 if((c->type == comm_tcp || c->type == comm_http) && c->ssl) { 4704 #ifdef HAVE_SSL 4705 SSL_shutdown(c->ssl); 4706 SSL_free(c->ssl); 4707 #endif 4708 } 4709 if(c->type == comm_http && c->http_endpoint) { 4710 free(c->http_endpoint); 4711 c->http_endpoint = NULL; 4712 } 4713 comm_point_close(c); 4714 if(c->tcp_handlers) { 4715 int i; 4716 for(i=0; i<c->max_tcp_count; i++) 4717 comm_point_delete(c->tcp_handlers[i]); 4718 free(c->tcp_handlers); 4719 } 4720 free(c->timeout); 4721 if(c->type == comm_tcp || c->type == comm_local || c->type == comm_http) { 4722 sldns_buffer_free(c->buffer); 4723 #ifdef USE_DNSCRYPT 4724 if(c->dnscrypt && c->dnscrypt_buffer != c->buffer) { 4725 sldns_buffer_free(c->dnscrypt_buffer); 4726 } 4727 #endif 4728 if(c->tcp_req_info) { 4729 tcp_req_info_delete(c->tcp_req_info); 4730 } 4731 if(c->h2_session) { 4732 http2_session_delete(c->h2_session); 4733 } 4734 } 4735 ub_event_free(c->ev->ev); 4736 free(c->ev); 4737 free(c); 4738 } 4739 4740 void 4741 comm_point_send_reply(struct comm_reply *repinfo) 4742 { 4743 struct sldns_buffer* buffer; 4744 log_assert(repinfo && repinfo->c); 4745 #ifdef USE_DNSCRYPT 4746 buffer = repinfo->c->dnscrypt_buffer; 4747 if(!dnsc_handle_uncurved_request(repinfo)) { 4748 return; 4749 } 4750 #else 4751 buffer = repinfo->c->buffer; 4752 #endif 4753 if(repinfo->c->type == comm_udp) { 4754 if(repinfo->srctype) 4755 comm_point_send_udp_msg_if(repinfo->c, buffer, 4756 (struct sockaddr*)&repinfo->remote_addr, 4757 repinfo->remote_addrlen, repinfo); 4758 else 4759 comm_point_send_udp_msg(repinfo->c, buffer, 4760 (struct sockaddr*)&repinfo->remote_addr, 4761 repinfo->remote_addrlen, 0); 4762 #ifdef USE_DNSTAP 4763 /* 4764 * sending src (client)/dst (local service) addresses over DNSTAP from udp callback 4765 */ 4766 if(repinfo->c->dtenv != NULL && repinfo->c->dtenv->log_client_response_messages) { 4767 log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen); 4768 log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen); 4769 dt_msg_send_client_response(repinfo->c->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->type, repinfo->c->buffer); 4770 } 4771 #endif 4772 } else { 4773 #ifdef USE_DNSTAP 4774 /* 4775 * sending src (client)/dst (local service) addresses over DNSTAP from TCP callback 4776 */ 4777 if(repinfo->c->tcp_parent->dtenv != NULL && repinfo->c->tcp_parent->dtenv->log_client_response_messages) { 4778 log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen); 4779 log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen); 4780 dt_msg_send_client_response(repinfo->c->tcp_parent->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->type, 4781 ( repinfo->c->tcp_req_info? repinfo->c->tcp_req_info->spool_buffer: repinfo->c->buffer )); 4782 } 4783 #endif 4784 if(repinfo->c->tcp_req_info) { 4785 tcp_req_info_send_reply(repinfo->c->tcp_req_info); 4786 } else if(repinfo->c->use_h2) { 4787 if(!http2_submit_dns_response(repinfo->c->h2_session)) { 4788 comm_point_drop_reply(repinfo); 4789 return; 4790 } 4791 repinfo->c->h2_stream = NULL; 4792 repinfo->c->tcp_is_reading = 0; 4793 comm_point_stop_listening(repinfo->c); 4794 comm_point_start_listening(repinfo->c, -1, 4795 adjusted_tcp_timeout(repinfo->c)); 4796 return; 4797 } else { 4798 comm_point_start_listening(repinfo->c, -1, 4799 adjusted_tcp_timeout(repinfo->c)); 4800 } 4801 } 4802 } 4803 4804 void 4805 comm_point_drop_reply(struct comm_reply* repinfo) 4806 { 4807 if(!repinfo) 4808 return; 4809 log_assert(repinfo->c); 4810 log_assert(repinfo->c->type != comm_tcp_accept); 4811 if(repinfo->c->type == comm_udp) 4812 return; 4813 if(repinfo->c->tcp_req_info) 4814 repinfo->c->tcp_req_info->is_drop = 1; 4815 if(repinfo->c->type == comm_http) { 4816 if(repinfo->c->h2_session) { 4817 repinfo->c->h2_session->is_drop = 1; 4818 if(!repinfo->c->h2_session->postpone_drop) 4819 reclaim_http_handler(repinfo->c); 4820 return; 4821 } 4822 reclaim_http_handler(repinfo->c); 4823 return; 4824 } 4825 reclaim_tcp_handler(repinfo->c); 4826 } 4827 4828 void 4829 comm_point_stop_listening(struct comm_point* c) 4830 { 4831 verbose(VERB_ALGO, "comm point stop listening %d", c->fd); 4832 if(c->event_added) { 4833 if(ub_event_del(c->ev->ev) != 0) { 4834 log_err("event_del error to stoplisten"); 4835 } 4836 c->event_added = 0; 4837 } 4838 } 4839 4840 void 4841 comm_point_start_listening(struct comm_point* c, int newfd, int msec) 4842 { 4843 verbose(VERB_ALGO, "comm point start listening %d (%d msec)", 4844 c->fd==-1?newfd:c->fd, msec); 4845 if(c->type == comm_tcp_accept && !c->tcp_free) { 4846 /* no use to start listening no free slots. */ 4847 return; 4848 } 4849 if(c->event_added) { 4850 if(ub_event_del(c->ev->ev) != 0) { 4851 log_err("event_del error to startlisten"); 4852 } 4853 c->event_added = 0; 4854 } 4855 if(msec != -1 && msec != 0) { 4856 if(!c->timeout) { 4857 c->timeout = (struct timeval*)malloc(sizeof( 4858 struct timeval)); 4859 if(!c->timeout) { 4860 log_err("cpsl: malloc failed. No net read."); 4861 return; 4862 } 4863 } 4864 ub_event_add_bits(c->ev->ev, UB_EV_TIMEOUT); 4865 #ifndef S_SPLINT_S /* splint fails on struct timeval. */ 4866 c->timeout->tv_sec = msec/1000; 4867 c->timeout->tv_usec = (msec%1000)*1000; 4868 #endif /* S_SPLINT_S */ 4869 } else { 4870 if(msec == 0 || !c->timeout) { 4871 ub_event_del_bits(c->ev->ev, UB_EV_TIMEOUT); 4872 } 4873 } 4874 if(c->type == comm_tcp || c->type == comm_http) { 4875 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE); 4876 if(c->tcp_write_and_read) { 4877 verbose(5, "startlistening %d mode rw", (newfd==-1?c->fd:newfd)); 4878 ub_event_add_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE); 4879 } else if(c->tcp_is_reading) { 4880 verbose(5, "startlistening %d mode r", (newfd==-1?c->fd:newfd)); 4881 ub_event_add_bits(c->ev->ev, UB_EV_READ); 4882 } else { 4883 verbose(5, "startlistening %d mode w", (newfd==-1?c->fd:newfd)); 4884 ub_event_add_bits(c->ev->ev, UB_EV_WRITE); 4885 } 4886 } 4887 if(newfd != -1) { 4888 if(c->fd != -1 && c->fd != newfd) { 4889 verbose(5, "cpsl close of fd %d for %d", c->fd, newfd); 4890 sock_close(c->fd); 4891 } 4892 c->fd = newfd; 4893 ub_event_set_fd(c->ev->ev, c->fd); 4894 } 4895 if(ub_event_add(c->ev->ev, msec==0?NULL:c->timeout) != 0) { 4896 log_err("event_add failed. in cpsl."); 4897 return; 4898 } 4899 c->event_added = 1; 4900 } 4901 4902 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr) 4903 { 4904 verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr); 4905 if(c->event_added) { 4906 if(ub_event_del(c->ev->ev) != 0) { 4907 log_err("event_del error to cplf"); 4908 } 4909 c->event_added = 0; 4910 } 4911 if(!c->timeout) { 4912 ub_event_del_bits(c->ev->ev, UB_EV_TIMEOUT); 4913 } 4914 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE); 4915 if(rd) ub_event_add_bits(c->ev->ev, UB_EV_READ); 4916 if(wr) ub_event_add_bits(c->ev->ev, UB_EV_WRITE); 4917 if(ub_event_add(c->ev->ev, c->timeout) != 0) { 4918 log_err("event_add failed. in cplf."); 4919 return; 4920 } 4921 c->event_added = 1; 4922 } 4923 4924 size_t comm_point_get_mem(struct comm_point* c) 4925 { 4926 size_t s; 4927 if(!c) 4928 return 0; 4929 s = sizeof(*c) + sizeof(*c->ev); 4930 if(c->timeout) 4931 s += sizeof(*c->timeout); 4932 if(c->type == comm_tcp || c->type == comm_local) { 4933 s += sizeof(*c->buffer) + sldns_buffer_capacity(c->buffer); 4934 #ifdef USE_DNSCRYPT 4935 s += sizeof(*c->dnscrypt_buffer); 4936 if(c->buffer != c->dnscrypt_buffer) { 4937 s += sldns_buffer_capacity(c->dnscrypt_buffer); 4938 } 4939 #endif 4940 } 4941 if(c->type == comm_tcp_accept) { 4942 int i; 4943 for(i=0; i<c->max_tcp_count; i++) 4944 s += comm_point_get_mem(c->tcp_handlers[i]); 4945 } 4946 return s; 4947 } 4948 4949 struct comm_timer* 4950 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg) 4951 { 4952 struct internal_timer *tm = (struct internal_timer*)calloc(1, 4953 sizeof(struct internal_timer)); 4954 if(!tm) { 4955 log_err("malloc failed"); 4956 return NULL; 4957 } 4958 tm->super.ev_timer = tm; 4959 tm->base = base; 4960 tm->super.callback = cb; 4961 tm->super.cb_arg = cb_arg; 4962 tm->ev = ub_event_new(base->eb->base, -1, UB_EV_TIMEOUT, 4963 comm_timer_callback, &tm->super); 4964 if(tm->ev == NULL) { 4965 log_err("timer_create: event_base_set failed."); 4966 free(tm); 4967 return NULL; 4968 } 4969 return &tm->super; 4970 } 4971 4972 void 4973 comm_timer_disable(struct comm_timer* timer) 4974 { 4975 if(!timer) 4976 return; 4977 ub_timer_del(timer->ev_timer->ev); 4978 timer->ev_timer->enabled = 0; 4979 } 4980 4981 void 4982 comm_timer_set(struct comm_timer* timer, struct timeval* tv) 4983 { 4984 log_assert(tv); 4985 if(timer->ev_timer->enabled) 4986 comm_timer_disable(timer); 4987 if(ub_timer_add(timer->ev_timer->ev, timer->ev_timer->base->eb->base, 4988 comm_timer_callback, timer, tv) != 0) 4989 log_err("comm_timer_set: evtimer_add failed."); 4990 timer->ev_timer->enabled = 1; 4991 } 4992 4993 void 4994 comm_timer_delete(struct comm_timer* timer) 4995 { 4996 if(!timer) 4997 return; 4998 comm_timer_disable(timer); 4999 /* Free the sub struct timer->ev_timer derived from the super struct timer. 5000 * i.e. assert(timer == timer->ev_timer) 5001 */ 5002 ub_event_free(timer->ev_timer->ev); 5003 free(timer->ev_timer); 5004 } 5005 5006 void 5007 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg) 5008 { 5009 struct comm_timer* tm = (struct comm_timer*)arg; 5010 if(!(event&UB_EV_TIMEOUT)) 5011 return; 5012 ub_comm_base_now(tm->ev_timer->base); 5013 tm->ev_timer->enabled = 0; 5014 fptr_ok(fptr_whitelist_comm_timer(tm->callback)); 5015 (*tm->callback)(tm->cb_arg); 5016 } 5017 5018 int 5019 comm_timer_is_set(struct comm_timer* timer) 5020 { 5021 return (int)timer->ev_timer->enabled; 5022 } 5023 5024 size_t 5025 comm_timer_get_mem(struct comm_timer* ATTR_UNUSED(timer)) 5026 { 5027 return sizeof(struct internal_timer); 5028 } 5029 5030 struct comm_signal* 5031 comm_signal_create(struct comm_base* base, 5032 void (*callback)(int, void*), void* cb_arg) 5033 { 5034 struct comm_signal* com = (struct comm_signal*)malloc( 5035 sizeof(struct comm_signal)); 5036 if(!com) { 5037 log_err("malloc failed"); 5038 return NULL; 5039 } 5040 com->base = base; 5041 com->callback = callback; 5042 com->cb_arg = cb_arg; 5043 com->ev_signal = NULL; 5044 return com; 5045 } 5046 5047 void 5048 comm_signal_callback(int sig, short event, void* arg) 5049 { 5050 struct comm_signal* comsig = (struct comm_signal*)arg; 5051 if(!(event & UB_EV_SIGNAL)) 5052 return; 5053 ub_comm_base_now(comsig->base); 5054 fptr_ok(fptr_whitelist_comm_signal(comsig->callback)); 5055 (*comsig->callback)(sig, comsig->cb_arg); 5056 } 5057 5058 int 5059 comm_signal_bind(struct comm_signal* comsig, int sig) 5060 { 5061 struct internal_signal* entry = (struct internal_signal*)calloc(1, 5062 sizeof(struct internal_signal)); 5063 if(!entry) { 5064 log_err("malloc failed"); 5065 return 0; 5066 } 5067 log_assert(comsig); 5068 /* add signal event */ 5069 entry->ev = ub_signal_new(comsig->base->eb->base, sig, 5070 comm_signal_callback, comsig); 5071 if(entry->ev == NULL) { 5072 log_err("Could not create signal event"); 5073 free(entry); 5074 return 0; 5075 } 5076 if(ub_signal_add(entry->ev, NULL) != 0) { 5077 log_err("Could not add signal handler"); 5078 ub_event_free(entry->ev); 5079 free(entry); 5080 return 0; 5081 } 5082 /* link into list */ 5083 entry->next = comsig->ev_signal; 5084 comsig->ev_signal = entry; 5085 return 1; 5086 } 5087 5088 void 5089 comm_signal_delete(struct comm_signal* comsig) 5090 { 5091 struct internal_signal* p, *np; 5092 if(!comsig) 5093 return; 5094 p=comsig->ev_signal; 5095 while(p) { 5096 np = p->next; 5097 ub_signal_del(p->ev); 5098 ub_event_free(p->ev); 5099 free(p); 5100 p = np; 5101 } 5102 free(comsig); 5103 } 5104