Skip to content

Commit 370f270

Browse files
committed
examples: Add a hostname resolution example
Prints the IPv4, followed by the IPv6 address for the requested hostname, and exits. Closes: #41
1 parent ddf6e31 commit 370f270

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

examples/host-lookup.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright © 2014-2015 VideoLabs SAS
3+
* Copyright © 2021 Red Hat Inc
4+
*
5+
* Authors: Jonathan Calmels <[email protected]>
6+
* Bastien Nocera <[email protected]>
7+
*
8+
* Permission to use, copy, modify, and distribute this software for any
9+
* purpose with or without fee is hereby granted, provided that the above
10+
* copyright notice and this permission notice appear in all copies.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19+
*/
20+
21+
#include <compat.h>
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <signal.h>
25+
#include <time.h>
26+
27+
#include <microdns/microdns.h>
28+
29+
#include "compat.h"
30+
31+
#ifdef HAVE_UNISTD_H
32+
#include <unistd.h>
33+
#endif
34+
35+
#define TIMEOUT 1
36+
37+
static bool stopflag = false;
38+
static time_t start_time;
39+
40+
static double get_elapsed(void)
41+
{
42+
time_t t;
43+
44+
t = time(NULL);
45+
return difftime(t, start_time);
46+
}
47+
48+
static bool stop(void *p_cookie)
49+
{
50+
double elapsed;
51+
if (stopflag)
52+
return stopflag;
53+
elapsed = get_elapsed();
54+
return elapsed >= (double) TIMEOUT;
55+
}
56+
57+
static void callback(void *p_cookie, int status, const struct rr_entry *entries)
58+
{
59+
struct rr_entry *entry;
60+
char *hostname = p_cookie;
61+
char err[128];
62+
63+
if (status < 0) {
64+
mdns_strerror(status, err, sizeof(err));
65+
fprintf(stderr, "error: %s\n", err);
66+
return;
67+
}
68+
entry = (struct rr_entry *) entries;
69+
while (entry) {
70+
if (entry->type == RR_A)
71+
printf("%s resolves to IPv4 address %s\n", hostname, entry->data.A.addr_str);
72+
if (entry->type == RR_AAAA)
73+
printf("%s resolves to IPv6 address %s\n", hostname, entry->data.AAAA.addr_str);
74+
entry = entry->next;
75+
}
76+
stopflag = true;
77+
}
78+
79+
int main(int i_argc, char *ppsz_argv[])
80+
{
81+
int r = 0;
82+
char err[128];
83+
struct mdns_ctx *ctx;
84+
const char **ppsz_names;
85+
int i_nb_names;
86+
87+
if (i_argc <= 1)
88+
{
89+
fprintf(stderr, "Usage: %s [HOSTNAME]\n", ppsz_argv[0]);
90+
return (1);
91+
}
92+
93+
ppsz_names = (const char **) &ppsz_argv[1];
94+
i_nb_names = i_argc - 1;
95+
96+
if ((r = mdns_init(&ctx, NULL, MDNS_PORT)) < 0)
97+
goto err;
98+
start_time = time(NULL);
99+
if ((r = mdns_listen(ctx, ppsz_names, i_nb_names, RR_A, TIMEOUT, stop,
100+
callback, ppsz_argv[1])) < 0)
101+
goto err;
102+
stopflag = false;
103+
start_time = time(NULL);
104+
if ((r = mdns_listen(ctx, ppsz_names, i_nb_names, RR_AAAA, TIMEOUT, stop,
105+
callback, ppsz_argv[1])) < 0)
106+
goto err;
107+
err:
108+
if (r < 0) {
109+
mdns_strerror(r, err, sizeof(err));
110+
fprintf(stderr, "fatal: %s\n", err);
111+
}
112+
mdns_destroy(ctx);
113+
return (0);
114+
}

examples/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ examples_kwargs = {
1111

1212
executable('listen', 'main.c', kwargs: examples_kwargs)
1313
executable('announce', 'announce.c', kwargs: examples_kwargs)
14+
executable('host-lookup', 'host-lookup.c', kwargs: examples_kwargs)
1415

0 commit comments

Comments
 (0)