OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
7 | |
8 #include <fcntl.h> | |
9 #include <netinet/in.h> | |
10 #include <sys/socket.h> | |
11 #include <sys/un.h> | |
12 | |
13 #include <string> | |
14 | |
15 #include "base/basictypes.h" | |
16 | |
17 namespace forwarder2 { | |
18 | |
19 // Wrapper class around unix socket api. Can be used to create, bind or | |
20 // connect to both Unix domain sockets and TCP sockets. | |
21 class Socket { | |
22 public: | |
23 Socket(); | |
24 ~Socket(); | |
25 | |
26 bool BindUnix(const std::string& path, bool abstract); | |
27 bool BindTcp(const std::string& host, int port); | |
28 bool ConnectUnix(const std::string& path, bool abstract); | |
29 bool ConnectTcp(const std::string& host, int port); | |
30 | |
31 // Just a wrapper around unix socket shutdown(), see man 2 shutdown. | |
32 void Shutdown(); | |
33 | |
34 // Just a wrapper around unix socket close(), see man 2 close. | |
35 void Close(); | |
36 bool IsClosed() const { return socket_ < 0; } | |
37 | |
38 bool Accept(Socket* new_socket); | |
39 | |
40 bool IsFdInSet(const fd_set& fds) const; | |
41 bool AddFdToSet(fd_set* fds) const; | |
42 | |
43 // Just a wrapper around unix read() function. | |
44 // Reads up to buffer_size, but may read less then buffer_size. | |
45 // Returns the number of bytes read. | |
46 int Read(char* buffer, size_t buffer_size); | |
47 | |
48 // Same as Read(), just a wrapper around write(). | |
49 int Write(const char* buffer, size_t count); | |
50 | |
51 // Calls Read() multiple times until num_bytes is written to the provided | |
52 // buffer. No bounds checking is performed. | |
53 // Returns number of bytes read, which can be different from num_bytes in case | |
54 // of errror. | |
55 int ReadNumBytes(char* buffer, size_t num_bytes); | |
56 | |
57 // Calls Write() multiple times until num_bytes is written. No bounds checking | |
58 // is performed. Returns number of bytes written, which can be different from | |
59 // num_bytes in case of errror. | |
60 int WriteNumBytes(const char* buffer, size_t num_bytes); | |
61 | |
62 // Calls WriteNumBytes for the given std::string. | |
63 int WriteString(const std::string& buffer); | |
64 | |
65 bool has_error() const { return socket_error_; } | |
66 | |
67 // |notifier_fd| must be a valid pipe file descriptor created from the | |
68 // PipeNotifier and must live (not be closed) at least as long as this socket | |
69 // is alive. | |
70 void set_notifier_fd(int notifier_fd) { exit_notifier_fd_ = notifier_fd; } | |
71 // Unset the |exit_notifier_fd_| so that it will not receive notifications | |
72 // anymore. | |
73 void reset_notifier_fd() { exit_notifier_fd_ = -1; } | |
74 | |
75 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | |
76 | |
77 private: | |
78 union SockAddr { | |
79 // IPv4 sockaddr | |
80 sockaddr_in addr4; | |
81 // IPv6 sockaddr | |
82 sockaddr_in6 addr6; | |
83 // Unix Domain sockaddr | |
84 sockaddr_un addr_un; | |
85 }; | |
86 | |
87 // If |host| is empty, use localhost. | |
88 bool InitTcpSocket(const std::string& host, int port); | |
89 bool InitUnixSocket(const std::string& path, bool abstract); | |
90 bool BindAndListen(); | |
91 bool Connect(); | |
92 | |
93 bool Resolve(const std::string& host); | |
94 bool InitSocketInternal(); | |
95 void SetSocketError(); | |
96 | |
97 // Waits until either the Socket or the |exit_notifier_fd_| has received a | |
98 // read event (accept or read). Returns false iff an exit notification was | |
99 // received. | |
100 bool WaitForEvent() const; | |
101 | |
102 int socket_; | |
103 int port_; | |
104 bool socket_error_; | |
105 | |
106 // Family of the socket (IF_INET, IF_INET6 or PF_UNIX). | |
digit
2012/09/07 13:47:12
Nit: IF_INET/IF_INET6 do not exist, you probably m
felipeg
2012/09/07 13:56:57
Done.
| |
107 int family_; | |
108 | |
109 // True if this is an abstract unix domain socket. | |
110 bool abstract_; | |
111 | |
112 SockAddr addr_; | |
113 | |
114 // Points to one of the members of the above union depending on the family. | |
115 sockaddr* addr_ptr_; | |
116 // Length of one of the members of the above union depending on the family. | |
117 socklen_t addr_len_; | |
118 | |
119 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application | |
120 // exit notifications before calling socket blocking operations such as Read | |
121 // and Accept. | |
122 int exit_notifier_fd_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(Socket); | |
125 }; | |
126 | |
127 } // namespace forwarder | |
128 | |
129 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
OLD | NEW |