Using underscores as colon replacement

This commit is contained in:
WolverinDEV 2019-11-23 22:40:24 +01:00
parent bbe9f9f05f
commit 3877234037
1 changed files with 7 additions and 7 deletions

View File

@ -80,16 +80,16 @@ void WebDNSHandler::handle_message(const std::shared_ptr<DNSServerBinding>& bind
#define MAX_IPV6_ADDRESS_STR_LEN 39 #define MAX_IPV6_ADDRESS_STR_LEN 39
//Address at least one char long! //Address at least one char long!
bool parse_v6(uint8_t(result)[16], const std::string_view& address) { bool parse_v6(uint8_t(result)[16], const std::string_view& address, char colon = ':') {
uint16_t accumulator{0}; uint16_t accumulator{0};
uint8_t colon_count{0}, pos{0}; uint8_t colon_count{0}, pos{0};
memset(result, 0, 16); memset(result, 0, 16);
// Step 1: look for position of ::, and count colons after it // Step 1: look for position of ::, and count col ons after it
for(uint8_t i = 1; i <= MAX_IPV6_ADDRESS_STR_LEN && address.length() > i; i++) { for(uint8_t i = 1; i <= MAX_IPV6_ADDRESS_STR_LEN && address.length() > i; i++) {
if (address[i] == ':') { if (address[i] == colon) {
if (address[i - 1] == ':') { if (address[i - 1] == colon) {
colon_count = 14; // Double colon! colon_count = 14; // Double colon!
} else if (colon_count) { } else if (colon_count) {
colon_count -= 2; // Count backwards the number of colons after the :: colon_count -= 2; // Count backwards the number of colons after the ::
@ -99,12 +99,12 @@ bool parse_v6(uint8_t(result)[16], const std::string_view& address) {
// Step 2: convert from ascii to binary // Step 2: convert from ascii to binary
for(uint8_t i=0; i <= MAX_IPV6_ADDRESS_STR_LEN && pos < 16; i++) { for(uint8_t i=0; i <= MAX_IPV6_ADDRESS_STR_LEN && pos < 16; i++) {
if (address[i] == ':' || address.length() == i) { if (address[i] == colon || address.length() == i) {
result[pos] = accumulator >> 8U; result[pos] = accumulator >> 8U;
result[pos + 1] = accumulator; result[pos + 1] = accumulator;
accumulator = 0; accumulator = 0;
if (colon_count && i && address[i - 1] == ':') { if (colon_count && i && address[i - 1] == colon) {
pos = colon_count; pos = colon_count;
} else { } else {
pos += 2; pos += 2;
@ -165,7 +165,7 @@ void create_answer(DNSBuilder& response, const ClientAddress & client_address, c
if(parts.size() != 3) return; if(parts.size() != 3) return;
in6_addr result{}; in6_addr result{};
if(!parse_v6(result.__in6_u.__u6_addr8, parts[0])) return; if(!parse_v6(result.__in6_u.__u6_addr8, parts[0], '_')) return;
log_dns()->info("[{}] Sending requested IPv6 ({}): {}", client_address.str, dn, net::to_string(result)); log_dns()->info("[{}] Sending requested IPv6 ({}): {}", client_address.str, dn, net::to_string(result));