使用PcapDotNet DLL更改数据包IP地址后,除了数据包IP地址发生更改外,其他值也会增加

时间:2014-02-28 10:28:13

标签: c# packet pcap.net

我在我的应用程序中使用PcapDotNet DLL,在更改数据包IP Address时,似乎除IP Address之外的其他值已更改。

这是我改变IP Address

的方式
private Packet ChangePacketIp(Packet packet, IpV4Address oldIpAddress, IpV4Address newIpAddress)
{
    try
    {
        EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
        IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
        IpV4Datagram ipV4Datagram = packet.Ethernet.IpV4;
        ILayer layer = ipV4Datagram.ExtractLayer();
        DateTime packetTimestamp = packet.Timestamp;
        ILayer payload = packet.Ethernet.IpV4.Payload.ExtractLayer();

        if (packet.Ethernet.IpV4.Source == oldIpAddress)
        {
            ipV4Layer.Source = newIpAddress;
            ipV4Layer.HeaderChecksum = null;
        }
        else if (packet.Ethernet.IpV4.Destination == oldIpAddress)
        {
            ipV4Layer.CurrentDestination = newIpAddress;
            ipV4Layer.HeaderChecksum = null;
        }

        return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, payload);
    }
    catch (Exception)
    {
        return null;
    }
}

例如,原始数据包是:

http://i.stack.imgur.com/vEWuS.jpg

IP地址更改后的新数据包:

http://s8.postimg.org/4o78hzmyt/New_Packet.jpg

在将IP Address212.25.99.74更改为80.81.82.83之后,您可以看到另外2个字节的变化:从e6 16变为7a d6

这是更改IPv4数据包或其错误的方法吗? 这2个字节是什么?

1 个答案:

答案 0 :(得分:1)

您是否知道IP数据包的校验和必须重新计算以匹配新的IP地址? http://en.wikipedia.org/wiki/IPv4_header_checksum