2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 of the
4 * License as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * @brief Functions to iterate over collections of VALUE_PAIRs
22 * @note Do not modify collections of VALUE_PAIRs pointed to be a cursor
23 * with none fr_cursor_* functions, during the lifetime of that cursor.
25 * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26 * @copyright 2013-2015 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
27 * @copyright 2013-2015 The FreeRADIUS Server Project.
30 #include <freeradius-devel/libradius.h>
32 /** Internal function to update cursor state
34 * @param cursor to operate on.
35 * @param vp to set current and found positions to.
36 * @return value passed in as vp.
38 inline static VALUE_PAIR *fr_cursor_update(vp_cursor_t *cursor, VALUE_PAIR *vp)
42 cursor->current = NULL;
47 cursor->next = vp->next;
54 /** Setup a cursor to iterate over attribute pairs
56 * @param cursor Where to initialise the cursor (uses existing structure).
57 * @param const_vp to start from.
58 * @return the attribute pointed to by vp.
60 VALUE_PAIR *fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR * const *const_vp)
64 if (!const_vp || !cursor) {
68 memset(cursor, 0, sizeof(*cursor));
70 memcpy(&vp, &const_vp, sizeof(vp)); /* stupid const hacks */
73 * Useful check to see if uninitialised memory is pointed
77 if (*vp) VERIFY_VP(*vp);
79 memcpy(&cursor->first, &vp, sizeof(cursor->first));
80 cursor->current = *cursor->first;
82 if (cursor->current) {
83 VERIFY_VP(cursor->current);
84 cursor->next = cursor->current->next;
87 return cursor->current;
92 * @param in Cursor to copy.
93 * @param out Where to copy the cursor to.
95 void fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in)
97 memcpy(out, in, sizeof(*out));
100 /** Rewind cursor to the start of the list
102 * @param cursor to operate on.
103 * @return the VALUE_PAIR at the start of the list.
105 VALUE_PAIR *fr_cursor_first(vp_cursor_t *cursor)
107 if (!cursor->first) return NULL;
109 cursor->current = *cursor->first;
111 if (cursor->current) {
112 VERIFY_VP(cursor->current);
113 cursor->next = cursor->current->next;
114 if (cursor->next) VERIFY_VP(cursor->next);
115 cursor->found = NULL;
118 return cursor->current;
121 /** Wind cursor to the last pair in the list
123 * @param cursor to operate on.
124 * @return the VALUE_PAIR at the end of the list.
126 VALUE_PAIR *fr_cursor_last(vp_cursor_t *cursor)
128 if (!cursor->first || !*cursor->first) return NULL;
130 /* Need to start at the start */
131 if (!cursor->current) fr_cursor_first(cursor);
133 /* Wind to the end */
134 while (cursor->next) fr_cursor_next(cursor);
136 return cursor->current;
139 /** Iterate over a collection of VALUE_PAIRs of a given type in the pairlist
141 * Find the next attribute of a given type. If no fr_cursor_next_by_* function
142 * has been called on a cursor before, or the previous call returned
143 * NULL, the search will start with the current attribute. Subsequent calls to
144 * fr_cursor_next_by_* functions will start the search from the previously
147 * @param cursor to operate on.
148 * @param attr number to match.
149 * @param vendor number to match (0 for none vendor attribute).
150 * @param tag to match. Either a tag number or TAG_ANY to match any tagged or
151 * untagged attribute, TAG_NONE to match attributes without tags.
152 * @return the next matching VALUE_PAIR, or NULL if no VALUE_PAIRs match.
154 VALUE_PAIR *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag)
158 if (!cursor->first) return NULL;
160 for (i = !cursor->found ? cursor->current : cursor->found->next;
164 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
165 (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
170 return fr_cursor_update(cursor, i);
173 /** Iterate over attributes of a given DA in the pairlist
175 * Find the next attribute of a given type. If no fr_cursor_next_by_* function
176 * has been called on a cursor before, or the previous call returned
177 * NULL, the search will start with the current attribute. Subsequent calls to
178 * fr_cursor_next_by_* functions will start the search from the previously
181 * @note DICT_ATTR pointers are compared, not the attribute numbers and vendors.
183 * @param cursor to operate on.
184 * @param da to match.
185 * @param tag to match. Either a tag number or TAG_ANY to match any tagged or
186 * untagged attribute, TAG_NONE to match attributes without tags.
187 * @return the next matching VALUE_PAIR, or NULL if no VALUE_PAIRs match.
189 VALUE_PAIR *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag)
193 if (!cursor->first) return NULL;
195 for (i = !cursor->found ? cursor->current : cursor->found->next;
200 (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
205 return fr_cursor_update(cursor, i);
208 /** Advanced the cursor to the next VALUE_PAIR
210 * @param cursor to operate on.
211 * @return the next VALUE_PAIR, or NULL if no more VALUE_PAIRS in the collection.
213 VALUE_PAIR *fr_cursor_next(vp_cursor_t *cursor)
215 if (!cursor->first) return NULL;
217 cursor->current = cursor->next;
218 if (cursor->current) {
219 VERIFY_VP(cursor->current);
222 * Set this now in case 'current' gets freed before
223 * fr_cursor_next is called again.
225 cursor->next = cursor->current->next;
228 * Next call to fr_cursor_next_by_num will start from the current
229 * position in the list, not the last found instance.
231 cursor->found = NULL;
234 return cursor->current;
237 /** Return the next VALUE_PAIR without advancing the cursor
239 * @param cursor to operate on.
240 * @return the next VALUE_PAIR, or NULL if no more VALUE_PAIRS in the collection.
242 VALUE_PAIR *fr_cursor_next_peek(vp_cursor_t *cursor)
247 /** Return the VALUE_PAIR the cursor current points to
249 * @param cursor to operate on.
250 * @return the VALUE_PAIR the cursor currently points to.
252 VALUE_PAIR *fr_cursor_current(vp_cursor_t *cursor)
254 if (cursor->current) VERIFY_VP(cursor->current);
256 return cursor->current;
259 /** Insert a single VALUE_PAIR at the end of the list
261 * @note Will not advance cursor position to new attribute, but will set cursor
262 * to this attribute, if it's the first one in the list.
264 * Insert a VALUE_PAIR at the end of the list.
266 * @param cursor to operate on.
267 * @param vp to insert.
269 void fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp)
273 if (!fr_assert(cursor->first)) return; /* cursor must have been initialised */
280 * Only allow one VP to by inserted at a time
285 * Cursor was initialised with a pointer to a NULL value_pair
287 if (!*cursor->first) {
289 cursor->current = vp;
295 * We don't yet know where the last VALUE_PAIR is
297 * Assume current is closer to the end of the list and
298 * use that if available.
300 if (!cursor->last) cursor->last = cursor->current ? cursor->current : *cursor->first;
302 VERIFY_VP(cursor->last);
305 * Wind last to the end of the list.
307 if (cursor->last->next) {
308 for (i = cursor->last; i; i = i->next) {
315 * Either current was never set, or something iterated to the
316 * end of the attribute list. In both cases the newly inserted
317 * VALUE_PAIR should be set as the current VALUE_PAIR.
319 if (!cursor->current) cursor->current = vp;
322 * Add the VALUE_PAIR to the end of the list
324 cursor->last->next = vp;
325 cursor->last = vp; /* Wind it forward a little more */
328 * If the next pointer was NULL, and the VALUE_PAIR
329 * just added has a next pointer value, set the cursor's next
330 * pointer to the VALUE_PAIR's next pointer.
332 if (!cursor->next) cursor->next = cursor->current->next;
335 /** Merges multiple VALUE_PAIR into the cursor
337 * Add multiple VALUE_PAIR from add to cursor.
339 * @param cursor to insert VALUE_PAIRs with
340 * @param add one or more VALUE_PAIRs (may be NULL, which results in noop).
342 void fr_cursor_merge(vp_cursor_t *cursor, VALUE_PAIR *add)
349 if (!fr_assert(cursor->first)) return; /* cursor must have been initialised */
351 for (vp = fr_cursor_init(&from, &add);
353 vp = fr_cursor_next(&from)) {
354 fr_cursor_insert(cursor, vp);
358 /** Remove the current pair
360 * @todo this is really inefficient and should be fixed...
362 * The current VP will be set to the one before the VP being removed,
363 * this is so the commonly used check and remove loop (below) works
366 for (vp = fr_cursor_init(&cursor, head);
368 vp = fr_cursor_next(&cursor) {
370 vp = fr_cursor_remove(&cursor);
376 * @param cursor to remove the current pair from.
377 * @return NULL on error, else the VALUE_PAIR that was just removed.
379 VALUE_PAIR *fr_cursor_remove(vp_cursor_t *cursor)
381 VALUE_PAIR *vp, *before;
383 if (!fr_assert(cursor->first)) return NULL; /* cursor must have been initialised */
385 vp = cursor->current;
386 if (!vp) return NULL;
389 * Where VP is head of the list
391 if (*(cursor->first) == vp) {
392 *(cursor->first) = vp->next;
393 cursor->current = vp->next;
394 cursor->next = vp->next ? vp->next->next : NULL;
399 * Where VP is not head of the list
401 before = *(cursor->first);
402 if (!before) return NULL;
405 * Find the VP immediately preceding the one being removed
407 while (before->next != vp) before = before->next;
409 cursor->next = before->next = vp->next; /* close the gap */
410 cursor->current = before; /* current jumps back one, but this is usually desirable */
413 vp->next = NULL; /* limit scope of fr_pair_list_free() */
416 * Fixup cursor->found if we removed the VP it was referring to
418 if (vp == cursor->found) cursor->found = cursor->current;
421 * Fixup cursor->last if we removed the VP it was referring to
423 if (vp == cursor->last) cursor->last = cursor->current;
427 /** Replace the current pair
429 * @todo this is really inefficient and should be fixed...
431 * @param cursor to replace the current pair in.
432 * @param new VALUE_PAIR to insert.
433 * @return NULL on error, else the VALUE_PAIR we just replaced.
435 VALUE_PAIR *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new)
437 VALUE_PAIR *vp, **last;
439 if (!fr_assert(cursor->first)) return NULL; /* cursor must have been initialised */
441 vp = cursor->current;
443 *cursor->first = new;
447 last = cursor->first;
448 while (*last != vp) {
449 last = &(*last)->next;
452 fr_cursor_next(cursor); /* Advance the cursor past the one were about to replace */
455 new->next = vp->next;