Skip to content

Commit

Permalink
Split mdns packets writing in a separated function
Browse files Browse the repository at this point in the history
So we can fuzz it too
  • Loading branch information
chouquette committed Mar 19, 2020
1 parent bb95268 commit 843bb5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
3 changes: 3 additions & 0 deletions include/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
int
mdns_parse(struct mdns_hdr *hdr, struct rr_entry **entries, const uint8_t *buf,
size_t length);
int
mdns_write(const struct mdns_hdr *hdr, const struct rr_entry *entries,
uint8_t *buf, size_t* length);

void
mdns_free(struct rr_entry *entries);
Expand Down
41 changes: 26 additions & 15 deletions src/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,28 +470,39 @@ mdns_write_hdr(uint8_t *ptr, const struct mdns_hdr *hdr)
return (p - ptr);
}

int
mdns_write(const struct mdns_hdr *hdr, const struct rr_entry *entries,
uint8_t *buf, size_t* length)
{
*length = 0;
if (!entries) return (MDNS_ERROR);
const struct rr_entry *entry = entries;
size_t l;

l = mdns_write_hdr(buf, hdr);
*length += l;

for (entry = entries; entry; entry = entry->next) {
l = rr_write(buf + *length, entry, (hdr->flags & FLAG_QR) > 0);
if (l < 0) {
return (MDNS_STDERR);
}
*length += l;
}
return (0);
}

int
mdns_entries_send(const struct mdns_ctx *ctx, const struct mdns_hdr *hdr, const struct rr_entry *entries)
{
uint8_t buf[MDNS_PKT_MAXSZ] = {0};
const struct rr_entry *entry = entries;
ssize_t n = 0, l, r;

if (!entries) return (MDNS_ERROR);

l = mdns_write_hdr(buf, hdr);
n += l;
size_t l;

for (entry = entries; entry; entry = entry->next) {
l = rr_write(buf+n, entry, (hdr->flags & FLAG_QR) > 0);
if (l < 0) {
return (MDNS_STDERR);
}
n += l;
}
if (mdns_write(hdr, entries, buf, &l) < 0)
return (MDNS_ERROR);

for (size_t i = 0; i < ctx->nb_conns; ++i) {
r = sendto(ctx->conns[i].sock, (const char *) buf, n, 0,
ssize_t r = sendto(ctx->conns[i].sock, (const char *) buf, l, 0,
(const struct sockaddr *) &ctx->addr, ss_len(&ctx->addr));
if (r < 0)
return (MDNS_NETERR);
Expand Down

0 comments on commit 843bb5f

Please sign in to comment.