blob: a6d88d49c5eea6bb9d8f545dd7c0877e34ac8138 [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
5#ifndef PLATFORM_API_SOCKET_H_
6#define PLATFORM_API_SOCKET_H_
7
Yuri Wiitala10dea9f2019-02-04 20:19:17 -08008#include <cstdint>
9#include <memory>
10
Jordan Baylesf1e4bb72019-05-01 12:38:39 -070011#include "osp_base/error.h"
12#include "osp_base/ip_address.h"
13#include "osp_base/macros.h"
Yuri Wiitala82edd202018-10-31 18:42:58 -070014#include "platform/api/network_interface.h"
btolsch9ccfa782018-07-26 00:16:08 -070015
16namespace openscreen {
17namespace platform {
18
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080019class UdpSocket;
btolsch9ccfa782018-07-26 00:16:08 -070020
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080021// Platform-specific deleter of a UdpSocket instance returned by
22// UdpSocket::Create().
23struct UdpSocketDeleter {
24 void operator()(UdpSocket* socket) const;
25};
btolsch9ccfa782018-07-26 00:16:08 -070026
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080027using UdpSocketUniquePtr = std::unique_ptr<UdpSocket, UdpSocketDeleter>;
Yuri Wiitalab94f12e2018-10-31 18:28:53 -070028
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080029// An open UDP socket for sending/receiving datagrams to/from either specific
30// endpoints or over IP multicast.
31//
32// Usage: The socket is created and opened by calling the Create() method. This
33// returns a unique pointer that auto-closes/destroys the socket when it goes
34// out-of-scope.
35//
36// Platform implementation note: There must only be one platform-specific
37// implementation of UdpSocket linked into the library/application. For that
38// reason, none of the methods here are declared virtual (i.e., the overhead is
39// pure waste). However, UdpSocket can be subclassed to include all extra
40// private state, such as OS-specific handles. See UdpSocketPosix for a
41// reference implementation.
42class UdpSocket {
43 public:
Ryan Keane8f1c9252019-05-06 11:03:38 -070044 // Constants used to specify how we want packets sent from this socket.
45 enum class DscpMode : uint8_t {
46 // Default value set by the system on creation of a new socket.
47 kUnspecified = 0x0,
48
49 // Mode for Audio only.
50 kAudioOnly = 0xb8,
51
52 // Mode for Audio + Video.
53 kAudioVideo = 0x88,
54
55 // Mode for low priority operations such as trace log data.
56 kLowPriority = 0x20
57 };
58
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080059 using Version = IPAddress::Version;
btolsch9ccfa782018-07-26 00:16:08 -070060
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080061 // Creates a new, scoped UdpSocket within the IPv4 or IPv6 family.
62 static ErrorOr<UdpSocketUniquePtr> Create(Version version);
btolsch9ccfa782018-07-26 00:16:08 -070063
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080064 // Returns true if |socket| belongs to the IPv4/IPv6 address family.
65 bool IsIPv4() const;
66 bool IsIPv6() const;
67
68 // Sets the socket for address reuse, binds to the address/port.
69 Error Bind(const IPEndpoint& local_endpoint);
70
71 // Sets the device to use for outgoing multicast packets on the socket.
72 Error SetMulticastOutboundInterface(NetworkInterfaceIndex ifindex);
73
74 // Joins to the multicast group at the given address, using the specified
75 // interface.
76 Error JoinMulticastGroup(const IPAddress& address,
77 NetworkInterfaceIndex ifindex);
78
79 // Performs a non-blocking read on the socket, returning the number of bytes
80 // received. Note that a non-Error return value of 0 is a valid result,
81 // indicating an empty message has been received. Also note that
82 // Error::Code::kAgain might be returned if there is no message currently
83 // ready for receive, which can be expected during normal operation. |src| and
84 // |original_destination| are optional output arguments that provide the
85 // source of the message and its intended destination, respectively.
86 ErrorOr<size_t> ReceiveMessage(void* data,
87 size_t length,
88 IPEndpoint* src,
89 IPEndpoint* original_destination);
90
91 // Sends a message and returns the number of bytes sent, on success.
92 // Error::Code::kAgain might be returned to indicate the operation would
93 // block, which can be expected during normal operation.
94 Error SendMessage(const void* data, size_t length, const IPEndpoint& dest);
95
Ryan Keane8f1c9252019-05-06 11:03:38 -070096 // Sets the DSCP value to use for all messages sent from this socket.
97 Error SetDscp(DscpMode state);
98
Yuri Wiitala10dea9f2019-02-04 20:19:17 -080099 protected:
100 UdpSocket();
101 ~UdpSocket();
102
103 private:
Jordan Baylesf1e4bb72019-05-01 12:38:39 -0700104 OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocket);
Yuri Wiitala10dea9f2019-02-04 20:19:17 -0800105};
btolsch9ccfa782018-07-26 00:16:08 -0700106
107} // namespace platform
108} // namespace openscreen
109
btolscha21e8ed2018-08-30 15:13:48 -0700110#endif // PLATFORM_API_SOCKET_H_