blob: 50ba040f8a610c3768aba2ab8f147bf21a153364 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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#include "net/third_party/quiche/src/quic/tools/quic_client.h"
6
7#include <errno.h>
nharper2eb3f382019-07-24 14:12:31 -07008#include <netdb.h>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <netinet/in.h>
10#include <string.h>
11#include <sys/epoll.h>
12#include <sys/socket.h>
nharper2eb3f382019-07-24 14:12:31 -070013#include <sys/types.h>
QUICHE teama6ef0a62019-03-07 20:34:33 -050014
bnc463f2352019-10-10 04:49:34 -070015#include <utility>
16
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
18#include "net/third_party/quiche/src/quic/core/http/spdy_utils.h"
19#include "net/third_party/quiche/src/quic/core/quic_connection.h"
20#include "net/third_party/quiche/src/quic/core/quic_data_reader.h"
21#include "net/third_party/quiche/src/quic/core/quic_epoll_alarm_factory.h"
22#include "net/third_party/quiche/src/quic/core/quic_epoll_connection_helper.h"
23#include "net/third_party/quiche/src/quic/core/quic_packets.h"
24#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
nharper2eb3f382019-07-24 14:12:31 -070028#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050029#include "net/third_party/quiche/src/quic/tools/quic_simple_client_session.h"
30
QUICHE teama6ef0a62019-03-07 20:34:33 -050031namespace quic {
32
nharper2eb3f382019-07-24 14:12:31 -070033namespace tools {
34
35QuicSocketAddress LookupAddress(std::string host, std::string port) {
36 addrinfo hint;
37 memset(&hint, 0, sizeof(hint));
38 hint.ai_protocol = IPPROTO_UDP;
39
40 addrinfo* info_list = nullptr;
41 int result = getaddrinfo(host.c_str(), port.c_str(), &hint, &info_list);
42 if (result != 0) {
43 QUIC_LOG(ERROR) << "Failed to look up " << host << ": "
44 << gai_strerror(result);
45 return QuicSocketAddress();
46 }
47
48 CHECK(info_list != nullptr);
49 std::unique_ptr<addrinfo, void (*)(addrinfo*)> info_list_owned(info_list,
50 freeaddrinfo);
51 return QuicSocketAddress(info_list->ai_addr, info_list->ai_addrlen);
52}
53
54} // namespace tools
55
QUICHE teama6ef0a62019-03-07 20:34:33 -050056QuicClient::QuicClient(QuicSocketAddress server_address,
57 const QuicServerId& server_id,
58 const ParsedQuicVersionVector& supported_versions,
59 QuicEpollServer* epoll_server,
60 std::unique_ptr<ProofVerifier> proof_verifier)
61 : QuicClient(
62 server_address,
63 server_id,
64 supported_versions,
65 QuicConfig(),
66 epoll_server,
67 QuicWrapUnique(new QuicClientEpollNetworkHelper(epoll_server, this)),
nharper41ac9df2019-11-11 15:09:23 -080068 std::move(proof_verifier),
69 nullptr) {}
70
71QuicClient::QuicClient(QuicSocketAddress server_address,
72 const QuicServerId& server_id,
73 const ParsedQuicVersionVector& supported_versions,
74 QuicEpollServer* epoll_server,
75 std::unique_ptr<ProofVerifier> proof_verifier,
76 std::unique_ptr<SessionCache> session_cache)
77 : QuicClient(
78 server_address,
79 server_id,
80 supported_versions,
81 QuicConfig(),
82 epoll_server,
83 QuicWrapUnique(new QuicClientEpollNetworkHelper(epoll_server, this)),
84 std::move(proof_verifier),
85 std::move(session_cache)) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050086
87QuicClient::QuicClient(
88 QuicSocketAddress server_address,
89 const QuicServerId& server_id,
90 const ParsedQuicVersionVector& supported_versions,
91 QuicEpollServer* epoll_server,
92 std::unique_ptr<QuicClientEpollNetworkHelper> network_helper,
93 std::unique_ptr<ProofVerifier> proof_verifier)
94 : QuicClient(server_address,
95 server_id,
96 supported_versions,
97 QuicConfig(),
98 epoll_server,
99 std::move(network_helper),
nharper41ac9df2019-11-11 15:09:23 -0800100 std::move(proof_verifier),
101 nullptr) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102
103QuicClient::QuicClient(
104 QuicSocketAddress server_address,
105 const QuicServerId& server_id,
106 const ParsedQuicVersionVector& supported_versions,
107 const QuicConfig& config,
108 QuicEpollServer* epoll_server,
109 std::unique_ptr<QuicClientEpollNetworkHelper> network_helper,
110 std::unique_ptr<ProofVerifier> proof_verifier)
nharper41ac9df2019-11-11 15:09:23 -0800111 : QuicClient(server_address,
112 server_id,
113 supported_versions,
114 config,
115 epoll_server,
116 std::move(network_helper),
117 std::move(proof_verifier),
118 nullptr) {}
119
120QuicClient::QuicClient(
121 QuicSocketAddress server_address,
122 const QuicServerId& server_id,
123 const ParsedQuicVersionVector& supported_versions,
124 const QuicConfig& config,
125 QuicEpollServer* epoll_server,
126 std::unique_ptr<QuicClientEpollNetworkHelper> network_helper,
127 std::unique_ptr<ProofVerifier> proof_verifier,
128 std::unique_ptr<SessionCache> session_cache)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 : QuicSpdyClientBase(
130 server_id,
131 supported_versions,
132 config,
133 new QuicEpollConnectionHelper(epoll_server, QuicAllocator::SIMPLE),
134 new QuicEpollAlarmFactory(epoll_server),
135 std::move(network_helper),
nharper41ac9df2019-11-11 15:09:23 -0800136 std::move(proof_verifier),
137 std::move(session_cache)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 set_server_address(server_address);
139}
140
141QuicClient::~QuicClient() = default;
142
143std::unique_ptr<QuicSession> QuicClient::CreateQuicClientSession(
144 const ParsedQuicVersionVector& supported_versions,
145 QuicConnection* connection) {
vasilvv0fc587f2019-09-06 13:33:08 -0700146 return std::make_unique<QuicSimpleClientSession>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 *config(), supported_versions, connection, server_id(), crypto_config(),
rchd362a7a2019-05-15 17:19:26 -0700148 push_promise_index(), drop_response_body());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500149}
150
151QuicClientEpollNetworkHelper* QuicClient::epoll_network_helper() {
152 return static_cast<QuicClientEpollNetworkHelper*>(network_helper());
153}
154
155const QuicClientEpollNetworkHelper* QuicClient::epoll_network_helper() const {
156 return static_cast<const QuicClientEpollNetworkHelper*>(network_helper());
157}
158
159} // namespace quic