File: resolv_test.c

package info (click to toggle)
sniproxy 0.6.1%2Bgit20240321-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: ansic: 5,594; perl: 1,673; sh: 237; makefile: 131
file content (55 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <arpa/inet.h>
#include <ev.h>
#include <assert.h>
#include "resolv.h"
#include "address.h"

static int query_count = 0;


static void query_cb(struct Address *result, void *data) {
    int *query_count = (int *)data;
    char ip_buf[INET6_ADDRSTRLEN];

    if (result != NULL &&
            address_is_sockaddr(result) &&
            display_address(result, ip_buf, sizeof(ip_buf))) {

        fprintf(stderr, "query resolved to %s\n", ip_buf);

        query_count++;
    }
}

static void
test_init_cb(struct ev_loop *loop __attribute__((unused)), struct ev_timer *w __attribute__((unused)), int revents) {
    if (revents & EV_TIMER)
        resolv_query("localhost", RESOLV_MODE_DEFAULT, query_cb, NULL, &query_count);
}

static void
timeout_cb(struct ev_loop *loop, struct ev_timer *w __attribute__((unused)), int revents) {
    if (revents & EV_TIMER)
        ev_break(loop, EVBREAK_ALL);
}


int main() {
    struct ev_loop *loop = EV_DEFAULT;
    struct ev_timer timeout_watcher;
    struct ev_timer init_watcher;

    resolv_init(loop, NULL, NULL, 0);

    ev_timer_init(&init_watcher, &test_init_cb, 0.0, 0.0);
    ev_timer_start(loop, &init_watcher);
    ev_timer_init(&timeout_watcher, &timeout_cb, 5.0, 0.0);
    ev_timer_start(loop, &timeout_watcher);

    ev_run(loop, 0);

    resolv_shutdown(loop);

    return 0;
}