@@ -5,13 +5,115 @@ layout: page
5
5
permalink : /docs/
6
6
---
7
7
8
+ - [ Manpages] ( #manpages )
9
+ - [ Example] ( #example )
10
+
8
11
c-ares provides a set of library functions, datatypes, and enumerations
9
12
which integrators will use for their implementations. When you install c-ares,
10
13
you get man pages which describe their use and meanings.
11
14
15
+ ## Manpages
16
+
12
17
{% assign docs_paths = site.pages | where_exp: "page", "page.path contains 'docs/'" | map: "path" | sort -%}
13
18
{%- for path in docs_paths -%}
14
19
{%- assign my_page = site.pages | where: "path", path | first -%}
15
20
- [ {{ my_page.path | replace: "docs/", "" | replace: ".html", "" }}] (/{{ my_page.path }})
16
21
{% endfor -%}
17
22
23
+ ## Example
24
+ A simple name resolve might look like:
25
+
26
+ ` example.c ` :
27
+ {% highlight C %}
28
+ #include <stdio.h>
29
+ #include <string.h>
30
+ #include <ares.h>
31
+
32
+ /* Callback that is called when DNS query is finished * /
33
+ static void addrinfo_cb(void * arg, int status, int timeouts,
34
+ struct ares_addrinfo * result)
35
+ {
36
+ (void)arg; /* Example does not use user context * /
37
+ printf("Result: %s, timeouts: %d\n", ares_strerror(status), timeouts);
38
+
39
+ if (result) {
40
+ struct ares_addrinfo_node * node;
41
+ for (node = result->nodes; node != NULL; node = node->ai_next) {
42
+ char addr_buf[ 64] = "";
43
+ const void * ptr = NULL;
44
+ if (node->ai_family == AF_INET) {
45
+ const struct sockaddr_in * in_addr =
46
+ (const struct sockaddr_in * )((void * )node->ai_addr);
47
+ ptr = &in_addr->sin_addr;
48
+ } else if (node->ai_family == AF_INET6) {
49
+ const struct sockaddr_in6 * in_addr =
50
+ (const struct sockaddr_in6 * )((void * )node->ai_addr);
51
+ ptr = &in_addr->sin6_addr;
52
+ } else {
53
+ continue;
54
+ }
55
+ ares_inet_ntop(node->ai_family, ptr, addr_buf, sizeof(addr_buf));
56
+ printf("Addr: %s\n", addr_buf);
57
+ }
58
+ }
59
+ ares_freeaddrinfo(result);
60
+ }
61
+
62
+ int main(int argc, char ** argv)
63
+ {
64
+ ares_channel_t * channel = NULL;
65
+ struct ares_options options;
66
+ int optmask = 0;
67
+ struct ares_addrinfo_hints hints;
68
+
69
+ if (argc != 2) {
70
+ printf("Usage: %s domain\n", argv[ 0] );
71
+ return 1;
72
+ }
73
+
74
+ /* Initialize library * /
75
+ ares_library_init(ARES_LIB_INIT_ALL);
76
+
77
+ if (!ares_threadsafety()) {
78
+ printf("c-ares not compiled with thread support\n");
79
+ return 1;
80
+ }
81
+
82
+ memset(&options, 0, sizeof(options));
83
+ /* Enable event thread so we don't have to monitor file descriptors * /
84
+ optmask |= ARES_OPT_EVENT_THREAD;
85
+ options.evsys = ARES_EVSYS_DEFAULT;
86
+
87
+ /* Initialize channel to run queries, a single channel can accept unlimited
88
+ * queries * /
89
+ if (ares_init_options(&channel, &options, optmask) != ARES_SUCCESS) {
90
+ printf("c-ares initialization issue\n");
91
+ return 1;
92
+ }
93
+
94
+
95
+ /* Perform an IPv4 and IPv6 request for the provided domain name * /
96
+ memset(&hints, 0, sizeof(hints));
97
+ hints.ai_family = AF_UNSPEC;
98
+ hints.ai_flags = ARES_AI_CANONNAME;
99
+ ares_getaddrinfo(channel, argv[ 1] , NULL, &hints, addrinfo_cb,
100
+ NULL /* user context not specified * /);
101
+
102
+ /* Wait until no more requests are left to be processed * /
103
+ ares_queue_wait_empty(channel, -1);
104
+
105
+ /* Cleanup * /
106
+ ares_destroy(channel);
107
+
108
+ ares_library_cleanup();
109
+ return 0;
110
+ }
111
+ {% endhighlight %}
112
+
113
+ Compilation:
114
+ {% highlight bash %}
115
+ cc -I/usr/local/include -o example example.c -Wl,-rpath /usr/local/lib -lcares
116
+ {% endhighlight %}
117
+
118
+
119
+
0 commit comments