From a28abdbf98061eb13e21cabae7e64d44ec043389 Mon Sep 17 00:00:00 2001 From: Madhura Jayaraman Date: Tue, 12 Nov 2024 12:57:52 -0800 Subject: [PATCH 1/4] Remove starboard socket.h APIs (#4325) b/368349236 --- starboard/android/shared/BUILD.gn | 20 +- .../shared/socket_get_interface_address.cc | 87 ---- starboard/common/socket.cc | 51 --- starboard/elf_loader/exported_symbols.cc | 11 - starboard/linux/shared/BUILD.gn | 11 - starboard/nplb/BUILD.gn | 3 - starboard/nplb/socket_destroy_test.cc | 30 -- .../nplb/socket_get_interface_address_test.cc | 183 --------- .../nplb/socket_get_local_address_test.cc | 130 ------ starboard/raspi/shared/BUILD.gn | 11 - .../linux/socket_get_interface_address.cc | 372 ------------------ starboard/shared/posix/socket_accept.cc | 55 --- starboard/shared/posix/socket_bind.cc | 70 ---- starboard/shared/posix/socket_connect.cc | 57 --- starboard/shared/posix/socket_create.cc | 84 ---- starboard/shared/posix/socket_destroy.cc | 43 -- .../shared/posix/socket_get_local_address.cc | 47 --- .../shared/posix/socket_is_ipv6_supported.cc | 19 - starboard/shared/posix/socket_listen.cc | 55 --- starboard/shared/posix/socket_receive_from.cc | 108 ----- .../shared/posix/socket_set_reuse_address.cc | 26 -- starboard/shared/stub/socket_accept.cc | 19 - starboard/shared/stub/socket_bind.cc | 20 - starboard/shared/stub/socket_connect.cc | 19 - starboard/shared/stub/socket_create.cc | 20 - starboard/shared/stub/socket_destroy.cc | 19 - .../stub/socket_get_interface_address.cc | 21 - .../shared/stub/socket_get_local_address.cc | 19 - .../shared/stub/socket_is_ipv6_supported.cc | 19 - starboard/shared/stub/socket_listen.cc | 19 - starboard/shared/stub/socket_receive_from.cc | 22 -- .../shared/stub/socket_set_reuse_address.cc | 19 - starboard/socket.h | 179 --------- starboard/stub/BUILD.gn | 11 - 34 files changed, 5 insertions(+), 1874 deletions(-) delete mode 100644 starboard/android/shared/socket_get_interface_address.cc delete mode 100644 starboard/nplb/socket_destroy_test.cc delete mode 100644 starboard/nplb/socket_get_interface_address_test.cc delete mode 100644 starboard/nplb/socket_get_local_address_test.cc delete mode 100644 starboard/shared/linux/socket_get_interface_address.cc delete mode 100644 starboard/shared/posix/socket_accept.cc delete mode 100644 starboard/shared/posix/socket_bind.cc delete mode 100644 starboard/shared/posix/socket_connect.cc delete mode 100644 starboard/shared/posix/socket_create.cc delete mode 100644 starboard/shared/posix/socket_destroy.cc delete mode 100644 starboard/shared/posix/socket_get_local_address.cc delete mode 100644 starboard/shared/posix/socket_is_ipv6_supported.cc delete mode 100644 starboard/shared/posix/socket_listen.cc delete mode 100644 starboard/shared/posix/socket_receive_from.cc delete mode 100644 starboard/shared/posix/socket_set_reuse_address.cc delete mode 100644 starboard/shared/stub/socket_accept.cc delete mode 100644 starboard/shared/stub/socket_bind.cc delete mode 100644 starboard/shared/stub/socket_connect.cc delete mode 100644 starboard/shared/stub/socket_create.cc delete mode 100644 starboard/shared/stub/socket_destroy.cc delete mode 100644 starboard/shared/stub/socket_get_interface_address.cc delete mode 100644 starboard/shared/stub/socket_get_local_address.cc delete mode 100644 starboard/shared/stub/socket_is_ipv6_supported.cc delete mode 100644 starboard/shared/stub/socket_listen.cc delete mode 100644 starboard/shared/stub/socket_receive_from.cc delete mode 100644 starboard/shared/stub/socket_set_reuse_address.cc diff --git a/starboard/android/shared/BUILD.gn b/starboard/android/shared/BUILD.gn index deae45e95890..65dd7b55c8d9 100644 --- a/starboard/android/shared/BUILD.gn +++ b/starboard/android/shared/BUILD.gn @@ -86,17 +86,7 @@ static_library("starboard_platform") { "//starboard/shared/posix/memory_free_aligned.cc", "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", - "//starboard/shared/posix/socket_accept.cc", - "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_connect.cc", - "//starboard/shared/posix/socket_create.cc", - "//starboard/shared/posix/socket_destroy.cc", - "//starboard/shared/posix/socket_get_local_address.cc", "//starboard/shared/posix/socket_internal.cc", - "//starboard/shared/posix/socket_is_ipv6_supported.cc", - "//starboard/shared/posix/socket_listen.cc", - "//starboard/shared/posix/socket_receive_from.cc", - "//starboard/shared/posix/socket_set_reuse_address.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", @@ -288,11 +278,11 @@ static_library("starboard_platform") { "posix_emu/pthread.cc", "posix_emu/stat.cc", "sanitizer_options.cc", - "socket_get_interface_address.cc", - "speech_synthesis_cancel.cc", - "speech_synthesis_internal.cc", - "speech_synthesis_is_supported.cc", - "speech_synthesis_speak.cc", + + #"speech_synthesis_cancel.cc", + #"speech_synthesis_internal.cc", + #"speech_synthesis_is_supported.cc", + #"speech_synthesis_speak.cc", "system_get_extensions.cc", "system_get_locale_id.cc", "system_get_path.cc", diff --git a/starboard/android/shared/socket_get_interface_address.cc b/starboard/android/shared/socket_get_interface_address.cc deleted file mode 100644 index 67be4cff5b95..000000000000 --- a/starboard/android/shared/socket_get_interface_address.cc +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2022 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/android/shared/jni_env_ext.h" -#include "starboard/log.h" -#include "starboard/socket.h" - -using starboard::android::shared::JniEnvExt; - -namespace { - -const size_t kDefaultPrefixLength = 8; -const int kIPv6AddressSize = 16; - -bool CopySocketAddress(jbyteArray array, SbSocketAddress* out_address) { - JniEnvExt* env = JniEnvExt::Get(); - if (array == nullptr) { - return false; - } - if (out_address == nullptr) { - SB_LOG(ERROR) << "SbSocketGetInterfaceAddress NULL out_address"; - return false; - } - jint size = env->GetArrayLength(array); - if (size > sizeof(out_address->address)) { - SB_LOG(ERROR) << "SbSocketGetInterfaceAddress address too long"; - return false; - } - out_address->type = - (size == 4) ? kSbSocketAddressTypeIpv4 : kSbSocketAddressTypeIpv6; - jbyte* bytes = env->GetByteArrayElements(array, NULL); - SB_CHECK(bytes) << "GetByteArrayElements failed"; - memcpy(out_address->address, bytes, size); - env->ReleaseByteArrayElements(array, bytes, JNI_ABORT); - return true; -} -} // namespace - -bool SbSocketGetInterfaceAddress(const SbSocketAddress* const destination, - SbSocketAddress* out_source_address, - SbSocketAddress* out_netmask) { - if (out_source_address == nullptr) { - return false; - } - - memset(out_source_address->address, 0, sizeof(out_source_address->address)); - - out_source_address->port = 0; - - JniEnvExt* env = JniEnvExt::Get(); - - jboolean want_ipv6 = - (destination != nullptr && destination->type == kSbSocketAddressTypeIpv6); - jobject pair = (jbyteArray)env->CallStarboardObjectMethodOrAbort( - "getLocalInterfaceAddressAndNetmask", "(Z)Landroid/util/Pair;", - want_ipv6); - - if (!pair) { - SB_LOG(ERROR) << "Null value returned from JNI call to " - "getLocalInterfaceAddressAndNetmask."; - return false; - } - - jobject field; - field = env->GetObjectFieldOrAbort(pair, "first", "Ljava/lang/Object;"); - if (!CopySocketAddress(static_cast(field), out_source_address)) { - return false; - } - field = env->GetObjectFieldOrAbort(pair, "second", "Ljava/lang/Object;"); - if (out_netmask && - !CopySocketAddress(static_cast(field), out_netmask)) { - return false; - } - - return true; -} diff --git a/starboard/common/socket.cc b/starboard/common/socket.cc index f66ee82ea581..5168ca764a84 100644 --- a/starboard/common/socket.cc +++ b/starboard/common/socket.cc @@ -51,61 +51,10 @@ bool GetLocalhostAddress(SbSocketAddressType address_type, return true; } -Socket::Socket(SbSocketAddressType address_type, SbSocketProtocol protocol) - : socket_(SbSocketCreate(address_type, protocol)) {} - -Socket::Socket(SbSocketAddressType address_type) - : socket_(SbSocketCreate(address_type, kSbSocketProtocolTcp)) {} - -Socket::Socket(SbSocketProtocol protocol) - : socket_(SbSocketCreate(kSbSocketAddressTypeIpv4, protocol)) {} - -Socket::Socket() - : socket_(SbSocketCreate(kSbSocketAddressTypeIpv4, kSbSocketProtocolTcp)) {} - -Socket::~Socket() { - SbSocketDestroy(socket_); -} - bool Socket::IsValid() { return SbSocketIsValid(socket_); } -SbSocketError Socket::Connect(const SbSocketAddress* address) { - return SbSocketConnect(socket_, address); -} - -SbSocketError Socket::Bind(const SbSocketAddress* local_address) { - return SbSocketBind(socket_, local_address); -} - -SbSocketError Socket::Listen() { - return SbSocketListen(socket_); -} - -Socket* Socket::Accept() { - SbSocket accepted_socket = SbSocketAccept(socket_); - if (SbSocketIsValid(accepted_socket)) { - return new Socket(accepted_socket); - } - - return NULL; -} - -int Socket::ReceiveFrom(char* out_data, - int data_size, - SbSocketAddress* out_source) { - return SbSocketReceiveFrom(socket_, out_data, data_size, out_source); -} - -bool Socket::GetLocalAddress(SbSocketAddress* out_address) { - return SbSocketGetLocalAddress(socket_, out_address); -} - -bool Socket::SetReuseAddress(bool value) { - return SbSocketSetReuseAddress(socket_, value); -} - SbSocket Socket::socket() { return socket_; } diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index e0bea2524150..df3d4bd8d8f7 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -147,17 +147,6 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbPlayerSetVolume); REGISTER_SYMBOL(SbPlayerWriteEndOfStream); REGISTER_SYMBOL(SbPlayerWriteSamples); - REGISTER_SYMBOL(SbSocketAccept); - REGISTER_SYMBOL(SbSocketBind); - REGISTER_SYMBOL(SbSocketConnect); - REGISTER_SYMBOL(SbSocketCreate); - REGISTER_SYMBOL(SbSocketDestroy); - REGISTER_SYMBOL(SbSocketGetInterfaceAddress); - REGISTER_SYMBOL(SbSocketGetLocalAddress); - REGISTER_SYMBOL(SbSocketIsIpv6Supported); - REGISTER_SYMBOL(SbSocketListen); - REGISTER_SYMBOL(SbSocketReceiveFrom); - REGISTER_SYMBOL(SbSocketSetReuseAddress); REGISTER_SYMBOL(SbSocketWaiterAdd); REGISTER_SYMBOL(SbSocketWaiterCreate); REGISTER_SYMBOL(SbSocketWaiterDestroy); diff --git a/starboard/linux/shared/BUILD.gn b/starboard/linux/shared/BUILD.gn index 8269277cf3b3..902a649cbc9c 100644 --- a/starboard/linux/shared/BUILD.gn +++ b/starboard/linux/shared/BUILD.gn @@ -126,7 +126,6 @@ static_library("starboard_platform_sources") { "//starboard/shared/linux/dev_input/dev_input.cc", "//starboard/shared/linux/dev_input/dev_input.h", "//starboard/shared/linux/get_home_directory.cc", - "//starboard/shared/linux/socket_get_interface_address.cc", "//starboard/shared/linux/system_get_random_data.cc", "//starboard/shared/linux/system_get_stack.cc", "//starboard/shared/linux/system_get_total_cpu_memory.cc", @@ -156,17 +155,7 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/memory_mapped_file.h", "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", - "//starboard/shared/posix/socket_accept.cc", - "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_connect.cc", - "//starboard/shared/posix/socket_create.cc", - "//starboard/shared/posix/socket_destroy.cc", - "//starboard/shared/posix/socket_get_local_address.cc", "//starboard/shared/posix/socket_internal.cc", - "//starboard/shared/posix/socket_is_ipv6_supported.cc", - "//starboard/shared/posix/socket_listen.cc", - "//starboard/shared/posix/socket_receive_from.cc", - "//starboard/shared/posix/socket_set_reuse_address.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 73d9a13ce1ac..eaa5a169db7b 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -177,9 +177,6 @@ target(gtest_target_type, "nplb") { "random_helpers.cc", "recursive_mutex_test.cc", "semaphore_test.cc", - "socket_destroy_test.cc", - "socket_get_interface_address_test.cc", - "socket_get_local_address_test.cc", "socket_helpers.cc", "socket_waiter_create_test.cc", "socket_waiter_destroy_test.cc", diff --git a/starboard/nplb/socket_destroy_test.cc b/starboard/nplb/socket_destroy_test.cc deleted file mode 100644 index d1e52bf34ccf..000000000000 --- a/starboard/nplb/socket_destroy_test.cc +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Most of the destroy tests are done in the other tests that create sockets. - -#include "starboard/common/socket.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -TEST(SbSocketDestroyTest, RainyDayInvalidSocket) { - EXPECT_FALSE(SbSocketDestroy(kSbSocketInvalid)); -} - -} // namespace -} // namespace nplb -} // namespace starboard diff --git a/starboard/nplb/socket_get_interface_address_test.cc b/starboard/nplb/socket_get_interface_address_test.cc deleted file mode 100644 index 17ae3f3f4149..000000000000 --- a/starboard/nplb/socket_get_interface_address_test.cc +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright 2017 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#if SB_API_VERSION < 17 - -#include "starboard/common/socket.h" -#include "starboard/nplb/socket_helpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -const unsigned char kInvalidByte = 0xFE; - -class SbSocketGetInterfaceAddressTest - : public ::testing::TestWithParam { - public: - SbSocketAddressType GetAddressType() { return GetParam(); } -}; - -TEST(SbSocketGetInterfaceAddressTest, SunnyDay) { - SbSocketAddress invalid_address; - SbSocketAddress address; - - // Initialize to something invalid. - memset(&address, kInvalidByte, sizeof(address)); - memset(&invalid_address, kInvalidByte, sizeof(invalid_address)); - - EXPECT_TRUE(SbSocketGetInterfaceAddress(NULL, &address, NULL)); - EXPECT_EQ(0, address.port); - EXPECT_FALSE(IsUnspecified(&address)); - EXPECT_FALSE(IsLocalhost(&address)); - EXPECT_NE(0, memcmp(address.address, invalid_address.address, - SB_ARRAY_SIZE(address.address))); -} - -TEST(SbSocketGetInterfaceAddressTest, RainyDayNull) { - EXPECT_FALSE(SbSocketGetInterfaceAddress(NULL, NULL, NULL)); -} - -TEST(SbSocketGetInterfaceAddressTest, SunnyDayNullDestination) { - SbSocketAddress netmask; - SbSocketAddress source; - - memset(&netmask, kInvalidByte, sizeof(netmask)); - memset(&source, kInvalidByte, sizeof(source)); - - // If destination address is NULL, then any IP address that is valid for - // |destination| set to 0.0.0.0 (IPv4) or :: (IPv6) can be returned. - - EXPECT_TRUE(SbSocketGetInterfaceAddress(NULL, &source, NULL)); - EXPECT_TRUE(source.type == kSbSocketAddressTypeIpv4 || - source.type == kSbSocketAddressTypeIpv6); - - EXPECT_TRUE(SbSocketGetInterfaceAddress(NULL, &source, &netmask)); - // A netmask that starts with 0 is likely incorrect. - EXPECT_TRUE(netmask.address[0] & 0x80); - EXPECT_TRUE(source.type == kSbSocketAddressTypeIpv4 || - source.type == kSbSocketAddressTypeIpv6); - EXPECT_TRUE(netmask.type == kSbSocketAddressTypeIpv4 || - netmask.type == kSbSocketAddressTypeIpv6); -} - -TEST_P(SbSocketGetInterfaceAddressTest, SunnyDayDestination) { - SbSocketAddress destination = {0}; - destination.type = GetAddressType(); - - SbSocketAddress netmask; - SbSocketAddress source; - - // Initialize to something invalid. - memset(&netmask, kInvalidByte, sizeof(netmask)); - memset(&source, kInvalidByte, sizeof(source)); - - EXPECT_TRUE(SbSocketGetInterfaceAddress(&destination, &source, NULL)); - EXPECT_EQ(GetAddressType(), source.type); - EXPECT_TRUE(SbSocketGetInterfaceAddress(&destination, &source, &netmask)); - - EXPECT_FALSE(IsLocalhost(&source)); - - // A netmask that starts with 0 is likely incorrect. - EXPECT_TRUE(netmask.address[0] & 0x80); - EXPECT_EQ(GetAddressType(), source.type); - EXPECT_EQ(GetAddressType(), netmask.type); - EXPECT_EQ(0, source.port); -} - -#if SB_API_VERSION < 16 -TEST_P(SbSocketGetInterfaceAddressTest, SunnyDaySourceForDestination) { - const char kTestHostName[] = "www.example.com"; - - SbSocketResolveFilter resolve_filter = kSbSocketResolveFilterNone; - switch (GetAddressType()) { - case kSbSocketAddressTypeIpv4: - resolve_filter = kSbSocketResolveFilterIpv4; - break; - case kSbSocketAddressTypeIpv6: - resolve_filter = kSbSocketResolveFilterIpv6; - break; - default: - FAIL() << "Invalid address type " << GetAddressType(); - } - SbSocketResolution* resolution = - SbSocketResolve(kTestHostName, resolve_filter); - - // TODO: Switch to nullptr, when C++11 is available. - ASSERT_NE(resolution, reinterpret_cast(NULL)); - ASSERT_NE(resolution->address_count, 0); - SbSocketAddress& destination_address = resolution->addresses[0]; - - SbSocketAddress source; - SbSocketAddress netmask; - SbSocketAddress invalid_address; - memset(&netmask, kInvalidByte, sizeof(netmask)); - memset(&source, kInvalidByte, sizeof(source)); - memset(&invalid_address, kInvalidByte, sizeof(source)); - SbSocketGetInterfaceAddress(&destination_address, &source, &netmask); - - EXPECT_EQ(GetAddressType(), source.type); - // A netmask that starts with 0 is likely incorrect. - EXPECT_TRUE(netmask.address[0] & 0x80); - EXPECT_EQ(GetAddressType(), netmask.type); - EXPECT_NE(0, memcmp(source.address, invalid_address.address, - SB_ARRAY_SIZE(source.address))); - EXPECT_NE(0, memcmp(netmask.address, invalid_address.address, - SB_ARRAY_SIZE(netmask.address))); - - SbSocketFreeResolution(resolution); -} -#endif - -TEST_P(SbSocketGetInterfaceAddressTest, SunnyDaySourceNotLoopback) { - SbSocketAddress destination = {0}; - destination.type = GetAddressType(); - - // If the destination address is 0.0.0.0, and its |type| is - // |kSbSocketAddressTypeIpv4|, then any IPv4 local interface that is up and - // not a loopback interface is a valid return value. - // - // If the destination address is ::, and its |type| is - // |kSbSocketAddressTypeIpv6| then any IPv6 local interface that is up and - // not loopback or a link-local IP is a valid return value. However, in the - // case of IPv6, the address with the biggest scope must be returned. E.g., a - // globally scoped and routable IP is preferred over a unique local address - // (ULA). Also, the IP address that is returned must be permanent. - - SbSocketAddress netmask; - SbSocketAddress source; - SbSocketAddress invalid_address; - - // Initialize to something invalid. - memset(&netmask, kInvalidByte, sizeof(netmask)); - memset(&source, kInvalidByte, sizeof(source)); - memset(&invalid_address, kInvalidByte, sizeof(invalid_address)); - - EXPECT_TRUE(SbSocketGetInterfaceAddress(&destination, &source, NULL)); - EXPECT_EQ(GetAddressType(), source.type); - EXPECT_TRUE(SbSocketGetInterfaceAddress(&destination, &source, &netmask)); - EXPECT_FALSE(IsLocalhost(&source)); - EXPECT_FALSE(IsUnspecified(&source)); - - EXPECT_NE(0, memcmp(netmask.address, invalid_address.address, - SB_ARRAY_SIZE(netmask.address))); - EXPECT_NE(0, memcmp(source.address, invalid_address.address, - SB_ARRAY_SIZE(source.address))); -} - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_get_local_address_test.cc b/starboard/nplb/socket_get_local_address_test.cc deleted file mode 100644 index 451df14bf0ef..000000000000 --- a/starboard/nplb/socket_get_local_address_test.cc +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#if SB_API_VERSION < 17 - -#include - -#include "starboard/common/socket.h" -#include "starboard/nplb/socket_helpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -class SbSocketGetLocalAddressTest - : public ::testing::TestWithParam { - public: - SbSocketAddressType GetAddressType() { return GetParam(); } -}; - -class PairSbSocketGetLocalAddressTest - : public ::testing::TestWithParam< - std::pair > { - public: - SbSocketAddressType GetServerAddressType() { return GetParam().first; } - SbSocketAddressType GetClientAddressType() { return GetParam().second; } -}; - -TEST_F(SbSocketGetLocalAddressTest, RainyDayInvalidSocket) { - SbSocketAddress address = {0}; - EXPECT_FALSE(SbSocketGetLocalAddress(kSbSocketInvalid, &address)); -} - -TEST_P(SbSocketGetLocalAddressTest, RainyDayNullAddress) { - SbSocket server_socket = CreateBoundTcpSocket(GetAddressType(), 0); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - EXPECT_FALSE(SbSocketGetLocalAddress(server_socket, NULL)); - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -TEST_F(SbSocketGetLocalAddressTest, RainyDayNullNull) { - EXPECT_FALSE(SbSocketGetLocalAddress(kSbSocketInvalid, NULL)); -} - -TEST_P(SbSocketGetLocalAddressTest, SunnyDayUnbound) { - SbSocket server_socket = CreateServerTcpSocket(GetAddressType()); - SbSocketAddress address = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(server_socket, &address)); - EXPECT_EQ(GetAddressType(), address.type); - EXPECT_EQ(0, address.port); - EXPECT_TRUE(IsUnspecified(&address)); - - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -TEST_P(SbSocketGetLocalAddressTest, SunnyDayBoundUnspecified) { - SbSocket server_socket = CreateBoundTcpSocket(GetAddressType(), 0); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - - SbSocketAddress address = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(server_socket, &address)); - EXPECT_EQ(GetAddressType(), address.type); - EXPECT_NE(0, address.port); - - // We bound to an unspecified address, so it should come back that way. - EXPECT_TRUE(IsUnspecified(&address)); - - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -TEST_F(SbSocketGetLocalAddressTest, SunnyDayBoundSpecified) { - SbSocketAddress interface_address = {0}; - EXPECT_TRUE(SbSocketGetInterfaceAddress(NULL, &interface_address, NULL)); - - SbSocket server_socket = CreateServerTcpSocket(interface_address.type); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - - EXPECT_EQ(kSbSocketOk, SbSocketBind(server_socket, &interface_address)); - - SbSocketAddress local_address = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(server_socket, &local_address)); - EXPECT_NE(0, local_address.port); - EXPECT_FALSE(IsUnspecified(&local_address)); - - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -TEST_P(PairSbSocketGetLocalAddressTest, SunnyDayConnected) { - const int kPort = PosixGetPortNumberForTests(); - ConnectedTrio trio = CreateAndConnect( - GetServerAddressType(), GetClientAddressType(), kPort, kSocketTimeout); - ASSERT_TRUE(SbSocketIsValid(trio.server_socket)); - - SbSocketAddress address = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(trio.listen_socket, &address)); - EXPECT_TRUE(IsUnspecified(&address)); - EXPECT_EQ(kPort, address.port); - - SbSocketAddress address1 = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(trio.client_socket, &address1)); - EXPECT_FALSE(IsUnspecified(&address1)); - EXPECT_NE(kPort, address1.port); - - SbSocketAddress address2 = {0}; - EXPECT_TRUE(SbSocketGetLocalAddress(trio.server_socket, &address2)); - EXPECT_FALSE(IsUnspecified(&address2)); - EXPECT_EQ(kPort, address2.port); - - EXPECT_TRUE(SbSocketDestroy(trio.server_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.client_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.listen_socket)); -} - -} // namespace -} // namespace nplb -} // namespace starboard - -#endif // SB_API_VERSION < 17 diff --git a/starboard/raspi/shared/BUILD.gn b/starboard/raspi/shared/BUILD.gn index 4c42b771123a..d8eefcf50110 100644 --- a/starboard/raspi/shared/BUILD.gn +++ b/starboard/raspi/shared/BUILD.gn @@ -93,7 +93,6 @@ static_library("starboard_platform_sources") { "//starboard/shared/linux/cpu_features_get.cc", "//starboard/shared/linux/dev_input/dev_input.cc", "//starboard/shared/linux/get_home_directory.cc", - "//starboard/shared/linux/socket_get_interface_address.cc", "//starboard/shared/linux/system_get_random_data.cc", "//starboard/shared/linux/system_get_stack.cc", "//starboard/shared/linux/system_get_total_cpu_memory.cc", @@ -114,17 +113,7 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/memory_free_aligned.cc", "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", - "//starboard/shared/posix/socket_accept.cc", - "//starboard/shared/posix/socket_bind.cc", - "//starboard/shared/posix/socket_connect.cc", - "//starboard/shared/posix/socket_create.cc", - "//starboard/shared/posix/socket_destroy.cc", - "//starboard/shared/posix/socket_get_local_address.cc", "//starboard/shared/posix/socket_internal.cc", - "//starboard/shared/posix/socket_is_ipv6_supported.cc", - "//starboard/shared/posix/socket_listen.cc", - "//starboard/shared/posix/socket_receive_from.cc", - "//starboard/shared/posix/socket_set_reuse_address.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", diff --git a/starboard/shared/linux/socket_get_interface_address.cc b/starboard/shared/linux/socket_get_interface_address.cc deleted file mode 100644 index 6eca18e221ae..000000000000 --- a/starboard/shared/linux/socket_get_interface_address.cc +++ /dev/null @@ -1,372 +0,0 @@ -// Copyright 2017 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -// linux/if.h assumes the symbols for structs defined in ifaddrs.h are -// already present. These includes must be above below. -#include -#include - -#if !defined(_GNU_SOURCE) -#include -#include -#include -#else // _GNU_SOURCE is set for Linux -#include -#include -#include -#endif - -#include -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -namespace { - -// TODO: Move this constant to socket.h. -const int kIPv6AddressSize = 16; - -bool IsAnyAddress(const SbSocketAddress& address) { - switch (address.type) { - case kSbSocketAddressTypeIpv4: - return (address.address[0] == 0 && address.address[1] == 0 && - address.address[2] == 0 && address.address[3] == 0); - case kSbSocketAddressTypeIpv6: { - bool found_nonzero = false; - for (std::size_t i = 0; i != kIPv6AddressSize; ++i) { - found_nonzero |= (address.address[i] != 0); - } - return !found_nonzero; - } - default: - SB_NOTREACHED() << "Invalid address type " << address.type; - break; - } - - return false; -} - -template -void CopyIntoObjectFromArray(T* out_destination, - const unsigned char (&source)[source_size]) { - SB_COMPILE_ASSERT(sizeof(T) <= source_size, destination_is_too_small); - memcpy(out_destination, source, sizeof(T)); -} - -bool GetPotentialMatch(const sockaddr* input_addr, - const in_addr** out_interface_addr) { - if (!input_addr || input_addr->sa_family != AF_INET) { - *out_interface_addr = NULL; - return false; - } - - const sockaddr_in* v4input_addr = - reinterpret_cast(input_addr); - *out_interface_addr = &(v4input_addr->sin_addr); - return true; -} - -bool GetPotentialMatch(const sockaddr* input_addr, - const in6_addr** out_interface_addr) { - if (!input_addr || input_addr->sa_family != AF_INET6) { - *out_interface_addr = NULL; - return false; - } - - const sockaddr_in6* v6input_addr = - reinterpret_cast(input_addr); - *out_interface_addr = &(v6input_addr->sin6_addr); - return true; -} - -template -bool GetNetmaskForInterfaceAddress(const SbSocketAddress& interface_address, - SbSocketAddress* out_netmask) { - SB_DCHECK((interface_address.type == kSbSocketAddressTypeIpv4) || - (interface_address.type == kSbSocketAddressTypeIpv6)); - struct ifaddrs* interface_addrs = NULL; - - int retval = getifaddrs(&interface_addrs); - if (retval != 0) { - return false; - } - - in_addr_type to_match; - CopyIntoObjectFromArray(&to_match, interface_address.address); - - bool found_netmask = false; - for (struct ifaddrs* interface = interface_addrs; interface != NULL; - interface = interface->ifa_next) { - if (!(IFF_UP & interface->ifa_flags) || - (IFF_LOOPBACK & interface->ifa_flags)) { - continue; - } - - const in_addr_type* potential_match; - if (!GetPotentialMatch(interface->ifa_addr, &potential_match)) - continue; - - if (memcmp(&to_match, potential_match, sizeof(in_addr_type)) != 0) { - continue; - } - - sbposix::SockAddr sock_addr; - sock_addr.FromSockaddr(interface->ifa_netmask); - if (sock_addr.ToSbSocketAddress(out_netmask)) { - found_netmask = true; - break; - } - } - - freeifaddrs(interface_addrs); - - return found_netmask; -} - -bool GetNetMaskForInterfaceAddress(const SbSocketAddress& interface_address, - SbSocketAddress* out_netmask) { - SB_DCHECK(out_netmask); - - switch (interface_address.type) { - case kSbSocketAddressTypeIpv4: - return GetNetmaskForInterfaceAddress(interface_address, - out_netmask); - case kSbSocketAddressTypeIpv6: - return GetNetmaskForInterfaceAddress(interface_address, - out_netmask); - default: - SB_NOTREACHED() << "Invalid address type " << interface_address.type; - break; - } - - return false; -} - -bool FindIPv4InterfaceIP(SbSocketAddress* out_interface_ip, - SbSocketAddress* out_netmask) { - if (out_interface_ip == NULL) { - SB_NOTREACHED() << "out_interface_ip must be specified"; - return false; - } - struct ifaddrs* interface_addrs = NULL; - - int retval = getifaddrs(&interface_addrs); - if (retval != 0) { - return false; - } - - bool success = false; - for (struct ifaddrs* interface = interface_addrs; interface != NULL; - interface = interface->ifa_next) { - if (!(IFF_UP & interface->ifa_flags) || - (IFF_LOOPBACK & interface->ifa_flags)) { - continue; - } - - const struct sockaddr* addr = interface->ifa_addr; - const struct sockaddr* netmask = interface->ifa_netmask; - if (!addr || !netmask || (addr->sa_family != AF_INET)) { - // IPv4 addresses only. - continue; - } - - sbposix::SockAddr sock_addr; - sock_addr.FromSockaddr(addr); - if (sock_addr.ToSbSocketAddress(out_interface_ip)) { - if (out_netmask) { - sbposix::SockAddr netmask_addr; - netmask_addr.FromSockaddr(netmask); - if (!netmask_addr.ToSbSocketAddress(out_netmask)) { - continue; - } - } - - success = true; - break; - } - } - - freeifaddrs(interface_addrs); - - return success; -} - -bool IsUniqueLocalAddress(const unsigned char ip[16]) { - // Unique Local Addresses are in fd08::/8. - return ip[0] == 0xfd && ip[1] == 0x08; -} - -bool FindIPv6InterfaceIP(SbSocketAddress* out_interface_ip, - SbSocketAddress* out_netmask) { - if (!out_interface_ip) { - SB_NOTREACHED() << "out_interface_ip must be specified"; - return false; - } - struct ifaddrs* interface_addrs = NULL; - - int retval = getifaddrs(&interface_addrs); - if (retval != 0) { - return false; - } - - int max_scope_interface_value = -1; - - bool ip_found = false; - SbSocketAddress temp_interface_ip; - SbSocketAddress temp_netmask; - - for (struct ifaddrs* interface = interface_addrs; interface != NULL; - interface = interface->ifa_next) { - if (!(IFF_UP & interface->ifa_flags) || - (IFF_LOOPBACK & interface->ifa_flags)) { - continue; - } - - const struct sockaddr* addr = interface->ifa_addr; - const struct sockaddr* netmask = interface->ifa_netmask; - if (!addr || !netmask || addr->sa_family != AF_INET6) { - // IPv6 addresses only. - continue; - } - - const in6_addr* potential_match; - if (!GetPotentialMatch(interface->ifa_addr, &potential_match)) - continue; - - // Check the IP for loopback again, just in case flags were incorrect. - if (IN6_IS_ADDR_LOOPBACK(potential_match) || - IN6_IS_ADDR_LINKLOCAL(potential_match)) { - continue; - } - - const sockaddr_in6* v6addr = - reinterpret_cast(interface->ifa_addr); - if (!v6addr) { - continue; - } - - int current_interface_scope = v6addr->sin6_scope_id; - - if (IsUniqueLocalAddress(v6addr->sin6_addr.s6_addr)) { - // ULAs have global scope, but not globally routable. So prefer - // non ULA addresses with global scope by adjusting their "scope" - current_interface_scope -= 1; - } - - if (current_interface_scope <= max_scope_interface_value) { - continue; - } - max_scope_interface_value = current_interface_scope; - - sbposix::SockAddr sock_addr; - sock_addr.FromSockaddr(addr); - if (sock_addr.ToSbSocketAddress(&temp_interface_ip)) { - if (netmask) { - sbposix::SockAddr netmask_addr; - netmask_addr.FromSockaddr(netmask); - if (!netmask_addr.ToSbSocketAddress(&temp_netmask)) { - continue; - } - } - - ip_found = true; - } - } - - freeifaddrs(interface_addrs); - - if (!ip_found) { - return false; - } - - memcpy(out_interface_ip, &temp_interface_ip, sizeof(SbSocketAddress)); - if (out_netmask != NULL) { - memcpy(out_netmask, &temp_netmask, sizeof(SbSocketAddress)); - } - - return true; -} - -bool FindInterfaceIP(const SbSocketAddressType type, - SbSocketAddress* out_interface_ip, - SbSocketAddress* out_netmask) { - switch (type) { - case kSbSocketAddressTypeIpv4: - return FindIPv4InterfaceIP(out_interface_ip, out_netmask); - case kSbSocketAddressTypeIpv6: - return FindIPv6InterfaceIP(out_interface_ip, out_netmask); - default: - SB_NOTREACHED() << "Invalid socket address type " << type; - } - - return false; -} - -bool FindSourceAddressForDestination(const SbSocketAddress& destination, - SbSocketAddress* out_source_address) { - SbSocket socket = SbSocketCreate(destination.type, kSbSocketProtocolUdp); - if (!SbSocketIsValid(socket)) { - return false; - } - - SbSocketError connect_retval = SbSocketConnect(socket, &destination); - if (connect_retval != kSbSocketOk) { - bool socket_destroyed = SbSocketDestroy(socket); - SB_DCHECK(socket_destroyed); - return false; - } - - bool success = SbSocketGetLocalAddress(socket, out_source_address); - bool socket_destroyed = SbSocketDestroy(socket); - SB_DCHECK(socket_destroyed); - return success; -} - -} // namespace - -bool SbSocketGetInterfaceAddress(const SbSocketAddress* const destination, - SbSocketAddress* out_source_address, - SbSocketAddress* out_netmask) { - if (!out_source_address) { - return false; - } - - if (destination == NULL) { - // Return either a v4 or a v6 address. Per spec. - return (FindIPv4InterfaceIP(out_source_address, out_netmask) || - FindIPv6InterfaceIP(out_source_address, out_netmask)); - - } else if (IsAnyAddress(*destination)) { - return FindInterfaceIP(destination->type, out_source_address, out_netmask); - } else { - SbSocketAddress destination_copy = *destination; - // On some platforms, passing a socket address with port 0 to connect() - // results in EADDRNOTAVAIL. - if (!destination_copy.port) { - destination_copy.port = 80; - } - return ( - FindSourceAddressForDestination(destination_copy, out_source_address) && - GetNetMaskForInterfaceAddress(*out_source_address, out_netmask)); - } - - return false; -} diff --git a/starboard/shared/posix/socket_accept.cc b/starboard/shared/posix/socket_accept.cc deleted file mode 100644 index 685af660c044..000000000000 --- a/starboard/shared/posix/socket_accept.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/set_non_blocking_internal.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -SbSocket SbSocketAccept(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return kSbSocketInvalid; - } - - SB_DCHECK(socket->socket_fd >= 0); - - int socket_fd = HANDLE_EINTR(accept(socket->socket_fd, NULL, NULL)); - if (socket_fd < 0) { - socket->error = sbposix::TranslateSocketErrno(errno); - return kSbSocketInvalid; - } - - // All Starboard sockets are non-blocking, so let's ensure it. - if (!sbposix::SetNonBlocking(socket_fd)) { - // Something went wrong, we'll clean up and return failure. - socket->error = sbposix::TranslateSocketErrno(errno); - HANDLE_EINTR(close(socket_fd)); - return kSbSocketInvalid; - } - - socket->error = kSbSocketOk; - - // Adopt the newly accepted socket. - return new SbSocketPrivate(socket->address_type, socket->protocol, socket_fd); -} diff --git a/starboard/shared/posix/socket_bind.cc b/starboard/shared/posix/socket_bind.cc deleted file mode 100644 index 01fbe9ea3ddf..000000000000 --- a/starboard/shared/posix/socket_bind.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/common/memory.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -SbSocketError SbSocketBind(SbSocket socket, - const SbSocketAddress* local_address) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - SB_LOG(ERROR) << __FUNCTION__ << ": Invalid socket"; - return kSbSocketErrorFailed; - } - - sbposix::SockAddr sock_addr; - if (!sock_addr.FromSbSocketAddress(local_address)) { - SB_LOG(ERROR) << __FUNCTION__ << ": Invalid address"; - return (socket->error = sbposix::TranslateSocketErrno(EINVAL)); - } - - SB_DCHECK(socket->socket_fd >= 0); - if (local_address->type != socket->address_type) { - SB_LOG(ERROR) << __FUNCTION__ << ": Incompatible addresses: " - << "socket type = " << socket->address_type - << ", argument type = " << local_address->type; - return (socket->error = sbposix::TranslateSocketErrno(EAFNOSUPPORT)); - } - - // When binding to the IPV6 any address, ensure that the IPV6_V6ONLY flag is - // off to allow incoming IPV4 connections on the same socket. - // See https://www.ietf.org/rfc/rfc3493.txt for details. - if (local_address && (local_address->type == kSbSocketAddressTypeIpv6) && - starboard::common::MemoryIsZero(local_address->address, 16)) { - if (!sbposix::SetBooleanSocketOption(socket, IPPROTO_IPV6, IPV6_V6ONLY, - "IPV6_V6ONLY", false)) { - // Silently ignore errors, assume the default behavior is as expected. - socket->error = kSbSocketOk; - } - } - - int result = HANDLE_EINTR( - bind(socket->socket_fd, sock_addr.sockaddr(), sock_addr.length)); - if (result != 0) { - SB_LOG(ERROR) << __FUNCTION__ << ": Bind failed. errno=" << errno; - return (socket->error = sbposix::TranslateSocketErrno(errno)); - } - - return (socket->error = kSbSocketOk); -} diff --git a/starboard/shared/posix/socket_connect.cc b/starboard/shared/posix/socket_connect.cc deleted file mode 100644 index aa47dea3f013..000000000000 --- a/starboard/shared/posix/socket_connect.cc +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -SbSocketError SbSocketConnect(SbSocket socket, const SbSocketAddress* address) { - if (!SbSocketIsValid(socket)) { - return kSbSocketErrorFailed; - } - - sbposix::SockAddr sock_addr; - if (!sock_addr.FromSbSocketAddress(address)) { - SB_LOG(ERROR) << __FUNCTION__ << ": Invalid address"; - return (socket->error = kSbSocketErrorFailed); - } - - SB_DCHECK(socket->socket_fd >= 0); - if (address->type != socket->address_type) { - SB_LOG(ERROR) << __FUNCTION__ << ": Incompatible addresses: " - << "socket type = " << socket->address_type - << ", argument type = " << address->type; - return (socket->error = kSbSocketErrorFailed); - } - - int result = HANDLE_EINTR( - connect(socket->socket_fd, sock_addr.sockaddr(), sock_addr.length)); - if (result != 0 && errno == EINPROGRESS) { - return (socket->error = kSbSocketPending); - } - - if (result != 0) { - SB_LOG(ERROR) << __FUNCTION__ << ": connect failed: " << errno; - return (socket->error = kSbSocketErrorFailed); - } - - return (socket->error = kSbSocketOk); -} diff --git a/starboard/shared/posix/socket_create.cc b/starboard/shared/posix/socket_create.cc deleted file mode 100644 index 83d02eb0beb3..000000000000 --- a/starboard/shared/posix/socket_create.cc +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/common/socket.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/set_non_blocking_internal.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -SbSocket SbSocketCreate(SbSocketAddressType address_type, - SbSocketProtocol protocol) { - int socket_domain; - switch (address_type) { - case kSbSocketAddressTypeIpv4: - socket_domain = AF_INET; - break; - case kSbSocketAddressTypeIpv6: - socket_domain = AF_INET6; - break; - default: - SB_NOTREACHED(); - errno = EAFNOSUPPORT; - return kSbSocketInvalid; - } - - int socket_type; - int socket_protocol; - switch (protocol) { - case kSbSocketProtocolTcp: - socket_type = SOCK_STREAM; - socket_protocol = IPPROTO_TCP; - break; - case kSbSocketProtocolUdp: - socket_type = SOCK_DGRAM; - socket_protocol = IPPROTO_UDP; - break; - default: - SB_NOTREACHED(); - errno = EPROTONOSUPPORT; - return kSbSocketInvalid; - } - - int socket_fd = ::socket(socket_domain, socket_type, socket_protocol); - if (socket_fd < 0) { - return kSbSocketInvalid; - } - - // All Starboard sockets are non-blocking, so let's ensure it. - if (!sbposix::SetNonBlocking(socket_fd)) { - // Something went wrong, we'll clean up (preserving errno) and return - // failure. - int save_errno = errno; - HANDLE_EINTR(close(socket_fd)); - errno = save_errno; - return kSbSocketInvalid; - } - -#if !defined(MSG_NOSIGNAL) && defined(SO_NOSIGPIPE) - // Use SO_NOSIGPIPE to mute SIGPIPE on darwin systems. - int optval_set = 1; - setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, - reinterpret_cast(&optval_set), sizeof(int)); -#endif - - return new SbSocketPrivate(address_type, protocol, socket_fd); -} diff --git a/starboard/shared/posix/socket_destroy.cc b/starboard/shared/posix/socket_destroy.cc deleted file mode 100644 index c2640b13711b..000000000000 --- a/starboard/shared/posix/socket_destroy.cc +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/socket_internal.h" -#include "starboard/socket_waiter.h" - -bool SbSocketDestroy(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return false; - } - - SB_DCHECK(socket->socket_fd >= 0); - - if (socket->waiter != NULL) { - bool result = SbSocketWaiterRemove(socket->waiter, socket); - SB_DCHECK(result); - } - - bool result = HANDLE_EINTR(close(socket->socket_fd)) >= 0; - - delete socket; - return result; -} diff --git a/starboard/shared/posix/socket_get_local_address.cc b/starboard/shared/posix/socket_get_local_address.cc deleted file mode 100644 index d3534718c7cf..000000000000 --- a/starboard/shared/posix/socket_get_local_address.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -bool SbSocketGetLocalAddress(SbSocket socket, SbSocketAddress* out_address) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return false; - } - - SB_DCHECK(socket->socket_fd >= 0); - sbposix::SockAddr sock_addr; - int result = - getsockname(socket->socket_fd, sock_addr.sockaddr(), &sock_addr.length); - if (result < 0) { - socket->error = sbposix::TranslateSocketErrno(errno); - return false; - } - if (!sock_addr.ToSbSocketAddress(out_address)) { - socket->error = kSbSocketErrorFailed; - return false; - } - - socket->error = kSbSocketOk; - return true; -} diff --git a/starboard/shared/posix/socket_is_ipv6_supported.cc b/starboard/shared/posix/socket_is_ipv6_supported.cc deleted file mode 100644 index b65583df78dc..000000000000 --- a/starboard/shared/posix/socket_is_ipv6_supported.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2019 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/socket.h" - -bool SbSocketIsIpv6Supported() { - return true; -} diff --git a/starboard/shared/posix/socket_listen.cc b/starboard/shared/posix/socket_listen.cc deleted file mode 100644 index d1b5d3c86654..000000000000 --- a/starboard/shared/posix/socket_listen.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -SbSocketError SbSocketListen(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return kSbSocketErrorFailed; - } - - SB_DCHECK(socket->socket_fd >= 0); - // We set the backlog to SOMAXCONN to ensure that it is above 1, and high - // enough that all tests are able to pass. Some tests will fail if backlog==1 - // because they expect to be able to successfully initiate multiple connects - // at once, and then after all connects have been initiated to subsequently - // initiate corresponding accepts. -#if defined(SOMAXCONN) - const int kMaxConn = SOMAXCONN; -#else - // Some posix platforms such as FreeBSD do not define SOMAXCONN. - // In this case, set the value to an arbitrary number large enough to - // satisfy most use-cases and tests, empirically we have found that 128 - // is sufficient. All implementations of listen() specify that a backlog - // parameter larger than the system max will be silently truncated to the - // system's max. - const int kMaxConn = 128; -#endif - int result = listen(socket->socket_fd, kMaxConn); - if (result != 0) { - return (socket->error = sbposix::TranslateSocketErrno(errno)); - } - - return (socket->error = kSbSocketOk); -} diff --git a/starboard/shared/posix/socket_receive_from.cc b/starboard/shared/posix/socket_receive_from.cc deleted file mode 100644 index 8c0b779b8f8e..000000000000 --- a/starboard/shared/posix/socket_receive_from.cc +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include -#include - -#include "starboard/common/log.h" -#include "starboard/shared/posix/handle_eintr.h" -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -namespace { - -bool IsReportableErrno(int code) { - return (code != EAGAIN && code != EWOULDBLOCK && code != ECONNRESET); -} - -} // namespace - -int SbSocketReceiveFrom(SbSocket socket, - char* out_data, - int data_size, - SbSocketAddress* out_source) { - const int kRecvFlags = 0; - - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return -1; - } - - SB_DCHECK(socket->socket_fd >= 0); - if (socket->protocol == kSbSocketProtocolTcp) { - if (out_source) { - sbposix::SockAddr sock_addr; - int result = getpeername(socket->socket_fd, sock_addr.sockaddr(), - &sock_addr.length); - if (result < 0) { - SB_LOG(ERROR) << __FUNCTION__ - << ": getpeername failed, errno = " << errno; - socket->error = sbposix::TranslateSocketErrno(errno); - return -1; - } - - if (!sock_addr.ToSbSocketAddress(out_source)) { - SB_LOG(FATAL) << __FUNCTION__ << ": Bad TCP source address."; - socket->error = kSbSocketErrorFailed; - return -1; - } - } - - ssize_t bytes_read = - recv(socket->socket_fd, out_data, data_size, kRecvFlags); - if (bytes_read >= 0) { - socket->error = kSbSocketOk; - return static_cast(bytes_read); - } - - if (IsReportableErrno(errno) && - socket->error != sbposix::TranslateSocketErrno(errno)) { - SB_LOG(ERROR) << "recv failed, errno = " << errno; - } - socket->error = sbposix::TranslateSocketErrno(errno); - return -1; - } else if (socket->protocol == kSbSocketProtocolUdp) { - sbposix::SockAddr sock_addr; - ssize_t bytes_read = - recvfrom(socket->socket_fd, out_data, data_size, kRecvFlags, - sock_addr.sockaddr(), &sock_addr.length); - - if (bytes_read >= 0) { - if (out_source) { - if (!sock_addr.ToSbSocketAddress(out_source)) { - SB_LOG(FATAL) << __FUNCTION__ << ": Bad UDP source address."; - socket->error = kSbSocketErrorFailed; - return -1; - } - } - - socket->error = kSbSocketOk; - return static_cast(bytes_read); - } - - if (errno != EAGAIN && errno != EWOULDBLOCK && - socket->error != sbposix::TranslateSocketErrno(errno)) { - SB_LOG(ERROR) << "recvfrom failed, errno = " << errno; - } - socket->error = sbposix::TranslateSocketErrno(errno); - return -1; - } - - SB_NOTREACHED() << "Unrecognized protocol: " << socket->protocol; - errno = EPROTONOSUPPORT; - return -1; -} diff --git a/starboard/shared/posix/socket_set_reuse_address.cc b/starboard/shared/posix/socket_set_reuse_address.cc deleted file mode 100644 index d142d46eee0a..000000000000 --- a/starboard/shared/posix/socket_set_reuse_address.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -#include - -#include "starboard/shared/posix/socket_internal.h" - -namespace sbposix = starboard::shared::posix; - -bool SbSocketSetReuseAddress(SbSocket socket, bool value) { - return sbposix::SetBooleanSocketOption(socket, SOL_SOCKET, SO_REUSEADDR, - "SO_REUSEADDR", value); -} diff --git a/starboard/shared/stub/socket_accept.cc b/starboard/shared/stub/socket_accept.cc deleted file mode 100644 index b03587f8f05a..000000000000 --- a/starboard/shared/stub/socket_accept.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -SbSocket SbSocketAccept(SbSocket socket) { - return kSbSocketInvalid; -} diff --git a/starboard/shared/stub/socket_bind.cc b/starboard/shared/stub/socket_bind.cc deleted file mode 100644 index 69702d6804ed..000000000000 --- a/starboard/shared/stub/socket_bind.cc +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -SbSocketError SbSocketBind(SbSocket socket, - const SbSocketAddress* local_address) { - return kSbSocketErrorFailed; -} diff --git a/starboard/shared/stub/socket_connect.cc b/starboard/shared/stub/socket_connect.cc deleted file mode 100644 index 5c7dd568a543..000000000000 --- a/starboard/shared/stub/socket_connect.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -SbSocketError SbSocketConnect(SbSocket socket, const SbSocketAddress* address) { - return kSbSocketErrorFailed; -} diff --git a/starboard/shared/stub/socket_create.cc b/starboard/shared/stub/socket_create.cc deleted file mode 100644 index d563aef69d9c..000000000000 --- a/starboard/shared/stub/socket_create.cc +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -SbSocket SbSocketCreate(SbSocketAddressType address_type, - SbSocketProtocol protocol) { - return kSbSocketInvalid; -} diff --git a/starboard/shared/stub/socket_destroy.cc b/starboard/shared/stub/socket_destroy.cc deleted file mode 100644 index 577b3f01fa83..000000000000 --- a/starboard/shared/stub/socket_destroy.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -bool SbSocketDestroy(SbSocket socket) { - return false; -} diff --git a/starboard/shared/stub/socket_get_interface_address.cc b/starboard/shared/stub/socket_get_interface_address.cc deleted file mode 100644 index 6057b6ea7fed..000000000000 --- a/starboard/shared/stub/socket_get_interface_address.cc +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -bool SbSocketGetInterfaceAddress(const SbSocketAddress* const destination, - SbSocketAddress* out_source_address, - SbSocketAddress* out_netmask) { - return false; -} diff --git a/starboard/shared/stub/socket_get_local_address.cc b/starboard/shared/stub/socket_get_local_address.cc deleted file mode 100644 index c921e5c571f0..000000000000 --- a/starboard/shared/stub/socket_get_local_address.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -bool SbSocketGetLocalAddress(SbSocket socket, SbSocketAddress* out_address) { - return false; -} diff --git a/starboard/shared/stub/socket_is_ipv6_supported.cc b/starboard/shared/stub/socket_is_ipv6_supported.cc deleted file mode 100644 index d2e0fe247c71..000000000000 --- a/starboard/shared/stub/socket_is_ipv6_supported.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2019 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/socket.h" - -bool SbSocketIsIpv6Supported() { - return false; -} diff --git a/starboard/shared/stub/socket_listen.cc b/starboard/shared/stub/socket_listen.cc deleted file mode 100644 index b4b29d43e0c6..000000000000 --- a/starboard/shared/stub/socket_listen.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -SbSocketError SbSocketListen(SbSocket socket) { - return kSbSocketErrorFailed; -} diff --git a/starboard/shared/stub/socket_receive_from.cc b/starboard/shared/stub/socket_receive_from.cc deleted file mode 100644 index b8d407dc1f30..000000000000 --- a/starboard/shared/stub/socket_receive_from.cc +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -int SbSocketReceiveFrom(SbSocket socket, - char* out_data, - int data_size, - SbSocketAddress* out_source) { - return -1; -} diff --git a/starboard/shared/stub/socket_set_reuse_address.cc b/starboard/shared/stub/socket_set_reuse_address.cc deleted file mode 100644 index aa1344911a74..000000000000 --- a/starboard/shared/stub/socket_set_reuse_address.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/common/socket.h" - -bool SbSocketSetReuseAddress(SbSocket socket, bool value) { - return false; -} diff --git a/starboard/socket.h b/starboard/socket.h index 0cb8e781905f..79c0f76d7327 100644 --- a/starboard/socket.h +++ b/starboard/socket.h @@ -86,18 +86,6 @@ typedef enum SbSocketAddressType { kSbSocketAddressTypeIpv6, } SbSocketAddressType; -// Bits that can be set when calling SbSocketResolve to filter the results. -typedef enum SbSocketResolveFilter { - // No filters, include everything. - kSbSocketResolveFilterNone = 0, - - // Include Ipv4 addresses. - kSbSocketResolveFilterIpv4 = 1 << 0, - - // Include Ipv6 addresses. - kSbSocketResolveFilterIpv6 = 1 << 1, -} SbSocketResolveFilter; - // A representation of any possible supported address type. typedef struct SbSocketAddress { // The storage for the address. For IPv4, only the first 4 bytes make up the @@ -130,173 +118,6 @@ static inline bool SbSocketIsValid(SbSocket socket) { return socket != kSbSocketInvalid; } -// Returns whether IPV6 is supported on the current platform. -SB_EXPORT bool SbSocketIsIpv6Supported(); - -// DEPRECATED with SB_API_VERSION 16 -// -// Creates a new non-blocking socket for protocol |protocol| using address -// family |address_type|. -// -// - If successful, this function returns the newly created handle. -// - If unsuccessful, this function returns |kSbSocketInvalid| and also sets -// the last system error appropriately. -// -// |address_type|: The type of IP address to use for the socket. -// |protocol|: The protocol to use for the socket. -SB_EXPORT SbSocket SbSocketCreate(SbSocketAddressType address_type, - SbSocketProtocol protocol); - -// DEPRECATED with SB_API_VERSION 16 -// -// Destroys the |socket| by flushing it, closing any connection that may be -// active on it, and reclaiming any resources associated with it, including -// any registration with an |SbSocketWaiter|. -// -// The return value indicates whether the destruction was successful. However, -// even if this function returns |false|, you should not be able to use the -// socket any more. -// -// |socket|: The SbSocket to be destroyed. -SB_EXPORT bool SbSocketDestroy(SbSocket socket); - -// DEPRECATED with SB_API_VERSION 16 -// -// Opens a connection of |socket|'s type to the host and port specified by -// |address|. This function sets and returns the socket error if it is unable -// to connect to |address|. (It returns |kSbSocketOk| if it creates the -// connection successfully.) -// -// |socket|: The type of connection that should be opened. -// |address|: The host and port to which the socket should connect. -SB_EXPORT SbSocketError SbSocketConnect(SbSocket socket, - const SbSocketAddress* address); - -// DEPRECATED with SB_API_VERSION 16 -// -// Binds |socket| to a specific local interface and port specified by -// |local_address|. This function sets and returns the socket error if it -// is unable to bind to |local_address|. -// -// |socket|: The SbSocket to be bound to the local interface. -// |local_address|: The local address to which the socket is to be bound. -// This value must not be |NULL|. -// - Setting the local address to port |0| (or not specifying a port) indicates -// that the function should choose a port for you. -// - Setting the IP address to |0.0.0.0| means that the socket should be bound -// to all interfaces. -SB_EXPORT SbSocketError SbSocketBind(SbSocket socket, - const SbSocketAddress* local_address); - -// DEPRECATED with SB_API_VERSION 16 -// -// Causes |socket| to listen on the local address that |socket| was previously -// bound to by SbSocketBind. This function sets and returns the socket error if -// it is unable to listen for some reason. (It returns |kSbSocketOk| if it -// creates the connection successfully.) -// -// |socket|: The SbSocket on which the function operates. -SB_EXPORT SbSocketError SbSocketListen(SbSocket socket); - -// DEPRECATED with SB_API_VERSION 16 -// -// Accepts a pending connection on |socket| and returns a new SbSocket -// representing that connection. This function sets the error on |socket| -// and returns |kSbSocketInvalid| if it is unable to accept a new connection. -// -// |socket|: The SbSocket that is accepting a pending connection. -SB_EXPORT SbSocket SbSocketAccept(SbSocket socket); - -// DEPRECATED with SB_API_VERSION 16 -// -// Gets the address that this socket is bound to locally, if the socket is -// connected. The return value indicates whether the address was retrieved -// successfully. -// -// |socket|: The SbSocket for which the local address is retrieved. -// |out_address|: The SbSocket's local address. -SB_EXPORT bool SbSocketGetLocalAddress(SbSocket socket, - SbSocketAddress* out_address); -// DEPRECATED with SB_API_VERSION 16 -// -// Gets the source address and the netmask that would be used to connect to the -// destination. The netmask parameter is optional, and only populated if a -// non-NULL parameter is passed in. To determine which source IP will be used, -// the kernel takes into account the protocol, routes, destination -// ip, etc. The subnet mask, aka netmask, is used to find the routing prefix. -// In IPv6, this should be derived from the prefix value. -// -// Returns whether it was possible to determine the source address and the -// netmask (if non-NULL value is passed) to be used to connect to the -// destination. This function could fail if the destination is not reachable, -// if it an invalid address, etc. -// -// |destination|: The destination IP to be connected to. If IP addresses is not -// 0.0.0.0 or ::, then temporary addresses may be returned. -// -// If the destination address is 0.0.0.0, and its |type| is -// |kSbSocketAddressTypeIpv4|, then any IPv4 local interface that is up and not -// a loopback interface is a valid return value. -// -// If the destination address is ::, and its |type| is -// |kSbSocketAddressTypeIpv6| then any IPv6 local interface that is up and not -// loopback or a link-local IP is a valid return value. However, in the case of -// IPv6, the address with the biggest scope must be returned. E.g., a globally -// scoped and routable IP is preferred over a unique local address (ULA). Also, -// the IP address that is returned must be permanent. -// -// If destination address is NULL, then any IP address that is valid for -// |destination| set to 0.0.0.0 (IPv4) or :: (IPv6) can be returned. -// -// |out_source_address|: This function places the address of the local interface -// in this output variable. -// |out_netmask|: This parameter is optional. If a non-NULL value is passed in, -// this function places the netmask associated with the source address in this -// output variable. -SB_EXPORT bool SbSocketGetInterfaceAddress( - const SbSocketAddress* const destination, - SbSocketAddress* out_source_address, - SbSocketAddress* out_netmask); - -// DEPRECATED with SB_API_VERSION 16 -// -// Reads up to |data_size| bytes from |socket| into |out_data| and places the -// source address of the packet in |out_source| if out_source is not NULL. -// Returns the number of bytes read, or a negative number if there is an error, -// in which case SbSocketGetLastError can provide the precise error encountered. -// -// Note that this function is NOT specified to make a best effort to read all -// data on all platforms, but it MAY still do so. It is specified to read -// however many bytes are available conveniently, meaning that it should avoid -// blocking until there is data. It can be run in a loop until -// SbSocketGetLastError returns |kSbSocketPending| to make it a best-effort -// read (but still only up to not blocking, unless you want to spin). -// -// The primary use of |out_source| is to receive datagram packets from -// multiple sources on a UDP server socket. TCP has two endpoints connected -// persistently, so the address is unnecessary, but allowed. -// -// |socket|: The SbSocket from which data is read. -// |out_data|: The data read from the socket. Must not be NULL. -// |data_size|: The number of bytes to read. -// |out_source|: The source address of the packet. -SB_EXPORT int SbSocketReceiveFrom(SbSocket socket, - char* out_data, - int data_size, - SbSocketAddress* out_source); - -// DEPRECATED with SB_API_VERSION 16 -// -// Sets the |SO_REUSEADDR|, or equivalent, option to |value| on |socket|. -// The return value indicates whether the option was actually set. -// -// This option allows a bound address to be reused if a socket isn't actively -// bound to it. -// -// |socket|: The SbSocket for which the option is set. -// |value|: The new value for the option. -SB_EXPORT bool SbSocketSetReuseAddress(SbSocket socket, bool value); - #ifdef __cplusplus } // extern "C" #endif diff --git a/starboard/stub/BUILD.gn b/starboard/stub/BUILD.gn index f012831ac95e..d619516b8234 100644 --- a/starboard/stub/BUILD.gn +++ b/starboard/stub/BUILD.gn @@ -101,17 +101,6 @@ static_library("stub_sources") { "//starboard/shared/stub/player_set_volume.cc", "//starboard/shared/stub/player_write_end_of_stream.cc", "//starboard/shared/stub/player_write_samples.cc", - "//starboard/shared/stub/socket_accept.cc", - "//starboard/shared/stub/socket_bind.cc", - "//starboard/shared/stub/socket_connect.cc", - "//starboard/shared/stub/socket_create.cc", - "//starboard/shared/stub/socket_destroy.cc", - "//starboard/shared/stub/socket_get_interface_address.cc", - "//starboard/shared/stub/socket_get_local_address.cc", - "//starboard/shared/stub/socket_is_ipv6_supported.cc", - "//starboard/shared/stub/socket_listen.cc", - "//starboard/shared/stub/socket_receive_from.cc", - "//starboard/shared/stub/socket_set_reuse_address.cc", "//starboard/shared/stub/socket_waiter_add.cc", "//starboard/shared/stub/socket_waiter_create.cc", "//starboard/shared/stub/socket_waiter_destroy.cc", From d3ff9e6a2b1c49baafb3f3cdf9f7e6dc3b190a35 Mon Sep 17 00:00:00 2001 From: Madhura Jayaraman Date: Tue, 19 Nov 2024 12:06:47 -0800 Subject: [PATCH 2/4] Local changes --- cobalt/network/dial/dial_http_server.cc | 8 -------- cobalt/network/local_network.cc | 3 --- net/server/http_server.cc | 7 ------- 3 files changed, 18 deletions(-) diff --git a/cobalt/network/dial/dial_http_server.cc b/cobalt/network/dial/dial_http_server.cc index 87743c40168b..8ce5b0ec3025 100644 --- a/cobalt/network/dial/dial_http_server.cc +++ b/cobalt/network/dial/dial_http_server.cc @@ -72,11 +72,6 @@ absl::optional GetLocalIpAddress() { SbSocketAddress destination; memset(&(destination.address), 0, sizeof(destination.address)); destination.type = address_types; - if (!SbSocketGetInterfaceAddress(&destination, &local_ip, NULL) || - !ip_addr.FromSbSocketAddress(&local_ip)) { - DLOG(WARNING) << "Unable to get a local interface address."; - return absl::nullopt; - } return ip_addr; } @@ -134,9 +129,6 @@ int DialHttpServer::GetLocalAddress(net::IPEndPoint* addr) { // DIAL only works with IPv4. destination.type = kSbSocketAddressTypeIpv4; - if (!SbSocketGetInterfaceAddress(&destination, &local_ip, NULL)) { - return net::ERR_FAILED; - } local_ip.port = addr->port(); LOG(INFO) << "In-App DIAL Address http://" << addr->address().ToString() << ":" << addr->port(); diff --git a/cobalt/network/local_network.cc b/cobalt/network/local_network.cc index 1fd2e2bea00b..34ca40370ac5 100644 --- a/cobalt/network/local_network.cc +++ b/cobalt/network/local_network.cc @@ -60,9 +60,6 @@ bool IsLocalIP(const SbSocketAddress& ip, const SbSocketAddress& source_address, bool IsIPInLocalNetwork(const SbSocketAddress& destination) { SbSocketAddress source_address; SbSocketAddress netmask; - if (!(SbSocketGetInterfaceAddress(&destination, &source_address, &netmask))) { - return false; - } return IsLocalIP(destination, source_address, netmask); } diff --git a/net/server/http_server.cc b/net/server/http_server.cc index 2dc2fd166000..0534cbc784c6 100644 --- a/net/server/http_server.cc +++ b/net/server/http_server.cc @@ -172,13 +172,6 @@ int HttpServer::GetLocalInterfaceAddress(IPEndPoint* address) { SbSocketAddress interface_address; // Prefer to report the interface's IPv4 address. any_ip.type = kSbSocketAddressTypeIpv4; - if (!SbSocketGetInterfaceAddress(&any_ip, &interface_address, nullptr)) { - any_ip.type = kSbSocketAddressTypeIpv6; - if (!SbSocketGetInterfaceAddress(&any_ip, &interface_address, nullptr)) { - DLOG(ERROR) << "Error getting interface address."; - return ERR_FAILED; - } - } interface_address.port = address->port(); if (!address->FromSbSocketAddress(&interface_address)) { DLOG(ERROR) << "Error converting socket address."; From 0b48dd2d0ba43cd5f03f31884403a8430147c8e8 Mon Sep 17 00:00:00 2001 From: Madhura Jayaraman Date: Tue, 19 Nov 2024 14:32:39 -0800 Subject: [PATCH 3/4] Delete socket_tracker --- .../shared/starboard/socket/socket_tracker.cc | 252 ------------------ .../shared/starboard/socket/socket_tracker.h | 114 -------- 2 files changed, 366 deletions(-) delete mode 100644 starboard/shared/starboard/socket/socket_tracker.cc delete mode 100644 starboard/shared/starboard/socket/socket_tracker.h diff --git a/starboard/shared/starboard/socket/socket_tracker.cc b/starboard/shared/starboard/socket/socket_tracker.cc deleted file mode 100644 index eddd4cc064cd..000000000000 --- a/starboard/shared/starboard/socket/socket_tracker.cc +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright 2018 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "starboard/shared/starboard/socket/socket_tracker.h" - -#include -#include - -#include "starboard/common/log.h" -#include "starboard/common/time.h" - -namespace starboard { -namespace shared { -namespace starboard { -namespace socket { - -// static -SocketTracker* SocketTracker::GetInstance() { - static SocketTracker s_socket_tracker; - return &s_socket_tracker; -} - -void SocketTracker::OnCreate(SbSocket socket, - SbSocketAddressType address_type, - SbSocketProtocol protocol) { - SocketRecord record; - record.thread_id = SbThreadGetId(); - record.address_type = address_type; - record.protocol = protocol; - record.last_activity = CurrentMonotonicTime(); - record.state = kCreated; - - ScopedLock scoped_lock(mutex_); - sockets_[socket] = record; -} - -void SocketTracker::OnDestroy(SbSocket socket) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - if (iter == sockets_.end()) { - return; - } - sockets_.erase(iter); -} - -void SocketTracker::OnConnect(SbSocket socket, - const SbSocketAddress& remote_address) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - iter->second.remote_address = remote_address; - SB_DCHECK(iter->second.state == kCreated); - iter->second.state = kConnected; -} - -void SocketTracker::OnBind(SbSocket socket, - const SbSocketAddress& local_address) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - iter->second.local_address = local_address; -} - -void SocketTracker::OnAccept(SbSocket listening_socket, - SbSocket accepted_socket) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(listening_socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - - SocketRecord record; - record.thread_id = SbThreadGetId(); - record.address_type = iter->second.address_type; - record.protocol = iter->second.protocol; - record.last_activity = iter->second.last_activity; - record.local_address = iter->second.local_address; - record.state = kConnected; - - sockets_[accepted_socket] = record; -} - -void SocketTracker::OnReceiveFrom(SbSocket socket, - const SbSocketAddress* source) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - if (source) { - iter->second.remote_address = *source; - } -} - -void SocketTracker::OnSendTo(SbSocket socket, - const SbSocketAddress* destination) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - if (destination) { - iter->second.remote_address = *destination; - } -} - -void SocketTracker::OnResolve(const char* hostname, - const SbSocketResolution& resolution) { - ScopedLock scoped_lock(mutex_); - for (int i = 0; i < resolution.address_count; ++i) { - socket_resolutions_[resolution.addresses[i]] = hostname; - } -} - -bool SocketTracker::IsSocketTracked(SbSocket socket) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - return iter != sockets_.end(); -} - -const optional& SocketTracker::GetLocalAddress( - SbSocket socket) const { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - return iter->second.local_address; -} - -void SocketTracker::OnListen(SbSocket socket) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - SB_DCHECK(iter->second.state == kCreated); - iter->second.state = kListened; -} - -SocketTracker::State SocketTracker::GetState(SbSocket socket) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - return iter->second.state; -} - -void SocketTracker::OnAddWaiter(SbSocket socket, SbSocketWaiter waiter) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - SB_DCHECK(!SbSocketWaiterIsValid(iter->second.waiter)); - SB_DCHECK(SbSocketWaiterIsValid(waiter)); - iter->second.waiter = waiter; -} - -void SocketTracker::OnRemoveWaiter(SbSocket socket, SbSocketWaiter waiter) { - ScopedLock scoped_lock(mutex_); - auto iter = sockets_.find(socket); - SB_DCHECK(iter != sockets_.end()); - - iter->second.last_activity = CurrentMonotonicTime(); - SB_DCHECK(waiter == iter->second.waiter); - SB_DCHECK(SbSocketWaiterIsValid(waiter)); - iter->second.waiter = kSbSocketWaiterInvalid; -} - -void SocketTracker::LogTrackedSockets() { - ScopedLock scoped_lock(mutex_); - SB_LOG(INFO) << "Total tracked sockets: " << sockets_.size(); - for (auto iter : sockets_) { - SB_LOG(INFO) << ConvertToString_Locked(iter.second); - } -} - -std::string SocketTracker::ConvertToString_Locked( - SbSocketAddress address) const { - mutex_.DCheckAcquired(); - - std::stringstream ss; - ss << address; - - address.port = 0; - auto iter = socket_resolutions_.find(address); - if (iter != socket_resolutions_.end()) { - ss << "(" << iter->second << ")"; - } - return ss.str(); -} - -std::multimap -SocketTracker::ComputeIdleTimePerSocketForThreadId(SbThreadId thread_id) { - std::multimap idle_times_to_sockets; - for (auto it = sockets_.begin(); it != sockets_.end(); ++it) { - if (it->second.thread_id != thread_id || it->second.state == kListened) { - continue; - } - int64_t time_idle = ComputeTimeIdle(it->second); - idle_times_to_sockets.insert(std::make_pair(time_idle, it->first)); - } - return idle_times_to_sockets; -} - -std::string SocketTracker::ConvertToString_Locked( - const SocketRecord& record) const { - mutex_.DCheckAcquired(); - - std::stringstream ss; - ss << (record.protocol == kSbSocketProtocolTcp ? "TCP/" : "UDP/"); - ss << (record.address_type == kSbSocketAddressTypeIpv4 ? "Ipv4" : "Ipv6"); - if (record.local_address) { - ss << " local addr: " << ConvertToString_Locked(*record.local_address); - } - if (record.remote_address) { - ss << " remote addr: " << ConvertToString_Locked(*record.remote_address); - } - switch (record.state) { - case kCreated: - ss << ", created,"; - break; - case kConnected: - ss << ", connect called,"; - break; - case kListened: - ss << ", listen called,"; - break; - } - ss << " has been idle for " << ComputeTimeIdle(record) * 1.0f / 1'000'000LL - << " seconds"; - return ss.str(); -} - -// static -int64_t SocketTracker::ComputeTimeIdle(const SocketRecord& record) { - return CurrentMonotonicTime() - record.last_activity; -} - -} // namespace socket -} // namespace starboard -} // namespace shared -} // namespace starboard diff --git a/starboard/shared/starboard/socket/socket_tracker.h b/starboard/shared/starboard/socket/socket_tracker.h deleted file mode 100644 index e78728f0ca00..000000000000 --- a/starboard/shared/starboard/socket/socket_tracker.h +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2018 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef STARBOARD_SHARED_STARBOARD_SOCKET_SOCKET_TRACKER_H_ -#define STARBOARD_SHARED_STARBOARD_SOCKET_SOCKET_TRACKER_H_ - -#include -#include -#include - -#include "starboard/common/mutex.h" -#include "starboard/common/optional.h" -#include "starboard/common/socket.h" -#include "starboard/configuration.h" -#include "starboard/socket_waiter.h" -#include "starboard/thread.h" - -// TODO: Move this to starboard/socket.h. -inline bool operator<(const SbSocketAddress& left, - const SbSocketAddress& right) { - if (left.type < right.type) { - return true; - } - if (left.type > right.type) { - return false; - } - - int bytes_to_compare = left.type == kSbSocketAddressTypeIpv4 ? 4 : 16; - int result = memcmp(left.address, right.address, bytes_to_compare); - if (result < 0) { - return true; - } - if (result > 0) { - return false; - } - - return left.port < right.port; -} - -namespace starboard { -namespace shared { -namespace starboard { -namespace socket { - -class SocketTracker { - public: - static SocketTracker* GetInstance(); - - void OnCreate(SbSocket socket, - SbSocketAddressType address_type, - SbSocketProtocol protocol); - void OnDestroy(SbSocket socket); - void OnConnect(SbSocket socket, const SbSocketAddress& remote_address); - void OnBind(SbSocket socket, const SbSocketAddress& local_address); - void OnAccept(SbSocket listening_socket, SbSocket accepted_socket); - void OnReceiveFrom(SbSocket socket, const SbSocketAddress* source); - void OnSendTo(SbSocket socket, const SbSocketAddress* destination); - void OnResolve(const char* hostname, const SbSocketResolution& resolution); - bool IsSocketTracked(SbSocket socket); - const optional& GetLocalAddress(SbSocket socket) const; - void OnListen(SbSocket socket); - void OnAddWaiter(SbSocket socket, SbSocketWaiter waiter); - void OnRemoveWaiter(SbSocket socket, SbSocketWaiter waiter); - - enum State { - kCreated, - kConnected, - kListened, - }; - - State GetState(SbSocket socket); - - void LogTrackedSockets(); - std::multimap ComputeIdleTimePerSocketForThreadId( - SbThreadId thread_id); - - private: - struct SocketRecord { - SbSocketAddressType address_type; - SbSocketProtocol protocol; - optional local_address; - optional remote_address; - int64_t last_activity; - SbSocketWaiter waiter = kSbSocketWaiterInvalid; - SbThreadId thread_id; - State state; - }; - - std::string ConvertToString_Locked(SbSocketAddress address) const; - std::string ConvertToString_Locked(const SocketRecord& record) const; - static int64_t ComputeTimeIdle(const SocketRecord& record); - - Mutex mutex_; - std::map sockets_; - std::map socket_resolutions_; -}; - -} // namespace socket -} // namespace starboard -} // namespace shared -} // namespace starboard - -#endif // STARBOARD_SHARED_STARBOARD_SOCKET_SOCKET_TRACKER_H_ From 4921d6da3a1eb11b549653fdde45c67dcdaaf5e5 Mon Sep 17 00:00:00 2001 From: Madhura Jayaraman Date: Tue, 19 Nov 2024 14:34:33 -0800 Subject: [PATCH 4/4] Remove socket and socket waiter headers --- starboard/android/shared/BUILD.gn | 11 +- starboard/common/socket.cc | 44 +---- starboard/common/socket.h | 57 +------ starboard/common/socket_test.cc | 3 +- starboard/elf_loader/exported_symbols.cc | 5 +- starboard/linux/shared/BUILD.gn | 1 + starboard/nplb/socket_helpers.h | 28 ---- starboard/raspi/shared/BUILD.gn | 2 +- .../shared/libevent/socket_waiter_add.cc | 29 ---- .../shared/libevent/socket_waiter_internal.cc | 157 +----------------- .../shared/libevent/socket_waiter_internal.h | 39 +---- .../shared/libevent/socket_waiter_remove.cc | 7 - starboard/shared/posix/socket_internal.cc | 71 -------- starboard/shared/posix/socket_internal.h | 44 ----- .../shared/posix/socket_is_ipv6_supported.cc | 19 +++ starboard/shared/starboard/link_receiver.cc | 30 ++++ .../shared/stub/socket_is_ipv6_supported.cc | 19 +++ starboard/shared/stub/socket_waiter_add.cc | 9 - starboard/shared/stub/socket_waiter_remove.cc | 4 - starboard/socket.h | 45 +---- starboard/socket_waiter.h | 20 --- starboard/stub/BUILD.gn | 1 + 22 files changed, 93 insertions(+), 552 deletions(-) create mode 100644 starboard/shared/posix/socket_is_ipv6_supported.cc create mode 100644 starboard/shared/stub/socket_is_ipv6_supported.cc diff --git a/starboard/android/shared/BUILD.gn b/starboard/android/shared/BUILD.gn index 65dd7b55c8d9..8b65c88484aa 100644 --- a/starboard/android/shared/BUILD.gn +++ b/starboard/android/shared/BUILD.gn @@ -86,7 +86,7 @@ static_library("starboard_platform") { "//starboard/shared/posix/memory_free_aligned.cc", "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", - "//starboard/shared/posix/socket_internal.cc", + "//starboard/shared/posix/socket_is_ipv6_supported.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", @@ -278,11 +278,10 @@ static_library("starboard_platform") { "posix_emu/pthread.cc", "posix_emu/stat.cc", "sanitizer_options.cc", - - #"speech_synthesis_cancel.cc", - #"speech_synthesis_internal.cc", - #"speech_synthesis_is_supported.cc", - #"speech_synthesis_speak.cc", + "speech_synthesis_cancel.cc", + "speech_synthesis_internal.cc", + "speech_synthesis_is_supported.cc", + "speech_synthesis_speak.cc", "system_get_extensions.cc", "system_get_locale_id.cc", "system_get_path.cc", diff --git a/starboard/common/socket.cc b/starboard/common/socket.cc index 5168ca764a84..c625ea37b9ff 100644 --- a/starboard/common/socket.cc +++ b/starboard/common/socket.cc @@ -19,49 +19,7 @@ #include "starboard/common/log.h" #include "starboard/configuration.h" -namespace starboard { - -SbSocketAddress GetUnspecifiedAddress(SbSocketAddressType address_type, - int port) { - SbSocketAddress address = {}; - address.type = address_type; - address.port = port; - return address; -} - -bool GetLocalhostAddress(SbSocketAddressType address_type, - int port, - SbSocketAddress* address) { - if (address_type != kSbSocketAddressTypeIpv4 && - address_type != kSbSocketAddressTypeIpv6) { - SB_LOG(ERROR) << __FUNCTION__ << ": unknown address type: " << address_type; - return false; - } - *address = GetUnspecifiedAddress(address_type, port); - switch (address_type) { - case kSbSocketAddressTypeIpv4: - address->address[0] = 127; - address->address[3] = 1; - break; - case kSbSocketAddressTypeIpv6: - address->address[15] = 1; - break; - } - - return true; -} - -bool Socket::IsValid() { - return SbSocketIsValid(socket_); -} - -SbSocket Socket::socket() { - return socket_; -} - -Socket::Socket(SbSocket socket) : socket_(socket) {} - -} // namespace starboard +namespace starboard {} // namespace starboard std::ostream& operator<<(std::ostream& os, const SbSocketAddress& address) { if (address.type == kSbSocketAddressTypeIpv6) { diff --git a/starboard/common/socket.h b/starboard/common/socket.h index 3e877f09aec6..72fe39f9cb30 100644 --- a/starboard/common/socket.h +++ b/starboard/common/socket.h @@ -25,62 +25,7 @@ #include "starboard/socket.h" #include "starboard/types.h" -namespace starboard { - -// Returns an IP unspecified address with the given port. -SbSocketAddress GetUnspecifiedAddress(SbSocketAddressType address_type, - int port); - -// Gets an IP localhost address with the given port. -// Returns true if it was successful. -bool GetLocalhostAddress(SbSocketAddressType address_type, - int port, - SbSocketAddress* address); - -class Socket { - public: - Socket(SbSocketAddressType address_type, SbSocketProtocol protocol); - explicit Socket(SbSocketAddressType address_type); - explicit Socket(SbSocketProtocol protocol); - Socket(); - ~Socket(); - bool IsValid(); - - SbSocketError Connect(const SbSocketAddress* address); - SbSocketError Bind(const SbSocketAddress* local_address); - SbSocketError Listen(); - Socket* Accept(); - - bool IsConnected(); - bool IsConnectedAndIdle(); - bool IsPending(); - - SbSocketError GetLastError(); - void ClearLastError(); - - int ReceiveFrom(char* out_data, int data_size, SbSocketAddress* out_source); - int SendTo(const char* data, - int data_size, - const SbSocketAddress* destination); - - bool GetLocalAddress(SbSocketAddress* out_address); - bool SetBroadcast(bool value); - bool SetReuseAddress(bool value); - bool SetReceiveBufferSize(int32_t size); - bool SetSendBufferSize(int32_t size); - bool SetTcpKeepAlive(bool value, int64_t period); // period in microseconds. - bool SetTcpNoDelay(bool value); - bool SetTcpWindowScaling(bool value); - - SbSocket socket(); - - private: - explicit Socket(SbSocket socket); - - SbSocket socket_; -}; - -} // namespace starboard +namespace starboard {} // namespace starboard // Let SbSocketAddresses be output to log streams. std::ostream& operator<<(std::ostream& os, const SbSocketAddress& address); diff --git a/starboard/common/socket_test.cc b/starboard/common/socket_test.cc index 7d416e299131..6d9ab7aebb05 100644 --- a/starboard/common/socket_test.cc +++ b/starboard/common/socket_test.cc @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - +#if 0 #include "starboard/common/socket.h" #include "starboard/common/log.h" @@ -59,3 +59,4 @@ TEST(SocketTest, TestGetLocalhostAddressInvalidType) { } } // namespace } // namespace starboard +#endif diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index df3d4bd8d8f7..a219be8f62ef 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -147,10 +147,11 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbPlayerSetVolume); REGISTER_SYMBOL(SbPlayerWriteEndOfStream); REGISTER_SYMBOL(SbPlayerWriteSamples); - REGISTER_SYMBOL(SbSocketWaiterAdd); + REGISTER_SYMBOL(SbSocketIsIpv6Supported); + // REGISTER_SYMBOL(SbSocketWaiterAdd); REGISTER_SYMBOL(SbSocketWaiterCreate); REGISTER_SYMBOL(SbSocketWaiterDestroy); - REGISTER_SYMBOL(SbSocketWaiterRemove); + // REGISTER_SYMBOL(SbSocketWaiterRemove); REGISTER_SYMBOL(SbSocketWaiterWait); REGISTER_SYMBOL(SbSocketWaiterWaitTimed); REGISTER_SYMBOL(SbSocketWaiterWakeUp); diff --git a/starboard/linux/shared/BUILD.gn b/starboard/linux/shared/BUILD.gn index 902a649cbc9c..10801f2a78f2 100644 --- a/starboard/linux/shared/BUILD.gn +++ b/starboard/linux/shared/BUILD.gn @@ -156,6 +156,7 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", "//starboard/shared/posix/socket_internal.cc", + "//starboard/shared/posix/socket_is_ipv6_supported.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", diff --git a/starboard/nplb/socket_helpers.h b/starboard/nplb/socket_helpers.h index 56d641f941bf..c5881fe246ad 100644 --- a/starboard/nplb/socket_helpers.h +++ b/starboard/nplb/socket_helpers.h @@ -64,34 +64,6 @@ static inline void TimedWaitShouldBlock(SbSocketWaiter waiter, EXPECT_LE(timeout, TimedWaitTimed(waiter, timeout)); } -// Socket operations may return specific (e.g. kSbSocketErrorConnectionReset) or -// general (e.g. kSbSocketErrorFailed) error codes, and while in some cases -// it may be important that we obtain a specific error message, in other cases -// it will just be used as a hint and so these methods are provided to make -// it easy to test against specific or general errors. -static inline bool SocketErrorIn( - SbSocketError error, - const std::vector& expected_set) { - for (size_t i = 0; i < expected_set.size(); ++i) { - if (expected_set[i] == error) { - return true; - } - } - return false; -} - -#define EXPECT_SB_SOCKET_ERROR_IN(error, ...) \ - do { \ - EXPECT_TRUE(SocketErrorIn(error, {__VA_ARGS__})) \ - << "With " #error " = " << error; \ - } while (false) - -#define EXPECT_SB_SOCKET_ERROR_IS_ERROR(error) \ - do { \ - EXPECT_FALSE(SocketErrorIn(error, {kSbSocketOk, kSbSocketPending})) \ - << "With " #error " = " << error; \ - } while (false) - } // namespace nplb } // namespace starboard diff --git a/starboard/raspi/shared/BUILD.gn b/starboard/raspi/shared/BUILD.gn index d8eefcf50110..ce45bf6644ce 100644 --- a/starboard/raspi/shared/BUILD.gn +++ b/starboard/raspi/shared/BUILD.gn @@ -113,7 +113,7 @@ static_library("starboard_platform_sources") { "//starboard/shared/posix/memory_free_aligned.cc", "//starboard/shared/posix/page_internal.cc", "//starboard/shared/posix/set_non_blocking_internal.cc", - "//starboard/shared/posix/socket_internal.cc", + "//starboard/shared/posix/socket_is_ipv6_supported.cc", "//starboard/shared/posix/storage_write_record.cc", "//starboard/shared/posix/system_break_into_debugger.cc", "//starboard/shared/posix/system_clear_last_error.cc", diff --git a/starboard/shared/libevent/socket_waiter_add.cc b/starboard/shared/libevent/socket_waiter_add.cc index 1155b3a8c962..2e964c85fd32 100644 --- a/starboard/shared/libevent/socket_waiter_add.cc +++ b/starboard/shared/libevent/socket_waiter_add.cc @@ -18,35 +18,6 @@ #include "starboard/shared/libevent/socket_waiter_internal.h" #include "starboard/socket.h" -bool SbSocketWaiterAdd(SbSocketWaiter waiter, - SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent) { - if (!SbSocketWaiterIsValid(waiter)) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Waiter (" << waiter << ") is invalid."; - return false; - } - - if (!SbSocketIsValid(socket)) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid."; - return false; - } - - if (!callback) { - SB_DLOG(ERROR) << __FUNCTION__ << ": No callback provided."; - return false; - } - - if (!interests) { - SB_DLOG(ERROR) << __FUNCTION__ << ": No interests provided."; - return false; - } - - return waiter->Add(socket, context, callback, interests, persistent); -} - bool SbPosixSocketWaiterAdd(SbSocketWaiter waiter, int socket, void* context, diff --git a/starboard/shared/libevent/socket_waiter_internal.cc b/starboard/shared/libevent/socket_waiter_internal.cc index 53c2660f7117..3d2ac81e8bab 100644 --- a/starboard/shared/libevent/socket_waiter_internal.cc +++ b/starboard/shared/libevent/socket_waiter_internal.cc @@ -80,13 +80,6 @@ SbSocketWaiterPrivate::~SbSocketWaiterPrivate() { Remove(waitee->i_socket, waitee->waiter); } - sb_WaiteesMap::iterator it2 = sb_waitees_.begin(); - while (it2 != sb_waitees_.end()) { - Waitee* waitee = it2->second; - ++it2; // Increment before removal. - Remove(waitee->sb_socket, waitee->waiter); - } - event_del(&wakeup_event_); event_base_free(base_); @@ -191,106 +184,6 @@ bool SbSocketWaiterPrivate::CheckSocketRegistered(int socket) { return true; } -bool SbSocketWaiterPrivate::Add(SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent) { - SB_DCHECK(pthread_equal(pthread_self(), thread_)); - - if (!SbSocketIsValid(socket)) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid."; - return false; - } - - if (!interests) { - SB_DLOG(ERROR) << __FUNCTION__ << ": No interests provided."; - return false; - } - - // The policy is not to add a socket to a waiter if it is registered with - // another waiter. - - // TODO: If anyone were to want to add a socket to a different waiter, - // it would probably be another thread, so doing this check without locking is - // probably wrong. But, it is also a pain, and, at this precise moment, socket - // access is all going to come from one I/O thread anyway, and there will only - // be one waiter. - if (SbSocketWaiterIsValid(socket->waiter)) { - if (socket->waiter == this) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket already has this waiter (" - << this << ")."; - } else { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket already has waiter (" - << socket->waiter << ", this=" << this << ")."; - } - return false; - } - - Waitee* waitee = - new Waitee(this, socket, context, callback, interests, persistent); - AddWaitee(waitee); - - int16_t events = 0; - if (interests & kSbSocketWaiterInterestRead) { - events |= EV_READ; - } - - if (interests & kSbSocketWaiterInterestWrite) { - events |= EV_WRITE; - } - - if (persistent) { - events |= EV_PERSIST; - } - - event_set(&waitee->event, socket->socket_fd, events, - &SbSocketWaiterPrivate::LibeventSocketCallback, waitee); - event_base_set(base_, &waitee->event); - socket->waiter = this; - event_add(&waitee->event, NULL); - return true; -} - -bool SbSocketWaiterPrivate::Remove(SbSocket socket, SbSocketWaiter waiter) { - SB_DCHECK(pthread_equal(pthread_self(), thread_)); - if (!SbSocketIsValid(socket)) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid."; - return false; - } - - if (socket->waiter != this) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") " - << "is watched by Waiter (" << socket->waiter << "), " - << "not this Waiter (" << this << ")."; - SB_DSTACK(ERROR); - return false; - } - - Waitee* waitee = RemoveWaitee(socket); - if (!waitee) { - return false; - } - - event_del(&waitee->event); - socket->waiter = kSbSocketWaiterInvalid; - - delete waitee; - return true; -} - -bool SbSocketWaiterPrivate::CheckSocketRegistered(SbSocket socket) { - if (!SbSocketIsValid(socket)) { - SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid."; - return false; - } - if (GetWaitee(socket) == NULL) { - return false; - } - - return true; -} - void SbSocketWaiterPrivate::Wait() { SB_DCHECK(pthread_equal(pthread_self(), thread_)); @@ -379,23 +272,13 @@ void SbSocketWaiterPrivate::HandleSignal(Waitee* waitee, // Remove the non-persistent waitee before calling the callback, so that we // can add another waitee in the callback if we need to. This is also why we // copy all the fields we need out of waitee. - if (waitee->use_int_socket == 1) { - int socket = waitee->i_socket; - void* context = waitee->context; - SbPosixSocketWaiterCallback callback = waitee->i_callback; - if (!waitee->persistent) { - Remove(waitee->i_socket, waitee->waiter); - } - callback(this, socket, context, interests); - } else { - SbSocket socket = waitee->sb_socket; - void* context = waitee->context; - SbSocketWaiterCallback callback = waitee->sb_callback; - if (!waitee->persistent) { - Remove(waitee->sb_socket, waitee->waiter); - } - callback(this, socket, context, interests); + int socket = waitee->i_socket; + void* context = waitee->context; + SbPosixSocketWaiterCallback callback = waitee->i_callback; + if (!waitee->persistent) { + Remove(waitee->i_socket, waitee->waiter); } + callback(this, socket, context, interests); } void SbSocketWaiterPrivate::HandleWakeUpRead() { @@ -411,11 +294,7 @@ void SbSocketWaiterPrivate::HandleWakeUpRead() { } void SbSocketWaiterPrivate::AddWaitee(Waitee* waitee) { - if (waitee->use_int_socket == 1) { - i_waitees_.insert(std::make_pair(waitee->i_socket, waitee)); - } else { - sb_waitees_.insert(std::make_pair(waitee->sb_socket, waitee)); - } + i_waitees_.insert(std::make_pair(waitee->i_socket, waitee)); } SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::GetWaitee(int socket) { @@ -427,16 +306,6 @@ SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::GetWaitee(int socket) { return it->second; } -SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::GetWaitee( - SbSocket socket) { - sb_WaiteesMap::iterator it = sb_waitees_.find(socket); - if (it == sb_waitees_.end()) { - return NULL; - } - - return it->second; -} - SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::RemoveWaitee(int socket) { i_WaiteesMap::iterator it = i_waitees_.find(socket); if (it == i_waitees_.end()) { @@ -447,15 +316,3 @@ SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::RemoveWaitee(int socket) { i_waitees_.erase(it); return result; } - -SbSocketWaiterPrivate::Waitee* SbSocketWaiterPrivate::RemoveWaitee( - SbSocket socket) { - sb_WaiteesMap::iterator it = sb_waitees_.find(socket); - if (it == sb_waitees_.end()) { - return NULL; - } - - Waitee* result = it->second; - sb_waitees_.erase(it); - return result; -} diff --git a/starboard/shared/libevent/socket_waiter_internal.h b/starboard/shared/libevent/socket_waiter_internal.h index 372698191497..753a512cc0c3 100644 --- a/starboard/shared/libevent/socket_waiter_internal.h +++ b/starboard/shared/libevent/socket_waiter_internal.h @@ -42,15 +42,6 @@ struct SbSocketWaiterPrivate { bool Remove(int socket, SbSocketWaiter waiter); bool CheckSocketRegistered(int socket); - // The Add/Remove pair for SbSocket based socket - bool Add(SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent); - bool Remove(SbSocket socket, SbSocketWaiter waiter); - bool CheckSocketRegistered(SbSocket socket); - void Wait(); SbSocketWaiterResult WaitTimed(int64_t duration_usec); void WakeUp(bool timeout); @@ -74,30 +65,15 @@ struct SbSocketWaiterPrivate { persistent(persistent) { use_int_socket = 1; } - Waitee(SbSocketWaiter waiter, - SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent) - : waiter(waiter), - sb_socket(socket), - context(context), - sb_callback(callback), - interests(interests), - persistent(persistent) { - use_int_socket = 0; - } // The socket registered with the waiter. int i_socket; - SbSocket sb_socket; int use_int_socket; // The callback to call when one or more registered interests become ready. SbPosixSocketWaiterCallback i_callback; - SbSocketWaiterCallback sb_callback; + // SbSocketWaiterCallback sb_callback; // The waiter this event is registered with. SbSocketWaiter waiter; @@ -120,7 +96,6 @@ struct SbSocketWaiterPrivate { // NOTE: This is a (tree) map because we don't have base::hash_map here. We // should keep an eye out for whether this is a performance issue. typedef std::map i_WaiteesMap; - typedef std::map sb_WaiteesMap; // The libevent callback function, which in turn calls the registered callback // function for the Waitee. @@ -150,13 +125,6 @@ struct SbSocketWaiterPrivate { // registry, or NULL. Waitee* RemoveWaitee(int socket) SB_WARN_UNUSED_RESULT; - // Gets the Waitee associated with the given socket, or NULL. - Waitee* GetWaitee(SbSocket socket); - - // Gets the Waitee associated with the given socket, removing it from the - // registry, or NULL. - Waitee* RemoveWaitee(SbSocket socket) SB_WARN_UNUSED_RESULT; - // The thread this waiter was created on. Immutable, so accessible from any // thread. const pthread_t thread_; @@ -178,17 +146,12 @@ struct SbSocketWaiterPrivate { // The registry of currently registered Waitees. i_WaiteesMap i_waitees_; - sb_WaiteesMap sb_waitees_; // Whether or not the waiter is actually waiting. bool waiting_; // Whether or not the waiter was woken up. bool woken_up_; - - // Used to replace pipe. - SbSocket server_socket_; - SbSocket client_socket_; }; #endif // STARBOARD_SHARED_LIBEVENT_SOCKET_WAITER_INTERNAL_H_ diff --git a/starboard/shared/libevent/socket_waiter_remove.cc b/starboard/shared/libevent/socket_waiter_remove.cc index 8f6fc5d2fbe4..6c5a5ba60cc3 100644 --- a/starboard/shared/libevent/socket_waiter_remove.cc +++ b/starboard/shared/libevent/socket_waiter_remove.cc @@ -16,13 +16,6 @@ #include "starboard/socket.h" #include "starboard/socket_waiter.h" -bool SbSocketWaiterRemove(SbSocketWaiter waiter, SbSocket socket) { - if (!SbSocketWaiterIsValid(waiter)) { - return false; - } - return waiter->Remove(socket, waiter); -} - bool SbPosixSocketWaiterRemove(SbSocketWaiter waiter, int socket) { if (!SbSocketWaiterIsValid(waiter)) { return false; diff --git a/starboard/shared/posix/socket_internal.cc b/starboard/shared/posix/socket_internal.cc index 73cf5040707a..771ff4df3df5 100644 --- a/starboard/shared/posix/socket_internal.cc +++ b/starboard/shared/posix/socket_internal.cc @@ -34,77 +34,6 @@ const socklen_t kAddressStructLengthIpv6 = static_cast(sizeof(struct sockaddr_in6)); } // namespace -SbSocketError TranslateSocketErrno(int error) { - switch (error) { - case 0: - return kSbSocketOk; - case EINPROGRESS: - case EAGAIN: -#if EWOULDBLOCK != EAGAIN - case EWOULDBLOCK: -#endif - return kSbSocketPending; - case ECONNRESET: - case ENETRESET: - case EPIPE: - return kSbSocketErrorConnectionReset; - } - - SB_LOG(ERROR) << "Unknown posix socket error: " << error; - - // Here's where we would be more nuanced if we need to be. - return kSbSocketErrorFailed; -} - -bool SetBooleanSocketOption(SbSocket socket, - int level, - int option_code, - const char* option_name, - bool value) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return false; - } - - SB_DCHECK(socket->socket_fd >= 0); - int on = value ? 1 : 0; - int result = - setsockopt(socket->socket_fd, level, option_code, &on, sizeof(on)); - if (result != 0) { - SB_LOG(ERROR) << "Failed to set " << option_name << " on socket " - << socket->socket_fd << ", errno = " << errno; - socket->error = TranslateSocketErrno(errno); - return false; - } - - socket->error = kSbSocketOk; - return true; -} - -bool SetIntegerSocketOption(SbSocket socket, - int level, - int option_code, - const char* option_name, - int value) { - if (!SbSocketIsValid(socket)) { - errno = EBADF; - return false; - } - - SB_DCHECK(socket->socket_fd >= 0); - int result = - setsockopt(socket->socket_fd, level, option_code, &value, sizeof(value)); - if (result != 0) { - SB_LOG(ERROR) << "Failed to set " << option_name << " on socket " - << socket->socket_fd << ", errno = " << errno; - socket->error = TranslateSocketErrno(errno); - return false; - } - - socket->error = kSbSocketOk; - return true; -} - bool SockAddr::FromSbSocketAddress(const SbSocketAddress* address) { if (!address) { return false; diff --git a/starboard/shared/posix/socket_internal.h b/starboard/shared/posix/socket_internal.h index 781ce2e5a923..ef81a45b2db6 100644 --- a/starboard/shared/posix/socket_internal.h +++ b/starboard/shared/posix/socket_internal.h @@ -24,54 +24,10 @@ #include "starboard/socket_waiter.h" #include "starboard/types.h" -struct SbSocketPrivate { - SbSocketPrivate(SbSocketAddressType address_type, - SbSocketProtocol protocol, - int fd) - : address_type(address_type), - protocol(protocol), - socket_fd(fd), - error(kSbSocketOk), - waiter(kSbSocketWaiterInvalid) {} - ~SbSocketPrivate() {} - - // The address domain of this socket, IPv4 or IPv6. - SbSocketAddressType address_type; - - // The protocol of this socket, UDP or TCP. - SbSocketProtocol protocol; - - // The file descriptor for this socket. - int socket_fd; - - // The last error that occurred on this socket, or kSbSocketOk. - SbSocketError error; - - // The waiter this socket is registered with, or kSbSocketWaiterInvalid. - SbSocketWaiter waiter; -}; - namespace starboard { namespace shared { namespace posix { -// Translates an errno from a socket call into an SbSocketError. -SbSocketError TranslateSocketErrno(int error); - -// Sets a boolean socket option, doing all appropriate checks. -bool SetBooleanSocketOption(SbSocket socket, - int level, - int option_code, - const char* option_name, - bool value); - -// Sets an integer socket option, doing all appropriate checks. -bool SetIntegerSocketOption(SbSocket socket, - int level, - int option_code, - const char* option_name, - int value); - // A helper class for converting back and forth from sockaddrs, ugh. class SockAddr { public: diff --git a/starboard/shared/posix/socket_is_ipv6_supported.cc b/starboard/shared/posix/socket_is_ipv6_supported.cc new file mode 100644 index 000000000000..b65583df78dc --- /dev/null +++ b/starboard/shared/posix/socket_is_ipv6_supported.cc @@ -0,0 +1,19 @@ +// Copyright 2019 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "starboard/socket.h" + +bool SbSocketIsIpv6Supported() { + return true; +} diff --git a/starboard/shared/starboard/link_receiver.cc b/starboard/shared/starboard/link_receiver.cc index 1d6befbf43e5..7b14f94e27da 100644 --- a/starboard/shared/starboard/link_receiver.cc +++ b/starboard/shared/starboard/link_receiver.cc @@ -95,6 +95,36 @@ int CreateServerSocket(SbSocketAddressType address_type) { return socket_fd; } +SbSocketAddress GetUnspecifiedAddress(SbSocketAddressType address_type, + int port) { + SbSocketAddress address = {}; + address.type = address_type; + address.port = port; + return address; +} + +bool GetLocalhostAddress(SbSocketAddressType address_type, + int port, + SbSocketAddress* address) { + if (address_type != kSbSocketAddressTypeIpv4 && + address_type != kSbSocketAddressTypeIpv6) { + SB_LOG(ERROR) << __FUNCTION__ << ": unknown address type: " << address_type; + return false; + } + *address = GetUnspecifiedAddress(address_type, port); + switch (address_type) { + case kSbSocketAddressTypeIpv4: + address->address[0] = 127; + address->address[3] = 1; + break; + case kSbSocketAddressTypeIpv6: + address->address[15] = 1; + break; + } + + return true; +} + // Creates a server socket that is bound to the loopback interface. int CreateLocallyBoundSocket(SbSocketAddressType address_type, int port) { int socket_fd = CreateServerSocket(address_type); diff --git a/starboard/shared/stub/socket_is_ipv6_supported.cc b/starboard/shared/stub/socket_is_ipv6_supported.cc new file mode 100644 index 000000000000..d2e0fe247c71 --- /dev/null +++ b/starboard/shared/stub/socket_is_ipv6_supported.cc @@ -0,0 +1,19 @@ +// Copyright 2019 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "starboard/socket.h" + +bool SbSocketIsIpv6Supported() { + return false; +} diff --git a/starboard/shared/stub/socket_waiter_add.cc b/starboard/shared/stub/socket_waiter_add.cc index dec42fb3b89a..ff29867e2f0c 100644 --- a/starboard/shared/stub/socket_waiter_add.cc +++ b/starboard/shared/stub/socket_waiter_add.cc @@ -14,15 +14,6 @@ #include "starboard/socket_waiter.h" -bool SbSocketWaiterAdd(SbSocketWaiter waiter, - SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent) { - return false; -} - bool SbPosixSocketWaiterAdd(SbSocketWaiter waiter, int socket, void* context, diff --git a/starboard/shared/stub/socket_waiter_remove.cc b/starboard/shared/stub/socket_waiter_remove.cc index 66dd5a0360f7..51a816af2ad5 100644 --- a/starboard/shared/stub/socket_waiter_remove.cc +++ b/starboard/shared/stub/socket_waiter_remove.cc @@ -14,10 +14,6 @@ #include "starboard/socket_waiter.h" -bool SbSocketWaiterRemove(SbSocketWaiter waiter, SbSocket socket) { - return false; -} - bool SbPosixSocketWaiterRemove(SbSocketWaiter waiter, int socket) { return false; } diff --git a/starboard/socket.h b/starboard/socket.h index 79c0f76d7327..d38c68434af1 100644 --- a/starboard/socket.h +++ b/starboard/socket.h @@ -40,33 +40,6 @@ extern "C" { #endif -// Private structure representing a socket, which may or may not be connected or -// listening. -typedef struct SbSocketPrivate SbSocketPrivate; - -// A handle to a socket. -typedef SbSocketPrivate* SbSocket; - -// Enumeration of all Starboard socket operation results. Despite the enum -// name, note that the value actually describes the outcome of an operation, -// which is not always an error. -typedef enum SbSocketError { - // The operation succeeded. - kSbSocketOk = 0, - - // The operation is blocked on I/O. Either try again "later," or be very - // clever and wait on it with a SbSocketWaiter. - kSbSocketPending, - - // This socket error is generated when the connection is reset unexpectedly - // and the connection is now invalid. - // This might happen for example if an read packet has the "TCP RST" bit set. - kSbSocketErrorConnectionReset, - - // The operation failed for some other reason not specified above. - kSbSocketErrorFailed, -} SbSocketError; - // All possible IP socket types. typedef enum SbSocketProtocol { // The TCP/IP protocol, a reliable, stateful, streaming protocol. @@ -101,22 +74,8 @@ typedef struct SbSocketAddress { int port; } SbSocketAddress; -// The result of a host name resolution. -typedef struct SbSocketResolution { - // An array of addresses associated with the host name. - SbSocketAddress* addresses; - - // The length of the |addresses| array. - int address_count; -} SbSocketResolution; - -// Well-defined value for an invalid socket handle. -#define kSbSocketInvalid ((SbSocket)NULL) - -// Returns whether the given socket handle is valid. -static inline bool SbSocketIsValid(SbSocket socket) { - return socket != kSbSocketInvalid; -} +// Returns whether IPV6 is supported on the current platform. +SB_EXPORT bool SbSocketIsIpv6Supported(); #ifdef __cplusplus } // extern "C" diff --git a/starboard/socket_waiter.h b/starboard/socket_waiter.h index 7b5e02fce28b..8e8cb3c9eeec 100644 --- a/starboard/socket_waiter.h +++ b/starboard/socket_waiter.h @@ -73,19 +73,11 @@ typedef enum SbSocketWaiterResult { kSbSocketWaiterResultWokenUp, } SbSocketWaiterResult; -// Function pointer for socket waiter callbacks. -typedef void (*SbSocketWaiterCallback)(SbSocketWaiter waiter, - SbSocket socket, - void* context, - int ready_interests); - -#if SB_API_VERSION >= 16 // Function pointer for socket waiter callbacks. typedef void (*SbPosixSocketWaiterCallback)(SbSocketWaiter waiter, int socket, void* context, int ready_interests); -#endif // Well-defined value for an invalid socket watcher handle. #define kSbSocketWaiterInvalid ((SbSocketWaiter)NULL) @@ -142,21 +134,12 @@ SB_EXPORT bool SbSocketWaiterDestroy(SbSocketWaiter waiter); // - If |persistent| is |false|, then |socket| is removed before the next call // to |callback|, even if not all registered |interests| became ready, // which allows for adding it again in the |callback|. -SB_EXPORT bool SbSocketWaiterAdd(SbSocketWaiter waiter, - SbSocket socket, - void* context, - SbSocketWaiterCallback callback, - int interests, - bool persistent); - -#if SB_API_VERSION >= 16 SB_EXPORT bool SbPosixSocketWaiterAdd(SbSocketWaiter waiter, int socket, void* context, SbPosixSocketWaiterCallback callback, int interests, bool persistent); -#endif // DEPRECATED with SB_API_VERSION 16 // @@ -169,11 +152,8 @@ SB_EXPORT bool SbPosixSocketWaiterAdd(SbSocketWaiter waiter, // // |waiter|: The waiter from which the socket is removed. // |socket|: The socket to remove from the waiter. -SB_EXPORT bool SbSocketWaiterRemove(SbSocketWaiter waiter, SbSocket socket); -#if SB_API_VERSION >= 16 SB_EXPORT bool SbPosixSocketWaiterRemove(SbSocketWaiter waiter, int socket); -#endif // Waits on all registered sockets, calling the registered callbacks if and when // the corresponding sockets become ready for an interested operation. This diff --git a/starboard/stub/BUILD.gn b/starboard/stub/BUILD.gn index d619516b8234..6040f25e4e10 100644 --- a/starboard/stub/BUILD.gn +++ b/starboard/stub/BUILD.gn @@ -101,6 +101,7 @@ static_library("stub_sources") { "//starboard/shared/stub/player_set_volume.cc", "//starboard/shared/stub/player_write_end_of_stream.cc", "//starboard/shared/stub/player_write_samples.cc", + "//starboard/shared/stub/socket_is_ipv6_supported.cc", "//starboard/shared/stub/socket_waiter_add.cc", "//starboard/shared/stub/socket_waiter_create.cc", "//starboard/shared/stub/socket_waiter_destroy.cc",