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