Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement