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.
5 // A toy client, which connects to a specified port and sends QUIC
6 // request to that endpoint.
8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_
9 #define NET_TOOLS_QUIC_QUIC_CLIENT_H_
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/quic/crypto/crypto_handshake.h"
18 #include "net/quic/quic_config.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_packet_creator.h"
21 #include "net/tools/epoll_server/epoll_server.h"
22 #include "net/tools/quic/quic_client_session.h"
23 #include "net/tools/quic/quic_spdy_client_stream.h"
32 class QuicEpollConnectionHelper;
38 class QuicClient : public EpollCallbackInterface,
39 public QuicDataStream::Visitor {
41 class ResponseListener {
44 virtual ~ResponseListener() {}
45 virtual void OnCompleteResponse(QuicStreamId id,
46 const BalsaHeaders& response_headers,
47 const string& response_body) = 0;
50 // Create a quic client, which will have events managed by an externally owned
52 QuicClient(IPEndPoint server_address,
53 const QuicServerId& server_id,
54 const QuicVersionVector& supported_versions,
56 EpollServer* epoll_server);
57 QuicClient(IPEndPoint server_address,
58 const QuicServerId& server_id,
59 const QuicVersionVector& supported_versions,
61 const QuicConfig& config,
62 EpollServer* epoll_server);
64 ~QuicClient() override;
66 // Initializes the client to create a connection. Should be called exactly
67 // once before calling StartConnect or Connect. Returns true if the
68 // initialization succeeds, false otherwise.
71 // "Connect" to the QUIC server, including performing synchronous crypto
75 // Start the crypto handshake. This can be done in place of the synchronous
76 // Connect(), but callers are responsible for making sure the crypto handshake
80 // Returns true if the crypto handshake has yet to establish encryption.
81 // Returns false if encryption is active (even if the server hasn't confirmed
82 // the handshake) or if the connection has been closed.
83 bool EncryptionBeingEstablished();
85 // Disconnects from the QUIC server.
88 // Sends a request simple GET for each URL in |args|, and then waits for
90 void SendRequestsAndWaitForResponse(const
91 base::CommandLine::StringVector& args);
93 // Returns a newly created QuicSpdyClientStream, owned by the
95 QuicSpdyClientStream* CreateReliableClientStream();
97 // Wait for events until the stream with the given ID is closed.
98 void WaitForStreamToClose(QuicStreamId id);
100 // Wait for events until the handshake is confirmed.
101 void WaitForCryptoHandshakeConfirmed();
103 // Wait up to 50ms, and handle any events which occur.
104 // Returns true if there are any outstanding requests.
105 bool WaitForEvents();
107 // From EpollCallbackInterface
108 void OnRegistration(EpollServer* eps, int fd, int event_mask) override {}
109 void OnModification(int fd, int event_mask) override {}
110 void OnEvent(int fd, EpollEvent* event) override;
111 // |fd_| can be unregistered without the client being disconnected. This
112 // happens in b3m QuicProber where we unregister |fd_| to feed in events to
113 // the client from the SelectServer.
114 void OnUnregistration(int fd, bool replaced) override {}
115 void OnShutdown(EpollServer* eps, int fd) override {}
117 // QuicDataStream::Visitor
118 void OnClose(QuicDataStream* stream) override;
120 QuicClientSession* session() { return session_.get(); }
122 bool connected() const;
124 void set_bind_to_address(IPAddressNumber address) {
125 bind_to_address_ = address;
128 IPAddressNumber bind_to_address() const { return bind_to_address_; }
130 void set_local_port(int local_port) { local_port_ = local_port; }
132 const IPEndPoint& server_address() const { return server_address_; }
134 const IPEndPoint& client_address() const { return client_address_; }
136 int fd() { return fd_; }
138 const QuicServerId& server_id() const { return server_id_; }
140 // This should only be set before the initial Connect()
141 void set_server_id(const QuicServerId& server_id) {
142 server_id_ = server_id;
145 void SetUserAgentID(const string& user_agent_id) {
146 crypto_config_.set_user_agent_id(user_agent_id);
149 // SetProofVerifier sets the ProofVerifier that will be used to verify the
150 // server's certificate and takes ownership of |verifier|.
151 void SetProofVerifier(ProofVerifier* verifier) {
152 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
153 crypto_config_.SetProofVerifier(verifier);
156 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
157 // server supports channel IDs, to obtain a channel ID for signing a message
158 // proving possession of the channel ID. This object takes ownership of
160 void SetChannelIDSource(ChannelIDSource* source) {
161 crypto_config_.SetChannelIDSource(source);
164 void SetSupportedVersions(const QuicVersionVector& versions) {
165 supported_versions_ = versions;
168 // Takes ownership of the listener.
169 void set_response_listener(ResponseListener* listener) {
170 response_listener_.reset(listener);
173 QuicConfig* config() { return &config_; }
176 virtual QuicConnectionId GenerateConnectionId();
177 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
178 virtual QuicPacketWriter* CreateQuicPacketWriter();
180 virtual int ReadPacket(char* buffer,
182 IPEndPoint* server_address,
183 IPAddressNumber* client_ip);
185 EpollServer* epoll_server() { return epoll_server_; }
188 friend class net::tools::test::QuicClientPeer;
190 // A packet writer factory that always returns the same writer
191 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory {
193 DummyPacketWriterFactory(QuicPacketWriter* writer);
194 ~DummyPacketWriterFactory() override;
196 QuicPacketWriter* Create(QuicConnection* connection) const override;
199 QuicPacketWriter* writer_;
202 // Used during initialization: creates the UDP socket FD, sets socket options,
203 // and binds the socket to our address.
204 bool CreateUDPSocket();
206 // Read a UDP packet and hand it to the framer.
207 bool ReadAndProcessPacket();
209 // Address of the server.
210 const IPEndPoint server_address_;
212 // |server_id_| is a tuple (hostname, port, is_https) of the server.
213 QuicServerId server_id_;
215 // config_ and crypto_config_ contain configuration and cached state about
218 QuicCryptoClientConfig crypto_config_;
220 // Address of the client if the client is connected to the server.
221 IPEndPoint client_address_;
223 // If initialized, the address to bind to.
224 IPAddressNumber bind_to_address_;
225 // Local port to bind to. Initialize to 0.
228 // Writer used to actually send packets to the wire. Needs to outlive
230 scoped_ptr<QuicPacketWriter> writer_;
232 // Session which manages streams.
233 scoped_ptr<QuicClientSession> session_;
234 // Listens for events on the client socket.
235 EpollServer* epoll_server_;
239 // Helper to be used by created connections.
240 scoped_ptr<QuicEpollConnectionHelper> helper_;
242 // Listens for full responses.
243 scoped_ptr<ResponseListener> response_listener_;
245 // Tracks if the client is initialized to connect.
248 // If overflow_supported_ is true, this will be the number of packets dropped
249 // during the lifetime of the server. This may overflow if enough packets
251 uint32 packets_dropped_;
253 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
254 // because the socket would otherwise overflow.
255 bool overflow_supported_;
257 // This vector contains QUIC versions which we currently support.
258 // This should be ordered such that the highest supported version is the first
259 // element, with subsequent elements in descending order (versions can be
260 // skipped as necessary). We will always pick supported_versions_[0] as the
261 // initial version to use.
262 QuicVersionVector supported_versions_;
264 // If true, then the contents of each response will be printed to stdout
265 // when the stream is closed (in OnClose).
266 bool print_response_;
268 DISALLOW_COPY_AND_ASSIGN(QuicClient);
274 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_