trying out stuff

This commit is contained in:
j3d1 2016-02-23 12:24:49 +01:00
parent 49ab8a307e
commit a90401ddeb
9 changed files with 125 additions and 87 deletions

14
src/Filter.cpp Normal file
View file

@ -0,0 +1,14 @@
/*
* Filter.cpp
*
* Created on: 23.02.2016
* Author: jedi
*/
#include "Filter.h"
Filter::Filter() {
// TODO Auto-generated constructor stub
}

16
src/Filter.h Normal file
View file

@ -0,0 +1,16 @@
/*
* Filter.h
*
* Created on: 23.02.2016
* Author: jedi
*/
#ifndef FILTER_H_
#define FILTER_H_
class Filter {
public:
Filter();
};
#endif /* FILTER_H_ */

View file

@ -19,15 +19,6 @@
using namespace std;
Interactive::Interactive() {
// TODO Auto-generated constructor stub
}
Interactive::~Interactive() {
// TODO Auto-generated destructor stub
}
int Interactive::loop() {
string cmd;
vector<string> v;
@ -36,21 +27,37 @@ int Interactive::loop() {
//int argc;
Program p = Program();
p.init();
while (cmd.compare("quit")) {
while (1) {
cmd = readline("smrtlink> ");
add_history(cmd.c_str());
v = boost::program_options::split_unix(cmd);
if (!cmd.compare("quit") || !cmd.compare("q"))
return 0;
if (!cmd.empty()) {
add_history(cmd.c_str());
v = boost::program_options::split_unix(cmd);
//vc = vector<char const*>(v.size());
//std::transform(begin(v), end(v), begin(vc), [](std::string const &s) { return s.c_str(); });
//argv = &vc[0];
//argc = v.size();
//vc = vector<char const*>(v.size());
//std::transform(begin(v), end(v), begin(vc), [](std::string const &s) { return s.c_str(); });
//argv = &vc[0];
//argc = v.size();
p.run(v);
p.run(v);
}
}
return 0;
}
int Interactive::single(vector<string> v) {
Program p = Program();
p.init();
p.run(v);
return 0;
}

View file

@ -10,9 +10,12 @@
class Interactive {
public:
Interactive();
virtual ~Interactive();
Interactive() {
}
virtual ~Interactive() {
}
int loop();
int single(std::vector<std::string> v);
};
#endif /* INTERACTIVE_H_ */

View file

@ -34,14 +34,10 @@ int Program::run(vector<string> arg) {
case caseArg("reboot"):
if (!reboot())
return 0;
fprintf(stderr, "Not yet implemented.\n");
return 1;
break;
case caseArg("reset"):
if (!reset())
return 0;
fprintf(stderr, "Not yet implemented.\n");
return 1;
break;
case caseArg("save"):
if (!save())
@ -230,7 +226,7 @@ int Program::sniff() {
printPacket(p);
return 0;
};
s.listen();
s.receive();
io_service.run();
} catch (exception& e) {
cerr << "Exception: " << e.what() << "\n";
@ -441,6 +437,8 @@ int Program::set(Packet l, datasets t, function<int(Packet)> c) {
}
void Program::init() {
io_service = std::make_shared<boost::asio::io_service>();
sock = std::make_shared < Socket > (*io_service);
if (options.interface.compare("") == 0)
options.interface = host.getIface();

View file

@ -23,12 +23,12 @@ private:
int set(Packet,datasets,std::function<int(Packet)>);
int discover(std::function<int(Packet)>);
public:
Program() {
io_service = std::make_shared<boost::asio::io_service>();
sock = std::make_shared<Socket>(*io_service);
}
Program() {}
void init();
int run(std::vector<std::string>);
std::function<int()> callback = []() {
return 0;
};
int list();
int sniff();

View file

@ -13,17 +13,11 @@
#include "Host.h"
#include "Types.h"
#define SEND_F 1
#define RECEIVE_F 2
Socket::Socket(boost::asio::io_service& io_service) :
send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
}
//, resolver( io_service)
void Socket::init(short dst_port, short src_port) {
if (initialized == 3)
return;
if (options.flags.REVERSE) {
short p = dst_port;
dst_port = src_port;
@ -41,7 +35,15 @@ void Socket::init(short dst_port, short src_port) {
boost::asio::ip::address_v4::from_string("255.255.255.255"),
dst_port);
initialized = 3;
send_socket_.open(boost::asio::ip::udp::v4());
send_socket_.set_option(boost::asio::socket_base::broadcast(true));
send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_);
receive_socket_.open(boost::asio::ip::udp::v4());
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
receive_socket_.bind(wildcard_endpoint_);
}
void Socket::setHostIp(ipAddr ip) {
@ -49,12 +51,6 @@ void Socket::setHostIp(ipAddr ip) {
}
void Socket::send(Packet p) {
if (!send_socket_.is_open() || !(initialized&SEND_F)) {
send_socket_.open(boost::asio::ip::udp::v4());
send_socket_.set_option(boost::asio::socket_base::broadcast(true));
send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_);
}
bytes data = p.getBytes();
p.encode(data);
unsigned char * a = &data[0];
@ -62,30 +58,12 @@ void Socket::send(Packet p) {
broadcast_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_sent)
{
listen();
receive();
settimeout();
});
}
void Socket::listen() {
if (!receive_socket_.is_open() || !(initialized & RECEIVE_F)) {
receive_socket_.open(boost::asio::ip::udp::v4());
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
receive_socket_.set_option(
boost::asio::socket_base::reuse_address(true));
receive_socket_.bind(wildcard_endpoint_);
}
if (options.timeout != 0) {
timer.expires_from_now(
boost::posix_time::milliseconds(options.timeout));
timer.async_wait([this](const boost::system::error_code& error)
{
if (!error)
{
receive_socket_.close();
initialized&=~RECEIVE_F;
}
});
}
void Socket::receive() {
data.resize(MAX_LENGTH);
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
remote_endpoint_,
@ -104,7 +82,27 @@ void Socket::listen() {
if(!callback(p)) {
//TODO do something
}
listen();
receive();
settimeout();
}
});
}
void Socket::settimeout() {
if (options.timeout != 0) {
timer.expires_from_now(
boost::posix_time::milliseconds(options.timeout));
timer.async_wait(
[this](const boost::system::error_code& error)
{
if (!error)
{
receive_socket_.close();
receive_socket_.open(boost::asio::ip::udp::v4());
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
receive_socket_.bind(wildcard_endpoint_);
}
});
}
}

View file

@ -17,30 +17,34 @@
#define MAX_LENGTH 1024
typedef std::function<int(Packet)> Listener;
class Socket {
public:
Socket(boost::asio::io_service&);
virtual ~Socket() {
}
void init(short, short);
void send(Packet);
void listen();
void setHostIp(ipAddr);
std::function<int(Packet)> callback = [](Packet a) {
return 0;
};
Socket(boost::asio::io_service&);
virtual ~Socket() {
}
void init(short, short);
void send(Packet);
void setHostIp(ipAddr);
void listen();
Listener callback = [](Packet a) {
return 0;
};
void receive();
private:
boost::asio::ip::udp::socket send_socket_;
boost::asio::ip::udp::socket receive_socket_;
boost::asio::ip::udp::endpoint broadcast_endpoint_;
boost::asio::ip::udp::endpoint remote_endpoint_;
boost::asio::ip::udp::endpoint wildcard_endpoint_;
boost::asio::ip::udp::endpoint local_endpoint_;
boost::asio::deadline_timer timer;
bytes data = bytes(MAX_LENGTH);
ipAddr local_ip;
int initialized = 0;
void settimeout();
boost::asio::ip::udp::socket send_socket_;
boost::asio::ip::udp::socket receive_socket_;
boost::asio::ip::udp::endpoint broadcast_endpoint_;
boost::asio::ip::udp::endpoint remote_endpoint_;
boost::asio::ip::udp::endpoint wildcard_endpoint_;
boost::asio::ip::udp::endpoint local_endpoint_;
boost::asio::deadline_timer timer;
bytes data = bytes(MAX_LENGTH);
ipAddr local_ip;
int initialized = 0;
};

View file

@ -143,22 +143,20 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
Interactive p = Interactive();
if (options.flags.INTERACTIVE) {
if (optind < argc) {
cerr << "Command is ignored in interactive mode\n";
}
Interactive p = Interactive();
if (!p.loop())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
} else if (optind < argc) {
Program p = Program();
p.init();
vector<string> v;
while (optind < argc)
v.push_back(argv[optind++]);
p.run(v);
p.single(v);
}
exit(EXIT_FAILURE);
}