[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 | |
| 5 | #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 6 | #define NET_UDP_UDP_SOCKET_LIBEVENT_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" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 12 | #include "net/base/completion_callback.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 13 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 14 | #include "net/base/ip_endpoint.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 15 | #include "net/base/net_export.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 16 | #include "net/base/net_log.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 17 | #include "net/base/rand_callback.h" |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame^] | 18 | #include "net/socket/socket_descriptor.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 19 | #include "net/udp/datagram_socket.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 23 | class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 24 | public: |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 25 | UDPSocketLibevent(DatagramSocket::BindType bind_type, |
| 26 | const RandIntCallback& rand_int_cb, |
| 27 | net::NetLog* net_log, |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 28 | const net::NetLog::Source& source); |
| 29 | virtual ~UDPSocketLibevent(); |
| 30 | |
| 31 | // Connect the socket to connect with a certain |address|. |
| 32 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 33 | int Connect(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 34 | |
| 35 | // Bind the address/port for this socket to |address|. This is generally |
| 36 | // only used on a server. |
| 37 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 38 | int Bind(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 39 | |
| 40 | // Close the socket. |
| 41 | void Close(); |
| 42 | |
| 43 | // Copy the remote udp address into |address| and return a network error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 44 | int GetPeerAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 45 | |
| 46 | // Copy the local udp address into |address| and return a network error code. |
| 47 | // (similar to getsockname) |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 48 | int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 49 | |
| 50 | // IO: |
| 51 | // Multiple outstanding read requests are not supported. |
| 52 | // Full duplex mode (reading and writing at the same time) is supported |
| 53 | |
| 54 | // Read from the socket. |
| 55 | // Only usable from the client-side of a UDP socket, after the socket |
| 56 | // has been connected. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 57 | int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 58 | |
| 59 | // Write to the socket. |
| 60 | // Only usable from the client-side of a UDP socket, after the socket |
| 61 | // has been connected. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 62 | int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 63 | |
| 64 | // Read from a socket and receive sender address information. |
| 65 | // |buf| is the buffer to read data into. |
| 66 | // |buf_len| is the maximum amount of data to read. |
| 67 | // |address| is a buffer provided by the caller for receiving the sender |
| 68 | // address information about the received data. This buffer must be kept |
| 69 | // alive by the caller until the callback is placed. |
| 70 | // |address_length| is a ptr to the length of the |address| buffer. This |
| 71 | // is an input parameter containing the maximum size |address| can hold |
| 72 | // and also an output parameter for the size of |address| upon completion. |
| 73 | // |callback| the callback on completion of the Recv. |
| 74 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 75 | // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, |
| 76 | // and |address_length| alive until the callback is called. |
| 77 | int RecvFrom(IOBuffer* buf, |
| 78 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 79 | IPEndPoint* address, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 80 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 81 | |
| 82 | // Send to a socket with a particular destination. |
| 83 | // |buf| is the buffer to send |
| 84 | // |buf_len| is the number of bytes to send |
| 85 | // |address| is the recipient address. |
| 86 | // |address_length| is the size of the recipient address |
| 87 | // |callback| is the user callback function to call on complete. |
| 88 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 89 | // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 90 | // alive until the callback is called. |
| 91 | int SendTo(IOBuffer* buf, |
| 92 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 93 | const IPEndPoint& address, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 94 | const CompletionCallback& callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 95 | |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 96 | // Set the receive buffer size (in bytes) for the socket. |
| 97 | bool SetReceiveBufferSize(int32 size); |
| 98 | |
| 99 | // Set the send buffer size (in bytes) for the socket. |
| 100 | bool SetSendBufferSize(int32 size); |
| 101 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 102 | // Returns true if the socket is already connected or bound. |
| 103 | bool is_connected() const { return socket_ != kInvalidSocket; } |
| 104 | |
[email protected] | eaf10dc | 2011-07-18 21:47:35 | [diff] [blame] | 105 | const BoundNetLog& NetLog() const { return net_log_; } |
| 106 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 107 | // Sets corresponding flags in |socket_options_| to allow the socket |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 108 | // to share the local address to which the socket will be bound with |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 109 | // other processes. Should be called before Bind(). |
| 110 | void AllowAddressReuse(); |
| 111 | |
| 112 | // Sets corresponding flags in |socket_options_| to allow sending |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 113 | // and receiving packets to and from broadcast addresses. Should be |
| 114 | // called before Bind(). |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 115 | void AllowBroadcast(); |
| 116 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 117 | // Join the multicast group. |
| 118 | // |group_address| is the group address to join, could be either |
| 119 | // an IPv4 or IPv6 address. |
| 120 | // Return a network error code. |
| 121 | int JoinGroup(const IPAddressNumber& group_address) const; |
| 122 | |
| 123 | // Leave the multicast group. |
| 124 | // |group_address| is the group address to leave, could be either |
| 125 | // an IPv4 or IPv6 address. If the socket hasn't joined the group, |
| 126 | // it will be ignored. |
| 127 | // It's optional to leave the multicast group before destroying |
| 128 | // the socket. It will be done by the OS. |
| 129 | // Return a network error code. |
| 130 | int LeaveGroup(const IPAddressNumber& group_address) const; |
| 131 | |
| 132 | // Set the time-to-live option for UDP packets sent to the multicast |
| 133 | // group address. The default value of this option is 1. |
| 134 | // Cannot be negative or more than 255. |
| 135 | // Should be called before Bind(). |
| 136 | // Return a network error code. |
| 137 | int SetMulticastTimeToLive(int time_to_live); |
| 138 | |
| 139 | // Set the loopback flag for UDP socket. If this flag is true, the host |
| 140 | // will receive packets sent to the joined group from itself. |
| 141 | // The default value of this option is true. |
| 142 | // Should be called before Bind(). |
| 143 | // Return a network error code. |
| 144 | // |
| 145 | // Note: the behavior of |SetMulticastLoopbackMode| is slightly |
| 146 | // different between Windows and Unix-like systems. The inconsistency only |
| 147 | // happens when there are more than one applications on the same host |
| 148 | // joined to the same multicast group while having different settings on |
| 149 | // multicast loopback mode. On Windows, the applications with loopback off |
| 150 | // will not RECEIVE the loopback packets; while on Unix-like systems, the |
| 151 | // applications with loopback off will not SEND the loopback packets to |
| 152 | // other applications on the same host. See MSDN: http://goo.gl/6vqbj |
| 153 | int SetMulticastLoopbackMode(bool loopback); |
| 154 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 155 | private: |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 156 | enum SocketOptions { |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 157 | SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, |
| 158 | SOCKET_OPTION_BROADCAST = 1 << 1, |
| 159 | SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 160 | }; |
| 161 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 162 | class ReadWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 163 | public: |
| 164 | explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 165 | |
| 166 | // MessageLoopForIO::Watcher methods |
| 167 | |
[email protected] | 2a848e0e | 2012-08-09 22:27:31 | [diff] [blame] | 168 | virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 169 | |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 170 | virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 171 | |
| 172 | private: |
| 173 | UDPSocketLibevent* const socket_; |
| 174 | |
| 175 | DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
| 176 | }; |
| 177 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 178 | class WriteWatcher : public base::MessageLoopForIO::Watcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 179 | public: |
| 180 | explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 181 | |
| 182 | // MessageLoopForIO::Watcher methods |
| 183 | |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 184 | virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 185 | |
[email protected] | 2a848e0e | 2012-08-09 22:27:31 | [diff] [blame] | 186 | virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 187 | |
| 188 | private: |
| 189 | UDPSocketLibevent* const socket_; |
| 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
| 192 | }; |
| 193 | |
| 194 | void DoReadCallback(int rv); |
| 195 | void DoWriteCallback(int rv); |
| 196 | void DidCompleteRead(); |
| 197 | void DidCompleteWrite(); |
| 198 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 199 | // Handles stats and logging. |result| is the number of bytes transferred, on |
| 200 | // success, or the net error code on failure. On success, LogRead takes in a |
| 201 | // sockaddr and its length, which are mandatory, while LogWrite takes in an |
| 202 | // optional IPEndPoint. |
| 203 | void LogRead(int result, const char* bytes, socklen_t addr_len, |
| 204 | const sockaddr* addr) const; |
| 205 | void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; |
| 206 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 207 | // Returns the OS error code (or 0 on success). |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 208 | int CreateSocket(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 209 | |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 210 | // Same as SendTo(), except that address is passed by pointer |
| 211 | // instead of by reference. It is called from Write() with |address| |
| 212 | // set to NULL. |
| 213 | int SendToOrWrite(IOBuffer* buf, |
| 214 | int buf_len, |
| 215 | const IPEndPoint* address, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 216 | const CompletionCallback& callback); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 217 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 218 | int InternalConnect(const IPEndPoint& address); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 219 | int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 220 | int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 221 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 222 | // Applies |socket_options_| to |socket_|. Should be called before |
| 223 | // Bind(). |
| 224 | int SetSocketOptions(); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 225 | int DoBind(const IPEndPoint& address); |
| 226 | int RandomBind(const IPEndPoint& address); |
| 227 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 228 | int socket_; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 229 | int addr_family_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 230 | |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 231 | // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 232 | // options that should be applied to |socket_| before Bind(). |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 233 | int socket_options_; |
| 234 | |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 235 | // Multicast socket options cached for SetSocketOption. |
| 236 | // Cannot be used after Bind(). |
| 237 | int multicast_time_to_live_; |
| 238 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 239 | // How to do source port binding, used only when UDPSocket is part of |
| 240 | // UDPClientSocket, since UDPServerSocket provides Bind. |
| 241 | DatagramSocket::BindType bind_type_; |
| 242 | |
| 243 | // PRNG function for generating port numbers. |
| 244 | RandIntCallback rand_int_cb_; |
| 245 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 246 | // These are mutable since they're just cached copies to make |
| 247 | // GetPeerAddress/GetLocalAddress smarter. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 248 | mutable scoped_ptr<IPEndPoint> local_address_; |
| 249 | mutable scoped_ptr<IPEndPoint> remote_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 250 | |
| 251 | // The socket's libevent wrappers |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 252 | base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 253 | base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 254 | |
| 255 | // The corresponding watchers for reads and writes. |
| 256 | ReadWatcher read_watcher_; |
| 257 | WriteWatcher write_watcher_; |
| 258 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 259 | // The buffer used by InternalRead() to retry Read requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 260 | scoped_refptr<IOBuffer> read_buf_; |
| 261 | int read_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 262 | IPEndPoint* recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 263 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 264 | // The buffer used by InternalWrite() to retry Write requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 265 | scoped_refptr<IOBuffer> write_buf_; |
| 266 | int write_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 267 | scoped_ptr<IPEndPoint> send_to_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 268 | |
| 269 | // External callback; called when read is complete. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 270 | CompletionCallback read_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 271 | |
| 272 | // External callback; called when write is complete. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 273 | CompletionCallback write_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 274 | |
| 275 | BoundNetLog net_log_; |
| 276 | |
| 277 | DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
| 278 | }; |
| 279 | |
| 280 | } // namespace net |
| 281 | |
| 282 | #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |