Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
leleliu008 committed Jan 24, 2025
1 parent 4363292 commit e5d05aa
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .ycm_extra_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
'-I',
'./library/qrencode/src/include',
'-I',
'./library/libcares/src',
'-I',
'./library/cJSON',
'-I',
'/home/linuxbrew/.linuxbrew/include/',
Expand Down
53 changes: 20 additions & 33 deletions library/libcares/src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);

Expand 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));
Expand All @@ -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);

Expand Down
91 changes: 91 additions & 0 deletions library/libcares/src/dns2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include <ares.h>

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 <domain>", argv[0]);
return 1;
}

return dns_lookup(argv[1]);
}

0 comments on commit e5d05aa

Please sign in to comment.