blob: 44547d5fa0c1d6056946e0c556bfe839498b0e65 [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
tfarina4eb7aad82015-09-14 17:10:345#ifndef NET_UDP_UDP_SOCKET_POSIX_H_
6#define NET_UDP_UDP_SOCKET_POSIX_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"
tfarina42f7ce832015-10-08 20:45:5317#include "net/base/network_change_notifier.h"
[email protected]7f86564d2013-07-18 00:41:2218#include "net/base/rand_callback.h"
eroman87c53d62015-04-02 06:51:0719#include "net/log/net_log.h"
[email protected]bbfef1f2013-08-28 03:00:5120#include "net/socket/socket_descriptor.h"
[email protected]5370c012011-06-29 03:47:0421#include "net/udp/datagram_socket.h"
tfarina42f7ce832015-10-08 20:45:5322#include "net/udp/diff_serv_code_point.h"
[email protected]a2798d92011-03-02 22:56:1823
24namespace net {
25
tfarina4eb7aad82015-09-14 17:10:3426class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe {
[email protected]a2798d92011-03-02 22:56:1827 public:
tfarina4eb7aad82015-09-14 17:10:3428 UDPSocketPosix(DatagramSocket::BindType bind_type,
29 const RandIntCallback& rand_int_cb,
30 net::NetLog* net_log,
31 const net::NetLog::Source& source);
32 virtual ~UDPSocketPosix();
[email protected]a2798d92011-03-02 22:56:1833
hidehiko17fac552014-12-08 06:02:1734 // Opens the socket.
35 // Returns a net error code.
36 int Open(AddressFamily address_family);
37
pauljensenbe5bc3232015-10-05 20:39:2738 // Binds this socket to |network|. All data traffic on the socket will be sent
39 // and received via |network|. Must be called before Connect(). This call will
40 // fail if |network| has disconnected. Communication using this socket will
41 // fail if |network| disconnects.
42 // Returns a net error code.
43 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network);
44
hidehiko17fac552014-12-08 06:02:1745 // Connects the socket to connect with a certain |address|.
46 // Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1847 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0448 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1849
hidehiko17fac552014-12-08 06:02:1750 // Binds the address/port for this socket to |address|. This is generally
51 // only used on a server. Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1852 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0453 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1854
hidehiko17fac552014-12-08 06:02:1755 // Closes the socket.
56 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
[email protected]a2798d92011-03-02 22:56:1857 void Close();
58
hidehiko17fac552014-12-08 06:02:1759 // Copies the remote udp address into |address| and returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0460 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1861
hidehiko17fac552014-12-08 06:02:1762 // Copies the local udp address into |address| and returns a net error code.
[email protected]a2798d92011-03-02 22:56:1863 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0464 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1865
66 // IO:
67 // Multiple outstanding read requests are not supported.
68 // Full duplex mode (reading and writing at the same time) is supported
69
hidehiko17fac552014-12-08 06:02:1770 // Reads from the socket.
[email protected]a2798d92011-03-02 22:56:1871 // Only usable from the client-side of a UDP socket, after the socket
72 // has been connected.
[email protected]3f55aa12011-12-07 02:03:3373 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1874
hidehiko17fac552014-12-08 06:02:1775 // Writes to the socket.
[email protected]a2798d92011-03-02 22:56:1876 // Only usable from the client-side of a UDP socket, after the socket
77 // has been connected.
[email protected]83039bb2011-12-09 18:43:5578 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1879
hidehiko17fac552014-12-08 06:02:1780 // Reads from a socket and receive sender address information.
[email protected]a2798d92011-03-02 22:56:1881 // |buf| is the buffer to read data into.
82 // |buf_len| is the maximum amount of data to read.
83 // |address| is a buffer provided by the caller for receiving the sender
84 // address information about the received data. This buffer must be kept
85 // alive by the caller until the callback is placed.
86 // |address_length| is a ptr to the length of the |address| buffer. This
87 // is an input parameter containing the maximum size |address| can hold
88 // and also an output parameter for the size of |address| upon completion.
[email protected]73c5b692014-07-01 12:43:4189 // |callback| is the callback on completion of the RecvFrom.
[email protected]a2798d92011-03-02 22:56:1890 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
91 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
92 // and |address_length| alive until the callback is called.
93 int RecvFrom(IOBuffer* buf,
94 int buf_len,
[email protected]43d4a0262011-03-09 19:26:0495 IPEndPoint* address,
[email protected]3f55aa12011-12-07 02:03:3396 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1897
hidehiko17fac552014-12-08 06:02:1798 // Sends to a socket with a particular destination.
[email protected]a2798d92011-03-02 22:56:1899 // |buf| is the buffer to send
100 // |buf_len| is the number of bytes to send
101 // |address| is the recipient address.
102 // |address_length| is the size of the recipient address
103 // |callback| is the user callback function to call on complete.
104 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
105 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
106 // alive until the callback is called.
107 int SendTo(IOBuffer* buf,
108 int buf_len,
[email protected]43d4a0262011-03-09 19:26:04109 const IPEndPoint& address,
[email protected]83039bb2011-12-09 18:43:55110 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:18111
hidehiko17fac552014-12-08 06:02:17112 // Sets the receive buffer size (in bytes) for the socket.
113 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15114 int SetReceiveBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40115
hidehiko17fac552014-12-08 06:02:17116 // Sets the send buffer size (in bytes) for the socket.
117 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15118 int SetSendBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40119
[email protected]a2798d92011-03-02 22:56:18120 // Returns true if the socket is already connected or bound.
hidehiko17fac552014-12-08 06:02:17121 bool is_connected() const { return is_connected_; }
[email protected]a2798d92011-03-02 22:56:18122
[email protected]eaf10dc2011-07-18 21:47:35123 const BoundNetLog& NetLog() const { return net_log_; }
124
[email protected]a1781d7b2012-07-16 11:52:34125 // Sets corresponding flags in |socket_options_| to allow the socket
[email protected]7be809df2012-08-07 17:00:48126 // to share the local address to which the socket will be bound with
hidehiko17fac552014-12-08 06:02:17127 // other processes. Should be called between Open() and Bind().
128 // Returns a net error code.
129 int AllowAddressReuse();
[email protected]a1781d7b2012-07-16 11:52:34130
hidehiko17fac552014-12-08 06:02:17131 // Sets corresponding flags in |socket_options_| to allow or disallow sending
132 // and receiving packets to and from broadcast addresses.
133 // Returns a net error code.
134 int SetBroadcast(bool broadcast);
[email protected]a1781d7b2012-07-16 11:52:34135
hidehiko17fac552014-12-08 06:02:17136 // Joins the multicast group.
[email protected]5f01ce22013-04-30 00:53:18137 // |group_address| is the group address to join, could be either
138 // an IPv4 or IPv6 address.
hidehiko17fac552014-12-08 06:02:17139 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18140 int JoinGroup(const IPAddressNumber& group_address) const;
141
hidehiko17fac552014-12-08 06:02:17142 // Leaves the multicast group.
[email protected]5f01ce22013-04-30 00:53:18143 // |group_address| is the group address to leave, could be either
144 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
145 // it will be ignored.
146 // It's optional to leave the multicast group before destroying
147 // the socket. It will be done by the OS.
hidehiko17fac552014-12-08 06:02:17148 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18149 int LeaveGroup(const IPAddressNumber& group_address) const;
150
hidehiko17fac552014-12-08 06:02:17151 // Sets interface to use for multicast. If |interface_index| set to 0,
152 // default interface is used.
[email protected]7b29dac2013-12-05 11:19:42153 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17154 // Returns a net error code.
[email protected]7b29dac2013-12-05 11:19:42155 int SetMulticastInterface(uint32 interface_index);
156
hidehiko17fac552014-12-08 06:02:17157 // Sets the time-to-live option for UDP packets sent to the multicast
[email protected]5f01ce22013-04-30 00:53:18158 // group address. The default value of this option is 1.
159 // Cannot be negative or more than 255.
160 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17161 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18162 int SetMulticastTimeToLive(int time_to_live);
163
hidehiko17fac552014-12-08 06:02:17164 // Sets the loopback flag for UDP socket. If this flag is true, the host
[email protected]5f01ce22013-04-30 00:53:18165 // will receive packets sent to the joined group from itself.
166 // The default value of this option is true.
167 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17168 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18169 //
170 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
171 // different between Windows and Unix-like systems. The inconsistency only
172 // happens when there are more than one applications on the same host
173 // joined to the same multicast group while having different settings on
174 // multicast loopback mode. On Windows, the applications with loopback off
175 // will not RECEIVE the loopback packets; while on Unix-like systems, the
176 // applications with loopback off will not SEND the loopback packets to
177 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
178 int SetMulticastLoopbackMode(bool loopback);
179
hidehiko17fac552014-12-08 06:02:17180 // Sets the differentiated services flags on outgoing packets. May not
[email protected]a8a442b32013-10-22 00:34:41181 // do anything on some platforms.
hidehiko17fac552014-12-08 06:02:17182 // Returns a net error code.
[email protected]a8a442b32013-10-22 00:34:41183 int SetDiffServCodePoint(DiffServCodePoint dscp);
184
[email protected]1901dbcb2014-03-10 07:18:37185 // Resets the thread to be used for thread-safety checks.
186 void DetachFromThread();
187
[email protected]a2798d92011-03-02 22:56:18188 private:
[email protected]a1781d7b2012-07-16 11:52:34189 enum SocketOptions {
hidehiko17fac552014-12-08 06:02:17190 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
[email protected]a1781d7b2012-07-16 11:52:34191 };
192
[email protected]2da659e2013-05-23 20:51:34193 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18194 public:
tfarina4eb7aad82015-09-14 17:10:34195 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18196
197 // MessageLoopForIO::Watcher methods
198
dchengb03027d2014-10-21 12:00:20199 void OnFileCanReadWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18200
dchengb03027d2014-10-21 12:00:20201 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18202
203 private:
tfarina4eb7aad82015-09-14 17:10:34204 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18205
206 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
207 };
208
[email protected]2da659e2013-05-23 20:51:34209 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18210 public:
tfarina4eb7aad82015-09-14 17:10:34211 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18212
213 // MessageLoopForIO::Watcher methods
214
dchengb03027d2014-10-21 12:00:20215 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18216
dchengb03027d2014-10-21 12:00:20217 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18218
219 private:
tfarina4eb7aad82015-09-14 17:10:34220 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18221
222 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
223 };
224
225 void DoReadCallback(int rv);
226 void DoWriteCallback(int rv);
227 void DidCompleteRead();
228 void DidCompleteWrite();
229
[email protected]8866f622011-10-18 20:08:10230 // Handles stats and logging. |result| is the number of bytes transferred, on
231 // success, or the net error code on failure. On success, LogRead takes in a
232 // sockaddr and its length, which are mandatory, while LogWrite takes in an
233 // optional IPEndPoint.
234 void LogRead(int result, const char* bytes, socklen_t addr_len,
235 const sockaddr* addr) const;
236 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
237
[email protected]06043ae2011-03-16 00:00:21238 // Same as SendTo(), except that address is passed by pointer
239 // instead of by reference. It is called from Write() with |address|
240 // set to NULL.
241 int SendToOrWrite(IOBuffer* buf,
242 int buf_len,
243 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55244 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21245
[email protected]8866f622011-10-18 20:08:10246 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21247 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
248 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18249
[email protected]a1781d7b2012-07-16 11:52:34250 // Applies |socket_options_| to |socket_|. Should be called before
251 // Bind().
hidehiko17fac552014-12-08 06:02:17252 int SetMulticastOptions();
[email protected]5370c012011-06-29 03:47:04253 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56254 // Binds to a random port on |address|.
255 int RandomBind(const IPAddressNumber& address);
[email protected]5370c012011-06-29 03:47:04256
[email protected]a2798d92011-03-02 22:56:18257 int socket_;
sergeyu30ad5b32015-03-27 20:31:02258
[email protected]5f01ce22013-04-30 00:53:18259 int addr_family_;
hidehiko17fac552014-12-08 06:02:17260 bool is_connected_;
[email protected]a2798d92011-03-02 22:56:18261
[email protected]7be809df2012-08-07 17:00:48262 // Bitwise-or'd combination of SocketOptions. Specifies the set of
263 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34264 int socket_options_;
265
[email protected]7b29dac2013-12-05 11:19:42266 // Multicast interface.
267 uint32 multicast_interface_;
268
hidehiko17fac552014-12-08 06:02:17269 // Multicast socket options cached for SetMulticastOption.
[email protected]5f01ce22013-04-30 00:53:18270 // Cannot be used after Bind().
271 int multicast_time_to_live_;
272
[email protected]5370c012011-06-29 03:47:04273 // How to do source port binding, used only when UDPSocket is part of
274 // UDPClientSocket, since UDPServerSocket provides Bind.
275 DatagramSocket::BindType bind_type_;
276
277 // PRNG function for generating port numbers.
278 RandIntCallback rand_int_cb_;
279
[email protected]a2798d92011-03-02 22:56:18280 // These are mutable since they're just cached copies to make
281 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04282 mutable scoped_ptr<IPEndPoint> local_address_;
283 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18284
tfarina4eb7aad82015-09-14 17:10:34285 // The socket's posix wrappers
[email protected]2da659e2013-05-23 20:51:34286 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
287 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18288
289 // The corresponding watchers for reads and writes.
290 ReadWatcher read_watcher_;
291 WriteWatcher write_watcher_;
292
[email protected]43d4a0262011-03-09 19:26:04293 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18294 scoped_refptr<IOBuffer> read_buf_;
295 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04296 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18297
[email protected]43d4a0262011-03-09 19:26:04298 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18299 scoped_refptr<IOBuffer> write_buf_;
300 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04301 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18302
303 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33304 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18305
306 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55307 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18308
309 BoundNetLog net_log_;
310
tfarina4eb7aad82015-09-14 17:10:34311 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix);
[email protected]a2798d92011-03-02 22:56:18312};
313
314} // namespace net
315
tfarina4eb7aad82015-09-14 17:10:34316#endif // NET_UDP_UDP_SOCKET_POSIX_H_