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