Add byte ordering helper functions to WinDivert.

Some sample programs were linking to WinSock2
just for ntohl()/ntohs().  This dependency can
now be removed.
This commit is contained in:
basil00
2018-11-16 07:50:04 +08:00
parent 8ff3c86330
commit 54e627e82b
8 changed files with 113 additions and 39 deletions
+8
View File
@@ -20,3 +20,11 @@ EXPORTS
WinDivertHelperCompileFilter
WinDivertHelperEvalFilter
WinDivertHelperFormatFilter
WinDivertHelperNtohs
WinDivertHelperHtons
WinDivertHelperNtohl
WinDivertHelperHtonl
WinDivertHelperNtohll
WinDivertHelperHtonll
WinDivertHelperNtohIpv6Address
WinDivertHelperHtonIpv6Address
+48
View File
@@ -4527,3 +4527,51 @@ extern UINT64 WinDivertHelperHashPacket(const VOID *pPacket, UINT packetLen,
icmpv6_header, tcp_header, udp_header);
}
/*
* Byte ordering.
*/
extern UINT16 WinDivertHelperNtohs(UINT16 x)
{
return BYTESWAP16(x);
}
extern UINT16 WinDivertHelperHtons(UINT16 x)
{
return BYTESWAP16(x);
}
extern UINT32 WinDivertHelperNtohl(UINT32 x)
{
return BYTESWAP32(x);
}
extern UINT32 WinDivertHelperHtonl(UINT32 x)
{
return BYTESWAP32(x);
}
extern UINT64 WinDivertHelperNtohll(UINT64 x)
{
return BYTESWAP64(x);
}
extern UINT64 WinDivertHelperHtonll(UINT64 x)
{
return BYTESWAP64(x);
}
static void WinDivertByteSwap128(const UINT *inAddr, UINT *outAddr)
{
UINT32 tmp[4], i; // tmp[] allows overlapping
for (i = 0; i < 4; i++)
{
tmp[3-i] = BYTESWAP32(inAddr[0]);
}
for (i = 0; i < 4; i++)
{
outAddr[i] = tmp[i];
}
}
extern void WinDivertHelperNtohIpv6Address(const UINT *inAddr, UINT *outAddr)
{
WinDivertByteSwap128(inAddr, outAddr);
}
extern void WinDivertHelperHtonIpv6Address(const UINT *inAddr, UINT *outAddr)
{
WinDivertByteSwap128(inAddr, outAddr);
}
+9
View File
@@ -45,6 +45,15 @@
#define BYTESWAP32(x) \
((((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) | \
(((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000))
#define BYTESWAP64(x) \
((((x) >> 56) & 0x00000000000000FFull) | \
(((x) >> 40) & 0x000000000000FF00ull) | \
(((x) >> 24) & 0x0000000000FF0000ull) | \
(((x) >> 8) & 0x00000000FF000000ull) | \
(((x) << 8) & 0x000000FF00000000ull) | \
(((x) << 24) & 0x0000FF0000000000ull) | \
(((x) << 40) & 0x00FF000000000000ull) | \
(((x) << 56) & 0xFF00000000000000ull))
#define ntohs(x) BYTESWAP16(x)
#define htons(x) BYTESWAP16(x)
#define ntohl(x) BYTESWAP32(x)
+5 -15
View File
@@ -41,7 +41,6 @@
*
*/
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
@@ -49,21 +48,12 @@
#include "windivert.h"
#define ntohs(x) WinDivertHelperNtohs(x)
#define ntohl(x) WinDivertHelperNtohl(x)
#define MAXBUF 0xFFFF
#define INET6_ADDRSTRLEN 45
/*
* IPv6 address byte swap.
*/
void byteswap128(UINT32 *dst_addr, const UINT32 *src_addr)
{
int i;
for (i = 0; i < 4; i++)
{
dst_addr[i] = ntohl(src_addr[4-i-1]);
}
}
/*
* Entry.
*/
@@ -198,8 +188,8 @@ int __cdecl main(int argc, char **argv)
}
if (ipv6_header != NULL)
{
byteswap128(src_addr, ipv6_header->SrcAddr);
byteswap128(dst_addr, ipv6_header->DstAddr);
WinDivertHelperNtohIpv6Address(ipv6_header->SrcAddr, src_addr);
WinDivertHelperNtohIpv6Address(ipv6_header->DstAddr, dst_addr);
WinDivertHelperFormatIPv6Address(src_addr, src_str,
sizeof(src_str));
WinDivertHelperFormatIPv6Address(dst_addr, dst_str,
+8 -15
View File
@@ -47,7 +47,6 @@
* This program is similar to Linux's iptables with the "-j REJECT" target.
*/
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
@@ -55,8 +54,14 @@
#include "windivert.h"
#define ntohs(x) WinDivertHelperNtohs(x)
#define ntohl(x) WinDivertHelperNtohl(x)
#define htons(x) WinDivertHelperHtons(x)
#define htonl(x) WinDivertHelperHtonl(x)
#define MAXBUF 0xFFFF
#define INET6_ADDRSTRLEN 45
#define IPPROTO_ICMPV6 58
/*
* Pre-fabricated packets.
@@ -97,18 +102,6 @@ static void PacketIpv6Init(PWINDIVERT_IPV6HDR packet);
static void PacketIpv6TcpInit(PTCPV6PACKET packet);
static void PacketIpv6Icmpv6Init(PICMPV6PACKET packet);
/*
* IPv6 address byte swap.
*/
void byteswap128(UINT32 *dst_addr, const UINT32 *src_addr)
{
int i;
for (i = 0; i < 4; i++)
{
dst_addr[i] = ntohl(src_addr[4-i-1]);
}
}
/*
* Entry.
*/
@@ -230,8 +223,8 @@ int __cdecl main(int argc, char **argv)
}
if (ipv6_header != NULL)
{
byteswap128(src_addr, ipv6_header->SrcAddr);
byteswap128(dst_addr, ipv6_header->DstAddr);
WinDivertHelperNtohIpv6Address(ipv6_header->SrcAddr, src_addr);
WinDivertHelperNtohIpv6Address(ipv6_header->DstAddr, dst_addr);
WinDivertHelperFormatIPv6Address(src_addr, src_str,
sizeof(src_str));
WinDivertHelperFormatIPv6Address(dst_addr, dst_str,
+5 -1
View File
@@ -42,13 +42,17 @@
* blockpage to the browser.
*/
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "windivert.h"
#define ntohs(x) WinDivertHelperNtohs(x)
#define ntohl(x) WinDivertHelperNtohl(x)
#define htons(x) WinDivertHelperHtons(x)
#define htonl(x) WinDivertHelperHtonl(x)
#define MAXBUF 0xFFFF
#define MAXURL 4096
+23 -1
View File
@@ -530,12 +530,34 @@ extern WINDIVERTEXPORT BOOL WinDivertHelperEvalFilter(
/*
* Format the given filter string.
*/
extern BOOL WinDivertHelperFormatFilter(
extern WINDIVERTEXPORT BOOL WinDivertHelperFormatFilter(
__in const char *filter,
__in WINDIVERT_LAYER layer,
__out char *buffer,
__in UINT bufLen);
/*
* Byte ordering.
*/
extern WINDIVERTEXPORT UINT16 WinDivertHelperNtohs(
__in UINT16 x);
extern WINDIVERTEXPORT UINT16 WinDivertHelperHtons(
__in UINT16 x);
extern WINDIVERTEXPORT UINT32 WinDivertHelperNtohl(
__in UINT32 x);
extern WINDIVERTEXPORT UINT32 WinDivertHelperHtonl(
__in UINT32 x);
extern WINDIVERTEXPORT UINT64 WinDivertHelperNtohll(
__in UINT64 x);
extern WINDIVERTEXPORT UINT64 WinDivertHelperHtonll(
__in UINT64 x);
extern WINDIVERTEXPORT void WinDivertHelperNtohIpv6Address(
__in const UINT *inAddr,
__out UINT *outAddr);
extern WINDIVERTEXPORT void WinDivertHelperHtonIpv6Address(
__in const UINT *inAddr,
__out UINT *outAddr);
#endif /* WINDIVERT_KERNEL */
#ifdef __cplusplus
+7 -7
View File
@@ -79,19 +79,19 @@ do
--output-lib install/MINGW/$CPU/WinDivert.lib 2>/dev/null
echo "\tbuild install/MINGW/$CPU/netdump.exe..."
$CC -s -O2 -Iinclude/ examples/netdump/netdump.c \
-o "install/MINGW/$CPU/netdump.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/netdump.exe" -lWinDivert \
-L"install/MINGW/$CPU/"
echo "\tbuild install/MINGW/$CPU/netfilter.exe..."
$CC -s -O2 -Iinclude/ examples/netfilter/netfilter.c \
-o "install/MINGW/$CPU/netfilter.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/netfilter.exe" -lWinDivert \
-L"install/MINGW/$CPU/"
echo "\tbuild install/MINGW/$CPU/passthru.exe..."
$CC -s -O2 -Iinclude/ examples/passthru/passthru.c \
-o "install/MINGW/$CPU/passthru.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/passthru.exe" -lWinDivert \
-L"install/MINGW/$CPU/"
echo "\tbuild install/MINGW/$CPU/webfilter.exe..."
$CC -s -O2 -Iinclude/ examples/webfilter/webfilter.c \
-o "install/MINGW/$CPU/webfilter.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/webfilter.exe" -lWinDivert \
-L"install/MINGW/$CPU/"
echo "\tbuild install/MINGW/$CPU/streamdump.exe..."
$CC -s -O2 -Iinclude/ examples/streamdump/streamdump.c \
@@ -99,15 +99,15 @@ do
-L"install/MINGW/$CPU/"
echo "\tcopy install/MINGW/$CPU/flowtrack.exe..."
$CC -s -O2 -Iinclude/ examples/flowtrack/flowtrack.c \
-o "install/MINGW/$CPU/flowtrack.exe" -lWinDivert -lws2_32 -lpsapi \
-o "install/MINGW/$CPU/flowtrack.exe" -lWinDivert -lpsapi \
-lshlwapi -L"install/MINGW/$CPU/"
echo "\tcopy install/MINGW/$CPU/windivertctl.exe..."
$CC -s -O2 -Iinclude/ examples/windivertctl/windivertctl.c \
-o "install/MINGW/$CPU/windivertctl.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/windivertctl.exe" -lWinDivert \
-lpsapi -lshlwapi -L"install/MINGW/$CPU/"
echo "\tcopy install/MINGW/$CPU/socketdump.exe..."
$CC -s -O2 -Iinclude/ examples/socketdump/socketdump.c \
-o "install/MINGW/$CPU/socketdump.exe" -lWinDivert -lws2_32 \
-o "install/MINGW/$CPU/socketdump.exe" -lWinDivert \
-lpsapi -lshlwapi -L"install/MINGW/$CPU/"
echo "\tcopy install/MINGW/$CPU/WinDivert$BITS.sys..."
cp install/WDDK/$CPU/WinDivert$BITS.sys install/MINGW/$CPU