Optimizing main loop (#35)

* Mem leak fix

Memleak fix for strdup()

* Typo fix

forgot ;

* Optimizing Main Loop

Filtersmap will only be updated if there is a newer version of our config.

* Include sys/stat.h

* Added myself in the credits

Thanks for the trust
This commit is contained in:
Phil
2023-01-22 00:18:11 +02:00
committed by GitHub
parent 48b52ae645
commit 7b577e9548
2 changed files with 21 additions and 11 deletions

View File

@@ -179,3 +179,4 @@ This XDP project performs basic layer 3/4 forwarding using source port mapping s
## Credits
* [Christian Deacon](https://github.com/gamemann) - Creator.
* [Phil](https://github.com/Nasty07) - Contributor.

View File

@@ -9,6 +9,7 @@
#include <getopt.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <net/if.h>
@@ -291,6 +292,7 @@ int attachxdp(int ifidx, int progfd, struct cmdline *cmd)
return mode;
}
struct stat conf_stat;
int main(int argc, char *argv[])
{
// Parse the command line.
@@ -341,8 +343,9 @@ int main(int argc, char *argv[])
setcfgdefaults(&cfg);
// Create last updated variable.
time_t lastupdated = time(NULL);
time_t lastupdatecheck = time(NULL);
time_t statslastupdated = time(NULL);
time_t lastupdated = time(NULL);
// Update config.
updateconfig(&cfg, cmd.cfgfile);
@@ -510,20 +513,26 @@ int main(int argc, char *argv[])
}
// Check for auto-update.
if (cfg.updatetime > 0 && (curTime - lastupdated) > cfg.updatetime)
if (cfg.updatetime > 0 && (curTime - lastupdatecheck) > cfg.updatetime)
{
// Memleak fix for strdup() in updateconfig()
// Before updating it again, we need to free the old return value
free(cfg.interface);
// Check if config file have been modified
if (stat(cmd.cfgfile, &conf_stat) == 0 && conf_stat.st_mtime > lastupdated) {
// Memleak fix for strdup() in updateconfig()
// Before updating it again, we need to free the old return value
free(cfg.interface);
// Update config.
updateconfig(&cfg, cmd.cfgfile);
// Update config.
updateconfig(&cfg, cmd.cfgfile);
// Update BPF maps.
updatefilters(&cfg);
// Update timer
lastupdated = time(NULL);
}
// Update BPF maps.
updatefilters(&cfg);
// Update last updated variable.
lastupdated = time(NULL);
lastupdatecheck = time(NULL);
}
// Update stats.