blob: ba42e04430cac96328201d18557a543138939598 [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
tfarina6f2f3beb2017-04-19 11:53:14136 // Call this to enable SO_REUSEADDR on the underlying socket.
137 // Should be called between Open() and Bind().
hidehiko17fac552014-12-08 06:02:17138 // Returns a net error code.
139 int AllowAddressReuse();
[email protected]a1781d7b2012-07-16 11:52:34140
tfarina6f2f3beb2017-04-19 11:53:14141 // Call this to allow or disallow sending and receiving packets to and from
142 // broadcast addresses.
hidehiko17fac552014-12-08 06:02:17143 // Returns a net error code.
144 int SetBroadcast(bool broadcast);
[email protected]a1781d7b2012-07-16 11:52:34145
hidehiko17fac552014-12-08 06:02:17146 // Joins the multicast group.
[email protected]5f01ce22013-04-30 00:53:18147 // |group_address| is the group address to join, could be either
148 // an IPv4 or IPv6 address.
hidehiko17fac552014-12-08 06:02:17149 // Returns a net error code.
martijnd8dafab2016-03-14 18:28:49150 int JoinGroup(const IPAddress& group_address) const;
[email protected]5f01ce22013-04-30 00:53:18151
hidehiko17fac552014-12-08 06:02:17152 // Leaves the multicast group.
[email protected]5f01ce22013-04-30 00:53:18153 // |group_address| is the group address to leave, could be either
154 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
155 // it will be ignored.
156 // It's optional to leave the multicast group before destroying
157 // the socket. It will be done by the OS.
hidehiko17fac552014-12-08 06:02:17158 // Returns a net error code.
martijnd8dafab2016-03-14 18:28:49159 int LeaveGroup(const IPAddress& group_address) const;
[email protected]5f01ce22013-04-30 00:53:18160
hidehiko17fac552014-12-08 06:02:17161 // Sets interface to use for multicast. If |interface_index| set to 0,
162 // default interface is used.
[email protected]7b29dac2013-12-05 11:19:42163 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17164 // Returns a net error code.
tfarinac40df1c2015-11-30 22:31:47165 int SetMulticastInterface(uint32_t interface_index);
[email protected]7b29dac2013-12-05 11:19:42166
hidehiko17fac552014-12-08 06:02:17167 // Sets the time-to-live option for UDP packets sent to the multicast
[email protected]5f01ce22013-04-30 00:53:18168 // group address. The default value of this option is 1.
169 // Cannot be negative or more than 255.
170 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17171 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18172 int SetMulticastTimeToLive(int time_to_live);
173
hidehiko17fac552014-12-08 06:02:17174 // Sets the loopback flag for UDP socket. If this flag is true, the host
[email protected]5f01ce22013-04-30 00:53:18175 // will receive packets sent to the joined group from itself.
176 // The default value of this option is true.
177 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17178 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18179 //
180 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
181 // different between Windows and Unix-like systems. The inconsistency only
182 // happens when there are more than one applications on the same host
183 // joined to the same multicast group while having different settings on
184 // multicast loopback mode. On Windows, the applications with loopback off
185 // will not RECEIVE the loopback packets; while on Unix-like systems, the
186 // applications with loopback off will not SEND the loopback packets to
187 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
188 int SetMulticastLoopbackMode(bool loopback);
189
hidehiko17fac552014-12-08 06:02:17190 // Sets the differentiated services flags on outgoing packets. May not
[email protected]a8a442b32013-10-22 00:34:41191 // do anything on some platforms.
hidehiko17fac552014-12-08 06:02:17192 // Returns a net error code.
[email protected]a8a442b32013-10-22 00:34:41193 int SetDiffServCodePoint(DiffServCodePoint dscp);
194
[email protected]1901dbcb2014-03-10 07:18:37195 // Resets the thread to be used for thread-safety checks.
196 void DetachFromThread();
197
[email protected]a2798d92011-03-02 22:56:18198 private:
[email protected]a1781d7b2012-07-16 11:52:34199 enum SocketOptions {
hidehiko17fac552014-12-08 06:02:17200 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
[email protected]a1781d7b2012-07-16 11:52:34201 };
202
[email protected]2da659e2013-05-23 20:51:34203 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18204 public:
tfarina4eb7aad82015-09-14 17:10:34205 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18206
207 // MessageLoopForIO::Watcher methods
208
dchengb03027d2014-10-21 12:00:20209 void OnFileCanReadWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18210
dchengb03027d2014-10-21 12:00:20211 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18212
213 private:
tfarina4eb7aad82015-09-14 17:10:34214 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18215
216 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
217 };
218
[email protected]2da659e2013-05-23 20:51:34219 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18220 public:
tfarina4eb7aad82015-09-14 17:10:34221 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18222
223 // MessageLoopForIO::Watcher methods
224
dchengb03027d2014-10-21 12:00:20225 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18226
dchengb03027d2014-10-21 12:00:20227 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18228
229 private:
tfarina4eb7aad82015-09-14 17:10:34230 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18231
232 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
233 };
234
235 void DoReadCallback(int rv);
236 void DoWriteCallback(int rv);
237 void DidCompleteRead();
238 void DidCompleteWrite();
239
[email protected]8866f622011-10-18 20:08:10240 // Handles stats and logging. |result| is the number of bytes transferred, on
241 // success, or the net error code on failure. On success, LogRead takes in a
242 // sockaddr and its length, which are mandatory, while LogWrite takes in an
243 // optional IPEndPoint.
244 void LogRead(int result, const char* bytes, socklen_t addr_len,
245 const sockaddr* addr) const;
246 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
247
[email protected]06043ae2011-03-16 00:00:21248 // Same as SendTo(), except that address is passed by pointer
249 // instead of by reference. It is called from Write() with |address|
250 // set to NULL.
251 int SendToOrWrite(IOBuffer* buf,
252 int buf_len,
253 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55254 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21255
[email protected]8866f622011-10-18 20:08:10256 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21257 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
258 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18259
[email protected]a1781d7b2012-07-16 11:52:34260 // Applies |socket_options_| to |socket_|. Should be called before
261 // Bind().
hidehiko17fac552014-12-08 06:02:17262 int SetMulticastOptions();
[email protected]5370c012011-06-29 03:47:04263 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56264 // Binds to a random port on |address|.
martijnd8dafab2016-03-14 18:28:49265 int RandomBind(const IPAddress& address);
[email protected]5370c012011-06-29 03:47:04266
[email protected]a2798d92011-03-02 22:56:18267 int socket_;
sergeyu30ad5b32015-03-27 20:31:02268
[email protected]5f01ce22013-04-30 00:53:18269 int addr_family_;
hidehiko17fac552014-12-08 06:02:17270 bool is_connected_;
[email protected]a2798d92011-03-02 22:56:18271
[email protected]7be809df2012-08-07 17:00:48272 // Bitwise-or'd combination of SocketOptions. Specifies the set of
273 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34274 int socket_options_;
275
[email protected]7b29dac2013-12-05 11:19:42276 // Multicast interface.
tfarinac40df1c2015-11-30 22:31:47277 uint32_t multicast_interface_;
[email protected]7b29dac2013-12-05 11:19:42278
hidehiko17fac552014-12-08 06:02:17279 // Multicast socket options cached for SetMulticastOption.
[email protected]5f01ce22013-04-30 00:53:18280 // Cannot be used after Bind().
281 int multicast_time_to_live_;
282
[email protected]5370c012011-06-29 03:47:04283 // How to do source port binding, used only when UDPSocket is part of
284 // UDPClientSocket, since UDPServerSocket provides Bind.
285 DatagramSocket::BindType bind_type_;
286
287 // PRNG function for generating port numbers.
288 RandIntCallback rand_int_cb_;
289
[email protected]a2798d92011-03-02 22:56:18290 // These are mutable since they're just cached copies to make
291 // GetPeerAddress/GetLocalAddress smarter.
danakja9850e12016-04-18 22:28:08292 mutable std::unique_ptr<IPEndPoint> local_address_;
293 mutable std::unique_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18294
tfarina4eb7aad82015-09-14 17:10:34295 // The socket's posix wrappers
[email protected]2da659e2013-05-23 20:51:34296 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
297 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18298
299 // The corresponding watchers for reads and writes.
300 ReadWatcher read_watcher_;
301 WriteWatcher write_watcher_;
302
[email protected]43d4a0262011-03-09 19:26:04303 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18304 scoped_refptr<IOBuffer> read_buf_;
305 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04306 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18307
[email protected]43d4a0262011-03-09 19:26:04308 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18309 scoped_refptr<IOBuffer> write_buf_;
310 int write_buf_len_;
danakja9850e12016-04-18 22:28:08311 std::unique_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18312
313 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33314 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18315
316 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55317 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18318
tfarina42834112016-09-22 13:38:20319 NetLogWithSource net_log_;
[email protected]a2798d92011-03-02 22:56:18320
pauljensenf0d9535a2016-06-10 19:51:19321 // Network that this socket is bound to via BindToNetwork().
322 NetworkChangeNotifier::NetworkHandle bound_network_;
323
tfarina4eb7aad82015-09-14 17:10:34324 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix);
[email protected]a2798d92011-03-02 22:56:18325};
326
327} // namespace net
328
tfarina5dd13c22016-11-16 12:08:26329#endif // NET_SOCKET_UDP_SOCKET_POSIX_H_