blob: 0636225f83b4c5c873a9188fceb7e6c93fcee125 [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
Jordan Baylesab678102019-01-29 13:54:27 -08008#include "base/error.h"
btolsch9ccfa782018-07-26 00:16:08 -07009#include "base/ip_address.h"
Yuri Wiitala82edd202018-10-31 18:42:58 -070010#include "platform/api/network_interface.h"
Jordan Bayles9eb09742019-01-15 14:04:52 -080011#include "third_party/abseil/src/absl/types/optional.h"
btolsch9ccfa782018-07-26 00:16:08 -070012
13namespace openscreen {
14namespace platform {
15
16// Opaque type for the platform to implement.
btolsch56aa52a2018-09-10 11:40:12 -070017struct UdpSocketPrivate;
18using UdpSocketPtr = UdpSocketPrivate*;
btolsch9ccfa782018-07-26 00:16:08 -070019
Yuri Wiitala82edd202018-10-31 18:42:58 -070020// TODO(miu): These should return std::unique_ptr<>, so that code structure
21// auto-scopes the lifetime of the instance (i.e., auto-closing the socket too).
btolsch56aa52a2018-09-10 11:40:12 -070022UdpSocketPtr CreateUdpSocketIPv4();
23UdpSocketPtr CreateUdpSocketIPv6();
btolsch9ccfa782018-07-26 00:16:08 -070024
Yuri Wiitalab94f12e2018-10-31 18:28:53 -070025// Returns true if |socket| is not null and it belongs to the IPv4/IPv6 address
26// family.
27bool IsIPv4Socket(UdpSocketPtr socket);
28bool IsIPv6Socket(UdpSocketPtr socket);
29
btolsch9ccfa782018-07-26 00:16:08 -070030// Closes the underlying platform socket and frees any allocated memory.
btolsch56aa52a2018-09-10 11:40:12 -070031void DestroyUdpSocket(UdpSocketPtr socket);
btolsch9ccfa782018-07-26 00:16:08 -070032
Jordan Baylesab678102019-01-29 13:54:27 -080033Error BindUdpSocket(UdpSocketPtr socket,
34 const IPEndpoint& endpoint,
35 NetworkInterfaceIndex ifindex);
36Error JoinUdpMulticastGroup(UdpSocketPtr socket,
37 const IPAddress& address,
38 NetworkInterfaceIndex ifindex);
btolsch9ccfa782018-07-26 00:16:08 -070039
Jordan Bayles9eb09742019-01-15 14:04:52 -080040absl::optional<int64_t> ReceiveUdp(UdpSocketPtr socket,
41 void* data,
42 int64_t length,
43 IPEndpoint* src,
44 IPEndpoint* original_destination);
45absl::optional<int64_t> SendUdp(UdpSocketPtr socket,
46 const void* data,
47 int64_t length,
48 const IPEndpoint& dest);
btolsch9ccfa782018-07-26 00:16:08 -070049
50} // namespace platform
51} // namespace openscreen
52
btolscha21e8ed2018-08-30 15:13:48 -070053#endif // PLATFORM_API_SOCKET_H_