smartdns.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "atomic.h"
  19. #include "conf.h"
  20. #include "dns_client.h"
  21. #include "dns_server.h"
  22. #include "fast_ping.h"
  23. #include "hashtable.h"
  24. #include "list.h"
  25. #include "tlog.h"
  26. #include "util.h"
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #define RESOLVE_FILE "/etc/resolv.conf"
  33. #define MAX_LINE_LEN 1024
  34. #define MAX_KEY_LEN 64
  35. int smartdns_load_from_resolv(void)
  36. {
  37. FILE *fp = NULL;
  38. char line[MAX_LINE_LEN];
  39. char key[MAX_KEY_LEN];
  40. char value[MAX_LINE_LEN];
  41. char ns_ip[DNS_MAX_IPLEN];
  42. int port = PORT_NOT_DEFINED;
  43. int ret = -1;
  44. int filed_num = 0;
  45. int line_num = 0;
  46. fp = fopen(RESOLVE_FILE, "r");
  47. if (fp == NULL) {
  48. tlog(TLOG_ERROR, "open %s failed, %s", RESOLVE_FILE, strerror(errno));
  49. return -1;
  50. }
  51. while (fgets(line, MAX_LINE_LEN, fp)) {
  52. line_num++;
  53. filed_num = sscanf(line, "%63s %1023[^\r\n]s", key, value);
  54. if (filed_num != 2) {
  55. continue;
  56. }
  57. if (strncmp(key, "nameserver", MAX_KEY_LEN) != 0) {
  58. continue;
  59. }
  60. if (parse_ip(value, ns_ip, &port) != 0) {
  61. continue;
  62. }
  63. if (port == PORT_NOT_DEFINED) {
  64. port = DEFAULT_DNS_PORT;
  65. }
  66. strncpy(dns_conf_servers[dns_conf_server_num].server, ns_ip, DNS_MAX_IPLEN);
  67. dns_conf_servers[dns_conf_server_num].port = port;
  68. dns_conf_servers[dns_conf_server_num].type = DNS_SERVER_UDP;
  69. dns_conf_server_num++;
  70. ret = 0;
  71. }
  72. fclose(fp);
  73. return ret;
  74. }
  75. int smartdns_add_servers(void)
  76. {
  77. int i = 0;
  78. int ret = 0;
  79. for (i = 0; i < dns_conf_server_num; i++) {
  80. ret = dns_add_server(dns_conf_servers[i].server, dns_conf_servers[i].port, dns_conf_servers[i].type);
  81. if (ret != 0) {
  82. tlog(TLOG_ERROR, "add server failed, %s:%d", dns_conf_servers[i].server, dns_conf_servers[i].port);
  83. return -1;
  84. }
  85. }
  86. return 0;
  87. }
  88. int smartdns_init()
  89. {
  90. int ret;
  91. if (load_conf("smartdns.conf") != 0) {
  92. fprintf(stderr, "load config failed.");
  93. }
  94. ret = tlog_init(".", "smartdns.log", 1024 * 1024, 8, 1, 0, 0);
  95. if (ret != 0) {
  96. tlog(TLOG_ERROR, "start tlog failed.\n");
  97. goto errout;
  98. }
  99. tlog_setlogscreen(1);
  100. tlog_setlevel(dns_conf_loglevel);
  101. if (dns_conf_server_num <= 0) {
  102. if (smartdns_load_from_resolv() != 0) {
  103. tlog(TLOG_ERROR, "load dns from resolv failed.");
  104. goto errout;
  105. }
  106. }
  107. ret = fast_ping_init();
  108. if (ret != 0) {
  109. tlog(TLOG_ERROR, "start ping failed.\n");
  110. goto errout;
  111. }
  112. ret = dns_server_init();
  113. if (ret != 0) {
  114. tlog(TLOG_ERROR, "start dns server failed.\n");
  115. goto errout;
  116. }
  117. ret = dns_client_init();
  118. if (ret != 0) {
  119. tlog(TLOG_ERROR, "start dns client failed.\n");
  120. goto errout;
  121. }
  122. ret = smartdns_add_servers();
  123. if (ret != 0) {
  124. tlog(TLOG_ERROR, "add servers failed.");
  125. goto errout;
  126. }
  127. return 0;
  128. errout:
  129. return -1;
  130. }
  131. int smartdns_run()
  132. {
  133. return dns_server_run();
  134. }
  135. void smartdns_exit()
  136. {
  137. dns_server_exit();
  138. dns_client_exit();
  139. fast_ping_exit();
  140. tlog_exit();
  141. }
  142. void sig_handle(int sig)
  143. {
  144. switch (sig) {
  145. case SIGINT:
  146. dns_server_stop();
  147. return;
  148. break;
  149. default:
  150. break;
  151. }
  152. tlog(TLOG_ERROR, "process exit.\n");
  153. _exit(0);
  154. }
  155. int main(int argc, char *argv[])
  156. {
  157. int ret;
  158. signal(SIGABRT, sig_handle);
  159. ret = smartdns_init();
  160. if (ret != 0) {
  161. goto errout;
  162. }
  163. signal(SIGINT, sig_handle);
  164. atexit(smartdns_exit);
  165. return smartdns_run();
  166. errout:
  167. return 1;
  168. }