/*
* =====================================================================================
*
* Filename: sendraw.c
*
* Description: test
*
* Version: 1.0
* Created: 2013年09月27日 12时54分57秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#define BUFLEN 512
char senddata[] = {0x21,0x01,0x01,0x42,0x00,0x00,0x1a,0x3a,0x06,0x04,0x03,0x00,0x0e,0xfb,0x14,0xda,0xe9,0x74,0x9c,0x3c,0xc0,0xa8,0x64,0x3b,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static void dataFilter(char *buffer, int len);
static int sendData(int argc, char *argv[])
{
int skfd, n;
char buf[BUFLEN] = {0,};
struct ether_header *eth = NULL;
struct sockaddr_ll toaddr;
struct ifreq ifr;
unsigned char src_mac[ETH_ALEN] = {0};
unsigned char dst_mac[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
if(3 != argc)
{
printf("Usage: %s netdevName \n", argv[0]);
exit(1);
}
if((skfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
perror("create error !");
exit(1);
}
bzero(&toaddr, sizeof(toaddr));
bzero(&ifr, sizeof(ifr));
strcpy(ifr.ifr_name, argv[1]);
//获取接口索引
if(-1 == ioctl(skfd, SIOCGIFINDEX, &ifr))
{
perror("get dev index error ");
exit(1);
}
toaddr.sll_ifindex = ifr.ifr_ifindex;
printf("interface index:%d\n", ifr.ifr_ifindex);
//获取接口的mac地址
if(-1 == ioctl(skfd, SIOCGIFHWADDR, &ifr))
{
perror("get dev MAC addr error:");
exit(1);
}
memcpy(src_mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
printf("MAC : %02x-%02x-%02x-%02x-%02x-%02x\n", src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5]);
//开始填充,构造以太头部
eth = (struct ether_header *)buf;
memcpy(eth->ether_dhost, dst_mac, ETH_ALEN);
memcpy(eth->ether_shost, src_mac, ETH_ALEN);
eth->ether_type = htons(0x8036);
memcpy((buf+sizeof(struct ether_header)), senddata, sizeof(senddata));
int len = sizeof(struct ether_header)+sizeof(senddata);
toaddr.sll_family = PF_PACKET;
toaddr.sll_protocol = htons(ETH_P_ALL);
n = sendto(skfd, buf, len, 0, (struct sockaddr *)&toaddr, sizeof(toaddr));
char buffer[2048] = {0,};
while(1)
{
n = recvfrom(skfd, buffer, 2048, 0, NULL, NULL);
/*printf("%d bytes read \n", n);*/
dataFilter(buffer, n);
}
close(skfd);
return 0;
}
static void dataFilter(char *buffer, int len)
{
//recv data head of 6 bytes is dest mac, along with if 6 source mac
struct ether_header *eth;
eth = (struct ether_header *)buffer;
if(eth->ether_type == htons(0x8033))
{
printf("===========================len = %d===========================\n", len);
printf("dest mac addr:%02x:%02x:%02x:%02x:%02x:%02x\n", \
eth->ether_dhost[0], eth->ether_dhost[1], eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]);
printf("source mac addr:%02x:%02x:%02x:%02x:%02x:%02x\n", \
eth->ether_shost[0], eth->ether_shost[1], eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
}
}
int main(int argc, char *argv[])
{
sendData(argc, argv);
return 0;
}