Skip to content

Commit

Permalink
Only call callback when valid entries were received
Browse files Browse the repository at this point in the history
Our network listening code might receive packets that were for a
different request than the one we made. Make sure that we only call the
callback when valid entries were received, and make it possible for
clients to skip over invalid entries.
  • Loading branch information
hadess committed Jul 1, 2023
1 parent b7a403d commit b1dbacf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
11 changes: 7 additions & 4 deletions examples/host-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ static void callback(void *p_cookie, int status, const struct rr_entry *entries)
}
entry = (struct rr_entry *) entries;
while (entry) {
if (entry->type == RR_A)
printf("%s resolves to IPv4 address %s\n", hostname, entry->data.A.addr_str);
if (entry->type == RR_AAAA)
printf("%s resolves to IPv6 address %s\n", hostname, entry->data.AAAA.addr_str);
if (entry->valid)
{
if (entry->type == RR_A)
printf("%s resolves to IPv4 address %s\n", hostname, entry->data.A.addr_str);
else if (entry->type == RR_AAAA)
printf("%s resolves to IPv6 address %s\n", hostname, entry->data.AAAA.addr_str);
}
entry = entry->next;
}
stopflag = true;
Expand Down
1 change: 1 addition & 0 deletions include/microdns/rr.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct rr_entry {

/* Answers only */
uint32_t ttl;
uint16_t valid : 1; // whether the answer is valid for the request
uint16_t data_len;
union rr_data data;

Expand Down
20 changes: 13 additions & 7 deletions src/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ hostname_match(const char *hostname, const char *match)

static int
mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
unsigned int nb_names, mdns_listen_callback callback,
void *p_cookie)
unsigned int nb_names, enum rr_type type,
mdns_listen_callback callback, void *p_cookie)
{
struct mdns_hdr ahdr = {0};
struct rr_entry *entries;
Expand All @@ -785,6 +785,8 @@ mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
return r;
}
for (size_t i = 0; i < ctx->nb_conns; ++i) {
bool has_valid_entries = false;

if ((pfd[i].revents & POLLIN) == 0)
continue;
r = mdns_recv(&ctx->conns[i], &ahdr, &entries);
Expand All @@ -801,17 +803,21 @@ mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
}

for (struct rr_entry *entry = entries; entry; entry = entry->next) {
if (entry->type != type)
continue;
for (unsigned int i = 0; i < nb_names; ++i) {
if (!strrcmp(entry->name, names[i])) {
callback(p_cookie, r, entries);
break;
entry->valid = true;
has_valid_entries = true;
} else if (is_host_address_rr_type(entry->type) &&
hostname_match(names[i], entry->name)) {
callback(p_cookie, r, entries);
break;
entry->valid = true;
has_valid_entries = true;
}
}
}
if (has_valid_entries)
callback(p_cookie, r, entries);
mdns_free(entries);
}
return 0;
Expand Down Expand Up @@ -870,7 +876,7 @@ mdns_listen(const struct mdns_ctx *ctx, const char *const names[],
}
t1 = t2;
}
mdns_listen_probe_network(ctx, names, nb_names, callback, p_cookie);
mdns_listen_probe_network(ctx, names, nb_names, type, callback, p_cookie);
}
free(qns);
return (0);
Expand Down

0 comments on commit b1dbacf

Please sign in to comment.