From 2147d16441f795f10226ef31e58977de83f17e0b Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Mon, 23 Oct 2017 21:36:33 +0200 Subject: [PATCH 1/6] added --loop option for automatic reconnects --- doc/openfortivpn.1.in | 10 ++++++++++ src/config.c | 9 +++++++++ src/config.h | 1 + src/io.c | 9 +++++++++ src/io.h | 2 ++ src/main.c | 21 ++++++++++++++++++++- 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/doc/openfortivpn.1.in b/doc/openfortivpn.1.in index d918cfaf..0b8da458 100644 --- a/doc/openfortivpn.1.in +++ b/doc/openfortivpn.1.in @@ -26,6 +26,7 @@ openfortivpn \- Client for PPP+SSL VPN tunnel services [\fB\-\-pppd-plugin=\fI\fR] [\fB\-\-pppd-ipparam=\fI\fR] [\fB\-\-pppd-ifname=\fI\fR] +[\fB\-\-loop\fR] [\fB\-c\fR \fI\fR] [\fB\-v|\-q\fR] .br @@ -125,9 +126,16 @@ Provides an extra parameter to the ip-up, ip-pre-up and ip-down scripts. See man .BR pppd(8) for further details .TP +<<<<<<< 5e9adc7e3fd27ee36b0a00583dce1225226f57a2 \fB\-\-pppd-ifname=\fI\fR Set the ppp interface name. Only if supported by pppd. Patched versions of pppd implement this option but may not be available on your platform. +======= +\fB\-\-loop\=\fIfR +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). +>>>>>>> added --loop option for automatic reconnects .TP \fB\-v\fR Increase verbosity. Can be used multiple times to be even more verbose. @@ -233,3 +241,5 @@ pppd-use-peerdns = 1 insecure-ssl = 0 .br cipher-list = HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4 +.br +loop = 0 diff --git a/src/config.c b/src/config.c index 54e86854..b81d9b93 100644 --- a/src/config.c +++ b/src/config.c @@ -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) { diff --git a/src/config.h b/src/config.h index 05d0c3f3..ee67d309 100644 --- a/src/config.h +++ b/src/config.h @@ -68,6 +68,7 @@ struct vpn_config { int pppd_use_peerdns; int use_syslog; int half_internet_routes; + int loop; char *pppd_log; char *pppd_plugin; diff --git a/src/io.c b/src/io.c index a65e2fdd..65dd94d6 100644 --- a/src/io.c +++ b/src/io.c @@ -109,6 +109,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. * @@ -559,6 +567,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); } diff --git a/src/io.h b/src/io.h index b18d0a0d..f6a7f0db 100644 --- a/src/io.h +++ b/src/io.h @@ -56,4 +56,6 @@ struct tunnel; int io_loop(struct tunnel *tunnel); +int get_sig_received(void); + #endif diff --git a/src/main.c b/src/main.c index b3cd7b6e..f46046ec 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ " [--pppd-ifname=] [--pppd-ipparam=]\n" \ " [--pppd-plugin=] [--ca-file=]\n" \ " [--user-cert=] [--user-key=]\n" \ -" [--trusted-cert=] [--use-syslog]\n" \ +" [--trusted-cert=] [--use-syslog] [--loop=]\n" \ " [-c ] [-v|-q]\n" \ " openfortivpn --help\n" \ " openfortivpn --version\n" \ @@ -93,6 +93,8 @@ " --pppd-ifname= Set the pppd interface name, if supported by pppd.\n" \ " --pppd-ipparam= Provides an extra parameter to the ip-up, ip-pre-up\n" \ " and ip-down scripts. See man (8) pppd\n" \ +" --loop= Run the vpn in a loop and try to reconnect every\n" \ +" 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" \ @@ -181,6 +183,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}, @@ -301,6 +304,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); @@ -427,6 +441,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: From da99aa215fab2da92d27bc795437cfbaa4854546 Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Wed, 11 Oct 2017 15:13:51 +0200 Subject: [PATCH 2/6] call run_tunnel from a single place --- src/main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index f46046ec..88bf07fe 100644 --- a/src/main.c +++ b/src/main.c @@ -439,13 +439,15 @@ int main(int argc, char **argv) log_warn("This process was not spawned with root " "privileges, this will probably not work.\n"); - 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) + do { + if (run_tunnel(&cfg) != 0) { ret = EXIT_FAILURE; - } + } else + ret = EXIT_SUCCESS; + if ((cfg.loop > 0) && (get_sig_received() == 0)) + sleep(cfg.loop); + } while ((get_sig_received() == 0) && (cfg.loop !=0)); + goto exit; user_error: From b545afc2dce96549e896eaad47cfa088611ba8d0 Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Wed, 13 Dec 2017 13:47:25 +0100 Subject: [PATCH 3/6] catch too large loop values and clean up merge artifacts in man page --- doc/openfortivpn.1.in | 3 --- src/config.h | 3 ++- src/main.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/openfortivpn.1.in b/doc/openfortivpn.1.in index 0b8da458..e5435627 100644 --- a/doc/openfortivpn.1.in +++ b/doc/openfortivpn.1.in @@ -126,16 +126,13 @@ Provides an extra parameter to the ip-up, ip-pre-up and ip-down scripts. See man .BR pppd(8) for further details .TP -<<<<<<< 5e9adc7e3fd27ee36b0a00583dce1225226f57a2 \fB\-\-pppd-ifname=\fI\fR Set the ppp interface name. Only if supported by pppd. Patched versions of pppd implement this option but may not be available on your platform. -======= \fB\-\-loop\=\fIfR 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). ->>>>>>> added --loop option for automatic reconnects .TP \fB\-v\fR Increase verbosity. Can be used multiple times to be even more verbose. diff --git a/src/config.h b/src/config.h index ee67d309..2eeb616e 100644 --- a/src/config.h +++ b/src/config.h @@ -68,7 +68,8 @@ struct vpn_config { int pppd_use_peerdns; int use_syslog; int half_internet_routes; - int loop; + + unsigned int loop; char *pppd_log; char *pppd_plugin; diff --git a/src/main.c b/src/main.c index 88bf07fe..e7f57fda 100644 --- a/src/main.c +++ b/src/main.c @@ -307,7 +307,7 @@ int main(int argc, char **argv) if (strcmp(long_options[option_index].name, "loop") == 0) { long int loop = strtol(optarg, NULL, 0); - if (loop < 0) { + if ((loop < 0) || (loop >= UINT_MAX)){ log_warn("Bad loop option: " \ "\"%s\"\n", optarg); break; From cccc8c737b2b6dab8e1a3f53dfe3bb354ea4d219 Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Wed, 13 Dec 2017 13:49:27 +0100 Subject: [PATCH 4/6] make lint test pass --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index e7f57fda..1a48a353 100644 --- a/src/main.c +++ b/src/main.c @@ -307,7 +307,7 @@ int main(int argc, char **argv) if (strcmp(long_options[option_index].name, "loop") == 0) { long int loop = strtol(optarg, NULL, 0); - if ((loop < 0) || (loop >= UINT_MAX)){ + if ((loop < 0) || (loop >= UINT_MAX)) { log_warn("Bad loop option: " \ "\"%s\"\n", optarg); break; From 325dd4e67ffb9d62b51e61276bf9dbf56da8ff5a Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Tue, 20 Feb 2018 14:38:36 +0100 Subject: [PATCH 5/6] change the term "loop" to "persistent" --- doc/openfortivpn.1.in | 8 ++++---- src/config.c | 10 +++++----- src/config.h | 2 +- src/main.c | 24 ++++++++++++------------ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/doc/openfortivpn.1.in b/doc/openfortivpn.1.in index e5435627..fd68702c 100644 --- a/doc/openfortivpn.1.in +++ b/doc/openfortivpn.1.in @@ -26,7 +26,7 @@ openfortivpn \- Client for PPP+SSL VPN tunnel services [\fB\-\-pppd-plugin=\fI\fR] [\fB\-\-pppd-ipparam=\fI\fR] [\fB\-\-pppd-ifname=\fI\fR] -[\fB\-\-loop\fR] +[\fB\-\-persistent\fR] [\fB\-c\fR \fI\fR] [\fB\-v|\-q\fR] .br @@ -129,8 +129,8 @@ for further details \fB\-\-pppd-ifname=\fI\fR Set the ppp interface name. Only if supported by pppd. Patched versions of pppd implement this option but may not be available on your platform. -\fB\-\-loop\=\fIfR -Runs the vpn in an endless loop and tries to reconnect forever. +\fB\-\-persistent\=\fIfR +Runs the vpn persistently 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 @@ -239,4 +239,4 @@ insecure-ssl = 0 .br cipher-list = HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4 .br -loop = 0 +persistent = 0 diff --git a/src/config.c b/src/config.c index b81d9b93..451b678a 100644 --- a/src/config.c +++ b/src/config.c @@ -200,15 +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:" \ + } else if (strcmp(key, "persistent") == 0) { + long int persistent = strtol(val, NULL, 0); + if (persistent < 0) { + log_warn("Bad value for persistent in config file:" \ " \"%s\".\n", val); continue; } - cfg->loop =loop; + cfg->persistent = persistent; } else if (strcmp(key, "pppd-use-peerdns") == 0) { int pppd_use_peerdns = strtob(val); if (pppd_use_peerdns < 0) { diff --git a/src/config.h b/src/config.h index 2eeb616e..26bd45f0 100644 --- a/src/config.h +++ b/src/config.h @@ -69,7 +69,7 @@ struct vpn_config { int use_syslog; int half_internet_routes; - unsigned int loop; + unsigned int persistent; char *pppd_log; char *pppd_plugin; diff --git a/src/main.c b/src/main.c index 1a48a353..2b0e2adf 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ " [--pppd-ifname=] [--pppd-ipparam=]\n" \ " [--pppd-plugin=] [--ca-file=]\n" \ " [--user-cert=] [--user-key=]\n" \ -" [--trusted-cert=] [--use-syslog] [--loop=]\n" \ +" [--trusted-cert=] [--use-syslog] [--persistent=]\n" \ " [-c ] [-v|-q]\n" \ " openfortivpn --help\n" \ " openfortivpn --version\n" \ @@ -93,8 +93,8 @@ " --pppd-ifname= Set the pppd interface name, if supported by pppd.\n" \ " --pppd-ipparam= Provides an extra parameter to the ip-up, ip-pre-up\n" \ " and ip-down scripts. See man (8) pppd\n" \ -" --loop= Run the vpn in a loop and try to reconnect every\n" \ -" seconds\n" \ +" --persistent= Run the vpn persistently in a loop and try to reconnect\n" \ +" every seconds when dropping out\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" \ @@ -183,7 +183,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}, + {"persistent", required_argument, 0, 0}, {"ca-file", required_argument, 0, 0}, {"user-cert", required_argument, 0, 0}, {"user-key", required_argument, 0, 0}, @@ -305,14 +305,14 @@ int main(int argc, char **argv) break; } if (strcmp(long_options[option_index].name, - "loop") == 0) { - long int loop = strtol(optarg, NULL, 0); - if ((loop < 0) || (loop >= UINT_MAX)) { - log_warn("Bad loop option: " \ + "persistent") == 0) { + long int persistent = strtol(optarg, NULL, 0); + if ((persistent < 0) || (persistent >= UINT_MAX)) { + log_warn("Bad persistent option: " \ "\"%s\"\n", optarg); break; } - cfg.loop = loop; + cfg.persistent = persistent; break; } if (strcmp(long_options[option_index].name, @@ -444,9 +444,9 @@ int main(int argc, char **argv) ret = EXIT_FAILURE; } else ret = EXIT_SUCCESS; - if ((cfg.loop > 0) && (get_sig_received() == 0)) - sleep(cfg.loop); - } while ((get_sig_received() == 0) && (cfg.loop !=0)); + if ((cfg.persistent > 0) && (get_sig_received() == 0)) + sleep(cfg.persistent); + } while ((get_sig_received() == 0) && (cfg.persistent !=0)); goto exit; From 904833dfc9ff6ede974d6f140ded578f3e43571b Mon Sep 17 00:00:00 2001 From: Martin Hecht Date: Tue, 20 Feb 2018 14:41:35 +0100 Subject: [PATCH 6/6] wrap long lines to make lint happy --- src/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 2b0e2adf..d6fe9315 100644 --- a/src/main.c +++ b/src/main.c @@ -34,8 +34,8 @@ " [--pppd-ifname=] [--pppd-ipparam=]\n" \ " [--pppd-plugin=] [--ca-file=]\n" \ " [--user-cert=] [--user-key=]\n" \ -" [--trusted-cert=] [--use-syslog] [--persistent=]\n" \ -" [-c ] [-v|-q]\n" \ +" [--trusted-cert=] [--use-syslog]\n" \ +" [--persistent=] [-c ] [-v|-q]\n" \ " openfortivpn --help\n" \ " openfortivpn --version\n" \ "\n" @@ -93,8 +93,8 @@ " --pppd-ifname= Set the pppd interface name, if supported by pppd.\n" \ " --pppd-ipparam= Provides an extra parameter to the ip-up, ip-pre-up\n" \ " and ip-down scripts. See man (8) pppd\n" \ -" --persistent= Run the vpn persistently in a loop and try to reconnect\n" \ -" every seconds when dropping out\n" \ +" --persistent= Run the vpn persistently in a loop and try to re-\n" \ +" connect every seconds when dropping out\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" \