blob: eb18daa879fe9b51c18de5f885dc9946744b9844 [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"
eromanc9a6b722015-06-03 22:19:0017#include "net/base/net_util.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"
[email protected]a2798d92011-03-02 22:56:1822
23namespace net {
24
tfarina4eb7aad82015-09-14 17:10:3425class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe {
[email protected]a2798d92011-03-02 22:56:1826 public:
tfarina4eb7aad82015-09-14 17:10:3427 UDPSocketPosix(DatagramSocket::BindType bind_type,
28 const RandIntCallback& rand_int_cb,
29 net::NetLog* net_log,
30 const net::NetLog::Source& source);
31 virtual ~UDPSocketPosix();
[email protected]a2798d92011-03-02 22:56:1832
hidehiko17fac552014-12-08 06:02:1733 // Opens the socket.
34 // Returns a net error code.
35 int Open(AddressFamily address_family);
36
pauljensenbe5bc3232015-10-05 20:39:2737 // Binds this socket to |network|. All data traffic on the socket will be sent
38 // and received via |network|. Must be called before Connect(). This call will
39 // fail if |network| has disconnected. Communication using this socket will
40 // fail if |network| disconnects.
41 // Returns a net error code.
42 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network);
43
hidehiko17fac552014-12-08 06:02:1744 // Connects the socket to connect with a certain |address|.
45 // Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1846 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0447 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1848
hidehiko17fac552014-12-08 06:02:1749 // Binds the address/port for this socket to |address|. This is generally
50 // only used on a server. Should be called after Open().
[email protected]a2798d92011-03-02 22:56:1851 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0452 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1853
hidehiko17fac552014-12-08 06:02:1754 // Closes the socket.
55 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
[email protected]a2798d92011-03-02 22:56:1856 void Close();
57
hidehiko17fac552014-12-08 06:02:1758 // Copies the remote udp address into |address| and returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0459 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1860
hidehiko17fac552014-12-08 06:02:1761 // Copies the local udp address into |address| and returns a net error code.
[email protected]a2798d92011-03-02 22:56:1862 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0463 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1864
65 // IO:
66 // Multiple outstanding read requests are not supported.
67 // Full duplex mode (reading and writing at the same time) is supported
68
hidehiko17fac552014-12-08 06:02:1769 // Reads from the socket.
[email protected]a2798d92011-03-02 22:56:1870 // Only usable from the client-side of a UDP socket, after the socket
71 // has been connected.
[email protected]3f55aa12011-12-07 02:03:3372 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1873
hidehiko17fac552014-12-08 06:02:1774 // Writes to the socket.
[email protected]a2798d92011-03-02 22:56:1875 // Only usable from the client-side of a UDP socket, after the socket
76 // has been connected.
[email protected]83039bb2011-12-09 18:43:5577 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1878
hidehiko17fac552014-12-08 06:02:1779 // Reads from a socket and receive sender address information.
[email protected]a2798d92011-03-02 22:56:1880 // |buf| is the buffer to read data into.
81 // |buf_len| is the maximum amount of data to read.
82 // |address| is a buffer provided by the caller for receiving the sender
83 // address information about the received data. This buffer must be kept
84 // alive by the caller until the callback is placed.
85 // |address_length| is a ptr to the length of the |address| buffer. This
86 // is an input parameter containing the maximum size |address| can hold
87 // and also an output parameter for the size of |address| upon completion.
[email protected]73c5b692014-07-01 12:43:4188 // |callback| is the callback on completion of the RecvFrom.
[email protected]a2798d92011-03-02 22:56:1889 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
90 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
91 // and |address_length| alive until the callback is called.
92 int RecvFrom(IOBuffer* buf,
93 int buf_len,
[email protected]43d4a0262011-03-09 19:26:0494 IPEndPoint* address,
[email protected]3f55aa12011-12-07 02:03:3395 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1896
hidehiko17fac552014-12-08 06:02:1797 // Sends to a socket with a particular destination.
[email protected]a2798d92011-03-02 22:56:1898 // |buf| is the buffer to send
99 // |buf_len| is the number of bytes to send
100 // |address| is the recipient address.
101 // |address_length| is the size of the recipient address
102 // |callback| is the user callback function to call on complete.
103 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
104 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
105 // alive until the callback is called.
106 int SendTo(IOBuffer* buf,
107 int buf_len,
[email protected]43d4a0262011-03-09 19:26:04108 const IPEndPoint& address,
[email protected]83039bb2011-12-09 18:43:55109 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:18110
hidehiko17fac552014-12-08 06:02:17111 // Sets the receive buffer size (in bytes) for the socket.
112 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15113 int SetReceiveBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40114
hidehiko17fac552014-12-08 06:02:17115 // Sets the send buffer size (in bytes) for the socket.
116 // Returns a net error code.
[email protected]28b96d1c2014-04-09 12:21:15117 int SetSendBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40118
[email protected]a2798d92011-03-02 22:56:18119 // Returns true if the socket is already connected or bound.
hidehiko17fac552014-12-08 06:02:17120 bool is_connected() const { return is_connected_; }
[email protected]a2798d92011-03-02 22:56:18121
[email protected]eaf10dc2011-07-18 21:47:35122 const BoundNetLog& NetLog() const { return net_log_; }
123
[email protected]a1781d7b2012-07-16 11:52:34124 // Sets corresponding flags in |socket_options_| to allow the socket
[email protected]7be809df2012-08-07 17:00:48125 // to share the local address to which the socket will be bound with
hidehiko17fac552014-12-08 06:02:17126 // other processes. Should be called between Open() and Bind().
127 // Returns a net error code.
128 int AllowAddressReuse();
[email protected]a1781d7b2012-07-16 11:52:34129
hidehiko17fac552014-12-08 06:02:17130 // Sets corresponding flags in |socket_options_| to allow or disallow sending
131 // and receiving packets to and from broadcast addresses.
132 // Returns a net error code.
133 int SetBroadcast(bool broadcast);
[email protected]a1781d7b2012-07-16 11:52:34134
hidehiko17fac552014-12-08 06:02:17135 // Joins the multicast group.
[email protected]5f01ce22013-04-30 00:53:18136 // |group_address| is the group address to join, could be either
137 // an IPv4 or IPv6 address.
hidehiko17fac552014-12-08 06:02:17138 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18139 int JoinGroup(const IPAddressNumber& group_address) const;
140
hidehiko17fac552014-12-08 06:02:17141 // Leaves the multicast group.
[email protected]5f01ce22013-04-30 00:53:18142 // |group_address| is the group address to leave, could be either
143 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
144 // it will be ignored.
145 // It's optional to leave the multicast group before destroying
146 // the socket. It will be done by the OS.
hidehiko17fac552014-12-08 06:02:17147 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18148 int LeaveGroup(const IPAddressNumber& group_address) const;
149
hidehiko17fac552014-12-08 06:02:17150 // Sets interface to use for multicast. If |interface_index| set to 0,
151 // default interface is used.
[email protected]7b29dac2013-12-05 11:19:42152 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17153 // Returns a net error code.
[email protected]7b29dac2013-12-05 11:19:42154 int SetMulticastInterface(uint32 interface_index);
155
hidehiko17fac552014-12-08 06:02:17156 // Sets the time-to-live option for UDP packets sent to the multicast
[email protected]5f01ce22013-04-30 00:53:18157 // group address. The default value of this option is 1.
158 // Cannot be negative or more than 255.
159 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17160 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18161 int SetMulticastTimeToLive(int time_to_live);
162
hidehiko17fac552014-12-08 06:02:17163 // Sets the loopback flag for UDP socket. If this flag is true, the host
[email protected]5f01ce22013-04-30 00:53:18164 // will receive packets sent to the joined group from itself.
165 // The default value of this option is true.
166 // Should be called before Bind().
hidehiko17fac552014-12-08 06:02:17167 // Returns a net error code.
[email protected]5f01ce22013-04-30 00:53:18168 //
169 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
170 // different between Windows and Unix-like systems. The inconsistency only
171 // happens when there are more than one applications on the same host
172 // joined to the same multicast group while having different settings on
173 // multicast loopback mode. On Windows, the applications with loopback off
174 // will not RECEIVE the loopback packets; while on Unix-like systems, the
175 // applications with loopback off will not SEND the loopback packets to
176 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
177 int SetMulticastLoopbackMode(bool loopback);
178
hidehiko17fac552014-12-08 06:02:17179 // Sets the differentiated services flags on outgoing packets. May not
[email protected]a8a442b32013-10-22 00:34:41180 // do anything on some platforms.
hidehiko17fac552014-12-08 06:02:17181 // Returns a net error code.
[email protected]a8a442b32013-10-22 00:34:41182 int SetDiffServCodePoint(DiffServCodePoint dscp);
183
[email protected]1901dbcb2014-03-10 07:18:37184 // Resets the thread to be used for thread-safety checks.
185 void DetachFromThread();
186
[email protected]a2798d92011-03-02 22:56:18187 private:
[email protected]a1781d7b2012-07-16 11:52:34188 enum SocketOptions {
hidehiko17fac552014-12-08 06:02:17189 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
[email protected]a1781d7b2012-07-16 11:52:34190 };
191
[email protected]2da659e2013-05-23 20:51:34192 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18193 public:
tfarina4eb7aad82015-09-14 17:10:34194 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18195
196 // MessageLoopForIO::Watcher methods
197
dchengb03027d2014-10-21 12:00:20198 void OnFileCanReadWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18199
dchengb03027d2014-10-21 12:00:20200 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18201
202 private:
tfarina4eb7aad82015-09-14 17:10:34203 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18204
205 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
206 };
207
[email protected]2da659e2013-05-23 20:51:34208 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18209 public:
tfarina4eb7aad82015-09-14 17:10:34210 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
[email protected]a2798d92011-03-02 22:56:18211
212 // MessageLoopForIO::Watcher methods
213
dchengb03027d2014-10-21 12:00:20214 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
[email protected]a2798d92011-03-02 22:56:18215
dchengb03027d2014-10-21 12:00:20216 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
[email protected]a2798d92011-03-02 22:56:18217
218 private:
tfarina4eb7aad82015-09-14 17:10:34219 UDPSocketPosix* const socket_;
[email protected]a2798d92011-03-02 22:56:18220
221 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
222 };
223
224 void DoReadCallback(int rv);
225 void DoWriteCallback(int rv);
226 void DidCompleteRead();
227 void DidCompleteWrite();
228
[email protected]8866f622011-10-18 20:08:10229 // Handles stats and logging. |result| is the number of bytes transferred, on
230 // success, or the net error code on failure. On success, LogRead takes in a
231 // sockaddr and its length, which are mandatory, while LogWrite takes in an
232 // optional IPEndPoint.
233 void LogRead(int result, const char* bytes, socklen_t addr_len,
234 const sockaddr* addr) const;
235 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
236
[email protected]06043ae2011-03-16 00:00:21237 // Same as SendTo(), except that address is passed by pointer
238 // instead of by reference. It is called from Write() with |address|
239 // set to NULL.
240 int SendToOrWrite(IOBuffer* buf,
241 int buf_len,
242 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55243 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21244
[email protected]8866f622011-10-18 20:08:10245 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21246 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
247 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18248
[email protected]a1781d7b2012-07-16 11:52:34249 // Applies |socket_options_| to |socket_|. Should be called before
250 // Bind().
hidehiko17fac552014-12-08 06:02:17251 int SetMulticastOptions();
[email protected]5370c012011-06-29 03:47:04252 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56253 // Binds to a random port on |address|.
254 int RandomBind(const IPAddressNumber& address);
[email protected]5370c012011-06-29 03:47:04255
[email protected]a2798d92011-03-02 22:56:18256 int socket_;
sergeyu30ad5b32015-03-27 20:31:02257
[email protected]5f01ce22013-04-30 00:53:18258 int addr_family_;
hidehiko17fac552014-12-08 06:02:17259 bool is_connected_;
[email protected]a2798d92011-03-02 22:56:18260
[email protected]7be809df2012-08-07 17:00:48261 // Bitwise-or'd combination of SocketOptions. Specifies the set of
262 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34263 int socket_options_;
264
[email protected]7b29dac2013-12-05 11:19:42265 // Multicast interface.
266 uint32 multicast_interface_;
267
hidehiko17fac552014-12-08 06:02:17268 // Multicast socket options cached for SetMulticastOption.
[email protected]5f01ce22013-04-30 00:53:18269 // Cannot be used after Bind().
270 int multicast_time_to_live_;
271
[email protected]5370c012011-06-29 03:47:04272 // How to do source port binding, used only when UDPSocket is part of
273 // UDPClientSocket, since UDPServerSocket provides Bind.
274 DatagramSocket::BindType bind_type_;
275
276 // PRNG function for generating port numbers.
277 RandIntCallback rand_int_cb_;
278
[email protected]a2798d92011-03-02 22:56:18279 // These are mutable since they're just cached copies to make
280 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04281 mutable scoped_ptr<IPEndPoint> local_address_;
282 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18283
tfarina4eb7aad82015-09-14 17:10:34284 // The socket's posix wrappers
[email protected]2da659e2013-05-23 20:51:34285 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
286 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18287
288 // The corresponding watchers for reads and writes.
289 ReadWatcher read_watcher_;
290 WriteWatcher write_watcher_;
291
[email protected]43d4a0262011-03-09 19:26:04292 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18293 scoped_refptr<IOBuffer> read_buf_;
294 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04295 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18296
[email protected]43d4a0262011-03-09 19:26:04297 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18298 scoped_refptr<IOBuffer> write_buf_;
299 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04300 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18301
302 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33303 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18304
305 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55306 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18307
308 BoundNetLog net_log_;
309
tfarina4eb7aad82015-09-14 17:10:34310 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix);
[email protected]a2798d92011-03-02 22:56:18311};
312
313} // namespace net
314
tfarina4eb7aad82015-09-14 17:10:34315#endif // NET_UDP_UDP_SOCKET_POSIX_H_