>我使用此项目 DLL 并在更改 IP 地址后
: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;
}
}
在构建数据包之前,我需要将 UDP/TCP 校验和设置为 null,请尝试;
packet.Ethernet.IpV4.Tcp.Checksum = null;
但是 ushort 是一种不可为空的值类型,知道如何做到这一点吗?
按照 http://pcapdotnet.codeplex.com/discussions/349978 试试这个:
//IP
IpV4Layer ipLayer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
ipLayer.HeaderChecksum = null;
//TCP
TransportLayer transportLayer = (TransportLayer)packet.Ethernet.IpV4.Transport.ExtractLayer();
transportLayer.Checksum = null;
//UDP
UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
udpLayer.Checksum = null;