ldns documentation

- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

Data Structures | Typedefs | Functions
radix.h File Reference

Radix tree. More...

Go to the source code of this file.

## Data Structures
struct | ldns_radix_array_t
| Radix node select edge array. More...
struct | ldns_radix_node_t
| A node in a radix tree. More...
struct | ldns_radix_t
| An entire radix tree. More...

## Typedefs
typedef uint16_t | radix_strlen_t
typedef struct ldns_radix_array_t | ldns_radix_array_t
typedef struct ldns_radix_node_t | ldns_radix_node_t
typedef struct ldns_radix_t | ldns_radix_t

## Functions
ldns_radix_t * | ldns_radix_create (void)
| Create a new radix tree. More...
void | ldns_radix_init (ldns_radix_t *tree)
| Initialize radix tree. More...
void | ldns_radix_free (ldns_radix_t *tree)
| Free the radix tree. More...
ldns_status | ldns_radix_insert (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len, void *data)
| Insert data into the tree. More...
void * | ldns_radix_delete (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len)
| Delete data from the tree. More...
ldns_radix_node_t * | ldns_radix_search (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len)
| Search data in the tree. More...
int | ldns_radix_find_less_equal (ldns_radix_t *tree, uint8_t *key, radix_strlen_t len, ldns_radix_node_t **result)
| Search data in the tree, and if not found, find the closest smaller element in the tree. More...
ldns_radix_node_t * | ldns_radix_first (ldns_radix_t *tree)
| Get the first element in the tree. More...
ldns_radix_node_t * | ldns_radix_last (ldns_radix_t *tree)
| Get the last element in the tree. More...
ldns_radix_node_t * | ldns_radix_next (ldns_radix_node_t *node)
| Next element. More...
ldns_radix_node_t * | ldns_radix_prev (ldns_radix_node_t *node)
| Previous element. More...
ldns_status | ldns_radix_split (ldns_radix_t *tree1, size_t num, ldns_radix_t **tree2)
| Split radix tree intwo. More...
ldns_status | ldns_radix_join (ldns_radix_t *tree1, ldns_radix_t *tree2)
| Join two radix trees. More...
void | ldns_radix_traverse_postorder (ldns_radix_node_t *node, void(*func)(ldns_radix_node_t *, void *), void *arg)
| Call function for all nodes in the tree, such that leaf nodes are called before parent nodes. More...
void | ldns_radix_printf (FILE *fd, ldns_radix_t *tree)
| Print radix tree (for debugging purposes). More...

## Detailed Description

Radix tree.

Implementation taken from NSD 4, adjusted for use in ldns.

Definition in file radix.h.

## Typedef Documentation

typedef uint16_t radix_strlen_t

Definition at line 52 of file radix.h.

typedef struct ldns_radix_array_t ldns_radix_array_t

Definition at line 53 of file radix.h.

typedef struct ldns_radix_node_t ldns_radix_node_t

Definition at line 54 of file radix.h.

typedef struct ldns_radix_t ldns_radix_t

Definition at line 55 of file radix.h.

## Function Documentation

ldns_radix_t* ldns_radix_create | ( | void |  | ) |

Create a new radix tree.

Returns
  : new radix tree.

Allocate memory for it

Initialize it

Definition at line 114 of file radix.c.

References LDNS_MALLOC, and ldns_radix_init().

void ldns_radix_init | ( | ldns_radix_t * | tree | ) |

Initialize radix tree.

Parameters

  tree | uninitialized radix tree.

Initialize it

Definition at line 134 of file radix.c.

References ldns_radix_t::count, and ldns_radix_t::root.

void ldns_radix_free | ( | ldns_radix_t * | tree | ) |

Free the radix tree.

Parameters

  tree | radix tree.

Free the radix tree.

Definition at line 150 of file radix.c.

References LDNS_FREE, ldns_radix_traverse_postorder(), and ldns_radix_t::root.

ldns_status ldns_radix_insert | ( | ldns_radix_t * | tree,
|  | uint8_t * | key,
|  | radix_strlen_t | len,
|  | void * | data
| ) |  |

Insert data into the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.
  data | data.

Returns
  : status.

Search the trie until we can make no further process.

No prefix found

Example 1: The root: | [0]

Example 2: 'dns': | [0] –| [d+ns] dns

Find some space in the array for the first byte

Set relational pointers

Store the remainder of the prefix

Exact match found

Prefix found

Find some space in the array for the byte.

Example 3: 'ldns' | [0] –| [d+ns] dns –| [l+dns] ldns

Create remainder of the string.

Add new node.

Use existing element.

Example 4: 'edns' | [0] –| [d+ns] dns –| [e+dns] edns –| [l+dns] ldns

Create remainder of the string.

Add new node.

Use existing element, but it has a shared prefix, we need a split.

Definition at line 168 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_t::count, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_node_t::key, ldns_radix_node_t::klen, LDNS_FREE, LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_MEM_ERR, LDNS_STATUS_NULL, LDNS_STATUS_OK, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_node_t::parent, ldns_radix_node_t::parent_index, ldns_radix_t::root, and ldns_radix_array_t::str.

void* ldns_radix_delete | ( | ldns_radix_t * | tree,
|  | uint8_t * | key,
|  | radix_strlen_t | len
| ) |  |

Delete data from the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.

Returns
  : unlinked data or NULL if not present.

Definition at line 314 of file radix.c.

References ldns_radix_t::count, ldns_radix_node_t::data, and ldns_radix_search().

