michael@0: /* michael@0: This example code shows how to use the high-level, low-level, and michael@0: server-level interfaces of evdns. michael@0: michael@0: XXX It's pretty ugly and should probably be cleaned up. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: /* Compatibility for possible missing IPv6 declarations */ michael@0: #include "../ipv6-internal.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef _EVENT_HAVE_NETINET_IN6_H michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define u32 ev_uint32_t michael@0: #define u8 ev_uint8_t michael@0: michael@0: static const char * michael@0: debug_ntoa(u32 address) michael@0: { michael@0: static char buf[32]; michael@0: u32 a = ntohl(address); michael@0: evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", michael@0: (int)(u8)((a>>24)&0xff), michael@0: (int)(u8)((a>>16)&0xff), michael@0: (int)(u8)((a>>8 )&0xff), michael@0: (int)(u8)((a )&0xff)); michael@0: return buf; michael@0: } michael@0: michael@0: static void michael@0: main_callback(int result, char type, int count, int ttl, michael@0: void *addrs, void *orig) { michael@0: char *n = (char*)orig; michael@0: int i; michael@0: for (i = 0; i < count; ++i) { michael@0: if (type == DNS_IPv4_A) { michael@0: printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i])); michael@0: } else if (type == DNS_PTR) { michael@0: printf("%s: %s\n", n, ((char**)addrs)[i]); michael@0: } michael@0: } michael@0: if (!count) { michael@0: printf("%s: No answer (%d)\n", n, result); michael@0: } michael@0: fflush(stdout); michael@0: } michael@0: michael@0: static void michael@0: gai_callback(int err, struct evutil_addrinfo *ai, void *arg) michael@0: { michael@0: const char *name = arg; michael@0: int i; michael@0: if (err) { michael@0: printf("%s: %s\n", name, evutil_gai_strerror(err)); michael@0: } michael@0: if (ai && ai->ai_canonname) michael@0: printf(" %s ==> %s\n", name, ai->ai_canonname); michael@0: for (i=0; ai; ai = ai->ai_next, ++i) { michael@0: char buf[128]; michael@0: if (ai->ai_family == PF_INET) { michael@0: struct sockaddr_in *sin = michael@0: (struct sockaddr_in*)ai->ai_addr; michael@0: evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, michael@0: sizeof(buf)); michael@0: printf("[%d] %s: %s\n",i,name,buf); michael@0: } else { michael@0: struct sockaddr_in6 *sin6 = michael@0: (struct sockaddr_in6*)ai->ai_addr; michael@0: evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, michael@0: sizeof(buf)); michael@0: printf("[%d] %s: %s\n",i,name,buf); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: evdns_server_callback(struct evdns_server_request *req, void *data) michael@0: { michael@0: int i, r; michael@0: (void)data; michael@0: /* dummy; give 192.168.11.11 as an answer for all A questions, michael@0: * give foo.bar.example.com as an answer for all PTR questions. */ michael@0: for (i = 0; i < req->nquestions; ++i) { michael@0: u32 ans = htonl(0xc0a80b0bUL); michael@0: if (req->questions[i]->type == EVDNS_TYPE_A && michael@0: req->questions[i]->dns_question_class == EVDNS_CLASS_INET) { michael@0: printf(" -- replying for %s (A)\n", req->questions[i]->name); michael@0: r = evdns_server_request_add_a_reply(req, req->questions[i]->name, michael@0: 1, &ans, 10); michael@0: if (r<0) michael@0: printf("eeep, didn't work.\n"); michael@0: } else if (req->questions[i]->type == EVDNS_TYPE_PTR && michael@0: req->questions[i]->dns_question_class == EVDNS_CLASS_INET) { michael@0: printf(" -- replying for %s (PTR)\n", req->questions[i]->name); michael@0: r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name, michael@0: "foo.bar.example.com", 10); michael@0: if (r<0) michael@0: printf("ugh, no luck"); michael@0: } else { michael@0: printf(" -- skipping %s [%d %d]\n", req->questions[i]->name, michael@0: req->questions[i]->type, req->questions[i]->dns_question_class); michael@0: } michael@0: } michael@0: michael@0: r = evdns_server_request_respond(req, 0); michael@0: if (r<0) michael@0: printf("eeek, couldn't send reply.\n"); michael@0: } michael@0: michael@0: static int verbose = 0; michael@0: michael@0: static void michael@0: logfn(int is_warn, const char *msg) { michael@0: if (!is_warn && !verbose) michael@0: return; michael@0: fprintf(stderr, "%s: %s\n", is_warn?"WARN":"INFO", msg); michael@0: } michael@0: michael@0: int michael@0: main(int c, char **v) { michael@0: int idx; michael@0: int reverse = 0, servertest = 0, use_getaddrinfo = 0; michael@0: struct event_base *event_base = NULL; michael@0: struct evdns_base *evdns_base = NULL; michael@0: const char *resolv_conf = NULL; michael@0: if (c<2) { michael@0: fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] hostname\n", v[0]); michael@0: fprintf(stderr, "syntax: %s [-servertest]\n", v[0]); michael@0: return 1; michael@0: } michael@0: idx = 1; michael@0: while (idx < c && v[idx][0] == '-') { michael@0: if (!strcmp(v[idx], "-x")) michael@0: reverse = 1; michael@0: else if (!strcmp(v[idx], "-v")) michael@0: verbose = 1; michael@0: else if (!strcmp(v[idx], "-g")) michael@0: use_getaddrinfo = 1; michael@0: else if (!strcmp(v[idx], "-servertest")) michael@0: servertest = 1; michael@0: else if (!strcmp(v[idx], "-c")) { michael@0: if (idx + 1 < c) michael@0: resolv_conf = v[++idx]; michael@0: else michael@0: fprintf(stderr, "-c needs an argument\n"); michael@0: } else michael@0: fprintf(stderr, "Unknown option %s\n", v[idx]); michael@0: ++idx; michael@0: } michael@0: michael@0: #ifdef WIN32 michael@0: { michael@0: WSADATA WSAData; michael@0: WSAStartup(0x101, &WSAData); michael@0: } michael@0: #endif michael@0: michael@0: event_base = event_base_new(); michael@0: evdns_base = evdns_base_new(event_base, 0); michael@0: evdns_set_log_fn(logfn); michael@0: michael@0: if (servertest) { michael@0: evutil_socket_t sock; michael@0: struct sockaddr_in my_addr; michael@0: sock = socket(PF_INET, SOCK_DGRAM, 0); michael@0: if (sock == -1) { michael@0: perror("socket"); michael@0: exit(1); michael@0: } michael@0: evutil_make_socket_nonblocking(sock); michael@0: my_addr.sin_family = AF_INET; michael@0: my_addr.sin_port = htons(10053); michael@0: my_addr.sin_addr.s_addr = INADDR_ANY; michael@0: if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) { michael@0: perror("bind"); michael@0: exit(1); michael@0: } michael@0: evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL); michael@0: } michael@0: if (idx < c) { michael@0: int res; michael@0: #ifdef WIN32 michael@0: if (resolv_conf == NULL) michael@0: res = evdns_base_config_windows_nameservers(evdns_base); michael@0: else michael@0: #endif michael@0: res = evdns_base_resolv_conf_parse(evdns_base, michael@0: DNS_OPTION_NAMESERVERS, michael@0: resolv_conf ? resolv_conf : "/etc/resolv.conf"); michael@0: michael@0: if (res < 0) { michael@0: fprintf(stderr, "Couldn't configure nameservers"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME); michael@0: for (; idx < c; ++idx) { michael@0: if (reverse) { michael@0: struct in_addr addr; michael@0: if (evutil_inet_pton(AF_INET, v[idx], &addr)!=1) { michael@0: fprintf(stderr, "Skipping non-IP %s\n", v[idx]); michael@0: continue; michael@0: } michael@0: fprintf(stderr, "resolving %s...\n",v[idx]); michael@0: evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[idx]); michael@0: } else if (use_getaddrinfo) { michael@0: struct evutil_addrinfo hints; michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_protocol = IPPROTO_TCP; michael@0: hints.ai_flags = EVUTIL_AI_CANONNAME; michael@0: fprintf(stderr, "resolving (fwd) %s...\n",v[idx]); michael@0: evdns_getaddrinfo(evdns_base, v[idx], NULL, &hints, michael@0: gai_callback, v[idx]); michael@0: } else { michael@0: fprintf(stderr, "resolving (fwd) %s...\n",v[idx]); michael@0: evdns_base_resolve_ipv4(evdns_base, v[idx], 0, main_callback, v[idx]); michael@0: } michael@0: } michael@0: fflush(stdout); michael@0: event_base_dispatch(event_base); michael@0: return 0; michael@0: } michael@0: