/* $NetBSD: dns-example.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $ */
/*
This example code shows how to use the high-level, low-level, and
server-level interfaces of evdns.
XXX It's pretty ugly and should probably be cleaned up.
*/
#include <event2/event-config.h>
/* Compatibility for possible missing IPv6 declarations */
#include "../ipv6-internal.h"
static void
main_callback(int result, char type, int count, int ttl,
void *addrs, void *orig) {
char *n = (char*)orig;
int i;
for (i = 0; i < count; ++i) {
if (type == DNS_IPv4_A) {
printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
} else if (type == DNS_PTR) {
printf("%s: %s\n", n, ((char**)addrs)[i]);
}
}
if (!count) {
printf("%s: No answer (%d)\n", n, result);
}
fflush(stdout);
}
static void
evdns_server_callback(struct evdns_server_request *req, void *data)
{
int i, r;
(void)data;
/* dummy; give 192.168.11.11 as an answer for all A questions,
* give foo.bar.example.com as an answer for all PTR questions. */
for (i = 0; i < req->nquestions; ++i) {
u32 ans = htonl(0xc0a80b0bUL);
if (req->questions[i]->type == EVDNS_TYPE_A &&
req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
printf(" -- replying for %s (A)\n", req->questions[i]->name);
r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
1, &ans, 10);
if (r<0)
printf("eeep, didn't work.\n");
} else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
"foo.bar.example.com", 10);
if (r<0)
printf("ugh, no luck");
} else {
printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
req->questions[i]->type, req->questions[i]->dns_question_class);
}
}
r = evdns_server_request_respond(req, 0);
if (r<0)
printf("eeek, couldn't send reply.\n");
}