Skip to content

Commit

Permalink
(#1521) put all allocs/deallocs for EVT_proto in evtutils.c
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcheli committed Jun 26, 2023
1 parent 63b8cae commit 72aa7e4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
65 changes: 59 additions & 6 deletions src/evtutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include "scopestdlib.h"
#include "state_private.h"

protocol_info *
evtProtoCreate(void)

static protocol_info *
evtProtoCreateHttpBase(void)
{
protocol_info *proto = scope_calloc(1, sizeof(struct protocol_info_t));
http_post *post = scope_calloc(1, sizeof(struct http_post_t));
protocol_info *proto = scope_calloc(1, sizeof(protocol_info));
http_post *post = scope_calloc(1, sizeof(http_post));
if (!proto || !post) {
if (post) scope_free(post);
if (proto) scope_free(proto);
Expand All @@ -23,6 +24,58 @@ evtProtoCreate(void)
return proto;
}

protocol_info *
evtProtoCreateHttp1(bool isResponse)
{
protocol_info * proto = evtProtoCreateHttpBase();
if (!proto) {
DBG(NULL);
return NULL;
}

proto->ptype = (isResponse) ? EVT_HRES : EVT_HREQ;

return proto;
}

protocol_info *
evtProtoCreateHttp2Frame(uint32_t frameLen)
{
protocol_info *proto = evtProtoCreateHttpBase();
char *frame = scope_malloc(frameLen);
if (!proto || !frame) {
DBG(NULL);
if (proto) evtProtoDelete(proto);
if (frame) scope_free(frame);
return NULL;
}

proto->ptype = EVT_H2FRAME;
http_post *post = (http_post *)proto->data;
post->hdr = frame;

return proto;
}

protocol_info *
evtProtoCreateDetect(const char * const protocolName)
{
protocol_info *proto = scope_calloc(1, sizeof(protocol_info));
char *protname = scope_strdup(protocolName);
if (!proto || !protname) {
DBG(NULL);
if (protname) scope_free(protname);
if (proto) scope_free(proto);
return NULL;
}

proto->evtype = EVT_PROTO;
proto->ptype = EVT_DETECT;
proto->data = protname;

return proto;
}

bool
evtProtoDelete(protocol_info *proto)
{
Expand Down Expand Up @@ -86,8 +139,8 @@ evtDelete(evt_type *event)
case EVT_PROTO:
{
protocol_info *proto = (protocol_info *)event;
// Alloc'd in evtProtoCreate (http 1&2) or
// setProtocol (protocolDetection)
// Alloc'd in evtProtoCreateHttp1, evtProtoCreateHttp2Frame, or
// evtProtoCreateDetect
evtProtoDelete(proto);
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/evtutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
// At this time, we're just starting with events, but hope to migrate
// code here for logs/console and payloads over time.

protocol_info * evtProtoCreate(void);
protocol_info * evtProtoCreateHttp1(bool);
protocol_info * evtProtoCreateHttp2Frame(uint32_t);
protocol_info * evtProtoCreateDetect(const char * const);
bool evtProtoDelete(protocol_info *proto);

bool evtDelete(evt_type *event);
Expand Down
13 changes: 3 additions & 10 deletions src/httpstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ reportHttp1(http_state_t *httpstate)
{
if (!httpstate || !httpstate->hdr || !httpstate->hdrlen) return -1;

protocol_info *proto = evtProtoCreate();
protocol_info *proto = evtProtoCreateHttp1(httpstate->isResponse);
if (!proto) {
// Bummer! We're losing info.
DBG(NULL);
Expand All @@ -245,7 +245,6 @@ reportHttp1(http_state_t *httpstate)
int isSend = (httpstate->id.src == NETTX) || (httpstate->id.src == TLSTX);

// Set proto info
proto->ptype = (httpstate->isResponse) ? EVT_HRES : EVT_HREQ;
// We're a server if we 1) sent a response or 2) received a request
proto->isServer = (isSend && httpstate->isResponse) || (!isSend && !httpstate->isResponse);
proto->len = httpstate->hdrlen;
Expand Down Expand Up @@ -297,12 +296,9 @@ reportHttp2(http_state_t *state, net_info *net, http_buf_t *stash,
return FALSE;
}

protocol_info *proto = evtProtoCreate();
char *frame = scope_malloc(frameLen);
if (!proto || !frame) {
protocol_info *proto = evtProtoCreateHttp2Frame(frameLen);
if (!proto) {
scopeLogError("ERROR: failed to allocate protocol object");
if (proto) evtProtoDelete(proto);
if (frame) scope_free(frame);
DBG(NULL);
return FALSE;
}
Expand All @@ -311,16 +307,13 @@ reportHttp2(http_state_t *state, net_info *net, http_buf_t *stash,
post->ssl = state->id.isSsl;
post->start_duration = getTime();
post->id = state->id.uid;
post->hdr = frame;
if (stash->len) {
scope_memcpy(post->hdr, stash->buf, stash->len);
scope_memcpy(post->hdr + stash->len, buf, frameLen - stash->len);
} else {
scope_memcpy(post->hdr, buf, frameLen);
}


proto->ptype = EVT_H2FRAME;
// Unlike in the HTTP/1 case, we're sending TRUE here if the frame was
// sent, not if we're the server. We haven't parsed the frame to know if
// it's a request or response yet so we're sending half of the isServer
Expand Down
6 changes: 2 additions & 4 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "com.h"
#include "dbg.h"
#include "dns.h"
#include "evtutils.h"
#include "httpstate.h"
#include "metriccapture.h"
#include "mtcformat.h"
Expand Down Expand Up @@ -984,20 +985,17 @@ setProtocol(int sockfd, protocol_def_t *protoDef, net_info *net, char *buf, size
}

if (protoDef->detect && ctlEvtSourceEnabled(g_ctl, CFG_SRC_NET)) {
if ((proto = scope_calloc(1, sizeof(struct protocol_info_t))) == NULL)
if ((proto = evtProtoCreateDetect(protoDef->protname)) == NULL)
{
if (cpdata)
scope_free(cpdata);
if (match_data)
pcre2_match_data_free(match_data);
return FALSE;
}
proto->evtype = EVT_PROTO;
proto->ptype = EVT_DETECT;
proto->len = sizeof(protocol_def_t);
proto->fd = sockfd;
if (net) proto->uid = net->uid;
proto->data = (char *)scope_strdup(protoDef->protname);
cmdPostEvent(g_ctl, (char *)proto);
}

Expand Down

0 comments on commit 72aa7e4

Please sign in to comment.