2015-02-04 13:15:58 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef SHRPX_CONNECTION_H
|
|
|
|
#define SHRPX_CONNECTION_H
|
|
|
|
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <ev.h>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
|
|
|
#include "shrpx_rate_limit.h"
|
|
|
|
#include "shrpx_error.h"
|
2015-08-12 17:04:41 +02:00
|
|
|
#include "memchunk.h"
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
struct MemcachedRequest;
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
namespace tls {
|
2017-03-28 14:06:28 +02:00
|
|
|
struct TLSSessionCache;
|
2017-04-01 08:07:32 +02:00
|
|
|
} // namespace tls
|
2017-03-28 14:06:28 +02:00
|
|
|
|
2018-10-17 01:52:27 +02:00
|
|
|
enum class TLSHandshakeState {
|
|
|
|
NORMAL,
|
|
|
|
WAIT_FOR_SESSION_CACHE,
|
|
|
|
GOT_SESSION_CACHE,
|
|
|
|
CANCEL_SESSION_CACHE,
|
|
|
|
WRITE_STARTED,
|
2015-07-25 15:22:17 +02:00
|
|
|
};
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
struct TLSConnection {
|
2015-08-12 17:04:41 +02:00
|
|
|
DefaultMemchunks wbuf;
|
|
|
|
DefaultPeekMemchunks rbuf;
|
2017-04-01 08:24:12 +02:00
|
|
|
// Stores TLSv1.3 early data.
|
|
|
|
DefaultMemchunks earlybuf;
|
2015-02-04 13:15:58 +01:00
|
|
|
SSL *ssl;
|
2015-07-25 15:22:17 +02:00
|
|
|
SSL_SESSION *cached_session;
|
|
|
|
MemcachedRequest *cached_session_lookup_req;
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::TLSSessionCache *client_session_cache;
|
2015-07-29 13:57:11 +02:00
|
|
|
ev_tstamp last_write_idle;
|
2015-02-04 13:15:58 +01:00
|
|
|
size_t warmup_writelen;
|
2015-05-15 15:20:15 +02:00
|
|
|
// length passed to SSL_write and SSL_read last time. This is
|
|
|
|
// required since these functions require the exact same parameters
|
|
|
|
// on non-blocking I/O.
|
|
|
|
size_t last_writelen, last_readlen;
|
2018-10-17 01:52:27 +02:00
|
|
|
TLSHandshakeState handshake_state;
|
2015-02-04 13:15:58 +01:00
|
|
|
bool initial_handshake_done;
|
|
|
|
bool reneg_started;
|
2017-01-08 14:33:19 +01:00
|
|
|
// true if ssl is prepared to do handshake as server.
|
|
|
|
bool server_handshake;
|
2017-04-09 06:40:38 +02:00
|
|
|
// true if ssl is initialized as server, and client requested
|
|
|
|
// signed_certificate_timestamp extension.
|
|
|
|
bool sct_requested;
|
2017-04-01 08:24:12 +02:00
|
|
|
// true if TLSv1.3 early data has been completely received. Since
|
|
|
|
// SSL_read_early_data acts like SSL_do_handshake, this field may be
|
|
|
|
// true even if the negotiated TLS version is TLSv1.2 or earlier.
|
|
|
|
// This value is also true if this is client side connection for
|
|
|
|
// convenience.
|
|
|
|
bool early_data_finish;
|
2015-02-04 13:15:58 +01:00
|
|
|
};
|
|
|
|
|
2016-09-08 15:49:36 +02:00
|
|
|
struct TCPHint {
|
|
|
|
size_t write_buffer_size;
|
|
|
|
uint32_t rwin;
|
|
|
|
};
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
template <typename T> using EVCb = void (*)(struct ev_loop *, T *, int);
|
|
|
|
|
|
|
|
using IOCb = EVCb<ev_io>;
|
|
|
|
using TimerCb = EVCb<ev_timer>;
|
|
|
|
|
|
|
|
struct Connection {
|
2015-08-12 17:04:41 +02:00
|
|
|
Connection(struct ev_loop *loop, int fd, SSL *ssl, MemchunkPool *mcpool,
|
2016-01-19 08:56:12 +01:00
|
|
|
ev_tstamp write_timeout, ev_tstamp read_timeout,
|
|
|
|
const RateLimitConfig &write_limit,
|
|
|
|
const RateLimitConfig &read_limit, IOCb writecb, IOCb readcb,
|
|
|
|
TimerCb timeoutcb, void *data, size_t tls_dyn_rec_warmup_threshold,
|
2018-10-16 15:17:37 +02:00
|
|
|
ev_tstamp tls_dyn_rec_idle_timeout, Proto proto);
|
2015-02-04 13:15:58 +01:00
|
|
|
~Connection();
|
|
|
|
|
|
|
|
void disconnect();
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
void prepare_client_handshake();
|
|
|
|
void prepare_server_handshake();
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
int tls_handshake();
|
2015-08-31 16:30:40 +02:00
|
|
|
int write_tls_pending_handshake();
|
|
|
|
|
|
|
|
int check_http2_requirement();
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
// All write_* and writev_clear functions return number of bytes
|
|
|
|
// written. If nothing cannot be written (e.g., there is no
|
|
|
|
// allowance in RateLimit or underlying connection blocks), return
|
|
|
|
// 0. SHRPX_ERR_NETWORK is returned in case of error.
|
|
|
|
//
|
|
|
|
// All read_* functions return number of bytes read. If nothing
|
|
|
|
// cannot be read (e.g., there is no allowance in Ratelimit or
|
|
|
|
// underlying connection blocks), return 0. SHRPX_ERR_EOF is
|
|
|
|
// returned in case of EOF and no data was read. Otherwise
|
|
|
|
// SHRPX_ERR_NETWORK is return in case of error.
|
|
|
|
ssize_t write_tls(const void *data, size_t len);
|
|
|
|
ssize_t read_tls(void *data, size_t len);
|
|
|
|
|
2015-02-11 15:22:53 +01:00
|
|
|
size_t get_tls_write_limit();
|
2015-02-04 13:15:58 +01:00
|
|
|
// Updates the number of bytes written in warm up period.
|
|
|
|
void update_tls_warmup_writelen(size_t n);
|
2015-07-29 13:57:11 +02:00
|
|
|
// Tells there is no immediate write now. This triggers timer to
|
|
|
|
// determine fallback to short record size mode.
|
|
|
|
void start_tls_write_idle();
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
ssize_t write_clear(const void *data, size_t len);
|
|
|
|
ssize_t writev_clear(struct iovec *iov, int iovcnt);
|
|
|
|
ssize_t read_clear(void *data, size_t len);
|
|
|
|
|
nghttpx: Fix bug that data buffered in SSL object are not read
This is same issue described in https://github.com/h2o/h2o/issues/268.
That is if SSL object has decrypted data buffered inside it, and
application does not read it for some reason (e.g., rate limit), we
have to check the existence of data using SSL_pending. This is
because buffered data inside SSL is not notified by io watcher. It is
obvious, but we totally missed it.
nghttpx code normally reads everything until SSL_read returns error
(want-read). But if rate limit is involved, we stop reading early.
Also in HTTP/1 code, while processing one request, we just read until
buffer is filled up. In these cases, we may suffer from this problem.
This commit fixes this problem, by performing SSL_pending() and if it
has buffered data and read io watcher is enabled, we feed event using
ev_feed_event().
2015-04-06 15:31:36 +02:00
|
|
|
void handle_tls_pending_read();
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
void set_ssl(SSL *ssl);
|
|
|
|
|
2016-09-08 15:49:36 +02:00
|
|
|
int get_tcp_hint(TCPHint *hint) const;
|
|
|
|
|
2016-11-25 16:00:32 +01:00
|
|
|
// These functions are provided for read timer which is frequently
|
|
|
|
// restarted. We do a trick to make a bit more efficient than just
|
|
|
|
// calling ev_timer_again().
|
|
|
|
|
|
|
|
// Restarts read timer with timeout value |t|.
|
|
|
|
void again_rt(ev_tstamp t);
|
|
|
|
// Restarts read timer without chainging timeout.
|
|
|
|
void again_rt();
|
|
|
|
// Returns true if read timer expired.
|
|
|
|
bool expired_rt();
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
TLSConnection tls;
|
|
|
|
ev_io wev;
|
|
|
|
ev_io rev;
|
|
|
|
ev_timer wt;
|
|
|
|
ev_timer rt;
|
|
|
|
RateLimit wlimit;
|
|
|
|
RateLimit rlimit;
|
|
|
|
struct ev_loop *loop;
|
|
|
|
void *data;
|
|
|
|
int fd;
|
2015-10-21 12:22:46 +02:00
|
|
|
size_t tls_dyn_rec_warmup_threshold;
|
|
|
|
ev_tstamp tls_dyn_rec_idle_timeout;
|
2016-02-28 08:56:14 +01:00
|
|
|
// Application protocol used over the connection. This field is not
|
|
|
|
// used in this object at the moment. The rest of the program may
|
|
|
|
// use this value when it is useful.
|
2018-10-16 15:17:37 +02:00
|
|
|
Proto proto;
|
2017-03-22 14:53:46 +01:00
|
|
|
// The point of time when last read is observed. Note: since we use
|
2016-11-25 16:00:32 +01:00
|
|
|
// |rt| as idle timer, the activity is not limited to read.
|
|
|
|
ev_tstamp last_read;
|
|
|
|
// Timeout for read timer |rt|.
|
|
|
|
ev_tstamp read_timeout;
|
2015-02-04 13:15:58 +01:00
|
|
|
};
|
|
|
|
|
2017-03-15 15:36:38 +01:00
|
|
|
// Creates BIO_method shared by all SSL objects.
|
2016-05-07 09:18:58 +02:00
|
|
|
BIO_METHOD *create_bio_method();
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_CONNECTION_H
|