2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2021 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_quic_listener.h"
26 #include "shrpx_worker.h"
27 #include "shrpx_config.h"
28 #include "shrpx_log.h"
33 void readcb(struct ev_loop *loop, ev_io *w, int revent) {
34 auto l = static_cast<QUICListener *>(w->data);
39 QUICListener::QUICListener(const UpstreamAddr *faddr, Worker *worker)
40 : faddr_{faddr}, worker_{worker} {
41 ev_io_init(&rev_, readcb, faddr_->fd, EV_READ);
43 ev_io_start(worker_->get_loop(), &rev_);
46 QUICListener::~QUICListener() {
47 ev_io_stop(worker_->get_loop(), &rev_);
51 void QUICListener::on_read() {
53 std::array<uint8_t, 64_k> buf;
55 iovec msg_iov{buf.data(), buf.size()};
59 msg.msg_iov = &msg_iov;
62 uint8_t msg_ctrl[CMSG_SPACE(sizeof(in6_pktinfo))];
63 msg.msg_control = msg_ctrl;
65 auto quic_conn_handler = worker_->get_quic_connection_handler();
67 for (; pktcnt < 10;) {
68 msg.msg_namelen = sizeof(su);
69 msg.msg_controllen = sizeof(msg_ctrl);
71 auto nread = recvmsg(faddr_->fd, &msg, 0);
79 if (util::msghdr_get_local_addr(local_addr, &msg, su.storage.ss_family) !=
84 util::set_port(local_addr, faddr_->port);
86 if (LOG_ENABLED(INFO)) {
87 LOG(INFO) << "QUIC received packet: local="
88 << util::to_numeric_addr(&local_addr)
89 << " remote=" << util::to_numeric_addr(&su.sa, msg.msg_namelen)
90 << " " << nread << " bytes";
99 remote_addr.len = msg.msg_namelen;
101 quic_conn_handler->handle_packet(faddr_, remote_addr, local_addr,