HooksRegistrar& HooksRegistrar::connection( Engine::state_event_e event, connection_t f ) { if (f.empty()) { BOOST_THROW_EXCEPTION(einval() << errinfo_what( "Empty functional passed to hook registrarion." )); } throw_if_error( ib_hook_conn_register( m_engine.ib(), static_cast<ib_state_event_type_t>(event), &Internal::Hooks::connection, value_to_data<connection_t>( f, m_engine.main_memory_pool().ib() ) ) ); return *this; }
/** * @internal * Initialize a context for the perf_stats module. * * This is a hack. * Hook callbacks set here to get end times for hooks. * Currently no other modules register hook call backs. * Because of this it should * ensure that we are the last thing called per event. * * @param[in] ib IronBee object * @param[in] m Module object * @param[in] ctx Context object * @param[in] cbdata Callback data (unused) */ static ib_status_t perf_stats_context_close(ib_engine_t *ib, ib_module_t *m, ib_context_t *ctx, void *cbdata) { IB_FTRACE_INIT(); ib_status_t rc; int event; /* Check that we are in the main ctx otherwise return */ if (ctx != ib_context_main(ib)) { IB_FTRACE_RET_STATUS(IB_OK); } for (event = 0; event < IB_STATE_EVENT_NUM; ++event) { event_info_t *eventp = &event_info[event]; if ((eventp->cbdata_type == IB_CBDATA_NONE) || (eventp->cbdata_type == IB_CBDATA_CONN_DATA_T)) { rc = IB_EINVAL; ib_log_error(ib, "Cannot register handler " "for:%d name:%s cbdata_type: %d", eventp->number, eventp->name, eventp->cbdata_type); } else { switch( ib_state_hook_type( (ib_state_event_type_t)event ) ) { case IB_STATE_HOOK_CONN: rc = ib_hook_conn_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_stop_conn_callback, (void *)eventp ); break; case IB_STATE_HOOK_CONNDATA: rc = ib_hook_conndata_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_stop_conndata_callback, (void *)eventp ); break; case IB_STATE_HOOK_TX: rc = ib_hook_tx_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_stop_tx_callback, (void *)eventp ); break; case IB_STATE_HOOK_TXDATA: rc = ib_hook_txdata_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_stop_txdata_callback, (void *)eventp ); break; default: rc = IB_EINVAL; ib_log_error(ib, "Event with unknown hook type: %d/%s", eventp->number, eventp->name); } } if (rc != IB_OK) { ib_log_error(ib, "Hook register for " "event:%d name:%s cbdata_type: %d returned %s", eventp->number, eventp->name, eventp->cbdata_type, ib_status_to_string(rc)); } } IB_FTRACE_RET_STATUS(IB_OK); }
/** * @internal * Called when module is loaded * Start event hooks are registered here. * * @param[in] ib IronBee object * @param[in] m Module object * @param[in] cbdata Callback data (unused) */ static ib_status_t perf_stats_init(ib_engine_t *ib, ib_module_t *m, void *cbdata) { /*Detect main context otherwise return IB_ENGINE_CONTEXT_MAIN. */ IB_FTRACE_INIT(); ib_log_debug(ib, "Perf stats module loaded."); ib_status_t rc; int event; /* Register specific handlers for specific events, and a * generic handler for the rest */ for (event = 0; event < IB_STATE_EVENT_NUM; ++event) { event_info_t *eventp = &event_info[event]; /* Record event info */ eventp->number = event; eventp->name = ib_state_event_name((ib_state_event_type_t)event); eventp->cbdata_type = ib_state_event_cbdata_type(event); /* init the per connection counters here. * Otherwise use callback data type. */ if (event == conn_started_event) { rc = ib_hook_conn_register( ib, (ib_state_event_type_t)event, mod_perf_stats_reg_conn_counter, (void *)eventp ); } else if ((eventp->cbdata_type == IB_CBDATA_NONE) || (eventp->cbdata_type == IB_CBDATA_CONN_DATA_T)) { rc = IB_EINVAL; ib_log_error(ib, "Cannot register handler " "for:%d name:%s cbdata_type: %d", eventp->number, eventp->name, eventp->cbdata_type); } else { switch( ib_state_hook_type( (ib_state_event_type_t)event ) ) { case IB_STATE_HOOK_CONN: rc = ib_hook_conn_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_start_conn_callback, (void *)eventp ); break; case IB_STATE_HOOK_CONNDATA: rc = ib_hook_conndata_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_start_conndata_callback, (void *)eventp ); break; case IB_STATE_HOOK_TX: rc = ib_hook_tx_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_start_tx_callback, (void *)eventp ); break; case IB_STATE_HOOK_TXDATA: rc = ib_hook_txdata_register( ib, (ib_state_event_type_t)event, mod_perf_stats_event_start_txdata_callback, (void *)eventp ); break; default: rc = IB_EINVAL; ib_log_error(ib, "Event with unknown hook type: %d/%s", eventp->number, eventp->name); } } if (rc != IB_OK) { ib_log_error(ib, "Hook register for" "event:%d name:%s cbdata_type: %d returned %s", eventp->number, eventp->name, eventp->cbdata_type, ib_status_to_string(rc)); } } IB_FTRACE_RET_STATUS(IB_OK); }
/** * Ironbee initialisation function. Sets up engine and logging, * and reads Ironbee config. * * @param[in] cf Configuration rec * @return NGX_OK or error */ static ngx_int_t ironbee_init(ngx_conf_t *cf) { ngx_log_t *prev_log; ib_context_t *ctx; ib_cfgparser_t *cp; ironbee_proc_t *proc; ib_status_t rc, rc1; prev_log = ngxib_log(cf->log); ngx_regex_malloc_init(cf->pool); ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "ironbee_init %d", getpid()); proc = ngx_http_conf_get_module_main_conf(cf, ngx_ironbee_module); if (proc->loglevel == NGX_CONF_UNSET_UINT) proc->loglevel = 4; /* default */ rc = ib_initialize(); if (rc != IB_OK) cleanup_return(prev_log) IB2NG(rc); ib_util_log_level(proc->loglevel); rc = ib_engine_create(&ironbee, ngxib_server()); if (rc != IB_OK) cleanup_return(prev_log) IB2NG(rc); if (proc->use_ngxib_logger) ib_log_set_logger_fn(ironbee, ngxib_logger, NULL); /* Using default log level function. */ rc = ib_engine_init(ironbee); if (rc != IB_OK) cleanup_return(prev_log) IB2NG(rc); /* TODO: TS creates logfile at this point */ ib_hook_conn_register(ironbee, conn_opened_event, ngxib_conn_init, NULL); rc = ib_cfgparser_create(&cp, ironbee); assert((cp != NULL) || (rc != IB_OK)); if (rc != IB_OK) cleanup_return(prev_log) IB2NG(rc); rc = ib_engine_config_started(ironbee, cp); if (rc != IB_OK) cleanup_return(prev_log) IB2NG(rc); /* Get the main context, set some defaults */ ctx = ib_context_main(ironbee); ib_context_set_num(ctx, "logger.log_level", proc->loglevel); /* FIXME - use the temp pool operation for this */ char *buf = strndup((char*)proc->config_file.data, proc->config_file.len); rc = ib_cfgparser_parse(cp, buf); free(buf); rc1 = ib_engine_config_finished(ironbee); ib_cfgparser_destroy(cp); cleanup_return(prev_log) rc == IB_OK ? rc1 == IB_OK ? NGX_OK : IB2NG(rc1) : IB2NG(rc); }
/** * Initializes and configures the ironbee engine. */ static int ironbee_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptmp, server_rec *s) { ironbee_config_t *modcfg = (ironbee_config_t *)ap_get_module_config(s->module_config, &ironbee_module); ib_cfgparser_t *cp; ib_provider_t *lpr; void *init = NULL; ib_status_t rc; /* Init IB library. */ rc = ib_initialize(); if (rc != IB_OK) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Error initializing ib library"); return OK; } ib_util_log_level(4); /* Detect first (validation) run vs real config run. */ apr_pool_userdata_get(&init, "ironbee-init", s->process->pool); if (init == NULL) { ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, MODULE_NAME_FULL " loading."); apr_pool_userdata_set((const void *)1, "ironbee-init", apr_pool_cleanup_null, s->process->pool); return OK; } /// @todo Tracefile needs removed //ib_trace_init("/tmp/ironbee.trace"); ib_trace_init(NULL); /* Create the engine handle. */ rc = ib_engine_create(&ironbee, &ibplugin); if (rc != IB_OK) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Error creating engine: %s", ib_status_to_string(rc)); return OK; } /* Register the logger. */ rc = ib_provider_register(ironbee, IB_PROVIDER_TYPE_LOGGER, MODULE_NAME_STR, &lpr, &ironbee_logger_iface, NULL); if (rc != IB_OK) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Error registering log provider: %s", ib_status_to_string(rc)); return OK; } ib_provider_data_set(lpr, (void *)s); /* Default logger */ /// @todo Need to add a post set hook in core for this to work correctly ib_context_set_string(ib_context_engine(ironbee), IB_PROVIDER_TYPE_LOGGER, MODULE_NAME_STR); ib_context_set_num(ib_context_engine(ironbee), IB_PROVIDER_TYPE_LOGGER ".log_level", 4); rc = ib_engine_init(ironbee); if (rc != IB_OK) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Error initializing engine: %s", ib_status_to_string(rc)); return OK; } /* Register module cleanup. */ apr_pool_cleanup_register(p, (void *)s, ironbee_module_cleanup, apr_pool_cleanup_null); /* Register conn/tx init hooks. */ ib_hook_conn_register(ironbee, conn_opened_event, ironbee_conn_init, s); /* Configure the engine. */ if (modcfg->config != NULL) { ib_context_t *ctx; /* Notify the engine that the config process has started. This * will also create a main configuration context. */ ib_state_notify_cfg_started(ironbee); /* Get the main configuration context. */ ctx = ib_context_main(ironbee); /* Set some defaults */ ib_context_set_string(ctx, IB_PROVIDER_TYPE_LOGGER, MODULE_NAME_STR); ib_context_set_num(ctx, "logger.log_level", 4); /* Parse the config file. */ rc = ib_cfgparser_create(&cp, ironbee); if ((rc == IB_OK) && (cp != NULL)) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Parsing config: %s", modcfg->config); ib_cfgparser_parse(cp, modcfg->config); ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": Destroying config parser"); ib_cfgparser_destroy(cp); } /* Notify the engine that the config process has finished. This * will also close out the main configuration context. */ ib_state_notify_cfg_finished(ironbee); } else { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, IB_PRODUCT_NAME ": No config specified with IronBeeConfig directive"); } ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, MODULE_NAME_FULL " configured."); return OK; }