I am trying to look up domain asynchronously in c++. The reason is I want to be able to effectively add a time out period in case the system can't look up the domain. I came across the getaddrinfo_a() command so I decided to give it a try. However cancelling any dns look up that will not succeed (such as when there is no internet connection) will never take less than 20 seconds on my machine. Here is a simple example of this:
#include <iostream>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main() {
int ret;
gaicb* reqs;
reqs = new gaicb;
memset(reqs, 0, sizeof (gaicb));
reqs->ar_name = "google.com";
ret = getaddrinfo_a(GAI_NOWAIT, &reqs, 1, NULL);
if (ret != 0) {
cout << "something went wrong" << endl;
return false;
}
while (1) {
ret = gai_cancel(reqs);
if (ret == EAI_CANCELED || ret == EAI_ALLDONE) {
break;
}
usleep(100 * 1000); //sleep for 100 milliseconds
}
cout << "finished cancellation" << endl;
return 0;
}
Compile like this:
g++ -o main main.cpp -lanl
Then run the command on your linux based system without an internet connection like so:
time ./main
You will find that the program always takes about 20 seconds to close. Any help would be greatly appreciated!