mirror of https://github.com/OISF/suricata
app-layer-event: refactor
Move app layer event handling into app-layer-event.[ch]. Convert 'Set' macro's to functions. Get rid of duplication in Set and SetRaw. Set now calls SetRaw. Fix potentential int overflow condition in the event storage. Update callers.pull/795/head
parent
4ce53753bc
commit
347c0df9c4
@ -0,0 +1,137 @@
|
|||||||
|
/* Copyright (C) 2014 Open Information Security Foundation
|
||||||
|
*
|
||||||
|
* You can copy, redistribute or modify this Program under the terms of
|
||||||
|
* the GNU General Public License version 2 as published by the Free
|
||||||
|
* Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* version 2 along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file
|
||||||
|
*
|
||||||
|
* \author Victor Julien <victor@inliniac.net>
|
||||||
|
* \author Anoop Saldanha <anoopsaldanha@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "suricata-common.h"
|
||||||
|
#include "decode.h"
|
||||||
|
#include "flow.h"
|
||||||
|
#include "app-layer-events.h"
|
||||||
|
#include "app-layer-parser.h"
|
||||||
|
#include "util-enum.h"
|
||||||
|
|
||||||
|
/* events raised during protocol detection are stored in the
|
||||||
|
* packets storage, not in the flow. */
|
||||||
|
SCEnumCharMap app_layer_event_pkt_table[ ] = {
|
||||||
|
{ "APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS",
|
||||||
|
APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS },
|
||||||
|
{ "APPLAYER_WRONG_DIRECTION_FIRST_DATA",
|
||||||
|
APPLAYER_WRONG_DIRECTION_FIRST_DATA },
|
||||||
|
{ "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION",
|
||||||
|
APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION },
|
||||||
|
{ "APPLAYER_PROTO_DETECTION_SKIPPED",
|
||||||
|
APPLAYER_PROTO_DETECTION_SKIPPED },
|
||||||
|
{ NULL,
|
||||||
|
-1 },
|
||||||
|
};
|
||||||
|
|
||||||
|
int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
|
||||||
|
{
|
||||||
|
*event_id = SCMapEnumNameToValue(event_name, app_layer_event_pkt_table);
|
||||||
|
if (*event_id == -1) {
|
||||||
|
SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
|
||||||
|
"app-layer-event's packet event table.", event_name);
|
||||||
|
/* this should be treated as fatal */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DECODER_EVENTS_BUFFER_STEPS 8
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Set an app layer decoder event.
|
||||||
|
*
|
||||||
|
* \param sevents Pointer to a AppLayerDecoderEvents pointer. If *sevents is NULL
|
||||||
|
* memory will be allocated.
|
||||||
|
* \param event The event to be stored.
|
||||||
|
*/
|
||||||
|
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
|
||||||
|
{
|
||||||
|
if (*sevents == NULL) {
|
||||||
|
AppLayerDecoderEvents *new_devents = SCMalloc(sizeof(AppLayerDecoderEvents));
|
||||||
|
if (new_devents == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
memset(new_devents, 0, sizeof(AppLayerDecoderEvents));
|
||||||
|
*sevents = new_devents;
|
||||||
|
|
||||||
|
}
|
||||||
|
if ((*sevents)->cnt == UCHAR_MAX) {
|
||||||
|
/* we're full */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((*sevents)->cnt == (*sevents)->events_buffer_size) {
|
||||||
|
int steps = DECODER_EVENTS_BUFFER_STEPS;
|
||||||
|
if (UCHAR_MAX - (*sevents)->cnt < steps)
|
||||||
|
steps = UCHAR_MAX - (*sevents)->cnt < steps;
|
||||||
|
|
||||||
|
void *ptr = SCRealloc((*sevents)->events,
|
||||||
|
((*sevents)->cnt + steps) * sizeof(uint8_t));
|
||||||
|
if (ptr == NULL) {
|
||||||
|
/* couldn't grow buffer, but no reason to free old
|
||||||
|
* so we keep the events that may already be here */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
(*sevents)->events = ptr;
|
||||||
|
(*sevents)->events_buffer_size += steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*sevents)->events[(*sevents)->cnt++] = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Set an app layer decoder event.
|
||||||
|
*
|
||||||
|
* \param f Pointer to a flow containing DecoderEvents pointer head. If
|
||||||
|
* the head points to a DecoderEvents instance, a
|
||||||
|
* new instance would be created and the pointer head would
|
||||||
|
* would be updated with this new instance
|
||||||
|
* \param event The event to be stored.
|
||||||
|
*/
|
||||||
|
void AppLayerDecoderEventsSetEvent(Flow *f, uint8_t event)
|
||||||
|
{
|
||||||
|
AppLayerDecoderEvents *events = AppLayerParserGetDecoderEvents(f->alparser);
|
||||||
|
AppLayerDecoderEvents *new = events;
|
||||||
|
AppLayerDecoderEventsSetEventRaw(&events, event);
|
||||||
|
if (events != new)
|
||||||
|
AppLayerParserSetDecoderEvents(f->alparser, events);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)
|
||||||
|
{
|
||||||
|
if (events != NULL)
|
||||||
|
events->cnt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
|
||||||
|
{
|
||||||
|
if (events && *events != NULL) {
|
||||||
|
if ((*events)->events != NULL)
|
||||||
|
SCFree((*events)->events);
|
||||||
|
SCFree(*events);
|
||||||
|
*events = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
/* Copyright (C) 2014 Open Information Security Foundation
|
||||||
|
*
|
||||||
|
* You can copy, redistribute or modify this Program under the terms of
|
||||||
|
* the GNU General Public License version 2 as published by the Free
|
||||||
|
* Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* version 2 along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file
|
||||||
|
*
|
||||||
|
* \author Victor Julien <victor@inliniac.net>
|
||||||
|
* \author Anoop Saldanha <anoopsaldanha@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __APP_LAYER_EVENTS_H__
|
||||||
|
#define __APP_LAYER_EVENTS_H__
|
||||||
|
|
||||||
|
/* contains fwd declaration of AppLayerDecoderEvents_ */
|
||||||
|
#include "decode.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Data structure to store app layer decoder events.
|
||||||
|
*/
|
||||||
|
struct AppLayerDecoderEvents_ {
|
||||||
|
/* array of events */
|
||||||
|
uint8_t *events;
|
||||||
|
/* number of events in the above buffer */
|
||||||
|
uint8_t cnt;
|
||||||
|
/* current event buffer size */
|
||||||
|
uint8_t events_buffer_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* app layer pkt level events */
|
||||||
|
enum {
|
||||||
|
APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS,
|
||||||
|
APPLAYER_WRONG_DIRECTION_FIRST_DATA,
|
||||||
|
APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION,
|
||||||
|
APPLAYER_PROTO_DETECTION_SKIPPED,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* the event types for app events */
|
||||||
|
typedef enum AppLayerEventType_ {
|
||||||
|
APP_LAYER_EVENT_TYPE_GENERAL = 1,
|
||||||
|
APP_LAYER_EVENT_TYPE_TRANSACTION,
|
||||||
|
APP_LAYER_EVENT_TYPE_PACKET,
|
||||||
|
} AppLayerEventType;
|
||||||
|
|
||||||
|
int AppLayerGetPktEventInfo(const char *event_name, int *event_id);
|
||||||
|
|
||||||
|
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event);
|
||||||
|
void AppLayerDecoderEventsSetEvent(Flow *f, uint8_t event);
|
||||||
|
|
||||||
|
static inline int AppLayerDecoderEventsIsEventSet(AppLayerDecoderEvents *devents,
|
||||||
|
uint8_t event)
|
||||||
|
{
|
||||||
|
if (devents == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
int cnt = devents->cnt;
|
||||||
|
for (i = 0; i < cnt; i++) {
|
||||||
|
if (devents->events[i] == event)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events);
|
||||||
|
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events);
|
||||||
|
|
||||||
|
#endif /* __APP_LAYER_EVENTS_H__ */
|
||||||
|
|
||||||
Loading…
Reference in New Issue