From 4b36d05ccc6ffbbcba1fd1197d50ddc883612151 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 18 Dec 2019 14:20:29 +0900 Subject: [PATCH] Minor fixes, improvements When UDPC_ConnectionId is initialized, all of its bytes are now zeroed out. --- src/UDPConnection.cpp | 46 +++++++++++++++++++++++++++++++++++-------- src/test/TestUDPC.cpp | 22 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 40638ad..5bb82e9 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1791,28 +1791,54 @@ void UDPC::threadedUpdate(Context *ctx) { } UDPC_ConnectionId UDPC_create_id(UDPC_IPV6_ADDR_TYPE addr, uint16_t port) { - return UDPC_ConnectionId{addr, 0, port}; + UDPC_ConnectionId result; + + std::memset(&result, 0, sizeof(UDPC_ConnectionId)); + + result.addr = addr; +// result.scope_id = 0; + result.port = port; + return result; } UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_id, uint16_t port) { - return UDPC_ConnectionId{addr, scope_id, port}; + UDPC_ConnectionId result; + + std::memset(&result, 0, sizeof(UDPC_ConnectionId)); + + result.addr = addr; + result.scope_id = scope_id; + result.port = port; + return result; } UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port) { - return UDPC_ConnectionId{in6addr_any, 0, port}; + UDPC_ConnectionId result; + + std::memset(&result, 0, sizeof(UDPC_ConnectionId)); + +// result.addr = in6addr_any; + result.scope_id = 0; + result.port = port; + return result; } UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port) { - UDPC_ConnectionId conId{{0}, 0, port}; + UDPC_ConnectionId result; + + std::memset(&result, 0, sizeof(UDPC_ConnectionId)); + + result.port = port; + if(std::regex_match(addrString, ipv6_regex_nolink) || std::regex_match(addrString, ipv4_regex)) { - conId.addr = UDPC_strtoa(addrString); + result.addr = UDPC_strtoa(addrString); } else if(std::regex_match(addrString, ipv6_regex_linkonly)) { - conId.addr = UDPC_strtoa_link(addrString, &conId.scope_id); + result.addr = UDPC_strtoa_link(addrString, &result.scope_id); } else { - conId.addr = in6addr_loopback; + result.addr = in6addr_loopback; } - return conId; + return result; } UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium) { @@ -2436,6 +2462,10 @@ UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr) { UDPC_IPV6_ADDR_TYPE result = in6addr_loopback; std::cmatch matchResults; if(std::regex_match(addrStr, matchResults, ipv6_regex_nolink)) { + if(std::strcmp("::", addrStr) == 0) { + return in6addr_any; + } + unsigned int index = 0; unsigned int strIndex = 0; int doubleColonIndex = -1; diff --git a/src/test/TestUDPC.cpp b/src/test/TestUDPC.cpp index a49ec7d..f42c10b 100644 --- a/src/test/TestUDPC.cpp +++ b/src/test/TestUDPC.cpp @@ -235,3 +235,25 @@ TEST(UDPC, create_id_easy) { EXPECT_EQ(conId.scope_id, 3); EXPECT_EQ(conId.port, 123); } + +TEST(UDPC, ConnectionIdBits) { + UDPC_ConnectionId id = UDPC_create_id({0}, 0); + for(unsigned int i = 0; i < sizeof(UDPC_ConnectionId); ++i) { + EXPECT_EQ(((char*)&id)[i], 0); + } + + id = UDPC_create_id_full({0}, 0, 0); + for(unsigned int i = 0; i < sizeof(UDPC_ConnectionId); ++i) { + EXPECT_EQ(((char*)&id)[i], 0); + } + + id = UDPC_create_id_anyaddr(0); + for(unsigned int i = 0; i < sizeof(UDPC_ConnectionId); ++i) { + EXPECT_EQ(((char*)&id)[i], 0); + } + + id = UDPC_create_id_easy("::", 0); + for(unsigned int i = 0; i < sizeof(UDPC_ConnectionId); ++i) { + EXPECT_EQ(((char*)&id)[i], 0); + } +}