// Implementation of inet_ntop (create a printable representation of an // ip address). XP doesn't have its own inet_ntop, and // WSAAddressToString requires both IPv6 to be installed and for Winsock // to be initialized. constchar* win32_inet_ntop(int af, constvoid* src, char* dst,
socklen_t size) { if (!src || !dst) { return nullptr;
} switch (af) { case AF_INET: { return inet_ntop_v4(src, dst, size);
} case AF_INET6: { return inet_ntop_v6(src, dst, size);
}
} return nullptr;
}
// As above, but for inet_pton. Implements inet_pton for v4 and v6. // Note that our inet_ntop will output normal 'dotted' v4 addresses only. int win32_inet_pton(int af, constchar* src, void* dst) { if (!src || !dst) { return0;
} if (af == AF_INET) { return inet_pton_v4(src, dst);
} elseif (af == AF_INET6) { return inet_pton_v6(src, dst);
} return -1;
}
// Helper function for inet_ntop for IPv6 addresses. constchar* inet_ntop_v6(constvoid* src, char* dst, socklen_t size) { if (size < INET6_ADDRSTRLEN) { return nullptr;
} const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src); int runpos[8]; int current = 1; int max = 0; int maxpos = -1; int run_array_size = std::ssize(runpos); // Run over the address marking runs of 0s. for (int i = 0; i < run_array_size; ++i) { if (as_shorts[i] == 0) {
runpos[i] = current; if (current > max) {
maxpos = i;
max = current;
}
++current;
} else {
runpos[i] = -1;
current = 1;
}
}
if (max > 0) { int tmpmax = maxpos; // Run back through, setting -1 for all but the longest run. for (int i = run_array_size - 1; i >= 0; i--) { if (i > tmpmax) {
runpos[i] = -1;
} elseif (runpos[i] == -1) { // We're less than maxpos, we hit a -1, so the 'good' run is done. // Setting tmpmax -1 means all remaining positions get set to -1.
tmpmax = -1;
}
}
}
char* cursor = dst; // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper. // These addresses have an initial run of either eight zero-bytes followed // by 0xFFFF, or an initial run of ten zero-bytes. if (runpos[0] == 1 &&
(maxpos == 5 || (maxpos == 4 && as_shorts[5] == 0xFFFF))) {
*cursor++ = ':';
*cursor++ = ':'; if (maxpos == 4) {
cursor += snprintf(cursor, INET6_ADDRSTRLEN - 2, "ffff:");
} conststruct in_addr* as_v4 = reinterpret_cast<conststruct in_addr*>(&(as_shorts[6]));
inet_ntop_v4(as_v4, cursor, static_cast<socklen_t>(INET6_ADDRSTRLEN - (cursor - dst)));
} else { for (int i = 0; i < run_array_size; ++i) { if (runpos[i] == -1) {
cursor += snprintf(cursor, INET6_ADDRSTRLEN - (cursor - dst), "%x",
NetworkToHost16(as_shorts[i])); if (i != 7 && runpos[i + 1] != 1) {
*cursor++ = ':';
}
} elseif (runpos[i] == 1) { // Entered the run; print the colons and skip the run.
*cursor++ = ':';
*cursor++ = ':';
i += (max - 1);
}
}
} return dst;
}
// Helper function for inet_pton for IPv4 addresses. // `src` points to a character string containing an IPv4 network address in // dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number // of up to three digits in the range 0 to 255. // The address is converted and copied to dst, // which must be sizeof(struct in_addr) (4) bytes (32 bits) long. int inet_pton_v4(constchar* src, void* dst) { constint kIpv4AddressSize = 4; int found = 0; constchar* src_pos = src; unsignedchar result[kIpv4AddressSize] = {0};
while (*src_pos != '\0') { // strtol won't treat whitespace characters in the begining as an error, // so check to ensure this is started with digit before passing to strtol. if (!isdigit(*src_pos)) { return0;
} char* end_pos; long value = strtol(src_pos, &end_pos, 10); if (value < 0 || value > 255 || src_pos == end_pos) { return0;
}
++found; if (found > kIpv4AddressSize) { return0;
}
result[found - 1] = static_cast<unsignedchar>(value);
src_pos = end_pos; if (*src_pos == '.') { // There's more.
++src_pos;
} elseif (*src_pos != '\0') { // If it's neither '.' nor '\0' then return fail. return0;
}
} if (found != kIpv4AddressSize) { return0;
}
memcpy(dst, result, sizeof(result)); return1;
}
// Helper function for inet_pton for IPv6 addresses. int inet_pton_v6(constchar* src, void* dst) { // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. // Check for literal x in the input string. constchar* readcursor = src; char c = *readcursor++; while (c) { if (c == 'x') { return0;
}
c = *readcursor++;
}
readcursor = src;
// For addresses without a trailing IPv4 component ('normal' IPv6 addresses). while (*readcursor != 0 && addr_cursor < addr_end) { if (*readcursor == ':') { if (*(readcursor + 1) == ':') { if (seencompressed) { // Can only have one compressed run of zeroes ("::") per address. return0;
} // Hit a compressed run. Count colons to figure out how much of the // address is skipped.
readcursor += 2; constchar* coloncounter = readcursor; int coloncount = 0; if (*coloncounter == 0) { // Special case - trailing ::.
addr_cursor = addr_end;
} else { bool has_dot = false; while (*coloncounter) { if (*coloncounter == ':') {
++coloncount;
} elseif (*coloncounter == '.') {
has_dot = true;
}
++coloncounter;
} // (coloncount + 1) is the number of shorts left in the address. // If there's a dot, the last part is an IPv4 address, which is 2 // shorts but has no internal colon. int expected_shorts = coloncount + 1; if (has_dot) {
expected_shorts++;
} // If this number is greater than the number of available shorts, the // address is malformed. if (expected_shorts > addr_end - addr_cursor) { return0;
}
addr_cursor = addr_end - expected_shorts;
seencompressed = true;
}
} else {
++readcursor;
}
} else { if (strchr(readcursor, '.') && addr_cursor + 2 <= addr_end) { struct in_addr v4; if (inet_pton_v4(readcursor, &v4.s_addr)) {
memcpy(addr_cursor, &v4, sizeof(v4));
addr_cursor += 2;
readcursor += strlen(readcursor); break;
}
}
uint16_t word; int bytesread = 0; if (sscanf(readcursor, "%4hx%n", &word, &bytesread) != 1) { return0;
} else {
*addr_cursor = HostToNetwork16(word);
++addr_cursor;
readcursor += bytesread; if (*readcursor != ':' && *readcursor != '\0') { return0;
}
}
}
}
if (*readcursor != '\0' || addr_cursor < addr_end) { // Catches addresses too short or too long. return0;
}
memcpy(dst, &an_addr, sizeof(an_addr)); return1;
}
} // namespace webrtc
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-08-24)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.