How to get data from TCPPacket using SharpPcap?
Asked Answered
S

2

6

I need small sniffer for my report so I went with C# and SharpPcap.

packet = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);
tcpPacket = TcpPacket.GetEncapsulated(packet);

I have raw packet with all packet data. Then I get Packet out of it with some info such as source/destination/protocol/etc. After that I get tcp packet out of that.

Now how do I get only actual packet data that software sent/received?

Spurrier answered 5/10, 2011 at 0:43 Comment(0)
S
12

Was simple enough.

packet.PayloadPacket.PayloadPacket.PayloadData
Spurrier answered 5/10, 2011 at 1:33 Comment(2)
you're my hero :))Banneret
Thanks for answering your own question. You just saved me 30 minutes of experimenting =). +1 for question and answerCoiffure
A
3

tcpPacket.PayloadData or PayloadPacket should be what you want. PayloadData is valid if Packet.Net isn't aware of how to parse the data into a sub-packet, and PayloadPacket is valid if it does. I'm not sure any tcp packet parsing occurs today so you should be safe saying tcpPacket.PayloadData.

Maybe the api should be changed such that PayloadData is always the raw data in the tcp packet and PayloadPacket is null or non-null depending on whether the contents of the packet were parsed. Then you could get the contents of any packet, Ethernet, IP etc by using PayloadData. Thoughts?

You could refer to some nested PayloadPacket of 'packet' but it is difficult to know how the packet is constructed. Is it Ethernet->IP->Tcp or are there wrappers around the Ethernet packet? With TcpPacket.GetEncapsulated() these situations are handled for you.

Chris

Author of SharpPcap/Packet.Net

Achondrite answered 5/10, 2011 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.