Change functions IsValidIPv4/v6 to use addrinfo instead of custom logic

This commit is contained in:
2026-07-27 12:28:34 +02:00
parent c4e28df46f
commit 3e5c051645
2 changed files with 31 additions and 89 deletions
+3 -3
View File
@@ -14,9 +14,6 @@ Check if Block FullVerify is actually verifying fully (not missing any condition
A loophole in the reorg penalty system could potentially exist where someone broadcasts blocks one-at-a-time. Determine a solution to this. A loophole in the reorg penalty system could potentially exist where someone broadcasts blocks one-at-a-time. Determine a solution to this.
IPv6 support for the P2P node. Come on guys, it's 2026. RFC 2460 was in 1998. It's about time.
Like if someone is behind NAT, fine, workable. CGNAT? Lmao good luck.
TO TEST: TO TEST:
Implement Horizen's "Reorg Penalty" system to make it harder for the young chain to be attacked by a powerful miner. Implement Horizen's "Reorg Penalty" system to make it harder for the young chain to be attacked by a powerful miner.
@@ -31,3 +28,6 @@ a constant inflation rate of 1.5% per year. It's lower than fiat (USD is ~2.8% p
Move to a GPU algo. RandomX is a good candidate, but CPU mining is not that attractive to anyone but people who actually want to support the project. Move to a GPU algo. RandomX is a good candidate, but CPU mining is not that attractive to anyone but people who actually want to support the project.
Sadly, CPUs won't incentivize people who want to profit, which let's be fair, is the majority of miners. Sadly, CPUs won't incentivize people who want to profit, which let's be fair, is the majority of miners.
IPv6 support for the P2P node. Come on guys, it's 2026. RFC 2460 was in 1998. It's about time.
Like if someone is behind NAT, fine, workable. CGNAT? Lmao good luck.
+27 -85
View File
@@ -9,6 +9,9 @@
#include <crypto/crypto.h> #include <crypto/crypto.h>
#include <uint256.h> #include <uint256.h>
#include <time.h> #include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
typedef struct { typedef struct {
uint8_t bytes[32]; uint8_t bytes[32];
@@ -252,96 +255,35 @@ static inline bool ParseHexAddress32(const char* in, uint8_t outAddress[32]) {
} }
static inline bool IsValidIPv4(const char* ip) { static inline bool IsValidIPv4(const char* ip) {
if (!ip || *ip == '\0') { struct addrinfo hints, *res;
return false; int status;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // Only IPv4
hints.ai_socktype = AI_NUMERICHOST; // Only numeric addresses
status = getaddrinfo(ip, NULL, &hints, &res);
if (status == 0) {
freeaddrinfo(res);
return true;
} }
return false;
int octetCount = 0;
const char* p = ip;
while (*p != '\0') {
if (octetCount >= 4) {
return false;
}
if (*p < '0' || *p > '9') {
return false;
}
unsigned int value = 0;
int digits = 0;
while (*p >= '0' && *p <= '9') {
value = (value * 10u) + (unsigned int)(*p - '0');
if (value > 255u) {
return false;
}
++digits;
if (digits > 3) {
return false;
}
++p;
}
if (digits == 0) {
return false;
}
++octetCount;
if (octetCount < 4) {
if (*p != '.') {
return false;
}
++p;
if (*p == '\0') {
return false;
}
}
}
return octetCount == 4;
} }
static inline bool IsValidIPv6(const char* ip) { static inline bool IsValidIPv6(const char* ip) {
if (!ip || *ip == '\0') { struct addrinfo hints, *res;
return false; int status;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET6; // Only IPv6
hints.ai_socktype = AI_NUMERICHOST; // Only numeric addresses
status = getaddrinfo(ip, NULL, &hints, &res);
if (status == 0) {
freeaddrinfo(res);
return true;
} }
return false;
int colonCount = 0;
const char* p = ip;
while (*p != '\0') {
if (colonCount > 7) {
return false;
}
int hexDigits = 0;
while ((*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'f') ||
(*p >= 'A' && *p <= 'F')) {
++hexDigits;
if (hexDigits > 4) {
return false;
}
++p;
}
if (hexDigits == 0) {
return false;
}
++colonCount;
if (colonCount < 8) {
if (*p != ':') {
return false;
}
++p;
if (*p == '\0') {
return false;
}
}
}
return colonCount == 8;
} }
static inline void Uint256ToDecimal(const uint256_t* value, char* out, size_t outSize) { static inline void Uint256ToDecimal(const uint256_t* value, char* out, size_t outSize) {