Skip to content

Commit

Permalink
tweak encode IP prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Feb 25, 2025
1 parent 3ef0e32 commit 7fc7a86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/protocols/der/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ static ssize_t fr_der_encode_ipv4_prefix(fr_dbuff_t *dbuff, fr_dcursor_t *cursor
* octets" that contain the octets of the bit string. (For IP
* addresses, the encoding of the length will be just the length.)
*/
if (vp->vp_ip.prefix == 32) {
FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv4addr, sizeof(vp->vp_ipv4addr));
return fr_dbuff_set(dbuff, &our_dbuff);
}

FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) (8 - (vp->vp_ip.prefix & 0x07)));

len = (vp->vp_ip.prefix + 0x07) >> 3;
Expand Down Expand Up @@ -479,6 +485,12 @@ static ssize_t fr_der_encode_ipv6_prefix(fr_dbuff_t *dbuff, fr_dcursor_t *cursor
* addresses, the encoding of the length will be just the length.)
*/

if (vp->vp_ip.prefix == 128) {
FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) 0x00);
FR_DBUFF_IN_MEMCPY_RETURN(&our_dbuff, (uint8_t const *) &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
return fr_dbuff_set(dbuff, &our_dbuff);
}

FR_DBUFF_IN_RETURN(&our_dbuff, (uint8_t) (8 - (vp->vp_ip.prefix & 0x07)));

len = (vp->vp_ip.prefix + 0x07) >> 3;
Expand Down
8 changes: 7 additions & 1 deletion src/tests/unit/protocols/der/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ proto-dictionary der
encode-pair Test-IPv4-Prefix = 10.5.0.0/23
match 03 04 01 0a 05 00

encode-pair Test-IPv4-Prefix = 10.5.0.4/32
match 03 05 00 0a 05 00 04

#
# Just the length byte
#
Expand All @@ -900,8 +903,11 @@ proto-dictionary-root Test-IPv4-Prefix
decode-pair 03 04 01 0a 05 00
match Test-IPv4-Prefix = 10.5.0.0/23

decode-pair 03 05 00 0a 05 00 04
match Test-IPv4-Prefix = 10.5.0.4/32

decode-pair 03 01 00
match Test-IPv4-Prefix = 0.0.0.0/0

count
match 545
match 549

0 comments on commit 7fc7a86

Please sign in to comment.