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 "rust.h"
28 
30 #define ARRAY_CAP_STEP 16
32 
33 typedef struct AppProtoStringTuple {
35  const char *str;
37 
39 
40 const char *AppProtoToString(AppProto alproto)
41 {
42  const char *proto_name = NULL;
43  switch (alproto) {
44  // special cases
45  case ALPROTO_HTTP1:
46  proto_name = "http";
47  break;
48  case ALPROTO_HTTP:
49  proto_name = "http_any";
50  break;
51  default:
52  if (alproto < g_alproto_max) {
53  BUG_ON(g_alproto_strings[alproto].alproto != alproto);
54  proto_name = g_alproto_strings[alproto].str;
55  }
56  }
57  return proto_name;
58 }
59 
60 AppProto StringToAppProto(const char *proto_name)
61 {
62  if (proto_name == NULL)
63  return ALPROTO_UNKNOWN;
64 
65  // We could use a Multi Pattern Matcher
66  for (size_t i = 0; i < g_alproto_max; i++) {
67  if (strcmp(proto_name, g_alproto_strings[i].str) == 0)
68  return g_alproto_strings[i].alproto;
69  }
70 
71  return ALPROTO_UNKNOWN;
72 }
73 
74 void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
75 {
76  if (alproto < ALPROTO_MAX_STATIC) {
77  if (g_alproto_strings == NULL) {
79  if (g_alproto_strings == NULL) {
80  FatalError("Unable to allocate g_alproto_strings");
81  }
82  }
83  } else if (alproto == g_alproto_max) {
85  void *tmp = SCRealloc(g_alproto_strings,
87  if (tmp == NULL) {
88  FatalError("Unable to reallocate g_alproto_strings");
89  }
91  g_alproto_strings = tmp;
92  }
93  g_alproto_max++;
94  }
95  g_alproto_strings[alproto].str = proto_name;
96  g_alproto_strings[alproto].alproto = alproto;
97 }
AppProtoStringTuple
Definition: app-layer-protos.c:33
AppProtoStringTuple::alproto
AppProto alproto
Definition: app-layer-protos.c:34
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:40
g_alproto_strings
AppProtoStringTuple * g_alproto_strings
Definition: app-layer-protos.c:38
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:29
AppProtoRegisterProtoString
void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
Definition: app-layer-protos.c:74
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:60
AppProtoStringTuple::str
const char * str
Definition: app-layer-protos.c:35
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
ARRAY_CAP_STEP
#define ARRAY_CAP_STEP
Definition: app-layer-protos.c:30
suricata-common.h
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:36
AppProtoStringTuple
struct AppProtoStringTuple AppProtoStringTuple
FatalError
#define FatalError(...)
Definition: util-debug.h:502
str
#define str(s)
Definition: suricata-common.h:291
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:76
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
app-layer-protos.h
g_alproto_strings_cap
AppProto g_alproto_strings_cap
Definition: app-layer-protos.c:31
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53