From bb95268fc332fc917f21239c6035ddc4d43cecf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Beauz=C3=A9e-Luyssen?= Date: Wed, 5 Feb 2020 14:20:50 +0100 Subject: [PATCH] mdns: Split mdns_recv in 2 functions One that handles the actual receiving, and one that actually parses the payload --- include/mdns.h | 39 ++++++++++++++++++++++++++++ src/mdns.c | 70 ++++++++++++++++++++++++++++---------------------- 2 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 include/mdns.h diff --git a/include/mdns.h b/include/mdns.h new file mode 100644 index 0000000..d5d248e --- /dev/null +++ b/include/mdns.h @@ -0,0 +1,39 @@ +/***************************************************************************** + * This file is part of libmicrodns. + * + * Copyright © 2020 VideoLabs SAS + * + * Author: Hugo Beauzée-Luyssen + * + ***************************************************************************** + * 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 diff --git a/src/mdns.c b/src/mdns.c index bc23e37..876b14d 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -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 @@ -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; @@ -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