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. |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 34 | class UdpSocket { |
| 35 | public: |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 36 | // Client for the UdpSocket class. |
| 37 | class Client { |
| 38 | public: |
| 39 | virtual ~Client() = default; |
| 40 | |
| 41 | // Method called on socket configuration operations when an error occurs. |
| 42 | // These specific APIs are: |
| 43 | // UdpSocket::Bind() |
| 44 | // UdpSocket::SetMulticastOutboundInterface(...) |
| 45 | // UdpSocket::JoinMulticastGroup(...) |
| 46 | // UdpSocket::SetDscp(...) |
| 47 | virtual void OnError(UdpSocket* socket, Error error) = 0; |
| 48 | |
| 49 | // Method called when an error occurs during a SendMessage call. |
| 50 | virtual void OnSendError(UdpSocket* socket, Error error) = 0; |
| 51 | |
| 52 | // Method called when a packet is read. |
| 53 | virtual void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) = 0; |
| 54 | }; |
| 55 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 56 | // Constants used to specify how we want packets sent from this socket. |
| 57 | enum class DscpMode : uint8_t { |
| 58 | // Default value set by the system on creation of a new socket. |
| 59 | kUnspecified = 0x0, |
| 60 | |
| 61 | // Mode for Audio only. |
| 62 | kAudioOnly = 0xb8, |
| 63 | |
| 64 | // Mode for Audio + Video. |
| 65 | kAudioVideo = 0x88, |
| 66 | |
| 67 | // Mode for low priority operations such as trace log data. |
| 68 | kLowPriority = 0x20 |
| 69 | }; |
| 70 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 71 | using Version = IPAddress::Version; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 72 | |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 73 | // Creates a new, scoped UdpSocket within the IPv4 or IPv6 family. |
| 74 | // |local_endpoint| may be zero (see comments for Bind()). This method must be |
Yuri Wiitala | d697dfc | 2019-10-23 16:00:16 -0700 | [diff] [blame^] | 75 | // defined in the platform-level implementation. All |client| methods called |
| 76 | // will be queued on the provided |task_runner|. For this reason, the provided |
| 77 | // TaskRunner and Client must exist for the duration of the created socket's |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 78 | // lifetime. |
| 79 | static ErrorOr<UdpSocketUniquePtr> Create(TaskRunner* task_runner, |
| 80 | Client* client, |
| 81 | const IPEndpoint& local_endpoint); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 82 | |
Yuri Wiitala | d697dfc | 2019-10-23 16:00:16 -0700 | [diff] [blame^] | 83 | virtual ~UdpSocket(); |
| 84 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 85 | // Returns true if |socket| belongs to the IPv4/IPv6 address family. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 86 | virtual bool IsIPv4() const = 0; |
| 87 | virtual bool IsIPv6() const = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 88 | |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 89 | // Returns the current local endpoint's address and port. Initially, this will |
| 90 | // be the same as the value that was passed into Create(). However, it can |
| 91 | // later change after certain operations, such as Bind(), are executed. |
| 92 | virtual IPEndpoint GetLocalEndpoint() const = 0; |
| 93 | |
| 94 | // Binds to the address specified in the constructor. If the local endpoint's |
| 95 | // address is zero, the operating system will bind to all interfaces. If the |
| 96 | // local endpoint's port is zero, the operating system will automatically find |
Yuri Wiitala | d697dfc | 2019-10-23 16:00:16 -0700 | [diff] [blame^] | 97 | // a free local port and bind to it. Future calls to GetLocalEndpoint() will |
Yuri Wiitala | de01e53 | 2019-08-06 18:12:02 -0700 | [diff] [blame] | 98 | // reflect the resolved port. |
Ryan Keane | 83e6832 | 2019-08-23 14:54:38 -0700 | [diff] [blame] | 99 | virtual void Bind() = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 100 | |
| 101 | // Sets the device to use for outgoing multicast packets on the socket. |
Ryan Keane | 7e74bdd | 2019-08-27 08:15:21 -0700 | [diff] [blame] | 102 | virtual void SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 103 | |
| 104 | // Joins to the multicast group at the given address, using the specified |
| 105 | // interface. |
Ryan Keane | 7e74bdd | 2019-08-27 08:15:21 -0700 | [diff] [blame] | 106 | virtual void JoinMulticastGroup(const IPAddress& address, |
| 107 | NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 108 | |
Yuri Wiitala | d697dfc | 2019-10-23 16:00:16 -0700 | [diff] [blame^] | 109 | // Sends a message. If the message is not sent, Client::OnSendError() will be |
| 110 | // called to indicate this. Error::Code::kAgain indicates the operation would |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 111 | // block, which can be expected during normal operation. |
Ryan Keane | 71cacc2 | 2019-08-21 13:01:47 -0700 | [diff] [blame] | 112 | virtual void SendMessage(const void* data, |
| 113 | size_t length, |
| 114 | const IPEndpoint& dest) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 115 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 116 | // 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] | 117 | virtual void SetDscp(DscpMode state) = 0; |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 118 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 119 | protected: |
Yuri Wiitala | d697dfc | 2019-10-23 16:00:16 -0700 | [diff] [blame^] | 120 | UdpSocket(); |
Ryan Keane | 83050af | 2019-08-26 08:58:09 -0700 | [diff] [blame] | 121 | |
Ryan Keane | 63fbedd | 2019-08-19 12:33:41 -0700 | [diff] [blame] | 122 | private: |
Jordan Bayles | f1e4bb7 | 2019-05-01 12:38:39 -0700 | [diff] [blame] | 123 | OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocket); |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 124 | }; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 125 | |
| 126 | } // namespace platform |
| 127 | } // namespace openscreen |
| 128 | |
Jordan Bayles | ad2d2cd | 2019-05-22 14:49:40 -0700 | [diff] [blame] | 129 | #endif // PLATFORM_API_UDP_SOCKET_H_ |