1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2016 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_LIVE_CHECK_H 26 #define SHRPX_LIVE_CHECK_H 27 28 #include "shrpx.h" 29 30 #include <functional> 31 #include <random> 32 33 #include "ssl_compat.h" 34 35 #ifdef NGHTTP2_OPENSSL_IS_WOLFSSL 36 # include <wolfssl/options.h> 37 # include <wolfssl/openssl/ssl.h> 38 #else // !NGHTTP2_OPENSSL_IS_WOLFSSL 39 # include <openssl/ssl.h> 40 #endif // !NGHTTP2_OPENSSL_IS_WOLFSSL 41 42 #include <ev.h> 43 44 #include <nghttp2/nghttp2.h> 45 46 #include "shrpx_connection.h" 47 48 namespace shrpx { 49 50 class Worker; 51 struct DownstreamAddr; 52 struct DNSQuery; 53 54 class LiveCheck { 55 public: 56 LiveCheck(struct ev_loop *loop, SSL_CTX *ssl_ctx, Worker *worker, 57 DownstreamAddr *addr, std::mt19937 &gen); 58 ~LiveCheck(); 59 60 void disconnect(); 61 62 void on_success(); 63 void on_failure(); 64 65 int initiate_connection(); 66 67 // Schedules next connection attempt 68 void schedule(); 69 70 // Low level I/O operation callback; they are called from do_read() 71 // or do_write(). 72 int noop(); 73 int connected(); 74 int tls_handshake(); 75 int read_tls(); 76 int write_tls(); 77 int read_clear(); 78 int write_clear(); 79 80 int do_read(); 81 int do_write(); 82 83 // These functions are used to feed / extract data to 84 // nghttp2_session object. 85 int on_read(const uint8_t *data, size_t len); 86 int on_write(); 87 88 // Call this function when HTTP/2 connection was established. We 89 // don't call this function for HTTP/1 at the moment. 90 int connection_made(); 91 92 void start_settings_timer(); 93 void stop_settings_timer(); 94 95 // Call this function when SETTINGS ACK was received from server. 96 void settings_ack_received(); 97 98 void signal_write(); 99 100 private: 101 Connection conn_; 102 DefaultMemchunks wb_; 103 std::mt19937 &gen_; 104 ev_timer backoff_timer_; 105 ev_timer settings_timer_; 106 std::function<int(LiveCheck &)> read_, write_; 107 Worker *worker_; 108 // nullptr if no TLS is configured 109 SSL_CTX *ssl_ctx_; 110 // Address of remote endpoint 111 DownstreamAddr *addr_; 112 nghttp2_session *session_; 113 // Actual remote address used to contact backend. This is initially 114 // nullptr, and may point to either &addr_->addr, or 115 // resolved_addr_.get(). 116 const Address *raddr_; 117 // Resolved IP address if dns parameter is used 118 std::unique_ptr<Address> resolved_addr_; 119 std::unique_ptr<DNSQuery> dns_query_; 120 // The number of successful connect attempt in a row. 121 size_t success_count_; 122 // The number of unsuccessful connect attempt in a row. 123 size_t fail_count_; 124 // true when SETTINGS ACK has been received from server. 125 bool settings_ack_received_; 126 // true when GOAWAY has been queued. 127 bool session_closing_; 128 }; 129 130 } // namespace shrpx 131 132 #endif // SHRPX_LIVE_CHECK_H 133