| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- /*************************************************************************
- *
- * Copyright (C) 2018 Ruilin Peng (Nick) <[email protected]>.
- *
- * smartdns is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * smartdns is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "fast_ping.h"
- #include "dns_client.h"
- #include "dns_server.h"
- #include "hashtable.h"
- #include "list.h"
- #include "tlog.h"
- #include <stdio.h>
- #include <stdlib.h>
- int smartdns_init()
- {
- int ret;
- ret = tlog_init(".", "smartdns.log", 1024 * 1024, 8, 1, 0, 0);
- if (ret != 0) {
- fprintf(stderr, "start tlog failed.\n");
- goto errout;
- }
- ret = fast_ping_init();
- if (ret != 0) {
- fprintf(stderr, "start ping failed.\n");
- goto errout;
- }
- ret = dns_server_init();
- if (ret != 0) {
- fprintf(stderr, "start dns server failed.\n");
- goto errout;
- }
- ret = dns_client_init();
- if (ret != 0) {
- fprintf(stderr, "start dns client failed.\n");
- goto errout;
- }
- dns_add_server("192.168.1.1", 53, DNS_SERVER_UDP);
- dns_add_server("114.114.114.114", 53, DNS_SERVER_UDP);
- dns_add_server("123.207.137.88", 53, DNS_SERVER_UDP);
- fast_ping_start("192.168.1.1", 10, 1000, 0, 0);
- return 0;
- errout:
- return -1;
- }
- int smartdns_run()
- {
- return dns_server_run();
- }
- void smartdns_exit()
- {
- fast_ping_exit();
- dns_client_exit();
- dns_server_exit();
- tlog_exit();
- }
- struct data {
- struct list_head list;
- int n;
- };
- void list_test()
- {
- struct list_head head;
- struct list_head *iter;
- int i = 0;
- INIT_LIST_HEAD(&head);
- for (i = 0; i < 10; i++) {
- struct data *h = malloc(sizeof(struct data));
- h->n = i;
- list_add(&h->list, &head);
- }
- list_for_each(iter, &head)
- {
- struct data *d = list_entry(iter, struct data, list);
- printf("%d\n", d->n);
- }
- }
- struct data_hash {
- struct hlist_node node;
- int n;
- char str[32];
- };
- int hash_test()
- {
- DEFINE_HASHTABLE(ht, 7);
- struct data_hash *temp;
- struct data_hash *obj;
- int i;
- int key;
- for (i = 11; i < 17; i++) {
- temp = malloc(sizeof(struct data_hash));
- temp->n = i * i;
- hash_add(ht, &temp->node, temp->n);
- }
- for (i = 11; i < 17; i++) {
- key = i * i;
- hash_for_each_possible(ht, obj, node, key)
- {
- printf("value: %d\n", obj->n);
- };
- }
- return 0;
- }
- int hash_string_test()
- {
- DEFINE_HASHTABLE(ht, 7);
- struct data_hash *temp;
- struct data_hash *obj;
- int i;
- int key;
- for (i = 0; i < 10; i++) {
- temp = malloc(sizeof(struct data_hash));
- sprintf(temp->str, "%d", i);
- hash_add(ht, &temp->node, hash_string(temp->str));
- }
- for (i = 0; i < 10; i++) {
- char key_str[32];
- sprintf(key_str, "%d", i);
- key = hash_string(key_str);
- hash_for_each_possible(ht, obj, node, key)
- {
- printf("i = %d value: %s\n", i, obj->str);
- };
- }
- return 0;
- }
- #if 0
- struct data_rbtree {
- struct rb_node list;
- int value;
- };
- int rbtree_test()
- {
- struct rb_root root;
- struct rb_node *n;
- RB_EMPTY_ROOT(&root);
- int i;
- for (i = 0; i < 10; i++)
- {
- struct data_rbtree *r = malloc(sizeof(struct data_rbtree));
- r->value = i;
- rb_insert(&r->list, &root);
- }
- n = rb_first(&root);
- int num = 5;
- while (n) {
- struct data_rbtree *r = container_of(n, struct data_rbtree, list);
- if (r->value < num) {
- n = n->rb_left;
- } else if (r->value > num) {
- n = n->rb_right;
- } else {
- printf("n = %d\n", r->value);
- break;
- }
- }
- return 0;
- }
- #endif
- #include <signal.h>
- void sig_handle(int sig)
- {
- tlog(TLOG_ERROR, "process exit.\n");
- sleep(1);
- _exit(0);
- }
- int main(int argc, char *argv[])
- {
- int ret;
- atexit(smartdns_exit);
- signal(SIGABRT, sig_handle);
- ret = smartdns_init();
- if (ret != 0) {
- fprintf(stderr, "init smartdns failed.\n");
- goto errout;
- }
- return smartdns_run();
- errout:
- return 1;
- }
|