首 页文章中心

Linux学习网

您的位置Linux学习网 > Linux服务器应用 > 文章内容

Linux 混杂模式

作者:佚名  来源:不详  发布时间:2008-7-3 8:11:00
先在终端上运行:ifconfig eth0 -promisc

将网卡设置成混杂模式.然后运行代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
/*
* set the interface to promisc mode.
*/
int set_netpromisc()
{
int fd, s;
struct ifreq ifr;

fd = socket(AF_INET, SOCK_PACKET, htons(0x800));
if (fd < 0)
{
perror("can not get SOCK_PACKET socket");
return 0;
}

strcpy(ifr.ifr_name, "eth0");

s = ioctl(fd, SIOCGIFFLAGS, &ifr);

if (s < 0)
{
close(fd);
perror("can not get flags");
return 0;
}

ifr.ifr_flags |= IFF_PROMISC;

s = ioctl(fd, SIOCSIFFLAGS, &ifr);
if (s < 0)
{
close(fd);
perror("can not set flags");
return 0;
}
return fd;
}
/*
* display characters
*/
void show_char(void *buff, int len)
{
while (len --)
{
if (buff)
{
printf("%c", *((char *)(buff++)));
}
else
{
printf(".");
buff ++;
}

}
}

/*
* display a buff in Hex.
*/
void show_hex(void *buff, int len)
{
int i = 15, tmp;
while (len --)
{
printf("%02x ", *(unsigned char *)(buff ++));
if (i -- == 0)
{
printf("\t");
show_char(buff - 16, 16);
printf("\n");
i = 15;
}
}
if (i != 0)
{
tmp = i + 1;
while (tmp --)
printf(" %c ", '.');
printf("\t");
show_char(buff - 15 + i, 16 - i);
printf("\n");
}
printf("\n\n");
}

/*
* the main function.
*/
int main()
{
int sockfd, ret;
char buff[4096];

set_netpromisc();
if((sockfd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL))) == -1)
{
perror("socket error");
return 0;
}

memset(buff, 0x00, 4096);

/*
* recvfrom all the packages.
*/
while ((ret = recvfrom(sockfd, buff, 4096, 0, NULL, NULL)) > 0)
{
show_hex(buff, ret); // display it now..
memset(buff, 0x00, ret);
}
close(sockfd);
return 0;
}
收藏本页到: 365Key | del.icio.us | | 添加到雅虎收藏+
  • 网站帮助 - 广告合作 - 网站地图