| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "tools/android/forwarder2/socket.h" | 5 #include "tools/android/forwarder2/socket.h" |
| 6 | 6 |
| 7 #include <arpa/inet.h> | 7 #include <arpa/inet.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <netinet/in.h> | 10 #include <netinet/in.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 14 #include <sys/types.h> | 14 #include <sys/types.h> |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| 16 | 16 |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/safe_strerror_posix.h" | 19 #include "base/posix/safe_strerror.h" |
| 20 #include "tools/android/common/net.h" | 20 #include "tools/android/common/net.h" |
| 21 #include "tools/android/forwarder2/common.h" | 21 #include "tools/android/forwarder2/common.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 const int kNoTimeout = -1; | 24 const int kNoTimeout = -1; |
| 25 const int kConnectTimeOut = 10; // Seconds. | 25 const int kConnectTimeOut = 10; // Seconds. |
| 26 | 26 |
| 27 bool FamilyIsTCP(int family) { | 27 bool FamilyIsTCP(int family) { |
| 28 return family == AF_INET || family == AF_INET6; | 28 return family == AF_INET || family == AF_INET6; |
| 29 } | 29 } |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 return false; | 236 return false; |
| 237 } | 237 } |
| 238 int socket_errno; | 238 int socket_errno; |
| 239 socklen_t opt_len = sizeof(socket_errno); | 239 socklen_t opt_len = sizeof(socket_errno); |
| 240 if (getsockopt(socket_, SOL_SOCKET, SO_ERROR, &socket_errno, &opt_len) < 0) { | 240 if (getsockopt(socket_, SOL_SOCKET, SO_ERROR, &socket_errno, &opt_len) < 0) { |
| 241 PLOG(ERROR) << "getsockopt()"; | 241 PLOG(ERROR) << "getsockopt()"; |
| 242 SetSocketError(); | 242 SetSocketError(); |
| 243 return false; | 243 return false; |
| 244 } | 244 } |
| 245 if (socket_errno != 0) { | 245 if (socket_errno != 0) { |
| 246 LOG(ERROR) << "Could not connect to host: " << safe_strerror(socket_errno); | 246 LOG(ERROR) << "Could not connect to host: " |
| 247 << base::safe_strerror(socket_errno); |
| 247 SetSocketError(); | 248 SetSocketError(); |
| 248 return false; | 249 return false; |
| 249 } | 250 } |
| 250 return true; | 251 return true; |
| 251 } | 252 } |
| 252 | 253 |
| 253 bool Socket::Resolve(const std::string& host) { | 254 bool Socket::Resolve(const std::string& host) { |
| 254 struct addrinfo hints; | 255 struct addrinfo hints; |
| 255 struct addrinfo* res; | 256 struct addrinfo* res; |
| 256 memset(&hints, 0, sizeof(hints)); | 257 memset(&hints, 0, sizeof(hints)); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 ucred ucred; | 440 ucred ucred; |
| 440 socklen_t len = sizeof(ucred); | 441 socklen_t len = sizeof(ucred); |
| 441 if (getsockopt(socket.socket_, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { | 442 if (getsockopt(socket.socket_, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { |
| 442 CHECK_NE(ENOPROTOOPT, errno); | 443 CHECK_NE(ENOPROTOOPT, errno); |
| 443 return -1; | 444 return -1; |
| 444 } | 445 } |
| 445 return ucred.pid; | 446 return ucred.pid; |
| 446 } | 447 } |
| 447 | 448 |
| 448 } // namespace forwarder2 | 449 } // namespace forwarder2 |
| OLD | NEW |