我用sharppcap编写了一个简单的程序,将输入的udp数据包复制到另一个端口。假设,我在端口25000上有输入数据包,我将使用sharppcap接收它们,创建与prevoius相同的新以太网数据包,只将目标端口更改为27000,然后发送数据包。
但过了一段时间,有时是1小时,有时是2小时,有时是12小时,我会得到BSOD。我不知道到底是什么导致了BSOD(在minidump中,我只知道它的内核和硬件抽象层)在地址树中,第一个if“ipnat.sys系统".
device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
// Open the device for capturing
device.Open();
device.Filter = $"udp dst port {_portIncomming} and ip src not {_ipSource}";
// Start capture 'INFINTE' number of packets
device.Capture();
// Close the pcap device
// (Note: this line will never be called since
// we're capturing infinite number of packets
device.Close();
并接收:
private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
var device = e.Device;
var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
if (packet is EthernetPacket)
{
var eth = packet.Extract<EthernetPacket>();
var ip = eth.Extract<IPPacket>();
if (ip != null)
{
var udp = packet.Extract<UdpPacket>();
device.SendPacket(CreateDuplicate(eth.SourceHardwareAddress, eth.DestinationHardwareAddress, udp.PayloadData, ip.SourceAddress, ip.DestinationAddress));
}
}
}
和CreateDuplicate:
private static EthernetPacket CreateDuplicate(PhysicalAddress source, PhysicalAddress dest, byte[] data, IPAddress ipSource, IPAddress ipDest)
{
ushort udpSourcePort = _portIncomming;
ushort udpDestinationPort = _newDestPort;
var udpPacket = new UdpPacket(udpSourcePort, udpDestinationPort);
var ipSourceAddress = ipSource;
var ipDestinationAddress = ipDest;
var ipPacket = new IPv4Packet(ipSourceAddress, ipDestinationAddress);
var ethernetSourceHwAddress = source;
var ethernetDestinationHwAddress = dest;
// NOTE: using EthernetPacketType.None to illustrate that the Ethernet
// protocol type is updated based on the packet payload that is
// assigned to that particular Ethernet packet
var ethernetPacket = new EthernetPacket(ethernetSourceHwAddress,
ethernetDestinationHwAddress,
EthernetType.None);
// Now stitch all of the packets together
udpPacket.PayloadData = data;
udpPacket.ParentPacket = ipPacket;
ipPacket.PayloadPacket = udpPacket;
ethernetPacket.PayloadPacket = ipPacket;
ipPacket.UpdateIPChecksum();
udpPacket.Checksum = udpPacket.CalculateUdpChecksum();
ethernetPacket.UpdateCalculatedValues();
//byte[] packetBytes = ethernetPacket.Bytes;
return ethernetPacket;
}
你有什么想法或一些提示或已经编写的程序来解决我的问题。
运行操作系统:Windows Server 2012 R2