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 |
69 #if defined(OS_LINUX) || defined(OS_ANDROID) | 70 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 71 // Setting the keepalive interval varies by platform. |
| 72 |
70 // Set seconds until first TCP keep alive. | 73 // Set seconds until first TCP keep alive. |
71 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { | 74 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { |
72 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; | 75 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; |
73 return false; | 76 return false; |
74 } | 77 } |
75 // Set seconds between TCP keep alives. | 78 // Set seconds between TCP keep alives. |
76 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { | 79 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { |
77 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; | 80 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; |
78 return false; | 81 return false; |
79 } | 82 } |
| 83 #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 84 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay))) { |
| 85 PLOG(ERROR) << "Failed to set TCP_KEEPALIVE on fd: " << fd; |
| 86 return false; |
| 87 } |
80 #endif | 88 #endif |
81 return true; | 89 return true; |
82 } | 90 } |
83 | 91 |
84 #if defined(OS_LINUX) || defined(OS_ANDROID) | 92 #if defined(OS_LINUX) || defined(OS_ANDROID) |
85 // Checks if the kernel supports TCP FastOpen. | 93 // Checks if the kernel supports TCP FastOpen. |
86 bool SystemSupportsTCPFastOpen() { | 94 bool SystemSupportsTCPFastOpen() { |
87 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = | 95 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = |
88 "/proc/sys/net/ipv4/tcp_fastopen"; | 96 "/proc/sys/net/ipv4/tcp_fastopen"; |
89 std::string system_supports_tcp_fastopen; | 97 std::string system_supports_tcp_fastopen; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 } | 740 } |
733 } else { | 741 } else { |
734 tcp_fastopen_status_ = | 742 tcp_fastopen_status_ = |
735 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | 743 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? |
736 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | 744 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : |
737 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | 745 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); |
738 } | 746 } |
739 } | 747 } |
740 | 748 |
741 } // namespace net | 749 } // namespace net |
OLD | NEW |