I think this needs a fresh pair of eyes / fresh perspective (bare in mind that the code is in "Mid-Test-State"):
This line is to exit the Forever loop when the last packet is received - we will have to come up with a clean Break later - modify it to fit your needs.
- BuildPayload() Procedure added
-- orders the packets
-- combines the packet memory (all or whatever count you set @ the Break)
-- creates a file: PureBasic.deflate (this could be the problem - needs to be decoded in memory?)
Website I'm using to test with (GZIP-compatible encoding needs to be checked - I guess?):
http://i-tools.org/gzip
(this could be the problem - compression is not compatible?)
- testing data @ the website to confirm that I'm on the right track, but only receiving a garbled mess; I wasn't expecting a fully intact webpage, but I was hoping for some partially readable text.
Code: Select all
#DIVERT_LAYER_NETWORK = 0
#DIVERT_PRIORITY_DEFAULT = 0
#DIVERT_FLAG_SNIFF = 1
#MAXBUF = $FFFF
Structure DIVERT_ADDRESS
IfIdx.l
SubIfIdx.l
Direction.a
EndStructure
Structure DIVERT_IPHDR
StructureUnion
HdrLength.a
Version.a
EndStructureUnion
TOS.a
Length.u
Id.u
FragOff0.u
TTL.a
Protocol.a
Checksum.u
SrcAddr.l
DstAddr.l
EndStructure
Structure DIVERT_TCPHDR
SrcPort.u
DstPort.u
SeqNum.l
AckNum.l
StructureUnion
Reserved1.a
HdrLength.a
EndStructureUnion
StructureUnion
Fin.a
Syn.a
Rst.a
Psh.a
Ack.a
Urg.a
Reserved2.a
EndStructureUnion
Window.u
Checksum.u
UrgPtr.u
EndStructure
Structure PAYLOAD
Id.u
*ppData
pDataLen.l
EndStructure
Prototype protoDivertOpen(filter.s, layer, priority.u, flags.q)
Global DivertOpen.protoDivertOpen
Prototype.b protoDivertRecv(handle, *pPacket, packetLen, pAddr, recvLen)
Global DivertRecv.protoDivertRecv
Prototype.b protoDivertHelperParsePacket(*pPacket, packetLen, *ppIpHdr, *ppIpv6Hdr, *ppIcmpHdr, *ppIcmpv6Hdr, *ppTcpHdr, *ppUdpHdr, *ppData, pDataLen)
Global DivertHelperParsePacket.protoDivertHelperParsePacket
Prototype.b protoDivertClose(handle)
Global DivertClose.protoDivertClose
Global Dim pPL.PAYLOAD(0)
Procedure BuildPayload()
SortStructuredArray(pPL(), #PB_Sort_Ascending, OffsetOf(PAYLOAD\Id), TypeOf(PAYLOAD\Id))
For pCount = 0 To ArraySize(pPL()) - 1
Debug Str(pPL(pCount)\Id) + " (" + Str(pPL(pCount)\pDataLen) + ")"
Debug "---------------"
If *Payload = #Null
plSize = 0
*Payload = AllocateMemory(pPL(pCount)\pDataLen)
Else
plSize = MemorySize(*Payload)
*Payload = ReAllocateMemory(*Payload, plSize + pPL(pCount)\pDataLen)
EndIf
CopyMemory(pPL(pCount)\ppData, *Payload + plSize, pPL(pCount)\pDataLen)
Next
If CreateFile(0, "PureBasic.deflate")
WriteData(0, *Payload, MemorySize(*Payload))
CloseFile(0)
EndIf
FreeMemory(*Payload)
EndProcedure
WinDivert = OpenLibrary(#PB_Any, "WinDivert.dll")
If IsLibrary(WinDivert)
DivertOpen = GetFunction(WinDivert, "DivertOpen")
DivertSetParam = GetFunction(WinDivert, "DivertSetParam")
DivertRecv = GetFunction(WinDivert, "DivertRecv")
DivertHelperParsePacket = GetFunction(WinDivert, "DivertHelperParsePacket")
DivertClose = GetFunction(WinDivert, "DivertClose")
filter.s = "(ip.SrcAddr == 88.191.144.148 || ip.DstAddr == 88.191.144.148) && tcp.PayloadLength > 0"
hWndDivert = DivertOpen(filter, #DIVERT_LAYER_NETWORK, #DIVERT_PRIORITY_DEFAULT, #DIVERT_FLAG_SNIFF)
If hWndDivert <> #INVALID_HANDLE_VALUE
pAddr.DIVERT_ADDRESS
*ppIpHdr.DIVERT_IPHDR
*ppTcpHdr.DIVERT_TCPHDR
RunProgram("iexplore", "http://www.purebasic.com/", "")
Repeat
*pPacket = AllocateMemory(#MAXBUF)
If DivertRecv(hWndDivert, *pPacket, #MAXBUF, @pAddr, @recvLen)
DivertHelperParsePacket(*pPacket, recvLen, @*ppIpHdr, #Null, #Null, #Null, @*ppTcpHdr, #Null, @*ppData, @pDataLen)
If *ppData
PacketData.s = PeekS(*ppData, pDataLen, #PB_UTF8)
If FindString(PacketData, "HTTP") = 0
Redim pPL(pCount)
pPL(pCount)\Id = ntohs_(PeekU(@*ppIpHdr\Id))
pPL(pCount)\ppData = *ppData
pPL(pCount)\pDataLen = pDataLen
pCount + 1
If pCount = 13 : Break : EndIf
EndIf
EndIf
EndIf
FreeMemory(*pPacket)
ForEver
DivertClose(hWndDivert)
EndIf
CloseLibrary(WinDivert)
RunProgram("sc", "stop WinDivert1.0", "", #PB_Program_Hide)
RunProgram("sc", "delete WinDivert1.0", "", #PB_Program_Hide)
BuildPayload()
EndIf