suricata
app-layer-events.c
Go to the documentation of this file.
1 /* Copyright (C) 2014 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  */
24 
25 #include "suricata-common.h"
26 #include "decode.h"
27 #include "flow.h"
28 #include "app-layer-events.h"
29 #include "app-layer-parser.h"
30 #include "util-enum.h"
31 
32 /* events raised during protocol detection are stored in the
33  * packets storage, not in the flow. */
35  { "APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS",
37  { "APPLAYER_WRONG_DIRECTION_FIRST_DATA",
39  { "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION",
41  { "APPLAYER_PROTO_DETECTION_SKIPPED",
43  { "APPLAYER_NO_TLS_AFTER_STARTTLS",
45  { "APPLAYER_UNEXPECTED_PROTOCOL",
47  { NULL,
48  -1 },
49 };
50 
51 int AppLayerGetEventInfoById(int event_id, const char **event_name,
53 {
54  *event_name = SCMapEnumValueToName(event_id, app_layer_event_pkt_table);
55  if (*event_name == NULL) {
56  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%d\" not present in "
57  "app-layer-event's enum map table.", event_id);
58  /* yes this is fatal */
59  return -1;
60  }
61 
62  *event_type = APP_LAYER_EVENT_TYPE_PACKET;
63 
64  return 0;
65 }
66 
67 int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
68 {
69  *event_id = SCMapEnumNameToValue(event_name, app_layer_event_pkt_table);
70  if (*event_id == -1) {
71  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
72  "app-layer-event's packet event table.", event_name);
73  /* this should be treated as fatal */
74  return -1;
75  }
76 
77  return 0;
78 }
79 
80 #define DECODER_EVENTS_BUFFER_STEPS 8
81 
82 /**
83  * \brief Set an app layer decoder event.
84  *
85  * \param sevents Pointer to a AppLayerDecoderEvents pointer. If *sevents is NULL
86  * memory will be allocated.
87  * \param event The event to be stored.
88  */
90 {
91  if (*sevents == NULL) {
93  if (new_devents == NULL)
94  return;
95 
96  memset(new_devents, 0, sizeof(AppLayerDecoderEvents));
97  *sevents = new_devents;
98 
99  }
100  if ((*sevents)->cnt == UCHAR_MAX) {
101  /* we're full */
102  return;
103  }
104  if ((*sevents)->cnt == (*sevents)->events_buffer_size) {
105  int steps = DECODER_EVENTS_BUFFER_STEPS;
106  if (UCHAR_MAX - (*sevents)->cnt < steps)
107  steps = UCHAR_MAX - (*sevents)->cnt < steps;
108 
109  void *ptr = SCRealloc((*sevents)->events,
110  ((*sevents)->cnt + steps) * sizeof(uint8_t));
111  if (ptr == NULL) {
112  /* couldn't grow buffer, but no reason to free old
113  * so we keep the events that may already be here */
114  return;
115  }
116  (*sevents)->events = ptr;
117  (*sevents)->events_buffer_size += steps;
118  }
119 
120  (*sevents)->events[(*sevents)->cnt++] = event;
121 }
122 
123 /**
124  * \brief Set an app layer decoder event.
125  *
126  * \param f Pointer to a flow containing DecoderEvents pointer head. If
127  * the head points to a DecoderEvents instance, a
128  * new instance would be created and the pointer head would
129  * would be updated with this new instance
130  * \param event The event to be stored.
131  */
132 void AppLayerDecoderEventsSetEvent(Flow *f, uint8_t event)
133 {
136  AppLayerDecoderEventsSetEventRaw(&events, event);
137  if (events != new)
139 }
140 
142 {
143  if (events != NULL)
144  events->cnt = 0;
145 }
146 
147 
149 {
150  if (events && *events != NULL) {
151  if ((*events)->events != NULL)
152  SCFree((*events)->events);
153  SCFree(*events);
154  *events = NULL;
155  }
156 }
157 
AppLayerDecoderEvents * AppLayerParserGetDecoderEvents(AppLayerParserState *pstate)
enum AppLayerEventType_ AppLayerEventType
const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
Maps an enum value to a string name, from the supplied table.
Definition: util-enum.c:69
uint32_t event_type
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:41
SCEnumCharMap app_layer_event_pkt_table[]
Data structure to store app layer decoder events.
void AppLayerParserSetDecoderEvents(AppLayerParserState *pstate, AppLayerDecoderEvents *devents)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void AppLayerDecoderEventsSetEvent(Flow *f, uint8_t event)
Set an app layer decoder event.
#define SCRealloc(x, a)
Definition: util-mem.h:182
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
int AppLayerGetEventInfoById(int event_id, const char **event_name, AppLayerEventType *event_type)
#define SCMalloc(a)
Definition: util-mem.h:166
#define SCFree(a)
Definition: util-mem.h:228
int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
#define DECODER_EVENTS_BUFFER_STEPS
Flow data structure.
Definition: flow.h:325
AppLayerParserState * alparser
Definition: flow.h:437
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)