nfqws,tpws: read config from a file

This commit is contained in:
bol-van
2024-10-31 17:55:26 +03:00
parent c964677913
commit a4632ef6d7
8 changed files with 240 additions and 14 deletions

View File

@@ -41,6 +41,9 @@
#include "gzip.h"
#include "pools.h"
#define MAX_CONFIG_FILE_SIZE 16384
struct params_s params;
static void onhup(int sig)
@@ -119,6 +122,7 @@ static int get_default_ttl(void)
static void exithelp(void)
{
printf(
" @<config_file>\t\t\t\t; read file for options. must be the only argument. other options are ignored.\n\n"
" --bind-addr=<v4_addr>|<v6_addr>\t; for v6 link locals append %%interface_name\n"
" --bind-iface4=<interface_name>\t\t; bind to the first ipv4 addr of interface\n"
" --bind-iface6=<interface_name>\t\t; bind to the first ipv6 addr of interface\n"
@@ -211,8 +215,16 @@ static void exithelp(void)
);
exit(1);
}
static void cleanup_args()
{
free_command_line(params.argv,params.argc);
params.argv = NULL;
params.argc = 0;
}
static void cleanup_params(void)
{
cleanup_args();
dp_list_destroy(&params.desync_profiles);
hostlist_files_destroy(&params.hostlists);
@@ -392,6 +404,34 @@ void parse_params(int argc, char *argv[])
}
dp = &dpl->dp;
dp->n = ++desync_profile_count;
if (argc>=2 && argv[1][0]=='@')
{
// config from a file
char buf[MAX_CONFIG_FILE_SIZE];
buf[0]='x'; // fake argv[0]
buf[1]=' ';
size_t bufsize=sizeof(buf)-3;
if (!load_file(argv[1]+1,buf+2,&bufsize))
{
DLOG_ERR("could not load config file '%s'\n",argv[1]+1);
exit_clean(1);
}
buf[bufsize+2]=0;
// wordexp fails if it sees \t \n \r between args
replace_char(buf,'\n',' ');
replace_char(buf,'\r',' ');
replace_char(buf,'\t',' ');
params.argv = split_command_line(buf,&params.argc);
if (!params.argv)
{
DLOG_ERR("failed to split command line options from file '%s'\n",argv[1]+1);
exit_clean(1);
}
argv=params.argv;
argc=params.argc;
}
const struct option long_options[] = {
{ "help",no_argument,0,0 },// optidx=0
@@ -1091,6 +1131,9 @@ void parse_params(int argc, char *argv[])
HostlistsDebug();
IpsetsDebug();
VPRINT("\n");
// do not need args from file anymore
cleanup_args();
}
@@ -1250,6 +1293,7 @@ int main(int argc, char *argv[])
mask_from_preflen6_prepare();
parse_params(argc, argv);
argv=NULL; argc=0;
if (params.daemon) daemonize();