Skip to content

Commit

Permalink
fix Bof_IsIpAddressOpened
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Aug 20, 2024
1 parent 44e79dd commit 2217090
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
67 changes: 39 additions & 28 deletions lib/src/bofsocketos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <bofstd/bofsocketos.h>
#include <bofstd/bofstring.h>
#include <bofstd/bofsystem.h>
#include <bofstd/bofsocket.h>

#include <map>
#include <regex>
Expand Down Expand Up @@ -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<BofSocket> 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<BofSocket>(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<uint8_t *>(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<BofSocket>(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<uint8_t *>(pMsg_c)) == BOF_ERR_NO_ERROR)
{
Nb_U32 = 1;
Rts_B = (puSocket->V_ReadData(_TimeoutInMs_U32, Nb_U32, reinterpret_cast<uint8_t *>(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()
2 changes: 1 addition & 1 deletion tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions tests/src/ut_socketos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 2217090

Please sign in to comment.