How to send/receive layer 2 frames on wlan
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 开发者_JAVA百科(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.
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).
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.
精彩评论