I am attempting to write a custom packet sniffer. I am following the following tutorial...
http://www.binarytides.com/packet-sniffer-code-in-c-using-linux-sockets-bsd-part-2/
Doing so, I am unable to pick up an ARP requests packets. I do successfully pick up all other packets including ICMP, IP, etc...
Here is an overview of the code. Again, I am reading all other packets (every byte of every other packet) but I am not reading any ARP.
int main()
{
int saddr_size , data_size;
struct sockaddr saddr;
unsigned char *buffer = (unsigned char *) malloc(65536); //Its Big!
if(logfile==NULL)
{
printf("Unable to create log.txt file.");
}
printf("Starting...\n");
int sock_raw = socket( AF_PACKET , SOCK_RAW , htons(ETH_P_ALL)) ;
setsockopt(sock_raw , SOL_SOCKET , SO_BINDTODEVICE , "eth0" , strlen("eth0")+ 1 );
if(sock_raw < 0)
{
perror("Socket Error");
return 1;
}
while(1)
{
saddr_size = sizeof saddr;
//Receive a packet
data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &saddr , (socklen_t*)&saddr_size);
if(data_size <0 )
{
printf("Recvfrom error , failed to get packets\n");
return 1;
}
//Now process the packet
ProcessPacket(buffer , data_size);
}
close(sock_raw);
printf("Finished");
return 0;
}
As ARP doesn't use IP packets, you can't use
recvfrom
, you have to userecv
.See e.g. this example.