如何在.NET 中解析 UDP 数据包?
我使用 PCap.Net 捕获数据包,在本例中为 UDP 数据包,我可以通过 (PcapDotNet.packets.Ethernet.IpV4.Udp) 从 PCap.net 对象访问这些数据包。
我如何获取结果(Udp 数据包)并解析它?特别是解绑 UDP 数据包中发生的 DNS 请求和响应。
有图书馆可以提供帮助吗?
EDIT:更具体地说,我想要做的是从 DNS 响应中提取 IP 地址,并根据使用 Wireshark 的检查,它将通过以下方式:
(a) 输入:作为 DNS 响应的 UDP 数据包的有效负载
(b) 处理:解析出UDP数据包的DNS响应部分。找到“答案”部分,在其中找到类型为 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);
}
还不够吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)