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