| 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 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
| 6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
| 7 | 7 |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| 11 #include <sys/un.h> | 11 #include <sys/un.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 16 | 17 |
| 17 namespace forwarder2 { | 18 namespace forwarder2 { |
| 18 | 19 |
| 19 // Wrapper class around unix socket api. Can be used to create, bind or | 20 // Wrapper class around unix socket api. Can be used to create, bind or |
| 20 // connect to both Unix domain sockets and TCP sockets. | 21 // connect to both Unix domain sockets and TCP sockets. |
| 21 // TODO(pliard): Split this class into TCPSocket and UnixDomainSocket. | 22 // TODO(pliard): Split this class into TCPSocket and UnixDomainSocket. |
| 22 class Socket { | 23 class Socket { |
| 23 public: | 24 public: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // is performed. Returns number of bytes written, which can be different from | 63 // is performed. Returns number of bytes written, which can be different from |
| 63 // num_bytes in case of errror. | 64 // num_bytes in case of errror. |
| 64 int WriteNumBytes(const void* buffer, size_t num_bytes); | 65 int WriteNumBytes(const void* buffer, size_t num_bytes); |
| 65 | 66 |
| 66 // Calls WriteNumBytes for the given std::string. Note that the null | 67 // Calls WriteNumBytes for the given std::string. Note that the null |
| 67 // terminator is not written to the socket. | 68 // terminator is not written to the socket. |
| 68 int WriteString(const std::string& buffer); | 69 int WriteString(const std::string& buffer); |
| 69 | 70 |
| 70 bool has_error() const { return socket_error_; } | 71 bool has_error() const { return socket_error_; } |
| 71 | 72 |
| 72 // |exit_notifier_fd| must be a valid pipe file descriptor created from the | 73 // |event_fd| must be a valid pipe file descriptor created from the |
| 73 // PipeNotifier and must live (not be closed) at least as long as this socket | 74 // PipeNotifier and must live (not be closed) at least as long as this socket |
| 74 // is alive. | 75 // is alive. |
| 75 void set_exit_notifier_fd(int exit_notifier_fd) { | 76 void AddEventFd(int event_fd); |
| 76 exit_notifier_fd_ = exit_notifier_fd; | |
| 77 } | |
| 78 // Unset the |exit_notifier_fd_| so that it will not receive notifications | |
| 79 // anymore. | |
| 80 void reset_exit_notifier_fd() { exit_notifier_fd_ = -1; } | |
| 81 | 77 |
| 82 // Returns whether Accept() or Connect() was interrupted because the socket | 78 // Returns whether Accept() or Connect() was interrupted because the socket |
| 83 // received an exit notification. | 79 // received an external event fired through the provided fd. |
| 84 bool exited() const { return exited_; } | 80 bool DidReceiveEventOnFd(int fd) const; |
| 81 |
| 82 bool DidReceiveEvent() const; |
| 85 | 83 |
| 86 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | 84 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); |
| 87 | 85 |
| 88 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); | 86 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); |
| 89 | 87 |
| 90 private: | 88 private: |
| 89 enum EventType { |
| 90 READ, |
| 91 WRITE |
| 92 }; |
| 93 |
| 91 union SockAddr { | 94 union SockAddr { |
| 92 // IPv4 sockaddr | 95 // IPv4 sockaddr |
| 93 sockaddr_in addr4; | 96 sockaddr_in addr4; |
| 94 // IPv6 sockaddr | 97 // IPv6 sockaddr |
| 95 sockaddr_in6 addr6; | 98 sockaddr_in6 addr6; |
| 96 // Unix Domain sockaddr | 99 // Unix Domain sockaddr |
| 97 sockaddr_un addr_un; | 100 sockaddr_un addr_un; |
| 98 }; | 101 }; |
| 99 | 102 |
| 103 struct Event { |
| 104 int fd; |
| 105 bool was_fired; |
| 106 }; |
| 107 |
| 100 // If |host| is empty, use localhost. | 108 // If |host| is empty, use localhost. |
| 101 bool InitTcpSocket(const std::string& host, int port); | 109 bool InitTcpSocket(const std::string& host, int port); |
| 102 bool InitUnixSocket(const std::string& path); | 110 bool InitUnixSocket(const std::string& path); |
| 103 bool BindAndListen(); | 111 bool BindAndListen(); |
| 104 bool Connect(); | 112 bool Connect(); |
| 105 | 113 |
| 106 bool Resolve(const std::string& host); | 114 bool Resolve(const std::string& host); |
| 107 bool InitSocketInternal(); | 115 bool InitSocketInternal(); |
| 108 void SetSocketError(); | 116 void SetSocketError(); |
| 109 | 117 |
| 110 enum EventType { | |
| 111 READ, | |
| 112 WRITE | |
| 113 }; | |
| 114 | |
| 115 // Waits until either the Socket or the |exit_notifier_fd_| has received an | 118 // Waits until either the Socket or the |exit_notifier_fd_| has received an |
| 116 // event. | 119 // event. |
| 117 bool WaitForEvent(EventType type, int timeout_secs); | 120 bool WaitForEvent(EventType type, int timeout_secs); |
| 118 | 121 |
| 119 int socket_; | 122 int socket_; |
| 120 int port_; | 123 int port_; |
| 121 bool socket_error_; | 124 bool socket_error_; |
| 122 | 125 |
| 123 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). | 126 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). |
| 124 int family_; | 127 int family_; |
| 125 | 128 |
| 126 SockAddr addr_; | 129 SockAddr addr_; |
| 127 | 130 |
| 128 // Points to one of the members of the above union depending on the family. | 131 // Points to one of the members of the above union depending on the family. |
| 129 sockaddr* addr_ptr_; | 132 sockaddr* addr_ptr_; |
| 130 // Length of one of the members of the above union depending on the family. | 133 // Length of one of the members of the above union depending on the family. |
| 131 socklen_t addr_len_; | 134 socklen_t addr_len_; |
| 132 | 135 |
| 133 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application | 136 // Used to listen for external events (e.g. process received a SIGTERM) while |
| 134 // exit notifications before calling socket blocking operations such as Read | 137 // blocking on I/O operations. |
| 135 // and Accept. | 138 std::vector<Event> events_; |
| 136 int exit_notifier_fd_; | |
| 137 | |
| 138 bool exited_; | |
| 139 | 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(Socket); | 140 DISALLOW_COPY_AND_ASSIGN(Socket); |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 } // namespace forwarder | 143 } // namespace forwarder |
| 144 | 144 |
| 145 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 145 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
| OLD | NEW |