Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/app-layer-protos.c
Line
Count
Source
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
30
AppProto g_alproto_max = ALPROTO_MAX_STATIC;
31
81
#define ARRAY_CAP_STEP 16
32
AppProto g_alproto_strings_cap = ALPROTO_MAX_STATIC;
33
34
typedef struct AppProtoStringTuple {
35
    AppProto alproto;
36
    const char *str;
37
} AppProtoStringTuple;
38
39
AppProtoStringTuple *g_alproto_strings = NULL;
40
41
const char *AppProtoToStringRaw(AppProto alproto)
42
3.04k
{
43
3.04k
    const char *proto_name = NULL;
44
3.04k
    if (alproto < g_alproto_max) {
45
3.04k
        DEBUG_VALIDATE_BUG_ON(g_alproto_strings[alproto].alproto != alproto);
46
3.04k
        proto_name = g_alproto_strings[alproto].str;
47
3.04k
    }
48
3.04k
    return proto_name;
49
3.04k
}
50
51
const char *AppProtoToString(AppProto alproto)
52
1.46M
{
53
1.46M
    const char *proto_name = NULL;
54
1.46M
    switch (alproto) {
55
        // special cases
56
326k
        case ALPROTO_HTTP1:
57
326k
            proto_name = "http";
58
326k
            break;
59
5.12k
        case ALPROTO_HTTP:
60
5.12k
            proto_name = "http_any";
61
5.12k
            break;
62
1.13M
        default:
63
1.13M
            if (alproto < g_alproto_max) {
64
1.13M
                DEBUG_VALIDATE_BUG_ON(g_alproto_strings[alproto].alproto != alproto);
65
1.13M
                proto_name = g_alproto_strings[alproto].str;
66
1.13M
            }
67
1.46M
    }
68
1.46M
    return proto_name;
69
1.46M
}
70
71
AppProto StringToAppProto(const char *proto_name)
72
1.08M
{
73
1.08M
    if (proto_name == NULL)
74
0
        return ALPROTO_UNKNOWN;
75
76
    // We could use a Multi Pattern Matcher
77
25.4M
    for (size_t i = 0; i < g_alproto_max; i++) {
78
25.3M
        if (strcmp(proto_name, g_alproto_strings[i].str) == 0)
79
1.01M
            return g_alproto_strings[i].alproto;
80
25.3M
    }
81
82
71.7k
    return ALPROTO_UNKNOWN;
83
1.08M
}
84
85
AppProto AppProtoNewProtoFromString(const char *proto_name)
86
81
{
87
81
    AppProtoRegisterProtoString(g_alproto_max, proto_name);
88
81
    return g_alproto_max - 1;
89
81
}
90
91
void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
92
3.28k
{
93
3.28k
    if (alproto < ALPROTO_MAX_STATIC) {
94
3.20k
        if (g_alproto_strings == NULL) {
95
81
            g_alproto_strings = SCCalloc(g_alproto_strings_cap, sizeof(AppProtoStringTuple));
96
81
            if (g_alproto_strings == NULL) {
97
0
                FatalError("Unable to allocate g_alproto_strings");
98
0
            }
99
81
        }
100
3.20k
    } else if (alproto == g_alproto_max) {
101
81
        if (g_alproto_max == g_alproto_strings_cap) {
102
81
            void *tmp = SCRealloc(g_alproto_strings,
103
81
                    sizeof(AppProtoStringTuple) * (g_alproto_strings_cap + ARRAY_CAP_STEP));
104
81
            if (tmp == NULL) {
105
0
                FatalError("Unable to reallocate g_alproto_strings");
106
0
            }
107
81
            g_alproto_strings_cap += ARRAY_CAP_STEP;
108
81
            g_alproto_strings = tmp;
109
81
        }
110
81
        g_alproto_max++;
111
81
        SCAppLayerParserReallocCtx(alproto);
112
81
    }
113
3.28k
    g_alproto_strings[alproto].str = proto_name;
114
3.28k
    g_alproto_strings[alproto].alproto = alproto;
115
3.28k
}