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 NET_UDP_UDP_SOCKET_LIBEVENT_H_ | |
6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ | |
7 | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/threading/non_thread_safe.h" | |
12 #include "net/base/address_family.h" | |
13 #include "net/base/completion_callback.h" | |
14 #include "net/base/io_buffer.h" | |
15 #include "net/base/ip_endpoint.h" | |
16 #include "net/base/net_export.h" | |
17 #include "net/base/net_util.h" | |
18 #include "net/base/rand_callback.h" | |
19 #include "net/log/net_log.h" | |
20 #include "net/socket/socket_descriptor.h" | |
21 #include "net/udp/datagram_socket.h" | |
22 | |
23 namespace net { | |
24 | |
25 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { | |
26 public: | |
27 UDPSocketLibevent(DatagramSocket::BindType bind_type, | |
28 const RandIntCallback& rand_int_cb, | |
29 net::NetLog* net_log, | |
30 const net::NetLog::Source& source); | |
31 virtual ~UDPSocketLibevent(); | |
32 | |
33 // Opens the socket. | |
34 // Returns a net error code. | |
35 int Open(AddressFamily address_family); | |
36 | |
37 // Connects the socket to connect with a certain |address|. | |
38 // Should be called after Open(). | |
39 // Returns a net error code. | |
40 int Connect(const IPEndPoint& address); | |
41 | |
42 // Binds the address/port for this socket to |address|. This is generally | |
43 // only used on a server. Should be called after Open(). | |
44 // Returns a net error code. | |
45 int Bind(const IPEndPoint& address); | |
46 | |
47 // Closes the socket. | |
48 // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). | |
49 void Close(); | |
50 | |
51 // Copies the remote udp address into |address| and returns a net error code. | |
52 int GetPeerAddress(IPEndPoint* address) const; | |
53 | |
54 // Copies the local udp address into |address| and returns a net error code. | |
55 // (similar to getsockname) | |
56 int GetLocalAddress(IPEndPoint* address) const; | |
57 | |
58 // IO: | |
59 // Multiple outstanding read requests are not supported. | |
60 // Full duplex mode (reading and writing at the same time) is supported | |
61 | |
62 // Reads from the socket. | |
63 // Only usable from the client-side of a UDP socket, after the socket | |
64 // has been connected. | |
65 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
66 | |
67 // Writes to the socket. | |
68 // Only usable from the client-side of a UDP socket, after the socket | |
69 // has been connected. | |
70 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
71 | |
72 // Reads from a socket and receive sender address information. | |
73 // |buf| is the buffer to read data into. | |
74 // |buf_len| is the maximum amount of data to read. | |
75 // |address| is a buffer provided by the caller for receiving the sender | |
76 // address information about the received data. This buffer must be kept | |
77 // alive by the caller until the callback is placed. | |
78 // |address_length| is a ptr to the length of the |address| buffer. This | |
79 // is an input parameter containing the maximum size |address| can hold | |
80 // and also an output parameter for the size of |address| upon completion. | |
81 // |callback| is the callback on completion of the RecvFrom. | |
82 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
83 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, | |
84 // and |address_length| alive until the callback is called. | |
85 int RecvFrom(IOBuffer* buf, | |
86 int buf_len, | |
87 IPEndPoint* address, | |
88 const CompletionCallback& callback); | |
89 | |
90 // Sends to a socket with a particular destination. | |
91 // |buf| is the buffer to send | |
92 // |buf_len| is the number of bytes to send | |
93 // |address| is the recipient address. | |
94 // |address_length| is the size of the recipient address | |
95 // |callback| is the user callback function to call on complete. | |
96 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
97 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | |
98 // alive until the callback is called. | |
99 int SendTo(IOBuffer* buf, | |
100 int buf_len, | |
101 const IPEndPoint& address, | |
102 const CompletionCallback& callback); | |
103 | |
104 // Sets the receive buffer size (in bytes) for the socket. | |
105 // Returns a net error code. | |
106 int SetReceiveBufferSize(int32 size); | |
107 | |
108 // Sets the send buffer size (in bytes) for the socket. | |
109 // Returns a net error code. | |
110 int SetSendBufferSize(int32 size); | |
111 | |
112 // Returns true if the socket is already connected or bound. | |
113 bool is_connected() const { return is_connected_; } | |
114 | |
115 const BoundNetLog& NetLog() const { return net_log_; } | |
116 | |
117 // Sets corresponding flags in |socket_options_| to allow the socket | |
118 // to share the local address to which the socket will be bound with | |
119 // other processes. Should be called between Open() and Bind(). | |
120 // Returns a net error code. | |
121 int AllowAddressReuse(); | |
122 | |
123 // Sets corresponding flags in |socket_options_| to allow or disallow sending | |
124 // and receiving packets to and from broadcast addresses. | |
125 // Returns a net error code. | |
126 int SetBroadcast(bool broadcast); | |
127 | |
128 // Joins the multicast group. | |
129 // |group_address| is the group address to join, could be either | |
130 // an IPv4 or IPv6 address. | |
131 // Returns a net error code. | |
132 int JoinGroup(const IPAddressNumber& group_address) const; | |
133 | |
134 // Leaves the multicast group. | |
135 // |group_address| is the group address to leave, could be either | |
136 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | |
137 // it will be ignored. | |
138 // It's optional to leave the multicast group before destroying | |
139 // the socket. It will be done by the OS. | |
140 // Returns a net error code. | |
141 int LeaveGroup(const IPAddressNumber& group_address) const; | |
142 | |
143 // Sets interface to use for multicast. If |interface_index| set to 0, | |
144 // default interface is used. | |
145 // Should be called before Bind(). | |
146 // Returns a net error code. | |
147 int SetMulticastInterface(uint32 interface_index); | |
148 | |
149 // Sets the time-to-live option for UDP packets sent to the multicast | |
150 // group address. The default value of this option is 1. | |
151 // Cannot be negative or more than 255. | |
152 // Should be called before Bind(). | |
153 // Returns a net error code. | |
154 int SetMulticastTimeToLive(int time_to_live); | |
155 | |
156 // Sets the loopback flag for UDP socket. If this flag is true, the host | |
157 // will receive packets sent to the joined group from itself. | |
158 // The default value of this option is true. | |
159 // Should be called before Bind(). | |
160 // Returns a net error code. | |
161 // | |
162 // Note: the behavior of |SetMulticastLoopbackMode| is slightly | |
163 // different between Windows and Unix-like systems. The inconsistency only | |
164 // happens when there are more than one applications on the same host | |
165 // joined to the same multicast group while having different settings on | |
166 // multicast loopback mode. On Windows, the applications with loopback off | |
167 // will not RECEIVE the loopback packets; while on Unix-like systems, the | |
168 // applications with loopback off will not SEND the loopback packets to | |
169 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | |
170 int SetMulticastLoopbackMode(bool loopback); | |
171 | |
172 // Sets the differentiated services flags on outgoing packets. May not | |
173 // do anything on some platforms. | |
174 // Returns a net error code. | |
175 int SetDiffServCodePoint(DiffServCodePoint dscp); | |
176 | |
177 // Resets the thread to be used for thread-safety checks. | |
178 void DetachFromThread(); | |
179 | |
180 private: | |
181 enum SocketOptions { | |
182 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 | |
183 }; | |
184 | |
185 class ReadWatcher : public base::MessageLoopForIO::Watcher { | |
186 public: | |
187 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | |
188 | |
189 // MessageLoopForIO::Watcher methods | |
190 | |
191 void OnFileCanReadWithoutBlocking(int /* fd */) override; | |
192 | |
193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | |
194 | |
195 private: | |
196 UDPSocketLibevent* const socket_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | |
199 }; | |
200 | |
201 class WriteWatcher : public base::MessageLoopForIO::Watcher { | |
202 public: | |
203 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | |
204 | |
205 // MessageLoopForIO::Watcher methods | |
206 | |
207 void OnFileCanReadWithoutBlocking(int /* fd */) override {} | |
208 | |
209 void OnFileCanWriteWithoutBlocking(int /* fd */) override; | |
210 | |
211 private: | |
212 UDPSocketLibevent* const socket_; | |
213 | |
214 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | |
215 }; | |
216 | |
217 void DoReadCallback(int rv); | |
218 void DoWriteCallback(int rv); | |
219 void DidCompleteRead(); | |
220 void DidCompleteWrite(); | |
221 | |
222 // Handles stats and logging. |result| is the number of bytes transferred, on | |
223 // success, or the net error code on failure. On success, LogRead takes in a | |
224 // sockaddr and its length, which are mandatory, while LogWrite takes in an | |
225 // optional IPEndPoint. | |
226 void LogRead(int result, const char* bytes, socklen_t addr_len, | |
227 const sockaddr* addr) const; | |
228 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | |
229 | |
230 // Same as SendTo(), except that address is passed by pointer | |
231 // instead of by reference. It is called from Write() with |address| | |
232 // set to NULL. | |
233 int SendToOrWrite(IOBuffer* buf, | |
234 int buf_len, | |
235 const IPEndPoint* address, | |
236 const CompletionCallback& callback); | |
237 | |
238 int InternalConnect(const IPEndPoint& address); | |
239 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | |
240 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | |
241 | |
242 // Applies |socket_options_| to |socket_|. Should be called before | |
243 // Bind(). | |
244 int SetMulticastOptions(); | |
245 int DoBind(const IPEndPoint& address); | |
246 // Binds to a random port on |address|. | |
247 int RandomBind(const IPAddressNumber& address); | |
248 | |
249 int socket_; | |
250 | |
251 int addr_family_; | |
252 bool is_connected_; | |
253 | |
254 // Bitwise-or'd combination of SocketOptions. Specifies the set of | |
255 // options that should be applied to |socket_| before Bind(). | |
256 int socket_options_; | |
257 | |
258 // Multicast interface. | |
259 uint32 multicast_interface_; | |
260 | |
261 // Multicast socket options cached for SetMulticastOption. | |
262 // Cannot be used after Bind(). | |
263 int multicast_time_to_live_; | |
264 | |
265 // How to do source port binding, used only when UDPSocket is part of | |
266 // UDPClientSocket, since UDPServerSocket provides Bind. | |
267 DatagramSocket::BindType bind_type_; | |
268 | |
269 // PRNG function for generating port numbers. | |
270 RandIntCallback rand_int_cb_; | |
271 | |
272 // These are mutable since they're just cached copies to make | |
273 // GetPeerAddress/GetLocalAddress smarter. | |
274 mutable scoped_ptr<IPEndPoint> local_address_; | |
275 mutable scoped_ptr<IPEndPoint> remote_address_; | |
276 | |
277 // The socket's libevent wrappers | |
278 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | |
279 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | |
280 | |
281 // The corresponding watchers for reads and writes. | |
282 ReadWatcher read_watcher_; | |
283 WriteWatcher write_watcher_; | |
284 | |
285 // The buffer used by InternalRead() to retry Read requests | |
286 scoped_refptr<IOBuffer> read_buf_; | |
287 int read_buf_len_; | |
288 IPEndPoint* recv_from_address_; | |
289 | |
290 // The buffer used by InternalWrite() to retry Write requests | |
291 scoped_refptr<IOBuffer> write_buf_; | |
292 int write_buf_len_; | |
293 scoped_ptr<IPEndPoint> send_to_address_; | |
294 | |
295 // External callback; called when read is complete. | |
296 CompletionCallback read_callback_; | |
297 | |
298 // External callback; called when write is complete. | |
299 CompletionCallback write_callback_; | |
300 | |
301 BoundNetLog net_log_; | |
302 | |
303 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | |
304 }; | |
305 | |
306 } // namespace net | |
307 | |
308 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | |
OLD | NEW |