Skip to content

Commit

Permalink
Add config file parser. First config arguments are
Browse files Browse the repository at this point in the history
SOCKET_LISTENING=all|local
INCLUDE_YESTERDAY=true|false

If FTL we cannot find or interpret a config variable value, it will skip it and use the default value
  • Loading branch information
DL6ER committed Mar 14, 2017
1 parent e70be89 commit 7c9b1b4
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 24 deletions.
7 changes: 7 additions & 0 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

// Static structs
typedef struct {
const char* conf;
const char* log;
const char* pid;
const char* port;
Expand Down Expand Up @@ -92,6 +93,11 @@ typedef struct {
int wildcarddomains;
} countersStruct;

typedef struct {
bool socket_listenlocal;
bool include_yesterday;
} ConfigStruct;

// Dynamic structs
typedef struct {
int timestamp;
Expand Down Expand Up @@ -145,6 +151,7 @@ enum { QUERIES, FORWARDED, CLIENTS, DOMAINS, OVERTIME };
logFileNamesStruct files;
FTLFileNamesStruct FTLfiles;
countersStruct counters;
ConfigStruct config;

queriesDataStruct *queries;
forwardedDataStruct *forwarded;
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Please see LICENSE file for your rights under this license.

DEPS = FTL.h routines.h version.h
OBJ = main.o structs.o log.o daemon.o parser.o signals.o socket.o request.o grep.o setupVars.o args.o flush.o threads.o
OBJ = main.o structs.o log.o daemon.o parser.o signals.o socket.o request.o grep.o setupVars.o args.o flush.o threads.o config.o

# Get git commit version and date
GIT_BRANCH := $(shell git branch | sed -n 's/^\* //p')
Expand Down
2 changes: 1 addition & 1 deletion args.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void parse_args(int argc, char* argv[])
{
int i;
for(i=0; i < argc; i++) {
if(strcmp(argv[i], "debug") == 0)
if((strcmp(argv[i], "d") == 0) || (strcmp(argv[i], "debug") == 0))
debug = true;

if(strcmp(argv[i], "debugthreads") == 0)
Expand Down
92 changes: 92 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Log parsing routine
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

#include "FTL.h"

ConfigStruct config;
char *parse_FTLconf(FILE *fp, const char * key);

char *conflinebuffer = NULL;

void read_FTLconf(void)
{

FILE *fp;
char * buffer;

if((fp = fopen(FTLfiles.conf, "r")) == NULL)
{
logg("Notice: Opening of pihole-FTL.conf failed!");
logg(" Falling back to default settings");
return;
}

// Parse lines in the config file

// SOCKET_LISTENING
// defaults to: listen only local
config.socket_listenlocal = true;
buffer = parse_FTLconf(fp, "SOCKET_LISTENING");
if(buffer != NULL)
{
logg_str("SOCKET_LISTENING: ", buffer);
if(strcmp(buffer, "all") == 0)
config.socket_listenlocal = false;
}

// INCLUDE_YESTERDAY
// defaults to: no
config.include_yesterday = false;
buffer = parse_FTLconf(fp, "INCLUDE_YESTERDAY");
if(buffer != NULL)
{
logg_str("INCLUDE_YESTERDAY: ", buffer);
if((strcmp(buffer, "true") == 0) || (strcmp(buffer, "yes") == 0))
config.include_yesterday = true;
}

free(conflinebuffer);
conflinebuffer = NULL;
fclose(fp);
}

char *parse_FTLconf(FILE *fp, const char * key)
{

char * keystr = calloc(strlen(key)+2,sizeof(char));
conflinebuffer = calloc(1024,sizeof(char));

sprintf(keystr, "%s=", key);

// Go to beginning of file
fseek(fp, 0L, SEEK_SET);

while(fgets(conflinebuffer, 1023, fp) != NULL)
{
// Strip newline from fgets output
conflinebuffer[strlen(conflinebuffer) - 1] = '\0';

// Skip comment lines
if(conflinebuffer[0] == '#' || conflinebuffer[0] == ';')
continue;

// Skip lines with other keys
if((strstr(conflinebuffer, keystr)) == NULL)
continue;

// otherwise: key found
free(keystr);
return (find_equals(conflinebuffer) + 1);
}

// Key not found -> return NULL
free(keystr);
return NULL;
}
11 changes: 5 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ int main (int argc, char* argv[]) {
logg_str("FTL hash: ",GIT_VERSION);
logg_str("FTL date: ",GIT_DATE);

read_FTLconf();

if(!debug)
go_daemon();
else
Expand All @@ -34,12 +36,9 @@ int main (int argc, char* argv[]) {

read_gravity_files();

logg("Starting initial log file scan");
initialscan = true;
process_pihole_log(1);
process_pihole_log(0);
initialscan = false;
logg("Finished initial log file scan:");
logg("Starting initial log file parsing");
initial_log_parsing();
logg("Finished initial log file parsing");
log_counter_info();
check_setupVarsconf();

Expand Down
31 changes: 22 additions & 9 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ void extracttimestamp(char *readbuffer, int *querytimestamp, int *overTimetimest
int dnsmasqlogpos = 0;
int lastqueryID = 0;

void initial_log_parsing(void)
{
initialscan = true;
if(config.include_yesterday)
process_pihole_log(1);
process_pihole_log(0);
initialscan = false;
}

int checkLogForChanges(void)
{
// seek to the end of the file
Expand Down Expand Up @@ -65,7 +74,8 @@ void *pihole_log_thread(void *val)
pihole_log_flushed(true);
// Rescan files 0 (pihole.log) and 1 (pihole.log.1)
initialscan = true;
process_pihole_log(1);
if(config.include_yesterday)
process_pihole_log(1);
process_pihole_log(0);
initialscan = false;
}
Expand Down Expand Up @@ -188,9 +198,11 @@ void process_pihole_log(int file)
{
memory_check(OVERTIME);
timeidx = counters.overTime;
overTime[counters.overTime].timestamp = overTimetimestamp;
overTime[counters.overTime].total = 0;
overTime[counters.overTime].blocked = 0;
overTime[timeidx].timestamp = overTimetimestamp;
overTime[timeidx].total = 0;
overTime[timeidx].blocked = 0;
overTime[timeidx].forwardnum = 0;
overTime[timeidx].forwarddata = NULL;
counters.overTime++;
}

Expand Down Expand Up @@ -448,9 +460,8 @@ void process_pihole_log(int file)

// Determine time index for this forward request
// Get timestamp
int querytimestamp, overTimetimestamp;
int querytimestamp, overTimetimestamp, timeidx;
extracttimestamp(readbuffer, &querytimestamp, &overTimetimestamp);
int timeidx;
bool found = false;
for(i=0; i < counters.overTime; i++)
{
Expand All @@ -465,9 +476,11 @@ void process_pihole_log(int file)
{
memory_check(OVERTIME);
timeidx = counters.overTime;
overTime[counters.overTime].timestamp = overTimetimestamp;
overTime[counters.overTime].total = 0;
overTime[counters.overTime].blocked = 0;
overTime[timeidx].timestamp = overTimetimestamp;
overTime[timeidx].total = 0;
overTime[timeidx].blocked = 0;
overTime[timeidx].forwardnum = 0;
overTime[timeidx].forwarddata = NULL;
counters.overTime++;
}
// Determine if there is enough space for saving the current
Expand Down
5 changes: 4 additions & 1 deletion routines.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void logg_str_str(const char* str, char* str2, char* str3);
void log_counter_info(void);
void format_memory_size(char *prefix, int bytes, double *formated);

void initial_log_parsing(void);
int checkLogForChanges(void);
void open_pihole_log(void);
void handle_signals(void);
Expand Down Expand Up @@ -64,7 +65,9 @@ void parse_args(int argc, char* argv[]);

int detectStatus(char *domain);

void *GC_thread(void *val);
char* find_equals(const char* s);

void enable_lock(const char *message);
void disable_lock(const char *message);

void read_FTLconf(void);
2 changes: 1 addition & 1 deletion setupVars.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ char * read_setupVarsconf(const char * key)

sprintf(keystr, "%s=", key);

while(fgets(linebuffer, 1024, setupVarsfp) != NULL)
while(fgets(linebuffer, 1023, setupVarsfp) != NULL)
{
// Strip newline from fgets output
linebuffer[strlen(linebuffer) - 1] = '\0';
Expand Down
11 changes: 6 additions & 5 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ void init_socket(void)
// The function bzero() sets all values in a buffer to zero.
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
#if defined(LISTENLOCALHOST)
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#else
serv_addr.sin_addr.s_addr = INADDR_ANY;
#endif

if(config.socket_listenlocal)
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
else
serv_addr.sin_addr.s_addr = INADDR_ANY;

// The bind() system call binds a socket to an address,
// in this case the address of the current host and
// port number on which the server will run.
Expand Down
1 change: 1 addition & 0 deletions structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "FTL.h"

FTLFileNamesStruct FTLfiles = {
"/etc/pihole/pihole-FTL.conf",
"/var/log/pihole-FTL.log",
"/var/run/pihole-FTL.pid",
"/var/run/pihole-FTL.port"
Expand Down

0 comments on commit 7c9b1b4

Please sign in to comment.