写一个程序,可以分析跟本机有数据包往来的主机ip
千锋教育
#include
#include
#include
int main(int argc,char *argv[])
{
int sockfd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
if(sockfd<0)
return 0;
unsigned char buf[1500]="";
while(1){
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
unsigned short mac_type=0;
mac_type=ntohs(*((unsigned short *)(buf+12)));
if(mac_type==0x0800)
{
printf("-----------ip数据包------------\n");
//分析数据包的IP层数据
unsigned char *ip_head=buf+14;
char src_ip[16]="";//192.168.110.110
char dst_ip[16]="";//192.168.110.110
inet_ntop(AF_INET,(void *)ip_head+12,src_ip,16);
inet_ntop(AF_INET,(void *)ip_head+16,dst_ip,16);
printf("src_ip:%s--------》dst_ip:%s\n ",src_ip,dst_ip);
}
close(sockfd);
return 0;
}