使用Pcap.Net修复错误校验和的数据包

时间:2015-01-31 13:32:29

标签: c# packet pcap pcap.net

我有几个PCAP个文件,其中包含错误的Checksum,为了修复这些文件,我正在使用此方法:

private Packet FixBadChecksum(Packet packet)
        {
            try
            {
                EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
                IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
                DateTime packetTimestamp = packet.Timestamp;
                TransportLayer transportlayer = (TransportLayer)packet.Ethernet.IpV4.Transport.ExtractLayer();
                ILayer datagramLayer = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer();
                ipV4Layer.HeaderChecksum = null;

                if (transportlayer == null)
                    return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, datagramLayer);
                else
                {
                    transportlayer.Checksum = null;
                    ILayer payload = packet.Ethernet.IpV4.Transport.Payload.ExtractLayer();
                    return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, transportlayer, payload);
                }
            }
        catch (Exception)
        {
            return packet;
        }
    }

现在例如,如果我的数据包在此数据包之后ICMP(校验和错误)完成我的功能,它仍然是错误的Checksum,所以我的问题是:我需要检查每个数据包协议或那里这是一种通用的方法吗? (目前我的函数与TCPUDP一起工作正常但其他协议呢?)

1 个答案:

答案 0 :(得分:0)

引用a similar discussion in Pcap.Net site

  

您看到的错误校验和是IPv4标头或校验和   ICMP内部的TCP over IPv4标头。这些校验和很可能   因为ICMP上的数据只是数据包的部分副本   发送给ICMP的是。