2009-03-22 23:26:42 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2009-03-22 23:26:42 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 03:10:45 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2009-03-22 23:26:42 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
// network
|
2011-10-22 03:10:45 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2011-11-10 01:45:18 +01:00
|
|
|
#include <cstdio>
|
2009-03-22 23:26:42 +01:00
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2009-03-22 23:26:42 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
#include "kismet_datasource.hpp"
|
|
|
|
#include "kismet_featureset.hpp"
|
|
|
|
|
2009-03-22 23:26:42 +01:00
|
|
|
#define MAX_TCP_BUFFER 4096 // maximum accepted TCP data block size
|
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
// If you change this also change the according kismet command length !
|
2009-03-22 23:26:42 +01:00
|
|
|
#define MAX_KISMET_LINE 1024 // maximum length of a kismet command (assumed)
|
2010-11-18 23:48:56 +01:00
|
|
|
#define KISMET_COMMAND "*NETWORK: \001%1024[^\001]\001 %1024s %d %lf %lf"
|
2009-03-22 23:26:42 +01:00
|
|
|
|
|
|
|
using boost::lexical_cast;
|
|
|
|
using boost::bad_lexical_cast;
|
|
|
|
|
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(kismet_datasource)
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-03-22 23:26:42 +01:00
|
|
|
using mapnik::coord2d;
|
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
using mapnik::datasource_exception;
|
|
|
|
|
|
|
|
boost::mutex knd_list_mutex;
|
|
|
|
std::list<kismet_network_data> knd_list;
|
|
|
|
const unsigned int queue_size = 20;
|
|
|
|
|
2012-03-09 07:31:43 +01:00
|
|
|
kismet_datasource::kismet_datasource(parameters const& params)
|
2011-11-14 04:37:50 +01:00
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
|
|
|
extent_initialized_(false),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
srs_("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"),
|
|
|
|
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
|
2009-03-22 23:26:42 +01:00
|
|
|
{
|
2012-03-09 07:31:43 +01:00
|
|
|
boost::optional<std::string> host = params.get<std::string>("host");
|
2011-10-22 03:10:45 +02:00
|
|
|
if (host)
|
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
host_ = *host;
|
2011-10-22 03:10:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
throw datasource_exception("Kismet Plugin: missing <host> parameter");
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2012-03-09 07:31:43 +01:00
|
|
|
boost::optional<unsigned int> port = params.get<unsigned int>("port", 2501);
|
2011-10-22 03:10:45 +02:00
|
|
|
if (port)
|
|
|
|
{
|
|
|
|
port_ = *port;
|
|
|
|
}
|
|
|
|
|
2012-03-09 07:31:43 +01:00
|
|
|
boost::optional<std::string> srs = params.get<std::string>("srs");
|
2011-10-22 03:10:45 +02:00
|
|
|
if (srs)
|
|
|
|
{
|
|
|
|
srs_ = *srs;
|
|
|
|
}
|
2010-10-24 08:34:18 +02:00
|
|
|
|
2012-03-09 07:31:43 +01:00
|
|
|
boost::optional<std::string> ext = params.get<std::string>("extent");
|
2011-10-22 03:10:45 +02:00
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
extent_initialized_ = extent_.from_string(*ext);
|
|
|
|
}
|
2010-11-18 23:48:56 +01:00
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
kismet_thread.reset(new boost::thread(boost::bind(&kismet_datasource::run, this, host_, port_)));
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
kismet_datasource::~kismet_datasource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string kismet_datasource::name()
|
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
return "kismet";
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
mapnik::datasource::datasource_t kismet_datasource::type() const
|
2009-03-22 23:26:42 +01:00
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
return type_;
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> kismet_datasource::envelope() const
|
2009-03-22 23:26:42 +01:00
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
return extent_;
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
boost::optional<mapnik::datasource::geometry_t> kismet_datasource::get_geometry_type() const
|
|
|
|
{
|
|
|
|
return boost::optional<mapnik::datasource::geometry_t>(mapnik::datasource::Point);
|
|
|
|
}
|
|
|
|
|
2009-03-22 23:26:42 +01:00
|
|
|
layer_descriptor kismet_datasource::get_descriptor() const
|
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
return desc_;
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr kismet_datasource::features(query const& q) const
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
// std::clog << "kismet_datasource::features()" << endl;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
// TODO: use box2d to filter bbox before adding to featureset_ptr
|
2011-10-22 03:10:45 +02:00
|
|
|
// mapnik::box2d<double> const& e = q.get_bbox();
|
2009-03-22 23:26:42 +01:00
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(knd_list_mutex);
|
2011-10-22 03:10:45 +02:00
|
|
|
return boost::make_shared<kismet_featureset>(knd_list,
|
|
|
|
srs_,
|
|
|
|
desc_.get_encoding());
|
2009-03-22 23:26:42 +01:00
|
|
|
|
|
|
|
// TODO: if illegal:
|
|
|
|
// return featureset_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr kismet_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
// std::clog << "kismet_datasource::features_at_point()" << endl;
|
2009-03-22 23:26:42 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
return featureset_ptr();
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
void kismet_datasource::run(const std::string& ip_host, const unsigned int port)
|
2009-03-22 23:26:42 +01:00
|
|
|
{
|
2010-11-18 23:48:56 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Kismet Plugin: enter run" << std::endl;
|
|
|
|
#endif
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
int sockfd, n;
|
|
|
|
struct sockaddr_in sock_addr;
|
|
|
|
struct in_addr inadr;
|
|
|
|
struct hostent* host;
|
|
|
|
char buffer[MAX_TCP_BUFFER]; // TCP data send from kismet_server
|
|
|
|
std::string command;
|
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
if (inet_aton(ip_host.c_str(), &inadr))
|
2010-11-18 23:48:56 +01:00
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
host = gethostbyaddr((char*)&inadr, sizeof(inadr), AF_INET);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2010-11-18 23:48:56 +01:00
|
|
|
else
|
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
host = gethostbyname(ip_host.c_str());
|
2010-11-18 23:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (host == NULL)
|
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
std::cerr << "Kismet Plugin: error while searching host" << std::endl;
|
2010-11-18 23:48:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_addr.sin_family = AF_INET;
|
|
|
|
sock_addr.sin_port = htons(port);
|
|
|
|
memcpy(&sock_addr.sin_addr, host->h_addr_list[0], sizeof(sock_addr.sin_addr));
|
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
2010-11-18 23:48:56 +01:00
|
|
|
{
|
|
|
|
std::cerr << "Kismet Plugin: error while creating socket" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
if (connect(sockfd, (struct sockaddr*) &sock_addr, sizeof(sock_addr)))
|
2010-11-18 23:48:56 +01:00
|
|
|
{
|
|
|
|
std::cerr << "KISMET Plugin: Error while connecting" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
command = "!1 ENABLE NETWORK ssid,bssid,wep,bestlat,bestlon\n";
|
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
if (write(sockfd, command.c_str(), command.length()) != (signed)command.length())
|
2010-11-18 23:48:56 +01:00
|
|
|
{
|
|
|
|
std::cerr << "KISMET Plugin: Error sending command to " << ip_host << std::endl;
|
2011-10-22 03:10:45 +02:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
close(sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ssid[MAX_KISMET_LINE] = {};
|
|
|
|
char bssid[MAX_KISMET_LINE] = {};
|
|
|
|
double bestlat = 0;
|
|
|
|
double bestlon = 0;
|
|
|
|
int crypt = crypt_none;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
// BUG: if kismet_server is active sending after mapnik was killed and then restarted the
|
2010-11-18 23:48:56 +01:00
|
|
|
// assert is called. Needs to be analyzed!
|
|
|
|
while ((n = read(sockfd, buffer, sizeof(buffer))) > 0)
|
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
assert(n < MAX_TCP_BUFFER);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
buffer[n] = '\0';
|
2011-10-22 03:10:45 +02:00
|
|
|
std::string bufferObj(buffer); // TCP data send from kismet_server as STL string
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Kismet Plugin: buffer_obj=" << bufferObj << "[END]" << std::endl;
|
|
|
|
#endif
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
std::string::size_type found = 0;
|
|
|
|
std::string::size_type search_start = 0;
|
|
|
|
std::string kismet_line; // contains a line from kismet_server
|
2011-11-14 04:37:50 +01:00
|
|
|
do
|
2010-11-18 23:48:56 +01:00
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
found = bufferObj.find('\n', search_start);
|
2010-11-18 23:48:56 +01:00
|
|
|
if (found != std::string::npos)
|
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
kismet_line.assign(bufferObj, search_start, found - search_start);
|
2010-11-18 23:48:56 +01:00
|
|
|
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Kismet Plugin: line=" << kismet_line << " [ENDL]" << std::endl;
|
|
|
|
#endif
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
int param_number = 5; // the number of parameters to parse
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
// Attention: string length specified to the constant!
|
2011-10-22 03:10:45 +02:00
|
|
|
if (sscanf (kismet_line.c_str(),
|
2010-11-18 23:48:56 +01:00
|
|
|
KISMET_COMMAND,
|
|
|
|
ssid,
|
|
|
|
bssid,
|
|
|
|
&crypt,
|
|
|
|
&bestlat,
|
|
|
|
&bestlon) == param_number)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Kismet Plugin: ssid=" << ssid << ", bssid=" << bssid
|
2011-10-22 03:10:45 +02:00
|
|
|
<< ", crypt=" << crypt << ", bestlat=" << bestlat << ", bestlon=" << bestlon << std::endl;
|
2010-11-18 23:48:56 +01:00
|
|
|
#endif
|
2009-03-22 23:26:42 +01:00
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
kismet_network_data knd(ssid, bssid, bestlat, bestlon, crypt);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
boost::mutex::scoped_lock lock(knd_list_mutex);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
// the queue only grows to a max size
|
|
|
|
if (knd_list.size () >= queue_size)
|
|
|
|
{
|
2011-10-22 03:10:45 +02:00
|
|
|
knd_list.pop_front();
|
2010-11-18 23:48:56 +01:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 03:10:45 +02:00
|
|
|
knd_list.push_back(knd);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2010-11-18 23:48:56 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing if not matched!
|
|
|
|
}
|
|
|
|
|
2011-11-14 04:37:50 +01:00
|
|
|
search_start = found + 1;
|
2010-11-18 23:48:56 +01:00
|
|
|
}
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
2010-11-18 23:48:56 +01:00
|
|
|
while (found != std::string::npos);
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
std::cerr << "Kismet Plugin: error while reading from socket" << std::endl;
|
|
|
|
}
|
2009-03-22 23:26:42 +01:00
|
|
|
|
2010-11-18 23:48:56 +01:00
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Kismet Plugin: exit run" << std::endl;
|
|
|
|
#endif
|
2009-03-22 23:26:42 +01:00
|
|
|
}
|
2010-11-18 23:48:56 +01:00
|
|
|
|