2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 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_connection.h"
29 #endif // HAVE_UNISTD_H
30 #include <netinet/tcp.h>
34 #include <openssl/err.h>
36 #include "shrpx_tls.h"
37 #include "shrpx_memcached_request.h"
38 #include "shrpx_log.h"
41 #include "ssl_compat.h"
43 using namespace nghttp2;
47 #if !LIBRESSL_2_7_API && !OPENSSL_1_1_API
49 void *BIO_get_data(BIO *bio) { return bio->ptr; }
50 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
51 void BIO_set_init(BIO *bio, int init) { bio->init = init; }
53 #endif // !LIBRESSL_2_7_API && !OPENSSL_1_1_API
55 Connection::Connection(struct ev_loop *loop, int fd, SSL *ssl,
56 MemchunkPool *mcpool, ev_tstamp write_timeout,
57 ev_tstamp read_timeout,
58 const RateLimitConfig &write_limit,
59 const RateLimitConfig &read_limit, IOCb writecb,
60 IOCb readcb, TimerCb timeoutcb, void *data,
61 size_t tls_dyn_rec_warmup_threshold,
62 ev_tstamp tls_dyn_rec_idle_timeout, Proto proto)
63 : tls{DefaultMemchunks(mcpool), DefaultPeekMemchunks(mcpool),
64 DefaultMemchunks(mcpool)},
65 wlimit(loop, &wev, write_limit.rate, write_limit.burst),
66 rlimit(loop, &rev, read_limit.rate, read_limit.burst, this),
70 tls_dyn_rec_warmup_threshold(tls_dyn_rec_warmup_threshold),
71 tls_dyn_rec_idle_timeout(tls_dyn_rec_idle_timeout),
74 read_timeout(read_timeout) {
76 ev_io_init(&wev, writecb, fd, EV_WRITE);
77 ev_io_init(&rev, readcb, fd, EV_READ);
82 ev_timer_init(&wt, timeoutcb, 0., write_timeout);
83 ev_timer_init(&rt, timeoutcb, 0., read_timeout);
88 // set 0. to double field explicitly just in case
89 tls.last_write_idle = 0.;
96 Connection::~Connection() { disconnect(); }
98 void Connection::disconnect() {
100 SSL_set_shutdown(tls.ssl,
101 SSL_get_shutdown(tls.ssl) | SSL_RECEIVED_SHUTDOWN);
104 if (tls.cached_session) {
105 SSL_SESSION_free(tls.cached_session);
106 tls.cached_session = nullptr;
109 if (tls.cached_session_lookup_req) {
110 tls.cached_session_lookup_req->canceled = true;
111 tls.cached_session_lookup_req = nullptr;
114 SSL_shutdown(tls.ssl);
120 tls.last_write_idle = 0.;
121 tls.warmup_writelen = 0;
122 tls.last_writelen = 0;
123 tls.last_readlen = 0;
124 tls.handshake_state = TLSHandshakeState::NORMAL;
125 tls.initial_handshake_done = false;
126 tls.reneg_started = false;
127 tls.sct_requested = false;
128 tls.early_data_finish = false;
132 shutdown(fd, SHUT_WR);
137 // Stop watchers here because they could be activated in
139 ev_timer_stop(loop, &rt);
140 ev_timer_stop(loop, &wt);
146 void Connection::prepare_client_handshake() {
147 SSL_set_connect_state(tls.ssl);
148 // This prevents SSL_read_early_data from being called.
149 tls.early_data_finish = true;
152 void Connection::prepare_server_handshake() {
153 SSL_set_accept_state(tls.ssl);
154 tls.server_handshake = true;
157 // BIO implementation is inspired by openldap implementation:
158 // http://www.openldap.org/devel/cvsweb.cgi/~checkout~/libraries/libldap/tls_o.c
160 int shrpx_bio_write(BIO *b, const char *buf, int len) {
161 if (buf == nullptr || len <= 0) {
165 auto conn = static_cast<Connection *>(BIO_get_data(b));
166 auto &wbuf = conn->tls.wbuf;
168 BIO_clear_retry_flags(b);
170 if (conn->tls.initial_handshake_done) {
171 // After handshake finished, send |buf| of length |len| to the
174 // Only when TLS session was prematurely ended before server sent
175 // all handshake message, this condition is true. This could be
176 // alert from SSL_shutdown(). Since connection is already down,
177 // just return error.
181 auto nwrite = conn->write_clear(buf, len);
187 BIO_set_retry_write(b);
194 wbuf.append(buf, len);
201 int shrpx_bio_read(BIO *b, char *buf, int len) {
202 if (buf == nullptr || len <= 0) {
206 auto conn = static_cast<Connection *>(BIO_get_data(b));
207 auto &rbuf = conn->tls.rbuf;
209 BIO_clear_retry_flags(b);
211 if (conn->tls.initial_handshake_done && rbuf.rleft() == 0) {
212 auto nread = conn->read_clear(buf, len);
217 BIO_set_retry_read(b);
223 if (rbuf.rleft() == 0) {
224 BIO_set_retry_read(b);
228 return rbuf.remove(buf, len);
233 int shrpx_bio_puts(BIO *b, const char *str) {
234 return shrpx_bio_write(b, str, strlen(str));
239 int shrpx_bio_gets(BIO *b, char *buf, int len) { return -1; }
243 long shrpx_bio_ctrl(BIO *b, int cmd, long num, void *ptr) {
254 int shrpx_bio_create(BIO *b) {
257 #else // !OPENSSL_1_1_API
262 #endif // !OPENSSL_1_1_API
268 int shrpx_bio_destroy(BIO *b) {
277 #endif // !OPENSSL_1_1_API
285 BIO_METHOD *create_bio_method() {
286 auto meth = BIO_meth_new(BIO_TYPE_FD, "nghttpx-bio");
287 BIO_meth_set_write(meth, shrpx_bio_write);
288 BIO_meth_set_read(meth, shrpx_bio_read);
289 BIO_meth_set_puts(meth, shrpx_bio_puts);
290 BIO_meth_set_gets(meth, shrpx_bio_gets);
291 BIO_meth_set_ctrl(meth, shrpx_bio_ctrl);
292 BIO_meth_set_create(meth, shrpx_bio_create);
293 BIO_meth_set_destroy(meth, shrpx_bio_destroy);
298 #else // !OPENSSL_1_1_API
300 BIO_METHOD *create_bio_method() {
301 static auto meth = new BIO_METHOD{
302 BIO_TYPE_FD, "nghttpx-bio", shrpx_bio_write,
303 shrpx_bio_read, shrpx_bio_puts, shrpx_bio_gets,
304 shrpx_bio_ctrl, shrpx_bio_create, shrpx_bio_destroy,
310 #endif // !OPENSSL_1_1_API
312 void Connection::set_ssl(SSL *ssl) {
315 auto &tlsconf = get_config()->tls;
316 auto bio = BIO_new(tlsconf.bio_method);
317 BIO_set_data(bio, this);
318 SSL_set_bio(tls.ssl, bio, bio);
319 SSL_set_app_data(tls.ssl, this);
323 // We should buffer at least full encrypted TLS record here.
324 // Theoretically, peer can send client hello in several TLS records,
325 // which could exceed this limit, but it is not portable, and we don't
326 // have to handle such exotic behaviour.
327 bool read_buffer_full(DefaultPeekMemchunks &rbuf) {
328 return rbuf.rleft_buffered() >= 20_k;
332 int Connection::tls_handshake() {
334 ev_timer_stop(loop, &wt);
336 std::array<uint8_t, 16_k> buf;
338 if (ev_is_active(&rev)) {
339 auto nread = read_clear(buf.data(), buf.size());
341 if (LOG_ENABLED(INFO)) {
342 LOG(INFO) << "tls: handshake read error";
346 tls.rbuf.append(buf.data(), nread);
347 if (read_buffer_full(tls.rbuf)) {
352 if (tls.initial_handshake_done) {
353 return write_tls_pending_handshake();
356 switch (tls.handshake_state) {
357 case TLSHandshakeState::WAIT_FOR_SESSION_CACHE:
358 return SHRPX_ERR_INPROGRESS;
359 case TLSHandshakeState::GOT_SESSION_CACHE: {
360 // Use the same trick invented by @kazuho in h2o project.
362 // Discard all outgoing data.
364 // Rewind buffered incoming data to replay client hello.
365 tls.rbuf.disable_peek(false);
367 auto ssl_ctx = SSL_get_SSL_CTX(tls.ssl);
368 auto ssl_opts = SSL_get_options(tls.ssl);
371 auto ssl = tls::create_ssl(ssl_ctx);
375 if (ssl_opts & SSL_OP_NO_TICKET) {
376 SSL_set_options(ssl, SSL_OP_NO_TICKET);
381 SSL_set_accept_state(tls.ssl);
383 tls.handshake_state = TLSHandshakeState::NORMAL;
386 case TLSHandshakeState::CANCEL_SESSION_CACHE:
387 tls.handshake_state = TLSHandshakeState::NORMAL;
397 #if OPENSSL_1_1_1_API
398 if (!tls.server_handshake || tls.early_data_finish) {
399 rv = SSL_do_handshake(tls.ssl);
401 auto &tlsconf = get_config()->tls;
405 rv = SSL_read_early_data(tls.ssl, buf.data(), buf.size(), &nread);
406 if (rv == SSL_READ_EARLY_DATA_ERROR) {
407 // If we have early data, and server sends ServerHello, assume
408 // that handshake is completed in server side, and start
409 // processing request. If we don't exit handshake code here,
410 // server waits for EndOfEarlyData and Finished message from
411 // client, which voids the purpose of 0-RTT data. The left
412 // over of handshake is done through write_tls or read_tls.
413 if (tlsconf.no_postpone_early_data &&
414 (tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
416 tls.earlybuf.rleft()) {
423 if (LOG_ENABLED(INFO)) {
424 LOG(INFO) << "tls: read early data " << nread << " bytes";
427 tls.earlybuf.append(buf.data(), nread);
429 if (rv == SSL_READ_EARLY_DATA_FINISH) {
430 if (LOG_ENABLED(INFO)) {
431 LOG(INFO) << "tls: read all early data; total "
432 << tls.earlybuf.rleft() << " bytes";
434 tls.early_data_finish = true;
435 // The same reason stated above.
436 if (tlsconf.no_postpone_early_data &&
437 (tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
439 tls.earlybuf.rleft()) {
443 rv = SSL_do_handshake(tls.ssl);
449 #else // !OPENSSL_1_1_1_API
450 rv = SSL_do_handshake(tls.ssl);
451 #endif // !OPENSSL_1_1_1_API
454 auto err = SSL_get_error(tls.ssl, rv);
456 case SSL_ERROR_WANT_READ:
457 if (read_buffer_full(tls.rbuf)) {
458 if (LOG_ENABLED(INFO)) {
459 LOG(INFO) << "tls: handshake message is too large";
464 case SSL_ERROR_WANT_WRITE:
466 case SSL_ERROR_SSL: {
467 if (LOG_ENABLED(INFO)) {
468 LOG(INFO) << "tls: handshake libssl error: "
469 << ERR_error_string(ERR_get_error(), nullptr);
473 auto iovcnt = tls.wbuf.riovec(iov, 1);
474 auto nwrite = writev_clear(iov, iovcnt);
476 tls.wbuf.drain(nwrite);
479 return SHRPX_ERR_NETWORK;
482 if (LOG_ENABLED(INFO)) {
483 LOG(INFO) << "tls: handshake libssl error " << err;
485 return SHRPX_ERR_NETWORK;
489 if (tls.handshake_state == TLSHandshakeState::WAIT_FOR_SESSION_CACHE) {
490 if (LOG_ENABLED(INFO)) {
491 LOG(INFO) << "tls: handshake is still in progress";
493 return SHRPX_ERR_INPROGRESS;
496 // Don't send handshake data if handshake was completed in OpenSSL
497 // routine. We have to check HTTP/2 requirement if HTTP/2 was
498 // negotiated before sending finished message to the peer.
499 if (rv != 1 && tls.wbuf.rleft()) {
500 // First write indicates that resumption stuff has done.
501 if (tls.handshake_state != TLSHandshakeState::WRITE_STARTED) {
502 tls.handshake_state = TLSHandshakeState::WRITE_STARTED;
503 // If peek has already disabled, this is noop.
504 tls.rbuf.disable_peek(true);
506 std::array<struct iovec, 4> iov;
507 auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
508 auto nwrite = writev_clear(iov.data(), iovcnt);
510 if (LOG_ENABLED(INFO)) {
511 LOG(INFO) << "tls: handshake write error";
515 tls.wbuf.drain(nwrite);
517 if (tls.wbuf.rleft()) {
519 ev_timer_again(loop, &wt);
523 if (!read_buffer_full(tls.rbuf)) {
524 // We may have stopped reading
529 if (LOG_ENABLED(INFO)) {
530 LOG(INFO) << "tls: handshake is still in progress";
532 return SHRPX_ERR_INPROGRESS;
535 // Handshake was done
537 rv = check_http2_requirement();
543 tls.rbuf.disable_peek(true);
545 tls.initial_handshake_done = true;
547 return write_tls_pending_handshake();
550 int Connection::write_tls_pending_handshake() {
551 // Send handshake data left in the buffer
552 while (tls.wbuf.rleft()) {
553 std::array<struct iovec, 4> iov;
554 auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
555 auto nwrite = writev_clear(iov.data(), iovcnt);
557 if (LOG_ENABLED(INFO)) {
558 LOG(INFO) << "tls: handshake write error";
564 ev_timer_again(loop, &wt);
566 return SHRPX_ERR_INPROGRESS;
568 tls.wbuf.drain(nwrite);
571 // We have to start read watcher, since later stage of code expects
575 // We may have whole request in tls.rbuf. This means that we don't
576 // get notified further read event. This is especially true for
578 handle_tls_pending_read();
580 if (LOG_ENABLED(INFO)) {
581 LOG(INFO) << "SSL/TLS handshake completed";
582 nghttp2::tls::TLSSessionInfo tls_info{};
583 if (nghttp2::tls::get_tls_session_info(&tls_info, tls.ssl)) {
584 LOG(INFO) << "cipher=" << tls_info.cipher
585 << " protocol=" << tls_info.protocol
586 << " resumption=" << (tls_info.session_reused ? "yes" : "no")
588 << util::format_hex(tls_info.session_id,
589 tls_info.session_id_length);
596 int Connection::check_http2_requirement() {
597 const unsigned char *next_proto = nullptr;
598 unsigned int next_proto_len;
600 #ifndef OPENSSL_NO_NEXTPROTONEG
601 SSL_get0_next_proto_negotiated(tls.ssl, &next_proto, &next_proto_len);
602 #endif // !OPENSSL_NO_NEXTPROTONEG
603 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
604 if (next_proto == nullptr) {
605 SSL_get0_alpn_selected(tls.ssl, &next_proto, &next_proto_len);
607 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
608 if (next_proto == nullptr ||
609 !util::check_h2_is_selected(StringRef{next_proto, next_proto_len})) {
612 if (!nghttp2::tls::check_http2_tls_version(tls.ssl)) {
613 if (LOG_ENABLED(INFO)) {
614 LOG(INFO) << "TLSv1.2 was not negotiated. HTTP/2 must not be used.";
619 auto check_black_list = false;
620 if (tls.server_handshake) {
621 check_black_list = !get_config()->tls.no_http2_cipher_black_list;
623 check_black_list = !get_config()->tls.client.no_http2_cipher_black_list;
626 if (check_black_list &&
627 nghttp2::tls::check_http2_cipher_black_list(tls.ssl)) {
628 if (LOG_ENABLED(INFO)) {
629 LOG(INFO) << "The negotiated cipher suite is in HTTP/2 cipher suite "
630 "black list. HTTP/2 must not be used.";
639 constexpr size_t SHRPX_SMALL_WRITE_LIMIT = 1300;
642 size_t Connection::get_tls_write_limit() {
644 if (tls_dyn_rec_warmup_threshold == 0) {
645 return std::numeric_limits<ssize_t>::max();
648 auto t = ev_now(loop);
650 if (tls.last_write_idle >= 0. &&
651 t - tls.last_write_idle > tls_dyn_rec_idle_timeout) {
652 // Time out, use small record size
653 tls.warmup_writelen = 0;
654 return SHRPX_SMALL_WRITE_LIMIT;
657 if (tls.warmup_writelen >= tls_dyn_rec_warmup_threshold) {
658 return std::numeric_limits<ssize_t>::max();
661 return SHRPX_SMALL_WRITE_LIMIT;
664 void Connection::update_tls_warmup_writelen(size_t n) {
665 if (tls.warmup_writelen < tls_dyn_rec_warmup_threshold) {
666 tls.warmup_writelen += n;
670 void Connection::start_tls_write_idle() {
671 if (tls.last_write_idle < 0.) {
672 tls.last_write_idle = ev_now(loop);
676 ssize_t Connection::write_tls(const void *data, size_t len) {
677 // SSL_write requires the same arguments (buf pointer and its
678 // length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
679 // get_write_limit() may return smaller length than previously
680 // passed to SSL_write, which violates OpenSSL assumption. To avoid
681 // this, we keep last legnth passed to SSL_write to
682 // tls.last_writelen if SSL_write indicated I/O blocking.
683 if (tls.last_writelen == 0) {
684 len = std::min(len, wlimit.avail());
685 len = std::min(len, get_tls_write_limit());
690 len = tls.last_writelen;
691 tls.last_writelen = 0;
694 tls.last_write_idle = -1.;
698 #if OPENSSL_1_1_1_API
700 if (SSL_is_init_finished(tls.ssl)) {
701 rv = SSL_write(tls.ssl, data, len);
704 rv = SSL_write_early_data(tls.ssl, data, len, &nwrite);
705 // Use the same semantics with SSL_write.
710 #else // !OPENSSL_1_1_1_API
711 auto rv = SSL_write(tls.ssl, data, len);
712 #endif // !OPENSSL_1_1_1_API
715 auto err = SSL_get_error(tls.ssl, rv);
717 case SSL_ERROR_WANT_READ:
718 if (LOG_ENABLED(INFO)) {
719 LOG(INFO) << "Close connection due to TLS renegotiation";
721 return SHRPX_ERR_NETWORK;
722 case SSL_ERROR_WANT_WRITE:
723 tls.last_writelen = len;
724 // starting write watcher and timer is done in write_clear via
728 if (LOG_ENABLED(INFO)) {
729 LOG(INFO) << "SSL_write: "
730 << ERR_error_string(ERR_get_error(), nullptr);
732 return SHRPX_ERR_NETWORK;
734 if (LOG_ENABLED(INFO)) {
735 LOG(INFO) << "SSL_write: SSL_get_error returned " << err;
737 return SHRPX_ERR_NETWORK;
741 update_tls_warmup_writelen(rv);
746 ssize_t Connection::read_tls(void *data, size_t len) {
749 #if OPENSSL_1_1_1_API
750 if (tls.earlybuf.rleft()) {
751 return tls.earlybuf.remove(data, len);
753 #endif // OPENSSL_1_1_1_API
755 // SSL_read requires the same arguments (buf pointer and its
756 // length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
757 // rlimit_.avail() or rlimit_.avail() may return different length
758 // than the length previously passed to SSL_read, which violates
759 // OpenSSL assumption. To avoid this, we keep last legnth passed
760 // to SSL_read to tls_last_readlen_ if SSL_read indicated I/O
762 if (tls.last_readlen == 0) {
763 len = std::min(len, rlimit.avail());
768 len = tls.last_readlen;
769 tls.last_readlen = 0;
772 #if OPENSSL_1_1_1_API
773 if (!tls.early_data_finish) {
774 // TLSv1.3 handshake is still going on.
776 auto rv = SSL_read_early_data(tls.ssl, data, len, &nread);
777 if (rv == SSL_READ_EARLY_DATA_ERROR) {
778 auto err = SSL_get_error(tls.ssl, rv);
780 case SSL_ERROR_WANT_READ:
781 tls.last_readlen = len;
784 if (LOG_ENABLED(INFO)) {
785 LOG(INFO) << "SSL_read: "
786 << ERR_error_string(ERR_get_error(), nullptr);
788 return SHRPX_ERR_NETWORK;
790 if (LOG_ENABLED(INFO)) {
791 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
793 return SHRPX_ERR_NETWORK;
797 if (LOG_ENABLED(INFO)) {
798 LOG(INFO) << "tls: read early data " << nread << " bytes";
801 if (rv == SSL_READ_EARLY_DATA_FINISH) {
802 if (LOG_ENABLED(INFO)) {
803 LOG(INFO) << "tls: read all early data";
805 tls.early_data_finish = true;
806 // We may have stopped write watcher in write_tls.
811 #endif // OPENSSL_1_1_1_API
813 auto rv = SSL_read(tls.ssl, data, len);
816 auto err = SSL_get_error(tls.ssl, rv);
818 case SSL_ERROR_WANT_READ:
819 tls.last_readlen = len;
821 case SSL_ERROR_WANT_WRITE:
822 if (LOG_ENABLED(INFO)) {
823 LOG(INFO) << "Close connection due to TLS renegotiation";
825 return SHRPX_ERR_NETWORK;
826 case SSL_ERROR_ZERO_RETURN:
827 return SHRPX_ERR_EOF;
829 if (LOG_ENABLED(INFO)) {
830 LOG(INFO) << "SSL_read: " << ERR_error_string(ERR_get_error(), nullptr);
832 return SHRPX_ERR_NETWORK;
834 if (LOG_ENABLED(INFO)) {
835 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
837 return SHRPX_ERR_NETWORK;
844 ssize_t Connection::write_clear(const void *data, size_t len) {
845 len = std::min(len, wlimit.avail());
851 while ((nwrite = write(fd, data, len)) == -1 && errno == EINTR)
854 if (errno == EAGAIN || errno == EWOULDBLOCK) {
856 ev_timer_again(loop, &wt);
859 return SHRPX_ERR_NETWORK;
862 wlimit.drain(nwrite);
864 if (ev_is_active(&wt)) {
865 ev_timer_again(loop, &wt);
871 ssize_t Connection::writev_clear(struct iovec *iov, int iovcnt) {
872 iovcnt = limit_iovec(iov, iovcnt, wlimit.avail());
878 while ((nwrite = writev(fd, iov, iovcnt)) == -1 && errno == EINTR)
881 if (errno == EAGAIN || errno == EWOULDBLOCK) {
883 ev_timer_again(loop, &wt);
886 return SHRPX_ERR_NETWORK;
889 wlimit.drain(nwrite);
891 if (ev_is_active(&wt)) {
892 ev_timer_again(loop, &wt);
898 ssize_t Connection::read_clear(void *data, size_t len) {
899 len = std::min(len, rlimit.avail());
905 while ((nread = read(fd, data, len)) == -1 && errno == EINTR)
908 if (errno == EAGAIN || errno == EWOULDBLOCK) {
911 return SHRPX_ERR_NETWORK;
915 return SHRPX_ERR_EOF;
923 void Connection::handle_tls_pending_read() {
924 if (!ev_is_active(&rev)) {
927 rlimit.handle_tls_pending_read();
930 int Connection::get_tcp_hint(TCPHint *hint) const {
931 #if defined(TCP_INFO) && defined(TCP_NOTSENT_LOWAT)
932 struct tcp_info tcp_info;
933 socklen_t tcp_info_len = sizeof(tcp_info);
936 rv = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcp_info, &tcp_info_len);
942 auto avail_packets = tcp_info.tcpi_snd_cwnd > tcp_info.tcpi_unacked
943 ? tcp_info.tcpi_snd_cwnd - tcp_info.tcpi_unacked
946 // http://www.slideshare.net/kazuho/programming-tcp-for-responsiveness
948 // TODO 29 (5 (header) + 8 (explicit nonce) + 16 (tag)) is TLS
949 // overhead for AES-GCM. For CHACHA20_POLY1305, it is 21 since it
950 // does not need 8 bytes explicit nonce.
952 // For TLSv1.3, AES-GCM and CHACHA20_POLY1305 overhead are now 22
953 // bytes (5 (header) + 1 (ContentType) + 16 (tag)).
955 # ifdef TLS1_3_VERSION
956 if (SSL_version(tls.ssl) == TLS1_3_VERSION) {
959 # endif // TLS1_3_VERSION
965 (avail_packets + 2) * (tcp_info.tcpi_snd_mss - tls_overhead);
966 if (writable_size > 16_k) {
967 writable_size = writable_size & ~(16_k - 1);
969 if (writable_size < 536) {
970 LOG(INFO) << "writable_size is too small: " << writable_size;
972 // TODO is this required?
973 writable_size = std::max(writable_size, static_cast<size_t>(536 * 2));
976 // if (LOG_ENABLED(INFO)) {
977 // LOG(INFO) << "snd_cwnd=" << tcp_info.tcpi_snd_cwnd
978 // << ", unacked=" << tcp_info.tcpi_unacked
979 // << ", snd_mss=" << tcp_info.tcpi_snd_mss
980 // << ", rtt=" << tcp_info.tcpi_rtt << "us"
981 // << ", rcv_space=" << tcp_info.tcpi_rcv_space
982 // << ", writable=" << writable_size;
985 hint->write_buffer_size = writable_size;
986 // TODO tcpi_rcv_space is considered as rwin, is that correct?
987 hint->rwin = tcp_info.tcpi_rcv_space;
990 #else // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
992 #endif // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
995 void Connection::again_rt(ev_tstamp t) {
998 ev_timer_again(loop, &rt);
999 last_read = ev_now(loop);
1002 void Connection::again_rt() {
1003 rt.repeat = read_timeout;
1004 ev_timer_again(loop, &rt);
1005 last_read = ev_now(loop);
1008 bool Connection::expired_rt() {
1009 auto delta = read_timeout - (ev_now(loop) - last_read);
1014 ev_timer_again(loop, &rt);
1018 } // namespace shrpx