Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_client.h
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.
7
8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_
9 #define NET_TOOLS_QUIC_QUIC_CLIENT_H_
10
11 #include <string>
12
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"
24
25 namespace net {
26
27 class ProofVerifier;
28
29 namespace tools {
30
31 class QuicEpollConnectionHelper;
32
33 namespace test {
34 class QuicClientPeer;
35 }  // namespace test
36
37 class QuicClient : public EpollCallbackInterface,
38                    public QuicDataStream::Visitor {
39  public:
40   class ResponseListener {
41    public:
42     ResponseListener() {}
43     virtual ~ResponseListener() {}
44     virtual void OnCompleteResponse(QuicStreamId id,
45                                     const BalsaHeaders& response_headers,
46                                     const string& response_body) = 0;
47   };
48
49   QuicClient(IPEndPoint server_address,
50              const string& server_hostname,
51              const QuicVersionVector& supported_versions,
52              bool print_response);
53   QuicClient(IPEndPoint server_address,
54              const std::string& server_hostname,
55              const QuicConfig& config,
56              const QuicVersionVector& supported_versions);
57
58   virtual ~QuicClient();
59
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.
63   bool Initialize();
64
65   // "Connect" to the QUIC server, including performing synchronous crypto
66   // handshake.
67   bool Connect();
68
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
71   // completes.
72   bool StartConnect();
73
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();
78
79   // Disconnects from the QUIC server.
80   void Disconnect();
81
82   // Sends a request simple GET for each URL in |args|, and then waits for
83   // each to complete.
84   void SendRequestsAndWaitForResponse(const CommandLine::StringVector& args);
85
86   // Returns a newly created QuicSpdyClientStream, owned by the
87   // QuicClient.
88   QuicSpdyClientStream* CreateReliableClientStream();
89
90   // Wait for events until the stream with the given ID is closed.
91   void WaitForStreamToClose(QuicStreamId id);
92
93   // Wait for events until the handshake is confirmed.
94   void WaitForCryptoHandshakeConfirmed();
95
96   // Wait up to 50ms, and handle any events which occur.
97   // Returns true if there are any outstanding requests.
98   bool WaitForEvents();
99
100   // From EpollCallbackInterface
101   virtual void OnRegistration(EpollServer* eps,
102                               int fd,
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 {}
111
112   // QuicDataStream::Visitor
113   virtual void OnClose(QuicDataStream* stream) OVERRIDE;
114
115   QuicPacketCreator::Options* options();
116
117   QuicClientSession* session() { return session_.get(); }
118
119   bool connected() const;
120
121   void set_bind_to_address(IPAddressNumber address) {
122     bind_to_address_ = address;
123   }
124
125   IPAddressNumber bind_to_address() const { return bind_to_address_; }
126
127   void set_local_port(int local_port) { local_port_ = local_port; }
128
129   const IPEndPoint& server_address() const { return server_address_; }
130
131   const IPEndPoint& client_address() const { return client_address_; }
132
133   EpollServer* epoll_server() { return &epoll_server_; }
134
135   int fd() { return fd_; }
136
137   string server_hostname() {
138     return server_hostname_;
139   }
140
141   // This should only be set before the initial Connect()
142   void set_server_hostname(const string& hostname) {
143     server_hostname_ = hostname;
144   }
145
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);
151   }
152
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);
158   }
159
160   void SetSupportedVersions(const QuicVersionVector& versions) {
161     DCHECK(!session_.get());
162     supported_versions_ = versions;
163   }
164
165   // Takes ownership of the listener.
166   void set_response_listener(ResponseListener* listener) {
167     response_listener_.reset(listener);
168   }
169
170  protected:
171   virtual QuicGuid GenerateGuid();
172   virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
173   virtual QuicPacketWriter* CreateQuicPacketWriter();
174
175  private:
176   friend class net::tools::test::QuicClientPeer;
177
178   // Read a UDP packet and hand it to the framer.
179   bool ReadAndProcessPacket();
180
181   // Address of the server.
182   const IPEndPoint server_address_;
183
184   // Hostname of the server. This may be a DNS name or an IP address literal.
185   std::string server_hostname_;
186
187   // config_ and crypto_config_ contain configuration and cached state about
188   // servers.
189   QuicConfig config_;
190   QuicCryptoClientConfig crypto_config_;
191
192   // Address of the client if the client is connected to the server.
193   IPEndPoint client_address_;
194
195   // If initialized, the address to bind to.
196   IPAddressNumber bind_to_address_;
197   // Local port to bind to. Initialize to 0.
198   int local_port_;
199
200   // Session which manages streams.
201   scoped_ptr<QuicClientSession> session_;
202   // Listens for events on the client socket.
203   EpollServer epoll_server_;
204   // UDP socket.
205   int fd_;
206
207   // Helper to be used by created connections.
208   scoped_ptr<QuicEpollConnectionHelper> helper_;
209
210   // Listens for full responses.
211   scoped_ptr<ResponseListener> response_listener_;
212
213   // Writer used to actually send packets to the wire.
214   scoped_ptr<QuicPacketWriter> writer_;
215
216   // Tracks if the client is initialized to connect.
217   bool initialized_;
218
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
221   // are dropped.
222   int packets_dropped_;
223
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_;
227
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_;
234
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_;
238
239   DISALLOW_COPY_AND_ASSIGN(QuicClient);
240 };
241
242 }  // namespace tools
243 }  // namespace net
244
245 #endif  // NET_TOOLS_QUIC_QUIC_CLIENT_H_