Skip to content

Commit 9ae8294

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 9ae8294

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

examples/host-lookup.c

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

examples/meson.build

Lines changed: 1 addition & 0 deletions
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)