From f9e1bca4c12277912726449fd0862a815be285cb Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 13 Nov 2019 12:15:12 +0900 Subject: [PATCH] Add UDPC_create_id_easy UDPC_create_id_easy will detect if the input addr string is link local or not, and will create the ipv6 address based on it. --- src/UDPConnection.cpp | 12 ++++++++++++ src/UDPConnection.h | 2 ++ src/test/TestUDPC.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/test/UDPC_NetworkTest.c | 4 ++-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 767431c..d75646e 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1221,6 +1221,18 @@ UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port) { return UDPC_ConnectionId{in6addr_any, 0, port}; } +UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port) { + UDPC_ConnectionId conId{{0}, 0, port}; + if(std::regex_match(addrString, ipv6_regex_nolink)) { + conId.addr = UDPC_strtoa(addrString); + } else if(std::regex_match(addrString, ipv6_regex_linkonly)) { + conId.addr = UDPC_strtoa_link(addrString, &conId.scope_id); + } else { + conId.addr = in6addr_loopback; + } + return conId; +} + UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) { UDPC::Context *ctx = new UDPC::Context(false); ctx->flags.set(1, isClient != 0); diff --git a/src/UDPConnection.h b/src/UDPConnection.h index 28f3536..e9d41bd 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -111,6 +111,8 @@ UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_i UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port); +UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port); + UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient); UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId, int isClient); diff --git a/src/test/TestUDPC.cpp b/src/test/TestUDPC.cpp index f3c43b9..a49ec7d 100644 --- a/src/test/TestUDPC.cpp +++ b/src/test/TestUDPC.cpp @@ -199,3 +199,39 @@ TEST(UDPC, strtoa) { }; EXPECT_EQ(UDPC_strtoa("10.1.2.3"), addr); } + +TEST(UDPC, create_id_easy) { + UDPC_ConnectionId conId; + + // not link local + conId = UDPC_create_id_easy("::FFFF:7F00:1", 301); + for(unsigned int i = 0; i < 10; ++i) { + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[i], 0); + } + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[10], 0xFF); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[11], 0xFF); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[12], 0x7F); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[13], 0); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[14], 0); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[15], 0x1); + + EXPECT_EQ(conId.scope_id, 0); + EXPECT_EQ(conId.port, 301); + + // link local + conId = UDPC_create_id_easy("fe80::1234:5678:9%3", 123); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[0], 0xFE); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[1], 0x80); + for(unsigned int i = 2; i < 10; ++i) { + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[i], 0); + } + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[10], 0x12); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[11], 0x34); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[12], 0x56); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[13], 0x78); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[14], 0); + EXPECT_EQ(UDPC_IPV6_ADDR_SUB(conId.addr)[15], 0x9); + + EXPECT_EQ(conId.scope_id, 3); + EXPECT_EQ(conId.port, 123); +} diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index 8ebe6f1..4aba364 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -118,10 +118,10 @@ int main(int argc, char **argv) { if(strcmp(listenAddr, "any") == 0) { listenId = UDPC_create_id_anyaddr(atoi(listenPort)); } else { - listenId = UDPC_create_id(UDPC_strtoa(listenAddr), atoi(listenPort)); + listenId = UDPC_create_id_easy(listenAddr, atoi(listenPort)); } if(isClient) { - connectionId = UDPC_create_id(UDPC_strtoa(connectionAddr), atoi(connectionPort)); + connectionId = UDPC_create_id_easy(connectionAddr, atoi(connectionPort)); } UDPC_HContext context = UDPC_init_threaded_update(listenId, isClient); if(!context) {