如何使用pcapdotnet将数据包拆分为两个数据包?这是我尝试过的,但我不知道它是否正确:
public IEnumerable<Packet> splitPacket(Packet packet)
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
DateTime packetTimestamp = packet.Timestamp;
ILayer payload = packet.Ethernet.IpV4.Payload.ExtractLayer();
IpV4Fragmentation.Equals(packet, packet);
yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, payload);
}
答案 0 :(得分:1)
我从未使用过Pcap.Net,所以我不确定这是否可行,但一般的想法是将数据(“有效载荷层”)分成几个块,然后发送它。为了确保可以重新组合片段,您还需要添加一些有关每个片段的位置(偏移)的信息。
在Pcap.Net中,IpV4Fragmentation
类包含两个定义它的属性:
IpV4Fragmentation.Options
:
IpV4FragmentationOptions.MoreFragments
,IpV4FragmentationOptions.None
IpV4Fragmentation.Offset
:
考虑到这一点,我会写这样的东西:
(免责声明:这是用记事本写的,如果它甚至可以编译,我也不知道,更不用说它应该工作了):
public IEnumerable<Packet> Split(Packet packet, int numberOfFragments)
{
// get original layers
var ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
var ipV4 = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
var time = packet.Timestamp;
// extract the data
var payload = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer();
var totalLength = payload.Length;
// split data into smaller segments
var partialLength = totalLength / numberOfFragments;
// make sure it's divisible with 8
// (see http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly)
partialLength = (partialLength / 8) * 8;
// send one by one
var offset = 0;
while (offset < totalLength)
{
// get length for this fragment
var fragmentLength = partialLength;
var options = IpV4FragmentationOptions.MoreFragments;
// is this the last fragment? trim length if needed
if (offset + fragmentLength >= totalLength)
{
options = IpV4FragmentationOptions.None;
fragmentLength = totalLength - offset;
}
// copy the actual data into a new buffer
var newBuffer = payload.ReadBytes(offset, fragmentLength);
var newPayload = new PayloadLayer() { Data = newBuffer };
// change IP layer fragmentation options
ipV4.Fragmentation = new IpV4Fragmentation(options, offset);
// return it
yield return PacketBuilder.Build(time, ethernet, ipV4, newPayload);
// next offset
offset += fragmentLength;
}
}
[更新@ brickner的建议]