使用pcapdotnet将数据包拆分为2个数据包

时间:2012-11-07 15:16:04

标签: c# pcap.net

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

1 个答案:

答案 0 :(得分: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的建议]