| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A toy client, which connects to a specified port and sends QUIC | 5 // A toy client, which connects to a specified port and sends QUIC |
| 6 // request to that endpoint. | 6 // request to that endpoint. |
| 7 | 7 |
| 8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_H_ | 8 #ifndef NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ |
| 9 #define NET_TOOLS_QUIC_QUIC_CLIENT_H_ | 9 #define NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "net/base/io_buffer.h" |
| 17 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 19 #include "net/base/net_log.h" |
| 20 #include "net/http/http_response_headers.h" |
| 18 #include "net/quic/crypto/crypto_handshake.h" | 21 #include "net/quic/crypto/crypto_handshake.h" |
| 19 #include "net/quic/quic_config.h" | 22 #include "net/quic/quic_config.h" |
| 20 #include "net/quic/quic_framer.h" | 23 #include "net/quic/quic_framer.h" |
| 21 #include "net/quic/quic_packet_creator.h" | 24 #include "net/quic/quic_packet_creator.h" |
| 22 #include "net/tools/balsa/balsa_headers.h" | 25 #include "net/tools/quic/quic_simple_client_session.h" |
| 23 #include "net/tools/epoll_server/epoll_server.h" | 26 #include "net/tools/quic/quic_simple_client_stream.h" |
| 24 #include "net/tools/quic/quic_client_session.h" | |
| 25 #include "net/tools/quic/quic_spdy_client_stream.h" | |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 | 29 |
| 30 struct HttpRequestInfo; |
| 29 class ProofVerifier; | 31 class ProofVerifier; |
| 30 class QuicServerId; | 32 class QuicServerId; |
| 33 class QuicConnectionHelper; |
| 34 class UDPClientSocket; |
| 31 | 35 |
| 32 namespace tools { | 36 namespace tools { |
| 33 | 37 |
| 34 class QuicEpollConnectionHelper; | |
| 35 | |
| 36 namespace test { | 38 namespace test { |
| 37 class QuicClientPeer; | 39 class QuicClientPeer; |
| 38 } // namespace test | 40 } // namespace test |
| 39 | 41 |
| 40 class QuicClient : public EpollCallbackInterface, | 42 class QuicSimpleClient : public QuicDataStream::Visitor { |
| 41 public QuicDataStream::Visitor { | |
| 42 public: | 43 public: |
| 43 class ResponseListener { | 44 class ResponseListener { |
| 44 public: | 45 public: |
| 45 ResponseListener() {} | 46 ResponseListener() {} |
| 46 virtual ~ResponseListener() {} | 47 virtual ~ResponseListener() {} |
| 47 virtual void OnCompleteResponse(QuicStreamId id, | 48 virtual void OnCompleteResponse(QuicStreamId id, |
| 48 const BalsaHeaders& response_headers, | 49 const HttpResponseHeaders& response_headers, |
| 49 const std::string& response_body) = 0; | 50 const std::string& response_body) = 0; |
| 50 }; | 51 }; |
| 51 | 52 |
| 52 // Create a quic client, which will have events managed by an externally owned | 53 // Create a quic client, which will have events managed by an externally owned |
| 53 // EpollServer. | 54 // EpollServer. |
| 54 QuicClient(IPEndPoint server_address, | 55 QuicSimpleClient(IPEndPoint server_address, |
| 55 const QuicServerId& server_id, | 56 const QuicServerId& server_id, |
| 56 const QuicVersionVector& supported_versions, | 57 const QuicVersionVector& supported_versions); |
| 57 EpollServer* epoll_server); | 58 QuicSimpleClient(IPEndPoint server_address, |
| 58 QuicClient(IPEndPoint server_address, | 59 const QuicServerId& server_id, |
| 59 const QuicServerId& server_id, | 60 const QuicVersionVector& supported_versions, |
| 60 const QuicVersionVector& supported_versions, | 61 const QuicConfig& config); |
| 61 const QuicConfig& config, | |
| 62 EpollServer* epoll_server); | |
| 63 | 62 |
| 64 ~QuicClient() override; | 63 ~QuicSimpleClient() override; |
| 65 | 64 |
| 66 // Initializes the client to create a connection. Should be called exactly | 65 // Initializes the client to create a connection. Should be called exactly |
| 67 // once before calling StartConnect or Connect. Returns true if the | 66 // once before calling StartConnect or Connect. Returns true if the |
| 68 // initialization succeeds, false otherwise. | 67 // initialization succeeds, false otherwise. |
| 69 bool Initialize(); | 68 bool Initialize(); |
| 70 | 69 |
| 71 // "Connect" to the QUIC server, including performing synchronous crypto | 70 // "Connect" to the QUIC server, including performing synchronous crypto |
| 72 // handshake. | 71 // handshake. |
| 73 bool Connect(); | 72 bool Connect(); |
| 74 | 73 |
| 75 // Start the crypto handshake. This can be done in place of the synchronous | 74 // Start the crypto handshake. This can be done in place of the synchronous |
| 76 // Connect(), but callers are responsible for making sure the crypto handshake | 75 // Connect(), but callers are responsible for making sure the crypto handshake |
| 77 // completes. | 76 // completes. |
| 78 void StartConnect(); | 77 void StartConnect(); |
| 79 | 78 |
| 80 // Returns true if the crypto handshake has yet to establish encryption. | 79 // Returns true if the crypto handshake has yet to establish encryption. |
| 81 // Returns false if encryption is active (even if the server hasn't confirmed | 80 // Returns false if encryption is active (even if the server hasn't confirmed |
| 82 // the handshake) or if the connection has been closed. | 81 // the handshake) or if the connection has been closed. |
| 83 bool EncryptionBeingEstablished(); | 82 bool EncryptionBeingEstablished(); |
| 84 | 83 |
| 85 // Disconnects from the QUIC server. | 84 // Disconnects from the QUIC server. |
| 86 void Disconnect(); | 85 void Disconnect(); |
| 87 | 86 |
| 88 // Sends an HTTP request and does not wait for response before returning. | 87 // Sends an HTTP request and does not wait for response before returning. |
| 89 void SendRequest(const BalsaHeaders& headers, | 88 void SendRequest(const HttpRequestInfo& headers, |
| 90 base::StringPiece body, | 89 base::StringPiece body, |
| 91 bool fin); | 90 bool fin); |
| 92 | 91 |
| 93 // Sends an HTTP request and waits for response before returning. | 92 // Sends an HTTP request and waits for response before returning. |
| 94 void SendRequestAndWaitForResponse(const BalsaHeaders& headers, | 93 void SendRequestAndWaitForResponse(const HttpRequestInfo& headers, |
| 95 base::StringPiece body, | 94 base::StringPiece body, |
| 96 bool fin); | 95 bool fin); |
| 97 | 96 |
| 98 // Sends a request simple GET for each URL in |args|, and then waits for | 97 // Sends a request simple GET for each URL in |args|, and then waits for |
| 99 // each to complete. | 98 // each to complete. |
| 100 void SendRequestsAndWaitForResponse(const | 99 void SendRequestsAndWaitForResponse( |
| 101 base::CommandLine::StringVector& args); | 100 const base::CommandLine::StringVector& url_list); |
| 102 | 101 |
| 103 // Returns a newly created QuicSpdyClientStream, owned by the | 102 // Returns a newly created QuicSimpleClientStream, owned by the |
| 104 // QuicClient. | 103 // QuicSimpleClient. |
| 105 QuicSpdyClientStream* CreateReliableClientStream(); | 104 QuicSimpleClientStream* CreateReliableClientStream(); |
| 106 | 105 |
| 107 // Wait for events until the stream with the given ID is closed. | 106 // Wait for events until the stream with the given ID is closed. |
| 108 void WaitForStreamToClose(QuicStreamId id); | 107 void WaitForStreamToClose(QuicStreamId id); |
| 109 | 108 |
| 110 // Wait for events until the handshake is confirmed. | 109 // Wait for events until the handshake is confirmed. |
| 111 void WaitForCryptoHandshakeConfirmed(); | 110 void WaitForCryptoHandshakeConfirmed(); |
| 112 | 111 |
| 113 // Wait up to 50ms, and handle any events which occur. | 112 // Wait up to 50ms, and handle any events which occur. |
| 114 // Returns true if there are any outstanding requests. | 113 // Returns true if there are any outstanding requests. |
| 115 bool WaitForEvents(); | 114 bool WaitForEvents(); |
| 116 | 115 |
| 117 // From EpollCallbackInterface | 116 // Start the read loop on the socket. |
| 118 void OnRegistration(EpollServer* eps, int fd, int event_mask) override {} | 117 void StartReading(); |
| 119 void OnModification(int fd, int event_mask) override {} | 118 |
| 120 void OnEvent(int fd, EpollEvent* event) override; | 119 // Called on reads that complete asynchronously. Dispatches the packet and |
| 121 // |fd_| can be unregistered without the client being disconnected. This | 120 // calls StartReading() again. |
| 122 // happens in b3m QuicProber where we unregister |fd_| to feed in events to | 121 void OnReadComplete(int result); |
| 123 // the client from the SelectServer. | |
| 124 void OnUnregistration(int fd, bool replaced) override {} | |
| 125 void OnShutdown(EpollServer* eps, int fd) override {} | |
| 126 | 122 |
| 127 // QuicDataStream::Visitor | 123 // QuicDataStream::Visitor |
| 128 void OnClose(QuicDataStream* stream) override; | 124 void OnClose(QuicDataStream* stream) override; |
| 129 | 125 |
| 130 QuicClientSession* session() { return session_.get(); } | 126 QuicSimpleClientSession* session() { return session_.get(); } |
| 131 | 127 |
| 132 bool connected() const; | 128 bool connected() const; |
| 133 bool goaway_received() const; | 129 bool goaway_received() const; |
| 134 | 130 |
| 135 void set_bind_to_address(IPAddressNumber address) { | 131 void set_bind_to_address(IPAddressNumber address) { |
| 136 bind_to_address_ = address; | 132 bind_to_address_ = address; |
| 137 } | 133 } |
| 138 | 134 |
| 139 IPAddressNumber bind_to_address() const { return bind_to_address_; } | 135 IPAddressNumber bind_to_address() const { return bind_to_address_; } |
| 140 | 136 |
| 141 void set_local_port(int local_port) { local_port_ = local_port; } | 137 void set_local_port(int local_port) { local_port_ = local_port; } |
| 142 | 138 |
| 143 const IPEndPoint& server_address() const { return server_address_; } | 139 const IPEndPoint& server_address() const { return server_address_; } |
| 144 | 140 |
| 145 const IPEndPoint& client_address() const { return client_address_; } | 141 const IPEndPoint& client_address() const { return client_address_; } |
| 146 | 142 |
| 147 int fd() { return fd_; } | |
| 148 | |
| 149 const QuicServerId& server_id() const { return server_id_; } | 143 const QuicServerId& server_id() const { return server_id_; } |
| 150 | 144 |
| 151 // This should only be set before the initial Connect() | 145 // This should only be set before the initial Connect() |
| 152 void set_server_id(const QuicServerId& server_id) { | 146 void set_server_id(const QuicServerId& server_id) { |
| 153 server_id_ = server_id; | 147 server_id_ = server_id; |
| 154 } | 148 } |
| 155 | 149 |
| 156 void SetUserAgentID(const std::string& user_agent_id) { | 150 void SetUserAgentID(const std::string& user_agent_id) { |
| 157 crypto_config_.set_user_agent_id(user_agent_id); | 151 crypto_config_.set_user_agent_id(user_agent_id); |
| 158 } | 152 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 184 QuicConfig* config() { return &config_; } | 178 QuicConfig* config() { return &config_; } |
| 185 | 179 |
| 186 void set_store_response(bool val) { store_response_ = val; } | 180 void set_store_response(bool val) { store_response_ = val; } |
| 187 | 181 |
| 188 size_t latest_response_code() const; | 182 size_t latest_response_code() const; |
| 189 const std::string& latest_response_headers() const; | 183 const std::string& latest_response_headers() const; |
| 190 const std::string& latest_response_body() const; | 184 const std::string& latest_response_body() const; |
| 191 | 185 |
| 192 protected: | 186 protected: |
| 193 virtual QuicConnectionId GenerateConnectionId(); | 187 virtual QuicConnectionId GenerateConnectionId(); |
| 194 virtual QuicEpollConnectionHelper* CreateQuicConnectionHelper(); | 188 virtual QuicConnectionHelper* CreateQuicConnectionHelper(); |
| 195 virtual QuicPacketWriter* CreateQuicPacketWriter(); | 189 virtual QuicPacketWriter* CreateQuicPacketWriter(); |
| 196 | 190 |
| 197 virtual int ReadPacket(char* buffer, | |
| 198 int buffer_len, | |
| 199 IPEndPoint* server_address, | |
| 200 IPAddressNumber* client_ip); | |
| 201 | |
| 202 EpollServer* epoll_server() { return epoll_server_; } | |
| 203 | |
| 204 private: | 191 private: |
| 205 friend class net::tools::test::QuicClientPeer; | 192 friend class net::tools::test::QuicClientPeer; |
| 206 | 193 |
| 207 // A packet writer factory that always returns the same writer | 194 // A packet writer factory that always returns the same writer |
| 208 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory { | 195 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory { |
| 209 public: | 196 public: |
| 210 DummyPacketWriterFactory(QuicPacketWriter* writer); | 197 DummyPacketWriterFactory(QuicPacketWriter* writer); |
| 211 ~DummyPacketWriterFactory() override; | 198 ~DummyPacketWriterFactory() override; |
| 212 | 199 |
| 213 QuicPacketWriter* Create(QuicConnection* connection) const override; | 200 QuicPacketWriter* Create(QuicConnection* connection) const override; |
| 214 | 201 |
| 215 private: | 202 private: |
| 216 QuicPacketWriter* writer_; | 203 QuicPacketWriter* writer_; |
| 217 }; | 204 }; |
| 218 | 205 |
| 219 // Used during initialization: creates the UDP socket FD, sets socket options, | 206 // Used during initialization: creates the UDP socket FD, sets socket options, |
| 220 // and binds the socket to our address. | 207 // and binds the socket to our address. |
| 221 bool CreateUDPSocket(); | 208 bool CreateUDPSocket(); |
| 222 | 209 |
| 223 // If the socket has been created, then unregister and close() the FD. | |
| 224 void CleanUpUDPSocket(); | |
| 225 | |
| 226 // Read a UDP packet and hand it to the framer. | 210 // Read a UDP packet and hand it to the framer. |
| 227 bool ReadAndProcessPacket(); | 211 bool ReadAndProcessPacket(); |
| 228 | 212 |
| 213 // Used by |helper_| to time alarms. |
| 214 QuicClock clock_; |
| 215 |
| 229 // Address of the server. | 216 // Address of the server. |
| 230 const IPEndPoint server_address_; | 217 const IPEndPoint server_address_; |
| 231 | 218 |
| 232 // |server_id_| is a tuple (hostname, port, is_https) of the server. | 219 // |server_id_| is a tuple (hostname, port, is_https) of the server. |
| 233 QuicServerId server_id_; | 220 QuicServerId server_id_; |
| 234 | 221 |
| 235 // config_ and crypto_config_ contain configuration and cached state about | 222 // config_ and crypto_config_ contain configuration and cached state about |
| 236 // servers. | 223 // servers. |
| 237 QuicConfig config_; | 224 QuicConfig config_; |
| 238 QuicCryptoClientConfig crypto_config_; | 225 QuicCryptoClientConfig crypto_config_; |
| 239 | 226 |
| 240 // Address of the client if the client is connected to the server. | 227 // Address of the client if the client is connected to the server. |
| 241 IPEndPoint client_address_; | 228 IPEndPoint client_address_; |
| 242 | 229 |
| 243 // If initialized, the address to bind to. | 230 // If initialized, the address to bind to. |
| 244 IPAddressNumber bind_to_address_; | 231 IPAddressNumber bind_to_address_; |
| 245 // Local port to bind to. Initialize to 0. | 232 // Local port to bind to. Initialize to 0. |
| 246 int local_port_; | 233 int local_port_; |
| 247 | 234 |
| 248 // Writer used to actually send packets to the wire. Needs to outlive | 235 // Writer used to actually send packets to the wire. Needs to outlive |
| 249 // |session_|. | 236 // |session_|. |
| 250 scoped_ptr<QuicPacketWriter> writer_; | 237 scoped_ptr<QuicPacketWriter> writer_; |
| 251 | 238 |
| 252 // Session which manages streams. | 239 // Session which manages streams. |
| 253 scoped_ptr<QuicClientSession> session_; | 240 scoped_ptr<QuicSimpleClientSession> session_; |
| 254 // Listens for events on the client socket. | 241 |
| 255 EpollServer* epoll_server_; | 242 // UDP socket connected to the server. |
| 256 // UDP socket. | 243 scoped_ptr<UDPClientSocket> socket_; |
| 257 int fd_; | 244 |
| 245 // Connection on the socket. Owned by |session_|. |
| 246 QuicConnection* connection_; |
| 258 | 247 |
| 259 // Helper to be used by created connections. | 248 // Helper to be used by created connections. |
| 260 scoped_ptr<QuicEpollConnectionHelper> helper_; | 249 scoped_ptr<QuicConnectionHelper> helper_; |
| 261 | 250 |
| 262 // Listens for full responses. | 251 // Listens for full responses. |
| 263 scoped_ptr<ResponseListener> response_listener_; | 252 scoped_ptr<ResponseListener> response_listener_; |
| 264 | 253 |
| 265 // Tracks if the client is initialized to connect. | 254 // Tracks if the client is initialized to connect. |
| 266 bool initialized_; | 255 bool initialized_; |
| 267 | 256 |
| 268 // If overflow_supported_ is true, this will be the number of packets dropped | 257 // If overflow_supported_ is true, this will be the number of packets dropped |
| 269 // during the lifetime of the server. | 258 // during the lifetime of the server. |
| 270 QuicPacketCount packets_dropped_; | 259 QuicPacketCount packets_dropped_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 282 | 271 |
| 283 // If true, store the latest response code, headers, and body. | 272 // If true, store the latest response code, headers, and body. |
| 284 bool store_response_; | 273 bool store_response_; |
| 285 // HTTP response code from most recent response. | 274 // HTTP response code from most recent response. |
| 286 size_t latest_response_code_; | 275 size_t latest_response_code_; |
| 287 // HTTP headers from most recent response. | 276 // HTTP headers from most recent response. |
| 288 std::string latest_response_headers_; | 277 std::string latest_response_headers_; |
| 289 // Body of most recent response. | 278 // Body of most recent response. |
| 290 std::string latest_response_body_; | 279 std::string latest_response_body_; |
| 291 | 280 |
| 292 DISALLOW_COPY_AND_ASSIGN(QuicClient); | 281 bool read_pending_; |
| 282 |
| 283 // The number of iterations of the read loop that have completed synchronously |
| 284 // and without posting a new task to the message loop. |
| 285 int synchronous_read_count_; |
| 286 |
| 287 // The target buffer of the current read. |
| 288 scoped_refptr<IOBufferWithSize> read_buffer_; |
| 289 |
| 290 // The log used for the sockets. |
| 291 NetLog net_log_; |
| 292 |
| 293 base::WeakPtrFactory<QuicSimpleClient> weak_factory_; |
| 294 |
| 295 DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient); |
| 293 }; | 296 }; |
| 294 | 297 |
| 295 } // namespace tools | 298 } // namespace tools |
| 296 } // namespace net | 299 } // namespace net |
| 297 | 300 |
| 298 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_H_ | 301 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_ |
| OLD | NEW |