12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2016 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef SHRPX_LIVE_CHECK_H
262c593315Sopenharmony_ci#define SHRPX_LIVE_CHECK_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "shrpx.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <functional>
312c593315Sopenharmony_ci#include <random>
322c593315Sopenharmony_ci
332c593315Sopenharmony_ci#include <openssl/ssl.h>
342c593315Sopenharmony_ci
352c593315Sopenharmony_ci#include <ev.h>
362c593315Sopenharmony_ci
372c593315Sopenharmony_ci#include <nghttp2/nghttp2.h>
382c593315Sopenharmony_ci
392c593315Sopenharmony_ci#include "shrpx_connection.h"
402c593315Sopenharmony_ci
412c593315Sopenharmony_cinamespace shrpx {
422c593315Sopenharmony_ci
432c593315Sopenharmony_ciclass Worker;
442c593315Sopenharmony_cistruct DownstreamAddr;
452c593315Sopenharmony_cistruct DNSQuery;
462c593315Sopenharmony_ci
472c593315Sopenharmony_ciclass LiveCheck {
482c593315Sopenharmony_cipublic:
492c593315Sopenharmony_ci  LiveCheck(struct ev_loop *loop, SSL_CTX *ssl_ctx, Worker *worker,
502c593315Sopenharmony_ci            DownstreamAddr *addr, std::mt19937 &gen);
512c593315Sopenharmony_ci  ~LiveCheck();
522c593315Sopenharmony_ci
532c593315Sopenharmony_ci  void disconnect();
542c593315Sopenharmony_ci
552c593315Sopenharmony_ci  void on_success();
562c593315Sopenharmony_ci  void on_failure();
572c593315Sopenharmony_ci
582c593315Sopenharmony_ci  int initiate_connection();
592c593315Sopenharmony_ci
602c593315Sopenharmony_ci  // Schedules next connection attempt
612c593315Sopenharmony_ci  void schedule();
622c593315Sopenharmony_ci
632c593315Sopenharmony_ci  // Low level I/O operation callback; they are called from do_read()
642c593315Sopenharmony_ci  // or do_write().
652c593315Sopenharmony_ci  int noop();
662c593315Sopenharmony_ci  int connected();
672c593315Sopenharmony_ci  int tls_handshake();
682c593315Sopenharmony_ci  int read_tls();
692c593315Sopenharmony_ci  int write_tls();
702c593315Sopenharmony_ci  int read_clear();
712c593315Sopenharmony_ci  int write_clear();
722c593315Sopenharmony_ci
732c593315Sopenharmony_ci  int do_read();
742c593315Sopenharmony_ci  int do_write();
752c593315Sopenharmony_ci
762c593315Sopenharmony_ci  // These functions are used to feed / extract data to
772c593315Sopenharmony_ci  // nghttp2_session object.
782c593315Sopenharmony_ci  int on_read(const uint8_t *data, size_t len);
792c593315Sopenharmony_ci  int on_write();
802c593315Sopenharmony_ci
812c593315Sopenharmony_ci  // Call this function when HTTP/2 connection was established.  We
822c593315Sopenharmony_ci  // don't call this function for HTTP/1 at the moment.
832c593315Sopenharmony_ci  int connection_made();
842c593315Sopenharmony_ci
852c593315Sopenharmony_ci  void start_settings_timer();
862c593315Sopenharmony_ci  void stop_settings_timer();
872c593315Sopenharmony_ci
882c593315Sopenharmony_ci  // Call this function when SETTINGS ACK was received from server.
892c593315Sopenharmony_ci  void settings_ack_received();
902c593315Sopenharmony_ci
912c593315Sopenharmony_ci  void signal_write();
922c593315Sopenharmony_ci
932c593315Sopenharmony_ciprivate:
942c593315Sopenharmony_ci  Connection conn_;
952c593315Sopenharmony_ci  DefaultMemchunks wb_;
962c593315Sopenharmony_ci  std::mt19937 &gen_;
972c593315Sopenharmony_ci  ev_timer backoff_timer_;
982c593315Sopenharmony_ci  ev_timer settings_timer_;
992c593315Sopenharmony_ci  std::function<int(LiveCheck &)> read_, write_;
1002c593315Sopenharmony_ci  Worker *worker_;
1012c593315Sopenharmony_ci  // nullptr if no TLS is configured
1022c593315Sopenharmony_ci  SSL_CTX *ssl_ctx_;
1032c593315Sopenharmony_ci  // Address of remote endpoint
1042c593315Sopenharmony_ci  DownstreamAddr *addr_;
1052c593315Sopenharmony_ci  nghttp2_session *session_;
1062c593315Sopenharmony_ci  // Actual remote address used to contact backend.  This is initially
1072c593315Sopenharmony_ci  // nullptr, and may point to either &addr_->addr, or
1082c593315Sopenharmony_ci  // resolved_addr_.get().
1092c593315Sopenharmony_ci  const Address *raddr_;
1102c593315Sopenharmony_ci  // Resolved IP address if dns parameter is used
1112c593315Sopenharmony_ci  std::unique_ptr<Address> resolved_addr_;
1122c593315Sopenharmony_ci  std::unique_ptr<DNSQuery> dns_query_;
1132c593315Sopenharmony_ci  // The number of successful connect attempt in a row.
1142c593315Sopenharmony_ci  size_t success_count_;
1152c593315Sopenharmony_ci  // The number of unsuccessful connect attempt in a row.
1162c593315Sopenharmony_ci  size_t fail_count_;
1172c593315Sopenharmony_ci  // true when SETTINGS ACK has been received from server.
1182c593315Sopenharmony_ci  bool settings_ack_received_;
1192c593315Sopenharmony_ci  // true when GOAWAY has been queued.
1202c593315Sopenharmony_ci  bool session_closing_;
1212c593315Sopenharmony_ci};
1222c593315Sopenharmony_ci
1232c593315Sopenharmony_ci} // namespace shrpx
1242c593315Sopenharmony_ci
1252c593315Sopenharmony_ci#endif // SHRPX_LIVE_CHECK_H
126