1 // Copyright (c) 2012 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 QUICHE_QUIC_TOOLS_QUIC_CLIENT_DEFAULT_NETWORK_HELPER_H_ 6 #define QUICHE_QUIC_TOOLS_QUIC_CLIENT_DEFAULT_NETWORK_HELPER_H_ 7 8 #include <cstdint> 9 #include <memory> 10 #include <string> 11 12 #include "absl/types/optional.h" 13 #include "quiche/quic/core/io/quic_event_loop.h" 14 #include "quiche/quic/core/quic_packet_reader.h" 15 #include "quiche/quic/core/quic_udp_socket.h" 16 #include "quiche/quic/tools/quic_client_base.h" 17 #include "quiche/common/quiche_linked_hash_map.h" 18 19 namespace quic { 20 21 namespace test { 22 class QuicClientPeer; 23 } // namespace test 24 25 // An implementation of the QuicClientBase::NetworkHelper interface that is 26 // based on the QuicEventLoop API. 27 class QuicClientDefaultNetworkHelper : public QuicClientBase::NetworkHelper, 28 public QuicSocketEventListener, 29 public ProcessPacketInterface { 30 public: 31 QuicClientDefaultNetworkHelper(QuicEventLoop* event_loop, 32 QuicClientBase* client); 33 QuicClientDefaultNetworkHelper(const QuicClientDefaultNetworkHelper&) = 34 delete; 35 QuicClientDefaultNetworkHelper& operator=( 36 const QuicClientDefaultNetworkHelper&) = delete; 37 38 ~QuicClientDefaultNetworkHelper() override; 39 40 // From QuicSocketEventListener. 41 void OnSocketEvent(QuicEventLoop* event_loop, QuicUdpSocketFd fd, 42 QuicSocketEventMask events) override; 43 44 // From ProcessPacketInterface. This will be called for each received 45 // packet. 46 void ProcessPacket(const QuicSocketAddress& self_address, 47 const QuicSocketAddress& peer_address, 48 const QuicReceivedPacket& packet) override; 49 50 // From NetworkHelper. 51 void RunEventLoop() override; 52 bool CreateUDPSocketAndBind(QuicSocketAddress server_address, 53 QuicIpAddress bind_to_address, 54 int bind_to_port) override; 55 void CleanUpAllUDPSockets() override; 56 QuicSocketAddress GetLatestClientAddress() const override; 57 QuicPacketWriter* CreateQuicPacketWriter() override; 58 59 // Accessors provided for convenience, not part of any interface. event_loop()60 QuicEventLoop* event_loop() { return event_loop_; } fd_address_map()61 const quiche::QuicheLinkedHashMap<int, QuicSocketAddress>& fd_address_map() 62 const { 63 return fd_address_map_; 64 } 65 66 // If the client has at least one UDP socket, return the latest created one. 67 // Otherwise, return -1. 68 int GetLatestFD() const; 69 70 // Create socket for connection to |server_address| with default socket 71 // options. 72 // Return fd index. 73 virtual int CreateUDPSocket(QuicSocketAddress server_address, 74 bool* overflow_supported); 75 client()76 QuicClientBase* client() { return client_; } 77 set_max_reads_per_event_loop(int num_reads)78 void set_max_reads_per_event_loop(int num_reads) { 79 max_reads_per_event_loop_ = num_reads; 80 } 81 // If |fd| is an open UDP socket, unregister and close it. Otherwise, do 82 // nothing. 83 void CleanUpUDPSocket(int fd); 84 85 // Used for testing. 86 void SetClientPort(int port); 87 88 // Indicates that some of the FDs owned by the network helper may be 89 // unregistered by the external code by manually calling 90 // event_loop()->UnregisterSocket() (this is useful for certain scenarios 91 // where an external event loop is used). AllowFdsToBeUnregisteredExternally()92 void AllowFdsToBeUnregisteredExternally() { 93 fds_unregistered_externally_ = true; 94 } 95 96 // Bind a socket to a specific network interface. 97 bool BindInterfaceNameIfNeeded(int fd); 98 99 // Actually clean up |fd|. 100 virtual void CleanUpUDPSocketImpl(int fd); 101 102 private: 103 // Listens for events on the client socket. 104 QuicEventLoop* event_loop_; 105 106 // Map mapping created UDP sockets to their addresses. By using linked hash 107 // map, the order of socket creation can be recorded. 108 quiche::QuicheLinkedHashMap<int, QuicSocketAddress> fd_address_map_; 109 110 // If overflow_supported_ is true, this will be the number of packets dropped 111 // during the lifetime of the server. 112 QuicPacketCount packets_dropped_; 113 114 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped 115 // because the socket would otherwise overflow. 116 bool overflow_supported_; 117 118 // Point to a QuicPacketReader object on the heap. The reader allocates more 119 // space than allowed on the stack. 120 std::unique_ptr<QuicPacketReader> packet_reader_; 121 122 QuicClientBase* client_; 123 124 int max_reads_per_event_loop_; 125 126 // If true, some of the FDs owned by the network helper may be unregistered by 127 // the external code. 128 bool fds_unregistered_externally_ = false; 129 }; 130 131 } // namespace quic 132 133 #endif // QUICHE_QUIC_TOOLS_QUIC_CLIENT_DEFAULT_NETWORK_HELPER_H_ 134