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 #include "net/tools/quic/quic_client.h"
8 #include <netinet/in.h>
10 #include <sys/epoll.h>
11 #include <sys/socket.h>
14 #include "base/logging.h"
15 #include "net/quic/crypto/quic_random.h"
16 #include "net/quic/quic_connection.h"
17 #include "net/quic/quic_data_reader.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/tools/balsa/balsa_headers.h"
20 #include "net/tools/quic/quic_epoll_connection_helper.h"
21 #include "net/tools/quic/quic_socket_utils.h"
22 #include "net/tools/quic/quic_spdy_client_stream.h"
25 #define SO_RXQ_OVFL 40
31 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET;
33 QuicClient::QuicClient(IPEndPoint server_address,
34 const string& server_hostname,
35 const QuicVersionVector& supported_versions,
37 : server_address_(server_address),
38 server_hostname_(server_hostname),
41 helper_(CreateQuicConnectionHelper()),
44 overflow_supported_(false),
45 supported_versions_(supported_versions),
46 print_response_(print_response) {
47 config_.SetDefaults();
50 QuicClient::QuicClient(IPEndPoint server_address,
51 const string& server_hostname,
52 const QuicConfig& config,
53 const QuicVersionVector& supported_versions)
54 : server_address_(server_address),
55 server_hostname_(server_hostname),
59 helper_(CreateQuicConnectionHelper()),
62 overflow_supported_(false),
63 supported_versions_(supported_versions),
64 print_response_(false) {
67 QuicClient::~QuicClient() {
69 session()->connection()->SendConnectionClosePacket(
70 QUIC_PEER_GOING_AWAY, "");
74 bool QuicClient::Initialize() {
75 DCHECK(!initialized_);
77 epoll_server_.set_timeout_in_us(50 * 1000);
78 crypto_config_.SetDefaults();
80 int address_family = server_address_.GetSockAddrFamily();
81 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
83 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno);
88 int rc = setsockopt(fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow,
89 sizeof(get_overflow));
91 DLOG(WARNING) << "Socket overflow detection not supported";
93 overflow_supported_ = true;
97 if (address_family == AF_INET) {
98 rc = setsockopt(fd_, IPPROTO_IP, IP_PKTINFO,
99 &get_local_ip, sizeof(get_local_ip));
101 rc = setsockopt(fd_, IPPROTO_IPV6, IPV6_RECVPKTINFO,
102 &get_local_ip, sizeof(get_local_ip));
106 LOG(ERROR) << "IP detection not supported" << strerror(errno);
110 if (bind_to_address_.size() != 0) {
111 client_address_ = IPEndPoint(bind_to_address_, local_port_);
112 } else if (address_family == AF_INET) {
113 IPAddressNumber any4;
114 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4));
115 client_address_ = IPEndPoint(any4, local_port_);
117 IPAddressNumber any6;
118 CHECK(net::ParseIPLiteralToNumber("::", &any6));
119 client_address_ = IPEndPoint(any6, local_port_);
122 sockaddr_storage raw_addr;
123 socklen_t raw_addr_len = sizeof(raw_addr);
124 CHECK(client_address_.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr),
127 reinterpret_cast<const sockaddr*>(&raw_addr),
130 LOG(ERROR) << "Bind failed: " << strerror(errno);
134 SockaddrStorage storage;
135 if (getsockname(fd_, storage.addr, &storage.addr_len) != 0 ||
136 !client_address_.FromSockAddr(storage.addr, storage.addr_len)) {
137 LOG(ERROR) << "Unable to get self address. Error: " << strerror(errno);
140 epoll_server_.RegisterFD(fd_, this, kEpollFlags);
145 bool QuicClient::Connect() {
146 if (!StartConnect()) {
149 while (EncryptionBeingEstablished()) {
152 return session_->connection()->connected();
155 bool QuicClient::StartConnect() {
156 DCHECK(initialized_);
157 DCHECK(!connected());
159 QuicPacketWriter* writer = CreateQuicPacketWriter();
160 if (writer_.get() != writer) {
161 writer_.reset(writer);
164 session_.reset(new QuicClientSession(
167 new QuicConnection(GenerateGuid(), server_address_, helper_.get(),
168 writer_.get(), false, supported_versions_),
170 return session_->CryptoConnect();
173 bool QuicClient::EncryptionBeingEstablished() {
174 return !session_->IsEncryptionEstablished() &&
175 session_->connection()->connected();
178 void QuicClient::Disconnect() {
179 DCHECK(initialized_);
182 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY);
184 epoll_server_.UnregisterFD(fd_);
187 initialized_ = false;
190 void QuicClient::SendRequestsAndWaitForResponse(
191 const CommandLine::StringVector& args) {
192 for (size_t i = 0; i < args.size(); ++i) {
193 BalsaHeaders headers;
194 headers.SetRequestFirstlineFromStringPieces("GET", args[i], "HTTP/1.1");
195 QuicSpdyClientStream* stream = CreateReliableClientStream();
196 stream->SendRequest(headers, "", true);
197 stream->set_visitor(this);
200 while (WaitForEvents()) { }
203 QuicSpdyClientStream* QuicClient::CreateReliableClientStream() {
208 return session_->CreateOutgoingDataStream();
211 void QuicClient::WaitForStreamToClose(QuicStreamId id) {
214 while (!session_->IsClosedStream(id)) {
215 epoll_server_.WaitForEventsAndExecuteCallbacks();
219 void QuicClient::WaitForCryptoHandshakeConfirmed() {
222 while (!session_->IsCryptoHandshakeConfirmed()) {
223 epoll_server_.WaitForEventsAndExecuteCallbacks();
227 bool QuicClient::WaitForEvents() {
230 epoll_server_.WaitForEventsAndExecuteCallbacks();
231 return session_->num_active_requests() != 0;
234 void QuicClient::OnEvent(int fd, EpollEvent* event) {
237 if (event->in_events & EPOLLIN) {
238 while (connected() && ReadAndProcessPacket()) {
241 if (connected() && (event->in_events & EPOLLOUT)) {
242 writer_->SetWritable();
243 session_->connection()->OnCanWrite();
245 if (event->in_events & EPOLLERR) {
246 DVLOG(1) << "Epollerr";
250 void QuicClient::OnClose(QuicDataStream* stream) {
251 QuicSpdyClientStream* client_stream =
252 static_cast<QuicSpdyClientStream*>(stream);
253 if (response_listener_.get() != NULL) {
254 response_listener_->OnCompleteResponse(
255 stream->id(), client_stream->headers(), client_stream->data());
258 if (!print_response_) {
262 const BalsaHeaders& headers = client_stream->headers();
263 printf("%s\n", headers.first_line().as_string().c_str());
264 for (BalsaHeaders::const_header_lines_iterator i =
265 headers.header_lines_begin();
266 i != headers.header_lines_end(); ++i) {
267 printf("%s: %s\n", i->first.as_string().c_str(),
268 i->second.as_string().c_str());
270 printf("%s\n", client_stream->data().c_str());
273 QuicPacketCreator::Options* QuicClient::options() {
274 if (session() == NULL) {
277 return session_->options();
280 bool QuicClient::connected() const {
281 return session_.get() && session_->connection() &&
282 session_->connection()->connected();
285 QuicGuid QuicClient::GenerateGuid() {
286 return QuicRandom::GetInstance()->RandUint64();
289 QuicEpollConnectionHelper* QuicClient::CreateQuicConnectionHelper() {
290 return new QuicEpollConnectionHelper(&epoll_server_);
293 QuicPacketWriter* QuicClient::CreateQuicPacketWriter() {
294 return new QuicDefaultPacketWriter(fd_);
297 bool QuicClient::ReadAndProcessPacket() {
298 // Allocate some extra space so we can send an error if the server goes over
300 char buf[2 * kMaxPacketSize];
302 IPEndPoint server_address;
303 IPAddressNumber client_ip;
305 int bytes_read = QuicSocketUtils::ReadPacket(
306 fd_, buf, arraysize(buf), overflow_supported_ ? &packets_dropped_ : NULL,
307 &client_ip, &server_address);
309 if (bytes_read < 0) {
313 QuicEncryptedPacket packet(buf, bytes_read, false);
315 IPEndPoint client_address(client_ip, client_address_.port());
316 session_->connection()->ProcessUdpPacket(
317 client_address, server_address, packet);