looking in the libtins
tutorial (http://libtins.github.io/tutorial/sniffing/) about sniffing, I found this function implementation:
bool doo(PDU &some_pdu) {
// Search for it. If there is no IP PDU in the packet,
// the loop goes on
const IP &ip = some_pdu.rfind_pdu<IP>(); // non-const works as well
std::cout << "Destination address: " << ip->dst_addr() << std::endl;
// Just one packet please
return false;
}
void test() {
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter("ip src 192.168.0.100");
Sniffer sniffer("eth0", config);
sniffer.sniff_loop(doo);
}
Note that the doo
function in this example only prints the destination address - but you can adjust to your needs.
Please also note the use of sniff_loop
with doo
as a callback function.