This is my first C app, so please excuse some ignorance. I'm working on porting a Python-based libpcap
application over to C for speed reasons. I'm having issues understanding how to extract information from the packet. It comes to my handler function as a u_char
, and you can munge it from there. In python, I would just slice the "string" and extract my information that way, but I don't know how to proceed.
For example, how to I extract the destination and source MAC addresses from an ethernet frame (bytes 0-6 and 7-11 respectively). Right now I have:
void handle_sniffed(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
u_char *mac_dst = malloc(6*sizeof(u_char));
u_char *mac_src = malloc(6*sizeof(u_char));
memcpy(mac_dst, packet, 6);
memcpy(mac_src, packet+6, 6);
}
Am I doing this correctly, and now how do I display the MAC address, for example using printf
?