Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: net/udp/udp_socket_posix.h

Issue 2508593002: net: move udp directory into socket (Closed)
Patch Set: revert sys/socket.h change Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/udp/udp_socket_perftest.cc ('k') | net/udp/udp_socket_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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_POSIX_H_
6 #define NET_UDP_UDP_SOCKET_POSIX_H_
7
8 #include <stdint.h>
9
10 #include <memory>
11
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "net/base/address_family.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_export.h"
21 #include "net/base/network_change_notifier.h"
22 #include "net/base/rand_callback.h"
23 #include "net/log/net_log_with_source.h"
24 #include "net/socket/socket_descriptor.h"
25 #include "net/udp/datagram_socket.h"
26 #include "net/udp/diff_serv_code_point.h"
27
28 namespace net {
29
30 class IPAddress;
31 class NetLog;
32 struct NetLogSource;
33
34 class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe {
35 public:
36 UDPSocketPosix(DatagramSocket::BindType bind_type,
37 const RandIntCallback& rand_int_cb,
38 net::NetLog* net_log,
39 const net::NetLogSource& source);
40 virtual ~UDPSocketPosix();
41
42 // Opens the socket.
43 // Returns a net error code.
44 int Open(AddressFamily address_family);
45
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
53 // Connects the socket to connect with a certain |address|.
54 // Should be called after Open().
55 // Returns a net error code.
56 int Connect(const IPEndPoint& address);
57
58 // Binds the address/port for this socket to |address|. This is generally
59 // only used on a server. Should be called after Open().
60 // Returns a net error code.
61 int Bind(const IPEndPoint& address);
62
63 // Closes the socket.
64 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
65 void Close();
66
67 // Copies the remote udp address into |address| and returns a net error code.
68 int GetPeerAddress(IPEndPoint* address) const;
69
70 // Copies the local udp address into |address| and returns a net error code.
71 // (similar to getsockname)
72 int GetLocalAddress(IPEndPoint* address) const;
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
78 // Reads from the socket.
79 // Only usable from the client-side of a UDP socket, after the socket
80 // has been connected.
81 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
82
83 // Writes to the socket.
84 // Only usable from the client-side of a UDP socket, after the socket
85 // has been connected.
86 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
87
88 // Reads from a socket and receive sender address information.
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.
94 // |callback| is the callback on completion of the RecvFrom.
95 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
96 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
97 // alive until the callback is called.
98 int RecvFrom(IOBuffer* buf,
99 int buf_len,
100 IPEndPoint* address,
101 const CompletionCallback& callback);
102
103 // Sends to a socket with a particular destination.
104 // |buf| is the buffer to send.
105 // |buf_len| is the number of bytes to send.
106 // |address| is the recipient address.
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,
113 const IPEndPoint& address,
114 const CompletionCallback& callback);
115
116 // Sets the receive buffer size (in bytes) for the socket.
117 // Returns a net error code.
118 int SetReceiveBufferSize(int32_t size);
119
120 // Sets the send buffer size (in bytes) for the socket.
121 // Returns a net error code.
122 int SetSendBufferSize(int32_t size);
123
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
131 // Returns true if the socket is already connected or bound.
132 bool is_connected() const { return is_connected_; }
133
134 const NetLogWithSource& NetLog() const { return net_log_; }
135
136 // Sets corresponding flags in |socket_options_| to allow the socket
137 // to share the local address to which the socket will be bound with
138 // other processes. Should be called between Open() and Bind().
139 // Returns a net error code.
140 int AllowAddressReuse();
141
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);
146
147 // Joins the multicast group.
148 // |group_address| is the group address to join, could be either
149 // an IPv4 or IPv6 address.
150 // Returns a net error code.
151 int JoinGroup(const IPAddress& group_address) const;
152
153 // Leaves the multicast group.
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.
159 // Returns a net error code.
160 int LeaveGroup(const IPAddress& group_address) const;
161
162 // Sets interface to use for multicast. If |interface_index| set to 0,
163 // default interface is used.
164 // Should be called before Bind().
165 // Returns a net error code.
166 int SetMulticastInterface(uint32_t interface_index);
167
168 // Sets the time-to-live option for UDP packets sent to the multicast
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().
172 // Returns a net error code.
173 int SetMulticastTimeToLive(int time_to_live);
174
175 // Sets the loopback flag for UDP socket. If this flag is true, the host
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().
179 // Returns a net error code.
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
191 // Sets the differentiated services flags on outgoing packets. May not
192 // do anything on some platforms.
193 // Returns a net error code.
194 int SetDiffServCodePoint(DiffServCodePoint dscp);
195
196 // Resets the thread to be used for thread-safety checks.
197 void DetachFromThread();
198
199 private:
200 enum SocketOptions {
201 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
202 };
203
204 class ReadWatcher : public base::MessageLoopForIO::Watcher {
205 public:
206 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
207
208 // MessageLoopForIO::Watcher methods
209
210 void OnFileCanReadWithoutBlocking(int /* fd */) override;
211
212 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
213
214 private:
215 UDPSocketPosix* const socket_;
216
217 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
218 };
219
220 class WriteWatcher : public base::MessageLoopForIO::Watcher {
221 public:
222 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
223
224 // MessageLoopForIO::Watcher methods
225
226 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
227
228 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
229
230 private:
231 UDPSocketPosix* const socket_;
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
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
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,
255 const CompletionCallback& callback);
256
257 int InternalConnect(const IPEndPoint& address);
258 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
259 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
260
261 // Applies |socket_options_| to |socket_|. Should be called before
262 // Bind().
263 int SetMulticastOptions();
264 int DoBind(const IPEndPoint& address);
265 // Binds to a random port on |address|.
266 int RandomBind(const IPAddress& address);
267
268 int socket_;
269
270 int addr_family_;
271 bool is_connected_;
272
273 // Bitwise-or'd combination of SocketOptions. Specifies the set of
274 // options that should be applied to |socket_| before Bind().
275 int socket_options_;
276
277 // Multicast interface.
278 uint32_t multicast_interface_;
279
280 // Multicast socket options cached for SetMulticastOption.
281 // Cannot be used after Bind().
282 int multicast_time_to_live_;
283
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
291 // These are mutable since they're just cached copies to make
292 // GetPeerAddress/GetLocalAddress smarter.
293 mutable std::unique_ptr<IPEndPoint> local_address_;
294 mutable std::unique_ptr<IPEndPoint> remote_address_;
295
296 // The socket's posix wrappers
297 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
298 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
299
300 // The corresponding watchers for reads and writes.
301 ReadWatcher read_watcher_;
302 WriteWatcher write_watcher_;
303
304 // The buffer used by InternalRead() to retry Read requests
305 scoped_refptr<IOBuffer> read_buf_;
306 int read_buf_len_;
307 IPEndPoint* recv_from_address_;
308
309 // The buffer used by InternalWrite() to retry Write requests
310 scoped_refptr<IOBuffer> write_buf_;
311 int write_buf_len_;
312 std::unique_ptr<IPEndPoint> send_to_address_;
313
314 // External callback; called when read is complete.
315 CompletionCallback read_callback_;
316
317 // External callback; called when write is complete.
318 CompletionCallback write_callback_;
319
320 NetLogWithSource net_log_;
321
322 // Network that this socket is bound to via BindToNetwork().
323 NetworkChangeNotifier::NetworkHandle bound_network_;
324
325 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix);
326 };
327
328 } // namespace net
329
330 #endif // NET_UDP_UDP_SOCKET_POSIX_H_
OLDNEW
« no previous file with comments | « net/udp/udp_socket_perftest.cc ('k') | net/udp/udp_socket_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698