Skip to content
32 changes: 24 additions & 8 deletions src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ typedef struct AppLayerProtoDetectCtx_ {
/* Indicates the protocols that have registered themselves
* for protocol detection. This table is independent of the
* ipproto. */
const char *alproto_names[ALPROTO_MAX];
const char **alproto_names;

/* Protocol expectations, like ftp-data on tcp */
uint8_t *expectation_proto;
} AppLayerProtoDetectCtx;

typedef struct AppLayerProtoDetectAliases_ {
Expand Down Expand Up @@ -1718,6 +1721,15 @@ int AppLayerProtoDetectSetup(void)
}
}

alpd_ctx.alproto_names = SCCalloc(ALPROTO_MAX, sizeof(char *));
if (unlikely(alpd_ctx.alproto_names == NULL)) {
FatalError("Unable to alloc alproto_names.");
}
// to realloc when dynamic protos are added
alpd_ctx.expectation_proto = SCCalloc(ALPROTO_MAX, sizeof(uint8_t));
if (unlikely(alpd_ctx.expectation_proto == NULL)) {
FatalError("Unable to alloc expectation_proto.");
Comment on lines +1726 to +1731
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these errors have some more user-friendly part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so ?
This is a unique message showing clearly the problem, is it not ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I guess, maybe. One could always copy the error and share that with us, as many users probably wouldn't try to fix something like that...

}
AppLayerExpectationSetup();

SCReturnInt(0);
Expand Down Expand Up @@ -1749,6 +1761,11 @@ int AppLayerProtoDetectDeSetup(void)
}
}

SCFree(alpd_ctx.alproto_names);
alpd_ctx.alproto_names = NULL;
SCFree(alpd_ctx.expectation_proto);
alpd_ctx.expectation_proto = NULL;

SpmDestroyGlobalThreadCtx(alpd_ctx.spm_global_thread_ctx);

AppLayerProtoDetectFreeAliases();
Expand All @@ -1762,6 +1779,7 @@ void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_n
{
SCEnter();

// should have just been realloced when dynamic protos is added
if (alpd_ctx.alproto_names[alproto] == NULL)
alpd_ctx.alproto_names[alproto] = alproto_name;

Expand Down Expand Up @@ -2111,27 +2129,25 @@ void AppLayerProtoDetectSupportedAppProtocols(AppProto *alprotos)
SCReturn;
}

uint8_t expectation_proto[ALPROTO_MAX];

static void AppLayerProtoDetectPEGetIpprotos(AppProto alproto,
uint8_t *ipprotos)
{
if (expectation_proto[alproto] == IPPROTO_TCP) {
if (alpd_ctx.expectation_proto[alproto] == IPPROTO_TCP) {
ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
}
if (expectation_proto[alproto] == IPPROTO_UDP) {
if (alpd_ctx.expectation_proto[alproto] == IPPROTO_UDP) {
ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
}
}

void AppLayerRegisterExpectationProto(uint8_t proto, AppProto alproto)
{
if (expectation_proto[alproto]) {
if (proto != expectation_proto[alproto]) {
if (alpd_ctx.expectation_proto[alproto]) {
if (proto != alpd_ctx.expectation_proto[alproto]) {
SCLogError("Expectation on 2 IP protocols are not supported");
}
}
expectation_proto[alproto] = proto;
alpd_ctx.expectation_proto[alproto] = proto;
}

/***** Unittests *****/
Expand Down
11 changes: 10 additions & 1 deletion src/app-layer-frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@
struct FrameConfig {
SC_ATOMIC_DECLARE(uint64_t, types);
};
static struct FrameConfig frame_config[ALPROTO_MAX];
static struct FrameConfig *frame_config;

void FrameConfigInit(void)
{
frame_config = SCCalloc(ALPROTO_MAX, sizeof(struct FrameConfig));
if (unlikely(frame_config == NULL)) {
FatalError("Unable to alloc frame_config.");
}
for (AppProto p = 0; p < ALPROTO_MAX; p++) {
SC_ATOMIC_INIT(frame_config[p].types);
}
}

void FrameConfigDeInit(void)
{
SCFree(frame_config);
}

void FrameConfigEnableAll(void)
{
const uint64_t bits = UINT64_MAX;
Expand Down
1 change: 1 addition & 0 deletions src/app-layer-frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ FramesContainer *AppLayerFramesGetContainer(Flow *f);
FramesContainer *AppLayerFramesSetupContainer(Flow *f);

void FrameConfigInit(void);
void FrameConfigDeInit(void);
void FrameConfigEnableAll(void);
void FrameConfigEnable(const AppProto p, const uint8_t type);

Expand Down
Loading