【问题标题】:How to get net encap in c\c++ on linux?如何在 Linux 上的 c\c++ 中获取网络封装?
【发布时间】:2017-11-29 09:26:49
【问题描述】:
  • 在 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 
    

【问题讨论】:

    标签: c++ c linux networking


    【解决方案1】:

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

    /* 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)
    

    【讨论】:

    • 带有标题文档的链接,也许是一个简短的例子,这可能是一个很好的答案;)
    • 我没有找到返回“Link encap”(网络类型)的字段。 ioctl(sock, SIOCGIFADDR, &ifr)-------->获取ip
    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    相关资源
    最近更新 更多