From: Arran Cudbard-Bell Date: Tue, 19 Apr 2022 00:49:20 +0000 (-0500) Subject: Split out network byte order functions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04a52e70d86ea93eac0a1adc04a1675fe3ba1cf4;p=thirdparty%2Ffreeradius-server.git Split out network byte order functions --- diff --git a/src/lib/util/dbuff.h b/src/lib/util/dbuff.h index bd2950d52b8..d64366346a2 100644 --- a/src/lib/util/dbuff.h +++ b/src/lib/util/dbuff.h @@ -32,7 +32,7 @@ extern "C" { #include #include #include -#include +#include #include #include #include @@ -1472,7 +1472,7 @@ static inline ssize_t _fr_dbuff_in_##_type(uint8_t **pos_p, fr_dbuff_t *out, _ty { \ fr_assert(!out->is_const); \ _FR_DBUFF_EXTEND_LOWAT_POS_OR_RETURN(pos_p, out, sizeof(_type##_t)); \ - fr_net_from_##_type((*pos_p), num); \ + fr_nbo_from_##_type((*pos_p), num); \ return _fr_dbuff_set(pos_p, out, (*pos_p) + sizeof(_type##_t)); \ } FR_DBUFF_PARSE_INT_DEF(uint16) @@ -1546,11 +1546,12 @@ static inline ssize_t _fr_dbuff_in_double(uint8_t **pos_p, fr_dbuff_t *out, doub static inline ssize_t _fr_dbuff_in_uint64v(uint8_t **pos_p, fr_dbuff_t *dbuff, uint64_t num) { size_t ret; + uint8_t swapped[sizeof(uint64_t)]; ret = ROUND_UP_DIV((size_t)fr_high_bit_pos(num | 0x08), 8); - num = ntohll(num); + fr_nbo_from_uint64(swapped, num); - return _fr_dbuff_in_memcpy(pos_p, dbuff, ((uint8_t *)&num) + (sizeof(uint64_t) - ret), ret); + return _fr_dbuff_in_memcpy(pos_p, dbuff, (swapped + (sizeof(uint64_t) - ret)), ret); } /** Copy an integer value into a dbuff or marker using our internal variable length encoding @@ -1712,7 +1713,7 @@ static inline ssize_t _fr_dbuff_out_##_type(_type##_t *out, uint8_t **pos_p, fr_ { \ fr_assert(out); \ FR_DBUFF_EXTEND_LOWAT_OR_RETURN(in, sizeof(_type##_t)); \ - *out = fr_net_to_##_type((*pos_p)); \ + *out = fr_nbo_to_##_type((*pos_p)); \ return _fr_dbuff_set(pos_p, in, (*pos_p) + sizeof(_type##_t)); \ } @@ -1769,7 +1770,7 @@ static inline ssize_t _fr_dbuff_out_uint64v(uint64_t *num, uint8_t **pos_p, fr_d slen = _fr_dbuff_out_memcpy(((uint8_t *) num) + (8 - length), pos_p, dbuff, length); if (slen <= 0) return slen; - *num = fr_net_to_uint64((uint8_t const *)num); + *num = fr_nbo_to_uint64((uint8_t const *)num); return length; } @@ -1808,7 +1809,7 @@ static inline ssize_t _fr_dbuff_out_int64v(int64_t *num, uint8_t **pos_p, fr_dbu if (slen <= 0) return slen; if (msb & 0x80) memset(((uint8_t *)num), 0xff, sizeof(*num) - length); - *num = fr_net_to_int64((uint8_t const *)num); + *num = fr_nbo_to_int64((uint8_t const *)num); return length; } diff --git a/src/lib/util/dbuff_tests.c b/src/lib/util/dbuff_tests.c index 1d52876ba8f..2be5da8a028 100644 --- a/src/lib/util/dbuff_tests.c +++ b/src/lib/util/dbuff_tests.c @@ -93,7 +93,7 @@ static void test_dbuff_max(void) * * @note Passing constants to fr_dbuff_in() as it is written results in * warnings about narrowing casts on the constants--but those casts are in - * the underlying inlined fr_net_from*() functions. They have to be there; + * the underlying inlined fr_nbo_from*() functions. They have to be there; * that's how those functions work. (The tests worked despite the warnings.) * Using variables avoids the warnings, at least with the compile options * the build system uses by default. diff --git a/src/lib/util/dict_ext.h b/src/lib/util/dict_ext.h index 1535e2effcb..7ab2e4e7e3b 100644 --- a/src/lib/util/dict_ext.h +++ b/src/lib/util/dict_ext.h @@ -26,6 +26,7 @@ RCSIDH(dict_ext_h, "$Id$") #include #include +#include #include diff --git a/src/lib/util/dict_wasm.mk b/src/lib/util/dict_wasm.mk new file mode 100644 index 00000000000..48f8336d8be --- /dev/null +++ b/src/lib/util/dict_wasm.mk @@ -0,0 +1,11 @@ +TARGET := dict.wasm + +SRC_CC := emcc +SRC_CFLAGS := '' + +SOURCES := dict_ext.c \ + dict_fixup.c \ + dict_print.c \ + dict_tokenize.c \ + dict_unknown.c \ + dict_util.c \ diff --git a/src/lib/util/nbo.h b/src/lib/util/nbo.h new file mode 100644 index 00000000000..83de015e74b --- /dev/null +++ b/src/lib/util/nbo.h @@ -0,0 +1,233 @@ +#pragma once +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** Structures and functions for converting integers to/from network byte order + * + * @file src/lib/util/nbo.h + * + * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org) + * @copyright 2022 Arran Cudbard-Bell (a.cudbardb@freeradius.org) + */ +RCSIDH(nbo_h, "$Id$") + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** Write out an unsigned 16bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_uint16(uint8_t out[static sizeof(uint16_t)], uint16_t num) +{ + out[0] = (num >> 8) & 0xff; + out[1] = num & 0xff; +} + +/** Write out an unsigned 24bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_uint24(uint8_t out[static 3], uint32_t num) +{ + out[0] = (num >> 16) & 0xff; + out[1] = (num >> 8) & 0xff; + out[2] = num & 0xff; +} + +/** Write out an unsigned 32bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num) +{ + fr_nbo_from_uint16(out, (uint16_t) (num >> 16)); + fr_nbo_from_uint16(out + sizeof(uint16_t), (uint16_t) num); +} + +/** Write out an unsigned 64bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_uint64(uint8_t out[static sizeof(uint64_t)], uint64_t num) +{ + fr_nbo_from_uint32(out, (uint32_t)(num >> 32)); + fr_nbo_from_uint32(out + sizeof(uint32_t), (uint32_t)num); +} + +/** Write out an signed 16bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_int16(uint8_t out[static sizeof(int16_t)], int16_t num) +{ + out[0] = (num >> 8) & 0xff; + out[1] = num & 0xff; +} + +/** Write out an signed 32bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_int32(uint8_t out[static sizeof(int32_t)], int32_t num) +{ + fr_nbo_from_uint16(out, (int16_t) (num >> 16)); + fr_nbo_from_uint16(out + sizeof(int16_t), (int16_t) num); +} + +/** Write out an signed 64bit integer in wire format (big endian) + * + * @param[out] out Where to write the integer. + * @param[in] num to encode. + */ +static inline void fr_nbo_from_int64(uint8_t out[static sizeof(uint64_t)], uint64_t num) +{ + fr_nbo_from_uint32(out, (int32_t)(num >> 32)); + fr_nbo_from_uint32(out + sizeof(int32_t), (int32_t)num); +} + +/** Write out an unsigned 64bit integer in wire format using the fewest bytes possible + * + * @param[out] out Where to write big endian encoding of num. + * Should be at least 8 bytes. + * @param[in] num Number to encode. + * @return the number of bytes written to out. + */ +static inline size_t fr_nbo_from_uint64v(uint8_t out[static sizeof(uint64_t)], uint64_t num) +{ + size_t ret; + uint8_t swapped[sizeof(uint64_t)]; + + ret = ROUND_UP_DIV((size_t)fr_high_bit_pos(num | 0x80), 8); + + fr_nbo_from_uint64(swapped, num); + memcpy(out, (swapped + (sizeof(uint64_t) - ret)), ret); /* aligned */ + + return ret; +} + +/** Read an unsigned 16bit integer from wire format (big endian) + * + * @param[in] data To convert to a 16bit unsigned integer of native endianness. + * @return a 16 bit unsigned integer of native endianness. + */ +static inline uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)]) +{ + return (((uint16_t)data[0]) << 8) | data[1]; +} + +/** Read an unsigned 24bit integer from wire format (big endian) + * + * @param[in] data To convert to a 24bit unsigned integer of native endianness. + * @return a 24 bit unsigned integer of native endianness. + */ +static inline uint32_t fr_nbo_to_uint24(uint8_t const data[static 3]) +{ + return (((uint32_t)data[0]) << 16) | (((uint32_t)data[1]) << 8) | data[2]; +} + +/** Read an unsigned 32bit integer from wire format (big endian) + * + * @param[in] data To convert to a 32bit unsigned integer of native endianness. + * @return a 32 bit unsigned integer of native endianness. + */ +static inline uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)]) +{ + return ((uint32_t)fr_nbo_to_uint16(data) << 16) | fr_nbo_to_uint16(data + sizeof(uint16_t)); +} + +/** Read an unsigned 64bit integer from wire format (big endian) + * + * @param[in] data To convert to a 64bit unsigned integer of native endianness. + * @return a 64 bit unsigned integer of native endianness. + */ +static inline uint64_t fr_nbo_to_uint64(uint8_t const data[static sizeof(uint64_t)]) +{ + return ((uint64_t)fr_nbo_to_uint32(data) << 32) | fr_nbo_to_uint32(data + sizeof(uint32_t)); +} + +/* + * To get signed integers, simply cast. + */ +#define fr_nbo_to_int16(_x) ((int16_t) fr_nbo_to_uint16(_x)) +#define fr_nbo_to_int32(_x) ((int32_t) fr_nbo_to_uint32(_x)) +#define fr_nbo_to_int64(_x) ((int64_t) fr_nbo_to_uint64(_x)) + + +/** Read an unsigned 64bit integer from wire format (big endian) with a variable length encoding + * + * @param[in] data Buffer containing the number. + * @param[in] data_len Length of number. + * @return a 64 bit unsigned integer of native endianness. + */ +static inline uint64_t fr_nbo_to_uint64v(uint8_t const *data, size_t data_len) +{ + uint64_t num = 0; + uint64_t nbo; + + if (unlikely(data_len > sizeof(uint64_t))) return 0; + + /* + * Copy at an offset into memory + * allocated for the uin64_t + */ + memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */ + fr_nbo_from_uint64((uint8_t *)&nbo, num); + + return nbo; +} + +static inline uint64_t fr_nbo_to_int64v(uint8_t const *data, size_t data_len) +{ + int64_t num = 0; + uint64_t nbo; + + if (unlikely(data_len > sizeof(uint64_t))) return 0; + + /* + * Copy at an offset into memory + * allocated for the uin64_t + */ + memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */ + if (*data & 0x80) memset(((uint8_t *)&num) + data_len, 0xff, sizeof(num) - data_len); + + fr_nbo_from_uint64((uint8_t *)&nbo, num); + + return nbo; +} + +/** Convert bits (as in prefix length) to bytes, rounding up. + * + * @param bits number of bits in the prefix + * @return number of bytes taken to store the prefix + */ +static inline unsigned int fr_bytes_from_bits(unsigned int bits) +{ + return (bits + 7) >> 3; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/lib/util/net.h b/src/lib/util/net.h index 5de3433507f..899696e4002 100644 --- a/src/lib/util/net.h +++ b/src/lib/util/net.h @@ -155,197 +155,6 @@ int fr_udp_header_check(uint8_t const *data, uint16_t remaining, ip_header_t co uint16_t fr_ip_header_checksum(uint8_t const *data, uint8_t ihl); uint16_t fr_ip6_pesudo_header_checksum(struct in6_addr const *src, struct in6_addr const *dst, uint16_t ip_len, uint8_t ip_next); -/** Write out an unsigned 16bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_uint16(uint8_t out[static sizeof(uint16_t)], uint16_t num) -{ - out[0] = (num >> 8) & 0xff; - out[1] = num & 0xff; -} - -/** Write out an unsigned 24bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_uint24(uint8_t out[static 3], uint32_t num) -{ - out[0] = (num >> 16) & 0xff; - out[1] = (num >> 8) & 0xff; - out[2] = num & 0xff; -} - -/** Write out an unsigned 32bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num) -{ - fr_net_from_uint16(out, (uint16_t) (num >> 16)); - fr_net_from_uint16(out + sizeof(uint16_t), (uint16_t) num); -} - -/** Write out an unsigned 64bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_uint64(uint8_t out[static sizeof(uint64_t)], uint64_t num) -{ - fr_net_from_uint32(out, (uint32_t)(num >> 32)); - fr_net_from_uint32(out + sizeof(uint32_t), (uint32_t)num); -} - -/** Write out an signed 16bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_int16(uint8_t out[static sizeof(int16_t)], int16_t num) -{ - out[0] = (num >> 8) & 0xff; - out[1] = num & 0xff; -} - -/** Write out an signed 32bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_int32(uint8_t out[static sizeof(int32_t)], int32_t num) -{ - fr_net_from_uint16(out, (int16_t) (num >> 16)); - fr_net_from_uint16(out + sizeof(int16_t), (int16_t) num); -} - -/** Write out an signed 64bit integer in wire format (big endian) - * - * @param[out] out Where to write the integer. - * @param[in] num to encode. - */ -static inline void fr_net_from_int64(uint8_t out[static sizeof(uint64_t)], uint64_t num) -{ - fr_net_from_uint32(out, (int32_t)(num >> 32)); - fr_net_from_uint32(out + sizeof(int32_t), (int32_t)num); -} - -/** Write out an unsigned 64bit integer in wire format using the fewest bytes possible - * - * @param[out] out Where to write big endian encoding of num. - * Should be at least 8 bytes. - * @param[in] num Number to encode. - * @return the number of bytes written to out. - */ -static inline size_t fr_net_from_uint64v(uint8_t out[static sizeof(uint64_t)], uint64_t num) -{ - size_t ret; - - ret = ROUND_UP_DIV((size_t)fr_high_bit_pos(num | 0x80), 8); - num = ntohll(num); - memcpy(out, ((uint8_t *)&num) + (sizeof(uint64_t) - ret), ret); /* aligned */ - - return ret; -} - -/** Read an unsigned 16bit integer from wire format (big endian) - * - * @param[in] data To convert to a 16bit unsigned integer of native endianness. - * @return a 16 bit unsigned integer of native endianness. - */ -static inline uint16_t fr_net_to_uint16(uint8_t const data[static sizeof(uint16_t)]) -{ - return (((uint16_t)data[0]) << 8) | data[1]; -} - -/** Read an unsigned 24bit integer from wire format (big endian) - * - * @param[in] data To convert to a 24bit unsigned integer of native endianness. - * @return a 24 bit unsigned integer of native endianness. - */ -static inline uint32_t fr_net_to_uint24(uint8_t const data[static 3]) -{ - return (((uint32_t)data[0]) << 16) | (((uint32_t)data[1]) << 8) | data[2]; -} - -/** Read an unsigned 32bit integer from wire format (big endian) - * - * @param[in] data To convert to a 32bit unsigned integer of native endianness. - * @return a 32 bit unsigned integer of native endianness. - */ -static inline uint32_t fr_net_to_uint32(uint8_t const data[static sizeof(uint32_t)]) -{ - return ((uint32_t)fr_net_to_uint16(data) << 16) | fr_net_to_uint16(data + sizeof(uint16_t)); -} - -/** Read an unsigned 64bit integer from wire format (big endian) - * - * @param[in] data To convert to a 64bit unsigned integer of native endianness. - * @return a 64 bit unsigned integer of native endianness. - */ -static inline uint64_t fr_net_to_uint64(uint8_t const data[static sizeof(uint64_t)]) -{ - return ((uint64_t)fr_net_to_uint32(data) << 32) | fr_net_to_uint32(data + sizeof(uint32_t)); -} - -/* - * To get signed integers, simply cast. - */ -#define fr_net_to_int16(_x) ((int16_t) fr_net_to_uint16(_x)) -#define fr_net_to_int32(_x) ((int32_t) fr_net_to_uint32(_x)) -#define fr_net_to_int64(_x) ((int64_t) fr_net_to_uint64(_x)) - - -/** Read an unsigned 64bit integer from wire format (big endian) with a variable length encoding - * - * @param[in] data Buffer containing the number. - * @param[in] data_len Length of number. - * @return a 64 bit unsigned integer of native endianness. - */ -static inline uint64_t fr_net_to_uint64v(uint8_t const *data, size_t data_len) -{ - uint64_t num = 0; - - if (unlikely(data_len > sizeof(uint64_t))) return 0; - - /* - * Copy at an offset into memory - * allocated for the uin64_t - */ - memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */ - return ntohll(num); -} - -static inline uint64_t fr_net_to_int64v(uint8_t const *data, size_t data_len) -{ - int64_t num = 0; - - if (unlikely(data_len > sizeof(uint64_t))) return 0; - - /* - * Copy at an offset into memory - * allocated for the uin64_t - */ - memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */ - - if (*data & 0x80) memset(((uint8_t *)&num) + data_len, 0xff, sizeof(num) - data_len); - - return ntohll(num); -} - -/** Convert bits (as in prefix length) to bytes, rounding up. - * - * @param bits number of bits in the prefix - * @return number of bytes taken to store the prefix - */ -static inline unsigned int fr_bytes_from_bits(unsigned int bits) -{ - return (bits + 7) >> 3; -} - #ifdef __cplusplus } #endif diff --git a/src/lib/util/value.c b/src/lib/util/value.c index c567c639650..988b7be3d59 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -2575,7 +2575,7 @@ static inline int fr_value_box_cast_to_ethernet(TALLOC_CTX *ctx, fr_value_box_t case FR_TYPE_UINT64: { uint8_t array[8]; - fr_net_from_uint64(array, src->vb_uint64); + fr_nbo_from_uint64(array, src->vb_uint64); /* * For OUIs in the DB. @@ -2929,7 +2929,7 @@ static inline int fr_value_box_cast_to_integer(TALLOC_CTX *ctx, fr_value_box_t * } fr_value_box_init(dst, dst_type, dst_enumv, src->tainted); - dst->vb_uint64 = fr_net_to_uint64(&src->vb_ifid[0]); + dst->vb_uint64 = fr_nbo_to_uint64(&src->vb_ifid[0]); return 0; } diff --git a/src/listen/dns/proto_dns_udp.c b/src/listen/dns/proto_dns_udp.c index 13c75758e13..f5ed51c6566 100644 --- a/src/listen/dns/proto_dns_udp.c +++ b/src/listen/dns/proto_dns_udp.c @@ -172,7 +172,7 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time * proto_dns sets the priority */ - xid = fr_net_to_uint16(buffer); + xid = fr_nbo_to_uint16(buffer); /* * Print out what we received. diff --git a/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c b/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c index 1f9b3d2aa0a..3c33cb4b198 100644 --- a/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c +++ b/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c @@ -441,9 +441,9 @@ ssize_t eap_fast_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_ uint16_t len; fr_pair_t *vp; - attr = fr_net_to_uint16(p) & EAP_FAST_TLV_TYPE; + attr = fr_nbo_to_uint16(p) & EAP_FAST_TLV_TYPE; p += 2; - len = fr_net_to_uint16(p); + len = fr_nbo_to_uint16(p); p += 2; da = fr_dict_attr_child_by_num(parent, attr); diff --git a/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c b/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c index 3a4eba53601..cf5e9ed1742 100644 --- a/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c +++ b/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c @@ -175,13 +175,13 @@ static ssize_t eap_ttls_decode_pair(request_t *request, TALLOC_CTX *ctx, fr_dcur RDEBUG3("%04zu %02x%02x%02x%02x %02x%02x%02x%02x ...", p - data, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); - attr = fr_net_to_uint32(p); + attr = fr_nbo_to_uint32(p); p += 4; flags = p[0]; p++; - value_len = fr_net_to_uint64v(p, 3); /* Yes, that is a 24 bit length field */ + value_len = fr_nbo_to_uint64v(p, 3); /* Yes, that is a 24 bit length field */ p += 3; if (value_len < 8) { @@ -207,7 +207,7 @@ static ssize_t eap_ttls_decode_pair(request_t *request, TALLOC_CTX *ctx, fr_dcur * Do we have a vendor field? */ if (flags & FR_DIAMETER_AVP_FLAG_VENDOR) { - vendor = fr_net_to_uint32(p); + vendor = fr_nbo_to_uint32(p); p += 4; value_len -= 4; /* -= 4 for the vendor ID field */ diff --git a/src/modules/rlm_icmp/rlm_icmp.c b/src/modules/rlm_icmp/rlm_icmp.c index 16e6cbece67..f293a52a7c7 100644 --- a/src/modules/rlm_icmp/rlm_icmp.c +++ b/src/modules/rlm_icmp/rlm_icmp.c @@ -93,7 +93,7 @@ static uint16_t icmp_checksum(uint8_t *data, size_t data_len, uint16_t checksum) p = data; end = data + data_len; while (p < end) { - sum += fr_net_to_uint16(p); /* type / code */ + sum += fr_nbo_to_uint16(p); /* type / code */ p += 2; } diff --git a/src/protocols/arp/arp.h b/src/protocols/arp/arp.h index 7651dffbd54..0f92d81d016 100644 --- a/src/protocols/arp/arp.h +++ b/src/protocols/arp/arp.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff --git a/src/protocols/dhcpv4/decode.c b/src/protocols/dhcpv4/decode.c index 2de6cccb275..04dee0b1507 100644 --- a/src/protocols/dhcpv4/decode.c +++ b/src/protocols/dhcpv4/decode.c @@ -181,8 +181,8 @@ static ssize_t decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t if (data_len != 8) goto raw; - ipaddr = fr_net_to_uint32(p); - mask = fr_net_to_uint32(p + 4); + ipaddr = fr_nbo_to_uint32(p); + mask = fr_nbo_to_uint32(p + 4); p += 8; /* @@ -236,14 +236,14 @@ static ssize_t decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t mask <<= (32 - vp->vp_ip.prefix); if (*p > 24) { - ipaddr = fr_net_to_uint32(p + 1); + ipaddr = fr_nbo_to_uint32(p + 1); } else if (*p > 16) { - ipaddr = fr_net_to_uint24(p + 1); + ipaddr = fr_nbo_to_uint24(p + 1); ipaddr <<= 8; } else if (*p > 8) { - ipaddr = fr_net_to_uint16(p + 1); + ipaddr = fr_nbo_to_uint16(p + 1); ipaddr <<= 16; } else { /* 1..8 */ @@ -668,7 +668,7 @@ next: return data_len + 2; /* decoded the whole thing */ } - pen = fr_net_to_uint32(p); + pen = fr_nbo_to_uint32(p); /* * Verify that the parent (which should be a VSA) diff --git a/src/protocols/dhcpv6/base.c b/src/protocols/dhcpv6/base.c index 4a9fe6b9e28..1d9f7fcb739 100644 --- a/src/protocols/dhcpv6/base.c +++ b/src/protocols/dhcpv6/base.c @@ -352,7 +352,7 @@ static bool verify_to_client(uint8_t const *packet, size_t packet_len, fr_dhcpv6 switch (packet[0]) { case FR_PACKET_TYPE_VALUE_ADVERTISE: - transaction_id = fr_net_to_uint24(&packet[1]); + transaction_id = fr_nbo_to_uint24(&packet[1]); if (transaction_id != packet_ctx->transaction_id) { fail_tid: fr_strerror_const("Transaction ID does not match"); @@ -390,7 +390,7 @@ static bool verify_to_client(uint8_t const *packet, size_t packet_len, fr_dhcpv6 return true; case FR_PACKET_TYPE_VALUE_REPLY: - transaction_id = fr_net_to_uint24(&packet[1]); + transaction_id = fr_nbo_to_uint24(&packet[1]); if (transaction_id != packet_ctx->transaction_id) goto fail_tid; if (!fr_dhcpv6_option_find(options, end, FR_SERVER_ID)) goto fail_sid; @@ -449,7 +449,7 @@ static bool verify_to_client(uint8_t const *packet, size_t packet_len, fr_dhcpv6 return verify_to_client(option + 4, DHCPV6_GET_OPTION_LEN(option), packet_ctx); case FR_DHCPV6_LEASE_QUERY_REPLY: - transaction_id = fr_net_to_uint24(&packet[1]); + transaction_id = fr_nbo_to_uint24(&packet[1]); if (transaction_id != packet_ctx->transaction_id) goto fail_tid; if (!fr_dhcpv6_option_find(options, end, FR_SERVER_ID)) goto fail_sid; @@ -840,7 +840,7 @@ ssize_t fr_dhcpv6_encode(fr_dbuff_t *dbuff, uint8_t const *original, size_t leng FR_DBUFF_IN_MEMCPY_RETURN(&frame_dbuff, vp->vp_octets, DHCPV6_TRANSACTION_ID_LEN); } else { uint8_t id[DHCPV6_TRANSACTION_ID_LEN]; - fr_net_from_uint24(id, fr_rand()); + fr_nbo_from_uint24(id, fr_rand()); FR_DBUFF_IN_MEMCPY_RETURN(&frame_dbuff, id, sizeof(id)); /* Need 24 bits of the 32bit integer */ } break; @@ -945,9 +945,9 @@ static void dhcpv6_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len, break; } - length = fr_net_to_uint16(option + 2); + length = fr_nbo_to_uint16(option + 2); fprintf(fp, "%.*s", depth + 1, tabs); - fprintf(fp, "%04x %04x\t", fr_net_to_uint16(option), length); + fprintf(fp, "%04x %04x\t", fr_nbo_to_uint16(option), length); if ((option + 4 + length) > end) { print_hex_data(fp, option + 4, end - (option + 4), depth + 3); diff --git a/src/protocols/dhcpv6/decode.c b/src/protocols/dhcpv6/decode.c index 6c8e96fb0c8..5308bdd201f 100644 --- a/src/protocols/dhcpv6/decode.c +++ b/src/protocols/dhcpv6/decode.c @@ -332,7 +332,7 @@ static ssize_t decode_array(TALLOC_CTX *ctx, fr_pair_list_t *out, while (p < end) { if ((end - p) < 2) goto raw; - element_len = fr_net_to_uint16(p); + element_len = fr_nbo_to_uint16(p); if ((size_t) (end - p) < (((size_t) element_len) + 2)) goto raw; diff --git a/src/protocols/dhcpv6/dhcpv6.h b/src/protocols/dhcpv6/dhcpv6.h index 3d500ced124..a85fbb19d2d 100644 --- a/src/protocols/dhcpv6/dhcpv6.h +++ b/src/protocols/dhcpv6/dhcpv6.h @@ -46,8 +46,8 @@ extern size_t const fr_dhcpv6_attr_sizes[FR_TYPE_MAX + 1][2]; #define DHCPV6_RELAY_HDR_LEN (DHCPV6_MSG_TYPE_LEN + DHCPV6_HOP_COUNT_LEN + DHCPV6_LINK_ADDRESS_LEN + DHCPV6_PEER_ADDRESS_LEN) #define DHCPV6_OPT_HDR_LEN (sizeof(uint16_t) * 2) -#define DHCPV6_GET_OPTION_NUM(_x) fr_net_to_uint16(_x) -#define DHCPV6_GET_OPTION_LEN(_x) fr_net_to_uint16((_x) + 2) +#define DHCPV6_GET_OPTION_NUM(_x) fr_nbo_to_uint16(_x) +#define DHCPV6_GET_OPTION_LEN(_x) fr_nbo_to_uint16((_x) + 2) #define DHCPV6_MAX_RELAY_NESTING 10 diff --git a/src/protocols/dns/base.c b/src/protocols/dns/base.c index ee1d54767af..b25722d2084 100644 --- a/src/protocols/dns/base.c +++ b/src/protocols/dns/base.c @@ -85,7 +85,7 @@ static bool fr_dns_tlv_ok(uint8_t const *p, uint8_t const *end, fr_dns_decode_fa return false; } - len = fr_net_to_uint16(p + 2); + len = fr_nbo_to_uint16(p + 2); if ((p + 4 + len) > end) { DECODE_FAIL(TLV_OVERFLOWS_RR); return false; @@ -120,7 +120,7 @@ bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_d return false; } - qdcount = fr_net_to_uint16(packet + 4); + qdcount = fr_nbo_to_uint16(packet + 4); if (query) { /* @@ -135,11 +135,11 @@ bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_d DECODE_FAIL(NO_QUESTIONS); return false; } - if (fr_net_to_uint16(packet + 6) != 0) { + if (fr_nbo_to_uint16(packet + 6) != 0) { DECODE_FAIL(NS_IN_QUESTION); return false; } - if (fr_net_to_uint16(packet + 8) != 0) { + if (fr_nbo_to_uint16(packet + 8) != 0) { DECODE_FAIL(ANSWERS_IN_QUESTION); return false; } @@ -152,7 +152,7 @@ bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_d */ } - expected = fr_net_to_uint16(packet + 4) + fr_net_to_uint16(packet + 6) + fr_net_to_uint16(packet + 8) + fr_net_to_uint16(packet + 10); + expected = fr_nbo_to_uint16(packet + 4) + fr_nbo_to_uint16(packet + 6) + fr_nbo_to_uint16(packet + 8) + fr_nbo_to_uint16(packet + 10); count = 0; p = packet + DNS_HDR_LEN; @@ -329,7 +329,7 @@ bool fr_dns_packet_ok(uint8_t const *packet, size_t packet_len, bool query, fr_d return false; } - len = fr_net_to_uint16(p); + len = fr_nbo_to_uint16(p); if (len == 0) { DECODE_FAIL(ZERO_RR_LEN); return false; diff --git a/src/protocols/dns/decode.c b/src/protocols/dns/decode.c index 45d55d4c42d..f69b1135cef 100644 --- a/src/protocols/dns/decode.c +++ b/src/protocols/dns/decode.c @@ -274,7 +274,7 @@ static ssize_t decode_array(TALLOC_CTX *ctx, fr_pair_list_t *out, break; } - element_len = fr_net_to_uint16(p); + element_len = fr_nbo_to_uint16(p); if ((p + 2 + element_len) > end) { goto raw; } @@ -365,8 +365,8 @@ static ssize_t decode_dns_labels(TALLOC_CTX *ctx, fr_pair_list_t *out, } -#define DNS_GET_OPTION_NUM(_x) fr_net_to_uint16(_x) -#define DNS_GET_OPTION_LEN(_x) fr_net_to_uint16((_x) + 2) +#define DNS_GET_OPTION_NUM(_x) fr_nbo_to_uint16(_x) +#define DNS_GET_OPTION_LEN(_x) fr_nbo_to_uint16((_x) + 2) static ssize_t decode_option(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, @@ -483,7 +483,7 @@ static ssize_t decode_record(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_ int i, count; uint8_t const *p = rr; - count = fr_net_to_uint16(counter); + count = fr_nbo_to_uint16(counter); FR_PROTO_TRACE("Decoding %u of %s", count, attr->name); for (i = 0; i < count; i++) { ssize_t slen; diff --git a/src/protocols/dns/encode.c b/src/protocols/dns/encode.c index 7f97b01841b..2e5c962163c 100644 --- a/src/protocols/dns/encode.c +++ b/src/protocols/dns/encode.c @@ -597,7 +597,7 @@ static ssize_t encode_record(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, fr_pair if (!vp) break; } - fr_net_from_uint16(counter, count); + fr_nbo_from_uint16(counter, count); FR_PROTO_TRACE(" %s encoded %d records", attr->name, count); return fr_dbuff_set(dbuff, &work_dbuff); diff --git a/src/protocols/internal/encode.c b/src/protocols/internal/encode.c index a17041493c8..e42e502007f 100644 --- a/src/protocols/internal/encode.c +++ b/src/protocols/internal/encode.c @@ -231,7 +231,7 @@ static ssize_t internal_encode(fr_dbuff_t *dbuff, * the function. */ vlen = fr_dbuff_used(&value_dbuff); - flen = (ssize_t) fr_net_from_uint64v(buff, vlen); + flen = (ssize_t) fr_nbo_from_uint64v(buff, vlen); /* * Ugh, it's a long one, need to move the data. diff --git a/src/protocols/tftp/decode.c b/src/protocols/tftp/decode.c index 5987bb6aa8d..40be8293b6a 100644 --- a/src/protocols/tftp/decode.c +++ b/src/protocols/tftp/decode.c @@ -89,7 +89,7 @@ int fr_tftp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, si end = (data + data_len); /* Opcode */ - opcode = fr_net_to_uint16(p); + opcode = fr_nbo_to_uint16(p); vp = fr_pair_afrom_da(ctx, attr_tftp_opcode); if (!vp) goto error; @@ -202,7 +202,7 @@ int fr_tftp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, si vp = fr_pair_afrom_da(ctx, attr_tftp_block); if (!vp) goto error; - vp->vp_uint16 = fr_net_to_uint16(p); + vp->vp_uint16 = fr_nbo_to_uint16(p); fr_pair_append(out, vp); @@ -244,7 +244,7 @@ int fr_tftp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, si vp = fr_pair_afrom_da(ctx, attr_tftp_error_code); if (!vp) goto error; - vp->vp_uint16 = fr_net_to_uint16(p); + vp->vp_uint16 = fr_nbo_to_uint16(p); fr_pair_append(out, vp);