Skip to content

Commit

Permalink
Merge branch 'development' into new/DBinterval
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>

Conflicts:
	README.md
	config.c
  • Loading branch information
DL6ER committed Dec 12, 2017
2 parents 06b9e02 + 95bdd62 commit 59edb2a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct {
const char* log;
const char* pid;
const char* port;
const char* db;
char* db;
} FTLFileNamesStruct;

typedef struct {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Possible settings (**the option shown first is the default**):
- `RESOLVE_IPV6=yes|no` (Should `FTL` try to resolve IPv6 addresses to host names?)
- `RESOLVE_IPV4=yes|no` (Should `FTL` try to resolve IPv4 addresses to host names?)
- `DBINTERVAL=1.0` (How often do we store queries in FTL's database [minutes]?)
- `DBFILE=/etc/pihole/pihole-FTL.db` (Specify path and filename of FTL's SQLite long-term database. Setting this to `DBFILE=` disables the database altogether)

### Implemented keywords (starting with `>`, subject to change):

Expand Down
3 changes: 2 additions & 1 deletion args.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ void parse_args(int argc, char* argv[])
{
travis = true;
FTLfiles.log = "pihole-FTL.log";
FTLfiles.db = "pihole-FTL.db";
// FTLfiles.db will be set to "pihole-FTL.db" via config file on Travis
FTLfiles.conf = "pihole-FTL.conf";
files.log = "pihole.log";
ok = true;
}
Expand Down
27 changes: 26 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void read_FTLconf(void)
}

// Parse lines in the config file
logg("Starting config file parsing");
logg("Starting config file parsing (%s)", FTLfiles.conf);

// SOCKET_LISTENING
// defaults to: listen only local
Expand Down Expand Up @@ -150,8 +150,33 @@ void read_FTLconf(void)
else
logg(" DBINTERVAL: saving to DB file every %i seconds", config.DBinterval);

// DBFILE
// defaults to: "/etc/pihole/pihole-FTL.db"
buffer = parse_FTLconf(fp, "DBFILE");

errno = 0;
// Use sscanf() to obtain filename from config file parameter only if buffer != NULL
if(!(buffer != NULL && sscanf(buffer, "%127ms", &FTLfiles.db)))
{
// Use standard path if no custom path was obtained from the config file
FTLfiles.db = strdup("/etc/pihole/pihole-FTL.db");
}

// Test if memory allocation was successful
if(FTLfiles.db == NULL && errno != 0)
{
logg("FATAL: Allocating memory for FTLfiles.db failed (%s, %i). Exiting.", strerror(errno), errno);
exit(EXIT_FAILURE);
}
else if(FTLfiles.db != NULL && strlen(FTLfiles.db) > 0)
logg(" DBFILE: Using %s", FTLfiles.db);
else
logg(" DBFILE: Not using database due to empty filename");


logg("Finished config file parsing");

// Release memory
if(conflinebuffer != NULL)
{
free(conflinebuffer);
Expand Down
8 changes: 8 additions & 0 deletions database.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ bool db_create(void)

void db_init(void)
{
// First check if the user doesn't want to use the database and set an
// empty string as file name in FTL's config file
if(FTLfiles.db == NULL || strlen(FTLfiles.db) == 0)
{
database = false;
return;
}

int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE, NULL);
if( rc ){
logg("db_init() - Cannot open database (%i): %s", rc, sqlite3_errmsg(db));
Expand Down
3 changes: 2 additions & 1 deletion parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ void process_pihole_log(int file)
char *domainwithspaces = calloc(domainlen+3,sizeof(char));
// strncat() NULL-terminates the copied string (strncpy() doesn't!)
strncat(domain,domainstart+2,domainlen);
// Copy string into buffer surrounded by spaces
sprintf(domainwithspaces," %s ",domain);
// Convert domain to lower case
strtolower(domain);
sprintf(domainwithspaces," %s ",domain);

if(strcmp(domain, "pi.hole") == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FTLFileNamesStruct FTLfiles = {
"/var/log/pihole-FTL.log",
"/var/run/pihole-FTL.pid",
"/var/run/pihole-FTL.port",
"/etc/pihole/pihole-FTL.db"
NULL
};

logFileNamesStruct files = {
Expand Down
16 changes: 10 additions & 6 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ ts="$(dnsmasq_pre)"
cat <<EOT >> pihole.log
${ts} query[AAAA] raspberrypi from 127.0.0.1
${ts} /etc/pihole/local.list raspberrypi is fda2:2001:5647:0:ba27:ebff:fe37:4205
${ts} query[A] checkip.dyndns.org from 127.0.0.1
${ts} forwarded checkip.dyndns.org to 2001:1608:10:25::9249:d69b
${ts} forwarded checkip.dyndns.org to 2001:1608:10:25::1c04:b12f
${ts} forwarded checkip.dyndns.org to 2620:0:ccd::2
${ts} forwarded checkip.dyndns.org to 2620:0:ccc::2
${ts} reply checkip.dyndns.org is <CNAME>
${ts} query[A] ChEcKiP.DyNdNs.OrG from 127.0.0.1
${ts} forwarded ChEcKiP.DyNdNs.OrG to 2001:1608:10:25::9249:d69b
${ts} forwarded ChEcKiP.DyNdNs.OrG to 2001:1608:10:25::1c04:b12f
${ts} forwarded ChEcKiP.DyNdNs.OrG to 2620:0:ccd::2
${ts} forwarded ChEcKiP.DyNdNs.OrG to 2620:0:ccc::2
${ts} reply ChEcKiP.DyNdNs.OrG is <CNAME>
${ts} reply checkip.dyndns.com is 216.146.38.70
${ts} reply checkip.dyndns.com is 216.146.43.71
${ts} reply checkip.dyndns.com is 91.198.22.70
Expand Down Expand Up @@ -44,6 +44,10 @@ ${ts} /etc/pihole/gravity.list addomain.com is 1.2.3.4
EOT
touch "pihole-FTL.log"

cat <<EOT >> pihole-FTL.conf
DBFILE=pihole-FTL.db
EOT

# Start FTL
./pihole-FTL travis-ci

Expand Down

0 comments on commit 59edb2a

Please sign in to comment.