Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b1dbacf

Browse files
committedJul 1, 2023
Only call callback when valid entries were received
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.
1 parent b7a403d commit b1dbacf

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed
 

‎examples/host-lookup.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ static void callback(void *p_cookie, int status, const struct rr_entry *entries)
6767
}
6868
entry = (struct rr_entry *) entries;
6969
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);
70+
if (entry->valid)
71+
{
72+
if (entry->type == RR_A)
73+
printf("%s resolves to IPv4 address %s\n", hostname, entry->data.A.addr_str);
74+
else if (entry->type == RR_AAAA)
75+
printf("%s resolves to IPv6 address %s\n", hostname, entry->data.AAAA.addr_str);
76+
}
7477
entry = entry->next;
7578
}
7679
stopflag = true;

‎include/microdns/rr.h

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct rr_entry {
9595

9696
/* Answers only */
9797
uint32_t ttl;
98+
uint16_t valid : 1; // whether the answer is valid for the request
9899
uint16_t data_len;
99100
union rr_data data;
100101

‎src/mdns.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ hostname_match(const char *hostname, const char *match)
767767

768768
static int
769769
mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
770-
unsigned int nb_names, mdns_listen_callback callback,
771-
void *p_cookie)
770+
unsigned int nb_names, enum rr_type type,
771+
mdns_listen_callback callback, void *p_cookie)
772772
{
773773
struct mdns_hdr ahdr = {0};
774774
struct rr_entry *entries;
@@ -785,6 +785,8 @@ mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
785785
return r;
786786
}
787787
for (size_t i = 0; i < ctx->nb_conns; ++i) {
788+
bool has_valid_entries = false;
789+
788790
if ((pfd[i].revents & POLLIN) == 0)
789791
continue;
790792
r = mdns_recv(&ctx->conns[i], &ahdr, &entries);
@@ -801,17 +803,21 @@ mdns_listen_probe_network(const struct mdns_ctx *ctx, const char *const names[],
801803
}
802804

803805
for (struct rr_entry *entry = entries; entry; entry = entry->next) {
806+
if (entry->type != type)
807+
continue;
804808
for (unsigned int i = 0; i < nb_names; ++i) {
805809
if (!strrcmp(entry->name, names[i])) {
806-
callback(p_cookie, r, entries);
807-
break;
810+
entry->valid = true;
811+
has_valid_entries = true;
808812
} else if (is_host_address_rr_type(entry->type) &&
809813
hostname_match(names[i], entry->name)) {
810-
callback(p_cookie, r, entries);
811-
break;
814+
entry->valid = true;
815+
has_valid_entries = true;
812816
}
813817
}
814818
}
819+
if (has_valid_entries)
820+
callback(p_cookie, r, entries);
815821
mdns_free(entries);
816822
}
817823
return 0;
@@ -870,7 +876,7 @@ mdns_listen(const struct mdns_ctx *ctx, const char *const names[],
870876
}
871877
t1 = t2;
872878
}
873-
mdns_listen_probe_network(ctx, names, nb_names, callback, p_cookie);
879+
mdns_listen_probe_network(ctx, names, nb_names, type, callback, p_cookie);
874880
}
875881
free(qns);
876882
return (0);

0 commit comments

Comments
 (0)
Please sign in to comment.