1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef OSP_IMPL_QUIC_QUIC_SERVER_H_ 6 #define OSP_IMPL_QUIC_QUIC_SERVER_H_ 7 8 #include <cstdint> 9 #include <map> 10 #include <memory> 11 12 #include "osp/impl/quic/quic_connection_factory.h" 13 #include "osp/impl/quic/quic_service_common.h" 14 #include "osp/public/protocol_connection_server.h" 15 #include "platform/api/task_runner.h" 16 #include "platform/api/time.h" 17 #include "platform/base/ip_address.h" 18 #include "util/alarm.h" 19 20 namespace openscreen { 21 namespace osp { 22 23 // This class is the default implementation of ProtocolConnectionServer for the 24 // library. It manages connections to other endpoints as well as the lifetime 25 // of each incoming and outgoing stream. It works in conjunction with a 26 // QuicConnectionFactory implementation and MessageDemuxer. 27 // QuicConnectionFactory provides the ability to make a new QUIC 28 // connection from packets received on its server sockets. Incoming data is 29 // given to the QuicServer by the underlying QUIC implementation (through 30 // QuicConnectionFactory) and this is in turn handed to MessageDemuxer for 31 // routing CBOR messages. 32 class QuicServer final : public ProtocolConnectionServer, 33 public QuicConnectionFactory::ServerDelegate, 34 public ServiceConnectionDelegate::ServiceDelegate { 35 public: 36 QuicServer(const ServerConfig& config, 37 MessageDemuxer* demuxer, 38 std::unique_ptr<QuicConnectionFactory> connection_factory, 39 ProtocolConnectionServer::Observer* observer, 40 ClockNowFunctionPtr now_function, 41 TaskRunner* task_runner); 42 ~QuicServer() override; 43 44 // ProtocolConnectionServer overrides. 45 bool Start() override; 46 bool Stop() override; 47 bool Suspend() override; 48 bool Resume() override; 49 std::unique_ptr<ProtocolConnection> CreateProtocolConnection( 50 uint64_t endpoint_id) override; 51 52 // QuicProtocolConnection::Owner overrides. 53 void OnConnectionDestroyed(QuicProtocolConnection* connection) override; 54 55 // ServiceConnectionDelegate::ServiceDelegate overrides. 56 uint64_t OnCryptoHandshakeComplete(ServiceConnectionDelegate* delegate, 57 uint64_t connection_id) override; 58 void OnIncomingStream( 59 std::unique_ptr<QuicProtocolConnection> connection) override; 60 void OnConnectionClosed(uint64_t endpoint_id, 61 uint64_t connection_id) override; 62 void OnDataReceived(uint64_t endpoint_id, 63 uint64_t connection_id, 64 const uint8_t* data, 65 size_t data_size) override; 66 67 private: 68 void CloseAllConnections(); 69 70 // QuicConnectionFactory::ServerDelegate overrides. 71 QuicConnection::Delegate* NextConnectionDelegate( 72 const IPEndpoint& source) override; 73 void OnIncomingConnection( 74 std::unique_ptr<QuicConnection> connection) override; 75 76 // Deletes dead QUIC connections then returns the time interval before this 77 // method should be run again. 78 void Cleanup(); 79 80 const std::vector<IPEndpoint> connection_endpoints_; 81 std::unique_ptr<QuicConnectionFactory> connection_factory_; 82 83 std::unique_ptr<ServiceConnectionDelegate> pending_connection_delegate_; 84 85 // Maps an IPEndpoint to a generated endpoint ID. This is used to insulate 86 // callers from post-handshake changes to a connections actual peer endpoint. 87 std::map<IPEndpoint, uint64_t> endpoint_map_; 88 89 // Value that will be used for the next new endpoint in a Connect call. 90 uint64_t next_endpoint_id_ = 0; 91 92 // Maps endpoint addresses to data about connections that haven't successfully 93 // completed the QUIC handshake. 94 std::map<IPEndpoint, ServiceConnectionData> pending_connections_; 95 96 // Maps endpoint IDs to data about connections that have successfully 97 // completed the QUIC handshake. 98 std::map<uint64_t, ServiceConnectionData> connections_; 99 100 // Connections (endpoint IDs) that need to be destroyed, but have to wait for 101 // the next event loop due to the underlying QUIC implementation's way of 102 // referencing them. 103 std::vector<uint64_t> delete_connections_; 104 105 Alarm cleanup_alarm_; 106 }; 107 108 } // namespace osp 109 } // namespace openscreen 110 111 #endif // OSP_IMPL_QUIC_QUIC_SERVER_H_ 112