1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #ifndef SHRPX_CONNECTION_H
26 #define SHRPX_CONNECTION_H
27 
28 #include "shrpx_config.h"
29 
30 #include <sys/uio.h>
31 
32 #include <ev.h>
33 
34 #include <openssl/ssl.h>
35 
36 #include "shrpx_rate_limit.h"
37 #include "shrpx_error.h"
38 #include "memchunk.h"
39 
40 namespace shrpx {
41 
42 struct MemcachedRequest;
43 
44 namespace tls {
45 struct TLSSessionCache;
46 } // namespace tls
47 
48 enum class TLSHandshakeState {
49   NORMAL,
50   WAIT_FOR_SESSION_CACHE,
51   GOT_SESSION_CACHE,
52   CANCEL_SESSION_CACHE,
53   WRITE_STARTED,
54 };
55 
56 struct TLSConnection {
57   DefaultMemchunks wbuf;
58   DefaultPeekMemchunks rbuf;
59   // Stores TLSv1.3 early data.
60   DefaultMemchunks earlybuf;
61   SSL *ssl;
62   SSL_SESSION *cached_session;
63   MemcachedRequest *cached_session_lookup_req;
64   tls::TLSSessionCache *client_session_cache;
65   ev_tstamp last_write_idle;
66   size_t warmup_writelen;
67   // length passed to SSL_write and SSL_read last time.  This is
68   // required since these functions require the exact same parameters
69   // on non-blocking I/O.
70   size_t last_writelen, last_readlen;
71   TLSHandshakeState handshake_state;
72   bool initial_handshake_done;
73   bool reneg_started;
74   // true if ssl is prepared to do handshake as server.
75   bool server_handshake;
76   // true if ssl is initialized as server, and client requested
77   // signed_certificate_timestamp extension.
78   bool sct_requested;
79   // true if TLSv1.3 early data has been completely received.  Since
80   // SSL_read_early_data acts like SSL_do_handshake, this field may be
81   // true even if the negotiated TLS version is TLSv1.2 or earlier.
82   // This value is also true if this is client side connection for
83   // convenience.
84   bool early_data_finish;
85 };
86 
87 struct TCPHint {
88   size_t write_buffer_size;
89   uint32_t rwin;
90 };
91 
92 template <typename T> using EVCb = void (*)(struct ev_loop *, T *, int);
93 
94 using IOCb = EVCb<ev_io>;
95 using TimerCb = EVCb<ev_timer>;
96 
97 struct Connection {
98   Connection(struct ev_loop *loop, int fd, SSL *ssl, MemchunkPool *mcpool,
99              ev_tstamp write_timeout, ev_tstamp read_timeout,
100              const RateLimitConfig &write_limit,
101              const RateLimitConfig &read_limit, IOCb writecb, IOCb readcb,
102              TimerCb timeoutcb, void *data, size_t tls_dyn_rec_warmup_threshold,
103              ev_tstamp tls_dyn_rec_idle_timeout, Proto proto);
104   ~Connection();
105 
106   void disconnect();
107 
108   void prepare_client_handshake();
109   void prepare_server_handshake();
110 
111   int tls_handshake();
112   int write_tls_pending_handshake();
113 
114   int check_http2_requirement();
115 
116   // All write_* and writev_clear functions return number of bytes
117   // written.  If nothing cannot be written (e.g., there is no
118   // allowance in RateLimit or underlying connection blocks), return
119   // 0.  SHRPX_ERR_NETWORK is returned in case of error.
120   //
121   // All read_* functions return number of bytes read.  If nothing
122   // cannot be read (e.g., there is no allowance in Ratelimit or
123   // underlying connection blocks), return 0.  SHRPX_ERR_EOF is
124   // returned in case of EOF and no data was read.  Otherwise
125   // SHRPX_ERR_NETWORK is return in case of error.
126   ssize_t write_tls(const void *data, size_t len);
127   ssize_t read_tls(void *data, size_t len);
128 
129   size_t get_tls_write_limit();
130   // Updates the number of bytes written in warm up period.
131   void update_tls_warmup_writelen(size_t n);
132   // Tells there is no immediate write now.  This triggers timer to
133   // determine fallback to short record size mode.
134   void start_tls_write_idle();
135 
136   ssize_t write_clear(const void *data, size_t len);
137   ssize_t writev_clear(struct iovec *iov, int iovcnt);
138   ssize_t read_clear(void *data, size_t len);
139 
140   void handle_tls_pending_read();
141 
142   void set_ssl(SSL *ssl);
143 
144   int get_tcp_hint(TCPHint *hint) const;
145 
146   // These functions are provided for read timer which is frequently
147   // restarted.  We do a trick to make a bit more efficient than just
148   // calling ev_timer_again().
149 
150   // Restarts read timer with timeout value |t|.
151   void again_rt(ev_tstamp t);
152   // Restarts read timer without chainging timeout.
153   void again_rt();
154   // Returns true if read timer expired.
155   bool expired_rt();
156 
157   TLSConnection tls;
158   ev_io wev;
159   ev_io rev;
160   ev_timer wt;
161   ev_timer rt;
162   RateLimit wlimit;
163   RateLimit rlimit;
164   struct ev_loop *loop;
165   void *data;
166   int fd;
167   size_t tls_dyn_rec_warmup_threshold;
168   ev_tstamp tls_dyn_rec_idle_timeout;
169   // Application protocol used over the connection.  This field is not
170   // used in this object at the moment.  The rest of the program may
171   // use this value when it is useful.
172   Proto proto;
173   // The point of time when last read is observed.  Note: since we use
174   // |rt| as idle timer, the activity is not limited to read.
175   ev_tstamp last_read;
176   // Timeout for read timer |rt|.
177   ev_tstamp read_timeout;
178 };
179 
180 // Creates BIO_method shared by all SSL objects.
181 BIO_METHOD *create_bio_method();
182 
183 } // namespace shrpx
184 
185 #endif // SHRPX_CONNECTION_H
186