[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 | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 5 | #ifndef NET_UDP_UDP_SOCKET_POSIX_H_ |
| 6 | #define NET_UDP_UDP_SOCKET_POSIX_H_ |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 7 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/ref_counted.h" |
| 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 11 | #include "base/threading/non_thread_safe.h" |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 12 | #include "net/base/address_family.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 13 | #include "net/base/completion_callback.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 14 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 15 | #include "net/base/ip_endpoint.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 16 | #include "net/base/net_export.h" |
tfarina | 42f7ce83 | 2015-10-08 20:45:53 | [diff] [blame^] | 17 | #include "net/base/network_change_notifier.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 18 | #include "net/base/rand_callback.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 19 | #include "net/log/net_log.h" |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 20 | #include "net/socket/socket_descriptor.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 21 | #include "net/udp/datagram_socket.h" |
tfarina | 42f7ce83 | 2015-10-08 20:45:53 | [diff] [blame^] | 22 | #include "net/udp/diff_serv_code_point.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 26 | class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 27 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 28 | 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 33 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 34 | // Opens the socket. |
| 35 | // Returns a net error code. |
| 36 | int Open(AddressFamily address_family); |
| 37 | |
pauljensen | be5bc323 | 2015-10-05 20:39:27 | [diff] [blame] | 38 | // 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 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 45 | // Connects the socket to connect with a certain |address|. |
| 46 | // Should be called after Open(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 47 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 48 | int Connect(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 49 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 50 | // 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 52 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 53 | int Bind(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 54 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 55 | // Closes the socket. |
| 56 | // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 57 | void Close(); |
| 58 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 59 | // Copies the remote udp address into |address| and returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 60 | int GetPeerAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 61 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 62 | // Copies the local udp address into |address| and returns a net error code. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 63 | // (similar to getsockname) |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 64 | int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 65 | |
| 66 | // IO: |
| 67 | // Multiple outstanding read requests are not supported. |
| 68 | // Full duplex mode (reading and writing at the same time) is supported |
| 69 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 70 | // Reads from the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 71 | // Only usable from the client-side of a UDP socket, after the socket |
| 72 | // has been connected. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 73 | int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 74 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 75 | // Writes to the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 76 | // Only usable from the client-side of a UDP socket, after the socket |
| 77 | // has been connected. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 78 | int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 79 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 80 | // Reads from a socket and receive sender address information. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 81 | // |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] | 73c5b69 | 2014-07-01 12:43:41 | [diff] [blame] | 89 | // |callback| is the callback on completion of the RecvFrom. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 90 | // 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] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 95 | IPEndPoint* address, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 96 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 97 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 98 | // Sends to a socket with a particular destination. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 99 | // |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] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 109 | const IPEndPoint& address, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 110 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 111 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 112 | // Sets the receive buffer size (in bytes) for the socket. |
| 113 | // Returns a net error code. |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 114 | int SetReceiveBufferSize(int32 size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 115 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 116 | // Sets the send buffer size (in bytes) for the socket. |
| 117 | // Returns a net error code. |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 118 | int SetSendBufferSize(int32 size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 119 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 120 | // Returns true if the socket is already connected or bound. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 121 | bool is_connected() const { return is_connected_; } |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 122 | |
[email protected] | eaf10dc | 2011-07-18 21:47:35 | [diff] [blame] | 123 | const BoundNetLog& NetLog() const { return net_log_; } |
| 124 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 125 | // Sets corresponding flags in |socket_options_| to allow the socket |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 126 | // to share the local address to which the socket will be bound with |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 127 | // other processes. Should be called between Open() and Bind(). |
| 128 | // Returns a net error code. |
| 129 | int AllowAddressReuse(); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 130 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 131 | // 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] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 135 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 136 | // Joins the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 137 | // |group_address| is the group address to join, could be either |
| 138 | // an IPv4 or IPv6 address. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 139 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 140 | int JoinGroup(const IPAddressNumber& group_address) const; |
| 141 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 142 | // Leaves the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 143 | // |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. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 148 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 149 | int LeaveGroup(const IPAddressNumber& group_address) const; |
| 150 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 151 | // Sets interface to use for multicast. If |interface_index| set to 0, |
| 152 | // default interface is used. |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 153 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 154 | // Returns a net error code. |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 155 | int SetMulticastInterface(uint32 interface_index); |
| 156 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 157 | // Sets the time-to-live option for UDP packets sent to the multicast |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 158 | // group address. The default value of this option is 1. |
| 159 | // Cannot be negative or more than 255. |
| 160 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 161 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 162 | int SetMulticastTimeToLive(int time_to_live); |
| 163 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 164 | // 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] | 165 | // 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(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 168 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 169 | // |
| 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 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 180 | // Sets the differentiated services flags on outgoing packets. May not |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 181 | // do anything on some platforms. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 182 | // Returns a net error code. |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 183 | int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 184 | |
[email protected] | 1901dbcb | 2014-03-10 07:18:37 | [diff] [blame] | 185 | // Resets the thread to be used for thread-safety checks. |
| 186 | void DetachFromThread(); |
| 187 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 188 | private: |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 189 | enum SocketOptions { |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 190 | SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 191 | }; |
| 192 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 193 | class ReadWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 194 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 195 | explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 196 | |
| 197 | // MessageLoopForIO::Watcher methods |
| 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | void OnFileCanReadWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 200 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 201 | void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 202 | |
| 203 | private: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 204 | UDPSocketPosix* const socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 205 | |
| 206 | DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
| 207 | }; |
| 208 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 209 | class WriteWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 210 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 211 | explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 212 | |
| 213 | // MessageLoopForIO::Watcher methods |
| 214 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 215 | void OnFileCanReadWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 216 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 217 | void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 218 | |
| 219 | private: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 220 | UDPSocketPosix* const socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 221 | |
| 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] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 230 | // 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] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 238 | // 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] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 244 | const CompletionCallback& callback); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 245 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 246 | int InternalConnect(const IPEndPoint& address); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 247 | int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 248 | int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 249 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 250 | // Applies |socket_options_| to |socket_|. Should be called before |
| 251 | // Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 252 | int SetMulticastOptions(); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 253 | int DoBind(const IPEndPoint& address); |
[email protected] | acfe8e8 | 2013-12-12 14:12:56 | [diff] [blame] | 254 | // Binds to a random port on |address|. |
| 255 | int RandomBind(const IPAddressNumber& address); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 256 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 257 | int socket_; |
sergeyu | 30ad5b3 | 2015-03-27 20:31:02 | [diff] [blame] | 258 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 259 | int addr_family_; |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 260 | bool is_connected_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 261 | |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 262 | // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 263 | // options that should be applied to |socket_| before Bind(). |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 264 | int socket_options_; |
| 265 | |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 266 | // Multicast interface. |
| 267 | uint32 multicast_interface_; |
| 268 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 269 | // Multicast socket options cached for SetMulticastOption. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 270 | // Cannot be used after Bind(). |
| 271 | int multicast_time_to_live_; |
| 272 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 273 | // 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] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 280 | // These are mutable since they're just cached copies to make |
| 281 | // GetPeerAddress/GetLocalAddress smarter. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 282 | mutable scoped_ptr<IPEndPoint> local_address_; |
| 283 | mutable scoped_ptr<IPEndPoint> remote_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 284 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 285 | // The socket's posix wrappers |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 286 | base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 287 | base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 288 | |
| 289 | // The corresponding watchers for reads and writes. |
| 290 | ReadWatcher read_watcher_; |
| 291 | WriteWatcher write_watcher_; |
| 292 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 293 | // The buffer used by InternalRead() to retry Read requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 294 | scoped_refptr<IOBuffer> read_buf_; |
| 295 | int read_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 296 | IPEndPoint* recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 297 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 298 | // The buffer used by InternalWrite() to retry Write requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 299 | scoped_refptr<IOBuffer> write_buf_; |
| 300 | int write_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 301 | scoped_ptr<IPEndPoint> send_to_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 302 | |
| 303 | // External callback; called when read is complete. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 304 | CompletionCallback read_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 305 | |
| 306 | // External callback; called when write is complete. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 307 | CompletionCallback write_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 308 | |
| 309 | BoundNetLog net_log_; |
| 310 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 311 | DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | } // namespace net |
| 315 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 316 | #endif // NET_UDP_UDP_SOCKET_POSIX_H_ |