+ -
当前位置:首页 → 问答吧 → linux c/c++ 如何获取网络接口名称呢?

linux c/c++ 如何获取网络接口名称呢?

时间:2011-10-16

来源:互联网

比如有多张网卡。 使用 ifconfig 的命令 可以看到 eth0 eth1 lo 等。。并且会显示他们的详细信息,我要如何获得 eth0 eth1 这些名称呢?

作者: aaadddzxc   发布时间: 2011-10-16

ioctl.

作者: qq120848369   发布时间: 2011-10-16

我也想指定, 期待中。。。

作者: linux_6   发布时间: 2011-10-16

C/C++ code
Each network interface in a system corresponds to a path through which messages may be sent and received. A network interface usually has a hardware device associated with it, though certain interfaces such as the loopback interface, lo(4), do not.
The following ioctl(2) calls may be used to manipulate network interfaces. The ioctl is made on a socket (typically of type SOCK_DGRAM) in the desired domain. Most of the requests supported in earlier releases take an 
.Vt ifreq structure as its parameter. This structure has the form

struct  ifreq {
#define    IFNAMSIZ    16
    char    ifr_name[IFNAMSIZ];        /* if name, e.g. "en0" */
    union {
        struct    sockaddr ifru_addr;
        struct    sockaddr ifru_dstaddr;
        struct    sockaddr ifru_broadaddr;
        short     ifru_flags[2];
        short     ifru_index;
        int       ifru_metric;
        int       ifru_mtu;
        int       ifru_phys;
        int       ifru_media;
        caddr_t   ifru_data;
        int       ifru_cap[2];
    } ifr_ifru;
#define ifr_addr      ifr_ifru.ifru_addr      /* address */
#define ifr_dstaddr   ifr_ifru.ifru_dstaddr   /* other end of p-to-p link */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_flags     ifr_ifru.ifru_flags[0]  /* flags (low 16 bits) */
#define ifr_flagshigh ifr_ifru.ifru_flags[1]  /* flags (high 16 bits) */
#define ifr_metric    ifr_ifru.ifru_metric    /* metric */
#define ifr_mtu       ifr_ifru.ifru_mtu       /* mtu */
#define ifr_phys      ifr_ifru.ifru_phys      /* physical wire */
#define ifr_media     ifr_ifru.ifru_media     /* physical media */
#define ifr_data      ifr_ifru.ifru_data      /* for use by interface */
#define ifr_reqcap    ifr_ifru.ifru_cap[0]    /* requested capabilities */
#define ifr_curcap    ifr_ifru.ifru_cap[1]    /* current capabilities */
#define ifr_index     ifr_ifru.ifru_index     /* interface index */
};

作者: linwhwylb   发布时间: 2011-10-16

作者: linwhwylb   发布时间: 2011-10-16