使用 libpcap 同时在两个设备上监听

发布于 2024-10-05 05:25:20 字数 230 浏览 0 评论 0原文

我正在尝试使用 libpcap 在两个设备上监听,但我仍然不知道如何做到这一点。我尝试将设备设置为“任意”,但它不起作用。我正在尝试编写 dhcp 中继代理,因此我需要监听 eth0 和 eth1。

我尝试创建两个 pcap_loop,每个都有不同的设备和处理程序,但只有第一个 pcap_loop 有效,第二个被忽略。

有什么办法可以做到这一点,或者我应该离开 libpcap 并尝试使用原始套接字来做到这一点?

I am trying to listen on two devices with libpcap but I still cant find out how to do the trick. I tried to set device to "any" but it isnt working. I am trying to write dhcp relay agent so i need to listen on eth0 and eth1.

I tried to create two pcap_loops, each with different device and handler, but only first pcap_loop works, second one is ignored.

Is there any way how to do this or should I leave libpcap and try to do it with raw sockets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-10-12 05:25:20

您需要在单独的线程中运行 pcap_loop(),每个接口一个线程,我们这样做了,并且它有效。

libpcap 的某些部分不是线程安全的,至少是 pcap_setfilter(),因此请提供您自己的锁定。

如果您不想使用线程,则必须自己提供一个事件循环,在其中使用 select/poll 或类似方法监视每个设备的文件描述符。您可以使用 pcap_get_selectable_fd() 获取设备句柄的文件描述符。

You'll need to run your pcap_loop() in separate threads, one for each interface, we do that, and it works.

Some parts of libpcap, isn't thread safe though, atleast pcap_setfilter(), so provide your own locking around that.

If you do no want to use threads, you'll have to provide an event loop yourself, where you monitor the file descriptors of each device with select/poll or similar. You can get the file descriptor for a device handle with pcap_get_selectable_fd().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文