From: Arran Cudbard-Bell Date: Tue, 10 Apr 2018 04:40:39 +0000 (+0600) Subject: Add fr_pair_add_by_da X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d8b28ca2604eb21fa88fbdca9b7c5b546b2a00a;p=thirdparty%2Ffreeradius-server.git Add fr_pair_add_by_da --- diff --git a/src/include/pair.h b/src/include/pair.h index 2030f544f17..6ea8d3ceea2 100644 --- a/src/include/pair.h +++ b/src/include/pair.h @@ -224,6 +224,9 @@ void fr_pair_delete_by_num(VALUE_PAIR **head, unsigned int vendor, unsigned int void fr_pair_delete_by_child_num(VALUE_PAIR **head, fr_dict_attr_t const *parent, unsigned int attr, int8_t tag); +VALUE_PAIR *fr_pair_add_by_da(TALLOC_CTX *ctx, VALUE_PAIR **list, + fr_dict_attr_t const *da, int8_t tag); + VALUE_PAIR *fr_pair_update_by_da(TALLOC_CTX *ctx, VALUE_PAIR **list, fr_dict_attr_t const *da, int8_t tag, bool skip_if_exists); diff --git a/src/include/radiusd.h b/src/include/radiusd.h index 6947bf22427..028d0e54d37 100644 --- a/src/include/radiusd.h +++ b/src/include/radiusd.h @@ -527,24 +527,45 @@ int radius_copy_vp(TALLOC_CTX *ctx, VALUE_PAIR **out, REQUEST *request, char con #define pair_make_reply(_a, _b, _c) fr_pair_make(request->reply, &request->reply->vps, _a, _b, _c) #define pair_make_config(_a, _b, _c) fr_pair_make(request, &request->control, _a, _b, _c) +/** Allocate a VALUE_PAIR in the request list + * + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. + */ +#define pair_add_request(_da, _tag) fr_pair_add_by_da(request->packet, &request->packet->vps, _da, _tag) + +/** Allocate a VALUE_PAIR in the reply list + * + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. + */ +#define pair_add_reply(_da, _tag) fr_pair_add_by_da(request->reply, &request->reply->vps, _da, _tag) + +/** Allocate a VALUE_PAIR in the control list + * + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. + */ +#define pair_add_control(_da, _tag) fr_pair_add_by_da(request, &request->control, _da, _tag) + /** Return or allocate a VALUE_PAIR in the request list * - * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. - * @param[in] _tag tag of the attribute to be found or allocated. + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. */ #define pair_update_request(_da, _tag) fr_pair_update_by_da(request->packet, &request->packet->vps, _da, _tag, false) /** Return or allocate a VALUE_PAIR in the reply list * - * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. - * @param[in] _tag tag of the attribute to be found or allocated. + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. */ #define pair_update_reply(_da, _tag) fr_pair_update_by_da(request->reply, &request->reply->vps, _da, _tag, false) /** Return or allocate a VALUE_PAIR in the control list * - * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. - * @param[in] _tag tag of the attribute to be found or allocated. + * @param[in] _da #fr_dict_attr_t of the pair to be found or allocated. + * @param[in] _tag tag of the attribute to be found or allocated. */ #define pair_update_control(_da, _tag) fr_pair_update_by_da(request, &request->control, _da, _tag, false) diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 4dee793f14d..caaad26afef 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -897,6 +897,32 @@ void *fr_pair_iter_next_by_da(void **prev, void *to_eval, void *uctx) return NULL; } +/** Create a new VALUE_PAIR + * + * @param[in] ctx to allocate new #VALUE_PAIR in. + * @param[in,out] list in search and insert into it. + * @param[in] da of attribute to update. + * @param[in] tag of attribute to update. + * @return + * - 0 on success. + * - -1 on failure. + */ +VALUE_PAIR *fr_pair_add_by_da(TALLOC_CTX *ctx, VALUE_PAIR **list, + fr_dict_attr_t const *da, int8_t tag) +{ + vp_cursor_t cursor; + VALUE_PAIR *vp; + + (void)fr_pair_cursor_init(&cursor, list); + vp = fr_pair_afrom_da(ctx, da); + if (!vp) return NULL; + vp->tag = tag; + + fr_pair_cursor_prepend(&cursor, vp); + + return vp; +} + /** Create a new VALUE_PAIR or replaces the value of the head pair in the specified list * * If skip_if_exists is true, will return NULL if a pair matching the specified #fr_dict_attr_t @@ -920,8 +946,8 @@ VALUE_PAIR *fr_pair_update_by_da(TALLOC_CTX *ctx, VALUE_PAIR **list, fr_dict_attr_t const *da, int8_t tag, bool skip_if_exists) { - vp_cursor_t cursor; - VALUE_PAIR *vp; + vp_cursor_t cursor; + VALUE_PAIR *vp; (void)fr_pair_cursor_init(&cursor, list); vp = fr_pair_cursor_next_by_da(&cursor, da, tag);