拆分数据包(碎片)后 - 只有TCP数据包带有错误的TCP校验和

时间:2014-03-20 12:36:28

标签: c# .net packet

我正在使用PcapDotNet DLLS来拆分数据包,TCP packets似乎bad TCP checksumUDP packets收到了确认

这是我收到Packet并返回IEnumerable<Packet>的代码:

    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();
            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
                Packet newPacket = null;

                if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
                {
                    TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
                    tcpLayer.Checksum = null;
                    newPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, newPayload);
                }
                else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
                {
                    UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
                    udpLayer.Checksum = null;
                    newPacket = PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer, newPayload);
                }

                yield return newPacket;
                //yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, newPayload); //return
                offset += (ushort)fragmentLength; //next offset
            }
        }
    }

任何想法为什么?

0 个答案:

没有答案