smartdns.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "fast_ping.h"
  19. #include "dns_client.h"
  20. #include "dns_server.h"
  21. #include "hashtable.h"
  22. #include "list.h"
  23. #include "tlog.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. int smartdns_init()
  27. {
  28. int ret;
  29. ret = tlog_init(".", "smartdns.log", 1024 * 1024, 8, 1, 0, 0);
  30. if (ret != 0) {
  31. fprintf(stderr, "start tlog failed.\n");
  32. goto errout;
  33. }
  34. ret = fast_ping_init();
  35. if (ret != 0) {
  36. fprintf(stderr, "start ping failed.\n");
  37. goto errout;
  38. }
  39. ret = dns_server_init();
  40. if (ret != 0) {
  41. fprintf(stderr, "start dns server failed.\n");
  42. goto errout;
  43. }
  44. ret = dns_client_init();
  45. if (ret != 0) {
  46. fprintf(stderr, "start dns client failed.\n");
  47. goto errout;
  48. }
  49. dns_add_server("192.168.1.1", 53, DNS_SERVER_UDP);
  50. dns_add_server("114.114.114.114", 53, DNS_SERVER_UDP);
  51. dns_add_server("123.207.137.88", 53, DNS_SERVER_UDP);
  52. fast_ping_start("192.168.1.1", 10, 1000, 0, 0);
  53. return 0;
  54. errout:
  55. return -1;
  56. }
  57. int smartdns_run()
  58. {
  59. return dns_server_run();
  60. }
  61. void smartdns_exit()
  62. {
  63. fast_ping_exit();
  64. dns_client_exit();
  65. dns_server_exit();
  66. tlog_exit();
  67. }
  68. struct data {
  69. struct list_head list;
  70. int n;
  71. };
  72. void list_test()
  73. {
  74. struct list_head head;
  75. struct list_head *iter;
  76. int i = 0;
  77. INIT_LIST_HEAD(&head);
  78. for (i = 0; i < 10; i++) {
  79. struct data *h = malloc(sizeof(struct data));
  80. h->n = i;
  81. list_add(&h->list, &head);
  82. }
  83. list_for_each(iter, &head)
  84. {
  85. struct data *d = list_entry(iter, struct data, list);
  86. printf("%d\n", d->n);
  87. }
  88. }
  89. struct data_hash {
  90. struct hlist_node node;
  91. int n;
  92. char str[32];
  93. };
  94. int hash_test()
  95. {
  96. DEFINE_HASHTABLE(ht, 7);
  97. struct data_hash *temp;
  98. struct data_hash *obj;
  99. int i;
  100. int key;
  101. for (i = 11; i < 17; i++) {
  102. temp = malloc(sizeof(struct data_hash));
  103. temp->n = i * i;
  104. hash_add(ht, &temp->node, temp->n);
  105. }
  106. for (i = 11; i < 17; i++) {
  107. key = i * i;
  108. hash_for_each_possible(ht, obj, node, key)
  109. {
  110. printf("value: %d\n", obj->n);
  111. };
  112. }
  113. return 0;
  114. }
  115. int hash_string_test()
  116. {
  117. DEFINE_HASHTABLE(ht, 7);
  118. struct data_hash *temp;
  119. struct data_hash *obj;
  120. int i;
  121. int key;
  122. for (i = 0; i < 10; i++) {
  123. temp = malloc(sizeof(struct data_hash));
  124. sprintf(temp->str, "%d", i);
  125. hash_add(ht, &temp->node, hash_string(temp->str));
  126. }
  127. for (i = 0; i < 10; i++) {
  128. char key_str[32];
  129. sprintf(key_str, "%d", i);
  130. key = hash_string(key_str);
  131. hash_for_each_possible(ht, obj, node, key)
  132. {
  133. printf("i = %d value: %s\n", i, obj->str);
  134. };
  135. }
  136. return 0;
  137. }
  138. #if 0
  139. struct data_rbtree {
  140. struct rb_node list;
  141. int value;
  142. };
  143. int rbtree_test()
  144. {
  145. struct rb_root root;
  146. struct rb_node *n;
  147. RB_EMPTY_ROOT(&root);
  148. int i;
  149. for (i = 0; i < 10; i++)
  150. {
  151. struct data_rbtree *r = malloc(sizeof(struct data_rbtree));
  152. r->value = i;
  153. rb_insert(&r->list, &root);
  154. }
  155. n = rb_first(&root);
  156. int num = 5;
  157. while (n) {
  158. struct data_rbtree *r = container_of(n, struct data_rbtree, list);
  159. if (r->value < num) {
  160. n = n->rb_left;
  161. } else if (r->value > num) {
  162. n = n->rb_right;
  163. } else {
  164. printf("n = %d\n", r->value);
  165. break;
  166. }
  167. }
  168. return 0;
  169. }
  170. #endif
  171. #include <signal.h>
  172. void sig_handle(int sig)
  173. {
  174. tlog(TLOG_ERROR, "process exit.\n");
  175. sleep(1);
  176. _exit(0);
  177. }
  178. int main(int argc, char *argv[])
  179. {
  180. int ret;
  181. atexit(smartdns_exit);
  182. signal(SIGABRT, sig_handle);
  183. ret = smartdns_init();
  184. if (ret != 0) {
  185. fprintf(stderr, "init smartdns failed.\n");
  186. goto errout;
  187. }
  188. return smartdns_run();
  189. errout:
  190. return 1;
  191. }