c# - 如何在 .NET 中解析 UDP 数据包?

标签 c# .net dns udpclient pcap.net

如何在 .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/

相关文章:

ubuntu - Ubuntu 上名称/DNS 服务器的 GUI

azure - 如何使用 SRV(或任何其他)记录来重定向域?

c# - 检查 azure 消息是否具有属性

c# - 如何注册手动创建的装饰器实例

node.js - SSL 域与 ec2 DNS 名称不匹配

c# - 当 this == null 和 obj == null 时调用 IEquatable<T>.Equals(T obj) 的结果?

.net - MVVM模型间验证

c# - 从 Excel 文件中读取 — 特定工作表

c# - 为了使开发可移植到 Java,在 J# 和 C# 上进行整个开发是否有意义?

javascript - 如何使用 jquery ajax 从 url 将变量传递到 Controller