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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
// Copyright (c) 2006-2008 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 "net/socket/tcp_client_socket_libevent.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include "base/eintr_wrapper.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/trace_event.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#if defined(USE_SYSTEM_LIBEVENT)
#include <event.h>
#else
#include "third_party/libevent/event.h"
#endif
namespace net {
namespace {
const int kInvalidSocket = -1;
// Return 0 on success, -1 on failure.
// Too small a function to bother putting in a library?
int SetNonBlocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (-1 == flags)
return flags;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
// DisableNagle turns off buffering in the kernel. By default, TCP sockets will
// wait up to 200ms for more data to complete a packet before transmitting.
// After calling this function, the kernel will not wait. See TCP_NODELAY in
// `man 7 tcp`.
int DisableNagle(int fd) {
int on = 1;
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
}
// Convert values from <errno.h> to values from "net/base/net_errors.h"
int MapPosixError(int os_error) {
// There are numerous posix error codes, but these are the ones we thus far
// find interesting.
switch (os_error) {
case EAGAIN:
#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
return ERR_IO_PENDING;
case EACCES:
return ERR_ACCESS_DENIED;
case ENETDOWN:
return ERR_INTERNET_DISCONNECTED;
case ETIMEDOUT:
return ERR_TIMED_OUT;
case ECONNRESET:
case ENETRESET: // Related to keep-alive
case EPIPE:
return ERR_CONNECTION_RESET;
case ECONNABORTED:
return ERR_CONNECTION_ABORTED;
case ECONNREFUSED:
return ERR_CONNECTION_REFUSED;
case EHOSTUNREACH:
case ENETUNREACH:
return ERR_ADDRESS_UNREACHABLE;
case EADDRNOTAVAIL:
return ERR_ADDRESS_INVALID;
case 0:
return OK;
default:
LOG(WARNING) << "Unknown error " << os_error
<< " mapped to net::ERR_FAILED";
return ERR_FAILED;
}
}
int MapConnectError(int os_error) {
switch (os_error) {
case ETIMEDOUT:
return ERR_CONNECTION_TIMED_OUT;
default: {
int net_error = MapPosixError(os_error);
if (net_error == ERR_FAILED)
return ERR_CONNECTION_FAILED; // More specific than ERR_FAILED.
return net_error;
}
}
}
// Given os_error, an errno from a connect() attempt, returns true if
// connect() should be retried with another address.
bool ShouldTryNextAddress(int os_error) {
switch (os_error) {
case EADDRNOTAVAIL:
case EAFNOSUPPORT:
case ECONNREFUSED:
case ECONNRESET:
case EACCES:
case EPERM:
case ENETUNREACH:
case EHOSTUNREACH:
case ENETDOWN:
case ETIMEDOUT:
return true;
default:
return false;
}
}
} // namespace
//-----------------------------------------------------------------------------
TCPClientSocketLibevent::TCPClientSocketLibevent(const AddressList& addresses)
: socket_(kInvalidSocket),
addresses_(addresses),
current_ai_(addresses_.head()),
waiting_connect_(false),
read_watcher_(this),
write_watcher_(this),
read_callback_(NULL),
write_callback_(NULL) {
}
TCPClientSocketLibevent::~TCPClientSocketLibevent() {
Disconnect();
}
int TCPClientSocketLibevent::Connect(CompletionCallback* callback,
const BoundNetLog& net_log) {
// If already connected, then just return OK.
if (socket_ != kInvalidSocket)
return OK;
DCHECK(!waiting_connect_);
DCHECK(!net_log_.net_log());
TRACE_EVENT_BEGIN("socket.connect", this, "");
net_log.BeginEvent(NetLog::TYPE_TCP_CONNECT);
int rv = DoConnect();
if (rv == ERR_IO_PENDING) {
// Synchronous operation not supported.
DCHECK(callback);
net_log_ = net_log;
waiting_connect_ = true;
write_callback_ = callback;
} else {
TRACE_EVENT_END("socket.connect", this, "");
net_log.EndEvent(NetLog::TYPE_TCP_CONNECT);
}
return rv;
}
int TCPClientSocketLibevent::DoConnect() {
while (true) {
DCHECK(current_ai_);
int rv = CreateSocket(current_ai_);
if (rv != OK)
return rv;
if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr,
static_cast<int>(current_ai_->ai_addrlen)))) {
// Connected without waiting!
return OK;
}
int os_error = errno;
if (os_error == EINPROGRESS)
break;
close(socket_);
socket_ = kInvalidSocket;
if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) {
// connect() can fail synchronously for an address even on a
// non-blocking socket. As an example, this can happen when there is
// no route to the host. Retry using the next address in the list.
current_ai_ = current_ai_->ai_next;
} else {
DLOG(INFO) << "connect failed: " << os_error;
return MapConnectError(os_error);
}
}
// Initialize write_socket_watcher_ and link it to our MessagePump.
// POLLOUT is set if the connection is established.
// POLLIN is set if the connection fails.
if (!MessageLoopForIO::current()->WatchFileDescriptor(
socket_, true, MessageLoopForIO::WATCH_WRITE, &write_socket_watcher_,
&write_watcher_)) {
DLOG(INFO) << "WatchFileDescriptor failed: " << errno;
close(socket_);
socket_ = kInvalidSocket;
return MapPosixError(errno);
}
return ERR_IO_PENDING;
}
void TCPClientSocketLibevent::Disconnect() {
if (socket_ == kInvalidSocket)
return;
TRACE_EVENT_INSTANT("socket.disconnect", this, "");
bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
ok = write_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
close(socket_);
socket_ = kInvalidSocket;
waiting_connect_ = false;
// Reset for next time.
current_ai_ = addresses_.head();
}
bool TCPClientSocketLibevent::IsConnected() const {
if (socket_ == kInvalidSocket || waiting_connect_)
return false;
// Check if connection is alive.
char c;
int rv = HANDLE_EINTR(recv(socket_, &c, 1, MSG_PEEK));
if (rv == 0)
return false;
if (rv == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
return false;
return true;
}
bool TCPClientSocketLibevent::IsConnectedAndIdle() const {
if (socket_ == kInvalidSocket || waiting_connect_)
return false;
// Check if connection is alive and we haven't received any data
// unexpectedly.
char c;
int rv = HANDLE_EINTR(recv(socket_, &c, 1, MSG_PEEK));
if (rv >= 0)
return false;
if (errno != EAGAIN && errno != EWOULDBLOCK)
return false;
return true;
}
int TCPClientSocketLibevent::Read(IOBuffer* buf,
int buf_len,
CompletionCallback* callback) {
DCHECK_NE(kInvalidSocket, socket_);
DCHECK(!waiting_connect_);
DCHECK(!read_callback_);
// Synchronous operation not supported
DCHECK(callback);
DCHECK_GT(buf_len, 0);
TRACE_EVENT_BEGIN("socket.read", this, "");
int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len));
if (nread >= 0) {
TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", nread));
return nread;
}
if (errno != EAGAIN && errno != EWOULDBLOCK) {
DLOG(INFO) << "read failed, errno " << errno;
return MapPosixError(errno);
}
if (!MessageLoopForIO::current()->WatchFileDescriptor(
socket_, true, MessageLoopForIO::WATCH_READ,
&read_socket_watcher_, &read_watcher_)) {
DLOG(INFO) << "WatchFileDescriptor failed on read, errno " << errno;
return MapPosixError(errno);
}
read_buf_ = buf;
read_buf_len_ = buf_len;
read_callback_ = callback;
return ERR_IO_PENDING;
}
int TCPClientSocketLibevent::Write(IOBuffer* buf,
int buf_len,
CompletionCallback* callback) {
DCHECK_NE(kInvalidSocket, socket_);
DCHECK(!waiting_connect_);
DCHECK(!write_callback_);
// Synchronous operation not supported
DCHECK(callback);
DCHECK_GT(buf_len, 0);
TRACE_EVENT_BEGIN("socket.write", this, "");
int nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len));
if (nwrite >= 0) {
TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", nwrite));
return nwrite;
}
if (errno != EAGAIN && errno != EWOULDBLOCK)
return MapPosixError(errno);
if (!MessageLoopForIO::current()->WatchFileDescriptor(
socket_, true, MessageLoopForIO::WATCH_WRITE,
&write_socket_watcher_, &write_watcher_)) {
DLOG(INFO) << "WatchFileDescriptor failed on write, errno " << errno;
return MapPosixError(errno);
}
write_buf_ = buf;
write_buf_len_ = buf_len;
write_callback_ = callback;
return ERR_IO_PENDING;
}
bool TCPClientSocketLibevent::SetReceiveBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&size),
sizeof(size));
DCHECK(!rv) << "Could not set socket receive buffer size: " << errno;
return rv == 0;
}
bool TCPClientSocketLibevent::SetSendBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&size),
sizeof(size));
DCHECK(!rv) << "Could not set socket send buffer size: " << errno;
return rv == 0;
}
int TCPClientSocketLibevent::CreateSocket(const addrinfo* ai) {
socket_ = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (socket_ == kInvalidSocket)
return MapPosixError(errno);
if (SetNonBlocking(socket_)) {
const int err = MapPosixError(errno);
close(socket_);
socket_ = kInvalidSocket;
return err;
}
// This mirrors the behaviour on Windows. See the comment in
// tcp_client_socket_win.cc after searching for "NODELAY".
DisableNagle(socket_); // If DisableNagle fails, we don't care.
return OK;
}
void TCPClientSocketLibevent::DoReadCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(read_callback_);
// since Run may result in Read being called, clear read_callback_ up front.
CompletionCallback* c = read_callback_;
read_callback_ = NULL;
c->Run(rv);
}
void TCPClientSocketLibevent::DoWriteCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(write_callback_);
// since Run may result in Write being called, clear write_callback_ up front.
CompletionCallback* c = write_callback_;
write_callback_ = NULL;
c->Run(rv);
}
void TCPClientSocketLibevent::DidCompleteConnect() {
int result = ERR_UNEXPECTED;
// Check to see if connect succeeded
int os_error = 0;
socklen_t len = sizeof(os_error);
if (getsockopt(socket_, SOL_SOCKET, SO_ERROR, &os_error, &len) < 0)
os_error = errno;
if (os_error == EINPROGRESS || os_error == EALREADY) {
NOTREACHED(); // This indicates a bug in libevent or our code.
result = ERR_IO_PENDING;
} else if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) {
// This address failed, try next one in list.
const addrinfo* next = current_ai_->ai_next;
Disconnect();
current_ai_ = next;
BoundNetLog net_log = net_log_;
net_log_ = BoundNetLog();
TRACE_EVENT_END("socket.connect", this, "");
net_log.EndEvent(NetLog::TYPE_TCP_CONNECT);
result = Connect(write_callback_, net_log);
} else {
result = MapConnectError(os_error);
bool ok = write_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
waiting_connect_ = false;
TRACE_EVENT_END("socket.connect", this, "");
net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT);
net_log_ = BoundNetLog();
}
if (result != ERR_IO_PENDING) {
DoWriteCallback(result);
}
}
void TCPClientSocketLibevent::DidCompleteRead() {
int bytes_transferred;
bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(),
read_buf_len_));
int result;
if (bytes_transferred >= 0) {
TRACE_EVENT_END("socket.read", this,
StringPrintf("%d bytes", bytes_transferred));
result = bytes_transferred;
} else {
result = MapPosixError(errno);
}
if (result != ERR_IO_PENDING) {
read_buf_ = NULL;
read_buf_len_ = 0;
bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
DoReadCallback(result);
}
}
void TCPClientSocketLibevent::DidCompleteWrite() {
int bytes_transferred;
bytes_transferred = HANDLE_EINTR(write(socket_, write_buf_->data(),
write_buf_len_));
int result;
if (bytes_transferred >= 0) {
result = bytes_transferred;
TRACE_EVENT_END("socket.write", this,
StringPrintf("%d bytes", bytes_transferred));
} else {
result = MapPosixError(errno);
}
if (result != ERR_IO_PENDING) {
write_buf_ = NULL;
write_buf_len_ = 0;
write_socket_watcher_.StopWatchingFileDescriptor();
DoWriteCallback(result);
}
}
int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const {
DCHECK(address);
if (!current_ai_)
return ERR_UNEXPECTED;
address->Copy(current_ai_, false);
return OK;
}
} // namespace net
|