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_SIMPLE_CLIENT_H_
9 #define NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_
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/io_buffer.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/log/net_log.h"
21 #include "net/quic/crypto/crypto_handshake.h"
22 #include "net/quic/quic_config.h"
23 #include "net/quic/quic_framer.h"
24 #include "net/quic/quic_packet_creator.h"
25 #include "net/quic/quic_packet_reader.h"
26 #include "net/tools/quic/quic_client_session.h"
27 #include "net/tools/quic/quic_spdy_client_stream.h"
31 struct HttpRequestInfo
;
34 class QuicConnectionHelper
;
35 class UDPClientSocket
;
43 class QuicSimpleClient
: public QuicDataStream::Visitor
,
44 public QuicPacketReader::Visitor
{
46 class ResponseListener
{
49 virtual ~ResponseListener() {}
50 virtual void OnCompleteResponse(QuicStreamId id
,
51 const HttpResponseHeaders
& response_headers
,
52 const std::string
& response_body
) = 0;
55 // Create a quic client, which will have events managed by an externally owned
57 QuicSimpleClient(IPEndPoint server_address
,
58 const QuicServerId
& server_id
,
59 const QuicVersionVector
& supported_versions
);
60 QuicSimpleClient(IPEndPoint server_address
,
61 const QuicServerId
& server_id
,
62 const QuicVersionVector
& supported_versions
,
63 const QuicConfig
& config
);
65 ~QuicSimpleClient() override
;
67 // Initializes the client to create a connection. Should be called exactly
68 // once before calling StartConnect or Connect. Returns true if the
69 // initialization succeeds, false otherwise.
72 // "Connect" to the QUIC server, including performing synchronous crypto
76 // Start the crypto handshake. This can be done in place of the synchronous
77 // Connect(), but callers are responsible for making sure the crypto handshake
81 // Returns true if the crypto handshake has yet to establish encryption.
82 // Returns false if encryption is active (even if the server hasn't confirmed
83 // the handshake) or if the connection has been closed.
84 bool EncryptionBeingEstablished();
86 // Disconnects from the QUIC server.
89 // Sends an HTTP request and does not wait for response before returning.
90 void SendRequest(const HttpRequestInfo
& headers
,
91 base::StringPiece body
,
94 // Sends an HTTP request and waits for response before returning.
95 void SendRequestAndWaitForResponse(const HttpRequestInfo
& headers
,
96 base::StringPiece body
,
99 // Sends a request simple GET for each URL in |args|, and then waits for
101 void SendRequestsAndWaitForResponse(
102 const base::CommandLine::StringVector
& url_list
);
104 // Returns a newly created QuicSpdyClientStream, owned by the
106 QuicSpdyClientStream
* CreateReliableClientStream();
108 // Wait for events until the stream with the given ID is closed.
109 void WaitForStreamToClose(QuicStreamId id
);
111 // Wait for events until the handshake is confirmed.
112 void WaitForCryptoHandshakeConfirmed();
114 // Wait up to 50ms, and handle any events which occur.
115 // Returns true if there are any outstanding requests.
116 bool WaitForEvents();
118 // Migrate to a new socket during an active connection.
119 bool MigrateSocket(const IPAddressNumber
& new_host
);
121 // QuicPacketReader::Visitor
122 void OnReadError(int result
) override
;
123 bool OnPacket(const QuicEncryptedPacket
& packet
,
124 IPEndPoint local_address
,
125 IPEndPoint peer_address
) override
;
127 // QuicDataStream::Visitor
128 void OnClose(QuicDataStream
* stream
) override
;
130 QuicClientSession
* session() { return session_
.get(); }
132 bool connected() const;
133 bool goaway_received() const;
135 void set_bind_to_address(IPAddressNumber address
) {
136 bind_to_address_
= address
;
139 IPAddressNumber
bind_to_address() const { return bind_to_address_
; }
141 void set_local_port(int local_port
) { local_port_
= local_port
; }
143 const IPEndPoint
& server_address() const { return server_address_
; }
145 const IPEndPoint
& client_address() const { return client_address_
; }
147 const QuicServerId
& server_id() const { return server_id_
; }
149 // This should only be set before the initial Connect()
150 void set_server_id(const QuicServerId
& server_id
) {
151 server_id_
= server_id
;
154 void SetUserAgentID(const std::string
& user_agent_id
) {
155 crypto_config_
.set_user_agent_id(user_agent_id
);
158 // SetProofVerifier sets the ProofVerifier that will be used to verify the
159 // server's certificate and takes ownership of |verifier|.
160 void SetProofVerifier(ProofVerifier
* verifier
) {
161 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
162 crypto_config_
.SetProofVerifier(verifier
);
165 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
166 // server supports channel IDs, to obtain a channel ID for signing a message
167 // proving possession of the channel ID. This object takes ownership of
169 void SetChannelIDSource(ChannelIDSource
* source
) {
170 crypto_config_
.SetChannelIDSource(source
);
173 void SetSupportedVersions(const QuicVersionVector
& versions
) {
174 supported_versions_
= versions
;
177 // Takes ownership of the listener.
178 void set_response_listener(ResponseListener
* listener
) {
179 response_listener_
.reset(listener
);
182 QuicConfig
* config() { return &config_
; }
184 void set_store_response(bool val
) { store_response_
= val
; }
186 size_t latest_response_code() const;
187 const std::string
& latest_response_headers() const;
188 const std::string
& latest_response_body() const;
190 // Change the initial maximum packet size of the connection. Has to be called
191 // before Connect()/StartConnect() in order to have any effect.
192 void set_initial_max_packet_length(QuicByteCount initial_max_packet_length
) {
193 initial_max_packet_length_
= initial_max_packet_length
;
197 virtual QuicConnectionId
GenerateConnectionId();
198 virtual QuicConnectionHelper
* CreateQuicConnectionHelper();
199 virtual QuicPacketWriter
* CreateQuicPacketWriter();
201 virtual QuicClientSession
* CreateQuicClientSession(
202 const QuicConfig
& config
,
203 QuicConnection
* connection
,
204 const QuicServerId
& server_id
,
205 QuicCryptoClientConfig
* crypto_config
);
208 friend class net::tools::test::QuicClientPeer
;
210 // A packet writer factory that always returns the same writer
211 class DummyPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
213 DummyPacketWriterFactory(QuicPacketWriter
* writer
);
214 ~DummyPacketWriterFactory() override
;
216 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
219 QuicPacketWriter
* writer_
;
222 // Used during initialization: creates the UDP socket FD, sets socket options,
223 // and binds the socket to our address.
224 bool CreateUDPSocket();
226 // Read a UDP packet and hand it to the framer.
227 bool ReadAndProcessPacket();
229 // Used by |helper_| to time alarms.
232 // Address of the server.
233 const IPEndPoint server_address_
;
235 // |server_id_| is a tuple (hostname, port, is_https) of the server.
236 QuicServerId server_id_
;
238 // config_ and crypto_config_ contain configuration and cached state about
241 QuicCryptoClientConfig crypto_config_
;
243 // Address of the client if the client is connected to the server.
244 IPEndPoint client_address_
;
246 // If initialized, the address to bind to.
247 IPAddressNumber bind_to_address_
;
248 // Local port to bind to. Initialize to 0.
251 // Writer used to actually send packets to the wire. Needs to outlive
253 scoped_ptr
<QuicPacketWriter
> writer_
;
255 // Session which manages streams.
256 scoped_ptr
<QuicClientSession
> session_
;
258 // UDP socket connected to the server.
259 scoped_ptr
<UDPClientSocket
> socket_
;
261 // Connection on the socket. Owned by |session_|.
262 QuicConnection
* connection_
;
264 // Helper to be used by created connections.
265 scoped_ptr
<QuicConnectionHelper
> helper_
;
267 // Listens for full responses.
268 scoped_ptr
<ResponseListener
> response_listener_
;
270 // Tracks if the client is initialized to connect.
273 // If overflow_supported_ is true, this will be the number of packets dropped
274 // during the lifetime of the server.
275 QuicPacketCount packets_dropped_
;
277 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
278 // because the socket would otherwise overflow.
279 bool overflow_supported_
;
281 // This vector contains QUIC versions which we currently support.
282 // This should be ordered such that the highest supported version is the first
283 // element, with subsequent elements in descending order (versions can be
284 // skipped as necessary). We will always pick supported_versions_[0] as the
285 // initial version to use.
286 QuicVersionVector supported_versions_
;
288 // If true, store the latest response code, headers, and body.
289 bool store_response_
;
290 // HTTP response code from most recent response.
291 size_t latest_response_code_
;
292 // HTTP headers from most recent response.
293 std::string latest_response_headers_
;
294 // Body of most recent response.
295 std::string latest_response_body_
;
297 // The initial value of maximum packet size of the connection. If set to
298 // zero, the default is used.
299 QuicByteCount initial_max_packet_length_
;
301 // The log used for the sockets.
304 scoped_ptr
<QuicPacketReader
> packet_reader_
;
306 base::WeakPtrFactory
<QuicSimpleClient
> weak_factory_
;
308 DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient
);
314 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_