This topic describes how to integrate HTTPDNS SDK for C.
Step 1: Prepare the environment
The integration of the SDK depends on necessary build tools and third-party libraries. Before you integrate the SDK, install the dependencies on your build machine.
Name | Description | Version |
git | Versioning tool | 1.8 and later. |
cmake | Build tool | 3.0 and later. |
gcc | Compilation tool | 4.5 and later. |
vcpkg (Optional) | Dependency library management tool | We recommend that you use the latest version. |
libcurl | Application-layer protocol library | 7.33.0 and later. |
apr/apr-util | Cross-platform component library for C and C++ | 1.5.2 and later. |
cjson | JSON string parser library | We recommend that you use the latest version. |
1. Install a build tool
During the build process, you must clone code from GitHub, use CMake to build projects, and use GCC or G++ to compile code. Make sure that the following CLIs are installed on your computer. If not, run the provided commands to install them:
Ubuntu or Debian
sudo apt update sudo apt install -y git cmake gcc g++Alibaba Cloud, CentOS Stream, or Fedora
sudo yum check-update sudo yum install -y git cmake gcc gcc-c++OpenSUSE
sudo zypper refresh sudo zypper install -y git cmake gcc gcc-c++macOS
export HOMEBREW_NO_AUTO_UPDATE=1 brew install git gcc cmakeNotebrew is a package manager. It is not built in macOS. You must install brew before you install a package.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Windows
Download and install Visual Studio. For the workload, select Desktop development with C++.
2. Install a dependency library
The SDK uses libcurl 7.33.0 and later for network operations, apr or apr-util 1.5.2 and later to resolve memory management and cross-platform issues, and cJSON to parse server-side response messages. The SDK does not include these external libraries. Make sure that the libraries are installed, and add their header and library file directories to your project. In this example, the project supports VCPKG-based installation and manual installation of the C or C++ libraries.
2.1 Installation using VCPKG
Install the dependency libraries of the SDK.
macOS or Linux
./vcpkg install apr apr-util curl[openssl,http2] cjsonWindows
.\vcpkg.exe install apr apr-util curl[openssl,http2] cjsonNoteBy default, VCPKG installs libraries that correspond to the current platform. To perform cross-platform compilation, such as compiling an x86 library in a Windows x64 environment, you must specify a triplet. The following command is an example:
./vcpkg.exe install apr:x86-windows apr-util:x86-windows curl[openssl,http2]:x86-windows cjson:x86-windows
2.2 Manual installation
Ubuntu or Debian
sudo apt update sudo apt install -y libcurl4-openssl-dev libapr1-dev libaprutil1-dev libcjson-devAlibaba Cloud, CentOS Stream, or Fedora
sudo yum check-update sudo yum install -y libcurl-devel apr-devel apr-util-devel cjson-develOpenSUSE
sudo zypper refresh sudo zypper install -y libcurl-devel libapr1-devel libapr-util1-devel cJSON-develmacOS
brew install curl apr apr-util cjsonWindows
If the cJSON package cannot be automatically installed using the UNIX-like platform package manager, run the following command to install it:
git clone https://github.com/DaveGamble/cJSON.git && cd cJSON && mkdir build && cd build && cmake ../ && sudo make install && cd ../../ && rm -rf cJSONStep 2: Install the SDK
Linux or macOS
git clone https://github.com/aliyun/alibabacloud-httpdns-c-sdk.git cd alibabacloud-httpdns-c-sdk mkdir build cd build # If the dependency library is installed using VCPKG, add the Cmake parameter: -DVCPKG_ROOT=${Installation directory of VCPKG} cmake -DCMAKE_BUILD_TYPE=Release ../ make hdns_unite_test sudo make install sudo ldconfigWindows
Download the CMake project.
Open the CMake project in Visual Studio.
Add the Cmake parameter in the management configuration: -DVCPKG_ROOT=${Installation directory of VCPKG}
Step 3: Integrate the SDK
For more information about how to integrate the SDK, see Integration example. The following sections describe how to integrate the SDK.
3.1. Import the SDK
To integrate the SDK, you must import the locally installed libraries and header files. For example, add the following commands to the CMakeLists.txt file of a CMake project:
find_library(HTTPDNS_LIBRARY httpdns_c_sdk_static)
include_directories(${CMAKE_INSTALL_PREFIX}/include/httpdns)3.2 Initialize the SDK
Initialize the SDK runtime environment.
if (hdns_sdk_init() != HDNS_OK) {
hdns_sdk_cleanup();
}
// Use the API of the SDK.
3.3 Create a client
hdns_client_t *client = hdns_client_create(HTTPDNS_ACCOUNT, HTTPDNS_SECRET);
if (client == NULL) {
hdns_sdk_cleanup();
}
// Use HTTPDNS Client.HTTPDNS_ACCOUNT specifies the account ID assigned by HTTPDNS to an account. For more information about how to obtain the ID, see Use the service.
HTTPDNS_SECRET specifies the key used to sign the request. If authentication is not required for resolution, set this parameter to NULL. If authentication is required, you must specify this parameter. For more information, see Development and configuration.
3.4 Configure the client
After a client is created, you can configure the following settings to customize the HTTPDNS client:
// The request timeout period. Unit: milliseconds.
hdns_client_set_timeout(client, 2000);
// Specify whether to enable local caching.
hdns_client_set_using_cache(client, true);
// Specify whether to use HTTPS to access the HTTPDNS server.
hdns_client_set_using_https(client, true);
// Specify whether to sign a request.
hdns_client_set_using_sign(client, true);
// The number of request retries.
hdns_client_set_retry_times(client, 1);
// The HTTPDNS service cluster.
hdns_client_set_region(client, "global");
// The HTTPDNS scheduling cluster.
hdns_client_set_schedule_center_region(client, "cn");
// Specify whether to update the local cache after a network change.
hdns_client_enable_update_cache_after_net_change(client, true);
// Specify whether to allow HTTPDNS to obtain the expired data from the cache.
hdns_client_enable_expired_ip(client, true);
// Specify whether to automatically use local Domain Name System (DNS) servers.
hdns_client_enable_failover_localdns(client, true);
// The domain name that you want to pre-resolve.
hdns_client_add_pre_resolve_host(client, "www.aliyun.com");
// Add IP sniffing.
hdns_client_add_ip_probe_item(client, "www.aliyun.com", 443);
// The custom time to live (TTL).
hdns_client_add_custom_ttl_item(client, "www.aliyun.com", 120);
If you set the hdns_client_set_using_https parameter to `true`, your costs increase. For more information, see the Product Billing document.
3.5 Start the client
hdns_client_start(client);3.6 Resolve a domain name
After you start the client, you can call the API operations provided by the SDK to resolve domain names. The SDK provides multiple operations for different scenarios. The following code provides an example on resolving a single domain name by calling a synchronous operation.
hdns_list_head_t *results = NULL;
hdns_status_t status = hdns_get_result_for_host_sync_with_cache(client,
MOCK_BUSINESS_HOST,
HDNS_QUERY_AUTO,
NULL, &results);3.7 Select an IP address
After you obtain the resolution result of the domain name, you can access the workloads of your customer using the IP address to which the domain name is resolved.
if (hdns_status_is_ok(&status)) {
char ip[HDNS_IP_ADDRESS_STRING_LENGTH];
if (hdns_select_ip_randomly(results, HDNS_QUERY_AUTO, ip) == HDNS_OK) {
mock_access_business_web_server(ip);
}
}
if (hdns_status_is_ok(&status)) {
char ip[HDNS_IP_ADDRESS_STRING_LENGTH];
if (hdns_select_ip_randomly(results, HDNS_QUERY_AUTO, ip) == HDNS_OK) {
mock_access_business_web_server(ip);
}
}
hdns_list_free(results);3.8 Access the workloads
static void mock_access_business_web_server(const char *dst_ip) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
// Concatenate the request URL.
char url[256];
strcpy(url, "https://");
strcat(url, MOCK_BUSINESS_HOST);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
// Specify the pre-resolved host and IP address for HTTPS requests.
struct curl_slist *dns;
char sni[256];
strcpy(sni, MOCK_BUSINESS_HOST);
strcat(sni, ":443:");
strcat(sni, dst_ip);
dns = curl_slist_append(NULL, sni);
curl_easy_setopt(curl, CURLOPT_RESOLVE, dns);
// The callback.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_callback);
#if defined(_WIN32)
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
#endif
// Initiate an HTTP request.
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed, url=%s, ip=%s, error=%s\n",
url,
dst_ip,
curl_easy_strerror(res));
}
// Release the resources related to workload access.
curl_slist_free_all(dns);
/* always cleanup */
curl_easy_cleanup(curl);
}
}
3.9 Client Cleanup
If the client is no longer required, you must release it.
hdns_client_cleanup(client);3.10 Release SDK resources
If the SDK resources are no longer required, you must release them.
hdns_sdk_cleanup();