Skip to content

Commit

Permalink
Add fr_pair_add_by_da
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Apr 10, 2018
1 parent dd91460 commit 6d8b28c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/include/pair.h
Expand Up @@ -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);
Expand Down
33 changes: 27 additions & 6 deletions src/include/radiusd.h
Expand Up @@ -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)

Expand Down
30 changes: 28 additions & 2 deletions src/lib/util/pair.c
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 6d8b28c

Please sign in to comment.