June 17, 2011

print the source and destination address using kernel-

 int pack_socket,n;  
 int len,datasize;  
 char src_addr[20],dst_addr[20],*data;  
 struct sockaddr_ll sa;  
 socklen_t sl;  
 struct iphdr *ip_hdr;  
 char buf[2000];  
 struct in_addr addr;  
 pack_socket = socket(PF_PACKET,SOCK_DGRAM,htons(ETH_P_ALL));  
 sl = sizeof(struct sockaddr_ll);  
 n = recvfrom(pack_socket,buf,2000,0,(struct sockaddr *)&sa,&sl);  
 //check whether the packet is ip or not.  
 if(ntohs(sa.sll_protocol) == 0x0800){  
 printf(" IP packet received \n");  
 ip_hdr = (struct iphdr *) buf;  
 addr.s_addr = ntohl(ip_hdr->saddr);  
 printf("\n Source Addr = %s", inet_ntoa(addr));  
 addr.s_addr = ntohl(ip_hdr->daddr);