/** * Match against the Radix tree * * @param mpi provider instance * @param flags extra flags * @param data the data to search in * @param dlen length of the the data to search in * @param ctx it will be used to return user data * * @return status of the operation */ static ib_status_t modradix_match(ib_provider_inst_t *mpi, ib_flags_t flags, const uint8_t *data, size_t dlen, void *ctx) { IB_FTRACE_INIT(modradix_match); ib_status_t rc; modradix_provider_data_t *dt = mpi->data; if (dt == NULL) { IB_FTRACE_RET_STATUS(IB_EINVAL); } ib_log_debug(mpi->pr->ib, 4, "Matching AGAINST Radix tree %x", dt->radix_tree); ib_radix_t *radix_tree = dt->radix_tree; ib_radix_prefix_t *pre = NULL; rc = ib_radix_ip_to_prefix((const char *)data, &pre, mpi->mp); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } void *result = NULL; rc = ib_radix_match_closest(radix_tree, pre, &result); if (rc == IB_OK) { modradix_content_t *mrc = (modradix_content_t *)result; if (mrc->callback != NULL && mrc->data != NULL) { *(void **)ctx = result; IB_FTRACE_RET_STATUS(mrc->callback(mrc->data)); } else if (mrc->data != NULL) { if (ctx!= NULL) { *(void **)ctx = result; } IB_FTRACE_RET_STATUS(IB_OK); } else { IB_FTRACE_RET_STATUS(IB_ENOENT); } } IB_FTRACE_RET_STATUS(rc); }
/** * Add a prefix to the prefixes of the binradix, given a prefix and * callback + extra arg * * @param mpr matcher provider * @param prefixes pointer to the prefix container (i.e.: an BinRadix tree) * @param prefix the prefix to be added * @param callback the callback to register with the given prefix * @param arg the extra argument to pass to the callback * @param errptr a pointer reference to point where an error occurred * @param erroffset a pointer holding the offset of the error * * @return status of the operation */ static ib_status_t modbinradix_add_prefix_ex(ib_provider_inst_t *mpi, void *prefixes, const char *prefix, ib_void_fn_t callback, void *arg, const char **errptr, int *erroffset) { IB_FTRACE_INIT(); ib_status_t rc; ib_radix_t *binradix_tree = (ib_radix_t *)mpi->data; modbinradix_content_t *mrc = NULL; mrc = (modbinradix_content_t *)ib_mpool_calloc(mpi->pr->mp, 1, sizeof(modbinradix_content_t)); if (mrc == NULL) { ib_log_error(mpi->pr->ib, "Failed to allocate modbinradix_content_t" " for %s to the BinRadix tree %x", prefix, binradix_tree); IB_FTRACE_RET_STATUS(IB_EALLOC); } mrc->data = arg; mrc->callback = (modbinradix_callback_t)callback; ib_radix_prefix_t *pre = NULL; rc = ib_radix_ip_to_prefix(prefix, &pre, mpi->mp); if (rc != IB_OK) { ib_log_error(mpi->pr->ib, "Failed to create a binradix prefix for %s" " to the BinRadix tree %x", prefix, binradix_tree); IB_FTRACE_RET_STATUS(rc); } rc = ib_radix_insert_data(binradix_tree, pre, (void *) mrc); if (rc == IB_OK) { ib_log_debug(mpi->pr->ib, "prefix %s added to the BinRadix tree %x", prefix, binradix_tree); } else { ib_log_error(mpi->pr->ib, "Failed to load prefix %s to the BinRadix " "tree %x", prefix, binradix_tree); } IB_FTRACE_RET_STATUS(rc); }