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"
31 class QuicEpollConnectionHelper;
37 class QuicClient : public EpollCallbackInterface,
38 public QuicDataStream::Visitor {
40 class ResponseListener {
43 virtual ~ResponseListener() {}
44 virtual void OnCompleteResponse(QuicStreamId id,
45 const BalsaHeaders& response_headers,
46 const string& response_body) = 0;
49 QuicClient(IPEndPoint server_address,
50 const string& server_hostname,
51 const QuicVersionVector& supported_versions,
53 QuicClient(IPEndPoint server_address,
54 const std::string& server_hostname,
55 const QuicConfig& config,
56 const QuicVersionVector& supported_versions);
58 virtual ~QuicClient();
60 // Initializes the client to create a connection. Should be called exactly
61 // once before calling StartConnect or Connect. Returns true if the
62 // initialization succeeds, false otherwise.
65 // "Connect" to the QUIC server, including performing synchronous crypto
69 // Start the crypto handshake. This can be done in place of the synchronous
70 // Connect(), but callers are responsible for making sure the crypto handshake
74 // Returns true if the crypto handshake has yet to establish encryption.
75 // Returns false if encryption is active (even if the server hasn't confirmed
76 // the handshake) or if the connection has been closed.
77 bool EncryptionBeingEstablished();
79 // Disconnects from the QUIC server.
82 // Sends a request simple GET for each URL in |args|, and then waits for
84 void SendRequestsAndWaitForResponse(const CommandLine::StringVector& args);
86 // Returns a newly created QuicSpdyClientStream, owned by the
88 QuicSpdyClientStream* CreateReliableClientStream();
90 // Wait for events until the stream with the given ID is closed.
91 void WaitForStreamToClose(QuicStreamId id);
93 // Wait for events until the handshake is confirmed.
94 void WaitForCryptoHandshakeConfirmed();
96 // Wait up to 50ms, and handle any events which occur.
97 // Returns true if there are any outstanding requests.
100 // From EpollCallbackInterface
101 virtual void OnRegistration(EpollServer* eps,
103 int event_mask) OVERRIDE {}
104 virtual void OnModification(int fd, int event_mask) OVERRIDE {}
105 virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
106 // |fd_| can be unregistered without the client being disconnected. This
107 // happens in b3m QuicProber where we unregister |fd_| to feed in events to
108 // the client from the SelectServer.
109 virtual void OnUnregistration(int fd, bool replaced) OVERRIDE {}
110 virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
112 // QuicDataStream::Visitor
113 virtual void OnClose(QuicDataStream* stream) OVERRIDE;
115 QuicPacketCreator::Options* options();
117 QuicClientSession* session() { return session_.get(); }
119 bool connected() const;
121 void set_bind_to_address(IPAddressNumber address) {
122 bind_to_address_ = address;
125 IPAddressNumber bind_to_address() const { return bind_to_address_; }
127 void set_local_port(int local_port) { local_port_ = local_port; }
129 const IPEndPoint& server_address() const { return server_address_; }
131 const IPEndPoint& client_address() const { return client_address_; }
133 EpollServer* epoll_server() { return &epoll_server_; }
135 int fd() { return fd_; }
137 string server_hostname() {
138 return server_hostname_;
141 // This should only be set before the initial Connect()
142 void set_server_hostname(const string& hostname) {
143 server_hostname_ = hostname;
146 // SetProofVerifier sets the ProofVerifier that will be used to verify the
147 // server's certificate and takes ownership of |verifier|.
148 void SetProofVerifier(ProofVerifier* verifier) {
149 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
150 crypto_config_.SetProofVerifier(verifier);
153 // SetChannelIDSigner sets a ChannelIDSigner that will be called when the
154 // server supports channel IDs to sign a message proving possession of the
155 // given ChannelID. This object takes ownership of |signer|.
156 void SetChannelIDSigner(ChannelIDSigner* signer) {
157 crypto_config_.SetChannelIDSigner(signer);
160 void SetSupportedVersions(const QuicVersionVector& versions) {
161 DCHECK(!session_.get());
162 supported_versions_ = versions;
165 // Takes ownership of the listener.
166 void set_response_listener(ResponseListener* listener) {
167 response_listener_.reset(listener);
171 virtual QuicGuid GenerateGuid();
172 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
173 virtual QuicPacketWriter* CreateQuicPacketWriter();
176 friend class net::tools::test::QuicClientPeer;
178 // Read a UDP packet and hand it to the framer.
179 bool ReadAndProcessPacket();
181 // Address of the server.
182 const IPEndPoint server_address_;
184 // Hostname of the server. This may be a DNS name or an IP address literal.
185 std::string server_hostname_;
187 // config_ and crypto_config_ contain configuration and cached state about
190 QuicCryptoClientConfig crypto_config_;
192 // Address of the client if the client is connected to the server.
193 IPEndPoint client_address_;
195 // If initialized, the address to bind to.
196 IPAddressNumber bind_to_address_;
197 // Local port to bind to. Initialize to 0.
200 // Session which manages streams.
201 scoped_ptr<QuicClientSession> session_;
202 // Listens for events on the client socket.
203 EpollServer epoll_server_;
207 // Helper to be used by created connections.
208 scoped_ptr<QuicEpollConnectionHelper> helper_;
210 // Listens for full responses.
211 scoped_ptr<ResponseListener> response_listener_;
213 // Writer used to actually send packets to the wire.
214 scoped_ptr<QuicPacketWriter> writer_;
216 // Tracks if the client is initialized to connect.
219 // If overflow_supported_ is true, this will be the number of packets dropped
220 // during the lifetime of the server. This may overflow if enough packets
222 int packets_dropped_;
224 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
225 // because the socket would otherwise overflow.
226 bool overflow_supported_;
228 // This vector contains QUIC versions which we currently support.
229 // This should be ordered such that the highest supported version is the first
230 // element, with subsequent elements in descending order (versions can be
231 // skipped as necessary). We will always pick supported_versions_[0] as the
232 // initial version to use.
233 QuicVersionVector supported_versions_;
235 // If true, then the contents of each response will be printed to stdout
236 // when the stream is closed (in OnClose).
237 bool print_response_;
239 DISALLOW_COPY_AND_ASSIGN(QuicClient);
245 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_