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