Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / tools / quic / quic_client.h
blob206a27ec1e878886fe8488535a94249b7aef317a
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 // 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_
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/quic/crypto/crypto_handshake.h"
19 #include "net/quic/quic_config.h"
20 #include "net/quic/quic_framer.h"
21 #include "net/quic/quic_packet_creator.h"
22 #include "net/tools/balsa/balsa_headers.h"
23 #include "net/tools/epoll_server/epoll_server.h"
24 #include "net/tools/quic/quic_client_session.h"
25 #include "net/tools/quic/quic_spdy_client_stream.h"
27 namespace net {
29 class ProofVerifier;
30 class QuicServerId;
32 namespace tools {
34 class QuicEpollConnectionHelper;
36 namespace test {
37 class QuicClientPeer;
38 } // namespace test
40 class QuicClient : public EpollCallbackInterface,
41 public QuicDataStream::Visitor {
42 public:
43 class ResponseListener {
44 public:
45 ResponseListener() {}
46 virtual ~ResponseListener() {}
47 virtual void OnCompleteResponse(QuicStreamId id,
48 const BalsaHeaders& response_headers,
49 const std::string& response_body) = 0;
52 // A packet writer factory that always returns the same writer.
53 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory {
54 public:
55 explicit DummyPacketWriterFactory(QuicPacketWriter* writer);
56 ~DummyPacketWriterFactory() override;
58 QuicPacketWriter* Create(QuicConnection* connection) const override;
60 private:
61 QuicPacketWriter* writer_;
64 // Create a quic client, which will have events managed by an externally owned
65 // EpollServer.
66 QuicClient(IPEndPoint server_address,
67 const QuicServerId& server_id,
68 const QuicVersionVector& supported_versions,
69 EpollServer* epoll_server);
70 QuicClient(IPEndPoint server_address,
71 const QuicServerId& server_id,
72 const QuicVersionVector& supported_versions,
73 const QuicConfig& config,
74 EpollServer* epoll_server);
76 ~QuicClient() override;
78 // Initializes the client to create a connection. Should be called exactly
79 // once before calling StartConnect or Connect. Returns true if the
80 // initialization succeeds, false otherwise.
81 bool Initialize();
83 // "Connect" to the QUIC server, including performing synchronous crypto
84 // handshake.
85 bool Connect();
87 // Start the crypto handshake. This can be done in place of the synchronous
88 // Connect(), but callers are responsible for making sure the crypto handshake
89 // completes.
90 void StartConnect();
92 // Returns true if the crypto handshake has yet to establish encryption.
93 // Returns false if encryption is active (even if the server hasn't confirmed
94 // the handshake) or if the connection has been closed.
95 bool EncryptionBeingEstablished();
97 // Disconnects from the QUIC server.
98 void Disconnect();
100 // Sends an HTTP request and does not wait for response before returning.
101 void SendRequest(const BalsaHeaders& headers,
102 base::StringPiece body,
103 bool fin);
105 // Sends an HTTP request and waits for response before returning.
106 void SendRequestAndWaitForResponse(const BalsaHeaders& headers,
107 base::StringPiece body,
108 bool fin);
110 // Sends a request simple GET for each URL in |args|, and then waits for
111 // each to complete.
112 void SendRequestsAndWaitForResponse(
113 const std::vector<std::string>& url_list);
115 // Returns a newly created QuicSpdyClientStream, owned by the
116 // QuicClient.
117 QuicSpdyClientStream* CreateReliableClientStream();
119 // Wait for events until the stream with the given ID is closed.
120 void WaitForStreamToClose(QuicStreamId id);
122 // Wait for events until the handshake is confirmed.
123 void WaitForCryptoHandshakeConfirmed();
125 // Wait up to 50ms, and handle any events which occur.
126 // Returns true if there are any outstanding requests.
127 bool WaitForEvents();
129 // Migrate to a new socket during an active connection.
130 bool MigrateSocket(const IPAddressNumber& new_host);
132 // From EpollCallbackInterface
133 void OnRegistration(EpollServer* eps, int fd, int event_mask) override {}
134 void OnModification(int fd, int event_mask) override {}
135 void OnEvent(int fd, EpollEvent* event) override;
136 // |fd_| can be unregistered without the client being disconnected. This
137 // happens in b3m QuicProber where we unregister |fd_| to feed in events to
138 // the client from the SelectServer.
139 void OnUnregistration(int fd, bool replaced) override {}
140 void OnShutdown(EpollServer* eps, int fd) override {}
142 // QuicDataStream::Visitor
143 void OnClose(QuicDataStream* stream) override;
145 QuicClientSession* session() { return session_.get(); }
147 bool connected() const;
148 bool goaway_received() const;
150 void set_bind_to_address(IPAddressNumber address) {
151 bind_to_address_ = address;
154 IPAddressNumber bind_to_address() const { return bind_to_address_; }
156 void set_local_port(int local_port) { local_port_ = local_port; }
158 const IPEndPoint& server_address() const { return server_address_; }
160 const IPEndPoint& client_address() const { return client_address_; }
162 int fd() { return fd_; }
164 const QuicServerId& server_id() const { return server_id_; }
166 // This should only be set before the initial Connect()
167 void set_server_id(const QuicServerId& server_id) {
168 server_id_ = server_id;
171 void SetUserAgentID(const std::string& user_agent_id) {
172 crypto_config_.set_user_agent_id(user_agent_id);
175 // SetProofVerifier sets the ProofVerifier that will be used to verify the
176 // server's certificate and takes ownership of |verifier|.
177 void SetProofVerifier(ProofVerifier* verifier) {
178 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
179 crypto_config_.SetProofVerifier(verifier);
182 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
183 // server supports channel IDs, to obtain a channel ID for signing a message
184 // proving possession of the channel ID. This object takes ownership of
185 // |source|.
186 void SetChannelIDSource(ChannelIDSource* source) {
187 crypto_config_.SetChannelIDSource(source);
190 void SetSupportedVersions(const QuicVersionVector& versions) {
191 supported_versions_ = versions;
194 // Takes ownership of the listener.
195 void set_response_listener(ResponseListener* listener) {
196 response_listener_.reset(listener);
199 QuicConfig* config() { return &config_; }
201 void set_store_response(bool val) { store_response_ = val; }
203 size_t latest_response_code() const;
204 const std::string& latest_response_headers() const;
205 const std::string& latest_response_body() const;
207 // Change the initial maximum packet size of the connection. Has to be called
208 // before Connect()/StartConnect() in order to have any effect.
209 void set_initial_max_packet_length(QuicByteCount initial_max_packet_length) {
210 initial_max_packet_length_ = initial_max_packet_length;
213 protected:
214 virtual QuicConnectionId GenerateConnectionId();
215 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
216 virtual QuicPacketWriter* CreateQuicPacketWriter();
218 virtual int ReadPacket(char* buffer,
219 int buffer_len,
220 IPEndPoint* server_address,
221 IPAddressNumber* client_ip);
223 virtual QuicClientSession* CreateQuicClientSession(
224 const QuicConfig& config,
225 QuicConnection* connection,
226 const QuicServerId& server_id,
227 QuicCryptoClientConfig* crypto_config);
229 EpollServer* epoll_server() { return epoll_server_; }
231 // If the socket has been created, then unregister and close() the FD.
232 virtual void CleanUpUDPSocket();
234 private:
235 friend class net::tools::test::QuicClientPeer;
237 // Used during initialization: creates the UDP socket FD, sets socket options,
238 // and binds the socket to our address.
239 bool CreateUDPSocket();
241 // Actually clean up the socket.
242 void CleanUpUDPSocketImpl();
244 // Read a UDP packet and hand it to the framer.
245 bool ReadAndProcessPacket();
247 // Address of the server.
248 const IPEndPoint server_address_;
250 // |server_id_| is a tuple (hostname, port, is_https) of the server.
251 QuicServerId server_id_;
253 // config_ and crypto_config_ contain configuration and cached state about
254 // servers.
255 QuicConfig config_;
256 QuicCryptoClientConfig crypto_config_;
258 // Address of the client if the client is connected to the server.
259 IPEndPoint client_address_;
261 // If initialized, the address to bind to.
262 IPAddressNumber bind_to_address_;
263 // Local port to bind to. Initialize to 0.
264 int local_port_;
266 // Writer used to actually send packets to the wire. Needs to outlive
267 // |session_|.
268 scoped_ptr<QuicPacketWriter> writer_;
270 // Session which manages streams.
271 scoped_ptr<QuicClientSession> session_;
272 // Listens for events on the client socket.
273 EpollServer* epoll_server_;
274 // UDP socket.
275 int fd_;
277 // Helper to be used by created connections.
278 scoped_ptr<QuicEpollConnectionHelper> helper_;
280 // Listens for full responses.
281 scoped_ptr<ResponseListener> response_listener_;
283 // Tracks if the client is initialized to connect.
284 bool initialized_;
286 // If overflow_supported_ is true, this will be the number of packets dropped
287 // during the lifetime of the server.
288 QuicPacketCount packets_dropped_;
290 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
291 // because the socket would otherwise overflow.
292 bool overflow_supported_;
294 // This vector contains QUIC versions which we currently support.
295 // This should be ordered such that the highest supported version is the first
296 // element, with subsequent elements in descending order (versions can be
297 // skipped as necessary). We will always pick supported_versions_[0] as the
298 // initial version to use.
299 QuicVersionVector supported_versions_;
301 // If true, store the latest response code, headers, and body.
302 bool store_response_;
303 // HTTP response code from most recent response.
304 size_t latest_response_code_;
305 // HTTP headers from most recent response.
306 std::string latest_response_headers_;
307 // Body of most recent response.
308 std::string latest_response_body_;
310 // The initial value of maximum packet size of the connection. If set to
311 // zero, the default is used.
312 QuicByteCount initial_max_packet_length_;
314 DISALLOW_COPY_AND_ASSIGN(QuicClient);
317 } // namespace tools
318 } // namespace net
320 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_