Skip to content

Commit

Permalink
Update announce example
Browse files Browse the repository at this point in the history
- Listen for correct record type (RR_PTR)
- Respond with all answer entries required to correctly announce a service
- Add IPv6 support to example
  • Loading branch information
rolznz authored and tguillem committed Aug 21, 2019
1 parent 476664a commit a8c1fec
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions examples/announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,66 @@ static bool stop(void *cbarg)

static void callback(void *cbarg, int r, const struct mdns_ip *mdns_ip, const struct rr_entry *entry)
{
if (entry->type != RR_PTR)
{
printf("Unsupported request type: %d\n", entry->type);
return;
}
struct mdns_ctx *ctx = (struct mdns_ctx *) cbarg;
struct mdns_hdr hdr = {0};
struct rr_entry answer = {0};

struct rr_entry answers[4] = {0}; // A/AAAA, SRV, TXT, PTR
hdr.flags |= FLAG_QR;
hdr.flags |= FLAG_AA;
hdr.num_ans_rr = 1;
hdr.num_ans_rr = sizeof(answers) / sizeof(answers[0]);

for (int i = 0; i < hdr.num_ans_rr; i++)
{

answers[i].rr_class = RR_IN;
answers[i].ttl = 120;

if (i + 1 < hdr.num_ans_rr)
answers[i].next = &answers[i + 1];
}

answer.name = entry->name;
answer.type = entry->type;
answer.rr_class = entry->rr_class;
answer.ttl = 120;
char domain_name[] = "mdnshost.local";
char service_type[] = "_googlecast._tcp.local";
// link service type (_googlecast._tcp) to our domain (mdnshost.local)
char service_type_link[] = "mdnshost mDNShost._googlecast._tcp.local";

// RR_PTR: point service type (_vlc._tcp._local) to local domain
answers[0].type = RR_PTR;
answers[0].name = service_type;
answers[0].data.PTR.domain = service_type_link;

// RR_TXT: provide additional information (HTTP server root directory etc.)
answers[1].type = RR_TXT;
answers[1].name = service_type_link;

// RR_SRV: provide info about the service we're announcing (port no, etc.)
answers[2].type = RR_SRV;
answers[2].name = service_type_link;
answers[2].data.SRV.port = 9001;
answers[2].data.SRV.priority = 0;
answers[2].data.SRV.weight = 0;
answers[2].data.SRV.target = domain_name;

// RR_A/AAAA: link .local domain to IP address
answers[3].name = domain_name;
if (mdns_ip->family == AF_INET)
{
answers[3].type = RR_A;
memcpy(&answers[3].data.A.addr, &mdns_ip->ipv4,
sizeof(answers[3].data.A.addr));
}
else
{
answers[3].type = RR_AAAA;
memcpy(&answers[3].data.AAAA.addr, &mdns_ip->ipv6,
sizeof(answers[3].data.AAAA.addr));
}

memcpy(&answer.data.A.addr, &mdns_ip->ipv4, sizeof(answer.data.A.addr));
mdns_entries_send(ctx, &hdr, &answer);
mdns_entries_send(ctx, &hdr, answers);
}

int main(int argc, char *argv[])
Expand All @@ -69,8 +114,9 @@ int main(int argc, char *argv[])
if ((r = mdns_init(&ctx, MDNS_ADDR_IPV4, MDNS_PORT)) < 0)
goto err;

// test with `ping mdnshost.local`
mdns_announce(ctx, "mdnshost.local", RR_A, callback, ctx);
// test with `ping mdnshost.local` after discovery (run ./test first)
// NB: a zeroconf service (eg Avahi) must be running for ping to work
mdns_announce(ctx, "_googlecast._tcp.local", RR_PTR, callback, ctx);

if ((r = mdns_serve(ctx, stop, NULL)) < 0)
goto err;
Expand Down

0 comments on commit a8c1fec

Please sign in to comment.