c++ - 如何在 linux 上的 c\c++ 中获取 net encap?

标签 c++ c linux networking

  • 在 Linux 中有没有办法使用 C\C++ 代码获取有关所有网络适配器“Link encap:*****”的信息?我已经有了 IP 地址和 MAC 地址之类的东西。

  • 以下是 ifconfig 的示例输出:

    ethx  Link encap:Ethernet  HWaddr 00:0F:20:CF:8B:42
          inet addr:217.149.127.10  Bcast:217.149.127.63  Mask:255.255.255.192
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2472694671 errors:1 dropped:0 overruns:0 frame:0
          TX packets:44641779 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1761467179 (1679.8 Mb)  TX bytes:2870928587 (2737.9 Mb)
          Interrupt:28 
    

最佳答案

请看结构ifreq<net/if.h>

/* Interface request structure used for socket ioctl's. All interface ioctl's must have
parameter definitions which begin with ifr_name. The remainder may be interface specific. */

struct ifreq
  {
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZE
    union
      {
        char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
      } ifr_ifrn;

    union
      {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        struct sockaddr ifru_netmask;
        struct sockaddr ifru_hwaddr;
        short int ifru_flags;
        int ifru_ivalue;
        int ifru_mtu;
        struct ifmap ifru_map;
        char ifru_slave[IFNAMSIZ]; /* Just fits the size */
        char ifru_newname[IFNAMSIZ];
         __caddr_t ifru_data;
      } ifr_ifru;
  };
# define ifr_name ifr_ifrn.ifrn_name /* interface name */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
# define ifr_addr ifr_ifru.ifru_addr /* address */
# define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
# define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
# define ifr_flags ifr_ifru.ifru_flags /* flags */
# define ifr_metric ifr_ifru.ifru_ivalue /* metric */
# define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
# define ifr_map ifr_ifru.ifru_map /* device map */
# define ifr_slave ifr_ifru.ifru_slave /* slave device */
# define ifr_data ifr_ifru.ifru_data /* for use by interface */
# define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
# define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
# define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
# define ifr_newname ifr_ifru.ifru_newname /* New name */
# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

关于c++ - 如何在 linux 上的 c\c++ 中获取 net encap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47549228/

相关文章:

C - 在 memcpy 中使用 strchr

c - 二叉树搜索是在骗我吗?

c - 为什么 unsigned int 仍然是 signed?

linux - 无法找到安装了 abrt-hook-cpp 的核心文件

php - 使用 PHPMailer 发送后邮件被换行符破坏

c++ - cloud9 c++开发环境如何添加c++库

C++ 自定义安装程序(启动器)

c++ - 为什么 gcc 不将类声明编译为引用参数?

c++ - 无法使用 std::cout 打印/输出任何内容

linux - RTW_CMD_THREAD 负责什么?