C语言 Linux - rtnetlink RTM_GETLINK -处理和理解应答数据

soat7uwm  于 2023-11-16  发布在  Linux
关注(0)|答案(2)|浏览(170)

我试图了解rtnetlink是如何工作的,所以我开始尝试建立一个小库来进入它。
我已经到了发送/接收netlink消息工作正常的地步。现在我正在努力处理 RTM_GETLINK 请求的答案。这是我当前的代码:

#include <errno.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
  unsigned char buf[65535];
  unsigned char mac_buf[6];
  int tmp_int;

  /*
   * a standard request
   */
  struct {
    struct nlmsghdr __r_nl_msg;
    struct ifinfomsg __r_ii_msg;
  } req;

  int sfd, answer_size, attr_len;

  struct nlmsghdr *nl_msg_ptr;
  struct ifinfomsg *inf_msg_ptr;
  struct rtattr *rta_ptr;

  /*
   * create a socket
   */
  if ((sfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
    fprintf(stderr, "ERROR socket(): %s\n", strerror(errno));
    return (sfd);
  }

  /*
   * zero out req
   */
  memset(&req, 0, sizeof(req));

  /*
   * prepare the header
   */
  req.__r_nl_msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  req.__r_nl_msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
  req.__r_nl_msg.nlmsg_type = RTM_GETLINK; /* to get iface information */
  req.__r_nl_msg.nlmsg_seq = 1;            /* start seq */

  req.__r_ii_msg.ifi_family = AF_UNSPEC; /* AF_INET6 for IPv6 */
  req.__r_ii_msg.ifi_change =
      0xffffffff; /* man rtnetlink says this; but why? */

  /*
   * send out request
   */
  if (send(sfd, &req, req.__r_nl_msg.nlmsg_len, 0) < 0) {
    fprintf(stderr, "ERROR send(): %s\n", strerror(errno));
    close(sfd);
    return (-1);
  }

  /*
   * receive the answer/process it
   */
  while (1) {
    if ((answer_size = recv(sfd, buf, sizeof(buf), 0)) < 0) {
      fprintf(stderr, "ERROR recv(): %s\n", strerror(errno));
      close(sfd);
      return (-1);
    }

    printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
    printf("[INFO] %d bytes received.\n", answer_size);

    /*
     * go over the nlmsghdr's in this answer
     */
    for (nl_msg_ptr = (struct nlmsghdr *)buf;
         answer_size > (int)sizeof(*nl_msg_ptr);) {
      int len = nl_msg_ptr->nlmsg_len;

      printf("[INFO] Message length of '%d' bytes.\n", len);

      /*
       * check if we should stop here
       */
      switch (nl_msg_ptr->nlmsg_type) {
      case NLMSG_ERROR:
        printf("[ERROR] Message is of tmp_int 'NLMSG_ERROR'.\n");
      case NLMSG_DONE:
        close(sfd);
        return (0);
      }

      if (!NLMSG_OK(nl_msg_ptr, (unsigned int)answer_size)) {
        printf("[ERROR] Received message is NOT ok.\n");
        close(sfd);
        return (-1);
      }

      /*
       * set pointers to work with
       */
      inf_msg_ptr = (struct ifinfomsg *)NLMSG_DATA(nl_msg_ptr);
      rta_ptr = (struct rtattr *)IFLA_RTA(inf_msg_ptr);

      printf("[INFO] Index is: '%d'.\n", inf_msg_ptr->ifi_index);
      printf("[INFO] Iface Type: '%d'\n", inf_msg_ptr->ifi_type); /* ARPHRD_* */
      printf("[INFO] Up/Down: '%d'\n", inf_msg_ptr->ifi_flags & IFF_UP);
      printf("[INFO] Promisc: '%d'\n", inf_msg_ptr->ifi_flags & IFF_PROMISC);

      /*
       * process attributes
       */
      attr_len = IFLA_PAYLOAD(nl_msg_ptr);

      for (; RTA_OK(rta_ptr, attr_len); rta_ptr = RTA_NEXT(rta_ptr, attr_len)) {
        switch (rta_ptr->rta_type) {
        case IFLA_ADDRESS:
          memcpy(mac_buf, RTA_DATA(rta_ptr), 6);
          printf("[%d][ADDRRESS] %02x:%02x:%02x:%02x:%02x:%02x\n",
                 inf_msg_ptr->ifi_index, mac_buf[0], mac_buf[1], mac_buf[2],
                 mac_buf[3], mac_buf[4], mac_buf[5]);
          break;

        case IFLA_BROADCAST:
          memcpy(mac_buf, RTA_DATA(rta_ptr), 6);
          printf("[%d][BROADCAST] %02x:%02x:%02x:%02x:%02x:%02x\n",
                 inf_msg_ptr->ifi_index, mac_buf[0], mac_buf[1], mac_buf[2],
                 mac_buf[3], mac_buf[4], mac_buf[5]);
          break;

        case IFLA_IFNAME:
          printf("[%d][IFNAME] %s\n", inf_msg_ptr->ifi_index,
                 RTA_DATA(rta_ptr));
          break;

        case IFLA_MTU:
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][MTU] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_LINK:  /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][LINK] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_QDISC:
          printf("[%d][QDISC] %s\n", inf_msg_ptr->ifi_index, RTA_DATA(rta_ptr));
          break;

        case IFLA_STATS:
          printf("[%d][STATS] ...\n", inf_msg_ptr->ifi_index);
          break;

        case IFLA_COST: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][COST] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_PRIORITY: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][PRIORITY] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_MASTER: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][MASTER] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_WIRELESS: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][WIRELESS] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_PROTINFO: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][PROTINFO] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_TXQLEN:
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][TXQLEN] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_MAP: /* ??? */
          printf("[%d][MAP] ...\n", inf_msg_ptr->ifi_index);
          break;

        case IFLA_WEIGHT: /* ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][WIGHT] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_OPERSTATE: /* operational state; interface can be up but if
                                nothing is connected its OPERSTATE is down */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][OPERSTATE] %d=", inf_msg_ptr->ifi_index, tmp_int);
          switch (tmp_int) {
          case IF_OPER_UP:
            printf("IF_OPER_UP\n");
            break;
          case IF_OPER_DORMANT:
            printf("IF_OPER_DORMANT\n");
            break;
          case IF_OPER_LOWERLAYERDOWN:
            printf("IF_OPER_LOWERLAYERDOWN\n");
            break;
          case IF_OPER_DOWN:
            printf("IF_OPER_DOWN\n");
            break;
          case IF_OPER_NOTPRESENT:
            printf("IF_OPER_NOTPRESENT\n");
            break;
          case IF_OPER_TESTING:
            printf("IF_OPER_TESTING\n");
            break;
          case IF_OPER_UNKNOWN:
            printf("IF_OPER_UNKNOWN\n");
            break;
          default:
            printf("N/A\n");
            break;
          };
          break;

        case IFLA_LINKMODE: /* Mode of the link */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][LINKMODE] %d=", inf_msg_ptr->ifi_index, tmp_int);
          if (tmp_int == IF_LINK_MODE_DORMANT) {
            printf("IF_LINK_MODE_DORMANT\n");
          } else if (tmp_int == IF_LINK_MODE_DEFAULT) {
            printf("IF_LINK_MODE_DEFAULT\n");
          } else {
            printf("UNKNOWN\n");
          }
          break;

        case IFLA_LINKINFO: /* ??? */
          printf("[%d][LINKINFO] ... len=%d\n", inf_msg_ptr->ifi_index,
                 rta_ptr->rta_len);
          break;

        case IFLA_IFALIAS: /* alias of the interface - outdated since one
                              interface can have multiple addrs. */
          printf("[%d][IFALIAS] %s\n", inf_msg_ptr->ifi_index,
                 RTA_DATA(rta_ptr));
          break;

        case IFLA_STATS64: /* stats with 64 bit values */
          printf("[%d][STATS64] ...\n", inf_msg_ptr->ifi_index);
          break;

        case IFLA_GROUP: /* the group to which the interface belongs to ??? */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][GROUP] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_PROMISCUITY: /* if > 0, the interface is in promisc mode */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][PROMISCUITY] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_CARRIER: /* if > 0, the interface has a carrier e.g. is
                              connected to something (???) */
          memcpy(&tmp_int, RTA_DATA(rta_ptr), 4);
          printf("[%d][CARRIER] %d\n", inf_msg_ptr->ifi_index, tmp_int);
          break;

        case IFLA_PAD: /* ??? */
          printf("[%d][PAD] len=%d\n", inf_msg_ptr->ifi_index,
                 rta_ptr->rta_len);
          break;

        case IFLA_XDP: /* something about packet filtering ... ??? */
          printf("[%d][XDP] len=%d\n", inf_msg_ptr->ifi_index,
                 rta_ptr->rta_len);
          break;

        case IFLA_EVENT: /* ??? */
          printf("[%d][EVENT] len=%d\n", inf_msg_ptr->ifi_index,
                 rta_ptr->rta_len);
          break;

        default:
          printf("[%d][UNKNOWN] len=%d tmp_int=%d\n", inf_msg_ptr->ifi_index,
                 rta_ptr->rta_len, rta_ptr->rta_type);
          break;
        };
      }

      answer_size -= NLMSG_ALIGN(len);
      nl_msg_ptr = (struct nlmsghdr *)((char *)nl_msg_ptr + NLMSG_ALIGN(len));
    }
  }
  return (0);
}

