So first off you're writing an integer value to a string attribute, which is wrong. The only reason why the server isn't SEGVing is because the length of the VP has been left at zero, so the RADIUS encoder doesn't bother dereferencing the char *
inside the pair that's meant to contain the pair's value.
fr_pair_make
is the easier function to use here, as it takes both the attribute name and value as strings, so you don't need to worry about the C types.
The code snippet below should do what you want.
void test_avp(REQUEST *request)
{
VALUE_PAIR *vp = NULL;
vp = fr_pair_make(request->reply, &request->reply->vps, "Reply-Message", "Hello from FreeRADIUS", T_OP_SET);
if (vp)
{
log("Created VALUE_PAIR");
}
else
{
log("Failed to create VALUE_PAIR");
}
}
For a bit more of an explanation, lets look at the doxygen header:
/** Create a VALUE_PAIR from ASCII strings
*
* Converts an attribute string identifier (with an optional tag qualifier)
* and value string into a VALUE_PAIR.
*
* The string value is parsed according to the type of VALUE_PAIR being created.
*
* @param[in] ctx for talloc
* @param[in] vps list where the attribute will be added (optional)
* @param[in] attribute name.
* @param[in] value attribute value (may be NULL if value will be set later).
* @param[in] op to assign to new VALUE_PAIR.
* @return a new VALUE_PAIR.
*/
VALUE_PAIR *fr_pair_make(TALLOC_CTX *ctx, VALUE_PAIR **vps,
char const *attribute, char const *value, FR_TOKEN op)
- ctx - This is the packet or request that the vps will belong to. If you're adding attributes to the request it should be
request->packet
, reply would be request->reply
, control would be request
.
- vps - If specified, this will be which list to insert the new VP into. If this is NULL
fr_pair_make
will just return the pair and let you insert it into a list.
- attribute - The name of the attribute as a string.
- value - The value of the attribute as a string. For non-string types,
fr_pair_make
will attempt to perform a conversion. So, for example, passing "12345" for an integer type, will result in the integer value 12345 being written to an int
field in the attribute.
- op - You'll usually want to us
T_OP_SET
which means overwrite existing instances of the same attribute. See the T_OP_*
values of FR_TOKEN
and the code that uses them, if you want to understand the different operators and what they do.