-
2007-08-31
向具体的mac地址转发包
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
printf("input mac of wlanmultihop\n");
unsigned char mac[6];
unsigned int temp[3];
scanf("%x:%x:%x:%x:%x:%x",mac,mac+1,mac+2,mac+3,mac+4,mac+5);
int sock,sock1;
int n;
char buffer[2048];
unsigned char *iphead,*ethhead;
struct ifreq wlan;
strncpy(wlan.ifr_name,"eth0",IFNAMSIZ);
struct sockaddr_ll dest;
memset(&dest,0,sizeof(dest));
dest.sll_family=AF_PACKET;
dest.sll_protocol=htons(ETH_P_ALL);
dest.sll_ifindex =wlan.ifr_ifindex;
if((sock1=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))<0)
{
perror("socket1");
return -1;
}
if((sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))<0)
{
perror("socket");
return -1;
}
/*
open two 802.11 datalink socket to recieve and send packet
*/
wlan.ifr_flags|=IFF_PROMISC;
if (ioctl(sock,SIOCGIFFLAGS,&wlan) == -1)
{
perror("ioctl");
close(sock);
return -1;
}
if (ioctl(sock1,SIOCGIFFLAGS,&wlan) == -1)
{
perror("ioctl");
close(sock);
return -1;
}/*
change wlan netcard to in promiscuos mode
*/
while(1)
{
n=recvfrom(sock,buffer,2048,0,NULL,NULL);
printf("-----------------------------------------------------------\n");
printf("%d bytes read\n",n);
ethhead=buffer;
printf("the destination should be %x:%x:%x:%x:%x:%x\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
printf("the source mac is %x:%x:%x:%x:%x:%x\n",ethhead[0],ethhead[1],ethhead[2],ethhead[3],ethhead[4],ethhead[5]);
printf("the destination mac is %x:%x:%x:%x:%x:%x\n",ethhead[6],ethhead[7],ethhead[8],ethhead[9],ethhead[10],ethhead[11]);
iphead=buffer+14;
if(*iphead==0x45) {
printf("Source host %d,%d,%d,%d\n",iphead[12],iphead[13],iphead[14],iphead[15]);
printf("Dest host %d,%d,%d,%d\n",iphead[16],iphead[17],iphead[18],iphead[19]);
printf("Source,Dest ports %d,%d\n",(iphead[20]<<8)+iphead[21],(iphead[22]<<8)+iphead[23]);
printf("Layer-4 protocol %d\n",iphead[9]);
}
printf("-----------------------------------------------------------\n");
dest.sll_halen=sizeof(mac);
memcpy(dest.sll_addr,mac,6);
int flag=0;
if(
ethhead[6]==mac[0]&ðhead[7]==mac[1]&ðhead[8]==mac[2]&ðhead[9]==mac[3]&ðhead[10]==mac[4]&ðhead[11]==mac[5])
{
flag=1;
}
if(flag==1)
{
sendto(sock1,&buffer,sizeof(buffer),0,(struct sockaddr *)(&dest),sizeof(dest));
printf("send to %x:%x:%x:%x:%x:%x\n",dest.sll_addr[0],dest.sll_addr[1],dest.sll_addr[2],dest.sll_addr[3],dest.sll_addr[4],dest.sll_addr[5]);
}
}
return 0;
}







