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/command_line.h"
14 #include "base/containers/hash_tables.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_reliable_client_stream.h"
31 class QuicEpollConnectionHelper;
37 class QuicClient : public EpollCallbackInterface,
38 public ReliableQuicStream::Visitor {
40 QuicClient(IPEndPoint server_address,
41 const string& server_hostname,
42 const QuicVersionVector& supported_versions,
44 QuicClient(IPEndPoint server_address,
45 const std::string& server_hostname,
46 const QuicConfig& config,
47 const QuicVersionVector& supported_versions);
49 virtual ~QuicClient();
51 // Initializes the client to create a connection. Should be called exactly
52 // once before calling StartConnect or Connect. Returns true if the
53 // initialization succeeds, false otherwise.
56 // "Connect" to the QUIC server, including performing synchronous crypto
60 // Start the crypto handshake. This can be done in place of the synchronous
61 // Connect(), but callers are responsible for making sure the crypto handshake
65 // Returns true if the crypto handshake has yet to establish encryption.
66 // Returns false if encryption is active (even if the server hasn't confirmed
67 // the handshake) or if the connection has been closed.
68 bool EncryptionBeingEstablished();
70 // Disconnects from the QUIC server.
73 // Sends a request simple GET for each URL in arg, and then waits for
75 void SendRequestsAndWaitForResponse(const CommandLine::StringVector& args);
77 // Returns a newly created CreateReliableClientStream, owned by the
79 QuicReliableClientStream* CreateReliableClientStream();
81 // Wait for events until the stream with the given ID is closed.
82 void WaitForStreamToClose(QuicStreamId id);
84 // Wait for events until the handshake is confirmed.
85 void WaitForCryptoHandshakeConfirmed();
87 // Wait up to 50ms, and handle any events which occur.
88 // Returns true if there are any outstanding requests.
91 // From EpollCallbackInterface
92 virtual void OnRegistration(
93 EpollServer* eps, int fd, int event_mask) OVERRIDE {}
94 virtual void OnModification(int fd, int event_mask) OVERRIDE {}
95 virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
96 // |fd_| can be unregistered without the client being disconnected. This
97 // happens in b3m QuicProber where we unregister |fd_| to feed in events to
98 // the client from the SelectServer.
99 virtual void OnUnregistration(int fd, bool replaced) OVERRIDE {}
100 virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
102 // ReliableQuicStream::Visitor
103 virtual void OnClose(ReliableQuicStream* stream) OVERRIDE;
105 QuicPacketCreator::Options* options();
107 QuicClientSession* session() { return session_.get(); }
109 bool connected() const;
111 void set_bind_to_address(IPAddressNumber address) {
112 bind_to_address_ = address;
115 IPAddressNumber bind_to_address() const { return bind_to_address_; }
117 void set_local_port(int local_port) { local_port_ = local_port; }
119 const IPEndPoint& server_address() const { return server_address_; }
121 const IPEndPoint& client_address() const { return client_address_; }
123 EpollServer* epoll_server() { return &epoll_server_; }
125 int fd() { return fd_; }
127 // This should only be set before the initial Connect()
128 void set_server_hostname(const string& hostname) {
129 server_hostname_ = hostname;
132 // SetProofVerifier sets the ProofVerifier that will be used to verify the
133 // server's certificate and takes ownership of |verifier|.
134 void SetProofVerifier(ProofVerifier* verifier) {
135 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
136 crypto_config_.SetProofVerifier(verifier);
139 // SetChannelIDSigner sets a ChannelIDSigner that will be called when the
140 // server supports channel IDs to sign a message proving possession of the
141 // given ChannelID. This object takes ownership of |signer|.
142 void SetChannelIDSigner(ChannelIDSigner* signer) {
143 crypto_config_.SetChannelIDSigner(signer);
147 virtual QuicGuid GenerateGuid();
148 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
149 virtual QuicPacketWriter* CreateQuicPacketWriter();
152 friend class net::tools::test::QuicClientPeer;
154 // Read a UDP packet and hand it to the framer.
155 bool ReadAndProcessPacket();
157 // Set of streams created (and owned) by this client
158 base::hash_set<QuicReliableClientStream*> streams_;
160 // Address of the server.
161 const IPEndPoint server_address_;
163 // Hostname of the server. This may be a DNS name or an IP address literal.
164 std::string server_hostname_;
166 // config_ and crypto_config_ contain configuration and cached state about
169 QuicCryptoClientConfig crypto_config_;
171 // Address of the client if the client is connected to the server.
172 IPEndPoint client_address_;
174 // If initialized, the address to bind to.
175 IPAddressNumber bind_to_address_;
176 // Local port to bind to. Initialize to 0.
179 // Session which manages streams.
180 scoped_ptr<QuicClientSession> session_;
181 // Listens for events on the client socket.
182 EpollServer epoll_server_;
186 // Helper to be used by created connections.
187 scoped_ptr<QuicEpollConnectionHelper> helper_;
189 // Writer used to actually send packets to the wire.
190 scoped_ptr<QuicPacketWriter> writer_;
192 // Tracks if the client is initialized to connect.
195 // If overflow_supported_ is true, this will be the number of packets dropped
196 // during the lifetime of the server. This may overflow if enough packets
198 int packets_dropped_;
200 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
201 // because the socket would otherwise overflow.
202 bool overflow_supported_;
204 // This vector contains QUIC versions which we currently support.
205 // This should be ordered such that the highest supported version is the first
206 // element, with subsequent elements in descending order (versions can be
207 // skipped as necessary). We will always pick supported_versions_[0] as the
208 // initial version to use.
209 QuicVersionVector supported_versions_;
211 // If true, then the contents of each response will be printed to stdout
212 // when the stream is closed (in OnClose).
213 bool print_response_;
215 DISALLOW_COPY_AND_ASSIGN(QuicClient);
221 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_