suricata
app-layer-protos.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 "app-layer-protos.h"
27 #include "app-layer-parser.h"
28 #include "rust.h"
29 
31 #define ARRAY_CAP_STEP 16
33 
34 typedef struct AppProtoStringTuple {
36  const char *str;
38 
40 
41 const char *AppProtoToString(AppProto alproto)
42 {
43  const char *proto_name = NULL;
44  switch (alproto) {
45  // special cases
46  case ALPROTO_HTTP1:
47  proto_name = "http";
48  break;
49  case ALPROTO_HTTP:
50  proto_name = "http_any";
51  break;
52  default:
53  if (alproto < g_alproto_max) {
54  DEBUG_VALIDATE_BUG_ON(g_alproto_strings[alproto].alproto != alproto);
55  proto_name = g_alproto_strings[alproto].str;
56  }
57  }
58  return proto_name;
59 }
60 
61 AppProto StringToAppProto(const char *proto_name)
62 {
63  if (proto_name == NULL)
64  return ALPROTO_UNKNOWN;
65 
66  // We could use a Multi Pattern Matcher
67  for (size_t i = 0; i < g_alproto_max; i++) {
68  if (strcmp(proto_name, g_alproto_strings[i].str) == 0)
69  return g_alproto_strings[i].alproto;
70  }
71 
72  return ALPROTO_UNKNOWN;
73 }
74 
75 AppProto AppProtoNewProtoFromString(const char *proto_name)
76 {
78  return g_alproto_max - 1;
79 }
80 
81 void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
82 {
83  if (alproto < ALPROTO_MAX_STATIC) {
84  if (g_alproto_strings == NULL) {
86  if (g_alproto_strings == NULL) {
87  FatalError("Unable to allocate g_alproto_strings");
88  }
89  }
90  } else if (alproto == g_alproto_max) {
92  void *tmp = SCRealloc(g_alproto_strings,
94  if (tmp == NULL) {
95  FatalError("Unable to reallocate g_alproto_strings");
96  }
98  g_alproto_strings = tmp;
99  }
100  g_alproto_max++;
102  }
103  g_alproto_strings[alproto].str = proto_name;
104  g_alproto_strings[alproto].alproto = alproto;
105 }
AppProtoStringTuple
Definition: app-layer-protos.c:34
AppProtoStringTuple::alproto
AppProto alproto
Definition: app-layer-protos.c:35
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:41
g_alproto_strings
AppProtoStringTuple * g_alproto_strings
Definition: app-layer-protos.c:39
rust.h
ALPROTO_MAX_STATIC
@ ALPROTO_MAX_STATIC
Definition: app-layer-protos.h:79
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:30
AppProtoRegisterProtoString
void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
Definition: app-layer-protos.c:81
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:61
AppProtoStringTuple::str
const char * str
Definition: app-layer-protos.c:36
app-layer-parser.h
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
ARRAY_CAP_STEP
#define ARRAY_CAP_STEP
Definition: app-layer-protos.c:31
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
AppProtoStringTuple
struct AppProtoStringTuple AppProtoStringTuple
FatalError
#define FatalError(...)
Definition: util-debug.h:514
str
#define str(s)
Definition: suricata-common.h:308
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
app-layer-protos.h
AppProtoNewProtoFromString
AppProto AppProtoNewProtoFromString(const char *proto_name)
Definition: app-layer-protos.c:75
SCAppLayerParserReallocCtx
int SCAppLayerParserReallocCtx(AppProto alproto)
Definition: app-layer-parser.c:1739
g_alproto_strings_cap
AppProto g_alproto_strings_cap
Definition: app-layer-protos.c:32
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102