소스 검색

Link: added index attr; main link_cb: assert order corresponding with index

Fabian Peter Hammerle 7 년 전
부모
커밋
cffc28999b
3개의 변경된 파일27개의 추가작업 그리고 6개의 파일을 삭제
  1. 17 4
      link.cpp
  2. 7 0
      link.h
  3. 3 2
      main.cpp

+ 17 - 4
link.cpp

@@ -1,13 +1,25 @@
 #include "link.h"
 
-#include <libmnl/libmnl.h>
-#include <linux/if_link.h>
-#include <string>
+#include <libmnl/libmnl.h> // mnl_attr_*
+#include <linux/if_link.h> // IFLA_*
+#include <string>          // std::string
 
-// https://netfilter.org/projects/libmnl/doxygen/html/group__attr.html
+Link::Link(const ifinfomsg *msg) {
+  /*
+  struct ifinfomsg {
+      unsigned char  ifi_family; // AF_UNSPEC
+      unsigned short ifi_type;   // Device type
+      int            ifi_index;  // Interface index
+      unsigned int   ifi_flags;  // Device flags
+      unsigned int   ifi_change; // change mask
+  };
+  */
+  index = msg->ifi_index;
+}
 
 int Link::mnl_attr_cb(const nlattr *attr, void *data) {
   Link *link = (Link *)data;
+  // https://netfilter.org/projects/libmnl/doxygen/html/group__attr.html
   // /usr/include/linux/if_link.h
   switch (mnl_attr_get_type(attr)) {
   case IFLA_ADDRESS:
@@ -35,6 +47,7 @@ void Link::write_yaml(std::ostream &stream,
                       const yaml_indent_level_t indent_level) const {
   const std::string indent(indent_level, ' ');
   stream << "ifname: " + ifname + "\n";
+  // stream << indent << "index: " << index << "\n";
   stream << indent << "address: ";
   address.write_yaml(stream);
   stream << "\n";

+ 7 - 0
link.h

@@ -6,16 +6,23 @@
 #include "yaml.h"
 
 #include <linux/netlink.h>
+#include <linux/rtnetlink.h> // struct ifinfomsg
 #include <ostream>
 #include <string>
 
 class Link : public YamlObject {
+public:
+  unsigned int index;
+
+private:
   std::string ifname;
   HardwareAddress address, broadcast;
   unsigned long int mtu;
   OperState operstate;
 
 public:
+  Link(const ifinfomsg *msg);
+
   // typedef int (*mnl_attr_cb_t)(const struct nlattr *attr, void *data);
   static int mnl_attr_cb(const nlattr *attr, void *data);
 

+ 3 - 2
main.cpp

@@ -9,7 +9,7 @@
 #include <libmnl/libmnl.h>
 #include <linux/if_addr.h>
 #include <linux/netlink.h>
-#include <linux/rtnetlink.h>
+#include <linux/rtnetlink.h> // struct ifinfomsg
 
 // https://netfilter.org/projects/libmnl/doxygen/html/modules.html
 
@@ -28,7 +28,8 @@ void mnl_recv_run_cb_all(const mnl_socket *nl, void *buf, size_t bufsiz,
 
 int link_cb(const nlmsghdr *nlh, void *data) {
   vector<Link> *links = (vector<Link> *)data;
-  links->emplace_back();
+  links->emplace_back((const ifinfomsg *)mnl_nlmsg_get_payload(nlh));
+  assert(links->back().index == links->size());
   mnl_attr_parse(nlh, sizeof(ifinfomsg), Link::mnl_attr_cb, &links->back());
   return MNL_CB_OK;
 }