- add sources.
[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/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"
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 ReliableQuicStream::Visitor {
39  public:
40   QuicClient(IPEndPoint server_address,
41              const string& server_hostname,
42              const QuicVersionVector& supported_versions,
43              bool print_response);
44   QuicClient(IPEndPoint server_address,
45              const std::string& server_hostname,
46              const QuicConfig& config,
47              const QuicVersionVector& supported_versions);
48
49   virtual ~QuicClient();
50
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.
54   bool Initialize();
55
56   // "Connect" to the QUIC server, including performing synchronous crypto
57   // handshake.
58   bool Connect();
59
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
62   // completes.
63   bool StartConnect();
64
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();
69
70   // Disconnects from the QUIC server.
71   void Disconnect();
72
73   // Sends a request simple GET for each URL in arg, and then waits for
74   // each to complete.
75   void SendRequestsAndWaitForResponse(const CommandLine::StringVector& args);
76
77   // Returns a newly created CreateReliableClientStream, owned by the
78   // QuicClient.
79   QuicReliableClientStream* CreateReliableClientStream();
80
81   // Wait for events until the stream with the given ID is closed.
82   void WaitForStreamToClose(QuicStreamId id);
83
84   // Wait for events until the handshake is confirmed.
85   void WaitForCryptoHandshakeConfirmed();
86
87   // Wait up to 50ms, and handle any events which occur.
88   // Returns true if there are any outstanding requests.
89   bool WaitForEvents();
90
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 {}
101
102   // ReliableQuicStream::Visitor
103   virtual void OnClose(ReliableQuicStream* stream) OVERRIDE;
104
105   QuicPacketCreator::Options* options();
106
107   QuicClientSession* session() { return session_.get(); }
108
109   bool connected() const;
110
111   void set_bind_to_address(IPAddressNumber address) {
112     bind_to_address_ = address;
113   }
114
115   IPAddressNumber bind_to_address() const { return bind_to_address_; }
116
117   void set_local_port(int local_port) { local_port_ = local_port; }
118
119   const IPEndPoint& server_address() const { return server_address_; }
120
121   const IPEndPoint& client_address() const { return client_address_; }
122
123   EpollServer* epoll_server() { return &epoll_server_; }
124
125   int fd() { return fd_; }
126
127   // This should only be set before the initial Connect()
128   void set_server_hostname(const string& hostname) {
129     server_hostname_ = hostname;
130   }
131
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);
137   }
138
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);
144   }
145
146  protected:
147   virtual QuicGuid GenerateGuid();
148   virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper();
149   virtual QuicPacketWriter* CreateQuicPacketWriter();
150
151  private:
152   friend class net::tools::test::QuicClientPeer;
153
154   // Read a UDP packet and hand it to the framer.
155   bool ReadAndProcessPacket();
156
157   // Set of streams created (and owned) by this client
158   base::hash_set<QuicReliableClientStream*> streams_;
159
160   // Address of the server.
161   const IPEndPoint server_address_;
162
163   // Hostname of the server. This may be a DNS name or an IP address literal.
164   std::string server_hostname_;
165
166   // config_ and crypto_config_ contain configuration and cached state about
167   // servers.
168   QuicConfig config_;
169   QuicCryptoClientConfig crypto_config_;
170
171   // Address of the client if the client is connected to the server.
172   IPEndPoint client_address_;
173
174   // If initialized, the address to bind to.
175   IPAddressNumber bind_to_address_;
176   // Local port to bind to. Initialize to 0.
177   int local_port_;
178
179   // Session which manages streams.
180   scoped_ptr<QuicClientSession> session_;
181   // Listens for events on the client socket.
182   EpollServer epoll_server_;
183   // UDP socket.
184   int fd_;
185
186   // Helper to be used by created connections.
187   scoped_ptr<QuicEpollConnectionHelper> helper_;
188
189   // Writer used to actually send packets to the wire.
190   scoped_ptr<QuicPacketWriter> writer_;
191
192   // Tracks if the client is initialized to connect.
193   bool initialized_;
194
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
197   // are dropped.
198   int packets_dropped_;
199
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_;
203
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_;
210
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_;
214
215   DISALLOW_COPY_AND_ASSIGN(QuicClient);
216 };
217
218 }  // namespace tools
219 }  // namespace net
220
221 #endif  // NET_TOOLS_QUIC_QUIC_CLIENT_H_