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 SetRecvEcn(); 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 // Sets IPV6_V6ONLY on the socket. If this flag is true, the socket will be 262 // restricted to only IPv6; false allows both IPv4 and IPv6 traffic. 263 int SetIPv6Only(bool ipv6_only); 264 265 // Exposes the underlying socket descriptor for testing its state. Does not 266 // release ownership of the descriptor. SocketDescriptorForTesting()267 SocketDescriptor SocketDescriptorForTesting() const { return socket_; } 268 269 // Resets the thread to be used for thread-safety checks. 270 void DetachFromThread(); 271 272 // Apply |tag| to this socket. 273 void ApplySocketTag(const SocketTag& tag); 274 275 // Enables experimental optimization. This method should be called 276 // before the socket is used to read data for the first time. enable_experimental_recv_optimization()277 void enable_experimental_recv_optimization() { 278 DCHECK_EQ(kInvalidSocket, socket_); 279 experimental_recv_optimization_enabled_ = true; 280 } 281 282 // Sets iOS Network Service Type for option SO_NET_SERVICE_TYPE. 283 int SetIOSNetworkServiceType(int ios_network_service_type); 284 285 // Takes ownership of `socket`, which should be a socket descriptor opened 286 // with the specified address family. The socket should only be created but 287 // not bound or connected to an address. 288 int AdoptOpenedSocket(AddressFamily address_family, int socket); 289 get_multicast_interface_for_testing()290 uint32_t get_multicast_interface_for_testing() { 291 return multicast_interface_; 292 } get_msg_confirm_for_testing()293 bool get_msg_confirm_for_testing() { return sendto_flags_; } get_experimental_recv_optimization_enabled_for_testing()294 bool get_experimental_recv_optimization_enabled_for_testing() { 295 return experimental_recv_optimization_enabled_; 296 } 297 298 private: 299 enum SocketOptions { 300 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 301 }; 302 303 class ReadWatcher : public base::MessagePumpForIO::FdWatcher { 304 public: ReadWatcher(UDPSocketPosix * socket)305 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} 306 307 ReadWatcher(const ReadWatcher&) = delete; 308 ReadWatcher& operator=(const ReadWatcher&) = delete; 309 310 // MessagePumpForIO::FdWatcher methods 311 312 void OnFileCanReadWithoutBlocking(int /* fd */) override; 313 OnFileCanWriteWithoutBlocking(int)314 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} 315 316 private: 317 const raw_ptr<UDPSocketPosix> socket_; 318 }; 319 320 class WriteWatcher : public base::MessagePumpForIO::FdWatcher { 321 public: WriteWatcher(UDPSocketPosix * socket)322 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} 323 324 WriteWatcher(const WriteWatcher&) = delete; 325 WriteWatcher& operator=(const WriteWatcher&) = delete; 326 327 // MessagePumpForIO::FdWatcher methods 328 OnFileCanReadWithoutBlocking(int)329 void OnFileCanReadWithoutBlocking(int /* fd */) override {} 330 331 void OnFileCanWriteWithoutBlocking(int /* fd */) override; 332 333 private: 334 const raw_ptr<UDPSocketPosix> socket_; 335 }; 336 337 void DoReadCallback(int rv); 338 void DoWriteCallback(int rv); 339 void DidCompleteRead(); 340 void DidCompleteWrite(); 341 342 // Handles stats and logging. |result| is the number of bytes transferred, on 343 // success, or the net error code on failure. On success, LogRead takes in a 344 // sockaddr and its length, which are mandatory, while LogWrite takes in an 345 // optional IPEndPoint. 346 void LogRead(int result, 347 const char* bytes, 348 socklen_t addr_len, 349 const sockaddr* addr); 350 void LogWrite(int result, const char* bytes, const IPEndPoint* address); 351 352 // Same as SendTo(), except that address is passed by pointer 353 // instead of by reference. It is called from Write() with |address| 354 // set to nullptr. 355 int SendToOrWrite(IOBuffer* buf, 356 int buf_len, 357 const IPEndPoint* address, 358 CompletionOnceCallback callback); 359 360 int InternalConnect(const IPEndPoint& address); 361 362 // Reads data from a UDP socket. Depending whether the socket is connected or 363 // not, the method delegates the call to InternalRecvFromConnectedSocket() 364 // or InternalRecvFromNonConnectedSocket() respectively. 365 // For proper detection of truncated reads, the |buf_len| should always be 366 // one byte longer than the expected maximum packet length. 367 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); 368 369 // A more efficient implementation of the InternalRecvFrom() method for 370 // reading data from connected sockets. Internally the method uses the read() 371 // system call. 372 int InternalRecvFromConnectedSocket(IOBuffer* buf, 373 int buf_len, 374 IPEndPoint* address); 375 376 // An implementation of the InternalRecvFrom() method for reading data 377 // from non-connected sockets. Internally the method uses the recvmsg() 378 // system call. 379 int InternalRecvFromNonConnectedSocket(IOBuffer* buf, 380 int buf_len, 381 IPEndPoint* address); 382 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); 383 384 // Applies |socket_options_| to |socket_|. Should be called before 385 // Bind(). 386 int SetMulticastOptions(); 387 int DoBind(const IPEndPoint& address); 388 // Binds to a random port on |address|. 389 int RandomBind(const IPAddress& address); 390 391 // Sets `socket_hash_` and `tag_` on opened `socket_`. 392 int ConfigureOpenedSocket(); 393 394 int socket_; 395 396 // Hash of |socket_| to verify that it is not corrupted when calling close(). 397 // Used to debug https://crbug.com/906005. 398 // TODO(crbug.com/906005): Remove this once the bug is fixed. 399 int socket_hash_ = 0; 400 401 int addr_family_ = 0; 402 bool is_connected_ = false; 403 404 // Bitwise-or'd combination of SocketOptions. Specifies the set of 405 // options that should be applied to |socket_| before Bind(). 406 int socket_options_ = SOCKET_OPTION_MULTICAST_LOOP; 407 408 // Flags passed to sendto(). 409 int sendto_flags_ = 0; 410 411 // Multicast interface. 412 uint32_t multicast_interface_ = 0; 413 414 // Multicast socket options cached for SetMulticastOption. 415 // Cannot be used after Bind(). 416 int multicast_time_to_live_ = 1; 417 418 // How to do source port binding, used only when UDPSocket is part of 419 // UDPClientSocket, since UDPServerSocket provides Bind. 420 DatagramSocket::BindType bind_type_; 421 422 // These are mutable since they're just cached copies to make 423 // GetPeerAddress/GetLocalAddress smarter. 424 mutable std::unique_ptr<IPEndPoint> local_address_; 425 mutable std::unique_ptr<IPEndPoint> remote_address_; 426 427 // The socket's posix wrappers 428 base::MessagePumpForIO::FdWatchController read_socket_watcher_; 429 base::MessagePumpForIO::FdWatchController write_socket_watcher_; 430 431 // The corresponding watchers for reads and writes. 432 ReadWatcher read_watcher_; 433 WriteWatcher write_watcher_; 434 435 // The buffer used by InternalRead() to retry Read requests 436 scoped_refptr<IOBuffer> read_buf_; 437 int read_buf_len_ = 0; 438 raw_ptr<IPEndPoint> recv_from_address_ = nullptr; 439 440 // The buffer used by InternalWrite() to retry Write requests 441 scoped_refptr<IOBuffer> write_buf_; 442 int write_buf_len_ = 0; 443 std::unique_ptr<IPEndPoint> send_to_address_; 444 445 // External callback; called when read is complete. 446 CompletionOnceCallback read_callback_; 447 448 // External callback; called when write is complete. 449 CompletionOnceCallback write_callback_; 450 451 NetLogWithSource net_log_; 452 453 // Network that this socket is bound to via BindToNetwork(). 454 handles::NetworkHandle bound_network_; 455 456 // Whether net::activity_monitor should be updated every time bytes are 457 // received, without batching through |received_activity_monitor_|. This is 458 // initialized with the state of the "UdpSocketPosixAlwaysUpdateBytesReceived" 459 // feature. It is cached to avoid accessing the FeatureList every time bytes 460 // are received. 461 const bool always_update_bytes_received_; 462 463 // Used to lower the overhead updating activity monitor. 464 ReceivedActivityMonitor received_activity_monitor_; 465 466 // Current socket tag if |socket_| is valid, otherwise the tag to apply when 467 // |socket_| is opened. 468 SocketTag tag_; 469 470 // If set to true, the socket will use an optimized experimental code path. 471 // By default, the value is set to false. To use the optimization, the 472 // client of the socket has to opt-in by calling the 473 // enable_experimental_recv_optimization() method. 474 bool experimental_recv_optimization_enabled_ = false; 475 476 // Manages decrementing the global open UDP socket counter when this 477 // UDPSocket is destroyed. 478 OwnedUDPSocketCount owned_socket_count_; 479 480 THREAD_CHECKER(thread_checker_); 481 }; 482 483 } // namespace net 484 485 #endif // NET_SOCKET_UDP_SOCKET_POSIX_H_ 486