Skip to content

Commit

Permalink
added --loop option for automatic reconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbaseman committed Oct 19, 2017
1 parent 57d2017 commit c33847c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/openfortivpn.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ openfortivpn \- Client for PPP+SSL VPN tunnel services
[\fB\-\-pppd-log=\fI<file>\fR]
[\fB\-\-pppd-plugin=\fI<file>\fR]
[\fB\-\-pppd-ipparam=\fI<string>\fR]
[\fB\-\-loop\fR]
[\fB\-c\fR \fI<file>\fR]
[\fB\-v|\-q\fR]
.br
Expand Down Expand Up @@ -124,6 +125,11 @@ Provides an extra parameter to the ip-up, ip-pre-up and ip-down scripts. see man
.BR pppd(8)
for further details
.TP
\fB\-\-loop\=\fI<interval>fR
Runs the vpn in an endless loop and tries to reconnect forever.
The reconnect interval may be specified in seconds, where 0 means
no reconnect is done (this is the default).
.TP
\fB\-v\fR
Increase verbosity. Can be used multiple times to be even more verbose.
.TP
Expand Down Expand Up @@ -188,3 +194,5 @@ pppd-use-peerdns = 1
insecure-ssl = 0
.br
cipher-list = HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4
.br
loop = 0
9 changes: 9 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ int load_config(struct vpn_config *cfg, const char *filename)
continue;
}
cfg->half_internet_routes = half_internet_routes;
} else if (strcmp(key, "loop") == 0) {
long int loop = strtol(val, NULL, 0);
if (loop < 0) {
log_warn("Bad loop in config file:" \
" \"%s\".\n",
val);
continue;
}
cfg->loop =loop;
} else if (strcmp(key, "pppd-use-peerdns") == 0) {
int pppd_use_peerdns = strtob(val);
if (pppd_use_peerdns < 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct vpn_config {
int pppd_use_peerdns;
int use_syslog;
int half_internet_routes;
int loop;

char *pppd_log;
char *pppd_plugin;
Expand Down Expand Up @@ -94,6 +95,7 @@ struct vpn_config {
(cfg)->set_dns = 1; \
(cfg)->pppd_use_peerdns = 1; \
(cfg)->half_internet_routes = 0; \
(cfg)->loop = 0; \
(cfg)->use_syslog = 0; \
(cfg)->pppd_log = NULL; \
(cfg)->pppd_plugin = NULL; \
Expand Down
9 changes: 9 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ static void destroy_ssl_locks(void)
}
#endif

// global variable to pass signal out of its handler
volatile sig_atomic_t sig_received = 0;

int get_sig_received(void)
{
return (int)sig_received;
}

/*
* Adds a new packet to a pool.
*
Expand Down Expand Up @@ -556,6 +564,7 @@ static void *if_config(void *arg)

static void sig_handler(int signo)
{
sig_received = signo;
if (signo == SIGINT || signo == SIGTERM)
SEM_POST(&sem_stop_io);
}
Expand Down
2 changes: 2 additions & 0 deletions src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ struct tunnel;

int io_loop(struct tunnel *tunnel);

int get_sig_received(void);

#endif
21 changes: 20 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
" [--pppd-no-peerdns] [--pppd-log=<file>]\n" \
" [--pppd-ipparam=<string>] [--pppd-plugin=<file>]\n" \
" [--ca-file=<file>] [--user-cert=<file>] [--user-key=<file>] \n" \
" [--trusted-cert=<digest>] [--use-syslog] \n" \
" [--trusted-cert=<digest>] [--use-syslog] [--loop=<interval>]\n" \
" [-c <file>] [-v|-q]\n" \
" openfortivpn --help\n" \
" openfortivpn --version\n" \
Expand Down Expand Up @@ -87,6 +87,8 @@
" resolver and routes directly.\n" \
" --pppd-ipparam=<string> Provides an extra parameter to the ip-up, ip-pre-up\n" \
" and ip-down scripts. see man (8) pppd\n" \
" --loop=<interval> Run the vpn in a loop and try to reconnect every\n" \
" <interval> seconds\n" \
" -v Increase verbosity. Can be used multiple times\n" \
" to be even more verbose.\n" \
" -q Decrease verbosity. Can be used multiple times\n" \
Expand Down Expand Up @@ -142,6 +144,7 @@ int main(int argc, char **argv)
{"no-dns", no_argument, &cfg.set_dns, 0},
{"pppd-no-peerdns", no_argument, &cfg.pppd_use_peerdns, 0},
{"use-syslog", no_argument, &cfg.use_syslog, 1},
{"loop", required_argument, 0, 0},
{"ca-file", required_argument, 0, 0},
{"user-cert", required_argument, 0, 0},
{"user-key", required_argument, 0, 0},
Expand Down Expand Up @@ -254,6 +257,17 @@ int main(int argc, char **argv)
cfg.half_internet_routes = half_internet_routes;
break;
}
if (strcmp(long_options[option_index].name,
"loop") == 0) {
long int loop = strtol(optarg, NULL, 0);
if (loop < 0) {
log_warn("Bad loop option: " \
"\"%s\"\n", optarg);
break;
}
cfg.loop = loop;
break;
}
if (strcmp(long_options[option_index].name,
"set-dns") == 0) {
int set_dns = strtob(optarg);
Expand Down Expand Up @@ -380,6 +394,11 @@ int main(int argc, char **argv)

if (run_tunnel(&cfg) == 0)
ret = EXIT_SUCCESS;
while ((ret == EXIT_SUCCESS) && (cfg.loop!=0) && (get_sig_received()==0)) {
sleep(cfg.loop);
if (run_tunnel(&cfg) != 0)
ret = EXIT_FAILURE;
}
goto exit;

user_error:
Expand Down

0 comments on commit c33847c

Please sign in to comment.