我正在使用PcapDorNet
DLL,并尝试分割我的流量:
private IEnumerable<Packet> SplitPacket(Packet packet, int numberOfFragments)
{
IpV4Datagram ipV4Datagram = packet.Ethernet.IpV4;
Datagram datagram = packet.Ethernet.IpV4.Payload;
TransportLayer transportlayer = GetTransportLayer(packet);
if (transportlayer != null)
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
ILayer layer = packet.Ethernet.IpV4.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
ipV4Layer.HeaderChecksum = null;
DateTime packetTimestamp = packet.Timestamp;
PayloadLayer payload = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); //extract the data
int totalLength = payload.Length;
int partialLength = totalLength / numberOfFragments; //split data into smaller segments
partialLength = (partialLength / 8) * 8; //make sure it's divisible with 8
if (partialLength == 0)
partialLength = 8;
//(http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly)
ushort offset = 0; //send one by one
while (offset < totalLength)
{
int fragmentLength = partialLength; //get length for this fragment
IpV4FragmentationOptions options = IpV4FragmentationOptions.MoreFragments;
if (offset + fragmentLength >= totalLength) //is this the last fragment ? trim length if needed
{
options = IpV4FragmentationOptions.None;
fragmentLength = totalLength - offset;
}
byte[] newBuffer = ipV4Datagram.Payload.ToArray(); //copy the actual data into a new buffer
PayloadLayer newPayload = new PayloadLayer { Data = new Datagram(newBuffer, offset, fragmentLength) };
ipV4Layer.Fragmentation = new IpV4Fragmentation(options, offset); //change IP layer fragmentation options
transportlayer.Checksum = null;
yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, transportlayer, newPayload);
offset += (ushort)fragmentLength; //next offset
}
}
}
我的输入数据包是TCP packet
,输出是 4 个数据包:3 个IPv4 packets
,最后一个数据包是,TCP
但我收到的这个数据包在 TCP 层字段下显示此消息:
Checksum: 0x5d17 [不正确,应该是0xabb7(可能是“TCP checksum offload”引起的?)]
使用 UDP 似乎每个 UDP 数据包也分成几个 IPv4 数据包,最后一个是 UDP,但在 UDP 情况下它看起来很好。我做错了什么?
请查看原始数据包和分片:http ://www.filedropper.com/desktop_122