From 22170904c022d2857ee748ad64b2aee0a6b40874 Mon Sep 17 00:00:00 2001 From: "HARMEL, Bernard" Date: Tue, 20 Aug 2024 15:16:22 +0200 Subject: [PATCH] fix Bof_IsIpAddressOpened --- lib/src/bofsocketos.cpp | 67 +++++++++++++++++++++++---------------- tests/src/main.cpp | 2 +- tests/src/ut_socketos.cpp | 17 ++++++++++ 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/lib/src/bofsocketos.cpp b/lib/src/bofsocketos.cpp index 0ca7c46..a275e50 100644 --- a/lib/src/bofsocketos.cpp +++ b/lib/src/bofsocketos.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -2347,48 +2348,58 @@ bool Bof_IsIpAddressPingable(uint32_t _TimeoutInMs_U32, const std::string &_rIpA } return Rts_B; } + bool Bof_IsIpAddressOpened(uint32_t _TimeoutInMs_U32, const std::string &_rIpAddress_S) { bool Rts_B = false; -#if 0 uint32_t Nb_U32; char pMsg_c[0x100]; std::unique_ptr puSocket; BOF_SOCKET_PARAM SocketParams_X; + BOF_SOCKET_ADDRESS IpAddress_X; + bool IsUdp_B; - SocketParams_X.BroadcastPort_U16 = 0; - SocketParams_X.MulticastSender_B = false; - SocketParams_X.Ttl_U32 = 8; - SocketParams_X.KeepAlive_B = false; - SocketParams_X.ReUseAddress_B = true; - SocketParams_X.BaseChannelParam_X.Blocking_B = true; - SocketParams_X.BaseChannelParam_X.ListenBackLog_U32 = 0; - SocketParams_X.NoDelay_B = true; - SocketParams_X.BaseChannelParam_X.RcvBufferSize_U32 = 0; - SocketParams_X.BaseChannelParam_X.SndBufferSize_U32 = 0; - SocketParams_X.BindIpAddress_S = ""; - - puSocket = std::make_unique(SocketParams_X); - if ((puSocket) && (puSocket->LastErrorCode() == BOF_ERR_NO_ERROR)) + if (Bof_IpAddressToSocketAddress(_rIpAddress_S, IpAddress_X) == BOF_ERR_NO_ERROR) { - if (puSocket->IsUdp()) - { - strcpy(pMsg_c, "Hello"); - Nb_U32 = strlen(pMsg_c); - if (puSocket->V_WriteData(_TimeoutInMs_U32, Nb_U32, reinterpret_cast(pMsg_c)) == BOF_ERR_NO_ERROR) - { - Rts_B = true; + IsUdp_B = (IpAddress_X.SocketType_E == BOF_SOCK_TYPE::BOF_SOCK_UDP); + + SocketParams_X.BroadcastPort_U16 = 0; + SocketParams_X.MulticastSender_B = false; + SocketParams_X.Ttl_U32 = 0; + SocketParams_X.KeepAlive_B = false; + SocketParams_X.ReUseAddress_B = true; + SocketParams_X.BaseChannelParam_X.Blocking_B = true; + SocketParams_X.BaseChannelParam_X.ListenBackLog_U32 = 0; + SocketParams_X.NoDelay_B = true; + SocketParams_X.BaseChannelParam_X.RcvBufferSize_U32 = 0; + SocketParams_X.BaseChannelParam_X.SndBufferSize_U32 = 0; + SocketParams_X.BindIpAddress_S = IsUdp_B ? "udp://0.0.0.0:0": "tcp://0.0.0.0:0"; + + puSocket = std::make_unique(SocketParams_X); + if ((puSocket) && (puSocket->LastErrorCode() == BOF_ERR_NO_ERROR)) + { + if (IsUdp_B) + { + if (puSocket->SetDstIpAddress(IpAddress_X) == BOF_ERR_NO_ERROR) + { + strcpy(pMsg_c, "Hello"); + Nb_U32 = strlen(pMsg_c); + if (puSocket->V_WriteData(_TimeoutInMs_U32, Nb_U32, reinterpret_cast(pMsg_c)) == BOF_ERR_NO_ERROR) + { + Nb_U32 = 1; + Rts_B = (puSocket->V_ReadData(_TimeoutInMs_U32, Nb_U32, reinterpret_cast(pMsg_c)) == BOF_ERR_NO_ERROR); + } + } } - } - else - { - if (puSocket->V_Connect(_TimeoutInMs_U32, _rIpAddress_S, "") == BOF_ERR_NO_ERROR) + else { - Rts_B = true; + if (puSocket->V_Connect(_TimeoutInMs_U32, _rIpAddress_S, "") == BOF_ERR_NO_ERROR) + { + Rts_B = true; + } } } } -#endif return Rts_B; } END_BOF_NAMESPACE() \ No newline at end of file diff --git a/tests/src/main.cpp b/tests/src/main.cpp index b13672e..7ce527e 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) //::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.*:CircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*"; //::testing::GTEST_FLAG(filter) = "BofThreadPool_Test.Dispatch"; //::testing::GTEST_FLAG(filter) = "ConIo_Test.*"; - //::testing::GTEST_FLAG(filter) = "String_Test.*"; + //::testing::GTEST_FLAG(filter) = "SocketOs_Test.*"; // ::testing::GTEST_FLAG(filter) = "RawCircularBufferAlwaysContiguous_Test.*:RawCircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*"; // std::string CrtDir_S; // BOF::Bof_GetCurrentDirectory(CrtDir_S); diff --git a/tests/src/ut_socketos.cpp b/tests/src/ut_socketos.cpp index 9bb5399..2f9d625 100644 --- a/tests/src/ut_socketos.cpp +++ b/tests/src/ut_socketos.cpp @@ -28,6 +28,23 @@ USE_BOF_NAMESPACE() // #define BOF_UT_INTERNET_AVAILABLE +TEST(SocketOs_Test, IsIpAddressOpened) +{ + EXPECT_TRUE(Bof_IsIpAddressOpened(1000, "tcp://10.129.170.15:7")); // Echo protocol: https://medium.com/@bzami.ayman/echo-protocol-4cb25db9ae28 + EXPECT_TRUE(Bof_IsIpAddressOpened(1000, "tcp://10.129.170.15:8")); // Echo protocol: https://medium.com/@bzami.ayman/echo-protocol-4cb25db9ae28 + EXPECT_TRUE(Bof_IsIpAddressOpened(1000, "udp://10.129.170.15:7")); // Echo protocol: https://medium.com/@bzami.ayman/echo-protocol-4cb25db9ae28 + EXPECT_TRUE(Bof_IsIpAddressOpened(1000, "udp://10.129.170.15:8")); // Echo protocol: https://medium.com/@bzami.ayman/echo-protocol-4cb25db9ae28 +} + +TEST(SocketOs_Test, Ping) +{ + EXPECT_FALSE(Bof_IsIpAddressPingable(1000, "10.129.170.250")); + EXPECT_FALSE(Bof_IsIpAddressPingable(1000, "1.2.3.4")); + EXPECT_FALSE(Bof_IsIpAddressPingable(1000, "1.2.3.400")); + EXPECT_TRUE(Bof_IsIpAddressPingable(1000, "127.0.0.1")); + EXPECT_TRUE(Bof_IsIpAddressPingable(1000, "10.129.170.14")); + EXPECT_TRUE(Bof_IsIpAddressPingable(1000, "10.129.170.15")); +} void TestSocketAddress(bool _IsIpV6_B) {