]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add ipaddr2 field to value_box_t so we can start converting ip fields over
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 3 May 2017 13:49:19 +0000 (09:49 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 3 May 2017 13:49:29 +0000 (09:49 -0400)
src/include/pair.h
src/include/value.h
src/lib/util/value.c

index e7edfca343ec06c616c6338964c323bb62752e50..0bdef3fb21719c731321cd95ffe32a5cfda7ff36 100644 (file)
@@ -68,6 +68,9 @@ struct value_box {
                uint8_t                 ipv4prefix[6];          //!< IPv4 prefix (should be struct?).
                struct in6_addr         ipv6addr;               //!< IPv6 Address.
                uint8_t                 ipv6prefix[18];         //!< IPv6 prefix (should be struct?).
+
+               fr_ipaddr_t             ipaddr2;                //!< IPv4/6 address/prefix.
+
                uint8_t                 ifid[8];                //!< IPv6 interface ID (should be struct?).
                uint8_t                 ether[6];               //!< Ethernet (MAC) address.
 
index b064e4850a1c7f10349da2ee867e1f74dbe0901c..5fbafb8bcbab19cdd7a66d58159cd089af20dbed 100644 (file)
@@ -67,9 +67,11 @@ int          value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, value_box_t *dst, uint8_t
 /*
  *     Parsing
  */
+int            value_box_from_ipaddr(value_box_t *dst, fr_ipaddr_t const *ipaddr);
+
 int            value_box_from_str(TALLOC_CTX *ctx, value_box_t *dst,
-                                   PW_TYPE *src_type, fr_dict_attr_t const *src_enumv,
-                                   char const *src, ssize_t src_len, char quote);
+                                  PW_TYPE *src_type, fr_dict_attr_t const *src_enumv,
+                                  char const *src, ssize_t src_len, char quote);
 
 /*
  *     Printing
index 692074593ed0e170a39edc4ad174d2a67f820a2e..26d3f059259d5dce0d47cb9db8929a4d83e68db1 100644 (file)
@@ -1732,6 +1732,61 @@ int value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, value_box_t *dst, uint8_t *
        return 0;
 }
 
+/** Assign a #value_box_t value from an #fr_ipaddr_t
+ *
+ * Automatically determines the type of the value box from the ipaddr address family
+ * and the length of the prefix field.
+ *
+ * @param[in] dst      to assign ipaddr to.
+ * @param[in] ipaddr   to copy address from.
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+int value_box_from_ipaddr(value_box_t *dst, fr_ipaddr_t const *ipaddr)
+{
+       PW_TYPE type;
+
+       switch (ipaddr->af) {
+       case AF_INET:
+               if (ipaddr->prefix > 32) {
+                       fr_strerror_printf("Invalid IPv6 prefix length %i", ipaddr->prefix);
+                       return -1;
+               }
+
+               if (ipaddr->prefix == 32) {
+                       type = PW_TYPE_IPV4_ADDR;
+               } else {
+                       type = PW_TYPE_IPV4_PREFIX;
+               }
+               break;
+
+       case AF_INET6:
+               if (ipaddr->prefix > 128) {
+                       fr_strerror_printf("Invalid IPv6 prefix length %i", ipaddr->prefix);
+                       return -1;
+               }
+
+               if (ipaddr->prefix == 128) {
+                       type = PW_TYPE_IPV6_ADDR;
+               } else {
+                       type = PW_TYPE_IPV6_PREFIX;
+               }
+               break;
+
+       default:
+               fr_strerror_printf("Invalid address family %i", ipaddr->af);
+               return -1;
+       }
+
+       dst->type = type;
+       dst->tainted = false;   /* Discuss? */
+       dst->datum.ipaddr2 = *ipaddr;
+       dst->length = 0;        /* Length doesn't make sense for fixed length types */
+
+       return 0;
+}
+
 /** Convert string value to a value_box_t type
  *
  * @todo Should take taint param.