udp_socket.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2018 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. #ifndef SERVICES_NETWORK_UDP_SOCKET_H_
  5. #define SERVICES_NETWORK_UDP_SOCKET_H_
  6. #include <deque>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/containers/span.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "mojo/public/cpp/bindings/pending_remote.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "net/base/address_family.h"
  16. #include "net/base/completion_once_callback.h"
  17. #include "net/base/ip_endpoint.h"
  18. #include "net/traffic_annotation/network_traffic_annotation.h"
  19. #include "services/network/public/mojom/address_family.mojom.h"
  20. #include "services/network/public/mojom/ip_endpoint.mojom.h"
  21. #include "services/network/public/mojom/udp_socket.mojom.h"
  22. namespace net {
  23. class IOBuffer;
  24. class IOBufferWithSize;
  25. class NetLog;
  26. } // namespace net
  27. namespace network {
  28. class COMPONENT_EXPORT(NETWORK_SERVICE) UDPSocket : public mojom::UDPSocket {
  29. public:
  30. // Number of Send()/SendTo() requests that are queued internally. Public for
  31. // testing.
  32. static const uint32_t kMaxPendingSendRequests = 32;
  33. // A socket wrapper class that allows tests to substitute the default
  34. // implementation (implemented using net::UDPSocket) with a test
  35. // implementation.
  36. class SocketWrapper {
  37. public:
  38. virtual ~SocketWrapper() {}
  39. // This wrapper class forwards the functions to a concrete udp socket
  40. // implementation. Please refer to udp_socket_posix.h/udp_socket_win.h for
  41. // definitions.
  42. virtual int Connect(const net::IPEndPoint& remote_addr,
  43. mojom::UDPSocketOptionsPtr options,
  44. net::IPEndPoint* local_addr_out) = 0;
  45. virtual int Bind(const net::IPEndPoint& local_addr,
  46. mojom::UDPSocketOptionsPtr options,
  47. net::IPEndPoint* local_addr_out) = 0;
  48. virtual int SendTo(
  49. net::IOBuffer* buf,
  50. int buf_len,
  51. const net::IPEndPoint& dest_addr,
  52. net::CompletionOnceCallback callback,
  53. const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0;
  54. virtual int Write(
  55. net::IOBuffer* buf,
  56. int buf_len,
  57. net::CompletionOnceCallback callback,
  58. const net::NetworkTrafficAnnotationTag& traffic_annotation) = 0;
  59. virtual int SetBroadcast(bool broadcast) = 0;
  60. virtual int SetSendBufferSize(int send_buffer_size) = 0;
  61. virtual int SetReceiveBufferSize(int receive_buffer_size) = 0;
  62. virtual int JoinGroup(const net::IPAddress& group_address) = 0;
  63. virtual int LeaveGroup(const net::IPAddress& group_address) = 0;
  64. virtual int RecvFrom(net::IOBuffer* buf,
  65. int buf_len,
  66. net::IPEndPoint* address,
  67. net::CompletionOnceCallback callback) = 0;
  68. };
  69. UDPSocket(mojo::PendingRemote<mojom::UDPSocketListener> listener,
  70. net::NetLog* net_log);
  71. UDPSocket(const UDPSocket&) = delete;
  72. UDPSocket& operator=(const UDPSocket&) = delete;
  73. ~UDPSocket() override;
  74. // UDPSocket implementation.
  75. void Connect(const net::IPEndPoint& remote_addr,
  76. mojom::UDPSocketOptionsPtr options,
  77. ConnectCallback callback) override;
  78. void Bind(const net::IPEndPoint& local_addr,
  79. mojom::UDPSocketOptionsPtr options,
  80. BindCallback callback) override;
  81. void SetBroadcast(bool broadcast, SetBroadcastCallback callback) override;
  82. void SetSendBufferSize(int32_t send_buffer_size,
  83. SetSendBufferSizeCallback callback) override;
  84. void SetReceiveBufferSize(int32_t receive_buffer_size,
  85. SetSendBufferSizeCallback callback) override;
  86. void JoinGroup(const net::IPAddress& group_address,
  87. JoinGroupCallback callback) override;
  88. void LeaveGroup(const net::IPAddress& group_address,
  89. LeaveGroupCallback callback) override;
  90. void ReceiveMore(uint32_t num_additional_datagrams) override;
  91. void ReceiveMoreWithBufferSize(uint32_t num_additional_datagrams,
  92. uint32_t buffer_size) override;
  93. void SendTo(const net::IPEndPoint& dest_addr,
  94. base::span<const uint8_t> data,
  95. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
  96. SendToCallback callback) override;
  97. void Send(base::span<const uint8_t> data,
  98. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
  99. SendCallback callback) override;
  100. void Close() override;
  101. private:
  102. friend class UDPSocketTest;
  103. // Represents a pending Send()/SendTo() request that is yet to be sent to the
  104. // |socket_|. In the case of Send(), |addr| will be not filled in.
  105. struct PendingSendRequest {
  106. PendingSendRequest();
  107. ~PendingSendRequest();
  108. std::unique_ptr<net::IPEndPoint> addr;
  109. net::MutableNetworkTrafficAnnotationTag traffic_annotation;
  110. scoped_refptr<net::IOBufferWithSize> data;
  111. SendToCallback callback;
  112. };
  113. // Helper method to create a new SocketWrapper.
  114. std::unique_ptr<UDPSocket::SocketWrapper> CreateSocketWrapper() const;
  115. // Returns whether a successful Connect() or Bind() has been executed.
  116. bool IsConnectedOrBound() const;
  117. void DoRecvFrom(uint32_t buffer_size);
  118. void DoSendToOrWrite(
  119. const net::IPEndPoint* dest_addr,
  120. const base::span<const uint8_t>& data,
  121. const net::NetworkTrafficAnnotationTag& traffic_annotation,
  122. SendToCallback callback);
  123. void DoSendToOrWriteBuffer(
  124. const net::IPEndPoint* dest_addr,
  125. scoped_refptr<net::IOBufferWithSize> buffer,
  126. const net::NetworkTrafficAnnotationTag& traffic_annotation,
  127. SendToCallback callback);
  128. void OnRecvFromCompleted(uint32_t buffer_size, int net_result);
  129. void OnSendToCompleted(int net_result);
  130. raw_ptr<net::NetLog> net_log_;
  131. // Whether a Bind() has been successfully executed.
  132. bool is_bound_;
  133. // Whether a Connect() has been successfully executed.
  134. bool is_connected_;
  135. // The interface which gets data from fulfilled receive requests.
  136. mojo::Remote<mojom::UDPSocketListener> listener_;
  137. std::unique_ptr<SocketWrapper> wrapped_socket_;
  138. // Non-null when there is a pending RecvFrom operation on socket.
  139. scoped_refptr<net::IOBuffer> recvfrom_buffer_;
  140. // Non-null when there is a pending Send/SendTo operation on socket.
  141. scoped_refptr<net::IOBufferWithSize> send_buffer_;
  142. SendToCallback send_callback_;
  143. // The address of the sender of a received packet. This address might not be
  144. // filled if an error occurred during the receiving of the packet.
  145. net::IPEndPoint recvfrom_address_;
  146. // How many more packets the client side expects to receive.
  147. uint32_t remaining_recv_slots_;
  148. // The queue owns the PendingSendRequest instances.
  149. base::circular_deque<std::unique_ptr<PendingSendRequest>>
  150. pending_send_requests_;
  151. };
  152. } // namespace network
  153. #endif // SERVICES_NETWORK_UDP_SOCKET_H_