Browse Source

added prefix length to addresses

Fabian Peter Hammerle 7 years ago
parent
commit
f88d283f2b
4 changed files with 27 additions and 14 deletions
  1. 5 5
      README.md
  2. 8 3
      address.h
  3. 7 3
      inet6_address.h
  4. 7 3
      inet_address.h

+ 5 - 5
README.md

@@ -17,23 +17,23 @@ links:
   mtu: 65536
   mtu: 65536
   operstate: unknown
   operstate: unknown
   addresses:
   addresses:
-  - 127.0.0.1
+  - 127.0.0.1/8
 - ifname: eth0
 - ifname: eth0
   address: 11:22:33:44:55:66
   address: 11:22:33:44:55:66
   broadcast: ff:ff:ff:ff:ff:ff
   broadcast: ff:ff:ff:ff:ff:ff
   mtu: 1500
   mtu: 1500
   operstate: up
   operstate: up
   addresses:
   addresses:
-  - 192.168.2.101
-  - fe80::1322:33ff:fe44:5566
+  - 192.168.2.101/24
+  - fe80::1322:33ff:fe44:5566/64
 - ifname: wlan0
 - ifname: wlan0
   address: 77:88:99:00:aa:bb
   address: 77:88:99:00:aa:bb
   broadcast: ff:ff:ff:ff:ff:ff
   broadcast: ff:ff:ff:ff:ff:ff
   mtu: 1500
   mtu: 1500
   operstate: up
   operstate: up
   addresses:
   addresses:
-  - 192.168.20.11
-  - fe80::7588:99ff:fe00:aabb
+  - 192.168.20.11/24
+  - fe80::7588:99ff:fe00:aabb/64
 
 
 ```
 ```
 
 

+ 8 - 3
address.h

@@ -11,12 +11,15 @@
 #include <linux/netlink.h> // struct nlattr
 #include <linux/netlink.h> // struct nlattr
 #include <ostream>         // std::ostream
 #include <ostream>         // std::ostream
 
 
+// https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/ip/ipaddress.c
+// print_addrinfo()
+
 class Address : public YamlObject {
 class Address : public YamlObject {
 public:
 public:
   unsigned int ifindex;
   unsigned int ifindex;
 
 
 private:
 private:
-  unsigned char family;
+  unsigned char family, prefixlen;
   union {
   union {
     InetAddress inet_addr;
     InetAddress inet_addr;
     Inet6Address inet6_addr;
     Inet6Address inet6_addr;
@@ -36,6 +39,7 @@ public:
     ifindex = msg->ifa_index;
     ifindex = msg->ifa_index;
     family = msg->ifa_family;
     family = msg->ifa_family;
     assert(family == AF_INET || family == AF_INET6);
     assert(family == AF_INET || family == AF_INET6);
+    prefixlen = msg->ifa_prefixlen;
   }
   }
 
 
   // typedef int (*mnl_attr_cb_t)(const struct nlattr *attr, void *data);
   // typedef int (*mnl_attr_cb_t)(const struct nlattr *attr, void *data);
@@ -58,10 +62,11 @@ public:
                   const yaml_indent_level_t indent_level = 0) const {
                   const yaml_indent_level_t indent_level = 0) const {
     // const std::string indent(indent_level, ' ');
     // const std::string indent(indent_level, ' ');
     if (family == AF_INET) {
     if (family == AF_INET) {
-      inet_addr.write_yaml(stream, indent_level + 2);
+      stream << inet_addr.format();
     } else if (family == AF_INET6) {
     } else if (family == AF_INET6) {
-      inet6_addr.write_yaml(stream, indent_level + 2);
+      stream << inet6_addr.format();
     }
     }
+    stream << '/' << (int)prefixlen << '\n';
   }
   }
 };
 };
 
 

+ 7 - 3
inet6_address.h

@@ -33,11 +33,15 @@ public:
     inet_ntop(AF_INET6, bytes, dst, size);
     inet_ntop(AF_INET6, bytes, dst, size);
   }
   }
 
 
-  void write_yaml(std::ostream &stream,
-                  const yaml_indent_level_t indent_level = 0) const {
+  std::string format() const {
     char str[INET6_ADDRSTRLEN];
     char str[INET6_ADDRSTRLEN];
     format(str, sizeof(str));
     format(str, sizeof(str));
-    stream << str << '\n';
+    return std::string(str);
+  }
+
+  void write_yaml(std::ostream &stream,
+                  const yaml_indent_level_t indent_level = 0) const {
+    stream << format() << '\n';
   }
   }
 };
 };
 
 

+ 7 - 3
inet_address.h

@@ -33,11 +33,15 @@ public:
     inet_ntop(AF_INET, bytes, dst, size);
     inet_ntop(AF_INET, bytes, dst, size);
   }
   }
 
 
-  void write_yaml(std::ostream &stream,
-                  const yaml_indent_level_t indent_level = 0) const {
+  std::string format() const {
     char str[INET_ADDRSTRLEN];
     char str[INET_ADDRSTRLEN];
     format(str, sizeof(str));
     format(str, sizeof(str));
-    stream << str << '\n';
+    return std::string(str);
+  }
+
+  void write_yaml(std::ostream &stream,
+                  const yaml_indent_level_t indent_level = 0) const {
+    stream << format() << '\n';
   }
   }
 };
 };