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
37
39
40const 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 DEBUG_VALIDATE_BUG_ON(g_alproto_strings[alproto].alproto != alproto);
54 proto_name = g_alproto_strings[alproto].str;
55 }
56 }
57 return proto_name;
58}
59
60AppProto 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
74AppProto AppProtoNewProtoFromString(const char *proto_name)
75{
77 return g_alproto_max - 1;
78}
79
80void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
81{
82 if (alproto < ALPROTO_MAX_STATIC) {
83 if (g_alproto_strings == NULL) {
85 if (g_alproto_strings == NULL) {
86 FatalError("Unable to allocate g_alproto_strings");
87 }
88 }
89 } else if (alproto == g_alproto_max) {
91 void *tmp = SCRealloc(g_alproto_strings,
93 if (tmp == NULL) {
94 FatalError("Unable to reallocate g_alproto_strings");
95 }
98 }
100 }
101 g_alproto_strings[alproto].str = proto_name;
102 g_alproto_strings[alproto].alproto = alproto;
103}
AppProto g_alproto_strings_cap
AppProtoStringTuple * g_alproto_strings
AppProto g_alproto_max
#define ARRAY_CAP_STEP
AppProto AppProtoNewProtoFromString(const char *proto_name)
void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
uint16_t AppProto
@ ALPROTO_HTTP
@ ALPROTO_MAX_STATIC
@ ALPROTO_UNKNOWN
@ ALPROTO_HTTP1
#define str(s)
#define FatalError(...)
Definition util-debug.h:510
#define SCRealloc(ptr, sz)
Definition util-mem.h:50
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define DEBUG_VALIDATE_BUG_ON(exp)