写程序实现wireshark的抓包功能

选修了一门信息安全专业的课,做了个实验,是实现网络抓包的功能:

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/ip_icmp.h>
#include<netinet/tcp.h>
#include<netinet/udp.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/types.h>

#define BUFFSIZE 1024

int main(int argc,char **argv ){

  if(argc!=2){
  	printf("Usage:Proto_Name\n");
  	return 0;
  }
	int rawsock;
	unsigned char buff[BUFFSIZE];
	int n;
	int count = 0;
  char *ipr_name=argv[1];
  char *ipr_tcp="TCP";
  char *ipr_udp="UDP";
  char *ipr_icmp="ICMP";
  if(strcmp(ipr_name,ipr_tcp)==0)
	   rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
	else if(strcmp(ipr_name,ipr_udp)==0)
	   rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
	else if(strcmp(ipr_name,ipr_icmp)==0)
	   rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
	if(rawsock < 0){
		printf("raw socket error!\n");
		exit(1);
	}
	while(1)
{	
	n = recvfrom(rawsock,buff,BUFFSIZE,0,NULL,NULL);
	if(n<0){
		printf("receive error!\n");
		exit(1);
	}
		
	count++;
	struct ip *ip = (struct ip*)buff;
	printf("%4d	%15s",count,inet_ntoa(ip->ip_src));
	printf("%15s	%5d	%5d\n",inet_ntoa(ip->ip_dst),ip->ip_p,ntohs(ip->ip_len));	

	int i=0,j=0;
	for(i=0;i<n;i++)
    {
		if(i!=0 && i%16==0)
                {
			printf("	");
			for(j=i-16;j<i;j++)
			{
				if(buff[j]>=32&&buff[j]<=128)
				printf("%c",buff[j]);
				else printf(".");
			}
		printf("\n");
		}
	if(i%16 == 0) printf("%04x	",i);			
	printf("%02x",buff[i]);
	
	if(i==n-1)
    {
		for(j=0;j<15-i%16;j++) printf("  ");
		printf("	");
		for(j=i-i%16;j<=i;j++)
       {
			if(buff[j]>=32&&buff[j]<127)
                                printf("%c",buff[j]);
                                else printf(".");

		   }
	  }
   }
      
      printf("\n"); 
      printf("internet protocol\n");
      printf("version:%u\n",ip->ip_v);
      printf("Header Length:%u bytes\n",(ip->ip_hl)*4);
      printf("totle length:%d\n",ntohs(ip->ip_len));
      printf("Identification:%u\n",ip->ip_id);
      if((IP_RF&0x8000)!=0)
        printf("reserved bits:set\n");
      else
      	printf("reserved bits:not set\n");
      if((IP_DF&0x4000)!=0)
      	printf("dont fragment: not set\n");
      else
      	printf("dont fragment: set\n");
      if((IP_MF&0x2000)!=0)
      	printf("more fragment: set\n");
      else
      	printf("more fragment: not set\n");
      printf("Time to live:%u\n",ip->ip_ttl);
      if(ip->ip_p==6)
      	printf("protocol TCP(6)\n");
      else if(ip->ip_p==1)
      	printf("protocol ICMP(1)\n");
      else if(ip->ip_p==17)
      	printf("protocol UDP(17)\n");
      printf("source ip:%s\n",inet_ntoa(ip->ip_src));
      printf("destination ip:%s\n",inet_ntoa(ip->ip_dst));
      	
      //TCP
      if(ip->ip_p==6)
      {
      printf("transmission control protocol\n");
      struct tcphdr *tcp=(struct tcphdr *)(buff+(ip->ip_hl)*4);  
      printf("source port:%u\n",ntohs(tcp->source));
      printf("destation port:%u\n",ntohs(tcp->dest));
      printf("sequence number:%u\n",ntohl(tcp->seq));
      printf("acknowledgement number:%u\n",ntohl(tcp->ack_seq));
      printf("head length:%d\n",ntohs((tcp->doff)*4));
      if(tcp->urg==1)
        printf("urgent:set\n");
      else
      	printf("urgent:not set\n");
      if(tcp->ack==1)
      	printf("acknowledgment:set\n");
      else
      	printf("acknowledgment:not set\n");
      if(tcp->psh==1)
      	printf("push:set\n");
      else
      	printf("push:not set\n");
      if(tcp->rst==1)
      	printf("reset:set\n");
      else
      	printf("reset:not set\n");
      if(tcp->syn==1)
      	printf("syn:set\n");
      else
      	printf("syn:not set\n");
      	
      if(tcp->fin==1)
      	printf("fin:set\n");
      else
      	printf("fin:not set\n");
      printf("window size:%u\n",ntohs(tcp->window));
    }
    
    //UDP
   if(ip->ip_p==17)
   	{
   		struct udphdr *udp=(struct udphdr*)(buff+(ip->ip_hl)*4);
   		printf("user datagram protocol\n");
   		printf("source port:%u\n",udp->source);
   		printf("destination port:%u\n",udp->dest);
   		printf("length:%u\n",ntohs(udp->len));
   	}
    //ICMP
   if(ip->ip_p==1)
   	{ 
   		struct icmphdr *icmp = (struct icmphdr *)(buff+(ip->ip_hl)*4);
   		printf("Internet Control Message Protocol\n");
   		printf("type:%u",icmp->type);
   		if(icmp->type==0)
   			printf("(Echo Reply)\n");
   		else if(icmp->type==8)
   			printf("(Echo)\n");
   		else if(icmp->type==5)
   			printf("(Redirect)\n");
   		else if(icmp->type==3)
   			printf("(Dest Unreach)\n");
   		else if(icmp->type==4)
   			printf("(Source quench)\n");
   		else if(icmp->type==13)
   			printf("(Time Stamp)\n");
   		else if(icmp->type==14)
   			printf("(Time Stamp Reply)\n");
   		printf("Code:%u\n",icmp->code);
   		if(icmp->type==0||icmp->type==8)
   			{
   				printf("idetifier:0x%x\n",ntohs(icmp->un.echo.id));
   				printf("Sequence:%u\n",ntohs(icmp->un.echo.sequence));
   			}
   		if(icmp->type==3||icmp->type==4)
   			{
   				printf("Unused:%u\n",ntohs(icmp->un.frag.__unused));
   				printf("Mtu:%u\n",ntohs(icmp->un.frag.mtu));
   			}	
   		if(icmp->type==5)
   			printf("Gateway:%u\n",ntohs(icmp->un.gateway));
   	}     
	printf("\n\n");
}

}	
原文地址:https://www.cnblogs.com/justcxtoworld/p/2996548.html