OLD | NEW |
| (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_WIN_H_ | |
6 #define NET_UDP_UDP_SOCKET_WIN_H_ | |
7 | |
8 #include <qos2.h> | |
9 #include <stdint.h> | |
10 #include <winsock2.h> | |
11 | |
12 #include <memory> | |
13 | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/threading/non_thread_safe.h" | |
18 #include "base/win/object_watcher.h" | |
19 #include "base/win/scoped_handle.h" | |
20 #include "net/base/address_family.h" | |
21 #include "net/base/completion_callback.h" | |
22 #include "net/base/io_buffer.h" | |
23 #include "net/base/ip_endpoint.h" | |
24 #include "net/base/net_export.h" | |
25 #include "net/base/network_change_notifier.h" | |
26 #include "net/base/rand_callback.h" | |
27 #include "net/log/net_log_with_source.h" | |
28 #include "net/udp/datagram_socket.h" | |
29 #include "net/udp/diff_serv_code_point.h" | |
30 | |
31 namespace net { | |
32 | |
33 class IPAddress; | |
34 class NetLog; | |
35 struct NetLogSource; | |
36 | |
37 class NET_EXPORT UDPSocketWin | |
38 : NON_EXPORTED_BASE(public base::NonThreadSafe), | |
39 NON_EXPORTED_BASE(public base::win::ObjectWatcher::Delegate) { | |
40 public: | |
41 UDPSocketWin(DatagramSocket::BindType bind_type, | |
42 const RandIntCallback& rand_int_cb, | |
43 net::NetLog* net_log, | |
44 const net::NetLogSource& source); | |
45 ~UDPSocketWin() override; | |
46 | |
47 // Opens the socket. | |
48 // Returns a net error code. | |
49 int Open(AddressFamily address_family); | |
50 | |
51 // Binds this socket to |network|. All data traffic on the socket will be sent | |
52 // and received via |network|. Must be called before Connect(). This call will | |
53 // fail if |network| has disconnected. Communication using this socket will | |
54 // fail if |network| disconnects. | |
55 // Returns a net error code. | |
56 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network); | |
57 | |
58 // Connects the socket to connect with a certain |address|. | |
59 // Should be called after Open(). | |
60 // Returns a net error code. | |
61 int Connect(const IPEndPoint& address); | |
62 | |
63 // Binds the address/port for this socket to |address|. This is generally | |
64 // only used on a server. Should be called after Open(). | |
65 // Returns a net error code. | |
66 int Bind(const IPEndPoint& address); | |
67 | |
68 // Closes the socket. | |
69 // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). | |
70 void Close(); | |
71 | |
72 // Copies the remote udp address into |address| and returns a net error code. | |
73 int GetPeerAddress(IPEndPoint* address) const; | |
74 | |
75 // Copies the local udp address into |address| and returns a net error code. | |
76 // (similar to getsockname) | |
77 int GetLocalAddress(IPEndPoint* address) const; | |
78 | |
79 // IO: | |
80 // Multiple outstanding read requests are not supported. | |
81 // Full duplex mode (reading and writing at the same time) is supported | |
82 | |
83 // Reads from the socket. | |
84 // Only usable from the client-side of a UDP socket, after the socket | |
85 // has been connected. | |
86 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
87 | |
88 // Writes to the socket. | |
89 // Only usable from the client-side of a UDP socket, after the socket | |
90 // has been connected. | |
91 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
92 | |
93 // Reads from a socket and receive sender address information. | |
94 // |buf| is the buffer to read data into. | |
95 // |buf_len| is the maximum amount of data to read. | |
96 // |address| is a buffer provided by the caller for receiving the sender | |
97 // address information about the received data. This buffer must be kept | |
98 // alive by the caller until the callback is placed. | |
99 // |callback| is the callback on completion of the RecvFrom. | |
100 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
101 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|, | |
102 // alive until the callback is called. | |
103 int RecvFrom(IOBuffer* buf, | |
104 int buf_len, | |
105 IPEndPoint* address, | |
106 const CompletionCallback& callback); | |
107 | |
108 // Sends to a socket with a particular destination. | |
109 // |buf| is the buffer to send. | |
110 // |buf_len| is the number of bytes to send. | |
111 // |address| is the recipient address. | |
112 // |callback| is the user callback function to call on complete. | |
113 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
114 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | |
115 // alive until the callback is called. | |
116 int SendTo(IOBuffer* buf, | |
117 int buf_len, | |
118 const IPEndPoint& address, | |
119 const CompletionCallback& callback); | |
120 | |
121 // Sets the receive buffer size (in bytes) for the socket. | |
122 // Returns a net error code. | |
123 int SetReceiveBufferSize(int32_t size); | |
124 | |
125 // Sets the send buffer size (in bytes) for the socket. | |
126 // Returns a net error code. | |
127 int SetSendBufferSize(int32_t size); | |
128 | |
129 // Requests that packets sent by this socket not be fragment, either locally | |
130 // by the host, or by routers (via the DF bit in the IPv4 packet header). | |
131 // May not be supported by all platforms. Returns a return a network error | |
132 // code if there was a problem, but the socket will still be usable. Can not | |
133 // return ERR_IO_PENDING. | |
134 int SetDoNotFragment(); | |
135 | |
136 // Returns true if the socket is already connected or bound. | |
137 bool is_connected() const { return is_connected_; } | |
138 | |
139 const NetLogWithSource& NetLog() const { return net_log_; } | |
140 | |
141 // Sets corresponding flags in |socket_options_| to allow the socket | |
142 // to share the local address to which the socket will be bound with | |
143 // other processes. Should be called between Open() and Bind(). | |
144 // Returns a net error code. | |
145 int AllowAddressReuse(); | |
146 | |
147 // Sets corresponding flags in |socket_options_| to allow sending | |
148 // and receiving packets to and from broadcast addresses. | |
149 int SetBroadcast(bool broadcast); | |
150 | |
151 // Joins the multicast group. | |
152 // |group_address| is the group address to join, could be either | |
153 // an IPv4 or IPv6 address. | |
154 // Returns a net error code. | |
155 int JoinGroup(const IPAddress& group_address) const; | |
156 | |
157 // Leaves the multicast group. | |
158 // |group_address| is the group address to leave, could be either | |
159 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | |
160 // it will be ignored. | |
161 // It's optional to leave the multicast group before destroying | |
162 // the socket. It will be done by the OS. | |
163 // Return a net error code. | |
164 int LeaveGroup(const IPAddress& group_address) const; | |
165 | |
166 // Sets interface to use for multicast. If |interface_index| set to 0, | |
167 // default interface is used. | |
168 // Should be called before Bind(). | |
169 // Returns a net error code. | |
170 int SetMulticastInterface(uint32_t interface_index); | |
171 | |
172 // Sets the time-to-live option for UDP packets sent to the multicast | |
173 // group address. The default value of this option is 1. | |
174 // Cannot be negative or more than 255. | |
175 // Should be called before Bind(). | |
176 int SetMulticastTimeToLive(int time_to_live); | |
177 | |
178 // Sets the loopback flag for UDP socket. If this flag is true, the host | |
179 // will receive packets sent to the joined group from itself. | |
180 // The default value of this option is true. | |
181 // Should be called before Bind(). | |
182 // | |
183 // Note: the behavior of |SetMulticastLoopbackMode| is slightly | |
184 // different between Windows and Unix-like systems. The inconsistency only | |
185 // happens when there are more than one applications on the same host | |
186 // joined to the same multicast group while having different settings on | |
187 // multicast loopback mode. On Windows, the applications with loopback off | |
188 // will not RECEIVE the loopback packets; while on Unix-like systems, the | |
189 // applications with loopback off will not SEND the loopback packets to | |
190 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | |
191 int SetMulticastLoopbackMode(bool loopback); | |
192 | |
193 // Sets the differentiated services flags on outgoing packets. May not | |
194 // do anything on some platforms. | |
195 int SetDiffServCodePoint(DiffServCodePoint dscp); | |
196 | |
197 // Resets the thread to be used for thread-safety checks. | |
198 void DetachFromThread(); | |
199 | |
200 // This class by default uses overlapped IO. Call this method before Open() | |
201 // to switch to non-blocking IO. | |
202 void UseNonBlockingIO(); | |
203 | |
204 private: | |
205 enum SocketOptions { | |
206 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 | |
207 }; | |
208 | |
209 class Core; | |
210 | |
211 void DoReadCallback(int rv); | |
212 void DoWriteCallback(int rv); | |
213 | |
214 void DidCompleteRead(); | |
215 void DidCompleteWrite(); | |
216 | |
217 // base::ObjectWatcher::Delegate implementation. | |
218 void OnObjectSignaled(HANDLE object) override; | |
219 void OnReadSignaled(); | |
220 void OnWriteSignaled(); | |
221 | |
222 void WatchForReadWrite(); | |
223 | |
224 // Handles stats and logging. |result| is the number of bytes transferred, on | |
225 // success, or the net error code on failure. | |
226 void LogRead(int result, const char* bytes, const IPEndPoint* address) const; | |
227 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | |
228 | |
229 // Same as SendTo(), except that address is passed by pointer | |
230 // instead of by reference. It is called from Write() with |address| | |
231 // set to NULL. | |
232 int SendToOrWrite(IOBuffer* buf, | |
233 int buf_len, | |
234 const IPEndPoint* address, | |
235 const CompletionCallback& callback); | |
236 | |
237 int InternalConnect(const IPEndPoint& address); | |
238 | |
239 // Version for using overlapped IO. | |
240 int InternalRecvFromOverlapped(IOBuffer* buf, | |
241 int buf_len, | |
242 IPEndPoint* address); | |
243 int InternalSendToOverlapped(IOBuffer* buf, | |
244 int buf_len, | |
245 const IPEndPoint* address); | |
246 | |
247 // Version for using non-blocking IO. | |
248 int InternalRecvFromNonBlocking(IOBuffer* buf, | |
249 int buf_len, | |
250 IPEndPoint* address); | |
251 int InternalSendToNonBlocking(IOBuffer* buf, | |
252 int buf_len, | |
253 const IPEndPoint* address); | |
254 | |
255 // Applies |socket_options_| to |socket_|. Should be called before | |
256 // Bind(). | |
257 int SetMulticastOptions(); | |
258 int DoBind(const IPEndPoint& address); | |
259 // Binds to a random port on |address|. | |
260 int RandomBind(const IPAddress& address); | |
261 | |
262 SOCKET socket_; | |
263 int addr_family_; | |
264 bool is_connected_; | |
265 | |
266 // Bitwise-or'd combination of SocketOptions. Specifies the set of | |
267 // options that should be applied to |socket_| before Bind(). | |
268 int socket_options_; | |
269 | |
270 // Multicast interface. | |
271 uint32_t multicast_interface_; | |
272 | |
273 // Multicast socket options cached for SetMulticastOption. | |
274 // Cannot be used after Bind(). | |
275 int multicast_time_to_live_; | |
276 | |
277 // How to do source port binding, used only when UDPSocket is part of | |
278 // UDPClientSocket, since UDPServerSocket provides Bind. | |
279 DatagramSocket::BindType bind_type_; | |
280 | |
281 // PRNG function for generating port numbers. | |
282 RandIntCallback rand_int_cb_; | |
283 | |
284 // These are mutable since they're just cached copies to make | |
285 // GetPeerAddress/GetLocalAddress smarter. | |
286 mutable std::unique_ptr<IPEndPoint> local_address_; | |
287 mutable std::unique_ptr<IPEndPoint> remote_address_; | |
288 | |
289 // The core of the socket that can live longer than the socket itself. We pass | |
290 // resources to the Windows async IO functions and we have to make sure that | |
291 // they are not destroyed while the OS still references them. | |
292 scoped_refptr<Core> core_; | |
293 | |
294 // True if non-blocking IO is used. | |
295 bool use_non_blocking_io_; | |
296 | |
297 // Watches |read_write_event_|. | |
298 base::win::ObjectWatcher read_write_watcher_; | |
299 | |
300 // Events for read and write. | |
301 base::win::ScopedHandle read_write_event_; | |
302 | |
303 // The buffers used in Read() and Write(). | |
304 scoped_refptr<IOBuffer> read_iobuffer_; | |
305 scoped_refptr<IOBuffer> write_iobuffer_; | |
306 | |
307 int read_iobuffer_len_; | |
308 int write_iobuffer_len_; | |
309 | |
310 IPEndPoint* recv_from_address_; | |
311 | |
312 // Cached copy of the current address we're sending to, if any. Used for | |
313 // logging. | |
314 std::unique_ptr<IPEndPoint> send_to_address_; | |
315 | |
316 // External callback; called when read is complete. | |
317 CompletionCallback read_callback_; | |
318 | |
319 // External callback; called when write is complete. | |
320 CompletionCallback write_callback_; | |
321 | |
322 NetLogWithSource net_log_; | |
323 | |
324 // QWAVE data. Used to set DSCP bits on outgoing packets. | |
325 HANDLE qos_handle_; | |
326 QOS_FLOWID qos_flow_id_; | |
327 | |
328 DISALLOW_COPY_AND_ASSIGN(UDPSocketWin); | |
329 }; | |
330 | |
331 //----------------------------------------------------------------------------- | |
332 | |
333 // QWAVE (Quality Windows Audio/Video Experience) is the latest windows | |
334 // library for setting packet priorities (and other things). Unfortunately, | |
335 // Microsoft has decided that setting the DSCP bits with setsockopt() no | |
336 // longer works, so we have to use this API instead. | |
337 // This class is meant to be used as a singleton. It exposes a few dynamically | |
338 // loaded functions and a bool called "qwave_supported". | |
339 class NET_EXPORT QwaveAPI { | |
340 typedef BOOL (WINAPI *CreateHandleFn)(PQOS_VERSION, PHANDLE); | |
341 typedef BOOL (WINAPI *CloseHandleFn)(HANDLE); | |
342 typedef BOOL (WINAPI *AddSocketToFlowFn)( | |
343 HANDLE, SOCKET, PSOCKADDR, QOS_TRAFFIC_TYPE, DWORD, PQOS_FLOWID); | |
344 typedef BOOL (WINAPI *RemoveSocketFromFlowFn)( | |
345 HANDLE, SOCKET, QOS_FLOWID, DWORD); | |
346 typedef BOOL (WINAPI *SetFlowFn)( | |
347 HANDLE, QOS_FLOWID, QOS_SET_FLOW, ULONG, PVOID, DWORD, LPOVERLAPPED); | |
348 | |
349 public: | |
350 QwaveAPI(); | |
351 | |
352 static QwaveAPI& Get(); | |
353 | |
354 bool qwave_supported() const; | |
355 BOOL CreateHandle(PQOS_VERSION version, PHANDLE handle); | |
356 BOOL CloseHandle(HANDLE handle); | |
357 BOOL AddSocketToFlow(HANDLE handle, | |
358 SOCKET socket, | |
359 PSOCKADDR addr, | |
360 QOS_TRAFFIC_TYPE traffic_type, | |
361 DWORD flags, | |
362 PQOS_FLOWID flow_id); | |
363 BOOL RemoveSocketFromFlow(HANDLE handle, | |
364 SOCKET socket, | |
365 QOS_FLOWID flow_id, | |
366 DWORD reserved); | |
367 BOOL SetFlow(HANDLE handle, | |
368 QOS_FLOWID flow_id, | |
369 QOS_SET_FLOW op, | |
370 ULONG size, | |
371 PVOID data, | |
372 DWORD reserved, | |
373 LPOVERLAPPED overlapped); | |
374 | |
375 private: | |
376 bool qwave_supported_; | |
377 CreateHandleFn create_handle_func_; | |
378 CloseHandleFn close_handle_func_; | |
379 AddSocketToFlowFn add_socket_to_flow_func_; | |
380 RemoveSocketFromFlowFn remove_socket_from_flow_func_; | |
381 SetFlowFn set_flow_func_; | |
382 | |
383 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); | |
384 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); | |
385 }; | |
386 | |
387 | |
388 } // namespace net | |
389 | |
390 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | |
OLD | NEW |