Chinaunix首页 | 论坛 | 博客
  • 博客访问: 662801
  • 博文数量: 156
  • 博客积分: 4833
  • 博客等级: 上校
  • 技术积分: 1554
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-21 19:36
文章分类

全部博文(156)

文章存档

2016年(2)

2013年(1)

2012年(13)

2011年(30)

2010年(46)

2009年(29)

2008年(23)

2007年(12)

分类: LINUX

2010-02-05 09:45:53


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include /* the L2 protocols */

#define BUFFER_MAX 2048



int multicast_eth(int fd)
{
struct sockaddr_ll sll;
struct ifreq ifstruct;
struct packet_mreq mr;
int ret;

memset( &sll, 0, sizeof(sll) );
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons(ETH_P_ALL);


strcpy(ifstruct.ifr_name, "eth0");
ret = ioctl(fd, SIOCGIFINDEX, &ifstruct);
if(ret != 0 )
{
printf("line:% d\n", __LINE__);
}

sll.sll_ifindex = ifstruct.ifr_ifindex;
mr.mr_ifindex = ifstruct.ifr_ifindex;

ret = ioctl (fd, SIOCGIFHWADDR, &ifstruct);
if(ret != 0 )
{
printf("line:% d\n", __LINE__);
}
memcpy (sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, sizeof(sll.sll_addr));


if( bind(fd, (struct sockaddr *)&sll, sizeof(sll)) == -1 )
{
printf( "bind: ERROR\n" );
return -1;
}



ret = ioctl (fd, SIOCGIFFLAGS, &ifstruct);
if(ret != 0 )
{
printf("error: %s \n",strerror(errno));
printf("ret= %d,line: %d\n", ret,__LINE__);
}

ifstruct.ifr_flags |= (IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
ifstruct.ifr_flags &= ~IFF_ALLMULTI;
//
ret = ioctl (fd, SIOCSIFFLAGS, &ifstruct);
if(ret != 0 )
{
printf("error: %s \n",strerror(errno));
printf("ret= %d,line: %d\n", ret,__LINE__);
}
char mymac[6]={0x01,0x80,0xC2,0x00,0x00,0x00};

/*
memcpy(ifstruct.ifr_hwaddr.sa_data, mymac, 6);
ifstruct.ifr_hwaddr.sa_data[0] = 0x01;
ifstruct.ifr_hwaddr.sa_data[1] = 0x80;
ifstruct.ifr_hwaddr.sa_data[2] = 0xC2;
ifstruct.ifr_hwaddr.sa_data[3] = 0x00;
ifstruct.ifr_hwaddr.sa_data[4] = 0;
ifstruct.ifr_hwaddr.sa_data[5] = 0;

ret = ioctl (fd, SIOCADDMULTI, ifstruct);
if(ret != 0 )
{
printf("error: %s \n",strerror(errno));
printf("ret= %d,line: %d\n", ret,__LINE__);
}
*/
strcpy(ifstruct.ifr_name, "eth0");
ret = ioctl(fd, SIOCGIFINDEX, &ifstruct);
if(ret != 0 )
{
printf("line:% d\n", __LINE__);
}
memset(&mr, 0, sizeof(mr));
mr.mr_ifindex = ifstruct.ifr_ifindex;
mr.mr_type = PACKET_MR_MULTICAST;
mr.mr_alen = 6;
memcpy(mr.mr_address, mymac, 6);

ret = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,&mr, sizeof(mr));
if(ret != 0 )
{
printf("error: %s \n",strerror(errno));
printf("ret= %d,line: %d\n", ret,__LINE__);
}

return 0;
}

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

int sock, n_read, proto;
char buffer[BUFFER_MAX];
char *ethhead, *iphead, *tcphead, *udphead, *icmphead, *p;


if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
fprintf(stdout, "create socket error\n");
return -1;
}

multicast_eth(sock);



while(1)
{
n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
/*
14 6(dest)+6(source)+2(type or length)
+
20 ip header
+
8 icmp,tcp or udp header
= 42
*/
if(n_read < 42)
{
fprintf(stdout, "Incomplete header, packet corrupt\n");
continue;
}

ethhead = buffer;
p = ethhead;
int n = 0xFF;
printf("MAC: %.2X:%02X:%02X:%02X:%02X:%02X==>"
"%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
p[6]&n, p[7]&n, p[8]&n, p[9]&n, p[10]&n, p[11]&n,
p[0]&n, p[1]&n, p[2]&n,p[3]&n, p[4]&n, p[5]&n);

iphead = ethhead + 14;
p = iphead + 12;

printf("IP: %d.%d.%d.%d => %d.%d.%d.%d\n",
p[0]&0XFF, p[1]&0XFF, p[2]&0XFF, p[3]&0XFF,
p[4]&0XFF, p[5]&0XFF, p[6]&0XFF, p[7]&0XFF);
proto = (iphead + 9)[0];
p = iphead + 20;
printf("Protocol: ");
switch(proto)
{
case IPPROTO_ICMP: printf("ICMP\n");break;
case IPPROTO_IGMP: printf("IGMP\n");break;
case IPPROTO_IPIP: printf("IPIP\n");break;
case IPPROTO_TCP :
case IPPROTO_UDP :
printf("%s,", proto == IPPROTO_TCP ? "TCP": "UDP");
printf("source port: %u,",(p[0]<<8)&0XFF00 | p[1]&0XFF);
printf("dest port: %u\n", (p[2]<<8)&0XFF00 | p[3]&0XFF);
break;
case IPPROTO_RAW : printf("RAW\n");break;
default:printf("Unkown, please query in include/linux/in.h\n");
}
}
}


阅读(1234) | 评论(0) | 转发(0) |
0

上一篇:Understanding PIC GOT (ZZ)

下一篇:移植 dhcp 4.1.1

给主人留下些什么吧!~~