Skip to content

Commit

Permalink
New FilterSetParam function
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Mar 17, 2024
1 parent d253340 commit 9198d94
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/libnfdump/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ typedef struct FilterEngine_s {
uint32_t StartNode;
uint16_t Extended;
int hasGeoDB;
const char *ident;
char *label;
int (*filterFunction)(const struct FilterEngine_s *, recordHandle_t *, const char *);
int (*filterFunction)(const struct FilterEngine_s *, recordHandle_t *);
} FilterEngine_t;

static filterElement_t *FilterTree = NULL;
Expand Down Expand Up @@ -587,13 +588,18 @@ static void InitFilter(void) {
ClearFilter();
} // End of InitFilter

int FilterRecord(void *engine, recordHandle_t *handle, const char *ident, const int hasGeoDB) {
void FilterSetParam(void *engine, const char *ident, const int hasGeoDB) {
FilterEngine_t *filterEngine = (FilterEngine_t *)engine;
filterEngine->hasGeoDB = hasGeoDB;
return filterEngine->filterFunction(filterEngine, handle, ident);
filterEngine->ident = ident;
} // End of FilterSetParam

int FilterRecord(const void *engine, recordHandle_t *handle) {
FilterEngine_t *filterEngine = (FilterEngine_t *)engine;
return filterEngine->filterFunction(filterEngine, handle);
} // End of FilterRecord

static int RunFilterFast(const FilterEngine_t *engine, recordHandle_t *handle, const char *ident) {
static int RunFilterFast(const FilterEngine_t *engine, recordHandle_t *handle) {
uint32_t index = engine->StartNode;
int invert = 0;
int evaluate = 0;
Expand Down Expand Up @@ -639,7 +645,7 @@ static int RunFilterFast(const FilterEngine_t *engine, recordHandle_t *handle, c

} // End of RunFilter

static int RunExtendedFilter(const FilterEngine_t *engine, recordHandle_t *handle, const char *ident) {
static int RunExtendedFilter(const FilterEngine_t *engine, recordHandle_t *handle) {
uint32_t index = engine->StartNode;
int evaluate = 0;
int invert = 0;
Expand Down Expand Up @@ -714,7 +720,7 @@ static int RunExtendedFilter(const FilterEngine_t *engine, recordHandle_t *handl
} break;
case CMP_IDENT: {
char *str = (char *)data.dataPtr;
evaluate = str != NULL && (strcmp(ident, str) == 0 ? 1 : 0);
evaluate = str != NULL && (strcmp(engine->ident, str) == 0 ? 1 : 0);
} break;
case CMP_STRING: {
char *str = (char *)data.dataPtr;
Expand Down
4 changes: 3 additions & 1 deletion src/libnfdump/filter/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ void *CompileFilter(char *FilterSyntax);

void DisposeFilter(void *engine);

int FilterRecord(void *engine, recordHandle_t *handle, const char *ident, const int hasGeoDB);
void FilterSetParam(void *engine, const char *ident, const int hasGeoDB);

int FilterRecord(const void *engine, recordHandle_t *handle);

void DumpEngine(void *arg);

Expand Down
4 changes: 3 additions & 1 deletion src/nfdump/nfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ static stat_record_t process_data(void *engine, char *wfile, int element_stat, i
}
SetIdent(nffile_w, nffile_r->ident);
}
FilterSetParam(engine, nffile_r->ident, outputParams->hasGeoDB);

recordHandle_t *recordHandle = (recordHandle_t *)calloc(1, sizeof(recordHandle_t));
if (!recordHandle) {
Expand Down Expand Up @@ -323,6 +324,7 @@ static stat_record_t process_data(void *engine, char *wfile, int element_stat, i
if (next->stat_record->lastseen > t_last_flow) t_last_flow = next->stat_record->lastseen;
// continue with next file
}
FilterSetParam(engine, nffile_r->ident, outputParams->hasGeoDB);
continue;

} break; // not really needed
Expand Down Expand Up @@ -379,7 +381,7 @@ static stat_record_t process_data(void *engine, char *wfile, int element_stat, i

if (match) {
// filter netflow record with user supplied filter
match = FilterRecord(engine, recordHandle, nffile_r->ident, outputParams->hasGeoDB);
match = FilterRecord(engine, recordHandle);
}
if (match == 0) { // record failed to pass all filters
// go to next record
Expand Down
4 changes: 3 additions & 1 deletion src/nfreplay/nfreplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ static void send_data(void *engine, timeWindow_t *timeWindow, uint32_t limitReco
LogError("Empty file list. No files to process\n");
return;
}
FilterSetParam(engine, nffile->ident, NOGEODB);

peer.send_buffer = malloc(UDP_PACKET_SIZE);
peer.flush = 0;
Expand Down Expand Up @@ -279,6 +280,7 @@ static void send_data(void *engine, timeWindow_t *timeWindow, uint32_t limitReco
done = 1;
LogError("Unexpected end of file list\n");
}
FilterSetParam(engine, nffile->ident, NOGEODB);
// else continue with next file
continue;

Expand Down Expand Up @@ -321,7 +323,7 @@ static void send_data(void *engine, timeWindow_t *timeWindow, uint32_t limitReco

// filter netflow record with user supplied filter
// XXX if (match) match = (*Engine->FilterEngine)(Engine);
if (match) match = FilterRecord(engine, recordHandle, nffile->ident, 0);
if (match) match = FilterRecord(engine, recordHandle);

if (match == 0) { // record failed to pass all filters
// go to next record
Expand Down
12 changes: 11 additions & 1 deletion src/nfsen/nfprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ static void process_data(profile_channel_info_t *channels, unsigned int num_chan
LogError("Empty file list. No files to process");
return;
}
for (int j = 0; j < num_channels; j++) {
// apply profile filter
void *engine = channels[j].engine;
FilterSetParam(engine, nffile->ident, NOGEODB);
}

recordHandle_t *recordHandle = calloc(1, sizeof(recordHandle_t));
if (!recordHandle) {
Expand Down Expand Up @@ -139,6 +144,11 @@ static void process_data(profile_channel_info_t *channels, unsigned int num_chan
continue;
LogError("Unexpected end of file list");
}
for (int j = 0; j < num_channels; j++) {
// apply profile filter
void *engine = channels[j].engine;
FilterSetParam(engine, nffile->ident, NOGEODB);
}

continue;

Expand Down Expand Up @@ -169,7 +179,7 @@ static void process_data(profile_channel_info_t *channels, unsigned int num_chan

// apply profile filter
void *engine = channels[j].engine;
match = FilterRecord(engine, recordHandle, FILE_IDENT(nffile), NOGEODB);
match = FilterRecord(engine, recordHandle);

// if profile filter failed -> next profile
if (!match) continue;
Expand Down
4 changes: 3 additions & 1 deletion src/nfsen/nftrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static data_row *process(void *engine) {
LogError("Empty file list. No files to process\n");
return NULL;
}
FilterSetParam(engine, nffile->ident, NOGEODB);

port_table = (data_row *)calloc(65536, sizeof(data_row));
if (!port_table) {
Expand Down Expand Up @@ -195,6 +196,7 @@ static data_row *process(void *engine) {
done = 1;
LogError("Unexpected end of file list\n");
}
FilterSetParam(engine, nffile->ident, NOGEODB);
// else continue with next file
continue;

Expand All @@ -219,7 +221,7 @@ static data_row *process(void *engine) {
case V3Record: {
processed++;
MapRecordHandle(recordHandle, (recordHeaderV3_t *)record_ptr, processed);
int ret = FilterRecord(engine, recordHandle, nffile->ident, NOGEODB);
int ret = FilterRecord(engine, recordHandle);

if (ret == 0) { // record failed to pass the filter
// increment pointer by number of bytes for netflow record
Expand Down
3 changes: 2 additions & 1 deletion src/test/nftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ static void CheckFilter(char *filter, recordHandle_t *recordHandle, int expect)
} else {
printf("Compiled ok: %s\n", filter);
}
int ret = FilterRecord(engine, recordHandle, NULL, NOGEODB);
FilterSetParam(engine, NULL, NOGEODB);
int ret = FilterRecord(engine, recordHandle);
if (ret != expect) {
printf("*** Filter failed for %s\n", filter);
printf("*** Expected %d, result: %d\n", expect, ret);
Expand Down

0 comments on commit 9198d94

Please sign in to comment.