blob: e76b9eb858621b9fc6194ae590e1c16393fcd899 [file] [log] [blame]
btolsch9ccfa782018-07-26 00:16:08 -07001// 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 Baylesad2d2cd2019-05-22 14:49:40 -07005#ifndef PLATFORM_API_UDP_SOCKET_H_
6#define PLATFORM_API_UDP_SOCKET_H_
btolsch9ccfa782018-07-26 00:16:08 -07007
Ryan Keane83050af2019-08-26 08:58:09 -07008#include <atomic>
Yuri Wiitala10dea9f2019-02-04 20:19:17 -08009#include <cstdint>
Ryan Keanedeb48b32019-06-28 16:24:40 -070010#include <functional>
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080011#include <memory>
Ryan Keane9222e042019-08-27 10:44:13 -070012#include <mutex>
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080013
Yuri Wiitala82edd202018-10-31 18:42:58 -070014#include "platform/api/network_interface.h"
Jordan Baylesa26582d2019-07-10 14:44:58 -070015#include "platform/base/error.h"
16#include "platform/base/ip_address.h"
Yuri Wiitala977b6ef2019-11-07 17:08:25 -080017#include "platform/base/udp_packet.h"
btolsch9ccfa782018-07-26 00:16:08 -070018
19namespace openscreen {
20namespace platform {
21
Ryan Keanea4dfaa12019-08-19 10:30:20 -070022class TaskRunner;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080023class UdpSocket;
btolsch9ccfa782018-07-26 00:16:08 -070024
Ryan Keanedeb48b32019-06-28 16:24:40 -070025using UdpSocketUniquePtr = std::unique_ptr<UdpSocket>;
Yuri Wiitalab94f12e2018-10-31 18:28:53 -070026
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080027// An open UDP socket for sending/receiving datagrams to/from either specific
28// endpoints or over IP multicast.
29//
30// Usage: The socket is created and opened by calling the Create() method. This
31// returns a unique pointer that auto-closes/destroys the socket when it goes
32// out-of-scope.
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080033class UdpSocket {
34 public:
Ryan Keanea4dfaa12019-08-19 10:30:20 -070035 // Client for the UdpSocket class.
36 class Client {
37 public:
38 virtual ~Client() = default;
39
40 // Method called on socket configuration operations when an error occurs.
41 // These specific APIs are:
42 // UdpSocket::Bind()
43 // UdpSocket::SetMulticastOutboundInterface(...)
44 // UdpSocket::JoinMulticastGroup(...)
45 // UdpSocket::SetDscp(...)
46 virtual void OnError(UdpSocket* socket, Error error) = 0;
47
48 // Method called when an error occurs during a SendMessage call.
49 virtual void OnSendError(UdpSocket* socket, Error error) = 0;
50
51 // Method called when a packet is read.
52 virtual void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) = 0;
53 };
54
Ryan Keane8f1c9252019-05-06 11:03:38 -070055 // Constants used to specify how we want packets sent from this socket.
56 enum class DscpMode : uint8_t {
57 // Default value set by the system on creation of a new socket.
58 kUnspecified = 0x0,
59
60 // Mode for Audio only.
61 kAudioOnly = 0xb8,
62
63 // Mode for Audio + Video.
64 kAudioVideo = 0x88,
65
66 // Mode for low priority operations such as trace log data.
67 kLowPriority = 0x20
68 };
69
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080070 using Version = IPAddress::Version;
btolsch9ccfa782018-07-26 00:16:08 -070071
Yuri Wiitalade01e532019-08-06 18:12:02 -070072 // Creates a new, scoped UdpSocket within the IPv4 or IPv6 family.
73 // |local_endpoint| may be zero (see comments for Bind()). This method must be
Yuri Wiitalad697dfc2019-10-23 16:00:16 -070074 // defined in the platform-level implementation. All |client| methods called
75 // will be queued on the provided |task_runner|. For this reason, the provided
76 // TaskRunner and Client must exist for the duration of the created socket's
Ryan Keanea4dfaa12019-08-19 10:30:20 -070077 // lifetime.
78 static ErrorOr<UdpSocketUniquePtr> Create(TaskRunner* task_runner,
79 Client* client,
80 const IPEndpoint& local_endpoint);
btolsch9ccfa782018-07-26 00:16:08 -070081
Yuri Wiitalad697dfc2019-10-23 16:00:16 -070082 virtual ~UdpSocket();
83
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080084 // Returns true if |socket| belongs to the IPv4/IPv6 address family.
Ryan Keanedeb48b32019-06-28 16:24:40 -070085 virtual bool IsIPv4() const = 0;
86 virtual bool IsIPv6() const = 0;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080087
Yuri Wiitalade01e532019-08-06 18:12:02 -070088 // Returns the current local endpoint's address and port. Initially, this will
89 // be the same as the value that was passed into Create(). However, it can
90 // later change after certain operations, such as Bind(), are executed.
91 virtual IPEndpoint GetLocalEndpoint() const = 0;
92
93 // Binds to the address specified in the constructor. If the local endpoint's
94 // address is zero, the operating system will bind to all interfaces. If the
95 // local endpoint's port is zero, the operating system will automatically find
Yuri Wiitalad697dfc2019-10-23 16:00:16 -070096 // a free local port and bind to it. Future calls to GetLocalEndpoint() will
Yuri Wiitalade01e532019-08-06 18:12:02 -070097 // reflect the resolved port.
Ryan Keane83e68322019-08-23 14:54:38 -070098 virtual void Bind() = 0;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080099
100 // Sets the device to use for outgoing multicast packets on the socket.
Ryan Keane7e74bdd2019-08-27 08:15:21 -0700101 virtual void SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex) = 0;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800102
103 // Joins to the multicast group at the given address, using the specified
104 // interface.
Ryan Keane7e74bdd2019-08-27 08:15:21 -0700105 virtual void JoinMulticastGroup(const IPAddress& address,
106 NetworkInterfaceIndex ifindex) = 0;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800107
Yuri Wiitalad697dfc2019-10-23 16:00:16 -0700108 // Sends a message. If the message is not sent, Client::OnSendError() will be
109 // called to indicate this. Error::Code::kAgain indicates the operation would
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800110 // block, which can be expected during normal operation.
Ryan Keane71cacc22019-08-21 13:01:47 -0700111 virtual void SendMessage(const void* data,
112 size_t length,
113 const IPEndpoint& dest) = 0;
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800114
Ryan Keane8f1c9252019-05-06 11:03:38 -0700115 // Sets the DSCP value to use for all messages sent from this socket.
Ryan Keane7e74bdd2019-08-27 08:15:21 -0700116 virtual void SetDscp(DscpMode state) = 0;
Ryan Keanedeb48b32019-06-28 16:24:40 -0700117
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800118 protected:
Yuri Wiitalad697dfc2019-10-23 16:00:16 -0700119 UdpSocket();
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800120};
btolsch9ccfa782018-07-26 00:16:08 -0700121
122} // namespace platform
123} // namespace openscreen
124
Jordan Baylesad2d2cd2019-05-22 14:49:40 -0700125#endif // PLATFORM_API_UDP_SOCKET_H_