Skip to main content

Posts

Showing posts with the label programming in linux to print source and destination ip address for all

print the source and destination address using kernel-2.6.32.9

main() { 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)); while(1){ 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); } } }