forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpstracker.cc
287 lines (215 loc) · 8.48 KB
/
gpstracker.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
/*
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 "globalregistry.h"
#include "kis_net_microhttpd.h"
#include "messagebus.h"
#include "gpstracker.h"
#include "kis_gps.h"
#include "configfile.h"
#include "gpsserial2.h"
#include "gpsgpsd2.h"
#include "gpsfake.h"
#include "gpsweb.h"
GpsTracker::GpsTracker(GlobalRegistry *in_globalreg) :
Kis_Net_Httpd_CPPStream_Handler(in_globalreg) {
tracked_uuid_addition_id =
entrytracker->RegisterField("kismet.common.location.gps_uuid", TrackerUuid,
"UUID of GPS reporting location");
globalreg = in_globalreg;
// Register the gps component
_PCM(PACK_COMP_GPS) =
globalreg->packetchain->RegisterPacketComponent("gps");
// Register the packet chain hook
globalreg->packetchain->RegisterHandler(&kis_gpspack_hook, this,
CHAINPOS_POSTCAP, -100);
gps_prototypes.reset(new TrackerElement(TrackerVector));
gps_prototypes_vec = TrackerElementVector(gps_prototypes);
gps_instances.reset(new TrackerElement(TrackerVector));
gps_instances_vec = TrackerElementVector(gps_instances);
// Register the built-in GPS drivers
register_gps_builder(SharedGpsBuilder(new GPSSerialV2Builder(globalreg)));
register_gps_builder(SharedGpsBuilder(new GPSGpsdV2Builder(globalreg)));
register_gps_builder(SharedGpsBuilder(new GPSFakeBuilder(globalreg)));
register_gps_builder(SharedGpsBuilder(new GPSWebBuilder(globalreg)));
// Process any gps options in the config file
vector<string> gpsvec = globalreg->kismet_config->FetchOptVec("gps");
for (auto g : gpsvec) {
create_gps(g);
}
}
GpsTracker::~GpsTracker() {
local_eol_locker lock(&gpsmanager_mutex);
globalreg->RemoveGlobal("GPSTRACKER");
httpd->RemoveHandler(this);
globalreg->packetchain->RemoveHandler(&kis_gpspack_hook, CHAINPOS_POSTCAP);
}
void GpsTracker::register_gps_builder(SharedGpsBuilder in_builder) {
local_locker lock(&gpsmanager_mutex);
for (auto x : gps_prototypes_vec) {
SharedGpsBuilder gb = static_pointer_cast<KisGpsBuilder>(x);
if (gb->get_gps_class() == in_builder->get_gps_class()) {
_MSG("GPSTRACKER - tried to register a duplicate GPS driver for '" +
in_builder->get_gps_class() + "'", MSGFLAG_ERROR);
return;
}
}
gps_prototypes_vec.push_back(in_builder);
}
shared_ptr<KisGps> GpsTracker::create_gps(string in_definition) {
local_locker lock(&gpsmanager_mutex);
SharedGps gps;
SharedGpsBuilder builder;
size_t cpos = in_definition.find(":");
string types;
// Extract the type string
if (cpos == string::npos) {
types = in_definition;
} else {
types = in_definition.substr(0, cpos);
}
// Find a driver
for (auto p : gps_prototypes_vec) {
SharedGpsBuilder optbuilder = static_pointer_cast<KisGpsBuilder>(p);
if (optbuilder->get_gps_class() == types) {
builder = optbuilder;
break;
}
}
// Didn't find a builder...
if (builder == NULL) {
_MSG("GPSTRACKER - Failed to find driver for gps type '" + types + "'",
MSGFLAG_ERROR);
return NULL;
}
// If it's a singleton make sure we don't have something built already
if (builder->get_singleton()) {
for (auto d : gps_instances_vec) {
SharedGps igps = static_pointer_cast<KisGps>(d);
if (igps->get_gps_prototype()->get_gps_class() == types) {
_MSG("GPSTRACKER - Already defined a GPS of type '" + types + "', this "
"GPS driver cannot be defined multiple times.", MSGFLAG_ERROR);
return NULL;
}
}
}
// Fetch an instance
gps = builder->build_gps(builder);
// Open it
if (!gps->open_gps(in_definition)) {
_MSG("GPSTRACKER - Failed to open GPS '" + gps->get_gps_name() + "'", MSGFLAG_ERROR);
return NULL;
}
// Add it to the running GPS list
gps_instances_vec.push_back(gps);
// Sort running GPS by priority
sort(gps_instances_vec.begin(), gps_instances_vec.end(),
[](const SharedTrackerElement a, const SharedTrackerElement b) -> bool {
SharedGps ga = static_pointer_cast<KisGps>(a);
SharedGps gb = static_pointer_cast<KisGps>(b);
return ga->get_gps_priority() < gb->get_gps_priority();
});
return gps;
}
kis_gps_packinfo *GpsTracker::get_best_location() {
local_locker lock(&gpsmanager_mutex);
// Iterate
for (auto d : gps_instances_vec) {
SharedGps gps = static_pointer_cast<KisGps>(d);
if (gps->get_gps_data_only())
continue;
if (gps->get_location_valid()) {
kis_gps_packinfo *pi = new kis_gps_packinfo(gps->get_location());
pi->gpsuuid = gps->get_gps_uuid();
pi->gpsname = gps->get_gps_name();
return pi;
}
}
return NULL;
}
int GpsTracker::kis_gpspack_hook(CHAINCALL_PARMS) {
// We're an 'external user' of GpsTracker despite being inside it,
// so don't do thread locking - that's up to GpsTracker internals
GpsTracker *gpstracker = (GpsTracker *) auxdata;
// Don't override if this packet already has a location, which could
// come from a drone or from a PPI file
if (in_pack->fetch(_PCM(PACK_COMP_GPS)) != NULL)
return 1;
kis_gps_packinfo *gpsloc = gpstracker->get_best_location();
if (gpsloc == NULL)
return 0;
// Insert into chain; we were given a new location
in_pack->insert(_PCM(PACK_COMP_GPS), gpsloc);
return 1;
}
bool GpsTracker::Httpd_VerifyPath(const char *path, const char *method) {
if (strcmp(method, "GET") != 0)
return false;
string stripped = Httpd_StripSuffix(path);
if (!Httpd_CanSerialize(path))
return false;
if (stripped == "/gps/drivers")
return true;
if (stripped == "/gps/all_gps")
return true;
if (stripped == "/gps/location")
return true;
return false;
}
void GpsTracker::Httpd_CreateStreamResponse(
Kis_Net_Httpd *httpd __attribute__((unused)),
Kis_Net_Httpd_Connection *connection __attribute__((unused)),
const char *path, const char *method,
const char *upload_data __attribute__((unused)),
size_t *upload_data_size __attribute__((unused)),
std::stringstream &stream) {
local_locker lock(&gpsmanager_mutex);
if (strcmp(method, "GET") != 0) {
return;
}
string stripped = Httpd_StripSuffix(path);
if (stripped == "/gps/drivers") {
entrytracker->Serialize(httpd->GetSuffix(path), stream, gps_prototypes, NULL);
return;
}
if (stripped == "/gps/all_gps") {
entrytracker->Serialize(httpd->GetSuffix(path), stream, gps_instances, NULL);
return;
}
if (stripped == "/gps/location") {
kis_gps_packinfo *pi = get_best_location();
shared_ptr<kis_tracked_location_triplet> loctrip(new kis_tracked_location_triplet(globalreg, 0));
SharedTrackerElement ue(new TrackerElement(TrackerUuid, tracked_uuid_addition_id));
loctrip->add_map(ue);
if (pi != NULL) {
ue->set(pi->gpsuuid);
loctrip->set_lat(pi->lat);
loctrip->set_lon(pi->lon);
loctrip->set_alt(pi->alt);
loctrip->set_speed(pi->speed);
loctrip->set_heading(pi->heading);
loctrip->set_fix(pi->fix);
loctrip->set_valid(pi->fix >= 2);
loctrip->set_time_sec(pi->tv.tv_sec);
loctrip->set_time_usec(pi->tv.tv_usec);
delete(pi);
} else {
loctrip->set_valid(false);
}
entrytracker->Serialize(httpd->GetSuffix(path), stream, loctrip, NULL);
return;
}
return;
}