ldns_radix_node_t* ldns_radix_search | ( | ldns_radix_t * | tree,
|  | uint8_t * | key,
|  | radix_strlen_t | len
| ) |  |

Search data in the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.

Returns
  : the radix node or NULL if not found.

Must match additional string.

Definition at line 334 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_t::root, and ldns_radix_array_t::str.

int ldns_radix_find_less_equal | ( | ldns_radix_t * | tree,
|  | uint8_t * | key,
|  | radix_strlen_t | len,
|  | ldns_radix_node_t ** | result
| ) |  |

Search data in the tree, and if not found, find the closest smaller element in the tree.

Parameters

  tree | tree to insert to.
  key | key.
  len | length of key.
  result | the radix node with the exact or closest match. NULL if the key is smaller than the smallest key in the tree.

Returns
  1 if exact match, 0 otherwise.

No exact match. The lesser is in this or the previous node.

No exact match. The lesser is in this node or the last of this array, or something before this node.

No exact match. Find the previous in the array from this index.

Must match additional string.

Additional string is longer than key.

Key is before this node.

Key is after additional string.

Exact match.

There is a node which is an exact match, but has no element.

Definition at line 380 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_prev(), ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_t::root, and ldns_radix_array_t::str.

ldns_radix_node_t* ldns_radix_first | ( | ldns_radix_t * | tree | ) |

Get the first element in the tree.

Parameters

  tree | tree.

Returns
  : the radix node with the first element.

Definition at line 480 of file radix.c.

References ldns_radix_node_t::data, ldns_radix_next(), and ldns_radix_t::root.

ldns_radix_node_t* ldns_radix_last | ( | ldns_radix_t * | tree | ) |

Get the last element in the tree.

Parameters

  tree | tree.

Returns
  : the radix node with the last element.

Definition at line 499 of file radix.c.

References ldns_radix_t::root.

ldns_radix_node_t* ldns_radix_next | ( | ldns_radix_node_t * | node | ) |

Next element.

Parameters

  node | node.

Returns
  : node with next element.

Go down: most-left child is the next.

No elements in subtree, get to parent and go down next branch.

Node itself.

Dive into subtree.

Definition at line 513 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_node_t::len, ldns_radix_node_t::parent, and ldns_radix_node_t::parent_index.

ldns_radix_node_t* ldns_radix_prev | ( | ldns_radix_node_t * | node | ) |

Previous element.

Parameters

  node | node.

Returns
  : node with previous element.

Get to parent and go down previous branch.

Definition at line 554 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::parent, ldns_radix_node_t::parent_index, and ldns_radix_array_t::str.

ldns_status ldns_radix_split | ( | ldns_radix_t * | tree1,
|  | size_t | num,
|  | ldns_radix_t ** | tree2
| ) |  |

Split radix tree intwo.

Parameters

  tree1 | one tree.
  num | number of elements to split off.
  tree2 | another tree.

Returns
  : status.

Split radix tree intwo.

Delete current node from tree1.

Insert current node into tree2/

Update count; get first element from tree1 again.

Definition at line 682 of file radix.c.

References ldns_radix_node_t::data, ldns_radix_node_t::key, ldns_radix_node_t::klen, ldns_radix_create(), ldns_radix_delete(), ldns_radix_first(), ldns_radix_insert(), ldns_radix_next(), LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_MEM_ERR, LDNS_STATUS_NO_DATA, LDNS_STATUS_NULL, LDNS_STATUS_OK, and ldns_radix_t::root.

ldns_status ldns_radix_join | ( | ldns_radix_t * | tree1,
|  | ldns_radix_t * | tree2
| ) |  |

Join two radix trees.

Parameters

  tree1 | one tree.
  tree2 | another tree.

Returns
  : status.

Add all elements from tree2 into tree1.

Insert current node into tree1

Exist errors may occur

Definition at line 643 of file radix.c.

References ldns_radix_node_t::data, ldns_radix_node_t::key, ldns_radix_node_t::klen, ldns_radix_delete(), ldns_radix_first(), ldns_radix_insert(), ldns_radix_next(), LDNS_STATUS_EXISTS_ERR, LDNS_STATUS_NO_DATA, LDNS_STATUS_OK, and ldns_radix_t::root.

void ldns_radix_traverse_postorder | ( | ldns_radix_node_t * | node,
|  | void(*)(ldns_radix_node_t *, void *) | func,
|  | void * | arg
| ) |  |

Call function for all nodes in the tree, such that leaf nodes are called before parent nodes.

Parameters

  node | start node.
  func | function.
  arg | user argument.

Call user function

Definition at line 740 of file radix.c.

References ldns_radix_node_t::array, ldns_radix_node_t::capacity, ldns_radix_node_t::data, ldns_radix_array_t::edge, ldns_radix_node_t::key, ldns_radix_node_t::klen, LDNS_FREE, LDNS_MALLOC, ldns_radix_prev(), ldns_radix_traverse_postorder(), LDNS_XMALLOC, ldns_radix_array_t::len, ldns_radix_node_t::len, ldns_radix_node_t::offset, ldns_radix_node_t::parent, ldns_radix_node_t::parent_index, ldns_radix_t::root, and ldns_radix_array_t::str.

void ldns_radix_printf | ( | FILE * | fd,
|  | ldns_radix_t * | tree
| ) |  |

Print radix tree (for debugging purposes).

Parameters

  fd | file descriptor.
  tree | tree.

Print radix tree (for debugging purposes).

Definition at line 624 of file radix.c.

References ldns_radix_t::root.