Exemplo n.º 1
0
ib_status_t ib_field_create_alias(
    ib_field_t **pf,
    ib_mpool_t  *mp,
    const char  *name,
    size_t       nlen,
    ib_ftype_t   type,
    void        *storage_pval
)
{
    ib_status_t rc;
    char *name_copy;

    /* Allocate the field structure. */
    *pf = (ib_field_t *)ib_mpool_alloc(mp, sizeof(**pf));
    if (*pf == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }
    (*pf)->mp = mp;
    (*pf)->type = type;
    (*pf)->tfn = NULL;

    /* Copy the name. */
    (*pf)->nlen = nlen;
    name_copy = (char *)ib_mpool_alloc(mp, nlen);
    if (name_copy == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }
    memcpy(name_copy, name, nlen);
    (*pf)->name = (const char *)name_copy;

    (*pf)->val = (ib_field_val_t *)ib_mpool_calloc(mp,
        1, sizeof(*((*pf)->val))
    );
    if ((*pf)->val == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    (*pf)->val->pval = storage_pval;

    ib_field_util_log_debug("FIELD_CREATE_ALIAS", (*pf));
    return IB_OK;

failed:
    /* Make sure everything is cleaned up on failure. */
    *pf = NULL;

    return rc;
}
Exemplo n.º 2
0
ib_status_t ib_cfgmap_create(ib_cfgmap_t **pcm,
                             ib_mpool_t *pool)
{
    IB_FTRACE_INIT(ib_cfgmap_create);
    ib_hash_t *hash;
    ib_status_t rc;
    
    /* Underlying hash structure. */
    rc = ib_hash_create(&hash, pool);
    if (rc != IB_OK) {
        rc = IB_EALLOC;
        goto failed;
    }
    pool = hash->mp;

    *pcm = (ib_cfgmap_t *)ib_mpool_alloc(pool, sizeof(**pcm));
    if (*pcm == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    (*pcm)->mp = pool;
    (*pcm)->hash = hash;
    (*pcm)->data = NULL;

    IB_FTRACE_RET_STATUS(IB_OK);

failed:
    /* Make sure everything is cleaned up on failure. */
    *pcm = NULL;

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 3
0
ib_status_t ib_cfgmap_create(ib_cfgmap_t **pcm,
                             ib_mpool_t *pool)
{
    ib_hash_t *hash;
    ib_status_t rc;

    /* Underlying hash structure. */
    rc = ib_hash_create_nocase(&hash, pool);
    if (rc != IB_OK) {
        rc = IB_EALLOC;
        goto failed;
    }
    pool = ib_hash_pool(hash);

    *pcm = (ib_cfgmap_t *)ib_mpool_alloc(pool, sizeof(**pcm));
    if (*pcm == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    (*pcm)->mp = pool;
    (*pcm)->hash = hash;
    /* Set by ib_cfgmap_init() */
    (*pcm)->base = NULL;

    return IB_OK;

failed:
    /* Make sure everything is cleaned up on failure. */
    *pcm = NULL;

    return rc;
}
Exemplo n.º 4
0
ib_status_t ib_provider_define(ib_engine_t *ib,
                               const char *type,
                               ib_provider_register_fn_t fn_reg,
                               void *api)
{
    IB_FTRACE_INIT(ib_provider_define);
    ib_status_t rc;
    ib_provider_def_t *prd;
    char *type_copy;

    /* Create the provider definition. */
    prd = (ib_provider_def_t *)ib_mpool_calloc(ib->config_mp, 1, sizeof(*prd));
    if (prd == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    prd->mp = ib->config_mp;
    prd->fn_reg = fn_reg;
    prd->api = api;

    /* Copy the type. */
    type_copy = (char *)ib_mpool_alloc(prd->mp, strlen(type) + 1);
    if (type_copy == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    strcpy(type_copy, type);
    prd->type = (const char *)type_copy;

    rc = ib_hash_set(ib->apis, type, prd);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 5
0
Arquivo: pcre.c Projeto: niubl/ironbee
/**
 * Set the ID of a DFA rule.
 *
 * @param[in] mp Memory pool to use for allocations.
 * @param[in,out] operator_data DFA rule object to store ID into.
 *
 * @returns
 *   - IB_OK on success.
 *   - IB_EALLOC on memory failure.
 */
static
ib_status_t dfa_id_set(
    ib_mpool_t              *mp,
    modpcre_operator_data_t *operator_data
)
{
    assert(mp            != NULL);
    assert(operator_data != NULL);

    /* We compute the length of the string buffer as such:
     * +2 for the 0x prefix.
     * +1 for the \0 string terminations.
     * +16 for encoding 8 bytes (64 bits) as hex-pairs (2 chars / byte).
     */
    size_t id_sz = 16 + 2 + 1;
    char *id;
    id = ib_mpool_alloc(mp, id_sz+1);
    if (id == NULL) {
        return IB_EALLOC;
    }

    snprintf(id, id_sz, "%p", operator_data);
    operator_data->id = id;

    return IB_OK;
}
Exemplo n.º 6
0
ib_status_t ib_config_register_directive(ib_engine_t *ib,
                                         const char *name,
                                         ib_dirtype_t type,
                                         ib_void_fn_t fn_config,
                                         ib_config_cb_blkend_fn_t fn_blkend,
                                         void *cbdata)
{
    IB_FTRACE_INIT(ib_conf_register_directive);
    ib_dirmap_init_t *rec;
    ib_status_t rc;

    rec = (ib_dirmap_init_t *)ib_mpool_alloc(ib->config_mp, sizeof(*rec));
    if (rec == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    rec->name = name;
    rec->type = type;
    rec->cb._init = fn_config;
    rec->fn_blkend = fn_blkend;
    rec->cbdata = cbdata;

    rc = ib_hash_set(ib->dirmap, rec->name, (void *)rec);

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 7
0
ib_status_t ib_tfn_create(
    const ib_tfn_t **ptfn,
    ib_mpool_t      *mp,
    const char      *name,
    bool             handle_list,
    ib_tfn_fn_t      fn_execute,
    void            *cbdata
)
{
    assert(ptfn       != NULL);
    assert(mp         != NULL);
    assert(name       != NULL);
    assert(fn_execute != NULL);

    ib_tfn_t *tfn;
    char *name_copy;

    name_copy = ib_mpool_strdup(mp, name);
    if (name_copy == NULL) {
        return IB_EALLOC;
    }

    tfn = (ib_tfn_t *)ib_mpool_alloc(mp, sizeof(*tfn));
    if (tfn == NULL) {
        return IB_EALLOC;
    }
    tfn->name        = name_copy;
    tfn->fn_execute  = fn_execute;
    tfn->handle_list = handle_list;
    tfn->cbdata      = cbdata;

    *ptfn = tfn;

    return IB_OK;
}
Exemplo n.º 8
0
ib_status_t ib_dso_open(
    ib_dso_t   **dso,
    const char  *file,
    ib_mpool_t  *pool
)
{
    void *handle;

    /// @todo Probably need to do this portably someday

    handle = dlopen(file, RTLD_LAZY);
    if (handle == NULL) {
        ib_util_log_error("%s", dlerror());
        return IB_EINVAL;
    }

    *dso = ib_mpool_alloc(pool, sizeof(**dso));
    if (*dso == NULL) {
        dlclose(handle);
        return IB_EALLOC;
    }

    (*dso)->mp = pool;
    (*dso)->handle = handle;

    return IB_OK;
}
Exemplo n.º 9
0
Arquivo: tfn.c Projeto: niq/ironbee
ib_status_t ib_tfn_create(ib_engine_t *ib,
                          const char *name,
                          ib_tfn_fn_t transform,
                          void *fndata,
                          ib_tfn_t **ptfn)
{
    IB_FTRACE_INIT(ib_tfn_create);
    ib_status_t rc;
    ib_tfn_t *tfn;
    char *name_copy;
    size_t name_len = strlen(name) + 1;

    name_copy = (char *)ib_mpool_alloc(ib->mp, name_len);
    if (name_copy == NULL) {
        if (ptfn != NULL) {
            *ptfn = NULL;
        }
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    memcpy(name_copy, name, name_len);

    tfn = (ib_tfn_t *)ib_mpool_alloc(ib->mp, sizeof(*tfn));
    if (tfn == NULL) {
        if (ptfn != NULL) {
            *ptfn = NULL;
        }
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    tfn->name = name_copy;
    tfn->transform = transform;
    tfn->fndata = fndata;

    rc = ib_hash_set(ib->tfns, name_copy, tfn);
    if (rc != IB_OK) {
        if (ptfn != NULL) {
            *ptfn = NULL;
        }
        IB_FTRACE_RET_STATUS(rc);
    }

    if (ptfn != NULL) {
        *ptfn = tfn;
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 10
0
/**
 * Create an instance of the @c ee_match_any operator.
 *
 * Looks up the automata name and adds the automata to the operator instance.
 *
 * @param[in] ctx Current context.
 * @param[in] parameters Automata name.
 * @param[out] instance_data Instance data.
 * @param[in] cbdata Callback data.
 */
static
ib_status_t ee_match_any_operator_create(
    ib_context_t *ctx,
    const char   *parameters,
    void         *instance_data,
    void         *cbdata
)
{
    assert(ctx != NULL);
    assert(parameters != NULL);
    assert(instance_data != NULL);

    ib_status_t rc;
    ia_eudoxus_t* eudoxus;
    ee_operator_data_t *operator_data;
    ib_module_t *module;
    ib_engine_t *ib = ib_context_get_engine(ctx);
    ib_mpool_t *pool = ib_context_get_mpool(ctx);
    const ee_config_t *config = ee_get_config(ib);
    const ib_hash_t *eudoxus_pattern_hash;

    assert(config != NULL);
    assert(config->eudoxus_pattern_hash != NULL);

    /* Get my module object */
    rc = ib_engine_module_get(ib, MODULE_NAME_STR, &module);
    if (rc != IB_OK) {
        ib_log_error(ib, "Failed to get eudoxus operator module object: %s",
                     ib_status_to_string(rc));
        return rc;
    }
    /* Allocate a rule data object, populate it */
    operator_data = ib_mpool_alloc(pool, sizeof(*operator_data));
    if (operator_data == NULL) {
        return IB_EALLOC;
    }

    eudoxus_pattern_hash = config->eudoxus_pattern_hash;

    rc = ib_hash_get(eudoxus_pattern_hash, &eudoxus, parameters);
    if (rc == IB_ENOENT ) {
        ib_log_error(ib,
                     MODULE_NAME_STR ": No eudoxus automata named %s found.",
                     parameters);
        return rc;
    }
    else if (rc != IB_OK) {
        ib_log_error(ib,
                     MODULE_NAME_STR ": Error setting up eudoxus automata operator.");
        return rc;
    }

    operator_data->eudoxus = eudoxus;
    *(ee_operator_data_t **)instance_data = operator_data;
    ib_log_debug(ib, "Found compiled eudoxus pattern \"%s\"", parameters);

    return IB_OK;
}
Exemplo n.º 11
0
/**
 * Perf Event Start Event Callback.
 *
 * On a connection started event we register connection
 * counters for the connection.
 *
 * @param[in] ib IronBee object.
 * @param[in] event Event type.
 * @param[in] connp Connection object.
 * @param[in] cbdata Callback data: actually an perf_info_t describing the
 *            event.
 */
static ib_status_t mod_perf_stats_reg_conn_counter(
     ib_engine_t *ib,
     ib_state_event_type_t event_type,
     ib_conn_t *connp,
     void *cbdata
)
{
    IB_FTRACE_INIT();

    perf_info_t *perf_info;
    event_info_t *eventp = (event_info_t *)cbdata;
    int cevent = eventp->number;
    int rc;
    int event;

    perf_info = ib_mpool_alloc(connp->mp, sizeof(*perf_info) * IB_STATE_EVENT_NUM);

    for (event = 0; event < IB_STATE_EVENT_NUM; ++event) {
        if ((eventp->cbdata_type == IB_CBDATA_NONE) ||
            (eventp->cbdata_type == IB_CBDATA_CONN_DATA_T)) {
            ib_log_error(ib, "Cannot collect stats for:%d name:%s cbdata_type: %d",
                         eventp->number, eventp->name, eventp->cbdata_type);
        }
        else {
            perf_info_t *perfp = &perf_info[event];

            /* Does this event match conn_started_event?
             * If so we should init counters for this event.
             */
            if (event == cevent) {
                perfp->call_cnt = 1;
                perfp->start_usec = ib_clock_get_time();
            }
            else {
                perfp->call_cnt = 0;
                perfp->start_usec = 0;
            }

            /* Setup other defaults */
            perfp->number = event;
            perfp->name = ib_state_event_name((ib_state_event_type_t)event);
            perfp->cbdata_type = ib_state_event_cbdata_type(event);
            perfp->max_usec = 0;
            perfp->total_usec = 0;
            perfp->stop_usec = 0;

            ib_log_debug(ib, "Perf callback registered %s (%d) (%d)",
                         perfp->name, perfp->number, perfp->cbdata_type);
        }
    }

    rc = ib_hash_set(connp->data, "MOD_PERF_STATS" ,perf_info);
    if (rc != IB_OK) {
        ib_log_debug(ib, "Failed to store perf stats in connection data: %s", ib_status_to_string(rc));
        IB_FTRACE_RET_STATUS(rc);
    }
    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 12
0
Arquivo: pcre.c Projeto: niubl/ironbee
/**
 * Create the per-transaction data for use with the dfa operator.
 *
 * @param[in] m PCRE module.
 * @param[in,out] tx Transaction to store the value in.
 * @param[in] cpatt_data Compiled pattern data
 * @param[in] id The operator identifier used to get it's workspace.
 * @param[out] workspace Created.
 *
 * @returns
 *   - IB_OK on success.
 *   - IB_EALLOC on an allocation error.
 */
static
ib_status_t alloc_dfa_tx_data(
    const ib_module_t          *m,
    ib_tx_t                    *tx,
    const modpcre_cpat_data_t  *cpatt_data,
    const char                 *id,
    dfa_workspace_t           **workspace
)
{
    assert(tx);
    assert(tx->mp);
    assert(id);
    assert(workspace);

    ib_hash_t *hash;
    ib_status_t rc;
    dfa_workspace_t *ws;
    size_t size;

    *workspace = NULL;
    rc = get_or_create_operator_data_hash(m, tx, &hash);
    if (rc != IB_OK) {
        return rc;
    }

    ws = (dfa_workspace_t *)ib_mpool_alloc(tx->mp, sizeof(*ws));
    if (ws == NULL) {
        return IB_EALLOC;
    }

    ws->wscount = cpatt_data->dfa_ws_size;
    size = sizeof(*(ws->workspace)) * (ws->wscount);
    ws->workspace = (int *)ib_mpool_alloc(tx->mp, size);
    if (ws->workspace == NULL) {
        return IB_EALLOC;
    }

    rc = ib_hash_set(hash, id, ws);
    if (rc == IB_OK) {
        *workspace = ws;
    }

    return rc;
}
Exemplo n.º 13
0
ib_status_t ib_parsed_name_value_pair_list_add(
    ib_parsed_name_value_pair_list_wrapper_t *headers,
    const char *name,
    size_t name_len,
    const char *value,
    size_t value_len)
{
    IB_FTRACE_INIT();
    ib_status_t rc;

    assert(headers != NULL);
    assert(headers->mpool != NULL);
    assert(name != NULL);
    assert(value != NULL);

    ib_parsed_name_value_pair_list_t *ele;

    ele = ib_mpool_alloc(headers->mpool, sizeof(*ele));
    if (ele == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    rc = ib_bytestr_dup_mem(&ele->name,
                            headers->mpool,
                            (const uint8_t *)name,
                            name_len);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    rc = ib_bytestr_dup_mem(&ele->value,
                            headers->mpool,
                            (const uint8_t *)value,
                            value_len);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    ele->next = NULL;

    /* List is empty. Add first element. */
    if (headers->head == NULL) {
        headers->head = ele;
        headers->tail = ele;
        headers->size = 1;
    }

    /* Normal append to a list with values in it already. */
    else {
        headers->tail->next = ele;
        headers->tail = ele;
        ++(headers->size);
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 14
0
ib_status_t ib_action_inst_create_ex(
    ib_engine_t *ib,
    const char *name,
    const char *parameters,
    ib_action_inst_t **act_inst)
{
    assert(ib != NULL);
    assert(name != NULL);

    ib_hash_t *action_hash = ib->actions;
    ib_action_t *action;
    ib_status_t rc;
    ib_mpool_t *mpool = ib_engine_pool_main_get(ib);

    assert(mpool != NULL);

    rc = ib_hash_get(action_hash, &action, name);
    if (rc != IB_OK) {
        /* name is not registered */
        return rc;
    }

    *act_inst = (ib_action_inst_t *)ib_mpool_alloc(mpool,
                                                   sizeof(ib_action_inst_t));
    if (*act_inst == NULL) {
        return IB_EALLOC;
    }
    (*act_inst)->action = action;
    (*act_inst)->params = ib_mpool_strdup(mpool, parameters);
    (*act_inst)->fparam = NULL;

    if (action->fn_create != NULL) {
        rc = action->fn_create(
            ib,
            parameters,
            *act_inst,
            action->cbdata_create
        );
        if (rc != IB_OK) {
            return rc;
        }
    }
    else {
        rc = IB_OK;
    }

    if ((*act_inst)->fparam == NULL) {
        rc = ib_field_create(&((*act_inst)->fparam),
                             mpool,
                             IB_FIELD_NAME("param"),
                             IB_FTYPE_NULSTR,
                             ib_ftype_nulstr_in(parameters));
    }

    return rc;
}
Exemplo n.º 15
0
Arquivo: mpool.c Projeto: niq/ironbee
void *ib_mpool_memdup(ib_mpool_t *mp, const void *src, size_t size)
{
    IB_FTRACE_INIT(ib_mpool_memdup);
    void *ptr = ib_mpool_alloc(mp, size);

    if (ptr != NULL) {
        memcpy(ptr, src, size);
    }
    IB_FTRACE_RET_PTR(void, ptr);
}
Exemplo n.º 16
0
Arquivo: mpool.c Projeto: niq/ironbee
void *ib_mpool_calloc(ib_mpool_t *mp, size_t nelem, size_t size)
{
    IB_FTRACE_INIT(ib_mpool_calloc);
    void *ptr = ib_mpool_alloc(mp, nelem * size);

    if (ptr == NULL) {
        IB_FTRACE_RET_PTR(void, NULL);
    }
    memset(ptr, 0, nelem * size);
    IB_FTRACE_RET_PTR(void, ptr);
}
Exemplo n.º 17
0
Arquivo: mpool.c Projeto: niq/ironbee
char *ib_mpool_strdup(ib_mpool_t *mp, const char *src)
{
    IB_FTRACE_INIT(ib_mpool_strdup);
    size_t size = strlen(src)+1;
    char *ptr = (char *)ib_mpool_alloc(mp, size);

    if (ptr != NULL) {
        memcpy(ptr, src, size);
    }
    IB_FTRACE_RET_PTR(char, ptr);
}
Exemplo n.º 18
0
static void * mp_malloc(ib_kvstore_t *kvstore,
                        size_t size,
                        ib_kvstore_cbdata_t *cbdata)
{

    assert(kvstore);

    ib_kvstore_riak_server_t *riak =
        (ib_kvstore_riak_server_t *)kvstore->server;

    return ib_mpool_alloc(riak->mp, size);
}
Exemplo n.º 19
0
void* MemoryPool::alloc(size_t size) const
{
    void* memory = ib_mpool_alloc(ib(), size);
    if (! memory) {
        BOOST_THROW_EXCEPTION(
          ealloc() << errinfo_what(
            "ib_mpool_alloc() returned NULL"
          )
        );
    }
    return memory;
}
Exemplo n.º 20
0
/**
 * Create the per-transaction data for use with the dfa operator.
 *
 * @param[in,out] tx Transaction to store the value in.
 * @param[in] id The operator identifier used to get it's workspace.
 * @param[out] workspace Created.
 *
 * @returns
 *   - IB_OK on success.
 *   - IB_EALLOC on an allocation error.
 */
static ib_status_t alloc_dfa_tx_data(ib_tx_t *tx,
                                     const char *id,
                                     dfa_workspace_t **workspace)
{
    IB_FTRACE_INIT();

    assert(tx);
    assert(tx->mp);
    assert(id);
    assert(workspace);

    ib_hash_t *rule_data;
    ib_status_t rc;

    rc = get_or_create_rule_data_hash(tx, &rule_data);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    *workspace = (dfa_workspace_t *)ib_mpool_alloc(tx->mp, sizeof(**workspace));
    if (*workspace == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    (*workspace)->wscount = WORKSPACE_SIZE_DEFAULT;
    (*workspace)->workspace =
        (int *)ib_mpool_alloc(tx->mp,
                             sizeof(*((*workspace)->workspace)) *
                                (*workspace)->wscount);
    if ((*workspace)->workspace == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    rc = ib_hash_set(rule_data, id, *workspace);
    if (rc != IB_OK) {
        *workspace = NULL;
    }

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 21
0
ib_status_t ib_bytestr_create(
    ib_bytestr_t **pdst,
    ib_mpool_t    *pool,
    size_t         size
) {
    IB_FTRACE_INIT();

    assert(pdst != NULL);
    assert(pool != NULL);

    ib_status_t rc;

    /* Create the structure. */
    *pdst = (ib_bytestr_t *)ib_mpool_alloc(pool, sizeof(**pdst));
    if (*pdst == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    (*pdst)->data   = NULL;
    (*pdst)->mp     = pool;
    (*pdst)->flags  = 0;
    (*pdst)->size   = size;
    (*pdst)->length = 0;

    if (size != 0) {
        (*pdst)->data = (uint8_t *)ib_mpool_alloc(pool, size);
        if ((*pdst)->data == NULL) {
            rc = IB_EALLOC;
            goto failed;
        }
    }

    IB_FTRACE_RET_STATUS(IB_OK);

failed:
    *pdst = NULL;

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 22
0
ib_status_t ib_flags_oplist_parse(
    const ib_strval_t *map,
    ib_mpool_t        *mp,
    const char        *str,
    const char        *sep,
    ib_list_t         *oplist)
{
    if ( (map == NULL) || (str == NULL) || (sep == NULL) || (oplist == NULL) ) {
        return IB_EINVAL;
    }

    char       *copy;
    const char *tmp;

    /* Make a copy of the string that we can use for strtok */
    copy = ib_mpool_strdup(mp, str);
    if (copy == NULL) {
        return IB_EALLOC;
    }

    /* Clear the list */
    ib_list_clear(oplist);

    /* Walk through the separated list, parser each operator, build the list */
    tmp = strtok(copy, sep);
    do {
        ib_status_t           rc;
        ib_flags_op_t         op;
        ib_flags_t            flags;
        ib_flags_operation_t *operation;

        rc = parse_single(map, tmp, &op, &flags);
        if (rc != IB_OK) {
            return rc;
        }
        operation = ib_mpool_alloc(mp, sizeof(*operation));
        if (operation == NULL) {
            return IB_EALLOC;
        }
        operation->op = op;
        operation->flags = flags;
        rc = ib_list_push(oplist, operation);
        if (rc != IB_OK) {
            return rc;
        }
    } while ( (tmp = strtok(NULL, sep)) != NULL);

    return IB_OK;
}
Exemplo n.º 23
0
ib_status_t ib_array_create(ib_array_t **parr, ib_mpool_t *pool,
                            size_t ninit, size_t nextents)
{
    IB_FTRACE_INIT();
    ib_status_t rc;

    /* Validate. */
    if ((ninit == 0) || (nextents == 0)) {
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    /* Create the structure. */
    *parr = (ib_array_t *)ib_mpool_alloc(pool, sizeof(**parr));
    if (*parr == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }
    (*parr)->mp = pool;
    (*parr)->ninit = ninit;
    (*parr)->nextents = nextents;
    (*parr)->nelts = 0;
    (*parr)->size = ninit;

    /* Create the extents array. */
    (*parr)->extents = (void *)ib_mpool_calloc(pool,
                                               nextents, sizeof(void *));
    if ((*parr)->extents == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    /* Create the first data array in the first extent slot. */
    *((void ***)(*parr)->extents) = (void **)ib_mpool_calloc(pool,
        ninit, sizeof(void *)
    );
    if (*((void ***)(*parr)->extents) == NULL) {
        rc = IB_EALLOC;
        goto failed;
    }

    IB_FTRACE_RET_STATUS(IB_OK);

failed:
    /* Make sure everything is cleaned up on failure. */
    *parr = NULL;

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 24
0
ib_status_t ib_action_register(
    ib_engine_t            *ib,
    const char             *name,
    ib_flags_t              flags,
    ib_action_create_fn_t   fn_create,
    void                   *cbdata_create,
    ib_action_destroy_fn_t  fn_destroy,
    void                   *cbdata_destroy,
    ib_action_execute_fn_t  fn_execute,
    void                   *cbdata_execute
)
{
    IB_FTRACE_INIT();
    ib_hash_t *action_hash = ib->actions;
    ib_mpool_t *pool = ib_engine_pool_main_get(ib);
    ib_status_t rc;
    char *name_copy;
    ib_action_t *act;

    rc = ib_hash_get(action_hash, &act, name);
    if (rc == IB_OK) {
        /* name already is registered */
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    name_copy = ib_mpool_strdup(pool, name);
    if (name_copy == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    act = (ib_action_t *)ib_mpool_alloc(pool, sizeof(*act));
    if (act == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    act->name           = name_copy;
    act->flags          = flags;
    act->fn_create      = fn_create;
    act->cbdata_create  = cbdata_create;
    act->fn_destroy     = fn_destroy;
    act->cbdata_destroy = cbdata_destroy;
    act->fn_execute     = fn_execute;
    act->cbdata_execute = cbdata_execute;

    rc = ib_hash_set(action_hash, name_copy, act);

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 25
0
ib_status_t ib_bytestr_append_mem(
    ib_bytestr_t  *dst,
    const uint8_t *data,
    size_t         data_length
)
{
    IB_FTRACE_INIT();

    size_t dst_length = ib_bytestr_length(dst);
    size_t new_length;
    uint8_t *new_data = NULL;

    if (dst == NULL || IB_BYTESTR_CHECK_FREADONLY(dst->flags)) {
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }
    if (data == NULL && data_length != 0) {
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    new_length = dst_length + data_length;

    if (new_length > dst->size) {
        new_data = (uint8_t *)ib_mpool_alloc(dst->mp, new_length);
        if (new_data == NULL) {
            IB_FTRACE_RET_STATUS(IB_EALLOC);
        }
        if (dst_length > 0) {
            memcpy(
                new_data,
                ib_bytestr_const_ptr(dst),
                ib_bytestr_length(dst)
            );
        }
        dst->data = new_data;
        dst->size = new_length;
    }
    assert(new_length <= dst->size);

    if (data_length > 0) {
        memcpy(dst->data + dst_length, data, data_length);
        dst->length = new_length;
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 26
0
ib_status_t ib_action_register(
    ib_engine_t            *ib,
    const char             *name,
    ib_action_create_fn_t   fn_create,
    void                   *cbdata_create,
    ib_action_destroy_fn_t  fn_destroy,
    void                   *cbdata_destroy,
    ib_action_execute_fn_t  fn_execute,
    void                   *cbdata_execute
)
{
    ib_hash_t *action_hash = ib->actions;
    ib_mpool_t *pool = ib_engine_pool_main_get(ib);
    ib_status_t rc;
    char *name_copy;
    ib_action_t *act;

    rc = ib_hash_get(action_hash, &act, name);
    if (rc == IB_OK) {
        /* name already is registered */
        return IB_EINVAL;
    }

    name_copy = ib_mpool_strdup(pool, name);
    if (name_copy == NULL) {
        return IB_EALLOC;
    }

    act = (ib_action_t *)ib_mpool_alloc(pool, sizeof(*act));
    if (act == NULL) {
        return IB_EALLOC;
    }
    act->name           = name_copy;
    act->fn_create      = fn_create;
    act->cbdata_create  = cbdata_create;
    act->fn_destroy     = fn_destroy;
    act->cbdata_destroy = cbdata_destroy;
    act->fn_execute     = fn_execute;
    act->cbdata_execute = cbdata_execute;

    rc = ib_hash_set(action_hash, name_copy, act);

    return rc;
}
Exemplo n.º 27
0
/**
 * @brief Create the PCRE operator.
 * @param[in] ib The IronBee engine (unused)
 * @param[in] ctx The current IronBee context (unused)
 * @param[in,out] pool The memory pool into which @c op_inst->data
 *                will be allocated.
 * @param[in] The regular expression to be built.
 * @param[out] op_inst The operator instance that will be populated by
 *             parsing @a pattern.
 * @returns IB_OK on success or IB_EALLOC on any other type of error.
 */
static ib_status_t dfa_operator_create(ib_engine_t *ib,
                                        ib_context_t *ctx,
                                        const ib_rule_t *rule,
                                        ib_mpool_t *pool,
                                        const char *pattern,
                                        ib_operator_inst_t *op_inst)
{
    IB_FTRACE_INIT();
    const char* errptr;
    int erroffset;
    dfa_rule_data_t *rule_data = NULL;
    ib_status_t rc;

    rc = dfa_compile_internal(pool,
                              &rule_data,
                              pattern,
                              &errptr,
                              &erroffset);

    if (rc==IB_OK) {
        ib_log_debug(ib, "Compiled DFA operator pattern: %s", pattern);

        /* We compute the length of the string buffer as such:
         * +2 for the 0x prefix.
         * +1 for the \0 string terminations.
         * +16 for encoding 8 bytes (64 bits) as hex-pairs (2 chars / byte).
         */
        size_t id_sz = 16 + 2 + 1;
        char *id;
        id = ib_mpool_alloc(pool, id_sz);

        snprintf(id, id_sz, "%p", op_inst);
        rule_data->id = id;
        ib_log_debug(ib, "Created DFA operator with ID %s.", id);
        op_inst->data = rule_data;
    }
    else {
        ib_log_error(ib, "Failed to parse DFA operator pattern: %s", pattern);
    }


    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 28
0
ib_status_t ib_action_inst_create(ib_engine_t *ib,
                                  ib_context_t *ctx,
                                  const char *name,
                                  const char *parameters,
                                  ib_flags_t flags,
                                  ib_action_inst_t **act_inst)
{
    IB_FTRACE_INIT();
    ib_hash_t *action_hash = ib->actions;
    ib_mpool_t *pool = ib_engine_pool_main_get(ib);
    ib_action_t *action;
    ib_status_t rc;

    rc = ib_hash_get(action_hash, &action, name);
    if (rc != IB_OK) {
        /* name is not registered */
        IB_FTRACE_RET_STATUS(rc);
    }

    *act_inst = (ib_action_inst_t *)ib_mpool_alloc(pool,
                                                   sizeof(ib_action_inst_t));
    if (*act_inst == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }
    (*act_inst)->action = action;
    (*act_inst)->flags = flags;

    if (action->fn_create != NULL) {
        rc = action->fn_create(
            ib,
            ctx,
            pool,
            parameters,
            *act_inst,
            action->cbdata_create
        );
    }
    else {
        rc = IB_OK;
    }

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 29
0
/* Simple ASCII lowercase function (ex version); see string.h */
ib_status_t ib_strlower_ex(ib_strop_t op,
                           ib_mpool_t *mp,
                           uint8_t *data_in,
                           size_t dlen_in,
                           uint8_t **data_out,
                           size_t *dlen_out,
                           ib_flags_t *result)
{
    IB_FTRACE_INIT();
    ib_status_t rc = IB_OK;

    assert(mp != NULL);
    assert(data_in != NULL);
    assert(data_out != NULL);
    assert(dlen_out != NULL);
    assert(result != NULL);

    switch(op) {
    case IB_STROP_INPLACE:
        rc = inplace(IB_STRFLAG_ALIAS, data_in, dlen_in, result);
        *data_out = data_in;
        *dlen_out = dlen_in;
        break;

    case IB_STROP_COPY:
        *data_out = ib_mpool_alloc(mp, dlen_in);
        if (*data_out == NULL) {
            IB_FTRACE_RET_STATUS(IB_EALLOC);
        }
        *dlen_out = dlen_in;
        rc = inplace(IB_STRFLAG_NEWBUF, *data_out, dlen_in, result);
        break;

    case IB_STROP_COW:
        rc = copy_on_write(mp, data_in, dlen_in, data_out, dlen_out, result);
        break;

    default:
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    IB_FTRACE_RET_STATUS(rc);
}
Exemplo n.º 30
0
/**
 * Join three memory blocks into a single buffer
 *
 * @param[in] mp Memory pool
 * @param[in] p1 Pointer to block 1
 * @param[in] l1 Length of block 1
 * @param[in] p2 Pointer to block 2
 * @param[in] l2 Length of block 2
 * @param[in] p3 Pointer to block 3
 * @param[in] l3 Length of block 3
 * @param[in] nul true if NUL byte should be tacked on, false if not
 * @param[out] out Pointer to output block
 * @param[out] olen Length of the output block
 *
 * @returns status code
 */
static ib_status_t join3(ib_mpool_t *mp,
                         const char *p1,
                         size_t l1,
                         const char *p2,
                         size_t l2,
                         const char *p3,
                         size_t l3,
                         bool nul,
                         char **out,
                         size_t *olen)
{
    IB_FTRACE_INIT();
    size_t slen = l1 + l2 + l3;
    size_t buflen = slen + (nul ? 1 : 0);
    char *buf;
    char *p;

    /* Allocate the buffer */
    buf = (char *)ib_mpool_alloc(mp, buflen);
    if (buf == NULL) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    /* Copy the blocks in */
    p = buf;
    memmove(p, p1, l1);
    p += l1;
    memmove(p, p2, l2);
    p += l2;
    memmove(p, p3, l3);
    p += l3;
    if (nul) {
        *p = '\0';
    }

    /* Done */
    *out = buf;
    *olen = slen;
    IB_FTRACE_RET_STATUS(IB_OK);
}