Skip to content

Instantly share code, notes, and snippets.

@LightBells
Created October 1, 2019 08:15
Show Gist options
  • Save LightBells/7cf9031f2fb386ab0cdecb9b22b4fb50 to your computer and use it in GitHub Desktop.
Save LightBells/7cf9031f2fb386ab0cdecb9b22b4fb50 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <net/if.h>
#include <net/ethernet.h>
void *mac_ntoa(u_char *d){
static char str[18];
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x\n", d[0], d[1],d[2],d[3],d[4],d[5]);
return str;
}
void analyzePacket(u_char *buf){
analyzePacketHeader(buf);
}
void analyzePacketHeader(u_char *buf){
struct ether_header *eth;
static int packetCount=0;
eth = (struct ether_header *)buf;
if (ntohs(eth->ether_type)==0x0800){
printf("IPv4 Packet:\n");
printf("\tDst MAC addr : %17s\n", mac_ntoa(eth->ether_dhost));
printf("\tSrc MAC addr : %17s\n", mac_ntoa(eth->ether_shost));
printf("\tEthernet Type : 0x%04x\n", ntohs(eth->ether_type));
}
}
int main(){
int socketNumber;
u_char buf[1<<16-1];
socketNumber = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
while(1){
read(socketNumber, buf, sizeof(buf));
analyzePacket(buf);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment