在我的应用程序中,我使用Pcap.net
DLL 并以这种方式更改数据包 ip:
private Packet ChangePacketIp(Packet packet, string oldIpAddress, string newIpAddress)
{
try
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
DateTime packetTimestamp = packet.Timestamp;
if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress)
{
ipV4Layer.Source = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
else if (packet.Ethernet.IpV4.Destination.ToString() == oldIpAddress)
{
ipV4Layer.CurrentDestination = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
{
TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
tcpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, payload);
}
else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
{
UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
udpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Udp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer, payload);
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
如果我拥有的数据包与 TCP 数据包VLAN packet
packet.Ethernet.IpV4.Protocol
不同TCP
,在这种情况下我返回 null,那么如何在不事先知道我的数据包协议的情况下实现我的目的?