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.
This commit is contained in:
Stephen Seo 2019-11-13 12:15:12 +09:00
parent 90b36e3061
commit f9e1bca4c1
4 changed files with 52 additions and 2 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);
}

View file

@ -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) {