From: Felix Fietkau Date: Mon, 9 Jun 2014 21:40:59 +0000 (+0200) Subject: fix excessive stack usage X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=cef25024bc6d3948d1e4a164d82a63fa21414912;p=oweals%2Fmdnsd.git fix excessive stack usage Signed-off-by: Felix Fietkau --- diff --git a/cache.c b/cache.c index 1cbe18f..89bd015 100644 --- a/cache.c +++ b/cache.c @@ -343,7 +343,7 @@ cache_dump_records(struct blob_buf *buf, const char *name) { struct cache_record *r, *q = avl_find_element(&records, name, r, avl); const char *txt; - char buffer[MAX_NAME_LEN]; + char buffer[INET6_ADDRSTRLEN]; if (!q) return; diff --git a/service.c b/service.c index 1cf50e3..30f4770 100644 --- a/service.c +++ b/service.c @@ -85,35 +85,28 @@ service_name(const char *domain) static void service_add_ptr(const char *host) { - unsigned char buffer[MAX_NAME_LEN]; - int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL); + int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL); if (len < 1) return; - dns_add_answer(TYPE_PTR, buffer, len); + dns_add_answer(TYPE_PTR, mdns_buf, len); } static void service_add_srv(struct service *s) { - unsigned char buffer[MAX_NAME_LEN]; - struct dns_srv_data *sd; + struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf; char *host = service_name("local"); - int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL); + int len = sizeof(*sd); - if (len < 1) - return; - - sd = calloc(1, len + sizeof(struct dns_srv_data)); - if (!sd) + len += dn_comp(host, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL); + if (len <= sizeof(*sd)) return; sd->port = cpu_to_be16(s->port); - memcpy(&sd[1], buffer, len); - host = service_name(s->service); - dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data)); - free(sd); + dns_add_answer(TYPE_SRV, mdns_buf, len); + service_name(s->service); } #define TOUT_LOOKUP 60 diff --git a/ubus.c b/ubus.c index efd6cb1..08b808d 100644 --- a/ubus.c +++ b/ubus.c @@ -20,6 +20,7 @@ #include #include +#include "util.h" #include "ubus.h" #include "cache.h" #include "service.h" @@ -51,10 +52,10 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct cache_entry *s, *q; - char buffer[MAX_NAME_LEN]; + char *buffer = (char *) mdns_buf; void *c1 = NULL, *c2; - blob_buf_init(&b, 0); + blob_buf_init(&b, 0); avl_for_each_element(&entries, s, avl) { char *local; if (*((char *) s->avl.key) != '_') @@ -95,10 +96,10 @@ mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct cache_entry *s; - char buffer[MAX_NAME_LEN]; + char *buffer = (char *) mdns_buf; void *c; - blob_buf_init(&b, 0); + blob_buf_init(&b, 0); avl_for_each_element(&entries, s, avl) { char *local; if (*((char *) s->avl.key) == '_') diff --git a/util.c b/util.c index 0c4a4ee..87db94c 100644 --- a/util.c +++ b/util.c @@ -33,6 +33,7 @@ #include "dns.h" #include "util.h" +uint8_t mdns_buf[MDNS_BUF_LEN]; int debug = 0; static void diff --git a/util.h b/util.h index 947f851..140e1da 100644 --- a/util.h +++ b/util.h @@ -14,12 +14,17 @@ #ifndef _UTIL_H__ #define _UTIL_H__ +#include + #define DBG(level, fmt, ...) do { \ if (debug >= level) \ fprintf(stderr, "mdnsd: %s (%d): " fmt, __func__, __LINE__, ## __VA_ARGS__); \ } while (0) +#define MDNS_BUF_LEN (8 * 1024) + extern int debug; +extern uint8_t mdns_buf[MDNS_BUF_LEN]; void *memdup(const void *d, int l);