blob: 3885b4bc120dd34d805b603e97d7bd49fcde29d2 [file] [log] [blame]
[email protected]398dca82012-04-25 17:54:531// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a2798d92011-03-02 22:56:182// 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_
[email protected]a2798d92011-03-02 22:56:187
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
[email protected]7f86564d2013-07-18 00:41:2210#include "base/message_loop/message_loop.h"
[email protected]a2798d92011-03-02 22:56:1811#include "base/threading/non_thread_safe.h"
hidehiko17fac552014-12-08 06:02:1712#include "net/base/address_family.h"
[email protected]a2798d92011-03-02 22:56:1813#include "net/base/completion_callback.h"
[email protected]5370c012011-06-29 03:47:0414#include "net/base/io_buffer.h"
[email protected]43d4a0262011-03-09 19:26:0415#include "net/base/ip_endpoint.h"
[email protected]7f86564d2013-07-18 00:41:2216#include "net/base/net_export.h"
[email protected]7f86564d2013-07-18 00:41:2217#include "net/base/rand_callback.h"
eroman87c53d62015-04-02 06:51:0718#include "net/log/net_log.h"
[email protected]bbfef1f2013-08-28 03:00:5119#include "net/socket/socket_descriptor.h"
[email protected]5370c012011-06-29 03:47:0420#include "net/udp/datagram_socket.h"
[email protected]a2798d92011-03-02 22:56:1821
22namespace net {
23
[email protected]398dca82012-04-25 17:54:5324class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe {
[email protected]a2798d92011-03-02 22:56:1825 public:
[email protected]5370c012011-06-29 03:47:0426 UDPSocketLibevent(DatagramSocket::BindType bind_type,
27 const RandIntCallback& rand_int_cb,
28 net::NetLog* net_log,
[email protected]a2798d92011-03-02 22:56:1829 const net::NetLog::Source& source);
30 virtual ~UDPSocketLibevent();
31
hidehiko17fac552014-12-08 06:02:1732 // Opens the socket.
33 // Returns a net error code.
34 int Open(AddressFamily address_family);
35
36 // Connects the socket to connect with a certain |address|.
37 // Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1838 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0439 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1840
hidehiko17fac552014-12-08 06:02:1741 // Binds the address/port for this socket to |address|. This is generally
42 // only used on a server. Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1843 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0444 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1845
hidehiko17fac552014-12-08 06:02:1746 // Closes the socket.
47 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
[email protected]a2798d92011-03-02 22:56:1848 void Close();
49
hidehiko17fac552014-12-08 06:02:1750 // Copies the remote udp address into |address| and returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0451 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1852
hidehiko17fac552014-12-08 06:02:1753 // Copies the local udp address into |address| and returns a net error code.
[email protected]a2798d92011-03-02 22:56:1854 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0455 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1856
57 // IO:
58 // Multiple outstanding read requests are not supported.
59 // Full duplex mode (reading and writing at the same time) is supported
60
hidehiko17fac552014-12-08 06:02:1761 // Reads from the socket.
[email protected]a2798d92011-03-02 22:56:1862 // Only usable from the client-side of a UDP socket, after the socket
63 // has been connected.
[email protected]3f55aa12011-12-07 02:03:3364 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1865
hidehiko17fac552014-12-08 06:02:1766 // Writes to the socket.
[email protected]a2798d92011-03-02 22:56:1867 // Only usable from the client-side of a UDP socket, after the socket
68 // has been connected.
[email protected]83039bb2011-12-09 18:43:5569 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1870
hidehiko17fac552014-12-08 06:02:1771 // Reads from a socket and receive sender address information.
[email protected]a2798d92011-03-02 22:56:1872 // |buf| is the buffer to read data into.
73 // |buf_len| is the maximum amount of data to read.
74 // |address| is a buffer provided by the caller for receiving the sender
75 // address information about the received data. This buffer must be kept
76 // alive by the caller until the callback is placed.
77 // |address_length| is a ptr to the length of the |address| buffer. This
78 // is an input parameter containing the maximum size |address| can hold
79 // and also an output parameter for the size of |address| upon completion.
[email protected]73c5b692014-07-01 12:43:4180 // |callback| is the callback on completion of the RecvFrom.
[email protected]a2798d92011-03-02 22:56:1881 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
82 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
83 // and |address_length| alive until the callback is called.
84 int RecvFrom(IOBuffer* buf,
85 int buf_len,
[email protected]43d4a0262011-03-09 19:26:0486 IPEndPoint* address,
[email protected]3f55aa12011-12-07 02:03:3387 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1888
hidehiko17fac552014-12-08 06:02:1789 // Sends to a socket with a particular destination.
[email protected]a2798d92011-03-02 22:56:1890 // |buf| is the buffer to send
91 // |buf_len| is the number of bytes to send
92 // |address| is the recipient address.
93 // |address_length| is the size of the recipient address
94 // |callback| is the user callback function to call on complete.
95 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
96 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
97 // alive until the callback is called.
98 int SendTo(IOBuffer* buf,
99 int buf_len,
[email protected]43d4a0262011-03-09 19:26:04100 const IPEndPoint& address,
[email protected]83039bb2011-12-09 18:43:55101 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:18102
hidehiko17fac552014-12-08 06:02:17103 // Sets the receive buffer size (in bytes) for the socket.
104 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15105 int SetReceiveBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40106
hidehiko17fac552014-12-08 06:02:17107 // Sets the send buffer size (in bytes) for the socket.
108 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15109 int SetSendBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40110
[email protected]a2798d92011-03-02 22:56:18111 // Returns true if the socket is already connected or bound.
hidehiko17fac552014-12-08 06:02:17112 bool is_connected() const { return is_connected_; }
[email protected]a2798d92011-03-02 22:56:18113
[email protected]eaf10dc2011-07-18 21:47:35114 const BoundNetLog& NetLog() const { return net_log_; }
115
[email protected]a1781d7b2012-07-16 11:52:34116 // Sets corresponding flags in |socket_options_| to allow the socket
[email protected]7be809df2012-08-07 17:00:48117 // to share the local address to which the socket will be bound with
hidehiko17fac552014-12-08 06:02:17118 // other processes. Should be called between Open() and Bind().
119 // Returns a net error code.
120 int AllowAddressReuse();
[email protected]a1781d7b2012-07-16 11:52:34121
hidehiko17fac552014-12-08 06:02:17122 // Sets corresponding flags in |socket_options_| to allow or disallow sending
123 // and receiving packets to and from broadcast addresses.
124 // Returns a net error code.
125 int SetBroadcast(bool broadcast);
[email protected]a1781d7b2012-07-16 11:52:34126
hidehiko17fac552014-12-08 06:02:17127 // Joins the multicast group.
[email protected]5f01ce22013-04-30 00:53:18128 // |group_address| is the group address to join, could be either
129 // an IPv4 or IPv6 address.
hidehiko17fac552014-12-08 06:02:17130 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18131 int JoinGroup(const IPAddressNumber& group_address) const;
132
hidehiko17fac552014-12-08 06:02:17133 // Leaves the multicast group.
[email protected]5f01ce22013-04-30 00:53:18134 // |group_address| is the group address to leave, could be either
135 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
136 // it will be ignored.
137 // It's optional to leave the multicast group before destroying
138 // the socket. It will be done by the OS.
hidehiko17fac552014-12-08 06:02:17139 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18140 int LeaveGroup(const IPAddressNumber& group_address) const;
141
hidehiko17fac552014-12-08 06:02:17142 // Sets interface to use for multicast. If |interface_index| set to 0,
143 // default interface is used.
[email protected]7b29dac2013-12-05 11:19:42144 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17145 // Returns a net error code.
[email protected]7b29dac2013-12-05 11:19:42146 int SetMulticastInterface(uint32 interface_index);
147
hidehiko17fac552014-12-08 06:02:17148 // Sets the time-to-live option for UDP packets sent to the multicast
[email protected]5f01ce22013-04-30 00:53:18149 // group address. The default value of this option is 1.
150 // Cannot be negative or more than 255.
151 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17152 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18153 int SetMulticastTimeToLive(int time_to_live);
154
hidehiko17fac552014-12-08 06:02:17155 // Sets the loopback flag for UDP socket. If this flag is true, the host
[email protected]5f01ce22013-04-30 00:53:18156 // will receive packets sent to the joined group from itself.
157 // The default value of this option is true.
158 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17159 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18160 //
161 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
162 // different between Windows and Unix-like systems. The inconsistency only
163 // happens when there are more than one applications on the same host
164 // joined to the same multicast group while having different settings on
165 // multicast loopback mode. On Windows, the applications with loopback off
166 // will not RECEIVE the loopback packets; while on Unix-like systems, the
167 // applications with loopback off will not SEND the loopback packets to
168 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
169 int SetMulticastLoopbackMode(bool loopback);
170
hidehiko17fac552014-12-08 06:02:17171 // Sets the differentiated services flags on outgoing packets. May not
[email protected]a8a442b32013-10-22 00:34:41172 // do anything on some platforms.
hidehiko17fac552014-12-08 06:02:17173 // Returns a net error code.
[email protected]a8a442b32013-10-22 00:34:41174 int SetDiffServCodePoint(DiffServCodePoint dscp);
175
[email protected]1901dbcb2014-03-10 07:18:37176 // Resets the thread to be used for thread-safety checks.
177 void DetachFromThread();
178
[email protected]a2798d92011-03-02 22:56:18179 private:
[email protected]a1781d7b2012-07-16 11:52:34180 enum SocketOptions {
hidehiko17fac552014-12-08 06:02:17181 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
[email protected]a1781d7b2012-07-16 11:52:34182 };
183
[email protected]2da659e2013-05-23 20:51:34184 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18185 public:
186 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
187
188 // MessageLoopForIO::Watcher methods
189
dchengb03027d2014-10-21 12:00:20190 void OnFileCanReadWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18191
dchengb03027d2014-10-21 12:00:20192 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18193
194 private:
195 UDPSocketLibevent* const socket_;
196
197 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
198 };
199
[email protected]2da659e2013-05-23 20:51:34200 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18201 public:
202 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
203
204 // MessageLoopForIO::Watcher methods
205
dchengb03027d2014-10-21 12:00:20206 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18207
dchengb03027d2014-10-21 12:00:20208 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18209
210 private:
211 UDPSocketLibevent* const socket_;
212
213 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
214 };
215
216 void DoReadCallback(int rv);
217 void DoWriteCallback(int rv);
218 void DidCompleteRead();
219 void DidCompleteWrite();
220
[email protected]8866f622011-10-18 20:08:10221 // Handles stats and logging. |result| is the number of bytes transferred, on
222 // success, or the net error code on failure. On success, LogRead takes in a
223 // sockaddr and its length, which are mandatory, while LogWrite takes in an
224 // optional IPEndPoint.
225 void LogRead(int result, const char* bytes, socklen_t addr_len,
226 const sockaddr* addr) const;
227 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
228
[email protected]06043ae2011-03-16 00:00:21229 // Same as SendTo(), except that address is passed by pointer
230 // instead of by reference. It is called from Write() with |address|
231 // set to NULL.
232 int SendToOrWrite(IOBuffer* buf,
233 int buf_len,
234 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55235 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21236
[email protected]8866f622011-10-18 20:08:10237 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21238 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
239 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18240
[email protected]a1781d7b2012-07-16 11:52:34241 // Applies |socket_options_| to |socket_|. Should be called before
242 // Bind().
hidehiko17fac552014-12-08 06:02:17243 int SetMulticastOptions();
[email protected]5370c012011-06-29 03:47:04244 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56245 // Binds to a random port on |address|.
246 int RandomBind(const IPAddressNumber& address);
[email protected]5370c012011-06-29 03:47:04247
[email protected]a2798d92011-03-02 22:56:18248 int socket_;
sergeyu30ad5b32015-03-27 20:31:02249
[email protected]5f01ce22013-04-30 00:53:18250 int addr_family_;
hidehiko17fac552014-12-08 06:02:17251 bool is_connected_;
[email protected]a2798d92011-03-02 22:56:18252
[email protected]7be809df2012-08-07 17:00:48253 // Bitwise-or'd combination of SocketOptions. Specifies the set of
254 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34255 int socket_options_;
256
[email protected]7b29dac2013-12-05 11:19:42257 // Multicast interface.
258 uint32 multicast_interface_;
259
hidehiko17fac552014-12-08 06:02:17260 // Multicast socket options cached for SetMulticastOption.
[email protected]5f01ce22013-04-30 00:53:18261 // Cannot be used after Bind().
262 int multicast_time_to_live_;
263
[email protected]5370c012011-06-29 03:47:04264 // How to do source port binding, used only when UDPSocket is part of
265 // UDPClientSocket, since UDPServerSocket provides Bind.
266 DatagramSocket::BindType bind_type_;
267
268 // PRNG function for generating port numbers.
269 RandIntCallback rand_int_cb_;
270
[email protected]a2798d92011-03-02 22:56:18271 // These are mutable since they're just cached copies to make
272 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04273 mutable scoped_ptr<IPEndPoint> local_address_;
274 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18275
276 // The socket's libevent wrappers
[email protected]2da659e2013-05-23 20:51:34277 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
278 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18279
280 // The corresponding watchers for reads and writes.
281 ReadWatcher read_watcher_;
282 WriteWatcher write_watcher_;
283
[email protected]43d4a0262011-03-09 19:26:04284 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18285 scoped_refptr<IOBuffer> read_buf_;
286 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04287 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18288
[email protected]43d4a0262011-03-09 19:26:04289 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18290 scoped_refptr<IOBuffer> write_buf_;
291 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04292 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18293
294 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33295 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18296
297 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55298 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18299
300 BoundNetLog net_log_;
301
302 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
303};
304
305} // namespace net
306
307#endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_