-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Description
I did this
I have an application running on Free BSD on a i386 machine.The application wakes up every 10 seconds and tries to do a curl_easy_perform() .
1-At the beginning , I don't have the DNS server config on my system , hence curl_easy_perform fails (ON DNS resolution) .
2- Now I added the DNS server config and after that ping/host command etc works on that domain name
ping google.com
PING6(56=40+8+8 bytes) :: --> 2404:6800:4003:804::200e
host google.com
google.com has address 172.217.160.14
However curl_easy_perform() keeps failing for the DNS resolution.
The problem resolves only when I restart my application after adding the DNS server configuration on the system .
Please note I have not done curl_global_cleanup() on failure , hence curl_global_init() won't be done on every attempt .I am wondering if that causes some missing initializations with respect to DNS as the DNS server config was missing when curl_easy_init() (and hence implicit curl_global_init() ) was done for the first time .
cat /etc/nsswitch.conf
nsswitch.conf - name service switch configuration file
$Id:$
passwd: files sdk
Code
static size_t
write_data_in_mem (void *buffer, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct memStruct *in_mem = (struct memStruct *) userp;
in_mem->buff = realloc(in_mem->buff, in_mem->buff_size + realsize + 1);
if (in_mem->buff == NULL) {
syslog(LOG_ERR, "failed to allocate %d bytes for buffer",
realsize+1);
return 0;
}
bzero(&(in_mem->buff[in_mem->buff_size - 1]), realsize+1);
memcpy(&(in_mem->buff[in_mem->buff_size - 1]), buffer, realsize);
in_mem->buff_size += realsize;
in_mem->buff[in_mem->buff_size] = 0;
return realsize;
}
static CURL*
setup_curl_options (char *login_passwd, void *write_func,
void *hdrdata, void *write_data,
char *server_url)
{
CURL *curl_handle = NULL;
object_t *obj = get_object();
long port = obj->curr_port;
unsigned char *cert = obj->curr_cert;
boolean is_https = FALSE;
char *real_url = NULL;
curl_handle = curl_easy_init();
if (!curl_handle) {
PRINT("failed to initialize curl obj");
return NULL;
}
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 10L);
if (strstr(server_url, "https://") != NULL) {
is_https = TRUE;
curl_easy_setopt(curl_handle, CURLOPT_CAINFO, obj->ca_cert_file);
}
if (is_https ==TRUE) {
/* SSL options */
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER , 1);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST , 2);
}
/* providing login/passwd */
curl_easy_setopt(curl_handle, CURLOPT_USERPWD, login_passwd);
/* send the output to particular function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_func);
/* pass structure to write function */
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, (void *)hdrdata);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)write_data);
if (NULL != (real_url = get_complete_url_str(server_url, is_https))) {
curl_easy_setopt(curl_handle, CURLOPT_URL, real_url);
free(real_url);
} else {
curl_easy_setopt(curl_handle, CURLOPT_URL, server_url);
}
PRINT("Current Port:%ld", port);
if (port) {
curl_easy_setopt(curl_handle, CURLOPT_PORT, port);
}
return curl_handle;
}
int contact_server (const char* name, char *login_passwd, char * server_url )
{
CURL *curl_handle = NULL;
CURLcode res = CURLE_COULDNT_CONNECT;
struct memStruct header = {NULL, 0};
struct memStruct body = {NULL, 0};
long resp_code = 0;
int retval = -1;
/* init buffer to save server response */
header.buff = calloc(1, 1);
header.buff_size = 1;
body.buff = calloc(1, 1);
body.buff_size = 1;
curl_handle = setup_curl_options(login_passwd, write_data_in_mem,
(void*)&header, (void*)&body,
server_url);
if (!curl_handle) {
PRINT("failed to initialize curl obj");
free(header.buff);
free(body.buff);
return retval;
}
res = curl_easy_perform(curl_handle);
/* Check for errors */
if (res != CURLE_OK) {
PRINT("curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
retval = -1;
}
} else {
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &resp_code);
retval = 0 ;
}
free(header.buff);
free(body.buff);
if (curl_handle) { /* cleanup */
curl_easy_cleanup(curl_handle);
}
return retval;
}
thread_main(void)
{
int retval = -1;
while(1)
{
retval = contact_server("server1", "pwd", "https://google.com")
if(retval == -1)
{
sleep(10);
}
else {
break;
}
}
}
I expected the following
curl/libcurl version
7.43.0
[curl -V output]
operating system
FreeBSD 6