Subject: What is the correct way of integrating c-ares in a separate project

What is the correct way of integrating c-ares in a separate project

From: Austin Einter via c-ares <c-ares_at_cool.haxx.se>
Date: Mon, 31 Jul 2017 17:10:54 +0530

Hi All
I have a basic question regarding c-ares integration to my project.

By referring some online help, I have integrated it. However it is working
till sometime, after that it is crashing.
The stack trace is shown as above.

*(gdb) bt*
*#0 0x00007f959c01ac37 in __GI_raise (sig=sig_at_entry=6) at
../nptl/sysdeps/unix/sysv/linux/raise.c:56*
*#1 0x00007f959c01e028 in __GI_abort () at abort.c:89*
*#2 0x00007f959c0572a4 in __libc_message (do_abort=do_abort_at_entry=2,
fmt=fmt_at_entry=0x7f959c166d70 "*** %s ***: %s terminated\n")*
* at ../sysdeps/posix/libc_fatal.c:175*
*#3 0x00007f959c0f283c in __GI___fortify_fail (msg=<optimized out>,
msg_at_entry=0x7f959c166d07 "buffer overflow detected") at fortify_fail.c:38*
*#4 0x00007f959c0f1710 in __GI___chk_fail () at chk_fail.c:28*
*#5 0x00007f959c0f2787 in __fdelt_chk (d=<optimized out>) at
fdelt_chk.c:25*
*#6 0x00007f959c6b69ad in ares_fds () from
/usr/local/multiplier/system/libs/libcares.so.2*
*#7 0x000000000040b448 in rec_c_ares_execute () at
/home/necs/dev/apat/source/recorder/recdns.c:157*
*#8 0x00000000004052f2 in rec_main_thread (data=0x0) at
/home/necs/dev/apat/source/recorder/rec.c:772*
*#9 0x0000000000403de1 in main (argc=7, argv=0x7fff58cde398) at
/home/necs/dev/apat/source/recorder/main.c:129*
*(gdb) *

Let me explain, how I have integrated, please guide me where I am wrong.

I have initalized c-ares using below code.
From my main code I call below function to initialize c-ares.

*bool rec_c_ares_init()*
*{*
* int status;*
* int optmask = 0;*
* struct ares_options options;*

* status = ares_library_init(ARES_LIB_INIT_ALL);*
* if (status != ARES_SUCCESS)*
* {*
* printf("Failed to init c-ares\n");*
* goto err;*
* }*

* options.sock_state_cb = rec_c_ares_state_cb;*
* optmask |= ARES_OPT_SOCK_STATE_CB;*

* status = ares_init_options(&g_c_ares_channel, &options, optmask);*
* if(status != ARES_SUCCESS) *
* {*
* printf("Failed to init options in c-ares\n");*
* goto err;*
* }*

* ares_set_servers_csv(g_c_ares_channel, g_dns_servers);*

* printf("Successfully initialized c-ares lib\n");*
* return true;*

*err:*
* return false;*
*}*

*Then I have a main loop, and from main loop I call below function so that
dns *resolution is done and callback is called.
*void rec_c_ares_execute()*
*{*
* int nfds;*
* struct timeval *tvp, tv;*
* fd_set read_fds;*
* fd_set write_fds;*

* FD_ZERO(&read_fds);*
* FD_ZERO(&write_fds);*

* nfds = ares_fds(g_c_ares_channel, &read_fds, &write_fds);*
* if(nfds <= 0)*
* {*
* return;*
* }*

* tvp = ares_timeout(g_c_ares_channel, NULL, &tv);*
* select(nfds, &read_fds, &write_fds, NULL, tvp);*
* ares_process(g_c_ares_channel, &read_fds, &write_fds);*
*}*

Whenever I want to resolve a domain name, I call below function
*void rec_c_ares_gethostbyname(char *host, void *udata)*
*{*
* int length;*

* if(host == NULL)*
* {*
* printf("Query is being tried for NULL string\n");*
* }*
* else if(strlen(host) == 0)*
* {*
* printf("Query is being tried for BLANK string\n");*
* }*
* else*
* {*
* printf("Starting DNS resolution for %s\n", host);*
* ares_gethostbyname(g_c_ares_channel, host, AF_INET,
rec_c_ares_cb_1, udata);*
* }*
*}*

Please note that I will be calling rec_c_ares_execute() very frequently,
around 5 times a second. I hope it should not cause any issue.

Please let me know where I am wrong, and how do I avoid crash issue.
Received on 2017-07-31