OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <netinet/tcp.h> | 8 #include <netinet/tcp.h> |
9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 // After calling this function, the kernel will not wait. See TCP_NODELAY in | 49 // After calling this function, the kernel will not wait. See TCP_NODELAY in |
50 // `man 7 tcp`. | 50 // `man 7 tcp`. |
51 bool SetTCPNoDelay(int fd, bool no_delay) { | 51 bool SetTCPNoDelay(int fd, bool no_delay) { |
52 int on = no_delay ? 1 : 0; | 52 int on = no_delay ? 1 : 0; |
53 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); | 53 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
54 return error == 0; | 54 return error == 0; |
55 } | 55 } |
56 | 56 |
57 // SetTCPKeepAlive sets SO_KEEPALIVE. | 57 // SetTCPKeepAlive sets SO_KEEPALIVE. |
58 bool SetTCPKeepAlive(int fd, bool enable, int delay) { | 58 bool SetTCPKeepAlive(int fd, bool enable, int delay) { |
59 // Enabling TCP keepalives is the same on all platforms. | |
59 int on = enable ? 1 : 0; | 60 int on = enable ? 1 : 0; |
60 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) { | 61 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) { |
61 PLOG(ERROR) << "Failed to set SO_KEEPALIVE on fd: " << fd; | 62 PLOG(ERROR) << "Failed to set SO_KEEPALIVE on fd: " << fd; |
62 return false; | 63 return false; |
63 } | 64 } |
64 | 65 |
65 // If we disabled TCP keep alive, our work is done here. | 66 // If we disabled TCP keep alive, our work is done here. |
66 if (!enable) | 67 if (!enable) |
67 return true; | 68 return true; |
68 | 69 |
70 // Setting the keepalive interval varies by platform. | |
mmenke
2015/04/07 15:28:06
Indent here is weird, but that's what git cl forma
ramant (doing other things)
2015/04/08 20:08:38
Is it possible to move the comment inside #ifdef t
mmenke
2015/04/08 21:22:19
Done.
| |
69 #if defined(OS_LINUX) || defined(OS_ANDROID) | 71 #if defined(OS_LINUX) || defined(OS_ANDROID) |
70 // Set seconds until first TCP keep alive. | 72 // Set seconds until first TCP keep alive. |
71 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { | 73 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { |
72 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; | 74 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; |
73 return false; | 75 return false; |
74 } | 76 } |
75 // Set seconds between TCP keep alives. | 77 // Set seconds between TCP keep alives. |
76 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { | 78 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { |
77 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; | 79 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; |
78 return false; | 80 return false; |
79 } | 81 } |
82 #elif defined(OS_MACOSX) || defined(OS_IOS) | |
83 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay))) { | |
84 PLOG(ERROR) << "Failed to set TCP_KEEPALIVE on fd: " << fd; | |
85 return false; | |
86 } | |
80 #endif | 87 #endif |
81 return true; | 88 return true; |
82 } | 89 } |
83 | 90 |
84 #if defined(OS_LINUX) || defined(OS_ANDROID) | 91 #if defined(OS_LINUX) || defined(OS_ANDROID) |
85 // Checks if the kernel supports TCP FastOpen. | 92 // Checks if the kernel supports TCP FastOpen. |
86 bool SystemSupportsTCPFastOpen() { | 93 bool SystemSupportsTCPFastOpen() { |
87 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = | 94 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = |
88 "/proc/sys/net/ipv4/tcp_fastopen"; | 95 "/proc/sys/net/ipv4/tcp_fastopen"; |
89 std::string system_supports_tcp_fastopen; | 96 std::string system_supports_tcp_fastopen; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
732 } | 739 } |
733 } else { | 740 } else { |
734 tcp_fastopen_status_ = | 741 tcp_fastopen_status_ = |
735 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | 742 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? |
736 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | 743 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : |
737 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | 744 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); |
738 } | 745 } |
739 } | 746 } |
740 | 747 |
741 } // namespace net | 748 } // namespace net |
OLD | NEW |