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 | |
| 5 | #ifndef PLATFORM_API_SOCKET_H_ |
| 6 | #define PLATFORM_API_SOCKET_H_ |
| 7 | |
Jordan Bayles | ab67810 | 2019-01-29 13:54:27 -0800 | [diff] [blame] | 8 | #include "base/error.h" |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 9 | #include "base/ip_address.h" |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 10 | #include "platform/api/network_interface.h" |
Jordan Bayles | 9eb0974 | 2019-01-15 14:04:52 -0800 | [diff] [blame] | 11 | #include "third_party/abseil/src/absl/types/optional.h" |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 12 | |
| 13 | namespace openscreen { |
| 14 | namespace platform { |
| 15 | |
| 16 | // Opaque type for the platform to implement. |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 17 | struct UdpSocketPrivate; |
| 18 | using UdpSocketPtr = UdpSocketPrivate*; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 19 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 20 | // 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). |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 22 | UdpSocketPtr CreateUdpSocketIPv4(); |
| 23 | UdpSocketPtr CreateUdpSocketIPv6(); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 24 | |
Yuri Wiitala | b94f12e | 2018-10-31 18:28:53 -0700 | [diff] [blame] | 25 | // Returns true if |socket| is not null and it belongs to the IPv4/IPv6 address |
| 26 | // family. |
| 27 | bool IsIPv4Socket(UdpSocketPtr socket); |
| 28 | bool IsIPv6Socket(UdpSocketPtr socket); |
| 29 | |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 30 | // Closes the underlying platform socket and frees any allocated memory. |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 31 | void DestroyUdpSocket(UdpSocketPtr socket); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 32 | |
Jordan Bayles | ab67810 | 2019-01-29 13:54:27 -0800 | [diff] [blame] | 33 | Error BindUdpSocket(UdpSocketPtr socket, |
| 34 | const IPEndpoint& endpoint, |
| 35 | NetworkInterfaceIndex ifindex); |
| 36 | Error JoinUdpMulticastGroup(UdpSocketPtr socket, |
| 37 | const IPAddress& address, |
| 38 | NetworkInterfaceIndex ifindex); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 39 | |
Jordan Bayles | 9eb0974 | 2019-01-15 14:04:52 -0800 | [diff] [blame] | 40 | absl::optional<int64_t> ReceiveUdp(UdpSocketPtr socket, |
| 41 | void* data, |
| 42 | int64_t length, |
| 43 | IPEndpoint* src, |
| 44 | IPEndpoint* original_destination); |
| 45 | absl::optional<int64_t> SendUdp(UdpSocketPtr socket, |
| 46 | const void* data, |
| 47 | int64_t length, |
| 48 | const IPEndpoint& dest); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 49 | |
| 50 | } // namespace platform |
| 51 | } // namespace openscreen |
| 52 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 53 | #endif // PLATFORM_API_SOCKET_H_ |