forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicetracker_view.cc
487 lines (376 loc) · 16.4 KB
/
devicetracker_view.cc
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "devicetracker_view.h"
#include "devicetracker_component.h"
#include "util.h"
#include "kis_mutex.h"
#include "kismet_algorithm.h"
DevicetrackerView::DevicetrackerView(const std::string& in_id, const std::string& in_description,
new_device_cb in_new_cb, updated_device_cb in_update_cb) :
tracker_component{},
new_cb {in_new_cb},
update_cb {in_update_cb} {
using namespace std::placeholders;
register_fields();
reserve_fields(nullptr);
view_id->set(in_id);
view_description->set(in_description);
device_list = std::make_shared<TrackerElementVector>();
auto uri = fmt::format("/devices/views/{}/devices", in_id);
device_endp =
std::make_shared<Kis_Net_Httpd_Simple_Post_Endpoint>(uri, false,
[this](std::ostream& stream, const std::string& uri, SharedStructured post_structured,
Kis_Net_Httpd_Connection::variable_cache_map& variable_cache) -> unsigned int {
return device_endpoint_handler(stream, uri, post_structured, variable_cache);
}, &mutex);
time_endp =
std::make_shared<Kis_Net_Httpd_Path_Tracked_Endpoint>(
[this](const std::vector<std::string>& path) -> bool {
return device_time_endpoint_path(path);
}, false,
[this](const std::vector<std::string>& path) -> std::shared_ptr<TrackerElement> {
return device_time_endpoint(path);
}, &mutex);
}
std::shared_ptr<TrackerElementVector> DevicetrackerView::doDeviceWork(DevicetrackerViewWorker& worker) {
// Make a copy of the vector
local_demand_locker dl(&mutex);
dl.lock();
auto immutable_copy = std::make_shared<TrackerElementVector>(device_list);
dl.unlock();
return doDeviceWork(worker, immutable_copy);
}
std::shared_ptr<TrackerElementVector> DevicetrackerView::doDeviceWork(DevicetrackerViewWorker& worker,
std::shared_ptr<TrackerElementVector> devices) {
auto ret = std::make_shared<TrackerElementVector>();
kismet__for_each(devices->begin(), devices->end(),
[&](SharedTrackerElement val) {
if (val == nullptr)
return;
auto dev = std::static_pointer_cast<kis_tracked_device_base>(val);
bool m;
{
local_locker devlocker(&dev->device_mutex);
m = worker.matchDevice(dev);
}
if (m)
ret->push_back(dev);
});
worker.setMatchedDevices(ret);
return ret;
}
void DevicetrackerView::newDevice(std::shared_ptr<kis_tracked_device_base> device) {
local_locker l(&mutex);
if (new_cb != nullptr)
if (new_cb(device))
device_list->push_back(device);
}
void DevicetrackerView::updateDevice(std::shared_ptr<kis_tracked_device_base> device) {
local_locker l(&mutex);
if (update_cb == nullptr)
return;
bool retain = update_cb(device);
auto dpmi = device_presence_map.find(device->get_key());
// If we're adding the device (or keeping it) and we don't have it tracked,
// add it and record it in the presence map
if (retain && dpmi == device_presence_map.end()) {
device_list->push_back(device);
device_presence_map[device->get_key()] = true;
}
// if we're removing the device, find it in the vector and remove it, and remove
// it from the presence map; this is expensive
if (!retain && dpmi != device_presence_map.end()) {
for (auto di = device_list->begin(); di != device_list->end(); ++di) {
if (*di == device) {
device_list->erase(di);
break;
}
}
device_presence_map.erase(dpmi);
}
}
void DevicetrackerView::removeDevice(std::shared_ptr<kis_tracked_device_base> device) {
local_locker l(&mutex);
auto di = device_presence_map.find(device->get_key());
if (di != device_presence_map.end()) {
device_presence_map.erase(di);
for (auto vi = device_list->begin(); vi != device_list->end(); ++vi) {
if (*vi == device) {
device_list->erase(vi);
break;
}
}
}
}
bool DevicetrackerView::device_time_endpoint_path(const std::vector<std::string>& path) {
// /devices/views/[id]/last-time/[time]/devices
if (path.size() < 6)
return false;
if (path[0] != "devices" || path[1] != "views" || path[3] != "last-time" || path[5] != "devices")
return false;
if (path[2] != get_view_id())
return false;
try {
StringTo<int64_t>(path[4]);
} catch (const std::exception& e) {
return false;
}
return true;
}
std::shared_ptr<TrackerElement> DevicetrackerView::device_time_endpoint(const std::vector<std::string>& path) {
auto ret = std::make_shared<TrackerElementVector>();
if (path.size() < 6)
return ret;
auto tv = StringTo<int64_t>(path[4], 0);
time_t ts;
// Don't allow 'all' devices b/c it's really expensive
if (tv == 0)
return ret;
if (tv < 0)
ts = time(0) - tv;
else
ts = tv;
auto worker =
DevicetrackerViewFunctionWorker([&](std::shared_ptr<kis_tracked_device_base> dev) -> bool {
if (dev->get_last_time() < ts)
return false;
return true;
});
return doDeviceWork(worker);
}
unsigned int DevicetrackerView::device_endpoint_handler(std::ostream& stream,
const std::string& uri, SharedStructured structured,
std::map<std::string, std::shared_ptr<std::stringstream>>& postvars) {
// Summarization vector based on simplification part of shared data
auto summary_vec = std::vector<SharedElementSummary>{};
// Rename cache generated by summarization
auto rename_map = std::make_shared<TrackerElementSerializer::rename_map>();
// Timestamp limitation
time_t timestamp_min = 0;
// String search term, if any
auto search_term = std::string{};
// Search paths, if any
auto search_paths = std::vector<std::vector<int>>{};
// Order path
auto order_field = std::vector<int>{};
// Regular expression terms, if any
auto regex = SharedStructured{};
// Wrapper, if any, we insert under
std::shared_ptr<TrackerElementStringMap> wrapper_elem;
// Field we transmit in the final stage (dervied array, or map)
std::shared_ptr<TrackerElement> transmit;
// Windowed response elements, used in datatables and others
auto length_elem = std::make_shared<TrackerElementUInt64>();
auto start_elem = std::make_shared<TrackerElementUInt64>();
// Total and filtered output sizes
auto total_sz_elem = std::make_shared<TrackerElementUInt64>();
auto filtered_sz_elem = std::make_shared<TrackerElementUInt64>();
// Output device list, should be copied into for final output
auto output_devices_elem = std::make_shared<TrackerElementVector>();
// Datatables specific draw element
auto dt_draw_elem = std::make_shared<TrackerElementUInt64>();
try {
// If the structured component has a 'fields' record, derive the fields
// simplification
if (structured->hasKey("fields")) {
auto fields = structured->getStructuredByKey("fields");
auto fvec = fields->getStructuredArray();
for (const auto& i : fvec) {
if (i->isString()) {
auto s = std::make_shared<TrackerElementSummary>(i->getString());
summary_vec.push_back(s);
} else if (i->isArray()) {
auto mapvec = i->getStringVec();
if (mapvec.size() != 2)
throw StructuredDataException("Invalid field mapping, expected "
"[field, rename]");
auto s = std::make_shared<TrackerElementSummary>(mapvec[0], mapvec[1]);
summary_vec.push_back(s);
} else {
throw StructuredDataException("Invalid field mapping, expected "
"field or [field,rename]");
}
}
}
// Capture timestamp and negative-offset timestamp
int64_t raw_ts = structured->getKeyAsNumber("last_time", 0);
if (raw_ts < 0)
timestamp_min = time(0) + raw_ts;
else
timestamp_min = raw_ts;
// Regex
if (structured->hasKey("regex"))
regex = structured->getStructuredByKey("regex");
} catch (const StructuredDataException& e) {
stream << "Invalid request: " << e.what() << "\n";
return 400;
}
// Input fields from variables
unsigned int in_window_start = 0;
unsigned int in_window_len = 0;
unsigned int in_dt_draw = 0;
int in_order_column_num = 0;
unsigned int in_order_direction = 0;
// Column number->path field mapping
auto column_number_map = StructuredData::structured_num_map{};
// Parse datatables sub-data for windowing, etc
try {
// Extract the column number -> column fieldpath data
if (structured->hasKey("colmap"))
column_number_map = structured->getStructuredByKey("colmap")->getStructuredNumMap();
if (structured->getKeyAsBool("datatable", false)) {
// Extract from the raw postvars
if (postvars.find("start") != postvars.end())
*(postvars["start"]) >> in_window_start;
if (postvars.find("length") != postvars.end())
*(postvars["length"]) >> in_window_len;
if (postvars.find("draw") != postvars.end())
*(postvars["draw"]) >> in_dt_draw;
if (postvars.find("search[value]") != postvars.end())
*(postvars["search[value]"]) >> search_term;
// Search every field we return
if (search_term.length() != 0)
for (const auto& svi : summary_vec)
search_paths.push_back(svi->resolved_path);
// We only allow ordering by a single column, we don't do sub-ordering;
// look for that single column
if (postvars.find("order[0][column]") != postvars.end())
*(postvars["order[0][column]"]) >> in_order_column_num;
// We can only sort by a column that makes sense
auto column_index = column_number_map.find(in_order_column_num);
if (column_index == column_number_map.end())
in_order_column_num = -1;
// What direction do we sort in
if (in_order_column_num >= 0 &&
postvars.find("order[0][dir]") != postvars.end()) {
auto order = postvars.find("order[0][dir]")->second->str();
if (order == "asc")
in_order_direction = 1;
else
in_order_direction = 0;
// Resolve the path, we only allow the first one
auto column_index_vec = column_index->second->getStringVec();
if (column_index_vec.size() >= 1) {
auto summary = TrackerElementSummary{column_index_vec[0]};
order_field = summary.resolved_path;
}
}
if (in_window_len > 500)
in_window_len = 500;
// Set the window elements for datatables
length_elem->set(in_window_len);
start_elem->set(in_window_start);
dt_draw_elem->set(in_dt_draw);
// Set up the datatables wrapper
wrapper_elem = std::make_shared<TrackerElementStringMap>();
transmit = wrapper_elem;
wrapper_elem->insert("draw", dt_draw_elem);
wrapper_elem->insert("data", output_devices_elem);
wrapper_elem->insert("recordsTotal", total_sz_elem);
wrapper_elem->insert("recordsFiltered", filtered_sz_elem);
// We transmit the wrapper elem
transmit = wrapper_elem;
}
} catch (const std::exception& e) {
stream << "Invalid request: " << e.what() << "\n";
return 400;
}
// Next vector we do work on
auto next_work_vec = std::make_shared<TrackerElementVector>();
// Copy the entire vector list, under lock, to the next work vector; this makes it an independent copy
// which is protected from the main vector being grown/shrank. While we're in there, log the total
// size of the original vector for windowed ops.
{
local_locker l(&mutex);
next_work_vec->set(device_list->begin(), device_list->end());
total_sz_elem->set(next_work_vec->size());
}
// If we have a time filter, apply that first, it's the fastest.
if (timestamp_min > 0) {
auto worker =
DevicetrackerViewFunctionWorker([timestamp_min] (std::shared_ptr<kis_tracked_device_base> dev) -> bool {
if (dev->get_last_time() < timestamp_min)
return false;
return true;
});
// Do the work and copy the vector
auto ts_vec = doDeviceWork(worker, next_work_vec);
next_work_vec->set(ts_vec->begin(), ts_vec->end());
}
// Apply a string filter
if (search_term.length() > 0 && search_paths.size() > 0) {
auto worker =
DevicetrackerViewStringmatchWorker(search_term, search_paths);
auto s_vec = doDeviceWork(worker, next_work_vec);
next_work_vec->set(s_vec->begin(), s_vec->end());
}
// Apply a regex filter
if (regex != nullptr) {
try {
auto worker =
DevicetrackerViewRegexWorker(regex);
auto r_vec = doDeviceWork(worker, next_work_vec);
next_work_vec->set(r_vec->begin(), r_vec->end());
} catch (const std::exception& e) {
stream << "Invalid regex: " << e.what() << "\n";
return 400;
}
}
// Apply the filtered length
filtered_sz_elem->set(next_work_vec->size());
// Slice from the beginning of the list
if (in_window_start >= next_work_vec->size())
in_window_start = 0;
// Update the start
start_elem->set(in_window_start);
auto si = next_work_vec->begin() + in_window_start;
auto ei = next_work_vec->begin();
if (in_window_len + in_window_start >= next_work_vec->size() || in_window_len == 0)
ei = next_work_vec->end();
else
ei = ei + in_window_len;
// Update the end
length_elem->set(ei - si);
// Do a partial fast-sort
if (in_order_column_num >= 0 && order_field.size() > 0) {
kismet__partial_sort(si, ei, next_work_vec->end(),
[&](SharedTrackerElement a, SharedTrackerElement b) -> bool {
SharedTrackerElement fa;
SharedTrackerElement fb;
fa = GetTrackerElementPath(order_field, a);
fb = GetTrackerElementPath(order_field, b);
if (fa == nullptr)
return in_order_direction == 0;
if (fb == nullptr)
return in_order_direction != 0;
if (in_order_direction == 0)
return FastSortTrackerElementLess(fa, fb);
return FastSortTrackerElementLess(fb, fa);
});
}
// Summarize into the output element
for (auto i = si; i != ei; ++i) {
auto simple = SharedTrackerElement{};
output_devices_elem->push_back(SummarizeSingleTrackerElement(*i, summary_vec, rename_map));
}
// If the transmit wasn't assigned to a wrapper...
if (transmit == nullptr)
transmit = output_devices_elem;
// Serialize
Globalreg::globalreg->entrytracker->Serialize(kishttpd::GetSuffix(uri), stream, transmit, rename_map);
// And done
return 200;
}