Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <net/if.h>
  3. #include <net/ethernet.h>
  4.  
  5. void *mac_ntoa(u_char *d){
  6. static char str[18];
  7. sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x\n", d[0], d[1],d[2],d[3],d[4],d[5]);
  8. return str;
  9. }
  10.  
  11. void analyzePacket(u_char *buf){
  12. analyzePacketHeader(buf);
  13. }
  14.  
  15. void analyzePacketHeader(u_char *buf){
  16. struct ether_header *eth;
  17. static int packetCount=0;
  18. eth = (struct ether_header *)buf;
  19. if (ntohs(eth->ether_type)==0x0800){
  20. printf("IPv4 Packet:\n");
  21. printf("\tDst MAC addr : %17s\n", mac_ntoa(eth->ether_dhost));
  22. printf("\tSrc MAC addr : %17s\n", mac_ntoa(eth->ether_shost));
  23. printf("\tEthernet Type : 0x%04x\n", ntohs(eth->ether_type));
  24. }
  25. }
  26.  
  27. int main(){
  28. int socketNumber;
  29. u_char buf[1<<16-1];
  30. socketNumber = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  31. while(1){
  32. read(socketNumber, buf, sizeof(buf));
  33. analyzePacket(buf);
  34. printf("\n");
  35. }
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement