Fix IPv6 bytes ordering again

This commit is contained in:
Nodir Temirkhodjaev 2022-05-24 11:42:43 +03:00
parent 828fb335d3
commit 8bb1a594d2
8 changed files with 48 additions and 41 deletions

View File

@ -44,9 +44,7 @@
# endif # endif
#endif #endif
#if 0 #define FORT_BIG_ENDIAN 0
# define FORT_BIG_ENDIAN 1
#endif
#if defined(FORT_DRIVER) #if defined(FORT_DRIVER)
# define LOG(...) DbgPrintEx(DPFLTR_SYSTEM_ID, DPFLTR_ERROR_LEVEL, "FORT: " __VA_ARGS__) # define LOG(...) DbgPrintEx(DPFLTR_SYSTEM_ID, DPFLTR_ERROR_LEVEL, "FORT: " __VA_ARGS__)

View File

@ -30,16 +30,6 @@ FORT_API BOOL is_time_in_period(FORT_TIME time, FORT_PERIOD period)
return (from <= to ? (x >= from && x < (to - 1)) : (x >= from || x < (to - 1))); return (from <= to ? (x >= from && x < (to - 1)) : (x >= from || x < (to - 1)));
} }
FORT_API int fort_ip6_cmp(const ip6_addr_t *l, const ip6_addr_t *r)
{
INT64 res = (INT64) l->hi64 - (INT64) r->hi64;
if (res == 0) {
res = (INT64) l->lo64 - (INT64) r->lo64;
}
return res == 0 ? 0 : (res > 0 ? 1 : -1);
}
static BOOL fort_conf_ip4_find(const UINT32 *iparr, UINT32 ip, UINT32 count, BOOL is_range) static BOOL fort_conf_ip4_find(const UINT32 *iparr, UINT32 ip, UINT32 count, BOOL is_range)
{ {
if (count == 0) if (count == 0)
@ -66,6 +56,8 @@ static BOOL fort_conf_ip4_find(const UINT32 *iparr, UINT32 ip, UINT32 count, BOO
return high >= 0 && ip >= iparr[high] && ip <= iparr[count + high]; return high >= 0 && ip >= iparr[high] && ip <= iparr[count + high];
} }
#define fort_ip6_cmp(l, r) fort_memcmp(l, r, sizeof(ip6_addr_t))
static BOOL fort_conf_ip6_find( static BOOL fort_conf_ip6_find(
const ip6_addr_t *iparr, const ip6_addr_t *ip, UINT32 count, BOOL is_range) const ip6_addr_t *iparr, const ip6_addr_t *ip, UINT32 count, BOOL is_range)
{ {

View File

@ -234,8 +234,6 @@ FORT_API int bit_scan_forward(unsigned long mask);
FORT_API BOOL is_time_in_period(FORT_TIME time, FORT_PERIOD period); FORT_API BOOL is_time_in_period(FORT_TIME time, FORT_PERIOD period);
FORT_API int fort_ip6_cmp(const ip6_addr_t *l, const ip6_addr_t *r);
FORT_API BOOL fort_conf_ip_inlist( FORT_API BOOL fort_conf_ip_inlist(
const UINT32 *ip, const PFORT_CONF_ADDR4_LIST addr_list, BOOL isIPv6); const UINT32 *ip, const PFORT_CONF_ADDR4_LIST addr_list, BOOL isIPv6);

View File

@ -290,7 +290,7 @@ FORT_API NTSTATUS fort_file_open(PUNICODE_STRING filePath, HANDLE *outHandle)
USHORT fort_le_u16_read(const char *cp, int offset) USHORT fort_le_u16_read(const char *cp, int offset)
{ {
USHORT v = *((USHORT *) (cp + offset)); USHORT v = *((USHORT *) (cp + offset));
#ifdef FORT_BIG_ENDIAN #if FORT_BIG_ENDIAN
RtlUshortByteSwap(v); RtlUshortByteSwap(v);
#endif #endif
return v; return v;
@ -299,7 +299,7 @@ USHORT fort_le_u16_read(const char *cp, int offset)
DWORD fort_le_u32_read(const char *cp, int offset) DWORD fort_le_u32_read(const char *cp, int offset)
{ {
DWORD v = *((DWORD *) (cp + offset)); DWORD v = *((DWORD *) (cp + offset));
#ifdef FORT_BIG_ENDIAN #if FORT_BIG_ENDIAN
RtlUlongByteSwap(v); RtlUlongByteSwap(v);
#endif #endif
return v; return v;
@ -309,7 +309,7 @@ BOOL fort_addr_is_local_broadcast(const UINT32 *ip, BOOL isIPv6)
{ {
if (isIPv6) { if (isIPv6) {
const ip6_addr_t *ip6 = (const ip6_addr_t *) ip; const ip6_addr_t *ip6 = (const ip6_addr_t *) ip;
return ip6->addr16[7] == 0xFF02; return ip6->addr16[0] == 0x2FF;
} }
return *ip == 0xFFFFFFFF; return *ip == 0xFFFFFFFF;

View File

@ -84,8 +84,9 @@ TEST_F(ConfUtilTest, confWriteRead)
ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("192.168.255.255"))); ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("192.168.255.255")));
ASSERT_FALSE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("193.0.0.0"))); ASSERT_FALSE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("193.0.0.0")));
ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("239.255.255.250"))); ASSERT_TRUE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("239.255.255.250")));
ASSERT_FALSE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::2"))); ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::2")));
ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::ffff:0:2"))); ASSERT_TRUE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("::ffff:0:2")));
ASSERT_FALSE(DriverCommon::confIp6InRange(data, NetUtil::textToIp6("65::")));
qint8 blockReason = FORT_BLOCK_REASON_UNKNOWN; qint8 blockReason = FORT_BLOCK_REASON_UNKNOWN;

View File

@ -36,6 +36,36 @@ TEST_F(NetUtilTest, ip6Text)
ASSERT_EQ(NetUtil::ip6ToText(NetUtil::textToIp6(ip6Str)), ip6Str); ASSERT_EQ(NetUtil::ip6ToText(NetUtil::textToIp6(ip6Str)), ip6Str);
} }
TEST_F(NetUtilTest, ip6Bytes01)
{
const ip6_addr_t ip = NetUtil::textToIp6("ff02::1:3");
ASSERT_EQ(ip.addr32[0], 0x2ff);
ASSERT_EQ(ip.addr32[1], 0);
ASSERT_EQ(ip.addr32[2], 0);
ASSERT_EQ(ip.addr32[3], 0x3000100);
}
TEST_F(NetUtilTest, ip6Bytes02)
{
const ip6_addr_t ip = NetUtil::textToIp6("fe80::e58c:84f8:a156:2a23");
ASSERT_EQ(ip.addr32[0], 0x80fe);
ASSERT_EQ(ip.addr32[1], 0);
ASSERT_EQ(ip.addr32[2], 0xf8848ce5);
ASSERT_EQ(ip.addr32[3], 0x232a56a1);
}
TEST_F(NetUtilTest, ip6Mask01)
{
const ip6_addr_t ip = NetUtil::applyIp6Mask(NetUtil::textToIp6("::2"), 126);
ASSERT_EQ(ip.addr32[0], 0);
ASSERT_EQ(ip.addr32[1], 0);
ASSERT_EQ(ip.addr32[2], 0);
ASSERT_EQ(ip.addr32[3], 0x03000000);
}
TEST_F(NetUtilTest, ip4Ranges) TEST_F(NetUtilTest, ip4Ranges)
{ {
IpRange ipRange; IpRange ipRange;

View File

@ -11,8 +11,7 @@ namespace {
bool compareLessIp6(const ip6_addr_t &l, const ip6_addr_t &r) bool compareLessIp6(const ip6_addr_t &l, const ip6_addr_t &r)
{ {
const qint64 res = qint64(l.hi64) - qint64(r.hi64); return memcmp(&l, &r, sizeof(ip6_addr_t)) < 0;
return res < 0 || (res == 0 && qint64(l.lo64) < qint64(r.lo64));
} }
void sortIp6Array(ip6_arr_t &array) void sortIp6Array(ip6_arr_t &array)

View File

@ -20,18 +20,6 @@ struct sock_addr
#define sock_addr_get_inp(sap) ((void *) &(sap)->u.in.sin_addr) #define sock_addr_get_inp(sap) ((void *) &(sap)->u.in.sin_addr)
namespace {
ip6_addr_t swapIp6(const ip6_addr_t &ip)
{
ip6_addr_t ip6;
ip6.lo64 = ntohll(ip.hi64);
ip6.hi64 = ntohll(ip.lo64);
return ip6;
}
}
quint32 NetUtil::textToIp4(const QString &text, bool *ok) quint32 NetUtil::textToIp4(const QString &text, bool *ok)
{ {
quint32 ip4; quint32 ip4;
@ -70,16 +58,14 @@ ip6_addr_t NetUtil::textToIp6(const QString &text, bool *ok)
memset(&ip6, 0, sizeof(ip6)); memset(&ip6, 0, sizeof(ip6));
} }
return swapIp6(ip6); return ip6;
} }
QString NetUtil::ip6ToText(const ip6_addr_t &ip) QString NetUtil::ip6ToText(const ip6_addr_t &ip)
{ {
wchar_t buf[MAX_IPV6_LEN]; wchar_t buf[MAX_IPV6_LEN];
const ip6_addr_t ip6 = swapIp6(ip); if (!InetNtopW(AF_INET6, (PVOID) &ip, buf, MAX_IPV6_LEN))
if (!InetNtopW(AF_INET6, (PVOID) &ip6, buf, MAX_IPV6_LEN))
return QString(); return QString();
return QString::fromWCharArray(buf); return QString::fromWCharArray(buf);
@ -103,16 +89,19 @@ quint32 NetUtil::applyIp4Mask(quint32 ip, int nbits)
ip6_addr_t NetUtil::applyIp6Mask(const ip6_addr_t &ip, int nbits) ip6_addr_t NetUtil::applyIp6Mask(const ip6_addr_t &ip, int nbits)
{ {
ip6_addr_t ip6 = ip; ip6_addr_t ip6 = ip;
quint64 *masked = &ip6.lo64; quint64 *masked = &ip6.hi64;
if (nbits <= 64) { if (nbits <= 64) {
ip6.lo64 = quint64(-1LL); *masked = quint64(-1LL);
masked = &ip6.hi64; masked = &ip6.lo64;
} else { } else {
nbits -= 64; nbits -= 64;
} }
*masked |= nbits == 0 ? quint64(-1LL) : (nbits == 64 ? 0 : ((1LL << (64 - nbits)) - 1)); quint64 maskedHost = ntohll(*masked);
maskedHost |= nbits == 0 ? quint64(-1LL) : (nbits == 64 ? 0 : ((1ULL << (64 - nbits)) - 1));
*masked = htonll(maskedHost);
return ip6; return ip6;
} }