IP_Address::IP_Address(const String& p_string) { clear(); if (p_string.find(":") >= 0) { _parse_ipv6(p_string); type = TYPE_IPV6; } else { _parse_ipv4(p_string, 0, &field8[0]); type = TYPE_IPV4; }; }
IP_Address::IP_Address(const String &p_string) { clear(); if (p_string == "*") { // Wildcard (not a valid IP) wildcard = true; } else if (p_string.find(":") >= 0) { // IPv6 _parse_ipv6(p_string); valid = true; } else if (p_string.get_slice_count(".") == 4) { // IPv4 (mapped to IPv6 internally) field16[5] = 0xffff; _parse_ipv4(p_string, 0, &field8[12]); valid = true; } else { ERR_PRINT("Invalid IP address"); } }
void IP_Address::_parse_ipv6(const String& p_string) { static const int parts_total = 8; int parts[parts_total] = {0}; int parts_count = 0; bool part_found = false; bool part_skip = false; bool part_ipv4 = false; int parts_idx = 0; for (int i=0; i<p_string.length(); i++) { CharType c = p_string[i]; if (c == ':') { if (i == 0) { continue; // next must be a ":" }; if (!part_found) { part_skip = true; parts[parts_idx++] = -1; }; part_found = false; } else if (c == '.') { part_ipv4 = true; } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { if (!part_found) { parts[parts_idx++] = i; part_found = true; ++parts_count; }; } else { ERR_EXPLAIN("Invalid character in IPv6 address: " + p_string); ERR_FAIL(); }; }; int parts_extra = 0; if (part_skip) { parts_extra = parts_total - parts_count; }; int idx = 0; for (int i=0; i<parts_idx; i++) { if (parts[i] == -1) { for (int j=0; j<parts_extra; j++) { field16[idx++] = 0; }; continue; }; if (part_ipv4 && i == parts_idx - 1) { _parse_ipv4(p_string, parts[i], (uint8_t*)&field16[idx]); // should be the last one } else { _parse_hex(p_string, parts[i], (uint8_t*)&(field16[idx++])); }; }; };