]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add iterator for iterating by DA
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 15 Sep 2017 08:22:41 +0000 (15:22 +0700)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 15 Sep 2017 08:22:41 +0000 (15:22 +0700)
src/include/pair.h
src/lib/util/cursor.c
src/lib/util/pair.c

index 10e1e5fdc1dc9445f7c551c2e12413e37a8417b1..7bff1ecf66c5d28cbdebc76955f97b7f13146563 100644 (file)
@@ -26,6 +26,7 @@
 RCSIDH(pair_h, "$Id$")
 
 #include <freeradius-devel/value.h>
+#include <freeradius-devel/cursor.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -200,6 +201,8 @@ int         fr_pair_to_unknown(VALUE_PAIR *vp);
 int            fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value);
 
 /* Searching and list modification */
+VALUE_PAIR     *fr_pair_cursor_init_by_da(fr_cursor_t *cursor, VALUE_PAIR **head, fr_dict_attr_t const *da);
+
 VALUE_PAIR     *fr_pair_find_by_da(VALUE_PAIR *head, fr_dict_attr_t const *da, int8_t tag);
 
 VALUE_PAIR     *fr_pair_find_by_num(VALUE_PAIR *head, unsigned int vendor, unsigned int attr, int8_t tag);
index b50d76f9fc994eca6b836f062ce2698597331446..fb21d1fde1ccbadd2b4b7fd55e0be9efc71abd0d 100644 (file)
@@ -64,7 +64,6 @@ static inline void *cursor_next(void **prev, fr_cursor_t *cursor, void *current)
        }
 
        if (!cursor->iter) {
-
                next = *NEXT_PTR(current);                              /* Fast path without custom iter */
                if (prev) *prev = current;
 
index 594cd2966cd6fccd90fdf44bcdea0a7eafc586e7..79bb5fdf5cac67ab9ba15c7a135957531e89491d 100644 (file)
@@ -607,6 +607,48 @@ int fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value)
        return 0;
 }
 
+/** Iterate over pairs with a specified da
+ *
+ * @param[in,out] prev The VALUE_PAIR before curr. Will be updated to point to the
+ *                     pair before the one returned, or the last pair in the list
+ *                     if no matching pairs found.
+ * @param[in] curr     The VALUE_PAIR after cursor->current.  Will be checked to
+ *                     see if it matches the specified fr_dict_attr_t.
+ * @param[in] ctx      The fr_dict_attr_t to search for.
+ * @return
+ *     - Next matching VALUE_PAIR.
+ *     - NULL if not more matching VALUE_PAIRs could be found.
+ */
+static void *_pair_iter_by_da(void **prev, void *curr, void *ctx)
+{
+       VALUE_PAIR      *c, *p;
+       fr_dict_attr_t  *da = ctx;
+       
+       if (!curr) return NULL;
+       
+       for (p = *prev, c = curr; c; p = c, c = c->next) {
+               VP_VERIFY(c);
+               if (c->da == da) break;
+       }
+       
+       *prev = p;
+       
+       return c;
+}
+
+/** Create a cursor which will only return pairs with the specified fr_dict_attr_t
+ *
+ * @param[in] cursor   Usually an #fr_cursor_t struct in stack memory.
+ * @param[in] head     Pointer to the start of the list.
+ * @param[in] da       We're searching for.
+ * @return
+ *     - The first VALUE_PAIR in the list matching da
+ */
+VALUE_PAIR *fr_pair_cursor_init_by_da(fr_cursor_t *cursor, VALUE_PAIR **head, fr_dict_attr_t const *da)
+{      
+       return fr_cursor_talloc_iter_init(cursor, head, _pair_iter_by_da, da, VALUE_PAIR);
+}
+
 /** Find the pair with the matching DAs
  *
  */