diff --git a/.ycm_extra_conf.py b/.ycm_extra_conf.py index af24a43..9297ea5 100644 --- a/.ycm_extra_conf.py +++ b/.ycm_extra_conf.py @@ -92,6 +92,8 @@ '-I', './library/qrencode/src/include', '-I', +'./library/libcares/src', +'-I', './library/cJSON', '-I', '/home/linuxbrew/.linuxbrew/include/', diff --git a/library/libcares/src/dns.c b/library/libcares/src/dns.c index 5f07d9e..4d4269e 100644 --- a/library/libcares/src/dns.c +++ b/library/libcares/src/dns.c @@ -16,7 +16,6 @@ static void state_cb(void * data, int s, int read, int write) { printf("Change state fd %d read:%d write:%d\n", s, read, write); } - static void callback(void * arg, int status, int timeouts, struct hostent * host) { if(!host || status != ARES_SUCCESS){ fprintf(stderr, "Failed to lookup %s\n", ares_strerror(status)); @@ -33,26 +32,6 @@ static void callback(void * arg, int status, int timeouts, struct hostent * host } } -static void wait_ares(ares_channel_t * channel) { - for(;;){ - struct timeval *tvp, tv; - fd_set read_fds, write_fds; - - FD_ZERO(&read_fds); - FD_ZERO(&write_fds); - - int nfds = ares_fds(channel, &read_fds, &write_fds); - - if (nfds == 0){ - break; - } - - tvp = ares_timeout(channel, NULL, &tv); - select(nfds, &read_fds, &write_fds, NULL, tvp); - ares_process(channel, &read_fds, &write_fds); - } -} - int dns_lookup(const char * domain) { int ret = ares_library_init(ARES_LIB_INIT_ALL); @@ -61,17 +40,9 @@ int dns_lookup(const char * domain) { return ret; } - struct ares_options options; - //options.sock_state_cb_data; - options.sock_state_cb = state_cb; - - int optmask = 0; - - optmask |= ARES_OPT_SOCK_STATE_CB; - ares_channel_t * channel = NULL; - ret = ares_init_options(&channel, &options, optmask); + ret = ares_init_options(&channel, NULL, 0); if (ret != ARES_SUCCESS) { fprintf(stderr, "%s\n", ares_strerror(ret)); @@ -85,9 +56,25 @@ int dns_lookup(const char * domain) { return ret; } - ares_gethostbyname(channel, domain, AF_INET, callback, NULL); - //ares_gethostbyname(channel, "google.com", AF_INET6, callback, NULL); - wait_ares(channel); + ares_gethostbyname(channel, domain, AF_UNSPEC, callback, NULL); + + for(;;) { + struct timeval *tvp, tv; + fd_set read_fds, write_fds; + + FD_ZERO(&read_fds); + FD_ZERO(&write_fds); + + int nfds = ares_fds(channel, &read_fds, &write_fds); + + if (nfds == 0){ + break; + } + + tvp = ares_timeout(channel, NULL, &tv); + select(nfds, &read_fds, &write_fds, NULL, tvp); + ares_process(channel, &read_fds, &write_fds); + } ares_destroy(channel); diff --git a/library/libcares/src/dns2.c b/library/libcares/src/dns2.c new file mode 100644 index 0000000..4417e9d --- /dev/null +++ b/library/libcares/src/dns2.c @@ -0,0 +1,91 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include + +static void callback(void * arg, int status, int timeouts, struct ares_addrinfo * result) { + printf("Result: %s, timeouts: %d\n", ares_strerror(status), timeouts); + + if (result != NULL) { + struct ares_addrinfo_node * node; + + for (node = result->nodes; node != NULL; node = node->ai_next) { + char addr_buf[64] = ""; + const void *ptr = NULL; + + if (node->ai_family == AF_INET) { + const struct sockaddr_in *in_addr = (const struct sockaddr_in *)((void *)node->ai_addr); + ptr = &in_addr->sin_addr; + } else if (node->ai_family == AF_INET6) { + const struct sockaddr_in6 *in_addr = (const struct sockaddr_in6 *)((void *)node->ai_addr); + ptr = &in_addr->sin6_addr; + } else { + continue; + } + + ares_inet_ntop(node->ai_family, ptr, addr_buf, sizeof(addr_buf)); + printf("%s\n", addr_buf); + } + + ares_freeaddrinfo(result); + } +} + +int dns_lookup(const char * domain) { + int ret = ares_library_init(ARES_LIB_INIT_ALL); + + if (ret != ARES_SUCCESS){ + fprintf(stderr, "%s\n", ares_strerror(ret)); + return ret; + } + + ares_channel_t * channel = NULL; + + struct ares_options options; + memset(&options, 0, sizeof(options)); + options.evsys = ARES_EVSYS_DEFAULT; + + ret = ares_init_options(&channel, &options, ARES_OPT_EVENT_THREAD); + + if (ret != ARES_SUCCESS) { + fprintf(stderr, "%s\n", ares_strerror(ret)); + return ret; + } + + ret = ares_set_servers_ports_csv(channel, "8.8.8.8"); + + if (ret != ARES_SUCCESS) { + fprintf(stderr, "%s\n", ares_strerror(ret)); + return ret; + } + + struct ares_addrinfo_hints hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = ARES_AI_CANONNAME; + + ares_getaddrinfo(channel, domain, NULL, &hints, callback, NULL); + + ret = ares_queue_wait_empty(channel, -1); + + ares_destroy(channel); + + ares_library_cleanup(); + + return ret; +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + fprintf(stderr, "Usage: %s ", argv[0]); + return 1; + } + + return dns_lookup(argv[1]); +}