Skip to content

Commit c473880

Browse files
authored
Merge pull request #72 from douzzer/20250902-wolfsentry_inetx_ntoa
20250902-wolfsentry_inetx_ntoa
2 parents b8024ea + 81e97bd commit c473880

File tree

2 files changed

+169
-30
lines changed

2 files changed

+169
-30
lines changed

src/routes.c

Lines changed: 165 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,63 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_table_iterate_end(
37043704
WOLFSENTRY_RETURN_OK;
37053705
}
37063706

3707+
static int ws_decimal_ntoa(int d, char *buf, int *buflen) {
3708+
if (d >= 1000) {
3709+
WOLFSENTRY_ERROR_RETURN(NUMERIC_ARG_TOO_BIG);
3710+
}
3711+
else if (d >= 100) {
3712+
if (*buflen < 3)
3713+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3714+
*buf++ = (char)('0' + (d/100));
3715+
d %= 100;
3716+
*buf++ = (char)('0' + (d/10));
3717+
d %= 10;
3718+
*buf = (char)('0' + d);
3719+
*buflen = 3;
3720+
WOLFSENTRY_RETURN_OK;
3721+
}
3722+
else if (d >= 10) {
3723+
if (*buflen < 2)
3724+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3725+
*buf++ = (char)('0' + (d/10));
3726+
d %= 10;
3727+
*buf = (char)('0' + d);
3728+
*buflen = 2;
3729+
WOLFSENTRY_RETURN_OK;
3730+
}
3731+
else {
3732+
if (*buflen < 1)
3733+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3734+
*buf = (char)('0' + d);
3735+
*buflen = 1;
3736+
WOLFSENTRY_RETURN_OK;
3737+
}
3738+
}
3739+
3740+
WOLFSENTRY_API int wolfsentry_inet4_ntoa(const byte *addr, unsigned int addr_bits, char *buf, int *buflen) {
3741+
const int b = (int)WOLFSENTRY_BITS_TO_BYTES(addr_bits);
3742+
int i;
3743+
const char *start_buf = buf;
3744+
3745+
for (i=0; i<4; ++i) {
3746+
int ret;
3747+
int buflen2 = *buflen;
3748+
ret = ws_decimal_ntoa(b > i ? (int)addr[i] : 0, buf, &buflen2);
3749+
WOLFSENTRY_RERETURN_IF_ERROR(ret);
3750+
buf += buflen2;
3751+
*buflen -= buflen2;
3752+
if (i < 3) {
3753+
if (*buflen < 2)
3754+
WOLFSENTRY_RETURN_OK;
3755+
*buf++ = '.';
3756+
--*buflen;
3757+
}
3758+
}
3759+
3760+
*buflen = (int)(buf - start_buf);
3761+
WOLFSENTRY_RETURN_OK;
3762+
}
3763+
37073764
static inline char hexdigit_ntoa(unsigned int d) {
37083765
d &= 0xf;
37093766
if (d < 10)
@@ -3712,6 +3769,102 @@ static inline char hexdigit_ntoa(unsigned int d) {
37123769
return (char)('a' + (d - 0xa));
37133770
}
37143771

3772+
WOLFSENTRY_API int wolfsentry_inet6_ntoa(const byte *addr, unsigned int addr_bits, char *buf, int *buflen) {
3773+
const int b = (int)WOLFSENTRY_BITS_TO_BYTES(addr_bits);
3774+
int i;
3775+
const char *start_buf = buf;
3776+
int this_zerospan_length = 0;
3777+
int this_zerospan_offset;
3778+
int longest_zerospan_length = 0;
3779+
int longest_zerospan_offset = 0;
3780+
3781+
for (i=0; i<16; i += 2) {
3782+
unsigned int octet0 = b > i ? addr[i] : 0;
3783+
unsigned int octet1 = b > i + 1 ? addr[i+1] : 0;
3784+
if ((octet0 == 0) && (octet1 == 0)) {
3785+
if (this_zerospan_length == 0) {
3786+
this_zerospan_length = 2;
3787+
this_zerospan_offset = i;
3788+
}
3789+
else
3790+
this_zerospan_length += 2;
3791+
}
3792+
else if (this_zerospan_length > 0) {
3793+
if (longest_zerospan_length < this_zerospan_length) {
3794+
longest_zerospan_length = this_zerospan_length;
3795+
longest_zerospan_offset = this_zerospan_offset;
3796+
}
3797+
this_zerospan_length = 0;
3798+
}
3799+
}
3800+
3801+
if (longest_zerospan_length < this_zerospan_length) {
3802+
longest_zerospan_length = this_zerospan_length;
3803+
longest_zerospan_offset = this_zerospan_offset;
3804+
}
3805+
3806+
if (longest_zerospan_length < 4)
3807+
longest_zerospan_offset = -1;
3808+
3809+
for (i=0; i<16; i += 2) {
3810+
unsigned int octet0 = b > i ? addr[i] : 0;
3811+
unsigned int octet1 = b > i + 1 ? addr[i+1] : 0;
3812+
if (i == longest_zerospan_offset) {
3813+
if (i == 0) {
3814+
if (*buflen < 2)
3815+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3816+
*buf++ = ':';
3817+
--*buflen;
3818+
}
3819+
if (*buflen < 1)
3820+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3821+
*buf++ = ':';
3822+
--*buflen;
3823+
i += longest_zerospan_length - 2;
3824+
continue;
3825+
}
3826+
if (octet0 >> 4) {
3827+
if (*buflen < 4)
3828+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3829+
*buf++ = hexdigit_ntoa(octet0 >> 4);
3830+
*buf++ = hexdigit_ntoa(octet0);
3831+
*buf++ = hexdigit_ntoa(octet1 >> 4);
3832+
*buf++ = hexdigit_ntoa(octet1);
3833+
*buflen -= 4;
3834+
}
3835+
else if (octet0) {
3836+
if (*buflen < 3)
3837+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3838+
*buf++ = hexdigit_ntoa(octet0);
3839+
*buf++ = hexdigit_ntoa(octet1 >> 4);
3840+
*buf++ = hexdigit_ntoa(octet1);
3841+
*buflen -= 3;
3842+
}
3843+
else if (octet1 >> 4) {
3844+
if (*buflen < 2)
3845+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3846+
*buf++ = hexdigit_ntoa(octet1 >> 4);
3847+
*buf++ = hexdigit_ntoa(octet1);
3848+
*buflen -= 2;
3849+
}
3850+
else {
3851+
if (*buflen < 1)
3852+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3853+
*buf++ = hexdigit_ntoa(octet1);
3854+
*buflen -= 1;
3855+
}
3856+
if (i < 14) {
3857+
if (*buflen < 1)
3858+
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
3859+
*buf++ = ':';
3860+
--*buflen;
3861+
}
3862+
}
3863+
3864+
*buflen = (int)(buf - start_buf);
3865+
WOLFSENTRY_RETURN_OK;
3866+
}
3867+
37153868
WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_format_address(
37163869
WOLFSENTRY_CONTEXT_ARGS_IN,
37173870
wolfsentry_addr_family_t sa_family,
@@ -3749,21 +3902,9 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_format_address(
37493902
*buflen = (int)(buf - buf_at_start);
37503903
WOLFSENTRY_RETURN_OK;
37513904
} else if (sa_family == WOLFSENTRY_AF_INET) {
3752-
byte addr_buf[sizeof(struct in_addr)];
3753-
memset(addr_buf, 0, sizeof addr_buf);
3754-
memcpy(addr_buf, addr, WOLFSENTRY_BITS_TO_BYTES(addr_bits));
3755-
if (inet_ntop(AF_INET, addr_buf, buf, (socklen_t)*buflen) == NULL)
3756-
WOLFSENTRY_ERROR_RETURN(SYS_OP_FAILED);
3757-
*buflen = (int)strlen(buf);
3758-
WOLFSENTRY_RETURN_OK;
3905+
WOLFSENTRY_ERROR_RERETURN(wolfsentry_inet4_ntoa(addr, addr_bits, buf, buflen));
37593906
} else if (sa_family == WOLFSENTRY_AF_INET6) {
3760-
byte addr_buf[sizeof(struct in6_addr)];
3761-
memset(addr_buf, 0, sizeof addr_buf);
3762-
memcpy(addr_buf, addr, WOLFSENTRY_BITS_TO_BYTES(addr_bits));
3763-
if (inet_ntop(AF_INET6, addr_buf, buf, (socklen_t)*buflen) == NULL)
3764-
WOLFSENTRY_ERROR_RETURN(SYS_OP_FAILED);
3765-
*buflen = (int)strlen(buf);
3766-
WOLFSENTRY_RETURN_OK;
3907+
WOLFSENTRY_ERROR_RERETURN(wolfsentry_inet6_ntoa(addr, addr_bits, buf, buflen));
37673908
} else if (sa_family == WOLFSENTRY_AF_LOCAL) {
37683909
if (WOLFSENTRY_BITS_TO_BYTES(addr_bits) >= (size_t)*buflen)
37693910
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
@@ -4281,24 +4422,18 @@ static wolfsentry_errcode_t wolfsentry_route_render_address(WOLFSENTRY_CONTEXT_A
42814422
WOLFSENTRY_ERROR_RETURN(IO_FAILED);
42824423
}
42834424
} else if (sa_family == WOLFSENTRY_AF_INET) {
4284-
byte addr_buf[4];
4285-
if (addr_bytes > sizeof addr_buf)
4286-
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
4287-
memset(addr_buf, 0, sizeof addr_buf);
4288-
memcpy(addr_buf, addr, addr_bytes);
4289-
if (inet_ntop(AF_INET, addr_buf, fmt_buf, sizeof fmt_buf) == NULL)
4290-
WOLFSENTRY_ERROR_RETURN(SYS_OP_FAILED);
4291-
if (fprintf(f, "%s/%u", fmt_buf, addr_bits) < 0)
4425+
int fmt_buf_len = (int)sizeof(fmt_buf);
4426+
int ret = wolfsentry_inet4_ntoa(addr, addr_bits, fmt_buf, &fmt_buf_len);
4427+
WOLFSENTRY_RERETURN_IF_ERROR(ret);
4428+
if (fprintf(f, "%.*s/%u", fmt_buf_len, fmt_buf, addr_bits) < 0)
42924429
WOLFSENTRY_ERROR_RETURN(IO_FAILED);
42934430
} else if (sa_family == WOLFSENTRY_AF_INET6) {
4294-
byte addr_buf[16];
4295-
if (addr_bytes > sizeof addr_buf)
4296-
WOLFSENTRY_ERROR_RETURN(BUFFER_TOO_SMALL);
4297-
memset(addr_buf, 0, sizeof addr_buf);
4298-
memcpy(addr_buf, addr, addr_bytes);
4299-
if (inet_ntop(AF_INET6, addr_buf, fmt_buf, sizeof fmt_buf) == NULL)
4300-
WOLFSENTRY_ERROR_RETURN(SYS_OP_FAILED);
4301-
if (fprintf(f, "[%s]/%u", fmt_buf, addr_bits) < 0)
4431+
int fmt_buf_len = (int)sizeof(fmt_buf);
4432+
int ret = wolfsentry_inet6_ntoa(addr, addr_bits, fmt_buf, &fmt_buf_len);
4433+
WOLFSENTRY_RERETURN_IF_ERROR(ret);
4434+
if (fprintf(f, "%.*s/%u", fmt_buf_len, fmt_buf, addr_bits) < 0)
4435+
WOLFSENTRY_ERROR_RETURN(IO_FAILED);
4436+
if (fprintf(f, "[%.*s]/%u", fmt_buf_len, fmt_buf, addr_bits) < 0)
43024437
WOLFSENTRY_ERROR_RETURN(IO_FAILED);
43034438
} else if (sa_family == WOLFSENTRY_AF_LOCAL) {
43044439
if (fprintf(f, "\"%.*s\"", (int)addr_bytes, addr) < 0)

wolfsentry/wolfsentry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,10 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_user_values_iterate_end(
33373337
struct wolfsentry_cursor **cursor);
33383338
/*!< \brief End an iteration loop started with wolfsentry_user_values_iterate_start(). Caller must have a lock on the context at entry. */
33393339

3340+
WOLFSENTRY_API int wolfsentry_inet4_ntoa(const byte *addr, unsigned int addr_bits, char *buf, int *buflen);
3341+
3342+
WOLFSENTRY_API int wolfsentry_inet6_ntoa(const byte *addr, unsigned int addr_bits, char *buf, int *buflen);
3343+
33403344
#define WOLFSENTRY_BASE64_DECODED_BUFSPC(buf, len) \
33413345
(((((len)+3)/4)*3) - ((len) > 1 ? \
33423346
((buf)[(len)-1] == '=') : \

0 commit comments

Comments
 (0)