-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquic-client.cc
More file actions
320 lines (271 loc) · 9.6 KB
/
quic-client.cc
File metadata and controls
320 lines (271 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright 2017 CCSL IME-USP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Diego Araújo <diegoamc@ime.usp.br>
*/
#include "ns3/address.h"
#include "ns3/address-utils.h"
#include "ns3/log.h"
#include "ns3/inet-socket-address.h"
#include "ns3/inet6-socket-address.h"
#include "ns3/packet-socket-address.h"
#include "ns3/node.h"
#include "ns3/socket.h"
#include "ns3/udp-socket.h"
#include "ns3/simulator.h"
#include "ns3/socket-factory.h"
#include "ns3/packet.h"
#include "ns3/boolean.h"
#include "ns3/trace-source-accessor.h"
#include "ns3/uinteger.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/quic-header.h"
#include "ns3/quic-stream-frame.h"
#include "quic-client.h"
#include "net/spdy/core/spdy_header_block.h"
#include "net/quic/platform/api/quic_text_utils.h"
#include "net/tools/quic/quic_simple_client.h"
#include "net/tools/quic/quic_client_message_loop_network_helper.h"
using std::string;
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
using std::stringstream;
namespace ns3 {
Time QuicClient::last_time;
namespace {
using net::ProofVerifier;
class FakeProofVerifier : public ProofVerifier {
public:
net::QuicAsyncStatus VerifyProof(
const string& /*hostname*/,
const uint16_t /*port*/,
const string& /*server_config*/,
net::QuicVersion /*quic_version*/,
net::QuicStringPiece /*chlo_hash*/,
const std::vector<string>& /*certs*/,
const string& /*cert_sct*/,
const string& /*signature*/,
const net::ProofVerifyContext* /*context*/,
string* /*error_details*/,
std::unique_ptr<net::ProofVerifyDetails>* /*details*/,
std::unique_ptr<net::ProofVerifierCallback> /*callback*/) override {
return net::QUIC_SUCCESS;
}
net::QuicAsyncStatus VerifyCertChain(
const std::string& /*hostname*/,
const std::vector<std::string>& /*certs*/,
const net::ProofVerifyContext* /*verify_context*/,
std::string* /*error_details*/,
std::unique_ptr<net::ProofVerifyDetails>* /*verify_details*/,
std::unique_ptr<net::ProofVerifierCallback> /*callback*/) override {
return net::QUIC_SUCCESS;
}
};
} // namespace
NS_LOG_COMPONENT_DEFINE ("QuicClient");
NS_OBJECT_ENSURE_REGISTERED (QuicClient);
TypeId
QuicClient::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::QuicClient")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<QuicClient> ()
.AddAttribute ("Protocol",
"The type id of the protocol to use for the rx socket.",
TypeIdValue (UdpSocketFactory::GetTypeId ()),
MakeTypeIdAccessor (&QuicClient::m_tid),
MakeTypeIdChecker ())
.AddAttribute ("ServerAddress",
"The Address on which to connect the rx socket.",
AddressValue (),
MakeAddressAccessor (&QuicClient::m_serverAddress),
MakeAddressChecker ())
.AddAttribute ("ZeroRttHandshake",
"Whether or not to initiate a 0-RTT connection.",
BooleanValue (true),
MakeBooleanAccessor (&QuicClient::m_zeroRtt),
MakeBooleanChecker ())
.AddTraceSource ("Rx",
"A packet has been received",
MakeTraceSourceAccessor (&QuicClient::m_rxTrace),
"ns3::Packet::AddressTracedCallback")
.AddAttribute ("MaxBytes",
"Number of bytes to send.",
UintegerValue (10),
ns3::MakeUintegerAccessor (&QuicClient::m_maxBytes),
ns3::MakeUintegerChecker<unsigned> (uint64_t(1)))
;
return tid;
}
QuicClient::QuicClient ()
{
NS_LOG_FUNCTION (this);
m_socket = 0;
m_totalRx = 0;
}
QuicClient::~QuicClient ()
{
NS_LOG_FUNCTION (this);
}
uint64_t QuicClient::GetTotalRx () const
{
NS_LOG_FUNCTION (this);
return m_totalRx;
}
Ptr<Socket>
QuicClient::GetListeningSocket (void) const
{
NS_LOG_FUNCTION (this);
return m_socket;
}
void QuicClient::DoDispose (void)
{
NS_LOG_FUNCTION (this);
m_socket = 0;
// chain up
Application::DoDispose ();
}
base::AtExitManager *QuicClient::exit_manager = nullptr;
base::MessageLoopForIO *QuicClient::message_loop = nullptr;
// Application Methods
void QuicClient::StartApplication () // Called at time specified by Start
{
NS_LOG_FUNCTION (this);
//cerr << "Client Start (MaxBytes " << m_maxBytes << ")" << endl;
cur_state = CONNECT_LOOP;
kCurNode = GetNode();
if(!exit_manager) exit_manager = new base::AtExitManager;
if(!message_loop) message_loop = new base::MessageLoopForIO;
net::QuicIpAddress ip_addr;
uint8_t buffer_tmp[Address::MAX_SIZE];
int len = m_serverAddress.CopyTo(buffer_tmp);
stringstream str;
for(int i = 0; i < 4; i++) {
str << int(buffer_tmp[i]);
if(i < 3) str << '.';
}
string buffer = str.str();
if(!ip_addr.FromString(buffer)) {
exit(13);
}
InetSocketAddress addr = InetSocketAddress::ConvertFrom(m_serverAddress);
net::QuicServerId server_id(buffer, addr.GetPort(), net::PRIVACY_MODE_DISABLED);
net::QuicVersionVector versions = net::AllSupportedVersions();
std::unique_ptr<ProofVerifier> proof_verifier;
proof_verifier.reset(new FakeProofVerifier());
client = new net::QuicSimpleClient(net::QuicSocketAddress(ip_addr, addr.GetPort()), server_id, versions, std::move(proof_verifier));
client->set_initial_max_packet_length(net::kDefaultMaxPacketSize);
if(!client->Initialize()) {
cerr << "FAIL" << endl;
exit(1);
}
dynamic_cast<net::QuicClientMessageLooplNetworkHelper*>(client->network_helper())->packet_reader_->client_ = this;
client->Connect();
}
void QuicClient::StopApplication () // Called at time specified by Stop
{
NS_LOG_FUNCTION (this);
if (m_socket)
{
m_socket->Close ();
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
}
}
void QuicClient::SendRequest() {
using net::SpdyHeaderBlock;
// Construct the string body from flags, if provided.
string body = "";
// Construct a GET or POST request for supplied URL.
SpdyHeaderBlock header_block;
header_block["max_bytes"] = net::QuicTextUtils::Uint64ToString(m_maxBytes);
// Make sure to store the response, for later output.
client->set_store_response(true);
// Send the request.
if(client->SendRequestAndWaitForResponse(header_block, body, /*fin=*/true)){
cur_state = SEND_REQUEST;
// std::cout << "SEND_REQUEST" << std::endl;
}
else{
cur_state = AFTER;
// std::cout << "AFTER" << std::endl;
}
}
void QuicClient::HandleRead (Ptr<Socket> socket)
{
// std::cout << "HandleRead" << std::endl;
socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket>>());
NS_LOG_FUNCTION (this << socket);
//cerr << "QuicClient::HandleRead()" << endl;
last_time = Simulator::Now();
net::QuicChromiumPacketReader *pktrd = dynamic_cast<net::QuicClientMessageLooplNetworkHelper*>(client->network_helper())->packet_reader_.get();
int ret = socket->Recv(reinterpret_cast<uint8_t*>(pktrd->read_buffer_->data()), pktrd->read_buffer_->size(), 0);
//cerr << "Read " << ret << " bytes (return val " << ret << ")" << endl;
pktrd->OnReadComplete(ret);
if(cur_state == CONNECT_LOOP) {
if (client->EncryptionBeingEstablished())
client->WaitForEvents();
else {
if (client->ConnectLogic())
client->Connect();
else {
client->FinishConnect();
SendRequest();
}
}
} else if(cur_state == SEND_REQUEST) {
if(!client->WaitForEvents())
cur_state = AFTER;
}
}
void
QuicClient::HandleSucessfulConnection (Ptr<Socket> socket) // not work ??
{
NS_LOG_FUNCTION (this << socket);
NS_LOG_INFO ("Connection suceeded. Time to start the QUIC handshake.");
Ptr<Packet> packet = Create<Packet> ();
QuicStreamFrame streamFrame;
streamFrame.SetFrameType (193);
streamFrame.SetStreamId (0);
streamFrame.SetOffset (0);
streamFrame.SetDataLength (1);
streamFrame.SetStreamData (1);
packet->AddHeader (streamFrame);
LongQuicHeader longHeader;
if (m_zeroRtt)
{
NS_LOG_INFO ("0-RTT Handshake.");
longHeader.SetLongPacketType (LongQuicHeader::CCLT);
}
else
{
NS_LOG_INFO ("1-RTT Handshake.");
longHeader.SetLongPacketType (LongQuicHeader::CLIN);
}
packet->AddHeader (longHeader);
// std::cout << "hello,world!" << std::endl;
m_socket->Send (packet);
}
void
QuicClient::HandleFailedConnection (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
NS_LOG_INFO ("Connection failed. There is nothing left to do.");
}
} // Namespace ns3