Skip to content

Commit f9fd596

Browse files
committed
example
1 parent 55b260d commit f9fd596

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ gem "kramdown-parser-gfm"
4141
gem "http_parser.rb", "~> 0.6.0", :platforms => [:jruby]
4242

4343
gem "webrick"
44+
gem "rouge"

_config.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Welcome to Jekyll!
2-
#
32
# This config file is meant for settings that affect your whole blog, values
43
# which you are expected to set up once and rarely edit after that. If you find
54
# yourself editing this file very often, consider using Jekyll's data files
@@ -26,7 +25,11 @@ url: "https://c-ares.org"
2625
github_username: c-ares
2726

2827
# Build settings
29-
markdown: GFM
28+
markdown: kramdown
29+
highlighter: rouge
30+
kramdown:
31+
input: GFM
32+
syntax_highlighter: rouge
3033
theme: minima
3134
plugins:
3235
# - jekyll-feed

docs.md

+102
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,115 @@ layout: page
55
permalink: /docs/
66
---
77

8+
- [Manpages](#manpages)
9+
- [Example](#example)
10+
811
c-ares provides a set of library functions, datatypes, and enumerations
912
which integrators will use for their implementations. When you install c-ares,
1013
you get man pages which describe their use and meanings.
1114

15+
## Manpages
16+
1217
{% assign docs_paths = site.pages | where_exp: "page", "page.path contains 'docs/'" | map: "path" | sort -%}
1318
{%- for path in docs_paths -%}
1419
{%- assign my_page = site.pages | where: "path", path | first -%}
1520
- [{{ my_page.path | replace: "docs/", "" | replace: ".html", "" }}](/{{ my_page.path }})
1621
{% endfor -%}
1722

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

Comments
 (0)