IP碎片碎片数量

时间:2014-03-19 13:35:21

标签: c# .net packet

我正在使用PcapDotNet DLLS播放Pcap文件并添加拆分数据包(碎片)选项:

    private IEnumerable<Packet> SplitPacket(Packet packet, int numberOfFragments)
    {
        IpV4Datagram ipV4Datagram = packet.Ethernet.IpV4;

        if (ipV4Datagram.Protocol == IpV4Protocol.Tcp || ipV4Datagram.Protocol == IpV4Protocol.Udp)
        {
            EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
            ILayer layer = packet.Ethernet.IpV4.ExtractLayer();
            TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
            tcpLayer.Checksum = null;
            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
            if (partialLength == 0)
                partialLength = 8;

            partialLength = (partialLength / 8) * 8; //make sure it's divisible with 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
                yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, newPayload); //return
                offset += (ushort)fragmentLength; //next offset
            }
        }
    }

我以这种方式调用函数:SplitPacket(Packet, 2);

当带有200个数据包的Pcap文件并调用带有numberOfFragments = 2的函数时,收到~330个数据包 使用numberOfFragments = 3调用函数时:收到~600个数据包 使用numberOfFragments = 4调用函数时:收到超过100,000个数据包 这个值意味着什么以及为什么会出现这种奇怪的行为?

0 个答案:

没有答案