How to sniff bluetooth packets in C?

249 Views Asked by At

I would like to capture all bluetooth packets received by my bluetooth adapter on x86 Ubuntu with a simple C program. Sniffing TCP/IP packets is as simple as this:

int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
char buf[65536];
while (1) {
    ssize_t bytes_read = read(sock, buf, 65536);
    write(1, buf, bytes_read);
}

man 2 socket lists AF_BLUETOOTH as domain, so I thought it might be as simple as socket(AF_BLUETOOTH, SOCK_RAW, 0). However, this does not work, just like socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) (whereas BTPROTO_HCI is from libbluetooth-dev). No errors occur (socket() and read() do not return -1), yet no packets are intercepted, although tcpdump does intercept some.

I found the source code of libpcap (tcpdump) for handling bluetooth, but don't really get it: https://github.com/the-tcpdump-group/libpcap/blob/master/pcap-bt-linux.c#L150-L402

0

There are 0 best solutions below