From 55dea8b973571e3e88df45d02fb16bd473891c1f Mon Sep 17 00:00:00 2001 From: Max Lv Date: Tue, 28 Jun 2016 15:33:55 +0800 Subject: [PATCH] Fix #690 --- src/local.c | 23 ++++++++++++++++------- src/netutils.c | 10 +++++++++- src/redir.c | 7 +++++++ src/tunnel.c | 8 ++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/local.c b/src/local.c index 52983a42..809c21de 100644 --- a/src/local.c +++ b/src/local.c @@ -80,6 +80,8 @@ #endif int verbose = 0; +int keep_resolving = 1; + #ifdef ANDROID int vpn = 0; uint64_t tx = 0; @@ -969,6 +971,10 @@ void accept_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & server->recv_ctx->io); } +void resolve_int_cb(int dummy) { + keep_resolving = 0; +} + #ifndef LIB_ONLY int main(int argc, char **argv) { @@ -1185,15 +1191,9 @@ int main(int argc, char **argv) // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); + signal(SIGINT, resolve_int_cb); #endif - struct ev_signal sigint_watcher; - struct ev_signal sigterm_watcher; - ev_signal_init(&sigint_watcher, signal_cb, SIGINT); - ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); - ev_signal_start(EV_DEFAULT, &sigint_watcher); - ev_signal_start(EV_DEFAULT, &sigterm_watcher); - // Setup keys LOGI("initializing ciphers... %s", method); int m = enc_init(password, method); @@ -1217,6 +1217,15 @@ int main(int argc, char **argv) listen_ctx.iface = iface; listen_ctx.method = m; + // Setup signal handler + struct ev_signal sigint_watcher; + struct ev_signal sigterm_watcher; + ev_signal_init(&sigint_watcher, signal_cb, SIGINT); + ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); + ev_signal_start(EV_DEFAULT, &sigint_watcher); + ev_signal_start(EV_DEFAULT, &sigterm_watcher); + + struct ev_loop *loop = EV_DEFAULT; // Setup socket diff --git a/src/netutils.c b/src/netutils.c index 1c6b1de9..7d5102f3 100644 --- a/src/netutils.c +++ b/src/netutils.c @@ -54,6 +54,10 @@ extern int verbose; +#if defined(MODULE_LOCAL) +extern int keep_resolving; +#endif + int set_reuseport(int socket) { int opt = 1; @@ -136,7 +140,11 @@ ssize_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, i for (i = 1; i < 8; i++) { err = getaddrinfo(host, port, &hints, &result); - if (!block || !err) { +#if defined(MODULE_LOCAL) + if (!keep_resolving) + break; +#endif + if ((!block || !err)) { break; } else { sleep(pow(2, i)); diff --git a/src/redir.c b/src/redir.c index 6b8633ca..0aa642be 100644 --- a/src/redir.c +++ b/src/redir.c @@ -83,6 +83,7 @@ static void free_server(server_t *server); static void close_and_free_server(EV_P_ server_t *server); int verbose = 0; +int keep_resolving = 1; static int mode = TCP_ONLY; static int auth = 0; @@ -669,6 +670,11 @@ static void accept_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & server->recv_ctx->io); } +void signal_cb(int dummy) { + keep_resolving = 0; + exit(-1); +} + int main(int argc, char **argv) { int i, c; @@ -846,6 +852,7 @@ int main(int argc, char **argv) // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); + signal(SIGINT, signal_cb); // Setup keys LOGI("initializing ciphers... %s", method); diff --git a/src/tunnel.c b/src/tunnel.c index 352f8f27..058f24bd 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -90,7 +90,9 @@ static void close_and_free_server(EV_P_ server_t *server); int vpn = 0; char *prefix; #endif + int verbose = 0; +int keep_resolving = 1; static int mode = TCP_ONLY; static int auth = 0; @@ -677,6 +679,11 @@ static void accept_cb(EV_P_ ev_io *w, int revents) } } +void signal_cb(int dummy) { + keep_resolving = 0; + exit(-1); +} + int main(int argc, char **argv) { int i, c; @@ -889,6 +896,7 @@ int main(int argc, char **argv) // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); + signal(SIGINT, signal_cb); #endif // Setup keys