Skip to content

Commit

Permalink
Add dual stack support
Browse files Browse the repository at this point in the history
We now can pass NULL instead of an IPV4/IPV6 address to use both
  • Loading branch information
chouquette committed Aug 26, 2020
1 parent c30f2b0 commit 1b4b07e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/announce.c
Expand Up @@ -127,7 +127,7 @@ int main(int argc, char *argv[])
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);

if ((r = mdns_init(&ctx, MDNS_ADDR_IPV4, MDNS_PORT)) < 0)
if ((r = mdns_init(&ctx, NULL, MDNS_PORT)) < 0)
goto err;

// test with `ping mdnshost.local` after discovery (run ./test first)
Expand Down
2 changes: 1 addition & 1 deletion examples/main.c
Expand Up @@ -77,7 +77,7 @@ int main(int i_argc, char *ppsz_argv[])
}
signal(SIGINT, &sighandler);

if ((r = mdns_init(&ctx, MDNS_ADDR_IPV4, MDNS_PORT)) < 0)
if ((r = mdns_init(&ctx, NULL, MDNS_PORT)) < 0)
goto err;
if ((r = mdns_listen(ctx, ppsz_names, i_nb_names, RR_PTR, 10, stop,
callback, NULL)) < 0)
Expand Down
2 changes: 1 addition & 1 deletion include/microdns/microdns.h
Expand Up @@ -90,7 +90,7 @@ typedef bool (*mdns_stop_func)(void*);
* @brief Allocates and initialize a new mdns context
*
* @param ctx Returns the allocated context for the library [OUT]
* @param addr Address to listen to
* @param addr Protocol specific address to listen to, or NULL to use both IPv4 and IPv6
* @param port Port to listen on
*
* @see use mdns_destroy() to clean
Expand Down
34 changes: 31 additions & 3 deletions src/mdns.c
Expand Up @@ -359,12 +359,40 @@ mdns_resolve(struct mdns_ctx *ctx, const char *addr, unsigned short port)

sprintf(buf, "%hu", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
errno = getaddrinfo(addr, buf, &hints, &res);
if (errno != 0)

if (addr == NULL) {
hints.ai_family = AF_INET;
/* First, get the ipv4 multicast address info */
errno = getaddrinfo(MDNS_ADDR_IPV4, buf, &hints, &res);
if (errno != 0)
return (MDNS_LKPERR);
/* Now get the ipv6 informations and link them with the ipv4 ones */
struct addrinfo *ipv6_res = NULL;
hints.ai_family = AF_INET6;
errno = getaddrinfo(MDNS_ADDR_IPV6, buf, &hints, &ipv6_res);
if (errno != 0) {
freeaddrinfo(res);
return (MDNS_LKPERR);
}
struct addrinfo* ipv4 = res;
for (; ipv4->ai_next != NULL; ipv4 = ipv4->ai_next)
;
ipv4->ai_next = ipv6_res;
} else {
int family;
if (!strcmp(addr, MDNS_ADDR_IPV4))
family = AF_INET;
else if (!strcmp(addr, MDNS_ADDR_IPV6))
family = AF_INET6;
else
return (MDNS_LKPERR);
hints.ai_family = family;
errno = getaddrinfo(addr, buf, &hints, &res);
if (errno != 0)
return (MDNS_LKPERR);
}

status = mdns_list_interfaces(&ifaddrs, &mdns_ips, &ctx->nb_conns, &mcast_addrs, res);
if ( status < 0) {
Expand Down

0 comments on commit 1b4b07e

Please sign in to comment.