⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catch_pppoe.c

📁 通过PF_PACKET接收ether层包
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>
#include <net/if.h>
#include <errno.h>
#include <asm/types.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <setjmp.h>
#include <netdb.h>
#include <sys/param.h>
#include <net/if_arp.h>

char maddr[6];

static volatile int gnContinue =1;
static void in_catch_sig(int nSig)
{
    gnContinue = 0;
}
/*------------get the mac address with interface name-----------*/
unsigned int get_mac(char *if_name)//only can get ip when if is connected
{
    	if(!strcmp(if_name,"null")) return 0;
    	struct protoent *protocol;
    	int interface;
      struct ifreq buf[16];
      struct ifconf ifc;
      /*char *maddr;*/
      int fd;
      if((protocol=getprotobyname("icmp"))==NULL)
	  	{
	  	  perror("getprotobyname");
	  	  exit(1);	
	  	}
      if((fd=socket(AF_INET,SOCK_RAW,protocol->p_proto))<0) //sock will run under root
	    {
	  	  perror("sock error");
	  	  exit(1);
	    }
      ifc.ifc_len=sizeof(buf);
      ifc.ifc_buf=(caddr_t)buf;
      
      if(ioctl(fd,SIOCGIFCONF,(caddr_t)&ifc)==0)//return information about all interfaces in system
      	{
      	  interface=ifc.ifc_len/sizeof(struct ifreq); //number of interface
      	  /*printf("interface num is %d.\n",interface);*/
      	  while(interface-- >0)
      	  {
      	  	if(!strcmp(if_name,buf[interface].ifr_name))
      	  		{
      	  			if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface])))
      	  			{
      	  			/*int len=strlen(buf[interface].ifr_hwaddr.sa_data);
                maddr=(char *)malloc(sizeof(char)*(len+1));
                if(maddr!=NULL)
                {
                  strcpy(maddr,buf[interface].ifr_hwaddr.sa_data);
                }*/
      	  			/*maddr=buf[interface].ifr_hwaddr.sa_data;*/
      	  			maddr[0]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[0];
                maddr[1]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[1];
                maddr[2]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[2];
                maddr[3]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[3];
                maddr[4]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[4];
                maddr[5]=(unsigned char) buf[interface].ifr_hwaddr.sa_data[5];
      	  			printf("interface name:%s\n",buf[interface].ifr_name);
      	  			printf("interface mac:%s\n",ether_ntoa(buf[interface].ifr_hwaddr.sa_data));
      	  			close(fd);
      	  			return(1);
      	  			break;
      	  		}
      	  	}
      	  }//end of while
      	  close(fd);
      	  return 0;
      	}
      else
      	{
      	  perror("ipmac:ioctl");
      	  close(fd);
      	  exit(1);	
      	}
}    	

int main(int argc,char *argv[])
{
    int sockfd,nlen;
    char *if_name;
    struct sigaction struact;
    struct ethhdr *hdr;
    char szbuffer[4096];
    /*char *maddr;*/
    char c[6];//broadcast address
    c[0]=255;
    c[1]=255;
    c[2]=255;
    c[3]=255;
    c[4]=255;
    c[5]=255;
    
    /*printf("c:%s\n",ether_ntoa(c));*/
    
    
/*---------------input the assigned interface name-------------*/
    if(argc<2) 
    	{
    		printf("Haven't entered the name of the assigned interface!\n");
    		exit(1);
      }
    if_name=argv[1];//input the interface name
    if(!get_mac(if_name))
    	{
    	  printf("%s isn't exist!\n",if_name);	
    	  exit(1);
    	}
    /*else //maddr=get_mac(if_name);
     printf("maddr:%s\n",ether_ntoa(maddr));*/
      
/*--------------create a socket---------------------*/    
    sockfd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP));
    if(sockfd<0)
    	{
    	  perror("Create socket");
    	  return -1;	
    	}
    	
/*------------------receive the signal----------------*/    	
    memset(&struact, 0,sizeof(struact));
    struact.sa_handler = in_catch_sig;
    sigfillset(&struact.sa_mask);
    
    if(sigaction(SIGINT,&struact,NULL)<0)
    	{
    	  perror("sigaction() error");
    	  return -1;	
    	}

/*-----------------begin to receive---------------------*/
    printf("Press CTRL+C to quit\n");   
    while(gnContinue)
    {
      	/*printf("step0\n");*/
      	nlen=recv(sockfd,szbuffer,sizeof(szbuffer),0);
      	/*printf("step1\n");*/
      	if(nlen<0) break;
      	hdr=(struct ethhdr *)szbuffer;
      	/*printf("step2\n");*/
      	/*printf("h_dest:%s \n",ether_ntoa(hdr->h_dest));
      	printf("h_source:%s\n",ether_ntoa(hdr->h_source));*/
      	      	
      	if(!bcmp(maddr,hdr->h_dest,6))
      		{
      			printf("Receive packet from %s\n",ether_ntoa(hdr->h_source));
      			continue;
      		}
      	else if(!bcmp(c,hdr->h_dest,6))
      			{
      			  printf("Receive broadcast paceket from %s\n",ether_ntoa(hdr->h_source));	
      			  continue;
      			}
      	else 
      		{
      			/*printf("step3\n");*/
      			continue;
      	  }
    }
    close(sockfd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -