如何在 WLAN 上发送/接收第 2 层帧
我想要做什么:在用户空间中实现第 2 层协议。
所以我在 Linux 2.6.32 下使用 pcap 来嗅探数据包:
...
struct pcap_t *pcap_h = pcap_open_live("wlan0", BUFSIZ, 1, 0, errbuf);
...
while (1) {
int ret = pcap_loop(pcap_h, -1, newpkt_callback, NULL);
...
}
...
这对所有数据包都适用。但是,当我使用 pcap 发送没有 ether_head 和 IP 标头的数据包时:
const char pkt[] = "WHATEVER";
nsent = pcap_sendpacket(pcap_h, (const u_char *)pkt, len);
...
我只能在本地主机上嗅探数据包,而不能在运行相同程序的其他笔记本电脑上嗅探数据包。所以问题是“如何在无线局域网上广播没有 ether_head 的消息”?任何指示将不胜感激。
What I wanna do: Implement a layer 2 protocol in user-space.
So I'm using pcap under Linux 2.6.32 to sniff packets:
...
struct pcap_t *pcap_h = pcap_open_live("wlan0", BUFSIZ, 1, 0, errbuf);
...
while (1) {
int ret = pcap_loop(pcap_h, -1, newpkt_callback, NULL);
...
}
...
Which works just fine for all packets. But, when I use pcap to send packets with no ether_head and no IP header:
const char pkt[] = "WHATEVER";
nsent = pcap_sendpacket(pcap_h, (const u_char *)pkt, len);
...
I can only sniff the packet on the localhost, and not on other laptops that are running the same program. So the question is "how can I broadcast messages without ether_head on a wlan"? Any pointers would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用接入点(基础设施模式),则无法执行此操作,因为接入点在其他无线站之间中继帧,因此必须知道如何使用第 2 层协议。
我建议在第 3 层实现您的协议(您可能需要研究
PF_PACKET
套接字)。You can't do this if you are using an access point (infrastructure mode), as the access point relays the frames between other wireless stations and thus must know how to talk your layer 2 protocol.
I suggest implementing your protocol at layer 3 (and you may want to look into
PF_PACKET
sockets).您必须发送带有标头的完整帧,而不仅仅是一些随机数据。
看看这个手册 http://linux.die.net/man/3/pcap 在函数 pcap_inject() 处。在创建新框架时,这可能会有所帮助 http://www.tcpdump.org/pcap.html,或者只使用 libnet 库 http://libnet.sourceforge.net/libnet.html。
You have to send complete frame with it's headers, not just some random data.
Take a look at this manual http://linux.die.net/man/3/pcap at function pcap_inject(). In creating new frame this could help http://www.tcpdump.org/pcap.html, or just use libnet library http://libnet.sourceforge.net/libnet.html.