From: Alan T. DeKok Date: Thu, 25 Jan 2024 16:49:40 +0000 (-0500) Subject: add encode / decode callbacks for dictionaries X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5f8724f7ff28206fc9a582a0999bec66849bd73;p=thirdparty%2Ffreeradius-server.git add encode / decode callbacks for dictionaries with forward definitions for fr_pair_list_t, and fr_dbuff_t, so that we don't have circular references --- diff --git a/src/lib/util/dict.h b/src/lib/util/dict.h index 84f5986b5e3..f7d933fad30 100644 --- a/src/lib/util/dict.h +++ b/src/lib/util/dict.h @@ -288,6 +288,45 @@ typedef enum { typedef bool (*fr_dict_attr_valid_func_t)(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, fr_dict_attr_flags_t *flags); +/* + * Forward declarations to avoid circular references. + */ +typedef struct pair_list_s fr_pair_list_t; +typedef struct fr_dbuff_s fr_dbuff_t; + +/** A generic interface for decoding packets to fr_pair_ts + * + * A decoding function should decode a single top level packet from wire format. + * + * Note that unlike #fr_tp_proto_decode_t, this function is NOT passed an encode_ctx. That is because when we + * do cross-protocol encoding, the "outer" protocol has no information it can share with the "inner" protocol. + * + * @param[in] ctx to allocate new pairs in. + * @param[in] vps where new VPs will be added + * @param[in] data to decode. + * @param[in] data_len The length of the incoming data. + * @return + * - <= 0 on error. May be the offset (as a negative value) where the error occurred. + * - > 0 on success. How many bytes were decoded. + */ +typedef ssize_t (*fr_dict_attr_decode_func_t)(TALLOC_CTX *ctx, fr_pair_list_t *vps, + uint8_t const *data, size_t data_len); + +/** A generic interface for encoding fr_pair_ts to packets + * + * An encoding function should encode multiple VPs to a wire format packet + * + * Note that unlike #fr_tp_proto_encode_t, this function is NOT passed an encode_ctx. That is because when we + * do cross-protocol encoding, the "outer" protocol has no information it can share with the "inner" protocol. + * + * @param[in] vps vps to encode + * @param[in] dbuff buffer where data can be written + * @return + * - <= 0 on error. May be the offset (as a negative value) where the error occurred. + * - > 0 on success. How many bytes were encoded + */ +typedef ssize_t(*fr_dict_attr_encode_func_t)(fr_dbuff_t *dbuff, fr_pair_list_t *vps); + /** Protocol-specific callbacks in libfreeradius-PROTOCOL * */ @@ -298,6 +337,8 @@ typedef struct { fr_table_num_ordered_t const *subtype_table; //!< for "encrypt=1", etc. size_t subtype_table_len; //!< length of subtype_table fr_dict_attr_valid_func_t attr_valid; //!< validation function for new attributes + fr_dict_attr_decode_func_t decode; //!< for decoding attributes + fr_dict_attr_encode_func_t encode; //!< for encoding attributes } fr_dict_protocol_t; typedef struct fr_dict_gctx_s fr_dict_gctx_t; diff --git a/src/lib/util/pair.h b/src/lib/util/pair.h index 5e7fc7b66c3..84e96db4bfa 100644 --- a/src/lib/util/pair.h +++ b/src/lib/util/pair.h @@ -49,7 +49,7 @@ typedef struct value_pair_s fr_pair_t; FR_TLIST_TYPES(fr_pair_order_list) -typedef struct { +typedef struct pair_list_s { FR_TLIST_HEAD(fr_pair_order_list) order; //!< Maintains the relative order of pairs in a list. bool _CONST is_child; //!< is a child of a VP