| 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> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // Returns the port allocated to this socket or zero on error. | 40 // Returns the port allocated to this socket or zero on error. |
| 41 int GetPort(); | 41 int GetPort(); |
| 42 | 42 |
| 43 bool IsFdInSet(const fd_set& fds) const; | 43 bool IsFdInSet(const fd_set& fds) const; |
| 44 bool AddFdToSet(fd_set* fds) const; | 44 bool AddFdToSet(fd_set* fds) const; |
| 45 | 45 |
| 46 // Just a wrapper around unix read() function. | 46 // Just a wrapper around unix read() function. |
| 47 // Reads up to buffer_size, but may read less then buffer_size. | 47 // Reads up to buffer_size, but may read less then buffer_size. |
| 48 // Returns the number of bytes read. | 48 // Returns the number of bytes read. |
| 49 int Read(char* buffer, size_t buffer_size); | 49 int Read(void* buffer, size_t buffer_size); |
| 50 | 50 |
| 51 // Same as Read(), just a wrapper around write(). | 51 // Same as Read(), just a wrapper around write(). |
| 52 int Write(const char* buffer, size_t count); | 52 int Write(const void* buffer, size_t count); |
| 53 | 53 |
| 54 // Calls Read() multiple times until num_bytes is written to the provided | 54 // Calls Read() multiple times until num_bytes is written to the provided |
| 55 // buffer. No bounds checking is performed. | 55 // buffer. No bounds checking is performed. |
| 56 // Returns number of bytes read, which can be different from num_bytes in case | 56 // Returns number of bytes read, which can be different from num_bytes in case |
| 57 // of errror. | 57 // of errror. |
| 58 int ReadNumBytes(char* buffer, size_t num_bytes); | 58 int ReadNumBytes(void* buffer, size_t num_bytes); |
| 59 | 59 |
| 60 // Calls Write() multiple times until num_bytes is written. No bounds checking | 60 // Calls Write() multiple times until num_bytes is written. No bounds checking |
| 61 // is performed. Returns number of bytes written, which can be different from | 61 // is performed. Returns number of bytes written, which can be different from |
| 62 // num_bytes in case of errror. | 62 // num_bytes in case of errror. |
| 63 int WriteNumBytes(const char* buffer, size_t num_bytes); | 63 int WriteNumBytes(const void* buffer, size_t num_bytes); |
| 64 | 64 |
| 65 // Calls WriteNumBytes for the given std::string. | 65 // Calls WriteNumBytes for the given std::string. Note that the null |
| 66 // terminator is not written to the socket. |
| 66 int WriteString(const std::string& buffer); | 67 int WriteString(const std::string& buffer); |
| 67 | 68 |
| 68 bool has_error() const { return socket_error_; } | 69 bool has_error() const { return socket_error_; } |
| 69 | 70 |
| 70 // |exit_notifier_fd| must be a valid pipe file descriptor created from the | 71 // |exit_notifier_fd| must be a valid pipe file descriptor created from the |
| 71 // PipeNotifier and must live (not be closed) at least as long as this socket | 72 // PipeNotifier and must live (not be closed) at least as long as this socket |
| 72 // is alive. | 73 // is alive. |
| 73 void set_exit_notifier_fd(int exit_notifier_fd) { | 74 void set_exit_notifier_fd(int exit_notifier_fd) { |
| 74 exit_notifier_fd_ = exit_notifier_fd; | 75 exit_notifier_fd_ = exit_notifier_fd; |
| 75 } | 76 } |
| 76 // Unset the |exit_notifier_fd_| so that it will not receive notifications | 77 // Unset the |exit_notifier_fd_| so that it will not receive notifications |
| 77 // anymore. | 78 // anymore. |
| 78 void reset_exit_notifier_fd() { exit_notifier_fd_ = -1; } | 79 void reset_exit_notifier_fd() { exit_notifier_fd_ = -1; } |
| 79 | 80 |
| 81 // Returns whether Accept() or Connect() was interrupted because the socket |
| 82 // received an exit notification. |
| 83 bool exited() const { return exited_; } |
| 84 |
| 80 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | 85 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); |
| 81 | 86 |
| 82 private: | 87 private: |
| 83 union SockAddr { | 88 union SockAddr { |
| 84 // IPv4 sockaddr | 89 // IPv4 sockaddr |
| 85 sockaddr_in addr4; | 90 sockaddr_in addr4; |
| 86 // IPv6 sockaddr | 91 // IPv6 sockaddr |
| 87 sockaddr_in6 addr6; | 92 sockaddr_in6 addr6; |
| 88 // Unix Domain sockaddr | 93 // Unix Domain sockaddr |
| 89 sockaddr_un addr_un; | 94 sockaddr_un addr_un; |
| 90 }; | 95 }; |
| 91 | 96 |
| 92 // If |host| is empty, use localhost. | 97 // If |host| is empty, use localhost. |
| 93 bool InitTcpSocket(const std::string& host, int port); | 98 bool InitTcpSocket(const std::string& host, int port); |
| 94 bool InitUnixSocket(const std::string& path, bool abstract); | 99 bool InitUnixSocket(const std::string& path, bool abstract); |
| 95 bool BindAndListen(); | 100 bool BindAndListen(); |
| 96 bool Connect(); | 101 bool Connect(); |
| 97 | 102 |
| 98 bool Resolve(const std::string& host); | 103 bool Resolve(const std::string& host); |
| 99 bool InitSocketInternal(); | 104 bool InitSocketInternal(); |
| 100 void SetSocketError(); | 105 void SetSocketError(); |
| 101 | 106 |
| 102 enum EventType { | 107 enum EventType { |
| 103 READ, | 108 READ, |
| 104 WRITE | 109 WRITE |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 // Waits until either the Socket or the |exit_notifier_fd_| has received a | 112 // Waits until either the Socket or the |exit_notifier_fd_| has received an |
| 108 // read event (accept or read). Returns false iff an exit notification was | 113 // event. |
| 109 // received. If |read| is false, it waits until Socket is ready to write, | 114 bool WaitForEvent(EventType type, int timeout_secs); |
| 110 // instead. If |timeout_secs| is a non-negative value, it sets the timeout, | |
| 111 // for the select operation. | |
| 112 bool WaitForEvent(EventType type, int timeout_secs) const; | |
| 113 | 115 |
| 114 int socket_; | 116 int socket_; |
| 115 int port_; | 117 int port_; |
| 116 bool socket_error_; | 118 bool socket_error_; |
| 117 | 119 |
| 118 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). | 120 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). |
| 119 int family_; | 121 int family_; |
| 120 | 122 |
| 121 // True if this is an abstract unix domain socket. | 123 // True if this is an abstract unix domain socket. |
| 122 bool abstract_; | 124 bool abstract_; |
| 123 | 125 |
| 124 SockAddr addr_; | 126 SockAddr addr_; |
| 125 | 127 |
| 126 // Points to one of the members of the above union depending on the family. | 128 // Points to one of the members of the above union depending on the family. |
| 127 sockaddr* addr_ptr_; | 129 sockaddr* addr_ptr_; |
| 128 // Length of one of the members of the above union depending on the family. | 130 // Length of one of the members of the above union depending on the family. |
| 129 socklen_t addr_len_; | 131 socklen_t addr_len_; |
| 130 | 132 |
| 131 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application | 133 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application |
| 132 // exit notifications before calling socket blocking operations such as Read | 134 // exit notifications before calling socket blocking operations such as Read |
| 133 // and Accept. | 135 // and Accept. |
| 134 int exit_notifier_fd_; | 136 int exit_notifier_fd_; |
| 135 | 137 |
| 138 bool exited_; |
| 139 |
| 136 DISALLOW_COPY_AND_ASSIGN(Socket); | 140 DISALLOW_COPY_AND_ASSIGN(Socket); |
| 137 }; | 141 }; |
| 138 | 142 |
| 139 } // namespace forwarder | 143 } // namespace forwarder |
| 140 | 144 |
| 141 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 145 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
| OLD | NEW |