字符串
我最大的问题是我不知道一些属性类型的含义以及它们的形式(*char[],int,unsigned int,... *)。此外,我不知道如何处理“嵌套”属性,如 IFLA_LINKINFO

IFLA_LINK, IFLA_COST, IFLA_PRIORITY, IFLA_MASTER, IFLA_WIRELESS, IFLA_PROTINFO, IFLA_MAP, IFLA_WEIGHT, IFLA_LINKINFO, IFLA_AF_SPEC, IFLA_GROUP, IFLA_NUM_RX_QUEUES, IFLA_NUM_TX_QUEUES, IFLA_CARRIER, IFLA_CARRIER_CHANGES, IFLA_PROTO_DOWN, IFLA_GSO_MAX_SEGS, IFLA_GSO_MAX_SIZE, IFLA_PAD, IFLA_CARRIER_UP_COUNT, IFLA_CARRIER_DOWN_COUNT, IFLA_EVENT


.所以基本上所有的.我使用 /usr/include/Linux/if_link.h 作为属性类型的引用.
任何形式的帮助对我的任何问题将是真的很好!(也对我的代码反馈)--提前感谢!

izkcnapc

izkcnapc1#

最简单的方法是使用libnl:
libnl github
描述于:
libnl docs
该链接的站点以及API信息可以在构建库时生成。
当您下载并构建后,有几个链接讨论这些属性:

