我想检查网络设备的状态例如混杂模式。基本上像示出的 IP一个命令。

也许有人可以把我在正确的方向?

我想为Linux因此Linux特定的头是可用做这在C

有帮助吗?

解决方案

您需要使用SIOCGIFFLAGS ioctl来获取与接口有关的标志。然后,可以检查是否IFF_PROMISC标志被设置:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>     
#include <sys/ioctl.h>  /* ioctl()  */
#include <sys/socket.h> /* socket() */
#include <arpa/inet.h>  
#include <unistd.h>     /* close()  */
#include <linux/if.h>   /* struct ifreq */

int main(int argc, char* argv[])
{
    /* this socket doesn't really matter, we just need a descriptor 
     * to perform the ioctl on */
    int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct ifreq ethreq;

    memset(&ethreq, 0, sizeof(ethreq));

    /* set the name of the interface we wish to check */
    strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ);
    /* grab flags associated with this interface */
    ioctl(fd, SIOCGIFFLAGS, &ethreq);
    if (ethreq.ifr_flags & IFF_PROMISC) {
        printf("%s is in promiscuous mode\n",
               ethreq.ifr_name);
    } else {
        printf("%s is NOT in promiscuous mode\n",
               ethreq.ifr_name);
    }

    close(fd);

    return 0;
}

如果你想的设置的接口为混杂模式,您将需要root权限,但你可以简单的设置领域ifr_flags和使用SIOCSIFFLAGS IOCTL:

/* ... */
ethreq.ifr_flags |= IFF_PROMISC;
ioctl(fd, SIOCSIFFLAGS, &ethreq);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top