开发者

find network interfaces that are not RUNNING

If I run the following code, it only prints interfaces that are in the RUNNING state. Is there a way to get a list of interfaces that are not RUNNING and could be either UP or DOWN?

int main()
{
    struct ifreq *pIfr;
    struct ifconf ifc;
    char buf[1024];
    int s, i, j;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s==-1)
        return -1;

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    ioctl(s, SIOCGIFCONF, &ifc);

    pIfr = ifc.ifc_req;
    for (i = ifc.ifc_len / sizeof(struct ifreq); --开发者_开发技巧i >= 0; pIfr++) {
        printf("name=%s\n", pIfr->ifr_name);
    }
    close(s);
    return 0;
}
~           


check man netdevice -- " The kernel fills the ifreqs with all current L3 interface addresses that are running: "

address is not well-defined if the interface is not running ... but you can get the names:

" The names of interfaces with no addresses or that don’t have the IFF_RUNNING flag set can be found via /proc/net/dev."


Looks like an ioctl loop with SIOCGIFNAME returns all interfaces. The input is an index, and the call returns the interface name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