您的位置:首页 > 运维架构 > Linux

[编程实例]linux 设置网卡为混杂模式

2008-12-10 20:12 417 查看
#include <stdio.h>

#include <errno.h>

#include <sys/ioctl.h>

#include <stdlib.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <string.h>

#include <linux/in.h>

#include <linux/if_ether.h>

#include <unistd.h>

#include <net/if.h>

int main(int argc, char **argv) {
int sock, n;
struct ifreq ethreq;

if ( (sock=socket(PF_PACKET, SOCK_RAW,
htons(ETH_P_ALL)))<0) {
perror("socket");
exit(1);
}

/* Set the network card in promiscuos mode */
strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
if (ioctl(sock,SIOCGIFFLAGS,ðreq)==-1) {
perror("ioctl");
close(sock);
exit(1);
}
ethreq.ifr_flags|=IFF_PROMISC;
if (ioctl(sock,SIOCSIFFLAGS,ðreq)==-1) {
perror("ioctl");
close(sock);
exit(1);
}

printf("Success to set eth0 to promiscuos mode.../n");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: