0

如何使用 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);
    }
4

1 回答 1

1

我从未使用过 Pcap.Net,所以我不确定这是否可行,但总体思路是将数据(“有效负载层”)分成几个块,然后发送。为了确保片段可以重新组装,您还需要添加一些关于每个片段的位置(偏移)的信息。

在 Pcap.Net 中,IpV4Fragmentation该类包含两个定义此属性的属性:

  • IpV4Fragmentation.Options
    • 对于除最后一个以外的所有片段,它应该设置为IpV4FragmentationOptions.MoreFragments,
    • 对于最后一个片段,它应该设置为IpV4FragmentationOptions.None
  • IpV4Fragmentation.Offset
    • 包含片段的偏移量(必须能被 8 整除)。对于第一个片段,此偏移量为零。

考虑到这一点,我会写这样的东西:

(免责声明:这是用记事本写的,我不知道它是否可以编译,更不用说按原样工作了):

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 的建议]

于 2012-11-08T12:09:17.397 回答