Skip to content

Commit

Permalink
Do proper curl error checking for domain lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Dec 22, 2023
1 parent 8c74aed commit 7470a9a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
49 changes: 42 additions & 7 deletions src/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,47 @@ static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");

curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(c_handle, CURLOPT_URL, NODES_LIST_URL);
curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);
curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data);
curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(c_handle, CURLOPT_HTTPGET, 1L);
int ret = curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set http headers (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_URL, NODES_LIST_URL);

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set url (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set write function callback (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data);

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set write data (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set useragent (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_HTTPGET, 1L);

if (ret != CURLE_OK) {
fprintf(stderr, "Failed to set get request (libcurl error %d)", ret);
goto on_exit;
}

int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type);

Expand All @@ -226,7 +261,7 @@ static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data)
goto on_exit;
}

int ret = curl_easy_setopt(c_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
ret = curl_easy_setopt(c_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

if (ret != CURLE_OK) {
fprintf(stderr, "TLSv1.2 could not be set (libcurl error %d)", ret);
Expand Down
44 changes: 37 additions & 7 deletions src/name_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,47 @@ void *lookup_thread_func(void *data)

headers = curl_slist_append(headers, "charsets: utf-8");

curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);
int ret = curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(c_handle, CURLOPT_URL, real_domain);
if (ret != CURLE_OK) {
lookup_error(self, "Failed to set http headers (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_URL, real_domain);

if (ret != CURLE_OK) {
lookup_error(self, "Failed to set url (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);

if (ret != CURLE_OK) {
lookup_error(self, "Failed to set write function callback (libcurl error %d)", ret);
goto on_exit;
}

ret = curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data);

if (ret != CURLE_OK) {
lookup_error(self, "Failed to set write data (libcurl error %d)", ret);
goto on_exit;
}

curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data);
ret = curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data);
if (ret != CURLE_OK) {
lookup_error(self, "Failed to set useragent (libcurl error %d)", ret);
goto on_exit;
}

curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
ret = curl_easy_setopt(c_handle, CURLOPT_POSTFIELDS, post_data);

curl_easy_setopt(c_handle, CURLOPT_POSTFIELDS, post_data);
if (ret != CURLE_OK) {
lookup_error(self, "Failed to set post data (libcurl error %d)", ret);
goto on_exit;
}

int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type);

Expand All @@ -308,7 +338,7 @@ void *lookup_thread_func(void *data)
goto on_exit;
}

int ret = curl_easy_setopt(c_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
ret = curl_easy_setopt(c_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);

if (ret != CURLE_OK) {
lookup_error(self, "TLS could not be enabled (libcurl error %d)", ret);
Expand Down

0 comments on commit 7470a9a

Please sign in to comment.