blob: 69e8ba3e6ab8ff3d69c0caefc39de2021f9901f3 [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
5#ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_
6#define NET_UDP_UDP_SOCKET_LIBEVENT_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"
[email protected]a2798d92011-03-02 22:56:1812#include "net/base/completion_callback.h"
[email protected]5370c012011-06-29 03:47:0413#include "net/base/io_buffer.h"
[email protected]43d4a0262011-03-09 19:26:0414#include "net/base/ip_endpoint.h"
[email protected]7f86564d2013-07-18 00:41:2215#include "net/base/net_export.h"
[email protected]a2798d92011-03-02 22:56:1816#include "net/base/net_log.h"
[email protected]7f86564d2013-07-18 00:41:2217#include "net/base/rand_callback.h"
[email protected]bbfef1f2013-08-28 03:00:5118#include "net/socket/socket_descriptor.h"
[email protected]5370c012011-06-29 03:47:0419#include "net/udp/datagram_socket.h"
[email protected]a2798d92011-03-02 22:56:1820
21namespace net {
22
[email protected]398dca82012-04-25 17:54:5323class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe {
[email protected]a2798d92011-03-02 22:56:1824 public:
[email protected]5370c012011-06-29 03:47:0425 UDPSocketLibevent(DatagramSocket::BindType bind_type,
26 const RandIntCallback& rand_int_cb,
27 net::NetLog* net_log,
[email protected]a2798d92011-03-02 22:56:1828 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]43d4a0262011-03-09 19:26:0433 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1834
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]43d4a0262011-03-09 19:26:0438 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1839
40 // Close the socket.
41 void Close();
42
43 // Copy the remote udp address into |address| and return a network error code.
[email protected]43d4a0262011-03-09 19:26:0444 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1845
46 // Copy the local udp address into |address| and return a network error code.
47 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0448 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1849
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]3f55aa12011-12-07 02:03:3357 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1858
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]83039bb2011-12-09 18:43:5562 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1863
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.
[email protected]73c5b692014-07-01 12:43:4173 // |callback| is the callback on completion of the RecvFrom.
[email protected]a2798d92011-03-02 22:56:1874 // 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]43d4a0262011-03-09 19:26:0479 IPEndPoint* address,
[email protected]3f55aa12011-12-07 02:03:3380 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1881
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]43d4a0262011-03-09 19:26:0493 const IPEndPoint& address,
[email protected]83039bb2011-12-09 18:43:5594 const CompletionCallback& callback);
[email protected]a2798d92011-03-02 22:56:1895
[email protected]df31da42011-10-18 01:44:4096 // Set the receive buffer size (in bytes) for the socket.
[email protected]28b96d1c2014-04-09 12:21:1597 int SetReceiveBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:4098
99 // Set the send buffer size (in bytes) for the socket.
[email protected]28b96d1c2014-04-09 12:21:15100 int SetSendBufferSize(int32 size);
[email protected]df31da42011-10-18 01:44:40101
[email protected]a2798d92011-03-02 22:56:18102 // Returns true if the socket is already connected or bound.
103 bool is_connected() const { return socket_ != kInvalidSocket; }
104
[email protected]eaf10dc2011-07-18 21:47:35105 const BoundNetLog& NetLog() const { return net_log_; }
106
[email protected]a1781d7b2012-07-16 11:52:34107 // Sets corresponding flags in |socket_options_| to allow the socket
[email protected]7be809df2012-08-07 17:00:48108 // to share the local address to which the socket will be bound with
[email protected]a1781d7b2012-07-16 11:52:34109 // other processes. Should be called before Bind().
110 void AllowAddressReuse();
111
112 // Sets corresponding flags in |socket_options_| to allow sending
[email protected]7be809df2012-08-07 17:00:48113 // and receiving packets to and from broadcast addresses. Should be
114 // called before Bind().
[email protected]a1781d7b2012-07-16 11:52:34115 void AllowBroadcast();
116
[email protected]5f01ce22013-04-30 00:53:18117 // 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
[email protected]7b29dac2013-12-05 11:19:42132 // Set interface to use for multicast. If |interface_index| set to 0, default
133 // interface is used.
134 // Should be called before Bind().
135 // Returns a network error code.
136 int SetMulticastInterface(uint32 interface_index);
137
[email protected]5f01ce22013-04-30 00:53:18138 // Set the time-to-live option for UDP packets sent to the multicast
139 // group address. The default value of this option is 1.
140 // Cannot be negative or more than 255.
141 // Should be called before Bind().
142 // Return a network error code.
143 int SetMulticastTimeToLive(int time_to_live);
144
145 // Set the loopback flag for UDP socket. If this flag is true, the host
146 // will receive packets sent to the joined group from itself.
147 // The default value of this option is true.
148 // Should be called before Bind().
149 // Return a network error code.
150 //
151 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
152 // different between Windows and Unix-like systems. The inconsistency only
153 // happens when there are more than one applications on the same host
154 // joined to the same multicast group while having different settings on
155 // multicast loopback mode. On Windows, the applications with loopback off
156 // will not RECEIVE the loopback packets; while on Unix-like systems, the
157 // applications with loopback off will not SEND the loopback packets to
158 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
159 int SetMulticastLoopbackMode(bool loopback);
160
[email protected]a8a442b32013-10-22 00:34:41161 // Set the differentiated services flags on outgoing packets. May not
162 // do anything on some platforms.
163 // Return a network error code.
164 int SetDiffServCodePoint(DiffServCodePoint dscp);
165
[email protected]1901dbcb2014-03-10 07:18:37166 // Resets the thread to be used for thread-safety checks.
167 void DetachFromThread();
168
[email protected]a2798d92011-03-02 22:56:18169 private:
[email protected]a1781d7b2012-07-16 11:52:34170 enum SocketOptions {
[email protected]5f01ce22013-04-30 00:53:18171 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0,
172 SOCKET_OPTION_BROADCAST = 1 << 1,
173 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2
[email protected]a1781d7b2012-07-16 11:52:34174 };
175
[email protected]2da659e2013-05-23 20:51:34176 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18177 public:
178 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
179
180 // MessageLoopForIO::Watcher methods
181
[email protected]2a848e0e2012-08-09 22:27:31182 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE;
[email protected]a2798d92011-03-02 22:56:18183
[email protected]f2cbbc82011-11-16 01:10:29184 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {}
[email protected]a2798d92011-03-02 22:56:18185
186 private:
187 UDPSocketLibevent* const socket_;
188
189 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
190 };
191
[email protected]2da659e2013-05-23 20:51:34192 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18193 public:
194 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
195
196 // MessageLoopForIO::Watcher methods
197
[email protected]f2cbbc82011-11-16 01:10:29198 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {}
[email protected]a2798d92011-03-02 22:56:18199
[email protected]2a848e0e2012-08-09 22:27:31200 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE;
[email protected]a2798d92011-03-02 22:56:18201
202 private:
203 UDPSocketLibevent* const socket_;
204
205 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
206 };
207
208 void DoReadCallback(int rv);
209 void DoWriteCallback(int rv);
210 void DidCompleteRead();
211 void DidCompleteWrite();
212
[email protected]8866f622011-10-18 20:08:10213 // Handles stats and logging. |result| is the number of bytes transferred, on
214 // success, or the net error code on failure. On success, LogRead takes in a
215 // sockaddr and its length, which are mandatory, while LogWrite takes in an
216 // optional IPEndPoint.
217 void LogRead(int result, const char* bytes, socklen_t addr_len,
218 const sockaddr* addr) const;
219 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
220
[email protected]a2798d92011-03-02 22:56:18221 // Returns the OS error code (or 0 on success).
[email protected]acfe8e82013-12-12 14:12:56222 int CreateSocket(int addr_family);
[email protected]a2798d92011-03-02 22:56:18223
[email protected]06043ae2011-03-16 00:00:21224 // Same as SendTo(), except that address is passed by pointer
225 // instead of by reference. It is called from Write() with |address|
226 // set to NULL.
227 int SendToOrWrite(IOBuffer* buf,
228 int buf_len,
229 const IPEndPoint* address,
[email protected]83039bb2011-12-09 18:43:55230 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21231
[email protected]8866f622011-10-18 20:08:10232 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21233 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
234 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18235
[email protected]a1781d7b2012-07-16 11:52:34236 // Applies |socket_options_| to |socket_|. Should be called before
237 // Bind().
238 int SetSocketOptions();
[email protected]5370c012011-06-29 03:47:04239 int DoBind(const IPEndPoint& address);
[email protected]acfe8e82013-12-12 14:12:56240 // Binds to a random port on |address|.
241 int RandomBind(const IPAddressNumber& address);
[email protected]5370c012011-06-29 03:47:04242
[email protected]a2798d92011-03-02 22:56:18243 int socket_;
[email protected]5f01ce22013-04-30 00:53:18244 int addr_family_;
[email protected]a2798d92011-03-02 22:56:18245
[email protected]7be809df2012-08-07 17:00:48246 // Bitwise-or'd combination of SocketOptions. Specifies the set of
247 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34248 int socket_options_;
249
[email protected]7b29dac2013-12-05 11:19:42250 // Multicast interface.
251 uint32 multicast_interface_;
252
[email protected]5f01ce22013-04-30 00:53:18253 // Multicast socket options cached for SetSocketOption.
254 // Cannot be used after Bind().
255 int multicast_time_to_live_;
256
[email protected]5370c012011-06-29 03:47:04257 // How to do source port binding, used only when UDPSocket is part of
258 // UDPClientSocket, since UDPServerSocket provides Bind.
259 DatagramSocket::BindType bind_type_;
260
261 // PRNG function for generating port numbers.
262 RandIntCallback rand_int_cb_;
263
[email protected]a2798d92011-03-02 22:56:18264 // These are mutable since they're just cached copies to make
265 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04266 mutable scoped_ptr<IPEndPoint> local_address_;
267 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18268
269 // The socket's libevent wrappers
[email protected]2da659e2013-05-23 20:51:34270 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
271 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18272
273 // The corresponding watchers for reads and writes.
274 ReadWatcher read_watcher_;
275 WriteWatcher write_watcher_;
276
[email protected]43d4a0262011-03-09 19:26:04277 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18278 scoped_refptr<IOBuffer> read_buf_;
279 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04280 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18281
[email protected]43d4a0262011-03-09 19:26:04282 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18283 scoped_refptr<IOBuffer> write_buf_;
284 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04285 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18286
287 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33288 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18289
290 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55291 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18292
293 BoundNetLog net_log_;
294
295 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
296};
297
298} // namespace net
299
300#endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_