From 9706596099352577963b633ef6a0638b66483cab Mon Sep 17 00:00:00 2001 From: fenggui321 Date: Tue, 28 Nov 2017 18:19:42 +0800 Subject: [PATCH] skip always dns resolve when ip already exist --- contributors.md | 4 ++- src/centralserver.c | 85 +++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 43 deletions(-) mode change 100644 => 100755 src/centralserver.c diff --git a/contributors.md b/contributors.md index e35ee65..f75f0de 100755 --- a/contributors.md +++ b/contributors.md @@ -20,4 +20,6 @@ Thank you to all contributors: [wenchangshou2](https://github.com/wenchangshou2) -[Lzxiangxmut](https://github.com/Lzxiangxmut) \ No newline at end of file +[Lzxiangxmut](https://github.com/Lzxiangxmut) + +[fenggui321](https://github.com/fenggui321) diff --git a/src/centralserver.c b/src/centralserver.c old mode 100644 new mode 100755 index ef1fa36..e2194a0 --- a/src/centralserver.c +++ b/src/centralserver.c @@ -462,49 +462,52 @@ _connect_auth_server(int level) { */ auth_server = config->auth_servers; hostname = auth_server->authserv_hostname; - debug(LOG_DEBUG, "Level %d: Resolving auth server [%s]", level, hostname); - h_addr = wd_gethostbyname(hostname); - if (!h_addr) { - /* - * DNS resolving it failed - */ - debug(LOG_INFO, "Level %d: Resolving auth server [%s] failed", level, hostname); - - if (auth_server->last_ip) { - free(auth_server->last_ip); - auth_server->last_ip = NULL; - } - mark_auth_server_bad(auth_server); - return _connect_auth_server(level); - } else { - /* - * DNS resolving was successful - */ - ip = safe_malloc(HTTP_IP_ADDR_LEN); - inet_ntop(AF_INET, h_addr, ip, HTTP_IP_ADDR_LEN); - ip[HTTP_IP_ADDR_LEN-1] = '\0'; - debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] succeeded = [%s]", level, hostname, ip); - - if (!auth_server->last_ip || strcmp(auth_server->last_ip, ip) != 0) { + if (NULL == auth_server->last_ip){ + debug(LOG_DEBUG, "Level %d: Resolving auth server [%s]", level, hostname); + h_addr = wd_gethostbyname(hostname); + if (!h_addr) { /* - * But the IP address is different from the last one we knew - * Update it - */ - debug(LOG_INFO, "Level %d: Updating last_ip IP of server [%s] to [%s]", level, hostname, ip); - if (auth_server->last_ip) + * DNS resolving it failed + */ + debug(LOG_INFO, "Level %d: Resolving auth server [%s] failed", level, hostname); + + if (auth_server->last_ip) { free(auth_server->last_ip); - auth_server->last_ip = ip; - - /* Update firewall rules */ - fw_clear_authservers(); - fw_set_authservers(); + auth_server->last_ip = NULL; + } + mark_auth_server_bad(auth_server); + return _connect_auth_server(level); } else { /* - * IP is the same as last time - */ - free(ip); + * DNS resolving was successful + */ + ip = safe_malloc(HTTP_IP_ADDR_LEN); + inet_ntop(AF_INET, h_addr, ip, HTTP_IP_ADDR_LEN); + ip[HTTP_IP_ADDR_LEN-1] = '\0'; + debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] succeeded = [%s]", level, hostname, ip); + + if (!auth_server->last_ip || strcmp(auth_server->last_ip, ip) != 0) { + /* + * But the IP address is different from the last one we knew + * Update it + */ + debug(LOG_INFO, "Level %d: Updating last_ip IP of server [%s] to [%s]", level, hostname, ip); + if (auth_server->last_ip) + free(auth_server->last_ip); + auth_server->last_ip = ip; + + /* Update firewall rules */ + fw_clear_authservers(); + fw_set_authservers(); + } else { + /* + * IP is the same as last time + */ + free(ip); + } + free(h_addr); } - + } /* * Connect to it */ @@ -513,9 +516,8 @@ _connect_auth_server(int level) { their_addr.sin_port = port; their_addr.sin_family = AF_INET; - their_addr.sin_addr = *h_addr; - memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); - free(h_addr); + inet_aton(auth_server->last_ip, &their_addr.sin_addr); + memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { debug(LOG_ERR, "Level %d: Failed to create a new SOCK_STREAM socket: %s", strerror(errno)); @@ -537,7 +539,6 @@ _connect_auth_server(int level) { mark_auth_server_bad(auth_server); return _connect_auth_server(level); /* Yay recursion! */ } - } } // 0, failure; 1, success