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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/p2p/socket_host_tcp.h"
#include "content/common/p2p_messages.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/sys_byteorder.h"
#include "net/socket/tcp_client_socket.h"
namespace {
const int kReadBufferSize = 4096;
const int kPacketHeaderSize = sizeof(uint16);
} // namespace
namespace content {
P2PSocketHostTcp::P2PSocketHostTcp(IPC::Message::Sender* message_sender,
int routing_id, int id)
: P2PSocketHost(message_sender, routing_id, id),
connected_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(
connect_callback_(this, &P2PSocketHostTcp::OnConnected)),
ALLOW_THIS_IN_INITIALIZER_LIST(
read_callback_(this, &P2PSocketHostTcp::OnRead)),
ALLOW_THIS_IN_INITIALIZER_LIST(
write_callback_(this, &P2PSocketHostTcp::OnWritten)) {
}
P2PSocketHostTcp::~P2PSocketHostTcp() {
if (state_ == STATE_OPEN) {
DCHECK(socket_.get());
socket_.reset();
}
}
bool P2PSocketHostTcp::InitAccepted(const net::IPEndPoint& remote_address,
net::StreamSocket* socket) {
DCHECK(socket);
DCHECK_EQ(state_, STATE_UNINITIALIZED);
remote_address_ = remote_address;
socket_.reset(socket);
state_ = STATE_OPEN;
DoRead();
return state_ != STATE_ERROR;
}
bool P2PSocketHostTcp::Init(const net::IPEndPoint& local_address,
const net::IPEndPoint& remote_address) {
DCHECK_EQ(state_, STATE_UNINITIALIZED);
remote_address_ = remote_address;
state_ = STATE_CONNECTING;
scoped_ptr<net::TCPClientSocket> tcp_socket(new net::TCPClientSocket(
net::AddressList::CreateFromIPAddress(
remote_address.address(), remote_address.port()),
NULL, net::NetLog::Source()));
if (tcp_socket->Bind(local_address) != net::OK) {
OnError();
return false;
}
socket_.reset(tcp_socket.release());
int result = socket_->Connect(&connect_callback_);
if (result != net::ERR_IO_PENDING) {
OnConnected(result);
}
return state_ != STATE_ERROR;
}
void P2PSocketHostTcp::OnError() {
socket_.reset();
if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING ||
state_ == STATE_OPEN)
message_sender_->Send(new P2PMsg_OnError(routing_id_, id_));
state_ = STATE_ERROR;
}
void P2PSocketHostTcp::OnConnected(int result) {
DCHECK_EQ(state_, STATE_CONNECTING);
DCHECK_NE(result, net::ERR_IO_PENDING);
if (result != net::OK) {
OnError();
return;
}
net::IPEndPoint address;
result = socket_->GetLocalAddress(&address);
if (result < 0) {
LOG(ERROR) << "P2PSocket::Init(): unable to get local address: "
<< result;
OnError();
return;
}
VLOG(1) << "Local address: " << address.ToString();
state_ = STATE_OPEN;
message_sender_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address));
DoRead();
}
void P2PSocketHostTcp::DoRead() {
int result;
do {
if (!read_buffer_) {
read_buffer_ = new net::GrowableIOBuffer();
read_buffer_->SetCapacity(kReadBufferSize);
} else if (read_buffer_->RemainingCapacity() < kReadBufferSize) {
// Make sure that we always have at least kReadBufferSize of
// remaining capacity in the read buffer. Normally all packets
// are smaller than kReadBufferSize, so this is not really
// required.
read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
read_buffer_->RemainingCapacity());
}
result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(),
&read_callback_);
DidCompleteRead(result);
} while (result > 0);
}
void P2PSocketHostTcp::OnRead(int result) {
DidCompleteRead(result);
if (state_ == STATE_OPEN) {
DoRead();
}
}
void P2PSocketHostTcp::OnPacket(std::vector<char>& data) {
if (!connected_) {
P2PSocketHost::StunMessageType type;
bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
if (stun && IsRequestOrResponse(type)) {
connected_ = true;
} else if (!stun || type == STUN_DATA_INDICATION) {
LOG(ERROR) << "Received unexpected data packet from "
<< remote_address_.ToString()
<< " before STUN binding is finished. "
<< "Terminating connection.";
OnError();
return;
}
}
message_sender_->Send(new P2PMsg_OnDataReceived(routing_id_, id_,
remote_address_, data));
}
void P2PSocketHostTcp::DidCompleteRead(int result) {
DCHECK_EQ(state_, STATE_OPEN);
if (result == net::ERR_IO_PENDING) {
return;
} else if (result < 0){
LOG(ERROR) << "Error when reading from TCP socket: " << result;
OnError();
return;
}
read_buffer_->set_offset(read_buffer_->offset() + result);
if (read_buffer_->offset() > kPacketHeaderSize) {
int packet_size =
ntohs(*reinterpret_cast<uint16*>(read_buffer_->StartOfBuffer()));
if (packet_size + kPacketHeaderSize <= read_buffer_->offset()) {
// We've got a full packet!
char* start = read_buffer_->StartOfBuffer() + kPacketHeaderSize;
std::vector<char> data(start, start + packet_size);
OnPacket(data);
// Move remaining data to the start of the buffer.
memmove(read_buffer_->StartOfBuffer(), start + packet_size,
read_buffer_->offset() - packet_size - kPacketHeaderSize);
read_buffer_->set_offset(read_buffer_->offset() - packet_size -
kPacketHeaderSize);
}
}
}
void P2PSocketHostTcp::Send(const net::IPEndPoint& to,
const std::vector<char>& data) {
if (!socket_.get()) {
// The Send message may be sent after the an OnError message was
// sent by hasn't been processed the renderer.
return;
}
if (write_buffer_) {
// Silently drop packet if we haven't finished sending previous
// packet.
VLOG(1) << "Dropping TCP packet.";
return;
}
if (!(to == remote_address_)) {
// Renderer should use this socket only to send data to
// |remote_address_|.
NOTREACHED();
OnError();
return;
}
if (!connected_) {
P2PSocketHost::StunMessageType type;
bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
if (!stun || type == STUN_DATA_INDICATION) {
LOG(ERROR) << "Page tried to send a data packet to " << to.ToString()
<< " before STUN binding is finished.";
OnError();
return;
}
}
int size = kPacketHeaderSize + data.size();
write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size);
*reinterpret_cast<uint16*>(write_buffer_->data()) = htons(data.size());
memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size());
DoWrite();
}
void P2PSocketHostTcp::DoWrite() {
while (true) {
int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(),
&write_callback_);
if (result >= 0) {
write_buffer_->DidConsume(result);
if (write_buffer_->BytesRemaining() == 0) {
write_buffer_ = NULL;
break;
}
} else {
if (result != net::ERR_IO_PENDING) {
LOG(ERROR) << "Error when sending data in TCP socket: " << result;
OnError();
}
break;
}
}
}
void P2PSocketHostTcp::OnWritten(int result) {
DCHECK(write_buffer_);
DCHECK_NE(result, net::ERR_IO_PENDING);
if (result < 0) {
DCHECK(result != net::ERR_IO_PENDING);
LOG(ERROR) << "Error when sending data in TCP socket: " << result;
OnError();
return;
}
write_buffer_->DidConsume(result);
if (write_buffer_->BytesRemaining() == 0) {
write_buffer_ = NULL;
} else {
DoWrite();
}
}
P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection(
const net::IPEndPoint& remote_address, int id) {
NOTREACHED();
OnError();
return NULL;
}
} // namespace content
|