diff --git a/includes/kore.h b/includes/kore.h index e1b8cfe..27acd64 100644 --- a/includes/kore.h +++ b/includes/kore.h @@ -535,7 +535,9 @@ int kore_msg_register(u_int8_t, void (*cb)(struct kore_msg *, const void *)); void kore_domain_init(void); +void kore_domain_cleanup(void); int kore_domain_new(char *); +void kore_domain_free(struct kore_domain *); void kore_module_init(void); void kore_module_reload(int); void kore_module_onload(void); @@ -547,6 +549,7 @@ void kore_module_load(const char *, const char *); void kore_domain_sslstart(struct kore_domain *); int kore_module_handler_new(const char *, const char *, const char *, const char *, int); +void kore_module_handler_free(struct kore_module_handle *); struct kore_domain *kore_domain_lookup(const char *); struct kore_module_handle *kore_module_handler_find(const char *, diff --git a/src/domain.c b/src/domain.c index 1dfbb92..946f6a4 100644 --- a/src/domain.c +++ b/src/domain.c @@ -42,6 +42,17 @@ kore_domain_init(void) TAILQ_INIT(&domains); } +void +kore_domain_cleanup(void) +{ + struct kore_domain *dom; + + while ((dom=TAILQ_FIRST(&domains)) != NULL) { + TAILQ_REMOVE(&domains, dom, list); + kore_domain_free(dom); + } +} + int kore_domain_new(char *domain) { @@ -71,6 +82,49 @@ kore_domain_new(char *domain) return (KORE_RESULT_OK); } +void +kore_domain_free(struct kore_domain *dom) +{ + struct kore_module_handle *hdlr; + + if (dom != NULL) { + + if (primary_dom == dom) { + primary_dom = NULL; + } + TAILQ_REMOVE(&domains, dom, list); + + if (dom->domain != NULL) { + kore_mem_free(dom->domain); + } + +#if !defined(KORE_NO_TLS) + if (dom->ssl_ctx != NULL) { + SSL_CTX_free(dom->ssl_ctx); + } + if (dom->cafile != NULL) { + kore_mem_free(dom->cafile); + } + if (dom->certkey != NULL) { + kore_mem_free(dom->certkey); + } + if (dom->certfile != NULL) { + kore_mem_free(dom->certfile); + } + if (dom->crlfile != NULL) { + kore_mem_free(dom->crlfile); + } +#endif + + /* Drop all handlers associated with this domain */ + while ((hdlr=TAILQ_FIRST(&(dom->handlers))) != NULL) { + TAILQ_REMOVE(&(dom->handlers), hdlr, list); + kore_module_handler_free(hdlr); + } + kore_mem_free(dom); + } +} + void kore_domain_sslstart(struct kore_domain *dom) { @@ -179,7 +233,9 @@ kore_domain_sslstart(struct kore_domain *dom) SSL_CTX_set_tlsext_servername_callback(dom->ssl_ctx, kore_tls_sni_cb); kore_mem_free(dom->certfile); + dom->certfile = NULL; kore_mem_free(dom->certkey); + dom->certkey = NULL; #endif } diff --git a/src/kore.c b/src/kore.c index 27ad690..f8e4fd3 100644 --- a/src/kore.c +++ b/src/kore.c @@ -421,6 +421,7 @@ kore_server_start(void) kore_platform_event_cleanup(); kore_connection_cleanup(); + kore_domain_cleanup(); net_cleanup(); } diff --git a/src/module.c b/src/module.c index af93aed..e72cd82 100644 --- a/src/module.c +++ b/src/module.c @@ -185,9 +185,7 @@ kore_module_handler_new(const char *path, const char *domain, if (hdlr->type == HANDLER_TYPE_DYNAMIC) { if (regcomp(&(hdlr->rctx), hdlr->path, REG_EXTENDED | REG_NOSUB)) { - kore_mem_free(hdlr->func); - kore_mem_free(hdlr->path); - kore_mem_free(hdlr); + kore_module_handler_free(hdlr); kore_debug("regcomp() on %s failed", path); return (KORE_RESULT_ERROR); } @@ -197,6 +195,35 @@ kore_module_handler_new(const char *path, const char *domain, return (KORE_RESULT_OK); } +void +kore_module_handler_free(struct kore_module_handle *hdlr) +{ + struct kore_handler_params *param; + + if (hdlr != NULL) { + if (hdlr->func != NULL) { + kore_mem_free(hdlr->func); + } + if (hdlr->path != NULL) { + kore_mem_free(hdlr->path); + } + if (hdlr->dom != NULL) { + TAILQ_REMOVE(&(hdlr->dom->handlers), hdlr, list); + } + regfree(&(hdlr->rctx)); + + /* Drop all validators associated with this handler */ + while ((param=TAILQ_FIRST(&(hdlr->params))) != NULL) { + TAILQ_REMOVE(&(hdlr->params), param, list); + if (param->name != NULL) { + kore_mem_free(param->name); + } + kore_mem_free(param); + } + kore_mem_free(hdlr); + } +} + struct kore_module_handle * kore_module_handler_find(const char *domain, const char *path) { diff --git a/src/worker.c b/src/worker.c index 3d5092a..a0d1ce0 100644 --- a/src/worker.c +++ b/src/worker.c @@ -370,6 +370,7 @@ kore_worker_entry(struct kore_worker *kw) kore_platform_event_cleanup(); kore_connection_cleanup(); + kore_domain_cleanup(); #if !defined(KORE_NO_HTTP) http_cleanup(); #endif