如何刷新原始AF

编程入门 行业动态 更新时间:2024-10-23 05:35:17
本文介绍了如何刷新原始AF_PACKET套接字以获取正确的已过滤数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &f, sizeof (f))

使用这个简单的BPF/LPF附加代码,当我尝试在套接字上接收数据包时,将得到一些与过滤器不匹配的错误数据包.似乎那些数据包在我调用setsockopt()之前就已经进入套接字了.

With this simple BPF/LPF attach code, when I try to receive packet on the socket, will get some wrong packets that doesn't match with the filter. Seems those packets got into the socket before I call setsockopt().

好像应该先创建AF_PACKET SOCK_RAW套接字,然后附加过滤器,然后冲洗套接字以清除那些错误的数据包.

Seems like should first create the AF_PACKET SOCK_RAW socket, then attach the filter, then flush the socket to get rid of those wrong packets.

问题是,如何清除那些数据包?

So the question is, how to flush those packet?

推荐答案

"bug"您所描述的是真实的,而且我在职业生涯中的多家公司中都看到过它.存在类似于口头传统"的东西.解决此错误是由一位网络工程师传给另一位工程师的.以下是常见的修补程序:

The "bug" you're describing is real and I've seen it at multiple companies in my career. There is something like an "oral tradition" around this bug that is passed from one network engineer to another. Here are the common fixes:

  • 只需在套接字上调用 recv ,直到它为空
  • 通过在用户模式下以及使用bpf过滤数据包进行双重过滤
  • 像libpcap一样使用零bpf技术,在这种情况下,您首先应用一个空的bpf,然后清空套接字,然后应用真实的bpf.
  • 我已经在我的博客上广泛讨论了这个问题尝试将围绕此错误的口头惯例整理成具体的建议和最佳实践.

    I've written about this problem extensively on my blog to try and codify the oral tradition around this bug into a concrete recommendation and best-practice.

    更多推荐

    如何刷新原始AF

    本文发布于:2023-11-26 21:02:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1635182.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:原始   AF

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!