app-layer: clean up counters registration

pull/2359/head
Victor Julien 9 years ago
parent c6134e007e
commit 8347aa01fa

@ -68,14 +68,21 @@ struct AppLayerThreadCtx_ {
#endif #endif
}; };
#define MAX_COUNTER_SIZE 64
typedef struct AppLayerCounterNames_ {
char name[MAX_COUNTER_SIZE];
char tx_name[MAX_COUNTER_SIZE];
} AppLayerCounterNames;
typedef struct AppLayerCounters_ { typedef struct AppLayerCounters_ {
char *name;
char *tx_name;
uint16_t counter_id; uint16_t counter_id;
uint16_t counter_tx_id; uint16_t counter_tx_id;
} AppLayerCounters; } AppLayerCounters;
AppLayerCounters applayer_counters[FLOW_PROTO_MAX][ALPROTO_MAX]; /* counter names. Only used at init. */
AppLayerCounterNames applayer_counter_names[FLOW_PROTO_APPLAYER_MAX][ALPROTO_MAX];
/* counter id's. Used that runtime. */
AppLayerCounters applayer_counters[FLOW_PROTO_APPLAYER_MAX][ALPROTO_MAX];
void AppLayerSetupCounters(); void AppLayerSetupCounters();
void AppLayerDeSetupCounters(); void AppLayerDeSetupCounters();
@ -707,48 +714,29 @@ void AppLayerSetupCounters()
for (alproto = 0; alproto < ALPROTO_MAX; alproto++) { for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
if (alprotos[alproto] == 1) { if (alprotos[alproto] == 1) {
char *str = "app_layer.flow."; const char *str = "app_layer.flow.";
char *tx_str = "app_layer.tx."; const char *tx_str = "app_layer.tx.";
char *alproto_str = AppLayerGetProtoName(alproto); const char *alproto_str = AppLayerGetProtoName(alproto);
int alproto_len = strlen(alproto_str) + 1;
uint8_t ipproto_map = FlowGetProtoMapping(ipprotos[ipproto]); uint8_t ipproto_map = FlowGetProtoMapping(ipprotos[ipproto]);
size_t size;
if (AppLayerParserProtoIsRegistered(ipprotos[ipproto], alproto) && if (AppLayerParserProtoIsRegistered(ipprotos[ipproto], alproto) &&
AppLayerParserProtoIsRegistered(other_ipproto, alproto)) AppLayerParserProtoIsRegistered(other_ipproto, alproto))
{ {
size = strlen(str) + alproto_len + strlen(ipproto_suffix); snprintf(applayer_counter_names[ipproto_map][alproto].name,
applayer_counters[ipproto_map][alproto].name = SCMalloc(size); sizeof(applayer_counter_names[ipproto_map][alproto].name),
if (applayer_counters[ipproto_map][alproto].name == NULL) {
return;
}
snprintf(applayer_counters[ipproto_map][alproto].name, size,
"%s%s%s", str, alproto_str, ipproto_suffix); "%s%s%s", str, alproto_str, ipproto_suffix);
if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) { if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) {
size = strlen(tx_str) + alproto_len + strlen(ipproto_suffix); snprintf(applayer_counter_names[ipproto_map][alproto].tx_name,
applayer_counters[ipproto_map][alproto].tx_name = SCMalloc(size); sizeof(applayer_counter_names[ipproto_map][alproto].tx_name),
if (applayer_counters[ipproto_map][alproto].tx_name == NULL) {
return;
}
snprintf(applayer_counters[ipproto_map][alproto].tx_name, size,
"%s%s%s", tx_str, alproto_str, ipproto_suffix); "%s%s%s", tx_str, alproto_str, ipproto_suffix);
} }
} else { } else {
size = strlen(str) + alproto_len; snprintf(applayer_counter_names[ipproto_map][alproto].name,
applayer_counters[ipproto_map][alproto].name = SCMalloc(size); sizeof(applayer_counter_names[ipproto_map][alproto].name),
if (applayer_counters[ipproto_map][alproto].name == NULL) {
return;
}
snprintf(applayer_counters[ipproto_map][alproto].name, size,
"%s%s", str, alproto_str); "%s%s", str, alproto_str);
if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) { if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) {
size = strlen(tx_str) + alproto_len; snprintf(applayer_counter_names[ipproto_map][alproto].tx_name,
applayer_counters[ipproto_map][alproto].tx_name = SCMalloc(size); sizeof(applayer_counter_names[ipproto_map][alproto].tx_name),
if (applayer_counters[ipproto_map][alproto].tx_name == NULL) {
return;
}
snprintf(applayer_counters[ipproto_map][alproto].tx_name, size,
"%s%s", tx_str, alproto_str); "%s%s", tx_str, alproto_str);
} }
} }
@ -771,11 +759,11 @@ void AppLayerRegisterThreadCounters(ThreadVars *tv)
if (alprotos[alproto] == 1) { if (alprotos[alproto] == 1) {
uint8_t ipproto_map = FlowGetProtoMapping(ipprotos[ipproto]); uint8_t ipproto_map = FlowGetProtoMapping(ipprotos[ipproto]);
applayer_counters[ipproto_map][alproto].counter_id = applayer_counters[ipproto_map][alproto].counter_id =
StatsRegisterCounter(applayer_counters[ipproto_map][alproto].name, tv); StatsRegisterCounter(applayer_counter_names[ipproto_map][alproto].name, tv);
if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) { if (AppLayerParserProtocolIsTxAware(ipprotos[ipproto], alproto)) {
applayer_counters[ipproto_map][alproto].counter_tx_id = applayer_counters[ipproto_map][alproto].counter_tx_id =
StatsRegisterCounter(applayer_counters[ipproto_map][alproto].tx_name, tv); StatsRegisterCounter(applayer_counter_names[ipproto_map][alproto].tx_name, tv);
} }
} }
} }
@ -784,28 +772,10 @@ void AppLayerRegisterThreadCounters(ThreadVars *tv)
void AppLayerDeSetupCounters() void AppLayerDeSetupCounters()
{ {
uint8_t ipprotos[] = { IPPROTO_TCP, IPPROTO_UDP }; memset(applayer_counter_names, 0, sizeof(applayer_counter_names));
uint8_t ipproto; memset(applayer_counters, 0, sizeof(applayer_counters));
AppProto alproto;
AppProto alprotos[ALPROTO_MAX];
AppLayerProtoDetectSupportedAppProtocols(alprotos);
for (ipproto = 0; ipproto < IPPROTOS_MAX; ipproto++) {
for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
if (alprotos[alproto] == 1) {
if (applayer_counters[FlowGetProtoMapping(ipprotos[ipproto])][alproto].name) {
SCFree(applayer_counters[FlowGetProtoMapping(ipprotos[ipproto])][alproto].name);
applayer_counters[FlowGetProtoMapping(ipprotos[ipproto])][alproto].name = NULL;
}
}
if (applayer_counters[FlowGetProtoMapping(ipproto)][alproto].tx_name) {
SCFree(applayer_counters[FlowGetProtoMapping(ipproto)][alproto].tx_name);
applayer_counters[FlowGetProtoMapping(ipproto)][alproto].tx_name = NULL;
}
}
}
} }
/***** Unittests *****/ /***** Unittests *****/
#ifdef UNITTESTS #ifdef UNITTESTS

@ -74,6 +74,8 @@ enum {
/* should be last */ /* should be last */
FLOW_PROTO_MAX, FLOW_PROTO_MAX,
}; };
/* max used in app-layer (counters) */
#define FLOW_PROTO_APPLAYER_MAX FLOW_PROTO_UDP + 1
/* /*
* Variables * Variables

Loading…
Cancel
Save