forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.c
198 lines (191 loc) · 4.24 KB
/
request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 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
* Socket request handling 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"
#include "api.h"
#include "shmem.h"
bool __attribute__((pure)) command(const char *client_message, const char* cmd) {
return strstr(client_message, cmd) != NULL;
}
void process_request(const char *client_message, int *sock)
{
char EOT[2];
EOT[0] = 0x04;
EOT[1] = 0x00;
bool processed = false;
if(command(client_message, ">stats"))
{
processed = true;
lock_shm();
getStats(sock);
unlock_shm();
}
else if(command(client_message, ">overTime"))
{
processed = true;
lock_shm();
getOverTime(sock);
unlock_shm();
}
else if(command(client_message, ">top-domains") || command(client_message, ">top-ads"))
{
processed = true;
lock_shm();
getTopDomains(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">top-clients"))
{
processed = true;
lock_shm();
getTopClients(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">forward-dest"))
{
processed = true;
lock_shm();
getForwardDestinations(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">forward-names"))
{
processed = true;
lock_shm();
getForwardDestinations(">forward-dest unsorted", sock);
unlock_shm();
}
else if(command(client_message, ">querytypes"))
{
processed = true;
lock_shm();
getQueryTypes(sock);
unlock_shm();
}
else if(command(client_message, ">getallqueries"))
{
processed = true;
lock_shm();
getAllQueries(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">recentBlocked"))
{
processed = true;
lock_shm();
getRecentBlocked(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">clientID"))
{
processed = true;
lock_shm();
getClientID(sock);
unlock_shm();
}
else if(command(client_message, ">QueryTypesoverTime"))
{
processed = true;
lock_shm();
getQueryTypesOverTime(sock);
unlock_shm();
}
else if(command(client_message, ">version"))
{
processed = true;
// No lock required
getVersion(sock);
}
else if(command(client_message, ">dbstats"))
{
processed = true;
// No lock required. Access to the database
// is guaranteed to be atomic
getDBstats(sock);
}
else if(command(client_message, ">ClientsoverTime"))
{
processed = true;
lock_shm();
getClientsOverTime(sock);
unlock_shm();
}
else if(command(client_message, ">client-names"))
{
processed = true;
lock_shm();
getClientNames(sock);
unlock_shm();
}
else if(command(client_message, ">unknown"))
{
processed = true;
lock_shm();
getUnknownQueries(sock);
unlock_shm();
}
else if(command(client_message, ">domain"))
{
processed = true;
lock_shm();
getDomainDetails(client_message, sock);
unlock_shm();
}
else if(command(client_message, ">cacheinfo"))
{
processed = true;
lock_shm();
getCacheInformation(sock);
unlock_shm();
}
else if(command(client_message, ">reresolve"))
{
processed = true;
logg("Received API request to re-resolve host names");
// Important: Don't obtain a lock for this request
// Locking will be done internally when needed
// onlynew=false -> reresolve all host names
resolveClients(false);
resolveForwardDestinations(false);
logg("Done re-resolving host names");
}
else if(command(client_message, ">recompile-regex"))
{
processed = true;
logg("Received API request to recompile regex");
lock_shm();
free_regex();
read_regex_from_file();
unlock_shm();
}
else if(command(client_message, ">update-mac-vendor"))
{
processed = true;
logg("Received API request to update vendors in network table");
updateMACVendorRecords();
}
// Test only at the end if we want to quit or kill
// so things can be processed before
if(command(client_message, ">quit") || command(client_message, EOT))
{
processed = true;
close(*sock);
*sock = 0;
}
if(!processed)
{
ssend(*sock,"unknown command: %s\n",client_message);
}
// End of queryable commands
if(*sock != 0)
{
// Send EOM
seom(*sock);
}
}