如何在 .NET 中解析 UDP 数据包?
我正在使用 PCap.Net 捕获数据包,在本例中为 UDP 数据包,我可以通过 (PcapDotNet.packets.Ethernet.IpV4.Udp) 从 PCap.net 对象访问它。
我如何获取结果、Udp 数据包并对其进行解析?特别是对发生在 UDP 数据包中的 DNS 请求和响应进行拆分。
是否有图书馆可以提供帮助?
编辑:更具体地说,我想做的是从 DNS 响应中提取 IP 地址,并基于使用 Wireshark 的检查:
(a) 输入:作为 DNS 响应的 UDP 数据包的负载
(b) 处理:解析出UDP数据包的DNS响应部分。找到 Answers 部分,在此找到类型为 A(主机地址)[不是 CNAME 记录] 的答复记录,然后使用此答复记录获取 IP 地址。
(c) 返回:来自 DNS 响应的 IP 地址。
最佳答案
来自 PCAP.Net:
Pcap.Net.DevelopersPack.0.7.0.46671.x64\src\InterpretingThePackets\Program.cs
// Compile the filter
using (BerkeleyPacketFilter filter = communicator.CreateFilter("ip and udp"))
{
// Set the filter
communicator.SetFilter(filter);
}
Console.WriteLine("Listening on " + selectedDevice.Description + "...");
// start the capture
communicator.ReceivePackets(0, PacketHandler);
}
// Callback function invoked by libpcap for every incoming packet
private static void PacketHandler(Packet packet)
{
// print timestamp and length of the packet
Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
IpV4Datagram ip = packet.Ethernet.IpV4;
UdpDatagram udp = ip.Udp;
// print ip addresses and udp ports
Console.WriteLine(ip.Source + ":" + udp.SourcePort+ " -> " + ip.Destination + ":" + udp.DestinationPort);
}
还不够吗?
关于c# - 如何在 .NET 中解析 UDP 数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518560/