blob: 3d71835d2c713fd7ece1378d47f0bc05d03e0c84 [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
tfarina5dd13c22016-11-16 12:08:265#ifndef NET_SOCKET_UDP_SOCKET_POSIX_H_
6#define NET_SOCKET_UDP_SOCKET_POSIX_H_
[email protected]a2798d92011-03-02 22:56:187
tfarinac40df1c2015-11-30 22:31:478#include <stdint.h>
9
danakja9850e12016-04-18 22:28:0810#include <memory>
11
Avi Drissman13fc8932015-12-20 04:40:4612#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]7f86564d2013-07-18 00:41:2214#include "base/message_loop/message_loop.h"
[email protected]a2798d92011-03-02 22:56:1815#include "base/threading/non_thread_safe.h"
hidehiko17fac552014-12-08 06:02:1716#include "net/base/address_family.h"
[email protected]a2798d92011-03-02 22:56:1817#include "net/base/completion_callback.h"
[email protected]5370c012011-06-29 03:47:0418#include "net/base/io_buffer.h"
[email protected]43d4a0262011-03-09 19:26:0419#include "net/base/ip_endpoint.h"
[email protected]7f86564d2013-07-18 00:41:2220#include "net/base/net_export.h"
tfarina42f7ce832015-10-08 20:45:5321#include "net/base/network_change_notifier.h"
[email protected]7f86564d2013-07-18 00:41:2222#include "net/base/rand_callback.h"
mikecironef22f9812016-10-04 03:40:1923#include "net/log/net_log_with_source.h"
tfarina5dd13c22016-11-16 12:08:2624#include "net/socket/datagram_socket.h"
25#include "net/socket/diff_serv_code_point.h"
[email protected]bbfef1f2013-08-28 03:00:5126#include "net/socket/socket_descriptor.h"
[email protected]a2798d92011-03-02 22:56:1827
28namespace net {
29
martijnd8dafab2016-03-14 18:28:4930class IPAddress;
mikecironef22f9812016-10-04 03:40:1931class NetLog;
32struct NetLogSource;
martijnd8dafab2016-03-14 18:28:4933
tfarina4eb7aad82015-09-14 17:10:3434class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe {
[email protected]a2798d92011-03-02 22:56:1835 public:
tfarina4eb7aad82015-09-14 17:10:3436 UDPSocketPosix(DatagramSocket::BindType bind_type,
37 const RandIntCallback& rand_int_cb,
38 net::NetLog* net_log,
mikecironef22f9812016-10-04 03:40:1939 const net::NetLogSource& source);
tfarina4eb7aad82015-09-14 17:10:3440 virtual ~UDPSocketPosix();
[email protected]a2798d92011-03-02 22:56:1841
hidehiko17fac552014-12-08 06:02:1742 // Opens the socket.
43 // Returns a net error code.
44 int Open(AddressFamily address_family);
45
pauljensenbe5bc3232015-10-05 20:39:2746 // Binds this socket to |network|. All data traffic on the socket will be sent
47 // and received via |network|. Must be called before Connect(). This call will
48 // fail if |network| has disconnected. Communication using this socket will
49 // fail if |network| disconnects.
50 // Returns a net error code.
51 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network);
52
hidehiko17fac552014-12-08 06:02:1753 // Connects the socket to connect with a certain |address|.
54 // Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1855 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0456 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1857
hidehiko17fac552014-12-08 06:02:1758 // Binds the address/port for this socket to |address|. This is generally
59 // only used on a server. Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1860 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0461 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1862
hidehiko17fac552014-12-08 06:02:1763 // Closes the socket.
64 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
[email protected]a2798d92011-03-02 22:56:1865 void Close();
66
hidehiko17fac552014-12-08 06:02:1767 // Copies the remote udp address into |address| and returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0468 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1869
hidehiko17fac552014-12-08 06:02:1770 // Copies the local udp address into |address| and returns a net error code.
[email protected]a2798d92011-03-02 22:56:1871 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0472 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1873
74 // IO:
75 // Multiple outstanding read requests are not supported.
76 // Full duplex mode (reading and writing at the same time) is supported
77
hidehiko17fac552014-12-08 06:02:1778 // Reads from the socket.
[email protected]a2798d92011-03-02 22:56:1879 // Only usable from the client-side of a UDP socket, after the socket
80 // has been connected.
[email protected]3f55aa12011-12-07 02:03:3381 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1882
hidehiko17fac552014-12-08 06:02:1783 // Writes to the socket.
[email protected]a2798d92011-03-02 22:56:1884 // Only usable from the client-side of a UDP socket, after the socket
85 // has been connected.
[email protected]83039bb2011-12-09 18:43:5586 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1887
hidehiko17fac552014-12-08 06:02:1788 // Reads from a socket and receive sender address information.
[email protected]a2798d92011-03-02 22:56:1889 // |buf| is the buffer to read data into.
90 // |buf_len| is the maximum amount of data to read.
91 // |address| is a buffer provided by the caller for receiving the sender
92 // address information about the received data. This buffer must be kept
93 // alive by the caller until the callback is placed.
[email protected]73c5b692014-07-01 12:43:4194 // |callback| is the callback on completion of the RecvFrom.
[email protected]a2798d92011-03-02 22:56:1895 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
tfarina39524dd2016-02-19 10:52:1096 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
97 // alive until the callback is called.
[email protected]a2798d92011-03-02 22:56:1898 int RecvFrom(IOBuffer* buf,
99 int buf_len,
[email protected]43d4a0262011-03-09 19:26:04100 IPEndPoint* address,
[email protected]3f55aa12011-12-07 02:03:33101 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:18102
hidehiko17fac552014-12-08 06:02:17103 // Sends to a socket with a particular destination.
tfarina39524dd2016-02-19 10:52:10104 // |buf| is the buffer to send.
105 // |buf_len| is the number of bytes to send.
[email protected]a2798d92011-03-02 22:56:18106 // |address| is the recipient address.
[email protected]a2798d92011-03-02 22:56:18107 // |callback| is the user callback function to call on complete.
108 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
109 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
110 // alive until the callback is called.
111 int SendTo(IOBuffer* buf,
112 int buf_len,
[email protected]43d4a0262011-03-09 19:26:04113 const IPEndPoint& address,
[email protected]83039bb2011-12-09 18:43:55114 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:18115
hidehiko17fac552014-12-08 06:02:17116 // Sets the receive buffer size (in bytes) for the socket.
117 // Returns a net error code.
tfarinac40df1c2015-11-30 22:31:47118 int SetReceiveBufferSize(int32_t size);
[email protected]df31da42011-10-18 01:44:40119
hidehiko17fac552014-12-08 06:02:17120 // Sets the send buffer size (in bytes) for the socket.
121 // Returns a net error code.
tfarinac40df1c2015-11-30 22:31:47122 int SetSendBufferSize(int32_t size);
[email protected]df31da42011-10-18 01:44:40123
rchff006a12016-08-24 23:56:31124 // Requests that packets sent by this socket not be fragment, either locally
125 // by the host, or by routers (via the DF bit in the IPv4 packet header).
126 // May not be supported by all platforms. Returns a return a network error
127 // code if there was a problem, but the socket will still be usable. Can not
128 // return ERR_IO_PENDING.
129 int SetDoNotFragment();
130
[email protected]a2798d92011-03-02 22:56:18131 // Returns true if the socket is already connected or bound.
hidehiko17fac552014-12-08 06:02:17132 bool is_connected() const { return is_connected_; }
[email protected]a2798d92011-03-02 22:56:18133
tfarina42834112016-09-22 13:38:20134 const NetLogWithSource& NetLog() const { return net_log_; }
[email protected]eaf10dc2011-07-18 21:47:35135
[email protected]a1781d7b2012-07-16 11:52:34136 // Sets corresponding flags in |socket_options_| to allow the socket
[email protected]7be809df2012-08-07 17:00:48137 // to share the local address to which the socket will be bound with
hidehiko17fac552014-12-08 06:02:17138 // other processes. Should be called between Open() and Bind().
139 // Returns a net error code.
140 int AllowAddressReuse();
[email protected]a1781d7b2012-07-16 11:52:34141
hidehiko17fac552014-12-08 06:02:17142 // Sets corresponding flags in |socket_options_| to allow or disallow sending
143 // and receiving packets to and from broadcast addresses.
144 // Returns a net error code.
145 int SetBroadcast(bool broadcast);
[email protected]a1781d7b2012-07-16 11:52:34146
hidehiko17fac552014-12-08 06:02:17147 // Joins the multicast group.
[email protected]5f01ce22013-04-30 00:53:18148 // |group_address| is the group address to join, could be either
149 // an IPv4 or IPv6 address.
hidehiko17fac552014-12-08 06:02:17150 // Returns a net error code.
martijnd8dafab2016-03-14 18:28:49151 int JoinGroup(const IPAddress& group_address) const;
[email protected]5f01ce22013-04-30 00:53:18152
hidehiko17fac552014-12-08 06:02:17153 // Leaves the multicast group.
[email protected]5f01ce22013-04-30 00:53:18154 // |group_address| is the group address to leave, could be either
155 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
156 // it will be ignored.
157 // It's optional to leave the multicast group before destroying
158 // the socket. It will be done by the OS.
hidehiko17fac552014-12-08 06:02:17159 // Returns a net error code.
martijnd8dafab2016-03-14 18:28:49160 int LeaveGroup(const IPAddress& group_address) const;
[email protected]5f01ce22013-04-30 00:53:18161
hidehiko17fac552014-12-08 06:02:17162 // Sets interface to use for multicast. If |interface_index| set to 0,
163 // default interface is used.
[email protected]7b29dac2013-12-05 11:19:42164 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17165 // Returns a net error code.
tfarinac40df1c2015-11-30 22:31:47166 int SetMulticastInterface(uint32_t interface_index);
[email protected]7b29dac2013-12-05 11:19:42167
hidehiko17fac552014-12-08 06:02:17168 // Sets the time-to-live option for UDP packets sent to the multicast
[email protected]5f01ce22013-04-30 00:53:18169 // group address. The default value of this option is 1.
170 // Cannot be negative or more than 255.
171 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17172 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18173 int SetMulticastTimeToLive(int time_to_live);
174
hidehiko17fac552014-12-08 06:02:17175 // Sets the loopback flag for UDP socket. If this flag is true, the host
[email protected]5f01ce22013-04-30 00:53:18176 // will receive packets sent to the joined group from itself.
177 // The default value of this option is true.
178 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17179 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18180 //
181 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
182 // different between Windows and Unix-like systems. The inconsistency only
183 // happens when there are more than one applications on the same host
184 // joined to the same multicast group while having different settings on
185 // multicast loopback mode. On Windows, the applications with loopback off
186 // will not RECEIVE the loopback packets; while on Unix-like systems, the
187 // applications with loopback off will not SEND the loopback packets to
188 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
189 int SetMulticastLoopbackMode(bool loopback);
190
hidehiko17fac552014-12-08 06:02:17191 // Sets the differentiated services flags on outgoing packets. May not
[email protected]a8a442b32013-10-22 00:34:41192 // do anything on some platforms.
hidehiko17fac552014-12-08 06:02:17193 // Returns a net error code.
[email protected]a8a442b32013-10-22 00:34:41194 int SetDiffServCodePoint(DiffServCodePoint dscp);
195
[email protected]1901dbcb2014-03-10 07:18:37196 // Resets the thread to be used for thread-safety checks.
197 void DetachFromThread();
198
[email protected]a2798d92011-03-02 22:56:18199 private:
[email protected]a1781d7b2012-07-16 11:52:34200 enum SocketOptions {
hidehiko17fac552014-12-08 06:02:17201 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
[email protected]a1781d7b2012-07-16 11:52:34202 };
203
[email protected]2da659e2013-05-23 20:51:34204 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18205 public:
tfarina4eb7aad82015-09-14 17:10:34206 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18207
208 // MessageLoopForIO::Watcher methods
209
dchengb03027d2014-10-21 12:00:20210 void OnFileCanReadWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18211
dchengb03027d2014-10-21 12:00:20212 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18213
214 private:
tfarina4eb7aad82015-09-14 17:10:34215 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18216
217 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
218 };
219
[email protected]2da659e2013-05-23 20:51:34220 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18221 public:
tfarina4eb7aad82015-09-14 17:10:34222 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18223
224 // MessageLoopForIO::Watcher methods
225
dchengb03027d2014-10-21 12:00:20226 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18227
dchengb03027d2014-10-21 12:00:20228 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18229
230 private:
tfarina4eb7aad82015-09-14 17:10:34231 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18232
233 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
234 };
235
236 void DoReadCallback(int rv);
237 void DoWriteCallback(int rv);
238 void DidCompleteRead();
239 void DidCompleteWrite();
240
[email protected]8866f622011-10-18 20:08:10241 // Handles stats and logging. |result| is the number of bytes transferred, on
242 // success, or the net error code on failure. On success, LogRead takes in a
243 // sockaddr and its length, which are mandatory, while LogWrite takes in an
244 // optional IPEndPoint.
245 void LogRead(int result, const char* bytes, socklen_t addr_len,
246 const sockaddr* addr) const;
247 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
248
[email protected]06043ae2011-03-16 00:00:21249 // Same as SendTo(), except that address is passed by pointer
250 // instead of by reference. It is called from Write() with |address|
251 // set to NULL.
252 int SendToOrWrite(IOBuffer* buf,
253 int buf_len,
254 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55255 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21256
[email protected]8866f622011-10-18 20:08:10257 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21258 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
259 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18260
[email protected]a1781d7b2012-07-16 11:52:34261 // Applies |socket_options_| to |socket_|. Should be called before
262 // Bind().
hidehiko17fac552014-12-08 06:02:17263 int SetMulticastOptions();
[email protected]5370c012011-06-29 03:47:04264 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56265 // Binds to a random port on |address|.
martijnd8dafab2016-03-14 18:28:49266 int RandomBind(const IPAddress& address);
[email protected]5370c012011-06-29 03:47:04267
[email protected]a2798d92011-03-02 22:56:18268 int socket_;
sergeyu30ad5b32015-03-27 20:31:02269
[email protected]5f01ce22013-04-30 00:53:18270 int addr_family_;
hidehiko17fac552014-12-08 06:02:17271 bool is_connected_;
[email protected]a2798d92011-03-02 22:56:18272
[email protected]7be809df2012-08-07 17:00:48273 // Bitwise-or'd combination of SocketOptions. Specifies the set of
274 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34275 int socket_options_;
276
[email protected]7b29dac2013-12-05 11:19:42277 // Multicast interface.
tfarinac40df1c2015-11-30 22:31:47278 uint32_t multicast_interface_;
[email protected]7b29dac2013-12-05 11:19:42279
hidehiko17fac552014-12-08 06:02:17280 // Multicast socket options cached for SetMulticastOption.
[email protected]5f01ce22013-04-30 00:53:18281 // Cannot be used after Bind().
282 int multicast_time_to_live_;
283
[email protected]5370c012011-06-29 03:47:04284 // How to do source port binding, used only when UDPSocket is part of
285 // UDPClientSocket, since UDPServerSocket provides Bind.
286 DatagramSocket::BindType bind_type_;
287
288 // PRNG function for generating port numbers.
289 RandIntCallback rand_int_cb_;
290
[email protected]a2798d92011-03-02 22:56:18291 // These are mutable since they're just cached copies to make
292 // GetPeerAddress/GetLocalAddress smarter.
danakja9850e12016-04-18 22:28:08293 mutable std::unique_ptr<IPEndPoint> local_address_;
294 mutable std::unique_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18295
tfarina4eb7aad82015-09-14 17:10:34296 // The socket's posix wrappers
[email protected]2da659e2013-05-23 20:51:34297 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
298 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18299
300 // The corresponding watchers for reads and writes.
301 ReadWatcher read_watcher_;
302 WriteWatcher write_watcher_;
303
[email protected]43d4a0262011-03-09 19:26:04304 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18305 scoped_refptr<IOBuffer> read_buf_;
306 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04307 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18308
[email protected]43d4a0262011-03-09 19:26:04309 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18310 scoped_refptr<IOBuffer> write_buf_;
311 int write_buf_len_;
danakja9850e12016-04-18 22:28:08312 std::unique_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18313
314 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33315 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18316
317 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55318 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18319
tfarina42834112016-09-22 13:38:20320 NetLogWithSource net_log_;
[email protected]a2798d92011-03-02 22:56:18321
pauljensenf0d9535a2016-06-10 19:51:19322 // Network that this socket is bound to via BindToNetwork().
323 NetworkChangeNotifier::NetworkHandle bound_network_;
324
tfarina4eb7aad82015-09-14 17:10:34325 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix);
[email protected]a2798d92011-03-02 22:56:18326};
327
328} // namespace net
329
tfarina5dd13c22016-11-16 12:08:26330#endif // NET_SOCKET_UDP_SOCKET_POSIX_H_