xref: /aosp_15_r20/external/cronet/net/socket/udp_socket_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_SOCKET_UDP_SOCKET_WIN_H_
6 #define NET_SOCKET_UDP_SOCKET_WIN_H_
7 
8 #include <winsock2.h>
9 
10 #include <qos2.h>
11 #include <stdint.h>
12 
13 // Must be after winsock2.h:
14 #include <MSWSock.h>
15 
16 #include <atomic>
17 #include <memory>
18 #include <set>
19 
20 #include "base/gtest_prod_util.h"
21 #include "base/memory/raw_ptr.h"
22 #include "base/memory/scoped_refptr.h"
23 #include "base/memory/weak_ptr.h"
24 #include "base/threading/thread_checker.h"
25 #include "base/win/object_watcher.h"
26 #include "base/win/scoped_handle.h"
27 #include "net/base/address_family.h"
28 #include "net/base/completion_once_callback.h"
29 #include "net/base/io_buffer.h"
30 #include "net/base/ip_endpoint.h"
31 #include "net/base/net_export.h"
32 #include "net/base/network_handle.h"
33 #include "net/base/sockaddr_storage.h"
34 #include "net/log/net_log_with_source.h"
35 #include "net/socket/datagram_socket.h"
36 #include "net/socket/diff_serv_code_point.h"
37 #include "net/socket/udp_socket_global_limits.h"
38 #include "net/traffic_annotation/network_traffic_annotation.h"
39 
40 namespace net {
41 
42 class IPAddress;
43 class NetLog;
44 struct NetLogSource;
45 class SocketTag;
46 
47 // QWAVE (Quality Windows Audio/Video Experience) is the latest windows
48 // library for setting packet priorities (and other things). Unfortunately,
49 // Microsoft has decided that setting the DSCP bits with setsockopt() no
50 // longer works, so we have to use this API instead.
51 // This class is meant to be used as a singleton. It exposes a few dynamically
52 // loaded functions and a bool called "qwave_supported".
53 class NET_EXPORT QwaveApi {
54   typedef BOOL(WINAPI* CreateHandleFn)(PQOS_VERSION, PHANDLE);
55   typedef BOOL(WINAPI* CloseHandleFn)(HANDLE);
56   typedef BOOL(WINAPI* AddSocketToFlowFn)(HANDLE,
57                                           SOCKET,
58                                           PSOCKADDR,
59                                           QOS_TRAFFIC_TYPE,
60                                           DWORD,
61                                           PQOS_FLOWID);
62   typedef BOOL(WINAPI* RemoveSocketFromFlowFn)(HANDLE,
63                                                SOCKET,
64                                                QOS_FLOWID,
65                                                DWORD);
66   typedef BOOL(WINAPI* SetFlowFn)(HANDLE,
67                                   QOS_FLOWID,
68                                   QOS_SET_FLOW,
69                                   ULONG,
70                                   PVOID,
71                                   DWORD,
72                                   LPOVERLAPPED);
73 
74  public:
75   QwaveApi();
76 
77   QwaveApi(const QwaveApi&) = delete;
78   QwaveApi& operator=(const QwaveApi&) = delete;
79 
80   static QwaveApi* GetDefault();
81 
82   virtual bool qwave_supported() const;
83   virtual void OnFatalError();
84 
85   virtual BOOL CreateHandle(PQOS_VERSION version, PHANDLE handle);
86   virtual BOOL CloseHandle(HANDLE handle);
87   virtual BOOL AddSocketToFlow(HANDLE handle,
88                                SOCKET socket,
89                                PSOCKADDR addr,
90                                QOS_TRAFFIC_TYPE traffic_type,
91                                DWORD flags,
92                                PQOS_FLOWID flow_id);
93   virtual BOOL RemoveSocketFromFlow(HANDLE handle,
94                                     SOCKET socket,
95                                     QOS_FLOWID flow_id,
96                                     DWORD reserved);
97   virtual BOOL SetFlow(HANDLE handle,
98                        QOS_FLOWID flow_id,
99                        QOS_SET_FLOW op,
100                        ULONG size,
101                        PVOID data,
102                        DWORD reserved,
103                        LPOVERLAPPED overlapped);
104 
105  private:
106   std::atomic<bool> qwave_supported_{false};
107 
108   CreateHandleFn create_handle_func_;
109   CloseHandleFn close_handle_func_;
110   AddSocketToFlowFn add_socket_to_flow_func_;
111   RemoveSocketFromFlowFn remove_socket_from_flow_func_;
112   SetFlowFn set_flow_func_;
113 };
114 
115 //-----------------------------------------------------------------------------
116 
117 // Helper for maintaining the state that (unlike a blanket socket option), DSCP
118 // values are set per-remote endpoint instead of just per-socket on Windows.
119 // The implementation creates a single QWAVE 'flow' for the socket, and adds
120 // all encountered remote addresses to that flow.  Flows are the minimum
121 // manageable unit within the QWAVE API.  See
122 // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/api/qos2/
123 // for Microsoft's documentation.
124 class NET_EXPORT DscpManager {
125  public:
126   DscpManager(QwaveApi* api, SOCKET socket);
127 
128   DscpManager(const DscpManager&) = delete;
129   DscpManager& operator=(const DscpManager&) = delete;
130 
131   ~DscpManager();
132 
133   // Remembers the latest |dscp| so PrepareToSend can add remote addresses to
134   // the qos flow. Destroys the old flow if it exists and |dscp| changes.
135   void Set(DiffServCodePoint dscp);
136 
137   // Constructs a qos flow for the latest set DSCP value if we don't already
138   // have one. Adds |remote_address| to the qos flow if it hasn't been added
139   // already. Does nothing if no DSCP value has been Set.
140   int PrepareForSend(const IPEndPoint& remote_address);
141 
142  private:
143   void RequestHandle();
144   static HANDLE DoCreateHandle(QwaveApi* api);
145   static void OnHandleCreated(QwaveApi* api,
146                               base::WeakPtr<DscpManager> dscp_manager,
147                               HANDLE handle);
148 
149   const raw_ptr<QwaveApi> api_;
150   const SOCKET socket_;
151 
152   DiffServCodePoint dscp_value_ = DSCP_NO_CHANGE;
153   // The remote addresses currently in the flow.
154   std::set<IPEndPoint> configured_;
155 
156   HANDLE qos_handle_ = nullptr;
157   bool handle_is_initializing_ = false;
158   // 0 means no flow has been constructed.
159   QOS_FLOWID flow_id_ = 0;
160   base::WeakPtrFactory<DscpManager> weak_ptr_factory_{this};
161 };
162 
163 //-----------------------------------------------------------------------------
164 
165 class NET_EXPORT UDPSocketWin : public base::win::ObjectWatcher::Delegate {
166  public:
167   // BindType is ignored. Windows has an option to do random binds, so
168   // UDPSocketWin sets that whenever connecting a socket.
169   UDPSocketWin(DatagramSocket::BindType bind_type,
170                net::NetLog* net_log,
171                const net::NetLogSource& source);
172 
173   UDPSocketWin(DatagramSocket::BindType bind_type,
174                NetLogWithSource source_net_log);
175 
176   UDPSocketWin(const UDPSocketWin&) = delete;
177   UDPSocketWin& operator=(const UDPSocketWin&) = delete;
178 
179   ~UDPSocketWin() override;
180 
181   // Opens the socket.
182   // Returns a net error code.
183   int Open(AddressFamily address_family);
184 
185   // Not implemented. Returns ERR_NOT_IMPLEMENTED.
186   int BindToNetwork(handles::NetworkHandle network);
187 
188   // Connects the socket to connect with a certain |address|.
189   // Should be called after Open().
190   // Returns a net error code.
191   int Connect(const IPEndPoint& address);
192 
193   // Binds the address/port for this socket to |address|.  This is generally
194   // only used on a server. Should be called after Open().
195   // Returns a net error code.
196   int Bind(const IPEndPoint& address);
197 
198   // Closes the socket.
199   void Close();
200 
201   // Copies the remote udp address into |address| and returns a net error code.
202   int GetPeerAddress(IPEndPoint* address) const;
203 
204   // Copies the local udp address into |address| and returns a net error code.
205   // (similar to getsockname)
206   int GetLocalAddress(IPEndPoint* address) const;
207 
208   // IO:
209   // Multiple outstanding read requests are not supported.
210   // Full duplex mode (reading and writing at the same time) is supported
211 
212   // Reads from the socket.
213   // Only usable from the client-side of a UDP socket, after the socket
214   // has been connected.
215   int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
216 
217   // Writes to the socket.
218   // Only usable from the client-side of a UDP socket, after the socket
219   // has been connected.
220   int Write(IOBuffer* buf,
221             int buf_len,
222             CompletionOnceCallback callback,
223             const NetworkTrafficAnnotationTag& traffic_annotation);
224 
225   // Reads from a socket and receive sender address information.
226   // |buf| is the buffer to read data into.
227   // |buf_len| is the maximum amount of data to read.
228   // |address| is a buffer provided by the caller for receiving the sender
229   //   address information about the received data.  This buffer must be kept
230   //   alive by the caller until the callback is placed.
231   // |callback| is the callback on completion of the RecvFrom.
232   // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
233   // If ERR_IO_PENDING is returned, this socket takes a ref to |buf| to keep
234   // it alive until the data is received. However, the caller must keep
235   // |address| alive until the callback is called.
236   int RecvFrom(IOBuffer* buf,
237                int buf_len,
238                IPEndPoint* address,
239                CompletionOnceCallback callback);
240 
241   // Sends to a socket with a particular destination.
242   // |buf| is the buffer to send.
243   // |buf_len| is the number of bytes to send.
244   // |address| is the recipient address.
245   // |callback| is the user callback function to call on complete.
246   // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
247   // If ERR_IO_PENDING is returned, this socket copies |address| for
248   // asynchronous sending, and takes a ref to |buf| to keep it alive until the
249   // data is sent.
250   int SendTo(IOBuffer* buf,
251              int buf_len,
252              const IPEndPoint& address,
253              CompletionOnceCallback callback);
254 
255   // Sets the receive buffer size (in bytes) for the socket.
256   // Returns a net error code.
257   int SetReceiveBufferSize(int32_t size);
258 
259   // Sets the send buffer size (in bytes) for the socket.
260   // Returns a net error code.
261   int SetSendBufferSize(int32_t size);
262 
263   // Requests that packets sent by this socket not be fragment, either locally
264   // by the host, or by routers (via the DF bit in the IPv4 packet header).
265   // May not be supported by all platforms. Returns a network error code if
266   // there was a problem, but the socket will still be usable. Can not
267   // return ERR_IO_PENDING.
268   int SetDoNotFragment();
269 
270   // Requests that packets received by this socket have the ECN bit set. Returns
271   // a network error code if there was a problem.
272   int SetRecvTos();
273 
274   // This is a no-op on Windows.
275   void SetMsgConfirm(bool confirm);
276 
277   // Returns true if the socket is already connected or bound.
is_connected()278   bool is_connected() const { return is_connected_; }
279 
NetLog()280   const NetLogWithSource& NetLog() const { return net_log_; }
281 
282   // Sets socket options to allow the socket to share the local address to which
283   // the socket will be bound with other processes. If multiple processes are
284   // bound to the same local address at the same time, behavior is undefined;
285   // e.g., it is not guaranteed that incoming  messages will be sent to all
286   // listening sockets. Returns a net error code.
287   //
288   // Should be called between Open() and Bind().
289   int AllowAddressReuse();
290 
291   // Sets socket options to allow sending and receiving packets to and from
292   // broadcast addresses.
293   int SetBroadcast(bool broadcast);
294 
295   // Sets socket options to allow the socket to share the local address to which
296   // the socket will be bound with other processes and attempt to allow all such
297   // sockets to receive the same multicast messages. Returns a net error code.
298   //
299   // For Windows, multicast messages should always be shared between sockets
300   // configured thusly as long as the sockets join the same multicast group and
301   // interface.
302   //
303   // Should be called between Open() and Bind().
304   int AllowAddressSharingForMulticast();
305 
306   // Joins the multicast group.
307   // |group_address| is the group address to join, could be either
308   // an IPv4 or IPv6 address.
309   // Returns a net error code.
310   int JoinGroup(const IPAddress& group_address) const;
311 
312   // Leaves the multicast group.
313   // |group_address| is the group address to leave, could be either
314   // an IPv4 or IPv6 address. If the socket hasn't joined the group,
315   // it will be ignored.
316   // It's optional to leave the multicast group before destroying
317   // the socket. It will be done by the OS.
318   // Return a net error code.
319   int LeaveGroup(const IPAddress& group_address) const;
320 
321   // Sets interface to use for multicast. If |interface_index| set to 0,
322   // default interface is used.
323   // Should be called before Bind().
324   // Returns a net error code.
325   int SetMulticastInterface(uint32_t interface_index);
326 
327   // Sets the time-to-live option for UDP packets sent to the multicast
328   // group address. The default value of this option is 1.
329   // Cannot be negative or more than 255.
330   // Should be called before Bind().
331   int SetMulticastTimeToLive(int time_to_live);
332 
333   // Sets the loopback flag for UDP socket. If this flag is true, the host
334   // will receive packets sent to the joined group from itself.
335   // The default value of this option is true.
336   // Should be called before Bind().
337   //
338   // Note: the behavior of |SetMulticastLoopbackMode| is slightly
339   // different between Windows and Unix-like systems. The inconsistency only
340   // happens when there are more than one applications on the same host
341   // joined to the same multicast group while having different settings on
342   // multicast loopback mode. On Windows, the applications with loopback off
343   // will not RECEIVE the loopback packets; while on Unix-like systems, the
344   // applications with loopback off will not SEND the loopback packets to
345   // other applications on the same host. See MSDN: http://goo.gl/6vqbj
346   int SetMulticastLoopbackMode(bool loopback);
347 
348   // Sets the differentiated services flags on outgoing packets. May not do
349   // anything on some platforms. A return value of ERR_INVALID_HANDLE indicates
350   // the value was not set but could succeed on a future call, because
351   // initialization is in progress.
352   int SetDiffServCodePoint(DiffServCodePoint dscp);
353 
354   // Requests that packets sent by this socket have the DSCP and/or ECN
355   // bits set. Returns a network error code if there was a problem. If
356   // DSCP_NO_CHANGE or ECN_NO_CHANGE are set, will preserve those parts of
357   // the original setting.
358   // ECN values other than 0 must not be used outside of tests, without
359   // appropriate congestion control.
360   int SetTos(DiffServCodePoint dscp, EcnCodePoint ecn);
361 
362   // Sets IPV6_V6ONLY on the socket. If this flag is true, the socket will be
363   // restricted to only IPv6; false allows both IPv4 and IPv6 traffic.
364   int SetIPv6Only(bool ipv6_only);
365 
366   // Resets the thread to be used for thread-safety checks.
367   void DetachFromThread();
368 
369   // This class by default uses overlapped IO. Call this method before Open() or
370   // AdoptOpenedSocket() to switch to non-blocking IO.
371   void UseNonBlockingIO();
372 
373   // Apply |tag| to this socket.
374   void ApplySocketTag(const SocketTag& tag);
375 
376   // Takes ownership of `socket`, which should be a socket descriptor opened
377   // with the specified address family. The socket should only be created but
378   // not bound or connected to an address. This method must be called after
379   // UseNonBlockingIO, otherwise the adopted socket will not have the
380   // non-blocking IO flag set.
381   int AdoptOpenedSocket(AddressFamily address_family, SOCKET socket);
382 
get_multicast_interface_for_testing()383   uint32_t get_multicast_interface_for_testing() {
384     return multicast_interface_;
385   }
get_use_non_blocking_io_for_testing()386   bool get_use_non_blocking_io_for_testing() { return use_non_blocking_io_; }
387 
388   // Because the windows API separates out DSCP and ECN better than Posix, this
389   // function does not actually return the correct DSCP value, instead always
390   // returning DSCP_DEFAULT rather than the last incoming value.
391   // If a use case arises for reading the incoming DSCP value, it would only
392   // then worth be executing the system call.
393   // However, the ECN member of the return value is correct if SetRecvTos()
394   // was called previously on the socket.
GetLastTos()395   DscpAndEcn GetLastTos() const { return last_tos_; }
396 
397  private:
398   enum SocketOptions {
399     SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
400   };
401 
402   class Core;
403 
404   void DoReadCallback(int rv);
405   void DoWriteCallback(int rv);
406 
407   void DidCompleteRead();
408   void DidCompleteWrite();
409 
410   // base::ObjectWatcher::Delegate implementation.
411   void OnObjectSignaled(HANDLE object) override;
412   void OnReadSignaled();
413   void OnWriteSignaled();
414 
415   void WatchForReadWrite();
416 
417   // Handles stats and logging. |result| is the number of bytes transferred, on
418   // success, or the net error code on failure.
419   void LogRead(int result, const char* bytes, const IPEndPoint* address) const;
420   void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
421 
422   // Same as SendTo(), except that address is passed by pointer
423   // instead of by reference. It is called from Write() with |address|
424   // set to NULL.
425   int SendToOrWrite(IOBuffer* buf,
426                     int buf_len,
427                     const IPEndPoint* address,
428                     CompletionOnceCallback callback);
429 
430   int InternalConnect(const IPEndPoint& address);
431 
432   // Returns a function pointer to the platform's instantiation of WSARecvMsg()
433   // or WSASendMsg().
434   LPFN_WSARECVMSG GetRecvMsgPointer();
435   LPFN_WSASENDMSG GetSendMsgPointer();
436 
437   // Populates |message| with |storage|, |data_buffer|, and |control_buffer| to
438   // use ECN before calls to either WSASendMsg() (if |send| is true) or
439   // WSARecvMsg().
440   // |data_buffer| is the datagram. |control_buffer| is the storage
441   // space for cmsgs. If |send| is false for an overlapped socket, the caller
442   // must retain a reference to |msghdr|, |storage|, and the buf members of
443   // |data_buffer| and |control_buffer|, in case WSARecvMsg() returns IO_PENDING
444   // and the result is delivered asynchronously.
445   void PopulateWSAMSG(WSAMSG& message,
446                       SockaddrStorage& storage,
447                       WSABUF* data_buffer,
448                       WSABUF& control_buffer,
449                       bool send);
450   // Sets last_tos_ to the last ECN codepoint contained in |message|.
451   void SetLastTosFromWSAMSG(WSAMSG& message);
452 
453   // Version for using overlapped IO.
454   int InternalRecvFromOverlapped(IOBuffer* buf,
455                                  int buf_len,
456                                  IPEndPoint* address);
457   int InternalSendToOverlapped(IOBuffer* buf,
458                                int buf_len,
459                                const IPEndPoint* address);
460 
461   // Version for using non-blocking IO.
462   int InternalRecvFromNonBlocking(IOBuffer* buf,
463                                   int buf_len,
464                                   IPEndPoint* address);
465   int InternalSendToNonBlocking(IOBuffer* buf,
466                                 int buf_len,
467                                 const IPEndPoint* address);
468 
469   // Applies |socket_options_| to |socket_|. Should be called before
470   // Bind().
471   int SetMulticastOptions();
472   int DoBind(const IPEndPoint& address);
473 
474   // Configures opened `socket_` depending on whether it uses nonblocking IO.
475   void ConfigureOpenedSocket();
476 
477   // This is provided to allow QwaveApi mocking in tests. |UDPSocketWin| method
478   // implementations should call |GetQwaveApi()| instead of
479   // |QwaveApi::GetDefault()| directly.
480   virtual QwaveApi* GetQwaveApi() const;
481 
482   SOCKET socket_;
483   int addr_family_ = 0;
484   bool is_connected_ = false;
485 
486   // Bitwise-or'd combination of SocketOptions. Specifies the set of
487   // options that should be applied to |socket_| before Bind().
488   int socket_options_;
489 
490   // Multicast interface.
491   uint32_t multicast_interface_ = 0;
492 
493   // Multicast socket options cached for SetMulticastOption.
494   // Cannot be used after Bind().
495   int multicast_time_to_live_ = 1;
496 
497   // These are mutable since they're just cached copies to make
498   // GetPeerAddress/GetLocalAddress smarter.
499   mutable std::unique_ptr<IPEndPoint> local_address_;
500   mutable std::unique_ptr<IPEndPoint> remote_address_;
501 
502   // The core of the socket that can live longer than the socket itself. We pass
503   // resources to the Windows async IO functions and we have to make sure that
504   // they are not destroyed while the OS still references them.
505   scoped_refptr<Core> core_;
506 
507   // True if non-blocking IO is used.
508   bool use_non_blocking_io_ = false;
509 
510   // Watches |read_write_event_|.
511   base::win::ObjectWatcher read_write_watcher_;
512 
513   // Events for read and write.
514   base::win::ScopedHandle read_write_event_;
515 
516   // The buffers used in Read() and Write().
517   scoped_refptr<IOBuffer> read_iobuffer_;
518   scoped_refptr<IOBuffer> write_iobuffer_;
519 
520   int read_iobuffer_len_ = 0;
521   int write_iobuffer_len_ = 0;
522 
523   raw_ptr<IPEndPoint> recv_from_address_ = nullptr;
524 
525   // Cached copy of the current address we're sending to, if any.  Used for
526   // logging.
527   std::unique_ptr<IPEndPoint> send_to_address_;
528 
529   // External callback; called when read is complete.
530   CompletionOnceCallback read_callback_;
531 
532   // External callback; called when write is complete.
533   CompletionOnceCallback write_callback_;
534 
535   NetLogWithSource net_log_;
536 
537   // Maintains remote addresses for QWAVE qos management.
538   std::unique_ptr<DscpManager> dscp_manager_;
539 
540   // Manages decrementing the global open UDP socket counter when this
541   // UDPSocket is destroyed.
542   OwnedUDPSocketCount owned_socket_count_;
543 
544   DscpAndEcn last_tos_ = {DSCP_DEFAULT, ECN_DEFAULT};
545 
546   // If true, the socket has been configured to report ECN on incoming
547   // datagrams.
548   bool report_ecn_ = false;
549 
550   // Function pointers to the platform implementations of WSARecvMsg() and
551   // WSASendMsg().
552   LPFN_WSARECVMSG wsa_recv_msg_ = nullptr;
553   LPFN_WSASENDMSG wsa_send_msg_ = nullptr;
554 
555   // The ECN codepoint to send on outgoing packets.
556   EcnCodePoint send_ecn_ = ECN_NOT_ECT;
557 
558   THREAD_CHECKER(thread_checker_);
559 
560   // Used to prevent null dereferences in OnObjectSignaled, when passing an
561   // error to both read and write callbacks. Cleared in Close()
562   base::WeakPtrFactory<UDPSocketWin> event_pending_{this};
563 };
564 
565 //-----------------------------------------------------------------------------
566 
567 
568 
569 }  // namespace net
570 
571 #endif  // NET_SOCKET_UDP_SOCKET_WIN_H_
572