C# 如何识别pcap.net/SharpPcap中的ICMP/ARP数据包?

C# 如何识别pcap.net/SharpPcap中的ICMP/ARP数据包?,c#,network-programming,protocols,sniffer,pcap.net,C#,Network Programming,Protocols,Sniffer,Pcap.net,所以这真的很奇怪。 我尝试了多种表达式,但还没有找到正确的布尔表达式来识别数据包是ICMP还是ARP数据包。 我试过了 packet.ipv4.icmp != null 这导致即使数据包不是ICMP,程序也会进入数据块 我也试过了 packet.ipv4.Protocol == IpV4Protocol.InternetControlMessageProtocol 但是,即使数据包是ICMP,程序也不会进入数据块 有什么想法吗?假设我们讨论的是ARP over Ethernet数据包与ICM

所以这真的很奇怪。 我尝试了多种表达式,但还没有找到正确的布尔表达式来识别数据包是ICMP还是ARP数据包。 我试过了

packet.ipv4.icmp != null
这导致即使数据包不是ICMP,程序也会进入数据块 我也试过了

packet.ipv4.Protocol == IpV4Protocol.InternetControlMessageProtocol
但是,即使数据包是ICMP,程序也不会进入数据块
有什么想法吗?

假设我们讨论的是ARP over Ethernet数据包与ICMP over IPv4 over Ethernet数据包:

1) 检查数据包是否为以太网

if (packet.DataLink.Kind == DataLinkKind.Ethernet) {
2) 检查以太网数据包是ARP还是IPv4:

if (packet.Ethernet.EtherType == EthernetType.IpV4) {

if (packet.Ethernet.EtherType == EthernetType.Arp) {
3) 如果这是IPv4,请检查它是否为ICMP:

if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol) {
在执行上述所有操作之前,您可能需要检查数据包是否有效

if (packet.IsValid) {

这将保证您在评估上述内容时不会得到空引用。

假设我们讨论的是ARP over Ethernet数据包与ICMP over IPv4 over Ethernet数据包:

1) 检查数据包是否为以太网

if (packet.DataLink.Kind == DataLinkKind.Ethernet) {
2) 检查以太网数据包是ARP还是IPv4:

if (packet.Ethernet.EtherType == EthernetType.IpV4) {

if (packet.Ethernet.EtherType == EthernetType.Arp) {
3) 如果这是IPv4,请检查它是否为ICMP:

if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol) {
在执行上述所有操作之前,您可能需要检查数据包是否有效

if (packet.IsValid) {
这应该保证在评估上述内容时不会得到空引用