2019-06-30 00:01:37 +08:00
|
|
|
/* 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
|
|
|
|
* Datastructure prototypes
|
|
|
|
*
|
|
|
|
* This file is copyright under the latest version of the EUPL.
|
|
|
|
* Please see LICENSE file for your rights under this license. */
|
|
|
|
#ifndef DATASTRUCTURE_H
|
|
|
|
#define DATASTRUCTURE_H
|
|
|
|
|
2019-09-07 17:48:32 +08:00
|
|
|
// Definition of sqlite3_stmt
|
2019-09-07 18:14:23 +08:00
|
|
|
#include "database/sqlite3.h"
|
2019-09-07 17:48:32 +08:00
|
|
|
|
2020-07-09 05:15:55 +08:00
|
|
|
// enum privacy_level
|
|
|
|
#include "enums.h"
|
2022-04-18 16:39:43 +08:00
|
|
|
|
|
|
|
// Definitions like OVERTIME_SLOT
|
|
|
|
#include "FTL.h"
|
2020-07-09 05:15:55 +08:00
|
|
|
|
2020-11-29 18:16:01 +08:00
|
|
|
extern const char *querytypes[TYPE_MAX];
|
2019-12-02 23:37:31 +08:00
|
|
|
|
2019-07-07 16:09:18 +08:00
|
|
|
typedef struct {
|
|
|
|
unsigned char magic;
|
2020-07-09 05:15:55 +08:00
|
|
|
enum query_status status;
|
|
|
|
enum query_types type;
|
|
|
|
enum privacy_level privacylevel;
|
|
|
|
enum reply_type reply;
|
|
|
|
enum dnssec_status dnssec;
|
2021-01-04 04:50:24 +08:00
|
|
|
uint16_t qtype;
|
2019-07-07 16:09:18 +08:00
|
|
|
int domainID;
|
|
|
|
int clientID;
|
2020-02-11 16:53:22 +08:00
|
|
|
int upstreamID;
|
2019-07-07 16:09:18 +08:00
|
|
|
int id; // the ID is a (signed) int in dnsmasq, so no need for a long int here
|
2020-01-29 16:33:09 +08:00
|
|
|
int CNAME_domainID; // only valid if query has a CNAME blocking status
|
2021-07-02 23:39:58 +08:00
|
|
|
int ede;
|
2019-07-07 16:09:18 +08:00
|
|
|
unsigned long response; // saved in units of 1/10 milliseconds (1 = 0.1ms, 2 = 0.2ms, 2500 = 250.0ms, etc.)
|
2021-01-04 04:50:24 +08:00
|
|
|
time_t timestamp;
|
2021-01-17 18:39:12 +08:00
|
|
|
// Adjacent bit field members in the struct flags may be packed to share
|
|
|
|
// and straddle the individual bytes. It is useful to pack the memory as
|
|
|
|
// tightly as possible as there may be dozens of thousands of these
|
|
|
|
// objects in memory (one per query).
|
2022-01-19 00:07:51 +08:00
|
|
|
// C99 guarantees that bit-fields will be packed as tightly as possible,
|
|
|
|
// provided they don't cross storage unit boundaries (6.7.2.1 #10).
|
2021-01-17 18:39:12 +08:00
|
|
|
struct query_flags {
|
|
|
|
bool whitelisted :1;
|
|
|
|
bool complete :1;
|
2021-01-18 21:57:20 +08:00
|
|
|
bool blocked :1;
|
2021-07-02 23:26:41 +08:00
|
|
|
bool database :1;
|
2021-10-11 01:21:24 +08:00
|
|
|
bool response_calculated :1;
|
2021-01-17 18:39:12 +08:00
|
|
|
} flags;
|
2019-07-07 16:09:18 +08:00
|
|
|
} queriesData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char magic;
|
2019-12-31 05:24:23 +08:00
|
|
|
bool new;
|
2021-06-25 19:10:39 +08:00
|
|
|
in_addr_t port;
|
2019-07-07 16:09:18 +08:00
|
|
|
int failed;
|
2021-06-25 19:10:39 +08:00
|
|
|
int overTime[OVERTIME_SLOTS];
|
2019-12-31 05:24:23 +08:00
|
|
|
size_t ippos;
|
|
|
|
size_t namepos;
|
2020-08-25 02:39:41 +08:00
|
|
|
time_t lastQuery;
|
2020-02-11 16:53:22 +08:00
|
|
|
} upstreamsData;
|
2019-07-07 16:09:18 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char magic;
|
2020-06-22 04:44:04 +08:00
|
|
|
unsigned char reread_groups;
|
2020-08-09 16:29:24 +08:00
|
|
|
char hwlen;
|
|
|
|
unsigned char hwaddr[16]; // See DHCP_CHADDR_MAX in dnsmasq/dhcp-protocol.h
|
2021-01-17 18:39:12 +08:00
|
|
|
struct client_flags {
|
|
|
|
bool new:1;
|
|
|
|
bool found_group:1;
|
|
|
|
bool aliasclient:1;
|
2021-09-20 18:08:58 +08:00
|
|
|
bool rate_limited:1;
|
2021-01-17 18:39:12 +08:00
|
|
|
} flags;
|
2019-07-07 16:09:18 +08:00
|
|
|
int count;
|
|
|
|
int blockedcount;
|
2020-11-15 15:50:24 +08:00
|
|
|
int aliasclient_id;
|
2020-12-23 17:26:50 +08:00
|
|
|
unsigned int id;
|
2021-02-03 23:17:21 +08:00
|
|
|
unsigned int rate_limit;
|
2019-07-07 16:09:18 +08:00
|
|
|
unsigned int numQueriesARP;
|
2021-01-17 18:39:12 +08:00
|
|
|
int overTime[OVERTIME_SLOTS];
|
2020-05-26 00:40:16 +08:00
|
|
|
size_t groupspos;
|
2019-12-31 05:24:23 +08:00
|
|
|
size_t ippos;
|
|
|
|
size_t namepos;
|
2020-05-20 20:29:32 +08:00
|
|
|
size_t ifacepos;
|
2019-12-31 05:24:23 +08:00
|
|
|
time_t lastQuery;
|
2020-06-06 02:09:54 +08:00
|
|
|
time_t firstSeen;
|
2019-07-07 16:09:18 +08:00
|
|
|
} clientsData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char magic;
|
|
|
|
int count;
|
|
|
|
int blockedcount;
|
2022-02-04 21:27:36 +08:00
|
|
|
uint32_t domainhash;
|
2021-01-17 18:39:12 +08:00
|
|
|
size_t domainpos;
|
2019-07-07 16:09:18 +08:00
|
|
|
} domainsData;
|
|
|
|
|
2019-12-30 19:18:04 +08:00
|
|
|
typedef struct {
|
|
|
|
unsigned char magic;
|
2020-07-09 05:15:55 +08:00
|
|
|
enum domain_client_status blocking_status;
|
2021-04-16 17:19:45 +08:00
|
|
|
enum reply_type force_reply;
|
2020-07-07 04:04:54 +08:00
|
|
|
enum query_types query_type;
|
2019-12-30 19:18:04 +08:00
|
|
|
int domainID;
|
|
|
|
int clientID;
|
2022-08-19 04:42:37 +08:00
|
|
|
int domainlist_id;
|
2019-12-30 19:18:04 +08:00
|
|
|
} DNSCacheData;
|
|
|
|
|
2020-07-07 04:04:54 +08:00
|
|
|
void strtolower(char *str);
|
2022-11-03 04:33:37 +08:00
|
|
|
uint32_t hashStr(const char *s) __attribute__((pure));
|
2020-08-11 06:21:33 +08:00
|
|
|
int findQueryID(const int id);
|
2021-02-13 23:49:37 +08:00
|
|
|
int findUpstreamID(const char * upstream, const in_port_t port);
|
2020-07-07 04:04:54 +08:00
|
|
|
int findDomainID(const char *domain, const bool count);
|
2020-11-15 15:50:24 +08:00
|
|
|
int findClientID(const char *client, const bool count, const bool aliasclient);
|
2022-08-20 21:27:38 +08:00
|
|
|
#define findCacheID(domainID, clientID, query_type, create_new) _findCacheID(domainID, clientID, query_type, create_new, __FUNCTION__, __LINE__, __FILE__)
|
|
|
|
int _findCacheID(const int domainID, const int clientID, const enum query_types query_type, const bool create_new, const char *func, const int line, const char *file);
|
2020-07-07 04:04:54 +08:00
|
|
|
bool isValidIPv4(const char *addr);
|
|
|
|
bool isValidIPv6(const char *addr);
|
|
|
|
|
2021-04-07 02:01:47 +08:00
|
|
|
bool is_blocked(const enum query_status status) __attribute__ ((const));
|
2022-08-20 20:30:48 +08:00
|
|
|
#define query_set_status(query, new_status) _query_set_status(query, new_status, __FUNCTION__, __LINE__, __FILE__)
|
|
|
|
void _query_set_status(queriesData *query, const enum query_status new_status, const char *func, const int line, const char *file);
|
2021-04-07 02:01:47 +08:00
|
|
|
|
2020-07-07 04:04:54 +08:00
|
|
|
void FTL_reload_all_domainlists(void);
|
|
|
|
void FTL_reset_per_client_domain_data(void);
|
|
|
|
|
2020-01-29 16:33:09 +08:00
|
|
|
const char *getDomainString(const queriesData* query);
|
|
|
|
const char *getCNAMEDomainString(const queriesData* query);
|
|
|
|
const char *getClientIPString(const queriesData* query);
|
|
|
|
const char *getClientNameString(const queriesData* query);
|
|
|
|
|
2020-08-11 06:55:16 +08:00
|
|
|
void change_clientcount(clientsData *client, int total, int blocked, int overTimeIdx, int overTimeMod);
|
|
|
|
|
2021-09-10 03:50:54 +08:00
|
|
|
const char *get_query_reply_str(const enum reply_type query) __attribute__ ((const));
|
|
|
|
|
2019-07-07 16:09:18 +08:00
|
|
|
// Pointer getter functions
|
|
|
|
#define getQuery(queryID, checkMagic) _getQuery(queryID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
|
2022-08-20 20:30:48 +08:00
|
|
|
queriesData* _getQuery(int queryID, bool checkMagic, int line, const char *func, const char *file);
|
2019-07-07 16:09:18 +08:00
|
|
|
#define getClient(clientID, checkMagic) _getClient(clientID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
|
2022-08-20 20:30:48 +08:00
|
|
|
clientsData* _getClient(int clientID, bool checkMagic, int line, const char *func, const char *file);
|
2019-07-07 16:09:18 +08:00
|
|
|
#define getDomain(domainID, checkMagic) _getDomain(domainID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
|
2022-08-20 20:30:48 +08:00
|
|
|
domainsData* _getDomain(int domainID, bool checkMagic, int line, const char *func, const char *file);
|
2020-02-11 16:53:22 +08:00
|
|
|
#define getUpstream(upstreamID, checkMagic) _getUpstream(upstreamID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
|
2022-08-20 20:30:48 +08:00
|
|
|
upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const char *func, const char *file);
|
2019-12-30 19:18:04 +08:00
|
|
|
#define getDNSCache(cacheID, checkMagic) _getDNSCache(cacheID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
|
2022-08-20 20:30:48 +08:00
|
|
|
DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char *func, const char *file);
|
2019-07-07 16:09:18 +08:00
|
|
|
|
2019-06-30 00:01:37 +08:00
|
|
|
#endif //DATASTRUCTURE_H
|