Put FIFO log into its own unit (out of the API code)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-06-17 19:12:22 +02:00
parent 3906490958
commit ae035c4ba7
7 changed files with 69 additions and 53 deletions
-1
View File
@@ -14,7 +14,6 @@ set(sources
dns.c
dns.h
ftl.c
ftl.h
routes.c
routes.h
settings.c
+10 -44
View File
@@ -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
-33
View File
@@ -1,33 +0,0 @@
/* 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
* API FTL 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
/* 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
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 {
int next_id;
time_t timestamp[LOG_SIZE];
char message[LOG_SIZE][MAX_MESSAGE];
} fifologData;
extern fifologData *fifo_log;
#endif // API_FTL_H