libnl/doc/api/lib_2route_2link_8c_source.html

lib/route/link.c

字符串

olqngx59

olqngx592#

下面是一个简单的例子:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/types.h>
#include <sys/socket.h>

#define MAX_PAYLOAD 2048

int main() {
    int nl_socket;
    struct sockaddr_nl src_addr, dest_addr;
    struct nlmsghdr *nlh;
    struct iovec iov;
    struct msghdr msg;

    // Create a Netlink socket
    nl_socket = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    if (nl_socket == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    // Initialize the source and destination addresses
    memset(&src_addr, 0, sizeof(src_addr));
    src_addr.nl_family = AF_NETLINK;
    src_addr.nl_pid = getpid();
    src_addr.nl_groups = 0;  // unicast

    memset(&dest_addr, 0, sizeof(dest_addr));
    dest_addr.nl_family = AF_NETLINK;
    dest_addr.nl_pid = 0;  // Kernel
    dest_addr.nl_groups = 0;  // unicast

    // Bind the socket
    bind(nl_socket, (struct sockaddr*)&src_addr, sizeof(src_addr));

    // Prepare and send the Netlink request
    nlh = (struct nlmsghdr*)malloc(NLMSG_SPACE(MAX_PAYLOAD));
    memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
    nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
    nlh->nlmsg_type = RTM_GETLINK;
    nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;

    iov.iov_base = (void*)nlh;
    iov.iov_len = nlh->nlmsg_len;

    memset(&msg, 0, sizeof(msg));
    msg.msg_name = (void*)&dest_addr;
    msg.msg_namelen = sizeof(dest_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

    // Send the request
    sendmsg(nl_socket, &msg, 0);

    // Receive and process responses
    while (1) {
        memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
        recvmsg(nl_socket, &msg, 0);

        if (nlh->nlmsg_type == NLMSG_DONE)
            break;

        if (nlh->nlmsg_type == NLMSG_ERROR) {
            perror("Netlink error");
            exit(EXIT_FAILURE);
        }

        struct ifinfomsg *ifi_info = (struct ifinfomsg*)NLMSG_DATA(nlh);

        printf("Interface Index: %d\n", ifi_info->ifi_index);
    }

    free(nlh);

    // Close the Netlink socket
    close(nl_socket);

    return 0;
}

字符串

相关问题