From ae035c4ba767895977813f41cdcbe4d05e19bcc7 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 17 Jun 2020 19:12:22 +0200 Subject: [PATCH] Put FIFO log into its own unit (out of the API code) Signed-off-by: DL6ER --- src/CMakeLists.txt | 2 ++ src/api/CMakeLists.txt | 1 - src/api/ftl.c | 54 ++++++++------------------------------- src/dnsmasq_interface.c | 2 +- src/fifo.c | 49 +++++++++++++++++++++++++++++++++++ src/{api/ftl.h => fifo.h} | 12 ++++----- src/shmem.c | 2 +- 7 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 src/fifo.c rename src/{api/ftl.h => fifo.h} (77%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa15dd67..cd11427b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -114,6 +114,8 @@ set(sources datastructure.h dnsmasq_interface.c dnsmasq_interface.h + fifo.c + fifo.h files.c files.h FTL.h diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt index 745915e2..9c8f1f29 100644 --- a/src/api/CMakeLists.txt +++ b/src/api/CMakeLists.txt @@ -14,7 +14,6 @@ set(sources dns.c dns.h ftl.c - ftl.h routes.c routes.h settings.c diff --git a/src/api/ftl.c b/src/api/ftl.c index 46072aea..234cacfd 100644 --- a/src/api/ftl.c +++ b/src/api/ftl.c @@ -8,22 +8,21 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -#include "FTL.h" +#include "../FTL.h" #include "../webserver/http-common.h" #include "../webserver/json_macros.h" #include "routes.h" -#include "ftl.h" -#include "datastructure.h" +#include "../datastructure.h" // get_FTL_version() -#include "log.h" +#include "../log.h" // git constants -#include "version.h" +#include "../version.h" // config struct -#include "config.h" -// {un,}lock_shm() -#include "../shmem.h" +#include "../config.h" // networkrecord #include "../database/network-table.h" +// struct fifologData +#include "../fifo.h" int api_ftl_client(struct mg_connection *conn) { @@ -54,6 +53,7 @@ int api_ftl_client(struct mg_connection *conn) JSON_SEND_OBJECT(json); } +// fifologData is allocated in shared memory for cross-fork compatibility fifologData *fifo_log = NULL; int api_ftl_dnsmasq_log(struct mg_connection *conn) { @@ -115,45 +115,11 @@ int api_ftl_dnsmasq_log(struct mg_connection *conn) } JSON_OBJ_ADD_ITEM(json, "log", log); JSON_OBJ_ADD_NUMBER(json, "nextID", fifo_log->next_id); + + // Send data JSON_SEND_OBJECT(json); } -void add_to_dnsmasq_log_fifo_buffer(const char *payload, const int length) -{ - // Lock SHM - lock_shm(); - - unsigned int idx = fifo_log->next_id++; - if(idx >= LOG_SIZE) - { - // Log is full, move everything one slot forward to make space for a new record at the end - // This pruges the oldest message from the list (it is overwritten by the second message) - memmove(fifo_log->message[0], fifo_log->message[1], (LOG_SIZE - 1u) * MAX_MESSAGE); - memmove(&fifo_log->timestamp[0], &fifo_log->timestamp[1], (LOG_SIZE - 1u) * sizeof(time_t)); - idx = LOG_SIZE - 1u; - } - - // Copy relevant string into temporary buffer - size_t copybytes = length < MAX_MESSAGE ? length : MAX_MESSAGE; - memcpy(fifo_log->message[idx], payload, copybytes); - - // Zero-terminate buffer, truncate newline if found - if(fifo_log->message[idx][copybytes - 1u] == '\n') - { - fifo_log->message[idx][copybytes - 1u] = '\0'; - } - else - { - fifo_log->message[idx][copybytes] = '\0'; - } - - // Set timestamp - fifo_log->timestamp[idx] = time(NULL); - - // Unlock SHM - unlock_shm(); -} - int api_ftl_network(struct mg_connection *conn) { // Verify requesting client is allowed to see this ressource diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 261272ed..7c5ef53e 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -35,7 +35,7 @@ // http_init() #include "webserver/webserver.h" // add_to_dnsmasq_log_buffer() -#include "api/ftl.h" +#include "fifo.h" static void print_flags(const unsigned int flags); static void save_reply_type(const unsigned int flags, const union all_addr *addr, diff --git a/src/fifo.c b/src/fifo.c new file mode 100644 index 00000000..b5c034aa --- /dev/null +++ b/src/fifo.c @@ -0,0 +1,49 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2019 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* dnsmasq FIFO log for Pi-hole's API +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "fifo.h" +// {un,}lock_shm() +#include "shmem.h" + +void add_to_dnsmasq_log_fifo_buffer(const char *payload, const int length) +{ + // Lock SHM + lock_shm(); + + unsigned int idx = fifo_log->next_id++; + if(idx >= LOG_SIZE) + { + // Log is full, move everything one slot forward to make space for a new record at the end + // This pruges the oldest message from the list (it is overwritten by the second message) + memmove(fifo_log->message[0], fifo_log->message[1], (LOG_SIZE - 1u) * MAX_MESSAGE); + memmove(&fifo_log->timestamp[0], &fifo_log->timestamp[1], (LOG_SIZE - 1u) * sizeof(time_t)); + idx = LOG_SIZE - 1u; + } + + // Copy relevant string into temporary buffer + size_t copybytes = length < MAX_MESSAGE ? length : MAX_MESSAGE; + memcpy(fifo_log->message[idx], payload, copybytes); + + // Zero-terminate buffer, truncate newline if found + if(fifo_log->message[idx][copybytes - 1u] == '\n') + { + fifo_log->message[idx][copybytes - 1u] = '\0'; + } + else + { + fifo_log->message[idx][copybytes] = '\0'; + } + + // Set timestamp + fifo_log->timestamp[idx] = time(NULL); + + // Unlock SHM + unlock_shm(); +} \ No newline at end of file diff --git a/src/api/ftl.h b/src/fifo.h similarity index 77% rename from src/api/ftl.h rename to src/fifo.h index c359be78..18ad3370 100644 --- a/src/api/ftl.h +++ b/src/fifo.h @@ -3,23 +3,23 @@ * Network-wide ad blocking via your own hardware. * * FTL Engine -* API FTL prototypes +* dnsmasq FIFO log prototypes * * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ #ifndef API_FTL_H #define API_FTL_H +#include "FTL.h" + /* From RFC 3164 */ #define MAX_MESSAGE 1024 // How many messages do we keep in memory (FIFO message buffer)? -// The memory required is the set number in kilobytes -// Defaults to 64 [uses 64 KB of memory] -#define LOG_SIZE 64 +// This number multiplied by MAX_MESSAGE (see above) gives the total buffer size +// Defaults to 128 [use 128 KB of memory for the log] +#define LOG_SIZE 128 -void init_dnsmasq_fifo_log(void); -void free_dnsmasq_fifo_log(void); void add_to_dnsmasq_log_fifo_buffer(const char *payload, const int length); typedef struct { diff --git a/src/shmem.c b/src/shmem.c index fd7e2e60..4321eceb 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -17,7 +17,7 @@ // data getter functions #include "datastructure.h" // fifologData -#include "api/ftl.h" +#include "fifo.h" /// The version of shared memory used #define SHARED_MEMORY_VERSION 9