Skip to content

Commit

Permalink
Add new option functions in nfconf and wire nfpcapd options
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Aug 31, 2024
1 parent 14b2179 commit 87a9e67
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
35 changes: 34 additions & 1 deletion src/libnffile/conf/nfconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,37 @@ int SetNameserver(char *ns) {
_res.nscount = 1;
return 1;

} // End of set_nameserver
} // End of set_nameserver

int OptSetBool(option_t *optionList, char *name, int valBool) {
int i = 0;
while (optionList[i].name != NULL) {
if (strcmp(optionList[i].name, name) == 0) {
optionList[i].valBool = valBool;
optionList[i].flags = OPTSET;
return 1;
}
i++;
}
return 0;
} // End of OptSetBool

int OptGetBool(option_t *optionList, char *name, int *valBool) {
int i = 0;
while (optionList[i].name != NULL) {
if (strcmp(optionList[i].name, name) == 0) {
if (optionList[i].flags == OPTDEFAULT) {
char confName[32] = "opt.";
strcat(confName, optionList[i].name);
int confBool = ConfGetValue(confName);
*valBool = confBool;
return 1;
} else {
*valBool = optionList[i].valBool;
return 1;
}
}
i++;
}
return 0;
} // End of OptGetBool
16 changes: 16 additions & 0 deletions src/libnffile/conf/nfconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@

#define NOCONF "none"

typedef enum { OPTDEFAULT, OPTSET } optFlags;
typedef struct option_s {
char *name;
union {
int valBool;
int64_t valInt64;
uint64_t valUint64;
char *valString;
};
optFlags flags;
} option_t;

int ConfOpen(char *filename, char *section);

int ConfGetFormatEntry(char *format, char **key, char **value);
Expand All @@ -50,4 +62,8 @@ int SetNameserver(char *ns);

void ConfInventory(char *confFile);

int OptSetBool(option_t *optionList, char *name, int valBool);

int OptGetBool(option_t *optionList, char *name, int *valBool);

#endif
5 changes: 5 additions & 0 deletions src/libnffile/conf/nfdump.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ fmt.pflog = "%ts %pfact %pfrea %pfdir on %pfifn %pfrule %pr %sap -> %dap %pkt
# MAXWORKERS
# see maxworkers in section [nfdump]
# maxworkers = 16

[nfpcapd]
# define -o options
# opt.fat = 1
# opt.payload = 1
44 changes: 28 additions & 16 deletions src/nfpcapd/nfpcapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ static int launcher_pid;
static pthread_mutex_t m_done = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t terminate = PTHREAD_COND_INITIALIZER;

uint32_t linktype;
uint32_t linkoffset;
static option_t nfpcapdOption[] = {
{.name = "fat", .valBool = 0, .flags = OPTDEFAULT}, {.name = "payload", .valBool = 0, .flags = OPTDEFAULT}, {.name = NULL}};

/*
* Function prototypes
Expand All @@ -116,7 +116,7 @@ static int setup_pcap_file(packetParam_t *param, char *pcap_file, char *filter,

static void WaitDone(void);

static int scanOptions(flowParam_t *flowParam, char *options);
static int scanOptions(option_t *optionList, char *options);

/*
* Functions
Expand Down Expand Up @@ -192,7 +192,7 @@ static int setup_pcap_file(packetParam_t *param, char *pcap_file, char *filter,
}
}

linktype = pcap_datalink(handle);
int linktype = pcap_datalink(handle);
switch (linktype) {
case DLT_RAW:
case DLT_PPP:
Expand Down Expand Up @@ -258,23 +258,33 @@ static void WaitDone(void) {

} // End of WaitDone

static int scanOptions(flowParam_t *flowParam, char *options) {
static int scanOptions(option_t *optionList, char *options) {
if (options == NULL) return 1;

char *option = strtok(options, ",");
while (option != NULL) {
if (strncasecmp(option, "fat", 4) == 0) {
flowParam->extendedFlow = 1;
dbg_printf("Found extended flow option\n");
} else if (strncasecmp(option, "payload", 8) == 0) {
flowParam->addPayload = 1;
dbg_printf("Found payload option\n");
} else {
int valBool = 1;
char *eq = strchr(option, '=');
if (eq) {
*eq++ = '\0';
switch (eq[0]) {
case '0':
valBool = 0;
break;
case '1':
valBool = 1;
break;
default:
LogError("Invalid bool value: %s", eq[0] ? eq : "empty value");
}
}
if (OptSetBool(optionList, option, valBool) == 0) {
LogError("Unknown option: %s", option);
return -1;
return 0;
}
option = strtok(NULL, ",");
}

return 0;
return 1;

} // End of scanOption

Expand Down Expand Up @@ -577,9 +587,11 @@ int main(int argc, char *argv[]) {
flowParam.extensionFormat = time_extension;
packetParam.doDedup = doDedup;

if (options && scanOptions(&flowParam, options) < 0) {
if (scanOptions(nfpcapdOption, options) == 0) {
exit(EXIT_FAILURE);
}
OptGetBool(nfpcapdOption, "fat", &flowParam.extendedFlow);
OptGetBool(nfpcapdOption, "payload", &flowParam.addPayload);

if ((datadir && sendHost) || (!datadir && !sendHost)) {
LogError("Specify either a local directory or a remote host to dump flows.");
Expand Down

0 comments on commit 87a9e67

Please sign in to comment.