Add support for UDPC_strtoa to accept ipv4 input

UDPC_strtoa can now return an ipv4-mapped ipv6 address given an ipv4
string. Also, added validation of input strings via regex.
This commit is contained in:
Stephen Seo 2019-09-17 17:17:16 +09:00
parent 17d05b4a19
commit a642db53f0
3 changed files with 186 additions and 151 deletions

View file

@ -11,6 +11,12 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <ios> #include <ios>
#include <regex>
static std::regex ipv6_regex = std::regex(R"d((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))d");
// TODO remove ipv6_regex_nolink when link device is supported
static std::regex ipv6_regex_nolink = std::regex(R"d((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))d");
static std::regex ipv4_regex = std::regex(R"d((1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])\.(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])\.(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])\.(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9]))d");
UDPC::SentPktInfo::SentPktInfo() : UDPC::SentPktInfo::SentPktInfo() :
id(0), id(0),
@ -1117,7 +1123,10 @@ const char *UDPC_atostr(UDPC_HContext ctx, UDPC_ConnectionId connectionId) {
} }
struct in6_addr UDPC_strtoa(const char *addrStr) { struct in6_addr UDPC_strtoa(const char *addrStr) {
struct in6_addr result{{0}}; struct in6_addr result = in6addr_loopback;
std::cmatch matchResults;
// TODO switch regex to ipv6_regex when link device is supported
if(std::regex_match(addrStr, matchResults, ipv6_regex_nolink)) {
unsigned int index = 0; unsigned int index = 0;
unsigned int strIndex = 0; unsigned int strIndex = 0;
int doubleColonIndex = -1; int doubleColonIndex = -1;
@ -1282,6 +1291,15 @@ struct in6_addr UDPC_strtoa(const char *addrStr) {
result.s6_addr[i - strIndex] = 0; result.s6_addr[i - strIndex] = 0;
} }
} }
} else if(std::regex_match(addrStr, matchResults, ipv4_regex)) {
for(unsigned int i = 0; i < 10; ++i) {
result.s6_addr[i] = 0;
}
result.s6_addr[10] = 0xFF;
result.s6_addr[11] = 0xFF;
for(unsigned int i = 0; i < 4; ++i) {
result.s6_addr[12 + i] = std::stoi(matchResults[i + 1].str());
}
}
return result; return result;
} }

View file

@ -102,6 +102,7 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx);
const char *UDPC_atostr(UDPC_HContext ctx, UDPC_ConnectionId connectionId); const char *UDPC_atostr(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
/// addrStr must be a valid ipv6 address or a valid ipv4 address
struct in6_addr UDPC_strtoa(const char *addrStr); struct in6_addr UDPC_strtoa(const char *addrStr);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -172,4 +172,20 @@ TEST(UDPC, strtoa) {
0x12, 0x34, 0xab, 0xcd 0x12, 0x34, 0xab, 0xcd
}; };
EXPECT_EQ(UDPC_strtoa("ff:100:1:1000::1234:abcd"), addr); EXPECT_EQ(UDPC_strtoa("ff:100:1:1000::1234:abcd"), addr);
addr = {
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0xFF, 0xFF,
0x7F, 0x0, 0x0, 0x1
};
EXPECT_EQ(UDPC_strtoa("127.0.0.1"), addr);
addr = {
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0xFF, 0xFF,
0xA, 0x1, 0x2, 0x3
};
EXPECT_EQ(UDPC_strtoa("10.1.2.3"), addr);
} }