Skip to content

Commit

Permalink
mdns: Split mdns_recv in 2 functions
Browse files Browse the repository at this point in the history
One that handles the actual receiving, and one that actually parses the
payload
  • Loading branch information
chouquette committed Mar 19, 2020
1 parent 219b180 commit bb95268
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
39 changes: 39 additions & 0 deletions include/mdns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*****************************************************************************
* This file is part of libmicrodns.
*
* Copyright © 2020 VideoLabs SAS
*
* Author: Hugo Beauzée-Luyssen <[email protected]>
*
*****************************************************************************
* libmicrodns is released under LGPLv2.1 (or later) and is also available
* under a commercial license.
*****************************************************************************
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/

#ifndef MDNS_H
#define MDNS_H

#include "microdns/microdns.h"

int
mdns_parse(struct mdns_hdr *hdr, struct rr_entry **entries, const uint8_t *buf,
size_t length);

void
mdns_free(struct rr_entry *entries);

#endif // MDNS_H
70 changes: 40 additions & 30 deletions src/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "compat.h"
#include "utils.h"
#include "microdns/microdns.h"
#include "mdns.h"

#define MDNS_PKT_MAXSZ 4096 // read/write buffer size

Expand Down Expand Up @@ -499,7 +500,7 @@ mdns_entries_send(const struct mdns_ctx *ctx, const struct mdns_hdr *hdr, const
return (0);
}

static void
void
mdns_free(struct rr_entry *entries)
{
struct rr_entry *entry;
Expand Down Expand Up @@ -527,45 +528,54 @@ mdns_read_header(const uint8_t *ptr, size_t *n, struct mdns_hdr *hdr)
return ptr;
}

int
mdns_parse(struct mdns_hdr *hdr, struct rr_entry **entries, const uint8_t *buf,
size_t length)
{
size_t num_entry;
struct rr_entry *entry;

*entries = NULL;

const uint8_t *ptr = mdns_read_header(buf, &length, hdr);
if (ptr == NULL)
return (MDNS_ERROR);

num_entry = hdr->num_qn + hdr->num_ans_rr + hdr->num_add_rr;
for (size_t i = 0; i < num_entry; ++i) {
entry = calloc(1, sizeof(struct rr_entry));
if (!entry)
goto err;
ptr = rr_read(ptr, &length, buf, entry, i >= hdr->num_qn);
if (!ptr) {
mdns_free(entry);
errno = ENOSPC;
goto err;
}
entry->next = *entries;
*entries = entry;
}
if (*entries == NULL) {
return (MDNS_ERROR);
}
return (0);
err:
mdns_free(*entries);
*entries = NULL;
return (MDNS_ERROR);
}

static int
mdns_recv(const struct mdns_conn* conn, struct mdns_hdr *hdr, struct rr_entry **entries)
{
uint8_t buf[MDNS_PKT_MAXSZ];
size_t num_entry, n;
ssize_t length;
struct rr_entry *entry;

*entries = NULL;
if ((length = recv(conn->sock, (char *) buf, sizeof(buf), 0)) < 0)
return (MDNS_NETERR);

n = (size_t)length;
const uint8_t *ptr = mdns_read_header(buf, &n, hdr);
if (ptr == NULL)
return (MDNS_ERROR);

num_entry = hdr->num_qn + hdr->num_ans_rr + hdr->num_add_rr;
for (size_t i = 0; i < num_entry; ++i) {
entry = calloc(1, sizeof(struct rr_entry));
if (!entry)
goto err;
ptr = rr_read(ptr, &n, buf, entry, i >= hdr->num_qn);
if (!ptr) {
mdns_free(entry);
errno = ENOSPC;
goto err;
}
entry->next = *entries;
*entries = entry;
}
if (*entries == NULL) {
return (MDNS_ERROR);
}
return (0);
err:
mdns_free(*entries);
*entries = NULL;
return (MDNS_ERROR);
return mdns_parse(hdr, entries, buf, (size_t)length);
}

void
Expand Down

0 comments on commit bb95268

Please sign in to comment.