blob: 6c8bf61e0dd58f8c75229ed9ee66fa7684577638 [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.
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]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.
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]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
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]a2798d92011-03-02 22:56:18155 private:
[email protected]a1781d7b2012-07-16 11:52:34156 enum SocketOptions {
[email protected]5f01ce22013-04-30 00:53:18157 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0,
158 SOCKET_OPTION_BROADCAST = 1 << 1,
159 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2
[email protected]a1781d7b2012-07-16 11:52:34160 };
161
[email protected]2da659e2013-05-23 20:51:34162 class ReadWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18163 public:
164 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
165
166 // MessageLoopForIO::Watcher methods
167
[email protected]2a848e0e2012-08-09 22:27:31168 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE;
[email protected]a2798d92011-03-02 22:56:18169
[email protected]f2cbbc82011-11-16 01:10:29170 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {}
[email protected]a2798d92011-03-02 22:56:18171
172 private:
173 UDPSocketLibevent* const socket_;
174
175 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
176 };
177
[email protected]2da659e2013-05-23 20:51:34178 class WriteWatcher : public base::MessageLoopForIO::Watcher {
[email protected]a2798d92011-03-02 22:56:18179 public:
180 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
181
182 // MessageLoopForIO::Watcher methods
183
[email protected]f2cbbc82011-11-16 01:10:29184 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {}
[email protected]a2798d92011-03-02 22:56:18185
[email protected]2a848e0e2012-08-09 22:27:31186 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE;
[email protected]a2798d92011-03-02 22:56:18187
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]8866f622011-10-18 20:08:10199 // 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]a2798d92011-03-02 22:56:18207 // Returns the OS error code (or 0 on success).
[email protected]43d4a0262011-03-09 19:26:04208 int CreateSocket(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:18209
[email protected]06043ae2011-03-16 00:00:21210 // 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]83039bb2011-12-09 18:43:55216 const CompletionCallback& callback);
[email protected]06043ae2011-03-16 00:00:21217
[email protected]8866f622011-10-18 20:08:10218 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21219 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
220 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18221
[email protected]a1781d7b2012-07-16 11:52:34222 // Applies |socket_options_| to |socket_|. Should be called before
223 // Bind().
224 int SetSocketOptions();
[email protected]5370c012011-06-29 03:47:04225 int DoBind(const IPEndPoint& address);
226 int RandomBind(const IPEndPoint& address);
227
[email protected]a2798d92011-03-02 22:56:18228 int socket_;
[email protected]5f01ce22013-04-30 00:53:18229 int addr_family_;
[email protected]a2798d92011-03-02 22:56:18230
[email protected]7be809df2012-08-07 17:00:48231 // Bitwise-or'd combination of SocketOptions. Specifies the set of
232 // options that should be applied to |socket_| before Bind().
[email protected]a1781d7b2012-07-16 11:52:34233 int socket_options_;
234
[email protected]5f01ce22013-04-30 00:53:18235 // Multicast socket options cached for SetSocketOption.
236 // Cannot be used after Bind().
237 int multicast_time_to_live_;
238
[email protected]5370c012011-06-29 03:47:04239 // 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]a2798d92011-03-02 22:56:18246 // These are mutable since they're just cached copies to make
247 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04248 mutable scoped_ptr<IPEndPoint> local_address_;
249 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18250
251 // The socket's libevent wrappers
[email protected]2da659e2013-05-23 20:51:34252 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
253 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
[email protected]a2798d92011-03-02 22:56:18254
255 // The corresponding watchers for reads and writes.
256 ReadWatcher read_watcher_;
257 WriteWatcher write_watcher_;
258
[email protected]43d4a0262011-03-09 19:26:04259 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18260 scoped_refptr<IOBuffer> read_buf_;
261 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04262 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18263
[email protected]43d4a0262011-03-09 19:26:04264 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18265 scoped_refptr<IOBuffer> write_buf_;
266 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04267 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18268
269 // External callback; called when read is complete.
[email protected]3f55aa12011-12-07 02:03:33270 CompletionCallback read_callback_;
[email protected]a2798d92011-03-02 22:56:18271
272 // External callback; called when write is complete.
[email protected]83039bb2011-12-09 18:43:55273 CompletionCallback write_callback_;
[email protected]a2798d92011-03-02 22:56:18274
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_