xref: /aosp_15_r20/external/cronet/net/socket/udp_socket_posix.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_POSIX_H_
6 #define NET_SOCKET_UDP_SOCKET_POSIX_H_
7 
8 #include <stdint.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 
12 #include <memory>
13 
14 #include "base/logging.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/memory/scoped_refptr.h"
17 #include "base/message_loop/message_pump_for_io.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/timer/timer.h"
20 #include "net/base/address_family.h"
21 #include "net/base/completion_once_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_handle.h"
26 #include "net/log/net_log_with_source.h"
27 #include "net/socket/datagram_socket.h"
28 #include "net/socket/diff_serv_code_point.h"
29 #include "net/socket/socket_descriptor.h"
30 #include "net/socket/socket_tag.h"
31 #include "net/socket/udp_socket_global_limits.h"
32 #include "net/traffic_annotation/network_traffic_annotation.h"
33 
34 namespace net {
35 
36 class IPAddress;
37 class NetLog;
38 struct NetLogSource;
39 class SocketTag;
40 
41 class NET_EXPORT UDPSocketPosix {
42  public:
43   // Performance helper for net::activity_monitor, it batches
44   // throughput samples, subject to a byte limit threshold (64 KB) or
45   // timer (100 ms), whichever comes first.  The batching is subject
46   // to a minimum number of samples (2) required by NQE to update its
47   // throughput estimate.
48   class ReceivedActivityMonitor {
49    public:
50     ReceivedActivityMonitor() = default;
51 
52     ReceivedActivityMonitor(const ReceivedActivityMonitor&) = delete;
53     ReceivedActivityMonitor& operator=(const ReceivedActivityMonitor&) = delete;
54 
55     ~ReceivedActivityMonitor() = default;
56     // Provided by sent/received subclass.
57     // Update throughput, but batch to limit overhead of net::activity_monitor.
58     void Increment(uint32_t bytes);
59     // For flushing cached values.
60     void OnClose();
61 
62    private:
63     void Update();
64     void OnTimerFired();
65 
66     uint32_t bytes_ = 0;
67     uint32_t increments_ = 0;
68     base::RepeatingTimer timer_;
69   };
70 
71   UDPSocketPosix(DatagramSocket::BindType bind_type,
72                  net::NetLog* net_log,
73                  const net::NetLogSource& source);
74 
75   UDPSocketPosix(DatagramSocket::BindType bind_type,
76                  NetLogWithSource source_net_log);
77 
78   UDPSocketPosix(const UDPSocketPosix&) = delete;
79   UDPSocketPosix& operator=(const UDPSocketPosix&) = delete;
80 
81   virtual ~UDPSocketPosix();
82 
83   // Opens the socket.
84   // Returns a net error code.
85   int Open(AddressFamily address_family);
86 
87   // Binds this socket to |network|. All data traffic on the socket will be sent
88   // and received via |network|. Must be called before Connect(). This call will
89   // fail if |network| has disconnected. Communication using this socket will
90   // fail if |network| disconnects.
91   // Returns a net error code.
92   int BindToNetwork(handles::NetworkHandle network);
93 
94   // Connects the socket to connect with a certain |address|.
95   // Should be called after Open().
96   // Returns a net error code.
97   int Connect(const IPEndPoint& address);
98 
99   // Binds the address/port for this socket to |address|.  This is generally
100   // only used on a server. Should be called after Open().
101   // Returns a net error code.
102   int Bind(const IPEndPoint& address);
103 
104   // Closes the socket.
105   void Close();
106 
107   // Copies the remote udp address into |address| and returns a net error code.
108   int GetPeerAddress(IPEndPoint* address) const;
109 
110   // Copies the local udp address into |address| and returns a net error code.
111   // (similar to getsockname)
112   int GetLocalAddress(IPEndPoint* address) const;
113 
114   // IO:
115   // Multiple outstanding read requests are not supported.
116   // Full duplex mode (reading and writing at the same time) is supported
117 
118   // Reads from the socket.
119   // Only usable from the client-side of a UDP socket, after the socket
120   // has been connected.
121   int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
122 
123   // Writes to the socket.
124   // Only usable from the client-side of a UDP socket, after the socket
125   // has been connected.
126   int Write(IOBuffer* buf,
127             int buf_len,
128             CompletionOnceCallback callback,
129             const NetworkTrafficAnnotationTag& traffic_annotation);
130 
131   // Reads from a socket and receive sender address information.
132   // |buf| is the buffer to read data into.
133   // |buf_len| is the maximum amount of data to read.
134   // |address| is a buffer provided by the caller for receiving the sender
135   //   address information about the received data.  This buffer must be kept
136   //   alive by the caller until the callback is placed.
137   // |callback| is the callback on completion of the RecvFrom.
138   // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
139   // If ERR_IO_PENDING is returned, this socket takes a ref to |buf| to keep
140   // it alive until the data is received. However, the caller must keep
141   // |address| alive until the callback is called.
142   int RecvFrom(IOBuffer* buf,
143                int buf_len,
144                IPEndPoint* address,
145                CompletionOnceCallback callback);
146 
147   // Sends to a socket with a particular destination.
148   // |buf| is the buffer to send.
149   // |buf_len| is the number of bytes to send.
150   // |address| is the recipient address.
151   // |callback| is the user callback function to call on complete.
152   // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
153   // If ERR_IO_PENDING is returned, this socket copies |address| for
154   // asynchronous sending, and takes a ref to |buf| to keep it alive until the
155   // data is sent.
156   int SendTo(IOBuffer* buf,
157              int buf_len,
158              const IPEndPoint& address,
159              CompletionOnceCallback callback);
160 
161   // Sets the receive buffer size (in bytes) for the socket.
162   // Returns a net error code.
163   int SetReceiveBufferSize(int32_t size);
164 
165   // Sets the send buffer size (in bytes) for the socket.
166   // Returns a net error code.
167   int SetSendBufferSize(int32_t size);
168 
169   // Requests that packets sent by this socket not be fragment, either locally
170   // by the host, or by routers (via the DF bit in the IPv4 packet header).
171   // May not be supported by all platforms. Returns a network error code if
172   // there was a problem, but the socket will still be usable. Can not
173   // return ERR_IO_PENDING.
174   int SetDoNotFragment();
175 
176   // Requests that packets received by this socket have the ECN bit set. Returns
177   // a network error code if there was a problem.
178   int SetRecvTos();
179 
180   // If |confirm| is true, then the MSG_CONFIRM flag will be passed to
181   // subsequent writes if it's supported by the platform.
182   void SetMsgConfirm(bool confirm);
183 
184   // Returns true if the socket is already connected or bound.
is_connected()185   bool is_connected() const { return is_connected_; }
186 
NetLog()187   const NetLogWithSource& NetLog() const { return net_log_; }
188 
189   // Call this to enable SO_REUSEADDR on the underlying socket.
190   // Should be called between Open() and Bind().
191   // Returns a net error code.
192   int AllowAddressReuse();
193 
194   // Call this to allow or disallow sending and receiving packets to and from
195   // broadcast addresses.
196   // Returns a net error code.
197   int SetBroadcast(bool broadcast);
198 
199   // Sets socket options to allow the socket to share the local address to which
200   // the socket will be bound with other processes and attempt to allow all such
201   // sockets to receive the same multicast messages. Returns a net error code.
202   //
203   // Ability and requirements for different sockets to receive the same messages
204   // varies between POSIX platforms.  For best results in allowing the messages
205   // to be shared, all sockets sharing the same address should join the same
206   // multicast group and interface. Also, the socket should listen to the
207   // specific multicast address rather than a wildcard address (e.g. 0.0.0.0).
208   //
209   // Should be called between Open() and Bind().
210   int AllowAddressSharingForMulticast();
211 
212   // Joins the multicast group.
213   // |group_address| is the group address to join, could be either
214   // an IPv4 or IPv6 address.
215   // Returns a net error code.
216   int JoinGroup(const IPAddress& group_address) const;
217 
218   // Leaves the multicast group.
219   // |group_address| is the group address to leave, could be either
220   // an IPv4 or IPv6 address. If the socket hasn't joined the group,
221   // it will be ignored.
222   // It's optional to leave the multicast group before destroying
223   // the socket. It will be done by the OS.
224   // Returns a net error code.
225   int LeaveGroup(const IPAddress& group_address) const;
226 
227   // Sets interface to use for multicast. If |interface_index| set to 0,
228   // default interface is used.
229   // Should be called before Bind().
230   // Returns a net error code.
231   int SetMulticastInterface(uint32_t interface_index);
232 
233   // Sets the time-to-live option for UDP packets sent to the multicast
234   // group address. The default value of this option is 1.
235   // Cannot be negative or more than 255.
236   // Should be called before Bind().
237   // Returns a net error code.
238   int SetMulticastTimeToLive(int time_to_live);
239 
240   // Sets the loopback flag for UDP socket. If this flag is true, the host
241   // will receive packets sent to the joined group from itself.
242   // The default value of this option is true.
243   // Should be called before Bind().
244   // Returns a net error code.
245   //
246   // Note: the behavior of |SetMulticastLoopbackMode| is slightly
247   // different between Windows and Unix-like systems. The inconsistency only
248   // happens when there are more than one applications on the same host
249   // joined to the same multicast group while having different settings on
250   // multicast loopback mode. On Windows, the applications with loopback off
251   // will not RECEIVE the loopback packets; while on Unix-like systems, the
252   // applications with loopback off will not SEND the loopback packets to
253   // other applications on the same host. See MSDN: http://goo.gl/6vqbj
254   int SetMulticastLoopbackMode(bool loopback);
255 
256   // Sets the differentiated services flags on outgoing packets. May not
257   // do anything on some platforms.
258   // Returns a net error code.
259   int SetDiffServCodePoint(DiffServCodePoint dscp);
260 
261   // Requests that packets sent by this socket have the DSCP and/or ECN
262   // bits set. Returns a network error code if there was a problem. If
263   // DSCP_NO_CHANGE or ECN_NO_CHANGE are set, will preserve those parts of
264   // the original setting.
265   // ECN values other than ECN_DEFAULT must not be used outside of tests,
266   // without appropriate congestion control.
267   int SetTos(DiffServCodePoint dscp, EcnCodePoint ecn);
268 
269   // Sets IPV6_V6ONLY on the socket. If this flag is true, the socket will be
270   // restricted to only IPv6; false allows both IPv4 and IPv6 traffic.
271   int SetIPv6Only(bool ipv6_only);
272 
273   // Exposes the underlying socket descriptor for testing its state. Does not
274   // release ownership of the descriptor.
SocketDescriptorForTesting()275   SocketDescriptor SocketDescriptorForTesting() const { return socket_; }
276 
277   // Resets the thread to be used for thread-safety checks.
278   void DetachFromThread();
279 
280   // Apply |tag| to this socket.
281   void ApplySocketTag(const SocketTag& tag);
282 
283   // Enables experimental optimization. This method should be called
284   // before the socket is used to read data for the first time.
enable_experimental_recv_optimization()285   void enable_experimental_recv_optimization() {
286     DCHECK_EQ(kInvalidSocket, socket_);
287     experimental_recv_optimization_enabled_ = true;
288   }
289 
290   // Sets iOS Network Service Type for option SO_NET_SERVICE_TYPE.
291   int SetIOSNetworkServiceType(int ios_network_service_type);
292 
293   // Takes ownership of `socket`, which should be a socket descriptor opened
294   // with the specified address family. The socket should only be created but
295   // not bound or connected to an address.
296   int AdoptOpenedSocket(AddressFamily address_family, int socket);
297 
get_multicast_interface_for_testing()298   uint32_t get_multicast_interface_for_testing() {
299     return multicast_interface_;
300   }
get_msg_confirm_for_testing()301   bool get_msg_confirm_for_testing() { return sendto_flags_; }
get_experimental_recv_optimization_enabled_for_testing()302   bool get_experimental_recv_optimization_enabled_for_testing() {
303     return experimental_recv_optimization_enabled_;
304   }
305 
GetLastTos()306   DscpAndEcn GetLastTos() const { return TosToDscpAndEcn(last_tos_); }
307 
308  private:
309   enum SocketOptions {
310     SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
311   };
312 
313   class ReadWatcher : public base::MessagePumpForIO::FdWatcher {
314    public:
ReadWatcher(UDPSocketPosix * socket)315     explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {}
316 
317     ReadWatcher(const ReadWatcher&) = delete;
318     ReadWatcher& operator=(const ReadWatcher&) = delete;
319 
320     // MessagePumpForIO::FdWatcher methods
321 
322     void OnFileCanReadWithoutBlocking(int /* fd */) override;
323 
OnFileCanWriteWithoutBlocking(int)324     void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
325 
326    private:
327     const raw_ptr<UDPSocketPosix> socket_;
328   };
329 
330   class WriteWatcher : public base::MessagePumpForIO::FdWatcher {
331    public:
WriteWatcher(UDPSocketPosix * socket)332     explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {}
333 
334     WriteWatcher(const WriteWatcher&) = delete;
335     WriteWatcher& operator=(const WriteWatcher&) = delete;
336 
337     // MessagePumpForIO::FdWatcher methods
338 
OnFileCanReadWithoutBlocking(int)339     void OnFileCanReadWithoutBlocking(int /* fd */) override {}
340 
341     void OnFileCanWriteWithoutBlocking(int /* fd */) override;
342 
343    private:
344     const raw_ptr<UDPSocketPosix> socket_;
345   };
346 
347   void DoReadCallback(int rv);
348   void DoWriteCallback(int rv);
349   void DidCompleteRead();
350   void DidCompleteWrite();
351 
352   // Handles stats and logging. |result| is the number of bytes transferred, on
353   // success, or the net error code on failure. On success, LogRead takes in a
354   // sockaddr and its length, which are mandatory, while LogWrite takes in an
355   // optional IPEndPoint.
356   void LogRead(int result,
357                const char* bytes,
358                socklen_t addr_len,
359                const sockaddr* addr);
360   void LogWrite(int result, const char* bytes, const IPEndPoint* address);
361 
362   // Same as SendTo(), except that address is passed by pointer
363   // instead of by reference. It is called from Write() with |address|
364   // set to nullptr.
365   int SendToOrWrite(IOBuffer* buf,
366                     int buf_len,
367                     const IPEndPoint* address,
368                     CompletionOnceCallback callback);
369 
370   int InternalConnect(const IPEndPoint& address);
371 
372   // Reads data from a UDP socket. Depending whether the socket is connected or
373   // not, the method delegates the call to InternalRecvFromConnectedSocket()
374   // or InternalRecvFromNonConnectedSocket() respectively.
375   // For proper detection of truncated reads, the |buf_len| should always be
376   // one byte longer than the expected maximum packet length.
377   int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
378 
379   // A more efficient implementation of the InternalRecvFrom() method for
380   // reading data from connected sockets. Internally the method uses the read()
381   // system call.
382   int InternalRecvFromConnectedSocket(IOBuffer* buf,
383                                       int buf_len,
384                                       IPEndPoint* address);
385 
386   // An implementation of the InternalRecvFrom() method for reading data
387   // from non-connected sockets. Internally the method uses the recvmsg()
388   // system call.
389   int InternalRecvFromNonConnectedSocket(IOBuffer* buf,
390                                          int buf_len,
391                                          IPEndPoint* address);
392   int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
393 
394   // Applies |socket_options_| to |socket_|. Should be called before
395   // Bind().
396   int SetMulticastOptions();
397   int DoBind(const IPEndPoint& address);
398   // Binds to a random port on |address|.
399   int RandomBind(const IPAddress& address);
400 
401   // Sets `socket_hash_` and `tag_` on opened `socket_`.
402   int ConfigureOpenedSocket();
403 
404   int socket_;
405 
406   // Hash of |socket_| to verify that it is not corrupted when calling close().
407   // Used to debug https://crbug.com/906005.
408   // TODO(crbug.com/906005): Remove this once the bug is fixed.
409   int socket_hash_ = 0;
410 
411   int addr_family_ = 0;
412   bool is_connected_ = false;
413 
414   // Bitwise-or'd combination of SocketOptions. Specifies the set of
415   // options that should be applied to |socket_| before Bind().
416   int socket_options_ = SOCKET_OPTION_MULTICAST_LOOP;
417 
418   // Flags passed to sendto().
419   int sendto_flags_ = 0;
420 
421   // Multicast interface.
422   uint32_t multicast_interface_ = 0;
423 
424   // Multicast socket options cached for SetMulticastOption.
425   // Cannot be used after Bind().
426   int multicast_time_to_live_ = 1;
427 
428   // How to do source port binding, used only when UDPSocket is part of
429   // UDPClientSocket, since UDPServerSocket provides Bind.
430   DatagramSocket::BindType bind_type_;
431 
432   // These are mutable since they're just cached copies to make
433   // GetPeerAddress/GetLocalAddress smarter.
434   mutable std::unique_ptr<IPEndPoint> local_address_;
435   mutable std::unique_ptr<IPEndPoint> remote_address_;
436 
437   // The socket's posix wrappers
438   base::MessagePumpForIO::FdWatchController read_socket_watcher_;
439   base::MessagePumpForIO::FdWatchController write_socket_watcher_;
440 
441   // The corresponding watchers for reads and writes.
442   ReadWatcher read_watcher_;
443   WriteWatcher write_watcher_;
444 
445   // The buffer used by InternalRead() to retry Read requests
446   scoped_refptr<IOBuffer> read_buf_;
447   int read_buf_len_ = 0;
448   raw_ptr<IPEndPoint> recv_from_address_ = nullptr;
449 
450   // The buffer used by InternalWrite() to retry Write requests
451   scoped_refptr<IOBuffer> write_buf_;
452   int write_buf_len_ = 0;
453   std::unique_ptr<IPEndPoint> send_to_address_;
454 
455   // External callback; called when read is complete.
456   CompletionOnceCallback read_callback_;
457 
458   // External callback; called when write is complete.
459   CompletionOnceCallback write_callback_;
460 
461   NetLogWithSource net_log_;
462 
463   // Network that this socket is bound to via BindToNetwork().
464   handles::NetworkHandle bound_network_;
465 
466   // Whether net::activity_monitor should be updated every time bytes are
467   // received, without batching through |received_activity_monitor_|. This is
468   // initialized with the state of the "UdpSocketPosixAlwaysUpdateBytesReceived"
469   // feature. It is cached to avoid accessing the FeatureList every time bytes
470   // are received.
471   const bool always_update_bytes_received_;
472 
473   // Used to lower the overhead updating activity monitor.
474   ReceivedActivityMonitor received_activity_monitor_;
475 
476   // Current socket tag if |socket_| is valid, otherwise the tag to apply when
477   // |socket_| is opened.
478   SocketTag tag_;
479 
480   // If set to true, the socket will use an optimized experimental code path.
481   // By default, the value is set to false. To use the optimization, the
482   // client of the socket has to opt-in by calling the
483   // enable_experimental_recv_optimization() method.
484   bool experimental_recv_optimization_enabled_ = false;
485 
486   // Manages decrementing the global open UDP socket counter when this
487   // UDPSocket is destroyed.
488   OwnedUDPSocketCount owned_socket_count_;
489 
490   // The last TOS byte received on the socket.
491   uint8_t last_tos_ = 0;
492 
493   THREAD_CHECKER(thread_checker_);
494 };
495 
496 }  // namespace net
497 
498 #endif  // NET_SOCKET_UDP_SOCKET_POSIX_H_
499