| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/tools/flip_server/tcp_socket_util.h" | 5 #include "net/tools/flip_server/tcp_socket_util.h" |
| 6 | 6 |
| 7 #include <arpa/inet.h> | 7 #include <arpa/inet.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <netinet/in.h> | 10 #include <netinet/in.h> |
| 11 #include <netinet/tcp.h> | 11 #include <netinet/tcp.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
| 15 #include <sys/types.h> | 15 #include <sys/types.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 | 17 |
| 18 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "net/socket/tcp_socket.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Used to ensure we delete the addrinfo structure alloc'd by getaddrinfo(). | 26 // Used to ensure we delete the addrinfo structure alloc'd by getaddrinfo(). |
| 26 class AddrinfoGuard { | 27 class AddrinfoGuard { |
| 27 public: | 28 public: |
| 28 explicit AddrinfoGuard(struct addrinfo* addrinfo_ptr) | 29 explicit AddrinfoGuard(struct addrinfo* addrinfo_ptr) |
| 29 : addrinfo_ptr_(addrinfo_ptr) {} | 30 : addrinfo_ptr_(addrinfo_ptr) {} |
| (...skipping 24 matching lines...) Expand all Loading... |
| 54 if (!close(*fd)) { | 55 if (!close(*fd)) { |
| 55 *fd = -1; | 56 *fd = -1; |
| 56 return true; | 57 return true; |
| 57 } | 58 } |
| 58 } | 59 } |
| 59 return false; | 60 return false; |
| 60 } | 61 } |
| 61 | 62 |
| 62 } // namespace | 63 } // namespace |
| 63 | 64 |
| 64 bool SetTCPNoDelay(int fd) { | |
| 65 int on = 1; | |
| 66 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&on), | |
| 67 sizeof(on)) == 0; | |
| 68 } | |
| 69 | |
| 70 int CreateTCPServerSocket(const std::string& host, | 65 int CreateTCPServerSocket(const std::string& host, |
| 71 const std::string& port, | 66 const std::string& port, |
| 72 bool is_numeric_host_address, | 67 bool is_numeric_host_address, |
| 73 int backlog, | 68 int backlog, |
| 74 bool reuseaddr, | 69 bool reuseaddr, |
| 75 bool reuseport, | 70 bool reuseport, |
| 76 bool wait_for_iface, | 71 bool wait_for_iface, |
| 77 bool disable_nagle, | 72 bool disable_nagle, |
| 78 int* listen_fd) { | 73 int* listen_fd) { |
| 79 // start out by assuming things will fail. | 74 // start out by assuming things will fail. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } else { | 163 } else { |
| 169 // couldn't even close the dang socket?! | 164 // couldn't even close the dang socket?! |
| 170 LOG(ERROR) << "Unable to close the socket.. Considering this a fatal " | 165 LOG(ERROR) << "Unable to close the socket.. Considering this a fatal " |
| 171 "error, and exiting\n"; | 166 "error, and exiting\n"; |
| 172 exit(EXIT_FAILURE); | 167 exit(EXIT_FAILURE); |
| 173 return -1; | 168 return -1; |
| 174 } | 169 } |
| 175 } | 170 } |
| 176 | 171 |
| 177 if (disable_nagle) { | 172 if (disable_nagle) { |
| 178 if (!SetTCPNoDelay(sock)) { | 173 if (!SetTCPNoDelay(sock, /*no_delay=*/true)) { |
| 179 close(sock); | 174 close(sock); |
| 180 LOG(FATAL) << "SetTCPNoDelay() failed on fd: " << sock; | 175 LOG(FATAL) << "SetTCPNoDelay() failed on fd: " << sock; |
| 181 return -1; | 176 return -1; |
| 182 } | 177 } |
| 183 } | 178 } |
| 184 | 179 |
| 185 if (listen(sock, backlog)) { | 180 if (listen(sock, backlog)) { |
| 186 // listen was unsuccessful. | 181 // listen was unsuccessful. |
| 187 LOG(ERROR) << "Listen was unsuccessful for (" << host << ":" << port | 182 LOG(ERROR) << "Listen was unsuccessful for (" << host << ":" << port |
| 188 << "): " << strerror(errno) << "\n"; | 183 << "): " << strerror(errno) << "\n"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 LOG(ERROR) << "Unable to create socket for (" << node << ":" << service | 241 LOG(ERROR) << "Unable to create socket for (" << node << ":" << service |
| 247 << "): " << strerror(errno); | 242 << "): " << strerror(errno); |
| 248 return -1; | 243 return -1; |
| 249 } | 244 } |
| 250 | 245 |
| 251 if (!base::SetNonBlocking(sock)) { | 246 if (!base::SetNonBlocking(sock)) { |
| 252 LOG(FATAL) << "base::SetNonBlocking failed: " << sock; | 247 LOG(FATAL) << "base::SetNonBlocking failed: " << sock; |
| 253 } | 248 } |
| 254 | 249 |
| 255 if (disable_nagle) { | 250 if (disable_nagle) { |
| 256 if (!SetTCPNoDelay(sock)) { | 251 if (!SetTCPNoDelay(sock, /*no_delay=*/true)) { |
| 257 close(sock); | 252 close(sock); |
| 258 LOG(FATAL) << "SetTCPNoDelay() failed on fd: " << sock; | 253 LOG(FATAL) << "SetTCPNoDelay() failed on fd: " << sock; |
| 259 return -1; | 254 return -1; |
| 260 } | 255 } |
| 261 } | 256 } |
| 262 | 257 |
| 263 int ret_val = 0; | 258 int ret_val = 0; |
| 264 if (connect(sock, results->ai_addr, results->ai_addrlen)) { | 259 if (connect(sock, results->ai_addr, results->ai_addrlen)) { |
| 265 if (errno != EINPROGRESS) { | 260 if (errno != EINPROGRESS) { |
| 266 LOG(ERROR) << "Connect was unsuccessful for (" << node << ":" << service | 261 LOG(ERROR) << "Connect was unsuccessful for (" << node << ":" << service |
| 267 << "): " << strerror(errno); | 262 << "): " << strerror(errno); |
| 268 close(sock); | 263 close(sock); |
| 269 return -1; | 264 return -1; |
| 270 } | 265 } |
| 271 } else { | 266 } else { |
| 272 ret_val = 1; | 267 ret_val = 1; |
| 273 } | 268 } |
| 274 | 269 |
| 275 // If we've gotten to here, Yeay! Success! | 270 // If we've gotten to here, Yeay! Success! |
| 276 *connect_fd = sock; | 271 *connect_fd = sock; |
| 277 | 272 |
| 278 return ret_val; | 273 return ret_val; |
| 279 } | 274 } |
| 280 | 275 |
| 281 } // namespace net | 276 } // namespace net |
| OLD | NEW |