Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Jul 4, 2024
1 parent 55b260d commit f9fd596
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ gem "kramdown-parser-gfm"
gem "http_parser.rb", "~> 0.6.0", :platforms => [:jruby]

gem "webrick"
gem "rouge"
7 changes: 5 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Welcome to Jekyll!
#
# This config file is meant for settings that affect your whole blog, values
# which you are expected to set up once and rarely edit after that. If you find
# yourself editing this file very often, consider using Jekyll's data files
Expand All @@ -26,7 +25,11 @@ url: "https://c-ares.org"
github_username: c-ares

# Build settings
markdown: GFM
markdown: kramdown
highlighter: rouge
kramdown:
input: GFM
syntax_highlighter: rouge
theme: minima
plugins:
# - jekyll-feed
Expand Down
102 changes: 102 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,115 @@ layout: page
permalink: /docs/
---

- [Manpages](#manpages)
- [Example](#example)

c-ares provides a set of library functions, datatypes, and enumerations
which integrators will use for their implementations. When you install c-ares,
you get man pages which describe their use and meanings.

## Manpages

{% assign docs_paths = site.pages | where_exp: "page", "page.path contains 'docs/'" | map: "path" | sort -%}
{%- for path in docs_paths -%}
{%- assign my_page = site.pages | where: "path", path | first -%}
- [{{ my_page.path | replace: "docs/", "" | replace: ".html", "" }}](/{{ my_page.path }})
{% endfor -%}

## Example
A simple name resolve might look like:

`example.c`:
{% highlight C %}
#include <stdio.h>
#include <string.h>
#include <ares.h>

/* Callback that is called when DNS query is finished */
static void addrinfo_cb(void *arg, int status, int timeouts,
struct ares_addrinfo *result)
{
(void)arg; /* Example does not use user context */
printf("Result: %s, timeouts: %d\n", ares_strerror(status), timeouts);

if (result) {
struct ares_addrinfo_node *node;
for (node = result->nodes; node != NULL; node = node->ai_next) {
char addr_buf[64] = "";
const void *ptr = NULL;
if (node->ai_family == AF_INET) {
const struct sockaddr_in *in_addr =
(const struct sockaddr_in *)((void *)node->ai_addr);
ptr = &in_addr->sin_addr;
} else if (node->ai_family == AF_INET6) {
const struct sockaddr_in6 *in_addr =
(const struct sockaddr_in6 *)((void *)node->ai_addr);
ptr = &in_addr->sin6_addr;
} else {
continue;
}
ares_inet_ntop(node->ai_family, ptr, addr_buf, sizeof(addr_buf));
printf("Addr: %s\n", addr_buf);
}
}
ares_freeaddrinfo(result);
}

int main(int argc, char **argv)
{
ares_channel_t *channel = NULL;
struct ares_options options;
int optmask = 0;
struct ares_addrinfo_hints hints;

if (argc != 2) {
printf("Usage: %s domain\n", argv[0]);
return 1;
}

/* Initialize library */
ares_library_init(ARES_LIB_INIT_ALL);

if (!ares_threadsafety()) {
printf("c-ares not compiled with thread support\n");
return 1;
}

memset(&options, 0, sizeof(options));
/* Enable event thread so we don't have to monitor file descriptors */
optmask |= ARES_OPT_EVENT_THREAD;
options.evsys = ARES_EVSYS_DEFAULT;

/* Initialize channel to run queries, a single channel can accept unlimited
* queries */
if (ares_init_options(&channel, &options, optmask) != ARES_SUCCESS) {
printf("c-ares initialization issue\n");
return 1;
}


/* Perform an IPv4 and IPv6 request for the provided domain name */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = ARES_AI_CANONNAME;
ares_getaddrinfo(channel, argv[1], NULL, &hints, addrinfo_cb,
NULL /* user context not specified */);

/* Wait until no more requests are left to be processed */
ares_queue_wait_empty(channel, -1);

/* Cleanup */
ares_destroy(channel);

ares_library_cleanup();
return 0;
}
{% endhighlight %}

Compilation:
{% highlight bash %}
cc -I/usr/local/include -o example example.c -Wl,-rpath /usr/local/lib -lcares
{% endhighlight %}



0 comments on commit f9fd596

Please sign in to comment.