Skip to content

Commit

Permalink
Merge pull request #998 from pi-hole/release/v5.3.3
Browse files Browse the repository at this point in the history
Pi-hole FTL v5.3.3
  • Loading branch information
DL6ER authored Dec 24, 2020
2 parents 0790cf7 + 3506b37 commit 66919f2
Show file tree
Hide file tree
Showing 65 changed files with 1,622 additions and 368 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
cmake_minimum_required(VERSION 2.8.12)
project(PIHOLE_FTL C)

set(DNSMASQ_VERSION pi-hole-2.81)
set(DNSMASQ_VERSION pi-hole-2.82)

add_subdirectory(src)
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ set(sources
log.h
main.c
main.h
memory.c
memory.h
overTime.c
overTime.h
regex.c
Expand Down Expand Up @@ -174,6 +172,7 @@ add_executable(pihole-FTL
$<TARGET_OBJECTS:sqlite3>
$<TARGET_OBJECTS:lua>
$<TARGET_OBJECTS:tre-regex>
$<TARGET_OBJECTS:syscalls>
)
if(STATIC STREQUAL "true")
set_target_properties(pihole-FTL PROPERTIES LINK_SEARCH_START_STATIC ON)
Expand Down Expand Up @@ -228,3 +227,4 @@ add_subdirectory(database)
add_subdirectory(dnsmasq)
add_subdirectory(lua)
add_subdirectory(tre-regex)
add_subdirectory(syscalls)
29 changes: 22 additions & 7 deletions src/FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@
// can be 24 hours + 59 minutes
#define OVERTIME_SLOTS ((MAXLOGAGE+1)*3600/OVERTIME_INTERVAL)

// Interval for resolving NEW client and upstream server host names [seconds]
// Default: 60 (once every minute)
#define RESOLVE_INTERVAL 60

// Interval for re-resolving ALL known host names [seconds]
// Default: 3600 (once every hour)
#define RERESOLVE_INTERVAL 3600
Expand Down Expand Up @@ -114,16 +110,35 @@
// Important: This number has to be smaller than 256 for this mechanism to work
#define NUM_RECHECKS 3

// Use out own memory handling functions that will detect possible errors
// Use out own syscalls handling functions that will detect possible errors
// and report accordingly in the log. This will make debugging FTL crashs
// caused by insufficient memory or by code bugs (not properly dealing
// with NULL pointers) much easier.
#undef strdup // strdup() is a macro in itself, it needs special handling
#define free(ptr) FTLfree(ptr, __FILE__, __FUNCTION__, __LINE__)
#define lib_strdup() strdup()
#undef strdup
#define strdup(str_in) FTLstrdup(str_in, __FILE__, __FUNCTION__, __LINE__)
#define calloc(numer_of_elements, element_size) FTLcalloc(numer_of_elements, element_size, __FILE__, __FUNCTION__, __LINE__)
#define realloc(ptr, new_size) FTLrealloc(ptr, new_size, __FILE__, __FUNCTION__, __LINE__)
#define printf(format, ...) FTLfprintf(stdout, __FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__)
#define fprintf(stream, format, ...) FTLfprintf(stream, __FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__)
#define vprintf(format, args) FTLvfprintf(stdout, __FILE__, __FUNCTION__, __LINE__, format, args)
#define vfprintf(stream, format, args) FTLvfprintf(stream, __FILE__, __FUNCTION__, __LINE__, format, args)
#define sprintf(buffer, format, ...) FTLsprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, ##__VA_ARGS__)
#define vsprintf(buffer, format, args) FTLvsprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, args)
#define asprintf(buffer, format, ...) FTLasprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, ##__VA_ARGS__)
#define vasprintf(buffer, format, args) FTLvasprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, args)
#define snprintf(buffer, maxlen, format, ...) FTLsnprintf(__FILE__, __FUNCTION__, __LINE__, buffer, maxlen, format, ##__VA_ARGS__)
#define vsnprintf(buffer, maxlen, format, args) FTLvsnprintf(__FILE__, __FUNCTION__, __LINE__, buffer, maxlen, format, args)
#define write(fd, buf, n) FTLwrite(fd, buf, n, __FILE__, __FUNCTION__, __LINE__)
#define accept(sockfd, addr, addrlen) FTLaccept(sockfd, addr, addrlen, __FILE__, __FUNCTION__, __LINE__)
#define recv(sockfd, buf, len, flags) FTLrecv(sockfd, buf, len, flags, __FILE__, __FUNCTION__, __LINE__)
#define recvfrom(sockfd, buf, len, flags, src_addr, addrlen) FTLrecvfrom(sockfd, buf, len, flags, src_addr, addrlen, __FILE__, __FUNCTION__, __LINE__)
#define sendto(sockfd, buf, len, flags, dest_addr, addrlen) FTLsendto(sockfd, buf, len, flags, dest_addr, addrlen, __FILE__, __FUNCTION__, __LINE__)
#define select(nfds, readfds, writefds, exceptfds, timeout) FTLselect(nfds, readfds, writefds, exceptfds, timeout, __FILE__, __FUNCTION__, __LINE__)
#define pthread_mutex_lock(mutex) FTLpthread_mutex_lock(mutex, __FILE__, __FUNCTION__, __LINE__)
#define fopen(pathname, mode) FTLfopen(pathname, mode, __FILE__, __FUNCTION__, __LINE__)
#define ftlallocate(fd, offset, len) FTLfallocate(fd, offset, len, __FILE__, __FUNCTION__, __LINE__)
#include "syscalls/syscalls.h"

// Preprocessor help functions
#define str(x) # x
Expand Down
22 changes: 11 additions & 11 deletions src/api/msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
void pack_eom(const int sock) {
// This byte is explicitly never used in the MessagePack spec, so it is perfect to use as an EOM for this API.
uint8_t eom = 0xc1;
swrite(sock, &eom, sizeof(eom));
write(sock, &eom, sizeof(eom));
}

static void pack_basic(const int sock, const uint8_t format, const void *value, const size_t size) {
swrite(sock, &format, sizeof(format));
swrite(sock, value, size);
write(sock, &format, sizeof(format));
write(sock, value, size);
}

static uint64_t __attribute__((const)) leToBe64(const uint64_t value) {
Expand All @@ -42,7 +42,7 @@ static uint64_t __attribute__((const)) leToBe64(const uint64_t value) {

void pack_bool(const int sock, const bool value) {
uint8_t packed = (uint8_t) (value ? 0xc3 : 0xc2);
swrite(sock, &packed, sizeof(packed));
write(sock, &packed, sizeof(packed));
}

void pack_uint8(const int sock, const uint8_t value) {
Expand Down Expand Up @@ -87,8 +87,8 @@ bool pack_fixstr(const int sock, const char *string) {
}

const uint8_t format = (uint8_t) (0xA0 | length);
swrite(sock, &format, sizeof(format));
swrite(sock, string, length);
write(sock, &format, sizeof(format));
write(sock, string, length);

return true;
}
Expand All @@ -104,17 +104,17 @@ bool pack_str32(const int sock, const char *string) {
}

const uint8_t format = 0xdb;
swrite(sock, &format, sizeof(format));
write(sock, &format, sizeof(format));
const uint32_t bigELength = htonl((uint32_t) length);
swrite(sock, &bigELength, sizeof(bigELength));
swrite(sock, string, length);
write(sock, &bigELength, sizeof(bigELength));
write(sock, string, length);

return true;
}

void pack_map16_start(const int sock, const uint16_t length) {
const uint8_t format = 0xde;
swrite(sock, &format, sizeof(format));
write(sock, &format, sizeof(format));
const uint16_t bigELength = htons(length);
swrite(sock, &bigELength, sizeof(bigELength));
write(sock, &bigELength, sizeof(bigELength));
}
24 changes: 10 additions & 14 deletions src/api/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

#include "FTL.h"
#include "api.h"
#include "log.h"
#include "../log.h"
#include "socket.h"
#include "request.h"
#include "config.h"
#include "memory.h"
#include "../config.h"
// global variable killed
#include "signals.h"
#include "../signals.h"

// The backlog argument defines the maximum length
// to which the queue of pending connections for
Expand Down Expand Up @@ -223,21 +222,15 @@ void __attribute__ ((format (gnu_printf, 2, 3))) ssend(const int sock, const cha
char *buffer;
va_list args;
va_start(args, format);
int ret = vasprintf(&buffer, format, args);
int bytes = vasprintf(&buffer, format, args);
va_end(args);
if(ret > 0)
if(bytes > 0 && buffer != NULL)
{
if(!write(sock, buffer, strlen(buffer)))
logg("WARNING: Socket write returned error %s (%i)", strerror(errno), errno);
write(sock, buffer, bytes);
free(buffer);
}
}

void swrite(const int sock, const void *value, size_t size) {
if(write(sock, value, size) == -1)
logg("WARNING: Socket write returned error code %i", errno);
}

static inline int checkClientLimit(const int socket) {
if(socket < MAXCONNS)
{
Expand Down Expand Up @@ -519,8 +512,11 @@ void *socket_listening_thread(void *args)

// Return early to avoid CPU spinning if Unix socket is not available
sock_avail = bind_to_unix_socket(&socketfd);
if(sock_avail)
if(!sock_avail)
{
logg("INFO: Unix socket will not be available");
return NULL;
}

// Listen as long as FTL is not killed
while(!killed)
Expand Down
1 change: 0 additions & 1 deletion src/api/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ void close_telnet_socket(void);
void close_unix_socket(bool unlink_file);
void seom(const int sock);
void ssend(const int sock, const char *format, ...) __attribute__ ((format (gnu_printf, 2, 3)));
void swrite(const int sock, const void* value, const size_t size);
void *telnet_listening_thread_IPv4(void *args);
void *telnet_listening_thread_IPv6(void *args);
void *socket_listening_thread(void *args);
Expand Down
101 changes: 29 additions & 72 deletions src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "FTL.h"
#include "args.h"
#include "version.h"
#include "memory.h"
#include "main.h"
#include "log.h"
// global variable killed
Expand All @@ -27,8 +26,6 @@
#include "shmem.h"
// LUA dependencies
#include "lua/ftl_lua.h"
#include <readline/history.h>
#include <wordexp.h>
// run_dhcp_discover()
#include "dhcp-discover.h"
// defined in dnsmasq.c
Expand Down Expand Up @@ -58,11 +55,31 @@ void parse_args(int argc, char* argv[])
if(strEndsWith(argv[0], "dnsmasq"))
consume_for_dnsmasq = true;

if(strEndsWith(argv[0], "lua"))
exit(run_lua_interpreter(argc, argv, false));

if(strEndsWith(argv[0], "luac"))
exit(run_luac(argc, argv));

// start from 1, as argv[0] is the executable name
for(int i = 1; i < argc; i++)
{
bool ok = false;

// Expose internal lua interpreter
if(strcmp(argv[i], "lua") == 0 ||
strcmp(argv[i], "--lua") == 0)
{
exit(run_lua_interpreter(argc - i, &argv[i], dnsmasq_debug));
}

// Expose internal lua compiler
if(strcmp(argv[i], "luac") == 0 ||
strcmp(argv[i], "--luac") == 0)
{
exit(luac_main(argc - i, &argv[i]));
}

// Implement dnsmasq's test function, no need to prepare the entire FTL
// environment (initialize shared memory, lead queries from long-term
// database, ...) when the task is a simple (dnsmasq) syntax check
Expand Down Expand Up @@ -279,78 +296,18 @@ void parse_args(int argc, char* argv[])
exit(EXIT_SUCCESS);
}

// Expose internal lua interpreter
if(strcmp(argv[i], "lua") == 0 ||
strcmp(argv[i], "--lua") == 0)
{
if(argc == i + 1) // No arguments after this one
printf("Pi-hole FTL %s\n", get_FTL_version());
#if defined(LUA_USE_READLINE)
wordexp_t word;
wordexp(LUA_HISTORY_FILE, &word, WRDE_NOCMD);
const char *history_file = NULL;
if(word.we_wordc == 1)
{
history_file = word.we_wordv[0];
const int ret_r = read_history(history_file);
if(dnsmasq_debug)
{
printf("Reading history ... ");
if(ret_r == 0)
printf("success\n");
else
printf("error - %s: %s\n", history_file, strerror(ret_r));
}

// The history file may not exist, try to create an empty one in this case
if(ret_r == ENOENT)
{
if(dnsmasq_debug)
{
printf("Creating new history file: %s\n", history_file);
}
FILE *history = fopen(history_file, "w");
if(history != NULL)
fclose(history);
}
}
#else
if(dnsmasq_debug)
printf("No readline available!\n");
#endif
const int ret = lua_main(argc - i, &argv[i]);
#if defined(LUA_USE_READLINE)
if(history_file != NULL)
{
const int ret_w = write_history(history_file);
if(dnsmasq_debug)
{
printf("Writing history ... ");
if(ret_w == 0)
printf("success\n");
else
printf("error - %s: %s\n", history_file, strerror(ret_w));
}

wordfree(&word);
}
#endif
exit(ret);
}

// Expose internal lua compiler
if(strcmp(argv[i], "luac") == 0 ||
strcmp(argv[i], "--luac") == 0)
{
if(argc == i + 1) // No arguments after this one
printf("Pi-hole FTL %s\n", get_FTL_version());
exit(luac_main(argc - i, &argv[i]));
}

// Complain if invalid options have been found
if(!ok)
{
printf("pihole-FTL: invalid option -- '%s'\nTry '%s --help' for more information\n", argv[i], argv[0]);
printf("pihole-FTL: invalid option -- '%s'\n", argv[i]);
printf("Command: '");
for(int j = 0; j < argc; j++)
{
printf("%s", argv[j]);
if(j < argc - 1)
printf(" ");
}
printf("'\nTry '%s --help' for more information\n", argv[0]);
exit(EXIT_FAILURE);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/capabilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#undef __USE_XOPEN
#include "FTL.h"
#include "capabilities.h"
#include "memory.h"
#include "config.h"
#include "log.h"

Expand Down
Loading

0 comments on commit 66919f2

Please sign in to comment.