[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame^] | 5 | #ifndef NET_SOCKET_UDP_SOCKET_POSIX_H_ |
| 6 | #define NET_SOCKET_UDP_SOCKET_POSIX_H_ |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 7 | |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 15 | #include "base/threading/non_thread_safe.h" |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 16 | #include "net/base/address_family.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 17 | #include "net/base/completion_callback.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 19 | #include "net/base/ip_endpoint.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 20 | #include "net/base/net_export.h" |
tfarina | 42f7ce83 | 2015-10-08 20:45:53 | [diff] [blame] | 21 | #include "net/base/network_change_notifier.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 22 | #include "net/base/rand_callback.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 23 | #include "net/log/net_log_with_source.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame^] | 24 | #include "net/socket/datagram_socket.h" |
| 25 | #include "net/socket/diff_serv_code_point.h" |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 26 | #include "net/socket/socket_descriptor.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 27 | |
| 28 | namespace net { |
| 29 | |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 30 | class IPAddress; |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 31 | class NetLog; |
| 32 | struct NetLogSource; |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 33 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 34 | class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 35 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 36 | UDPSocketPosix(DatagramSocket::BindType bind_type, |
| 37 | const RandIntCallback& rand_int_cb, |
| 38 | net::NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 39 | const net::NetLogSource& source); |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 40 | virtual ~UDPSocketPosix(); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 41 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 42 | // Opens the socket. |
| 43 | // Returns a net error code. |
| 44 | int Open(AddressFamily address_family); |
| 45 | |
pauljensen | be5bc323 | 2015-10-05 20:39:27 | [diff] [blame] | 46 | // 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 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 53 | // Connects the socket to connect with a certain |address|. |
| 54 | // Should be called after Open(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 55 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 56 | int Connect(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 57 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 58 | // 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 60 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 61 | int Bind(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 62 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 63 | // Closes the socket. |
| 64 | // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 65 | void Close(); |
| 66 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 67 | // Copies the remote udp address into |address| and returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 68 | int GetPeerAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 69 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 70 | // Copies the local udp address into |address| and returns a net error code. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 71 | // (similar to getsockname) |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 72 | int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 73 | |
| 74 | // IO: |
| 75 | // Multiple outstanding read requests are not supported. |
| 76 | // Full duplex mode (reading and writing at the same time) is supported |
| 77 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 78 | // Reads from the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 79 | // Only usable from the client-side of a UDP socket, after the socket |
| 80 | // has been connected. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 81 | int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 82 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 83 | // Writes to the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 84 | // Only usable from the client-side of a UDP socket, after the socket |
| 85 | // has been connected. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 86 | int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 87 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 88 | // Reads from a socket and receive sender address information. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 89 | // |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] | 73c5b69 | 2014-07-01 12:43:41 | [diff] [blame] | 94 | // |callback| is the callback on completion of the RecvFrom. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 95 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
tfarina | 39524dd | 2016-02-19 10:52:10 | [diff] [blame] | 96 | // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 97 | // alive until the callback is called. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 98 | int RecvFrom(IOBuffer* buf, |
| 99 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 100 | IPEndPoint* address, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 101 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 102 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 103 | // Sends to a socket with a particular destination. |
tfarina | 39524dd | 2016-02-19 10:52:10 | [diff] [blame] | 104 | // |buf| is the buffer to send. |
| 105 | // |buf_len| is the number of bytes to send. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 106 | // |address| is the recipient address. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 107 | // |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] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 113 | const IPEndPoint& address, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 114 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 115 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 116 | // Sets the receive buffer size (in bytes) for the socket. |
| 117 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 118 | int SetReceiveBufferSize(int32_t size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 119 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 120 | // Sets the send buffer size (in bytes) for the socket. |
| 121 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 122 | int SetSendBufferSize(int32_t size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 123 | |
rch | ff006a1 | 2016-08-24 23:56:31 | [diff] [blame] | 124 | // 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 131 | // Returns true if the socket is already connected or bound. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 132 | bool is_connected() const { return is_connected_; } |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 133 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 134 | const NetLogWithSource& NetLog() const { return net_log_; } |
[email protected] | eaf10dc | 2011-07-18 21:47:35 | [diff] [blame] | 135 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 136 | // Sets corresponding flags in |socket_options_| to allow the socket |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 137 | // to share the local address to which the socket will be bound with |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 138 | // other processes. Should be called between Open() and Bind(). |
| 139 | // Returns a net error code. |
| 140 | int AllowAddressReuse(); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 141 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 142 | // 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] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 146 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 147 | // Joins the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 148 | // |group_address| is the group address to join, could be either |
| 149 | // an IPv4 or IPv6 address. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 150 | // Returns a net error code. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 151 | int JoinGroup(const IPAddress& group_address) const; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 152 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 153 | // Leaves the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 154 | // |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. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 159 | // Returns a net error code. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 160 | int LeaveGroup(const IPAddress& group_address) const; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 161 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 162 | // Sets interface to use for multicast. If |interface_index| set to 0, |
| 163 | // default interface is used. |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 164 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 165 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 166 | int SetMulticastInterface(uint32_t interface_index); |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 167 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 168 | // Sets the time-to-live option for UDP packets sent to the multicast |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 169 | // group address. The default value of this option is 1. |
| 170 | // Cannot be negative or more than 255. |
| 171 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 172 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 173 | int SetMulticastTimeToLive(int time_to_live); |
| 174 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 175 | // Sets the loopback flag for UDP socket. If this flag is true, the host |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 176 | // 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(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 179 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 180 | // |
| 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 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 191 | // Sets the differentiated services flags on outgoing packets. May not |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 192 | // do anything on some platforms. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 193 | // Returns a net error code. |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 194 | int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 195 | |
[email protected] | 1901dbcb | 2014-03-10 07:18:37 | [diff] [blame] | 196 | // Resets the thread to be used for thread-safety checks. |
| 197 | void DetachFromThread(); |
| 198 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 199 | private: |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 200 | enum SocketOptions { |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 201 | SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 202 | }; |
| 203 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 204 | class ReadWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 205 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 206 | explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 207 | |
| 208 | // MessageLoopForIO::Watcher methods |
| 209 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 210 | void OnFileCanReadWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 211 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 212 | void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 213 | |
| 214 | private: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 215 | UDPSocketPosix* const socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 216 | |
| 217 | DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
| 218 | }; |
| 219 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 220 | class WriteWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 221 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 222 | explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 223 | |
| 224 | // MessageLoopForIO::Watcher methods |
| 225 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 226 | void OnFileCanReadWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 227 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 228 | void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 229 | |
| 230 | private: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 231 | UDPSocketPosix* const socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 232 | |
| 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] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 241 | // 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] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 249 | // 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] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 255 | const CompletionCallback& callback); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 256 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 257 | int InternalConnect(const IPEndPoint& address); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 258 | int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 259 | int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 260 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 261 | // Applies |socket_options_| to |socket_|. Should be called before |
| 262 | // Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 263 | int SetMulticastOptions(); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 264 | int DoBind(const IPEndPoint& address); |
[email protected] | acfe8e8 | 2013-12-12 14:12:56 | [diff] [blame] | 265 | // Binds to a random port on |address|. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 266 | int RandomBind(const IPAddress& address); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 267 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 268 | int socket_; |
sergeyu | 30ad5b3 | 2015-03-27 20:31:02 | [diff] [blame] | 269 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 270 | int addr_family_; |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 271 | bool is_connected_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 272 | |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 273 | // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 274 | // options that should be applied to |socket_| before Bind(). |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 275 | int socket_options_; |
| 276 | |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 277 | // Multicast interface. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 278 | uint32_t multicast_interface_; |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 279 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 280 | // Multicast socket options cached for SetMulticastOption. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 281 | // Cannot be used after Bind(). |
| 282 | int multicast_time_to_live_; |
| 283 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 284 | // 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 291 | // These are mutable since they're just cached copies to make |
| 292 | // GetPeerAddress/GetLocalAddress smarter. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 293 | mutable std::unique_ptr<IPEndPoint> local_address_; |
| 294 | mutable std::unique_ptr<IPEndPoint> remote_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 295 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 296 | // The socket's posix wrappers |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 297 | base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 298 | base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 299 | |
| 300 | // The corresponding watchers for reads and writes. |
| 301 | ReadWatcher read_watcher_; |
| 302 | WriteWatcher write_watcher_; |
| 303 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 304 | // The buffer used by InternalRead() to retry Read requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 305 | scoped_refptr<IOBuffer> read_buf_; |
| 306 | int read_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 307 | IPEndPoint* recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 308 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 309 | // The buffer used by InternalWrite() to retry Write requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 310 | scoped_refptr<IOBuffer> write_buf_; |
| 311 | int write_buf_len_; |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 312 | std::unique_ptr<IPEndPoint> send_to_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 313 | |
| 314 | // External callback; called when read is complete. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 315 | CompletionCallback read_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 316 | |
| 317 | // External callback; called when write is complete. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 318 | CompletionCallback write_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 319 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 320 | NetLogWithSource net_log_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 321 | |
pauljensen | f0d9535a | 2016-06-10 19:51:19 | [diff] [blame] | 322 | // Network that this socket is bound to via BindToNetwork(). |
| 323 | NetworkChangeNotifier::NetworkHandle bound_network_; |
| 324 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 325 | DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | } // namespace net |
| 329 | |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame^] | 330 | #endif // NET_SOCKET_UDP_SOCKET_POSIX_H_ |