Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fr_radius_encode_pair() a one-line wrapper around a dbuff version. #3463

Merged
merged 1 commit into from Jul 15, 2020

Conversation

jejones3141
Copy link
Contributor

No description provided.

@arr2036
Copy link
Member

arr2036 commented May 20, 2020

I'll hold off merging this until the rest of the encoder elements are done. Maybe use this PR to push up commits for each area of the encoder, and then squash it all down at the end.

@jejones3141
Copy link
Contributor Author

jejones3141 commented May 20, 2020 via email

@lgtm-com
Copy link

lgtm-com bot commented May 22, 2020

This pull request introduces 1 alert when merging 58006cb into 12501cf - view on LGTM.com

new alerts:

  • 1 for Multiplication result converted to larger type

@lgtm-com
Copy link

lgtm-com bot commented May 22, 2020

This pull request introduces 1 alert when merging 3f03237 into a02e21b - view on LGTM.com

new alerts:

  • 1 for Multiplication result converted to larger type

@arr2036
Copy link
Member

arr2036 commented May 22, 2020

@jejones3141 looks like you tripped LGTM CI. Can you take a look and see what it's complaining about.

@jejones3141
Copy link
Contributor Author

jejones3141 commented May 22, 2020 via email

fr_da_stack_t *da_stack, unsigned int depth,
fr_cursor_t *cursor, void *encoder_ctx)
{
ssize_t slen;
uint8_t *p = out;
size_t init_remaining = fr_dbuff_remaining(dbuff);
Copy link
Member

@arr2036 arr2036 May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added FR_DBUFF_MAX_NO_ADVANCE. Maybe it'd be better to use that here to initialise a fr_dbuff_t on the stack, then we don't have to initialise a new dbuff for every call for encode_tlv_hdr[_internal].

You can then return at the end with:

return fr_dbuff_advance(dbuff, fr_dbuff_used(tlv))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I'll look for places I can use FR_DBUFF_MAX_NO_ADVANCE to advantage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been 4+ weeks. Can these commits be updated to use the new API?

The idea behind DBUFF was to avoid these manual length calculations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they can, once FR_DBUFF_MAX_NO_ADVANCE() gives you a dbuff with matching start and p.

* Fully migrating away from the original encode_value() will require a revision of
* fr_struct_to_network() and revising the fr_encode_value_t type.
*/
static ssize_t encode_value_dbuff(fr_dbuff_t *dbuff,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think encode_value is used outside of this compilation unit, so there's no reason to wrap it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry; I thought replying to the email would show up here. The ptr/len version of encode_value() has to stay around because it's passed as a parameter to fr_struct_to_network(), which others use (and which, because it takes function pointer parameters, isn't easy to wrap); I wrote the one layer around it rather than doing it in each caller in src/protocols/radius/encode.c.

@jejones3141
Copy link
Contributor Author

jejones3141 commented May 28, 2020 via email

@jejones3141
Copy link
Contributor Author

jejones3141 commented May 28, 2020 via email

@jejones3141 jejones3141 force-pushed the radius_encode_dbuff branch 2 times, most recently from f1587e9 to a71fdd1 Compare June 11, 2020 21:04
FR_DBUFF_RETURN(encode_vsa_hdr, _dbuff, _da_stack, _depth, _cursor, _ctx)
#define ENCODE_TLV_HDR_RETURN(_dbuff, _da_stack, _depth, _cursor, _ctx) \
FR_DBUFF_RETURN(encode_tlv_hdr, _dbuff, _da_stack, _depth, _cursor, _ctx)
#define ENCODE_EXTENDED_HDR_RETURN(_dbuff, _da_stack, _depth, _cursor, _ctx) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these macros are only used once, there's no need to have them as macros.

@@ -303,14 +319,16 @@ static ssize_t encode_tlv_hdr_internal(uint8_t *out, size_t outlen,
vp = fr_cursor_current(cursor);
}

return p - out;
return init_remaining - fr_dbuff_remaining(dbuff);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be return fr_dbuff_used(dbuff)

here, and other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fr_dbuff_used(dbuff) includes bytes already written to dbuff, so just doing that doesn't work. What is needed is no-advance dbuffs having start set to the parent's p (or more accurately the same thing as their p is set to; the FR_DBUFF_NO_ADVANCE_RESERVE() macro has an edge case); actually, I'm not sure why that's not done for all children. Then you can write

fr_dbuff_t work_dbuff = FR_DBUFF_NO_ADVANCE(dbuff);
/* Do what's needed using work_dbuff */
return fr_dbuff_advance(dbuff, fr_dbuff_used(&work_dbuff));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's the pattern which should be used everywhere.

fr_da_stack_t *da_stack, unsigned int depth,
fr_cursor_t *cursor, void *encoder_ctx)
{
ssize_t slen;
ssize_t slen;
uint8_t *hdr_field = dbuff->p;
Copy link
Member

@alandekok alandekok Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a nitpick: we typically would use attr here for radius. We don't generally use field suffixes, as they're pretty much redundant.

Here, and other places in the commit.

@@ -724,7 +772,7 @@ static ssize_t attr_shift(uint8_t const *start, uint8_t const *end,
*/

while (check_len > (255 - hdr_len)) {
total += hdr_len;
new_hdr_count++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why total was changed to new_hdr_count and then a later multiplication. It should just use total as before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; I will change it back.

@@ -735,7 +783,7 @@ static ssize_t attr_shift(uint8_t const *start, uint8_t const *end,
* "encode_value" function to take into account the header
* lengths.
*/
if ((ptr + ptr[1] + total) > end) return (ptr + ptr[1]) - start;
if (fr_dbuff_advance(dbuff, new_hdr_count * hdr_len) < 0) return ptr[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning ptr[1] changes the meaning of this return code. It should return the number of bytes necessary to encode this data.

The previous code returned the total amount of data encoded since start. this change just returns the total amount of data in this particular attribute. So at the minimum, returning ptr[1] is different from what it was before.

@@ -1387,10 +1419,10 @@ ssize_t fr_radius_encode_pair(uint8_t *out, size_t outlen, fr_cursor_t *cursor,
* using a different scheme than the "long
* extended" one.
*/
ret = encode_concat(out, outlen, &da_stack, 0, cursor, encoder_ctx);
ENCODE_CONCAT_RETURN(dbuff, &da_stack, 0, cursor, encoder_ctx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear why macros are useful here.

@jejones3141 jejones3141 force-pushed the radius_encode_dbuff branch 2 times, most recently from d8ea999 to 66725a2 Compare July 6, 2020 21:21
The conversion is not complete; encode_value() must wait until
fr_struct_to_network(), which radius, dhcpv6, and arp use, can be
converted to use dbuff.
@arr2036 arr2036 merged commit c376be5 into FreeRADIUS:master Jul 15, 2020
@jejones3141 jejones3141 deleted the radius_encode_dbuff branch October 5, 2020 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants