c# - 识别数据包来自 pcap.net 的接口(interface)?

标签 c# windows network-programming pcap.net

我正在使用带有两个网络接口(interface) Controller 的计算机构建一座桥。 我正在使用多线程安排来使用 pcap.net 从两个接口(interface)接收数据包。问题是我不知道如何识别从哪个接口(interface)接收数据包。数据包是否携带任何信息可以判断它们是从哪个接口(interface)嗅探到的?

如果没有,可能的解决方法是将参数传递给回调函数。这可能吗?

最佳答案

ReceivePackets 的默认事件处理程序不提供接口(interface)信息。真的!另一方面,它所期望的只是一个带有单个 Packet 参数的 delegate void

让我们提供一个:

communicator.ReceivePackets(0, packet => PacketHandler(selectedDevice, packet));

我们将捕获的数据包传递给我们的委托(delegate),然后调用在我们的设备中传递的数据包处理程序。

我改编了 Pcap.Net site 上找到的示例.

namespace ConsoleApplication7
{
    using System;
    using System.Collections.Generic;

    using PcapDotNet.Core;
    using PcapDotNet.Packets;

    internal class Program
    {
        private static void Main(string[] args)
        {
            IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

            if (allDevices.Count == 0)
            {
                Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
                return;
            }

            for (int i = 0; i != allDevices.Count; ++i)
            {
                var device = allDevices[i];

                Console.Write((i + 1) + ". " + device.Name);

                if (device.Description != null)
                {
                    Console.WriteLine(" (" + device.Description + ")");
                }
                else
                {
                    Console.WriteLine(" (No description available)");
                }
            }

            int deviceIndex;
            do
            {
                Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
                var deviceIndexString = Console.ReadLine();

                if (!int.TryParse(deviceIndexString, out deviceIndex) || deviceIndex < 1 || deviceIndex > allDevices.Count)
                {
                    deviceIndex = 0;
                }
            }
            while (deviceIndex == 0);

            var selectedDevice = allDevices[deviceIndex - 1];

            using (var communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                Console.WriteLine("Listening on " + selectedDevice.Description + "...");

                communicator.ReceivePackets(0, packet => PacketHandler(selectedDevice, packet));
            }
        }

        private static void PacketHandler(PacketDevice device, Packet packet)
        {
            Console.WriteLine("[{0}] {1} {2}", device.Name, packet.Timestamp.ToString("hh:mm:ss.fff"), packet.Length);
        }
    }
}

1. rpcap://\Device\NPF_{0CDD10C5-9C40-47BD-811D-7CD24547CD28} (Network adapter 'Microsoft' on local host)
2. rpcap://\Device\NPF_{3C97403E-012B-4912-96B1-F8E246F93BA0} (Network adapter 'Sun' on local host)
3. rpcap://\Device\NPF_{06217B0B-1804-4ADD-9BEE-4A7EBC63B009} (Network adapter 'Microsoft' on local host)
4. rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09} (Network adapter 'Intel(R) 82579LM Gigabit Network Connection' on local host)
5. rpcap://\Device\NPF_{40199909-A7A1-4549-8D06-9DCE66F24A7E} (Network adapter 'Microsoft' on local host)

Enter the interface number (1-5): 4

Listening on Network adapter 'Intel(R) 82579LM Gigabit Network Connection' on local host...
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.552 55
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.552 60
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.848 60
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.848 54
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.958 55
[rpcap://\Device\NPF_{C189488C-4DD5-4410-981B-A5929234AC09}] 01:59:49.958 55

关于c# - 识别数据包来自 pcap.net 的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32456439/

相关文章:

c# - 使用 lambda 作为方法的 Delegate.CreateDelegate 产生 "method argument length mismatch"异常

c# - 使用 openssl_seal 加密的 php 数据。如何在 C# 中解码?

c# - 如何使套接字客户端准备好发送和接收而不需要服务器的操作?

c++ - UDP_TABLE_CLASS 值在哪里定义?

c# - 我可以使用脚本管理器为 WCF 服务库生成代理对象吗?

python - SSH 无法在脚本内工作并给出错误 'ssh' 未被识别为内部或外部命令、可操作程序或批处理文件

c++ - 在 Windows 10 凭据提供程序中获取当前锁定 session 的用户名(Microsoft 帐户与本地)的问题

c++ - 如何获取 DLL 中的函数列表(托管和非托管)?

java - 在 Java 上限制上传速度?

java - 如何在 Java 上同时支持 IPv4 和 IPv6