Figured out a way to split the "address" part of the buffer from the packet

data itself.  This makes for a much cleaner interface.

sys/divert.c
dll/divert.c
include/*.h
	DivertRecv and DivertSend now use IOCTLs instead of reads/writes.
	The 'address' parameter is passed by pointer to the driver, which
	writes directly to it (after sanity checks).
	This means that the data buffer now only contains the packet, which
	help to avoid some messy code.

examples/*/*.c
	Update the examples to reflect the new API.

doc/divert.html
	Update the documentation to reflect the new API.
This commit is contained in:
basil00
2011-08-21 17:59:13 +08:00
parent cf80d12e5d
commit ee386df032
8 changed files with 371 additions and 375 deletions
+8 -13
View File
@@ -38,23 +38,16 @@ extern "C" {
*/
typedef struct
{
UINT8 Reserved[7]; // Reserved for internal use.
UINT8 Direction; // Packet's direction.
UINT32 IfIdx; // Packet's interface index.
UINT32 SubIfIdx; // Packet's sub-interface index.
} DIVERT_PACKET, *PDIVERT_PACKET;
UINT8 Direction; // Packet's direction.
} DIVERT_ADDRESS, *PDIVERT_ADDRESS;
#ifndef DIVERT_PACKET_DIRECTION_OUTBOUND
#define DIVERT_PACKET_DIRECTION_OUTBOUND 0
#define DIVERT_PACKET_DIRECTION_INBOUND 1
#endif /* DIVERT_PACKET_DIRECTION_OUTBOUND */
#define DIVERT_PACKET_DIRECTION(pPacket) (pPacket->Direction)
#define DIVERT_PACKET_INTERFACE_INDEX(pPacket) (pPacket->IfIdx)
#define DIVERT_PACKET_SUB_INTERFACE_INDEX(pPacket) (pPacket->SubIfIdx)
#define DIVERT_PACKET_DATA(pPacket) \
((PVOID)(pPacket+1))
/*
* Open a handle to the divert device with the given filter.
*/
@@ -66,8 +59,9 @@ extern DIVERTEXPORT HANDLE DivertOpen(
*/
extern DIVERTEXPORT BOOL DivertRecv(
__in HANDLE handle,
__inout PDIVERT_PACKET pPacket,
__out PVOID pPacket,
__in UINT packetLen,
__out PDIVERT_ADDRESS pAddr,
__out_opt UINT *readLen);
/*
@@ -75,8 +69,9 @@ extern DIVERTEXPORT BOOL DivertRecv(
*/
extern DIVERTEXPORT BOOL DivertSend(
__in HANDLE handle,
__in PDIVERT_PACKET pPacket,
__in PVOID pPacket,
__in UINT packetLen,
__in PDIVERT_ADDRESS pAddr,
__out_opt UINT *writeLen);
/*
@@ -238,7 +233,7 @@ typedef struct
* Parse IPv4/IPv6/ICMP/ICMPv6/TCP/UDP headers from a raw packet.
*/
extern DIVERTEXPORT BOOL DivertHelperParse(
__in PDIVERT_PACKET pPacket,
__in PVOID pPacket,
__in UINT packetLen,
__out_opt PDIVERT_IPHDR *ppIpHdr,
__out_opt PDIVERT_IPV6HDR *ppIpv6Hdr,
@@ -253,7 +248,7 @@ extern DIVERTEXPORT BOOL DivertHelperParse(
* Calculate IPv4/IPv6/ICMP/ICMPv6/TCP/UDP checksums.
*/
extern DIVERTEXPORT UINT DivertHelperCalcChecksums(
__inout PDIVERT_PACKET pPacket,
__inout PVOID pPacket,
__in UINT packetLen,
__in UINT64 flags);
+9 -9
View File
@@ -16,18 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* NOTE: This file is NOT part of the divert API. For the divert API, include
* "divert.h" instead.
*/
#ifndef __DIVERT_DEVICE_H
#define __DIVERT_DEVICE_H
#define DIVERT_DEVICE_NAME L"\\Device\\Divert"
#define DIVERT_DOS_DEVICE_NAME L"\\??\\Divert"
#define DIVERT_VERSION 0
#define DIVERT_VERSION 1
#define DIVERT_MAGIC 0xF8D3
#define DIVERT_FILTER_FIELD_ZERO 0
@@ -115,13 +110,14 @@
/*
* Message definitions.
*/
struct divert_message_s
struct divert_ioctl_s
{
UINT16 magic; // DIVERT_MAGIC
UINT8 version; // DIVERT_VERSION
UINT8 reserved; // Reserved (set to 0x0)
PVOID arg; // Pointer to buffer
};
typedef struct divert_message_s *divert_message_t;
typedef struct divert_ioctl_s *divert_ioctl_t;
/*
* IOCTL structures.
@@ -139,7 +135,11 @@ typedef struct divert_ioctl_filter_s *divert_ioctl_filter_t;
/*
* IOCTL codes.
*/
#define IOCTL_DIVERT_RECV \
CTL_CODE(FILE_DEVICE_NETWORK, 0x908, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
#define IOCTL_DIVERT_SEND \
CTL_CODE(FILE_DEVICE_NETWORK, 0x909, METHOD_IN_DIRECT, FILE_ANY_ACCESS)
#define IOCTL_DIVERT_SET_FILTER \
CTL_CODE(FILE_DEVICE_NETWORK, 0x90A, METHOD_BUFFERED, FILE_ANY_ACCESS)
CTL_CODE(FILE_DEVICE_NETWORK, 0x90A, METHOD_IN_DIRECT, FILE_ANY_ACCESS)
#endif // __DIVERT_DEVICE_H