From: James Jones Date: Thu, 23 Apr 2020 18:11:24 +0000 (-0500) Subject: Move EAP-AKA to fr_cursor_filter*() and away from {is, first, next}_encodable().... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66bf0156bcbd8901a5cd12e47e5c3b5bf1562867;p=thirdparty%2Ffreeradius-server.git Move EAP-AKA to fr_cursor_filter*() and away from {is, first, next}_encodable(). (#3365) --- diff --git a/src/lib/eap_aka_sim/encode.c b/src/lib/eap_aka_sim/encode.c index 64968cfdb3e..fc483f45cf4 100644 --- a/src/lib/eap_aka_sim/encode.c +++ b/src/lib/eap_aka_sim/encode.c @@ -58,8 +58,18 @@ static ssize_t encode_tlv_hdr(uint8_t *out, size_t outlen, fr_da_stack_t *da_stack, unsigned int depth, fr_cursor_t *cursor, void *encoder_ctx); -static inline bool is_encodable(fr_dict_attr_t const *root, VALUE_PAIR *vp) +/** Evaluation function for EAP-AKA-encodability + * + * @param item pointer to a VALUE_PAIR + * @param uctx context + * + * @return true if the underlying VALUE_PAIR is EAP_AKA encodable, false otherwise + */ +static bool is_eap_aka_encodable(void *item, void *uctx) { + VALUE_PAIR *vp = item; + fr_aka_sim_encode_ctx_t *packet_ctx = uctx; + if (!vp) return false; if (vp->da->flags.internal) return false; /* @@ -67,43 +77,11 @@ static inline bool is_encodable(fr_dict_attr_t const *root, VALUE_PAIR *vp) * and absence is 'false' */ if ((vp->da->type == FR_TYPE_BOOL) && (vp->vp_bool == false)) return false; - if (!fr_dict_parent_common(root, vp->da, true)) return false; + if (!fr_dict_parent_common(packet_ctx->root, vp->da, true)) return false; return true; } -/** Find the next attribute to encode - * - * @param cursor to iterate over. - * @param encoder_ctx the context for the encoder - * @return encodable VALUE_PAIR, or NULL if none available. - */ -static inline VALUE_PAIR *next_encodable(fr_cursor_t *cursor, void *encoder_ctx) -{ - VALUE_PAIR *vp; - fr_aka_sim_encode_ctx_t *packet_ctx = encoder_ctx; - - while ((vp = fr_cursor_next(cursor))) if (is_encodable(packet_ctx->root, vp)) break; - return fr_cursor_current(cursor); -} - -/** Determine if the current attribute is encodable, or find the first one that is - * - * @param cursor to iterate over. - * @param encoder_ctx the context for the encoder - * @return encodable VALUE_PAIR, or NULL if none available. - */ -static inline VALUE_PAIR *first_encodable(fr_cursor_t *cursor, void *encoder_ctx) -{ - VALUE_PAIR *vp; - fr_aka_sim_encode_ctx_t *packet_ctx = encoder_ctx; - - vp = fr_cursor_current(cursor); - if (is_encodable(packet_ctx->root, vp)) return vp; - - return next_encodable(cursor, encoder_ctx); -} - /** Add an IV to a packet * @verbatim @@ -582,7 +560,7 @@ done: /* * Rebuilds the TLV stack for encoding the next attribute */ - vp = next_encodable(cursor, encoder_ctx); + vp = fr_cursor_filter_next(cursor, is_eap_aka_encodable, encoder_ctx); fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL); return len; @@ -890,7 +868,7 @@ ssize_t fr_aka_sim_encode_pair(uint8_t *out, size_t outlen, fr_cursor_t *cursor, CHECK_FREESPACE(outlen, 4); /* Attributes lengths are always multiples of 4 */ - vp = first_encodable(cursor, encoder_ctx); + vp = fr_cursor_filter_current(cursor, is_eap_aka_encodable, encoder_ctx); if (!vp) return 0; VP_VERIFY(vp); @@ -902,7 +880,7 @@ ssize_t fr_aka_sim_encode_pair(uint8_t *out, size_t outlen, fr_cursor_t *cursor, } if (vp->da->attr == FR_MAC) { - next_encodable(cursor, encoder_ctx); + fr_cursor_filter_next(cursor, is_eap_aka_encodable, encoder_ctx); return 0; } /* @@ -998,7 +976,7 @@ ssize_t fr_aka_sim_encode(REQUEST *request, VALUE_PAIR *to_encode, void *encode_ /* * Fast path, we just need to encode a subtype */ - if (!do_hmac && !first_encodable(&cursor, packet_ctx)) { + if (!do_hmac && !fr_cursor_filter_current(&cursor, is_eap_aka_encodable, packet_ctx)) { MEM(buff = talloc_array(eap_packet, uint8_t, 3)); buff[0] = subtype; /* SIM or AKA subtype */ diff --git a/src/lib/io/test_point.h b/src/lib/io/test_point.h index 0dd75f6eb33..18c24ec7162 100644 --- a/src/lib/io/test_point.h +++ b/src/lib/io/test_point.h @@ -73,6 +73,8 @@ typedef struct { typedef struct { fr_test_point_ctx_alloc_t test_ctx; //!< Allocate a test ctx for the encoder. fr_tp_proto_encode_t func; //!< Encoder for proto layer. + fr_cursor_eval_t eval; //!< Evaluation function to filter + ///< attributes to encode. } fr_test_point_proto_encode_t; /** Entry point for pair decoders @@ -91,4 +93,6 @@ typedef struct { fr_pair_encode_t func; //!< Encoder for pairs. fr_cursor_iter_t next_encodable; //!< Iterator to use to select attributes ///< to encode. + fr_cursor_eval_t eval; //!< Evaluation function to filter + ///< attributes to encode. } fr_test_point_pair_encode_t;