btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 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 | |
Jordan Bayles | ad2d2cd | 2019-05-22 14:49:40 -0700 | [diff] [blame] | 5 | #ifndef PLATFORM_API_UDP_SOCKET_H_ |
| 6 | #define PLATFORM_API_UDP_SOCKET_H_ |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 7 | |
Ryan Keane | 83050af | 2019-08-26 08:58:09 -0700 | [diff] [blame] | 8 | #include <atomic> |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 9 | #include <cstdint> |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 10 | #include <functional> |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 11 | #include <memory> |
Ryan Keane | 9222e04 | 2019-08-27 10:44:13 -0700 | [diff] [blame] | 12 | #include <mutex> |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 13 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 14 | #include "platform/api/network_interface.h" |
Ryan Keane | 230920e | 2019-08-29 13:21:40 -0700 | [diff] [blame] | 15 | #include "platform/api/udp_packet.h" |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 16 | #include "platform/base/error.h" |
| 17 | #include "platform/base/ip_address.h" |
| 18 | #include "platform/base/macros.h" |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 19 | |
| 20 | namespace openscreen { |
| 21 | namespace platform { |
| 22 | |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 23 | class TaskRunner; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 24 | class UdpSocket; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 25 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 26 | using UdpSocketUniquePtr = std::unique_ptr<UdpSocket>; |
Yuri Wiitala | b94f12e | 2018-10-31 18:28:53 -0700 | [diff] [blame] | 27 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 28 | // An open UDP socket for sending/receiving datagrams to/from either specific |
| 29 | // endpoints or over IP multicast. |
| 30 | // |
| 31 | // Usage: The socket is created and opened by calling the Create() method. This |
| 32 | // returns a unique pointer that auto-closes/destroys the socket when it goes |
| 33 | // out-of-scope. |
| 34 | // |
| 35 | // Platform implementation note: There must only be one platform-specific |
| 36 | // implementation of UdpSocket linked into the library/application. For that |
| 37 | // reason, none of the methods here are declared virtual (i.e., the overhead is |
| 38 | // pure waste). However, UdpSocket can be subclassed to include all extra |
| 39 | // private state, such as OS-specific handles. See UdpSocketPosix for a |
| 40 | // reference implementation. |
| 41 | class UdpSocket { |
| 42 | public: |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 43 | virtual ~UdpSocket(); |
| 44 | |
Ryan Keane | 9222e04 | 2019-08-27 10:44:13 -0700 | [diff] [blame] | 45 | class LifetimeObserver { |
| 46 | public: |
| 47 | virtual ~LifetimeObserver() = default; |
| 48 | |
| 49 | // Function to call upon creation of a new UdpSocket. |
| 50 | virtual void OnCreate(UdpSocket* socket) = 0; |
| 51 | |
| 52 | // Function to call upon deletion of a UdpSocket. |
| 53 | virtual void OnDestroy(UdpSocket* socket) = 0; |
| 54 | }; |
| 55 | |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 56 | // Client for the UdpSocket class. |
| 57 | class Client { |
| 58 | public: |
| 59 | virtual ~Client() = default; |
| 60 | |
| 61 | // Method called on socket configuration operations when an error occurs. |
| 62 | // These specific APIs are: |
| 63 | // UdpSocket::Bind() |
| 64 | // UdpSocket::SetMulticastOutboundInterface(...) |
| 65 | // UdpSocket::JoinMulticastGroup(...) |
| 66 | // UdpSocket::SetDscp(...) |
| 67 | virtual void OnError(UdpSocket* socket, Error error) = 0; |
| 68 | |
| 69 | // Method called when an error occurs during a SendMessage call. |
| 70 | virtual void OnSendError(UdpSocket* socket, Error error) = 0; |
| 71 | |
| 72 | // Method called when a packet is read. |
| 73 | virtual void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) = 0; |
| 74 | }; |
| 75 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 76 | // Constants used to specify how we want packets sent from this socket. |
| 77 | enum class DscpMode : uint8_t { |
| 78 | // Default value set by the system on creation of a new socket. |
| 79 | kUnspecified = 0x0, |
| 80 | |
| 81 | // Mode for Audio only. |
| 82 | kAudioOnly = 0xb8, |
| 83 | |
| 84 | // Mode for Audio + Video. |
| 85 | kAudioVideo = 0x88, |
| 86 | |
| 87 | // Mode for low priority operations such as trace log data. |
| 88 | kLowPriority = 0x20 |
| 89 | }; |
| 90 | |
Ryan Keane | 9222e04 | 2019-08-27 10:44:13 -0700 | [diff] [blame] | 91 | // The LifetimeObserver set here must exist during ANY future UdpSocket |
| 92 | // creations. SetLifetimeObserver(nullptr) must be called before any future |
| 93 | // socket creations on destructions after the observer is destroyed |
| 94 | static void SetLifetimeObserver(LifetimeObserver* observer); |
| 95 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 96 | using Version = IPAddress::Version; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 97 | |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 98 | // Creates a new, scoped UdpSocket within the IPv4 or IPv6 family. |
| 99 | // |local_endpoint| may be zero (see comments for Bind()). This method must be |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 100 | // defined in the platform-level implementation. All client_ methods called |
| 101 | // will be queued on the provided task_runner. For this reason, the provided |
| 102 | // task_runner and client must exist for the duration of the created socket's |
| 103 | // lifetime. |
| 104 | static ErrorOr<UdpSocketUniquePtr> Create(TaskRunner* task_runner, |
| 105 | Client* client, |
| 106 | const IPEndpoint& local_endpoint); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 107 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 108 | // Returns true if |socket| belongs to the IPv4/IPv6 address family. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 109 | virtual bool IsIPv4() const = 0; |
| 110 | virtual bool IsIPv6() const = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 111 | |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 112 | // Returns the current local endpoint's address and port. Initially, this will |
| 113 | // be the same as the value that was passed into Create(). However, it can |
| 114 | // later change after certain operations, such as Bind(), are executed. |
| 115 | virtual IPEndpoint GetLocalEndpoint() const = 0; |
| 116 | |
| 117 | // Binds to the address specified in the constructor. If the local endpoint's |
| 118 | // address is zero, the operating system will bind to all interfaces. If the |
| 119 | // local endpoint's port is zero, the operating system will automatically find |
| 120 | // a free local port and bind to it. Future calls to local_endpoint() will |
| 121 | // reflect the resolved port. |
Ryan Keane | 83e6832 | 2019-08-23 14:54:38 -0700 | [diff] [blame] | 122 | virtual void Bind() = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 123 | |
| 124 | // Sets the device to use for outgoing multicast packets on the socket. |
Ryan Keane | 7e74bdd | 2019-08-27 08:15:21 -0700 | [diff] [blame] | 125 | virtual void SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 126 | |
| 127 | // Joins to the multicast group at the given address, using the specified |
| 128 | // interface. |
Ryan Keane | 7e74bdd | 2019-08-27 08:15:21 -0700 | [diff] [blame] | 129 | virtual void JoinMulticastGroup(const IPAddress& address, |
| 130 | NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 131 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 132 | // Sends a message and returns the number of bytes sent, on success. |
| 133 | // Error::Code::kAgain might be returned to indicate the operation would |
| 134 | // block, which can be expected during normal operation. |
Ryan Keane | 71cacc2 | 2019-08-21 13:01:47 -0700 | [diff] [blame] | 135 | virtual void SendMessage(const void* data, |
| 136 | size_t length, |
| 137 | const IPEndpoint& dest) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 138 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 139 | // Sets the DSCP value to use for all messages sent from this socket. |
Ryan Keane | 7e74bdd | 2019-08-27 08:15:21 -0700 | [diff] [blame] | 140 | virtual void SetDscp(DscpMode state) = 0; |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 141 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 142 | protected: |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 143 | // Creates a new UdpSocket. The provided client and task_runner must exist for |
| 144 | // the duration of this socket's lifetime. |
| 145 | UdpSocket(TaskRunner* task_runner, Client* client); |
| 146 | |
Ryan Keane | 63fbedd | 2019-08-19 12:33:41 -0700 | [diff] [blame] | 147 | // Methods to take care of posting UdpSocket::Client callbacks for client_ to |
| 148 | // task_runner_. |
Ryan Keane | 230920e | 2019-08-29 13:21:40 -0700 | [diff] [blame] | 149 | // NOTE: OnError(...) will close the socket in addition to returning the |
| 150 | // error. |
Ryan Keane | 63fbedd | 2019-08-19 12:33:41 -0700 | [diff] [blame] | 151 | void OnError(Error error); |
| 152 | void OnSendError(Error error); |
| 153 | void OnRead(ErrorOr<UdpPacket> read_data); |
| 154 | |
Ryan Keane | 83050af | 2019-08-26 08:58:09 -0700 | [diff] [blame] | 155 | // Closes an open socket when a non-recoverable error occurs. |
| 156 | void CloseIfError(const Error& error); |
| 157 | |
| 158 | // Closes an open socket. |
| 159 | // NOTE: Concrete implementations of UdpSocket must call this method in their |
| 160 | // destructor. |
| 161 | void CloseIfOpen(); |
| 162 | |
| 163 | // Returns whether the socket is currently closed. |
| 164 | // NOTE: This must be checked before calling any operation on the socket. |
| 165 | bool is_closed() { return is_closed_.load(); } |
| 166 | |
Ryan Keane | 63fbedd | 2019-08-19 12:33:41 -0700 | [diff] [blame] | 167 | private: |
Ryan Keane | 9222e04 | 2019-08-27 10:44:13 -0700 | [diff] [blame] | 168 | static std::atomic<LifetimeObserver*> lifetime_observer_; |
| 169 | |
Ryan Keane | 83050af | 2019-08-26 08:58:09 -0700 | [diff] [blame] | 170 | // Closes this socket. |
| 171 | // NOTE: This method will only be called once. |
| 172 | virtual void Close() {} |
| 173 | |
| 174 | // Atomically keeps track of if the socket is closed, so that threading |
| 175 | // across different implementations isn't a problem. |
| 176 | std::atomic_bool is_closed_{false}; |
| 177 | |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 178 | // Client to use for callbacks. |
| 179 | // NOTE: client_ can be nullptr if the user does not want any callbacks (for |
| 180 | // example, in the send-only case). |
| 181 | Client* const client_; |
| 182 | |
| 183 | // Task runner to use for queuing client_ callbacks. |
| 184 | TaskRunner* const task_runner_; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 185 | |
Jordan Bayles | f1e4bb7 | 2019-05-01 12:38:39 -0700 | [diff] [blame] | 186 | OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocket); |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 187 | }; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 188 | |
| 189 | } // namespace platform |
| 190 | } // namespace openscreen |
| 191 | |
Jordan Bayles | ad2d2cd | 2019-05-22 14:49:40 -0700 | [diff] [blame] | 192 | #endif // PLATFORM_API_UDP_SOCKET_H_ |