mirror of
https://github.com/pi-hole/FTL.git
synced 2024-10-26 16:52:18 +02:00
Put FIFO log into its own unit (out of the API code)
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
@@ -114,6 +114,8 @@ set(sources
|
|||||||
datastructure.h
|
datastructure.h
|
||||||
dnsmasq_interface.c
|
dnsmasq_interface.c
|
||||||
dnsmasq_interface.h
|
dnsmasq_interface.h
|
||||||
|
fifo.c
|
||||||
|
fifo.h
|
||||||
files.c
|
files.c
|
||||||
files.h
|
files.h
|
||||||
FTL.h
|
FTL.h
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ set(sources
|
|||||||
dns.c
|
dns.c
|
||||||
dns.h
|
dns.h
|
||||||
ftl.c
|
ftl.c
|
||||||
ftl.h
|
|
||||||
routes.c
|
routes.c
|
||||||
routes.h
|
routes.h
|
||||||
settings.c
|
settings.c
|
||||||
|
|||||||
+10
-44
@@ -8,22 +8,21 @@
|
|||||||
* This file is copyright under the latest version of the EUPL.
|
* This file is copyright under the latest version of the EUPL.
|
||||||
* Please see LICENSE file for your rights under this license. */
|
* Please see LICENSE file for your rights under this license. */
|
||||||
|
|
||||||
#include "FTL.h"
|
#include "../FTL.h"
|
||||||
#include "../webserver/http-common.h"
|
#include "../webserver/http-common.h"
|
||||||
#include "../webserver/json_macros.h"
|
#include "../webserver/json_macros.h"
|
||||||
#include "routes.h"
|
#include "routes.h"
|
||||||
#include "ftl.h"
|
#include "../datastructure.h"
|
||||||
#include "datastructure.h"
|
|
||||||
// get_FTL_version()
|
// get_FTL_version()
|
||||||
#include "log.h"
|
#include "../log.h"
|
||||||
// git constants
|
// git constants
|
||||||
#include "version.h"
|
#include "../version.h"
|
||||||
// config struct
|
// config struct
|
||||||
#include "config.h"
|
#include "../config.h"
|
||||||
// {un,}lock_shm()
|
|
||||||
#include "../shmem.h"
|
|
||||||
// networkrecord
|
// networkrecord
|
||||||
#include "../database/network-table.h"
|
#include "../database/network-table.h"
|
||||||
|
// struct fifologData
|
||||||
|
#include "../fifo.h"
|
||||||
|
|
||||||
int api_ftl_client(struct mg_connection *conn)
|
int api_ftl_client(struct mg_connection *conn)
|
||||||
{
|
{
|
||||||
@@ -54,6 +53,7 @@ int api_ftl_client(struct mg_connection *conn)
|
|||||||
JSON_SEND_OBJECT(json);
|
JSON_SEND_OBJECT(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fifologData is allocated in shared memory for cross-fork compatibility
|
||||||
fifologData *fifo_log = NULL;
|
fifologData *fifo_log = NULL;
|
||||||
int api_ftl_dnsmasq_log(struct mg_connection *conn)
|
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_ITEM(json, "log", log);
|
||||||
JSON_OBJ_ADD_NUMBER(json, "nextID", fifo_log->next_id);
|
JSON_OBJ_ADD_NUMBER(json, "nextID", fifo_log->next_id);
|
||||||
|
|
||||||
|
// Send data
|
||||||
JSON_SEND_OBJECT(json);
|
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)
|
int api_ftl_network(struct mg_connection *conn)
|
||||||
{
|
{
|
||||||
// Verify requesting client is allowed to see this ressource
|
// Verify requesting client is allowed to see this ressource
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
// http_init()
|
// http_init()
|
||||||
#include "webserver/webserver.h"
|
#include "webserver/webserver.h"
|
||||||
// add_to_dnsmasq_log_buffer()
|
// add_to_dnsmasq_log_buffer()
|
||||||
#include "api/ftl.h"
|
#include "fifo.h"
|
||||||
|
|
||||||
static void print_flags(const unsigned int flags);
|
static void print_flags(const unsigned int flags);
|
||||||
static void save_reply_type(const unsigned int flags, const union all_addr *addr,
|
static void save_reply_type(const unsigned int flags, const union all_addr *addr,
|
||||||
|
|||||||
+49
@@ -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();
|
||||||
|
}
|
||||||
@@ -3,23 +3,23 @@
|
|||||||
* Network-wide ad blocking via your own hardware.
|
* Network-wide ad blocking via your own hardware.
|
||||||
*
|
*
|
||||||
* FTL Engine
|
* FTL Engine
|
||||||
* API FTL prototypes
|
* dnsmasq FIFO log prototypes
|
||||||
*
|
*
|
||||||
* This file is copyright under the latest version of the EUPL.
|
* This file is copyright under the latest version of the EUPL.
|
||||||
* Please see LICENSE file for your rights under this license. */
|
* Please see LICENSE file for your rights under this license. */
|
||||||
#ifndef API_FTL_H
|
#ifndef API_FTL_H
|
||||||
#define API_FTL_H
|
#define API_FTL_H
|
||||||
|
|
||||||
|
#include "FTL.h"
|
||||||
|
|
||||||
/* From RFC 3164 */
|
/* From RFC 3164 */
|
||||||
#define MAX_MESSAGE 1024
|
#define MAX_MESSAGE 1024
|
||||||
|
|
||||||
// How many messages do we keep in memory (FIFO message buffer)?
|
// How many messages do we keep in memory (FIFO message buffer)?
|
||||||
// The memory required is the set number in kilobytes
|
// This number multiplied by MAX_MESSAGE (see above) gives the total buffer size
|
||||||
// Defaults to 64 [uses 64 KB of memory]
|
// Defaults to 128 [use 128 KB of memory for the log]
|
||||||
#define LOG_SIZE 64
|
#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);
|
void add_to_dnsmasq_log_fifo_buffer(const char *payload, const int length);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
+1
-1
@@ -17,7 +17,7 @@
|
|||||||
// data getter functions
|
// data getter functions
|
||||||
#include "datastructure.h"
|
#include "datastructure.h"
|
||||||
// fifologData
|
// fifologData
|
||||||
#include "api/ftl.h"
|
#include "fifo.h"
|
||||||
|
|
||||||
/// The version of shared memory used
|
/// The version of shared memory used
|
||||||
#define SHARED_MEMORY_VERSION 9
|
#define SHARED_MEMORY_VERSION 9
|
||||||
|
|||||||
Reference in New Issue
Block a user