Skip to content

Instantly share code, notes, and snippets.

@yasuoka
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuoka/d3af7a96b44a34fecba3 to your computer and use it in GitHub Desktop.
Save yasuoka/d3af7a96b44a34fecba3 to your computer and use it in GitHub Desktop.
/*
* Usage:
* % cc -DDEBUG -o evdns_test evdns_test.c -levent
* % ./evdns_test
*/
#include <sys/param.h>
#include <sys/time.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/dns.h>
#include <stdlib.h>
#include <assert.h>
#ifndef nitems
#define nitems(_x) (sizeof(_x) / sizeof((_x)[0]))
#endif
#ifdef DEBUG
#define DBG(_x) printf _x
#else
#define DBG(_x)
#endif
static struct event ev_periodic;
static int result[] = { -1, -1 };
static void periodic (int, short, void *);
static void on_resolved (int, char, int, int, void *, void *);
int
main(int argc, char *argv[])
{
event_init();
evdns_init();
evdns_clear_nameservers_and_suspend();
/* nameserver doesn't work */
evdns_nameserver_ip_add("127.0.0.2");
/* below requests will be error by shutdown */
evdns_resolve_ipv4("www.iij.ad.jp", 0, on_resolved, (void *)0);
evdns_resolve_ipv4("www.iij.ad.jp", 0, on_resolved, (void *)1);
periodic(-1, 0, NULL);
event_loop(0); /* see periodic() */
assert(result[0] == DNS_ERR_SHUTDOWN);
assert(result[1] == DNS_ERR_SHUTDOWN);
exit(EXIT_SUCCESS);
}
static void
periodic(int fd, short ev, void *ctx)
{
static int step = 0;
struct timeval tv = { 1, 0 };
++step;
DBG(("%s(%d)\n", __func__, step));
switch (step) {
case 1:
/* reconfigure */
evdns_clear_nameservers_and_suspend();
evdns_nameserver_ip_add("127.0.0.2");
break;
case 2:
/* shutdown */
evdns_shutdown(1);
break;
case 8:
/* (try to) stop this program */
event_del(&ev_periodic);
return;
}
event_set(&ev_periodic, -1, 0, periodic, ctx);
event_add(&ev_periodic, &tv);
}
static void
on_resolved(int res, char t, int c, int ttl, void *addr, void *arg)
{
int ival = (intptr_t)arg;
DBG(("%s(result[%d]=%d)\n", __func__, ival, res));
assert(0 <= ival);
assert(ival < nitems(result));
result[ival] = res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment