File: testdns.c

package info (click to toggle)
st 1.9-3.4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 812 kB
  • sloc: ansic: 5,585; makefile: 379; asm: 362; sh: 27
file content (112 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "stx.h"
#include <netinet/in.h>
#include <arpa/inet.h>


#define MAX_ADDRS 128
#define TIMEOUT (4*1000000LL)

static void do_resolve(const char *host)
{
    struct in_addr addrs[MAX_ADDRS];
    int i, n = MAX_ADDRS;

    if (stx_dns_getaddrlist(host, addrs, &n, TIMEOUT) < 0) {
	fprintf(stderr, "stx_dns_getaddrlist: can't resolve %s: ", host);
	if (h_errno == NETDB_INTERNAL)
	    perror("");
	else
	    herror("");
    } else {
	if (n > 0)
	    printf("%-40s %s\n", (char *)host, inet_ntoa(addrs[0]));
	for (i = 1; i < n; i++)
	    printf("%-40s %s\n", "", inet_ntoa(addrs[i]));
    }
}

static void show_info(void)
{
    stx_cache_info_t info;

    stx_dns_cache_getinfo(&info);
    printf("DNS cache info:\n\n");
    printf("max_size:  %8d\n", (int)info.max_size);
    printf("capacity:  %8d bytes\n", (int)info.max_weight);
    printf("hash_size: %8d\n", (int)info.hash_size);
    printf("cur_size:  %8d\n"
	   "cur_mem:   %8d bytes\n"
	   "hits:      %8d\n"
	   "lookups:   %8d\n"
	   "inserts:   %8d\n"
	   "deletes:   %8d\n",
	   (int)info.cur_size, (int)info.cur_weight, (int)info.hits,
	   (int)info.lookups, (int)info.inserts, (int)info.deletes);
}

extern stx_cache_t *_stx_dns_cache;

static void printhost(void *host, void *data)
{
    printf("%s\n", (char *)host);
}

static void show_lru(void)
{
    printf("LRU hosts:\n\n");
    stx_cache_traverse_lru(_stx_dns_cache, printhost, 10);
}

static void show_mru(void)
{
    printf("MRU hosts:\n\n");
    stx_cache_traverse_mru(_stx_dns_cache, printhost, 10);
}

static void flush_cache(void)
{
    stx_cache_empty(_stx_dns_cache);
    printf("DNS cache is empty\n");
}


int main()
{
    char line[256];
    char str[sizeof(line)];

    st_init();
    stx_dns_cache_init(100, 10000, 101);

    for ( ; ; ) {
	fputs("> ", stdout);
	fflush(stdout);
	if (!fgets(line, sizeof(line), stdin))
	    break;
	if (sscanf(line, "%s", str) != 1)
	    continue;
	if (strcmp(str, "exit") == 0 || strcmp(str, "quit") == 0)
	    break;
	if (strcmp(str, "info") == 0) {
	    show_info();
	    continue;
	}
	if (strcmp(str, "lru") == 0) {
	    show_lru();
	    continue;
	}
	if (strcmp(str, "mru") == 0) {
	    show_mru();
	    continue;
	}
	if (strcmp(str, "flush") == 0) {
	    flush_cache();
	    continue;
	}

	do_resolve(str);
    }

    return 0;
}