Change functions IsValidIPv4/v6 to use addrinfo instead of custom logic
This commit is contained in:
@@ -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.
|
||||
|
||||
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:
|
||||
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.
|
||||
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.
|
||||
|
||||
+23
-81
@@ -9,6 +9,9 @@
|
||||
#include <crypto/crypto.h>
|
||||
#include <uint256.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t bytes[32];
|
||||
@@ -252,98 +255,37 @@ static inline bool ParseHexAddress32(const char* in, uint8_t outAddress[32]) {
|
||||
}
|
||||
|
||||
static inline bool IsValidIPv4(const char* ip) {
|
||||
if (!ip || *ip == '\0') {
|
||||
return false;
|
||||
}
|
||||
struct addrinfo hints, *res;
|
||||
int status;
|
||||
|
||||
int octetCount = 0;
|
||||
const char* p = ip;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // Only IPv4
|
||||
hints.ai_socktype = AI_NUMERICHOST; // Only numeric addresses
|
||||
|
||||
while (*p != '\0') {
|
||||
if (octetCount >= 4) {
|
||||
status = getaddrinfo(ip, NULL, &hints, &res);
|
||||
if (status == 0) {
|
||||
freeaddrinfo(res);
|
||||
return true;
|
||||
}
|
||||
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) {
|
||||
if (!ip || *ip == '\0') {
|
||||
return false;
|
||||
}
|
||||
struct addrinfo hints, *res;
|
||||
int status;
|
||||
|
||||
int colonCount = 0;
|
||||
const char* p = ip;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET6; // Only IPv6
|
||||
hints.ai_socktype = AI_NUMERICHOST; // Only numeric addresses
|
||||
|
||||
while (*p != '\0') {
|
||||
if (colonCount > 7) {
|
||||
status = getaddrinfo(ip, NULL, &hints, &res);
|
||||
if (status == 0) {
|
||||
freeaddrinfo(res);
|
||||
return true;
|
||||
}
|
||||
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) {
|
||||
if (!value || !out || outSize == 0) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user