]> granicus.if.org Git - libnl/commitdiff
rtnl-addr: Inherit prefix length to nl_addr objs in rtnl_addr_set_prefixlen()
authorThomas Graf <tgraf@suug.ch>
Thu, 14 Mar 2013 17:52:17 +0000 (18:52 +0100)
committerThomas Graf <tgraf@suug.ch>
Thu, 14 Mar 2013 17:52:17 +0000 (18:52 +0100)
Previously if using rtnl_addr_set_prefixlen() the new prefix length
was not forwarded to the corresponding 'struct nl_addr' objects
associated with address already and thus the comparison function
would fail.

This patch also clears the internal ADDR_ATTR_PREFIXLEN flag if
the prefix length has been reset.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
lib/route/addr.c

index 95a9447dff66d2ce77830f3b0b85d73da463c364..e62c9647bd328700417434742e8d1fed4d7ed636 100644 (file)
@@ -814,10 +814,39 @@ int rtnl_addr_get_family(struct rtnl_addr *addr)
        return addr->a_family;
 }
 
-void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefix)
+/**
+ * Set the prefix length / netmask
+ * @arg addr           Address
+ * @arg prefixlen      Length of prefix (netmask)
+ *
+ * Modifies the length of the prefix. If the address object contains a peer
+ * address the prefix length will apply to it, otherwise the prefix length
+ * will apply to the local address of the address.
+ *
+ * If the address object contains a peer or local address the corresponding
+ * `struct nl_addr` will be updated with the new prefix length.
+ *
+ * @note Specifying a length of 0 will remove the prefix length alltogether.
+ *
+ * @see rtnl_addr_get_prefixlen()
+ */
+void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen)
 {
-       addr->a_prefixlen = prefix;
-       addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
+       addr->a_prefixlen = prefixlen;
+
+       if (prefixlen)
+               addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
+       else
+               addr->ce_mask &= ~ADDR_ATTR_PREFIXLEN;
+
+       /*
+        * The prefix length always applies to the peer address if
+        * a peer address is present.
+        */
+       if (addr->a_peer)
+               nl_addr_set_prefixlen(addr->a_peer, prefixlen);
+       else if (addr->a_local)
+               nl_addr_set_prefixlen(addr->a_local, prefixlen);
 }
 
 int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)