Interpret command line arguments (use `pihole-FTL debug` for debug mode)

This commit is contained in:
DL6ER 2017-02-06 13:19:17 +01:00
parent 75a3085acb
commit b01d6b6194
No known key found for this signature in database
GPG Key ID: BB8EC0BC77973A30
8 changed files with 95 additions and 80 deletions

1
FTL.h
View File

@ -141,3 +141,4 @@ char ** setupVarsArray;
int setupVarsElements;
bool initialscan;
bool debug;

View File

@ -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
OBJ = main.o structs.o log.o daemon.o parser.o signals.o socket.o request.o grep.o setupVars.o args.o
# Get git commit version and date
GIT_BRANCH := $(shell git branch | sed -n 's/^\* //p')

22
args.c Normal file
View File

@ -0,0 +1,22 @@
/* 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
* Argument parsing routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
bool debug = false;
void parse_args(int argc, char* argv[])
{
int i;
for(i=0; i < argc; i++) {
if(strcmp(argv[i], "debug") == 0)
debug = true;
// Other arguments are ignored
}
}

25
log.c
View File

@ -30,9 +30,8 @@ void logg(const char* str)
int millisec = tv.tv_usec/1000;
fprintf(logfile, "[%d-%02d-%02d %02d:%02d:%02d.%03i] %s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str);
fflush(logfile);
#if defined(DEBUG)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str);
#endif
if(debug)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str);
}
void logg_int(const char* str, int i)
@ -44,9 +43,8 @@ void logg_int(const char* str, int i)
int millisec = tv.tv_usec/1000;
fprintf(logfile, "[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, i);
fflush(logfile);
#if defined(DEBUG)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, i);
#endif
if(debug)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, i);
}
void logg_str(const char* str, char* str2)
@ -58,9 +56,8 @@ void logg_str(const char* str, char* str2)
int millisec = tv.tv_usec/1000;
fprintf(logfile, "[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2);
fflush(logfile);
#if defined(DEBUG)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2);
#endif
if(debug)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2);
}
void logg_str_str(const char* str, char* str2, char* str3)
@ -72,9 +69,8 @@ void logg_str_str(const char* str, char* str2, char* str3)
int millisec = tv.tv_usec/1000;
fprintf(logfile, "[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s (%s)\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2, str3);
fflush(logfile);
#if defined(DEBUG)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s (%s)\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2, str3);
#endif
if(debug)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] %s%s (%s)\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, str2, str3);
}
void logg_struct_resize(const char* str, int from, int to)
@ -86,9 +82,8 @@ void logg_struct_resize(const char* str, int from, int to)
int millisec = tv.tv_usec/1000;
fprintf(logfile, "[%d-%02d-%02d %02d:%02d:%02d.%03i] Notice: Increasing %s struct size from %i to %i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, from, to);
fflush(logfile);
#if defined(DEBUG)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] Notice: Increasing %s struct size from %i to %i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, from, to);
#endif
if(debug)
printf("[%d-%02d-%02d %02d:%02d:%02d.%03i] Notice: Increasing %s struct size from %i to %i\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, millisec, str, from, to);
}
void log_counter_info(void)

21
main.c
View File

@ -11,7 +11,10 @@
#include "FTL.h"
#include "version.h"
int main () {
int main (int argc, char* argv[]) {
if(argc > 1)
parse_args(argc, argv);
open_FTL_log();
open_pihole_log();
@ -19,11 +22,12 @@ int main () {
logg_str("FTL branch: ",GIT_BRANCH);
logg_str("FTL hash: ",GIT_VERSION);
logg_str("FTL date: ",GIT_DATE);
#if !defined(DEBUG)
go_daemon();
#else
savepid(getpid());
#endif
if(!debug)
go_daemon();
else
savepid(getpid());
handle_signals();
init_socket();
@ -62,9 +66,8 @@ int main () {
else if(clientconnected)
{
clientconnected = false;
#if defined(DEBUG)
logg("Client disconnected");
#endif
if(debug)
logg("Client disconnected");
}
else
{

View File

@ -31,9 +31,8 @@ void process_request(void)
}
sprintf(socketsendbuffer,"domains_being_blocked %i\ndns_queries_today %i\nads_blocked_today %i\nads_percentage_today %f\n",counters.gravity,counters.queries,counters.blocked,percentage);
swrite();
#if defined(DEBUG)
logg("Sent stats data to client");
#endif
if(debug)
logg("Sent stats data to client");
}
else if(command(">overTime"))
{
@ -51,9 +50,8 @@ void process_request(void)
swrite();
}
}
#if defined(DEBUG)
logg("Sent overTime data to client");
#endif
if(debug)
logg("Sent overTime data to client");
}
else if(command(">top-domains") || command(">top-ads"))
{
@ -136,9 +134,8 @@ void process_request(void)
}
if(excludedomains != NULL)
clearSetupVarsArray();
#if defined(DEBUG)
logg("Sent top lists data to client");
#endif
if(debug)
logg("Sent top lists data to client");
}
else if(command(">top-clients"))
{
@ -186,9 +183,8 @@ void process_request(void)
}
if(excludeclients != NULL)
clearSetupVarsArray();
#if defined(DEBUG)
logg("Sent top clients data to client");
#endif
if(debug)
logg("Sent top clients data to client");
}
else if(command(">forward-dest"))
{
@ -209,17 +205,15 @@ void process_request(void)
sprintf(socketsendbuffer,"%i %i %s %s\n",i,forwarded[j].count,forwarded[j].ip,forwarded[j].name);
swrite();
}
#if defined(DEBUG)
logg("Sent forwarded destinations data to client");
#endif
if(debug)
logg("Sent forwarded destinations data to client");
}
else if(command(">querytypes"))
{
sprintf(socketsendbuffer,"A (IPv4): %i\nAAAA (IPv6): %i\nPTR: %i\nSRV: %i\n",counters.IPv4,counters.IPv6,counters.PTR,counters.SRV);
swrite();
#if defined(DEBUG)
logg("Sent query type data to client");
#endif
if(debug)
logg("Sent query type data to client");
}
else if(command(">getallqueries"))
{
@ -230,10 +224,11 @@ void process_request(void)
{
// Get from to until boundaries
sscanf(socketrecvbuffer, ">getallqueries-time %i %i",&from, &until);
#if defined(DEBUG)
logg_int("Showing only limited time interval starting at ",from);
logg_int("Showing only limited time interval ending at ",until);
#endif
if(debug)
{
logg_int("Showing only limited time interval starting at ",from);
logg_int("Showing only limited time interval ending at ",until);
}
filtertime = true;
}
@ -244,9 +239,8 @@ void process_request(void)
domainname = calloc(128, sizeof(char));
// Get domain name we want to see only (limit length to 127 chars)
sscanf(socketrecvbuffer, ">getallqueries-domain %127s", domainname);
#if defined(DEBUG)
logg_str("Showing only queries with domain ", domainname);
#endif
if(debug)
logg_str("Showing only queries with domain ", domainname);
filterdomainname = true;
}
@ -257,9 +251,8 @@ void process_request(void)
clientname = calloc(128, sizeof(char));
// Get client name we want to see only (limit length to 127 chars)
sscanf(socketrecvbuffer, ">getallqueries-client %127s", clientname);
#if defined(DEBUG)
logg_str("Showing only queries with client ", clientname);
#endif
if(debug)
logg_str("Showing only queries with client ", clientname);
filterclientname = true;
}
@ -272,9 +265,8 @@ void process_request(void)
ibeg = counters.queries-num;
if(ibeg < 0)
ibeg = 0;
#if defined(DEBUG)
logg_int("Showing only limited amount of queries: ",num);
#endif
if(debug)
logg_int("Showing only limited amount of queries: ",num);
}
// Get potentially existing filtering flags
@ -306,18 +298,21 @@ void process_request(void)
privacymode = true;
clearSetupVarsArray();
#if defined(DEBUG)
if(showpermitted)
logg("Showing permitted queries");
else
logg("Hiding permitted queries");
if(showblocked)
logg("Showing blocked queries");
else
logg("Hiding blocked queries");
if(privacymode)
logg("Privacy mode enabled");
#endif
if(debug)
{
if(showpermitted)
logg("Showing permitted queries");
else
logg("Hiding permitted queries");
if(showblocked)
logg("Showing blocked queries");
else
logg("Hiding blocked queries");
if(privacymode)
logg("Privacy mode enabled");
}
int i;
for(i=ibeg; i < counters.queries; i++)
@ -372,9 +367,8 @@ void process_request(void)
if(filterdomainname)
free(domainname);
#if defined(DEBUG)
logg("Sent all queries data to client");
#endif
if(debug)
logg("Sent all queries data to client");
}
else if(command(">recentBlocked"))
{
@ -385,9 +379,8 @@ void process_request(void)
// User wants a different number of requests
if(num >= counters.queries)
num = 0;
#if defined(DEBUG)
logg_int("Showing several blocked domains ",num);
#endif
if(debug)
logg_int("Showing several blocked domains ",num);
}
// Find most recent query with either status 1 (blocked)
// or status 4 (wildcard blocked)
@ -412,9 +405,8 @@ void process_request(void)
{
close(clientsocket);
clientsocket = 0;
#if defined(DEBUG)
logg("Clients wants to quit");
#endif
if(debug)
logg("Clients wants to quit");
}
else if(command(">kill"))
{

View File

@ -51,3 +51,5 @@ void getSetupVarsArray(char * input);
void clearSetupVarsArray(void);
bool insetupVarsArray(char * str);
bool getSetupVarsBool(char * input);
void parse_args(int argc, char* argv[]);

View File

@ -108,9 +108,9 @@ bool listen_socket(void)
// printf("ERROR on accept");
if (clientsocket > 0)
{
#if defined(DEBUG)
logg_str("Client connected: ", inet_ntoa (cli_addr.sin_addr));
#endif
if(debug)
logg_str("Client connected: ", inet_ntoa (cli_addr.sin_addr));
// const char * msg = "This is the Pi-hole FTL daemon, enter \"quit\" to quit\n\n";
// write(clientsocket, msg, strlen(msg));
return true;