From: Arran Cudbard-Bell Date: Wed, 3 May 2017 13:49:19 +0000 (-0400) Subject: Add ipaddr2 field to value_box_t so we can start converting ip fields over X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e39611c166003d9040ac2cc1021d6461ddea7a7;p=thirdparty%2Ffreeradius-server.git Add ipaddr2 field to value_box_t so we can start converting ip fields over --- diff --git a/src/include/pair.h b/src/include/pair.h index e7edfca343e..0bdef3fb217 100644 --- a/src/include/pair.h +++ b/src/include/pair.h @@ -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. diff --git a/src/include/value.h b/src/include/value.h index b064e4850a1..5fbafb8bcba 100644 --- a/src/include/value.h +++ b/src/include/value.h @@ -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 diff --git a/src/lib/util/value.c b/src/lib/util/value.c index 692074593ed..26d3f059259 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -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.