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