trying out stuff
This commit is contained in:
parent
49ab8a307e
commit
a90401ddeb
9 changed files with 125 additions and 87 deletions
14
src/Filter.cpp
Normal file
14
src/Filter.cpp
Normal 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
16
src/Filter.h
Normal 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_ */
|
||||||
|
|
@ -19,15 +19,6 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Interactive::Interactive() {
|
|
||||||
// TODO Auto-generated constructor stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Interactive::~Interactive() {
|
|
||||||
// TODO Auto-generated destructor stub
|
|
||||||
}
|
|
||||||
|
|
||||||
int Interactive::loop() {
|
int Interactive::loop() {
|
||||||
string cmd;
|
string cmd;
|
||||||
vector<string> v;
|
vector<string> v;
|
||||||
|
|
@ -36,21 +27,37 @@ int Interactive::loop() {
|
||||||
//int argc;
|
//int argc;
|
||||||
|
|
||||||
Program p = Program();
|
Program p = Program();
|
||||||
|
|
||||||
p.init();
|
p.init();
|
||||||
|
|
||||||
while (cmd.compare("quit")) {
|
while (1) {
|
||||||
cmd = readline("smrtlink> ");
|
cmd = readline("smrtlink> ");
|
||||||
add_history(cmd.c_str());
|
if (!cmd.compare("quit") || !cmd.compare("q"))
|
||||||
v = boost::program_options::split_unix(cmd);
|
return 0;
|
||||||
|
if (!cmd.empty()) {
|
||||||
|
add_history(cmd.c_str());
|
||||||
|
v = boost::program_options::split_unix(cmd);
|
||||||
|
|
||||||
//vc = vector<char const*>(v.size());
|
//vc = vector<char const*>(v.size());
|
||||||
//std::transform(begin(v), end(v), begin(vc), [](std::string const &s) { return s.c_str(); });
|
//std::transform(begin(v), end(v), begin(vc), [](std::string const &s) { return s.c_str(); });
|
||||||
//argv = &vc[0];
|
//argv = &vc[0];
|
||||||
//argc = v.size();
|
//argc = v.size();
|
||||||
|
p.run(v);
|
||||||
|
|
||||||
p.run(v);
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Interactive::single(vector<string> v) {
|
||||||
|
|
||||||
|
Program p = Program();
|
||||||
|
|
||||||
|
p.init();
|
||||||
|
|
||||||
|
p.run(v);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,12 @@
|
||||||
|
|
||||||
class Interactive {
|
class Interactive {
|
||||||
public:
|
public:
|
||||||
Interactive();
|
Interactive() {
|
||||||
virtual ~Interactive();
|
}
|
||||||
|
virtual ~Interactive() {
|
||||||
|
}
|
||||||
int loop();
|
int loop();
|
||||||
|
int single(std::vector<std::string> v);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* INTERACTIVE_H_ */
|
#endif /* INTERACTIVE_H_ */
|
||||||
|
|
|
||||||
|
|
@ -34,14 +34,10 @@ int Program::run(vector<string> arg) {
|
||||||
case caseArg("reboot"):
|
case caseArg("reboot"):
|
||||||
if (!reboot())
|
if (!reboot())
|
||||||
return 0;
|
return 0;
|
||||||
fprintf(stderr, "Not yet implemented.\n");
|
|
||||||
return 1;
|
|
||||||
break;
|
break;
|
||||||
case caseArg("reset"):
|
case caseArg("reset"):
|
||||||
if (!reset())
|
if (!reset())
|
||||||
return 0;
|
return 0;
|
||||||
fprintf(stderr, "Not yet implemented.\n");
|
|
||||||
return 1;
|
|
||||||
break;
|
break;
|
||||||
case caseArg("save"):
|
case caseArg("save"):
|
||||||
if (!save())
|
if (!save())
|
||||||
|
|
@ -230,7 +226,7 @@ int Program::sniff() {
|
||||||
printPacket(p);
|
printPacket(p);
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
s.listen();
|
s.receive();
|
||||||
io_service.run();
|
io_service.run();
|
||||||
} catch (exception& e) {
|
} catch (exception& e) {
|
||||||
cerr << "Exception: " << e.what() << "\n";
|
cerr << "Exception: " << e.what() << "\n";
|
||||||
|
|
@ -441,6 +437,8 @@ int Program::set(Packet l, datasets t, function<int(Packet)> c) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Program::init() {
|
void Program::init() {
|
||||||
|
io_service = std::make_shared<boost::asio::io_service>();
|
||||||
|
sock = std::make_shared < Socket > (*io_service);
|
||||||
if (options.interface.compare("") == 0)
|
if (options.interface.compare("") == 0)
|
||||||
options.interface = host.getIface();
|
options.interface = host.getIface();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,12 @@ private:
|
||||||
int set(Packet,datasets,std::function<int(Packet)>);
|
int set(Packet,datasets,std::function<int(Packet)>);
|
||||||
int discover(std::function<int(Packet)>);
|
int discover(std::function<int(Packet)>);
|
||||||
public:
|
public:
|
||||||
Program() {
|
Program() {}
|
||||||
io_service = std::make_shared<boost::asio::io_service>();
|
|
||||||
sock = std::make_shared<Socket>(*io_service);
|
|
||||||
}
|
|
||||||
void init();
|
void init();
|
||||||
int run(std::vector<std::string>);
|
int run(std::vector<std::string>);
|
||||||
|
std::function<int()> callback = []() {
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
int list();
|
int list();
|
||||||
int sniff();
|
int sniff();
|
||||||
|
|
|
||||||
|
|
@ -13,17 +13,11 @@
|
||||||
#include "Host.h"
|
#include "Host.h"
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
#define SEND_F 1
|
|
||||||
#define RECEIVE_F 2
|
|
||||||
|
|
||||||
Socket::Socket(boost::asio::io_service& io_service) :
|
Socket::Socket(boost::asio::io_service& io_service) :
|
||||||
send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
|
send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
|
||||||
}
|
}
|
||||||
//, resolver( io_service)
|
//, resolver( io_service)
|
||||||
void Socket::init(short dst_port, short src_port) {
|
void Socket::init(short dst_port, short src_port) {
|
||||||
if (initialized == 3)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (options.flags.REVERSE) {
|
if (options.flags.REVERSE) {
|
||||||
short p = dst_port;
|
short p = dst_port;
|
||||||
dst_port = src_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"),
|
boost::asio::ip::address_v4::from_string("255.255.255.255"),
|
||||||
dst_port);
|
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) {
|
void Socket::setHostIp(ipAddr ip) {
|
||||||
|
|
@ -49,12 +51,6 @@ void Socket::setHostIp(ipAddr ip) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send(Packet p) {
|
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();
|
bytes data = p.getBytes();
|
||||||
p.encode(data);
|
p.encode(data);
|
||||||
unsigned char * a = &data[0];
|
unsigned char * a = &data[0];
|
||||||
|
|
@ -62,30 +58,12 @@ void Socket::send(Packet p) {
|
||||||
broadcast_endpoint_,
|
broadcast_endpoint_,
|
||||||
[this](boost::system::error_code ec, std::size_t bytes_sent)
|
[this](boost::system::error_code ec, std::size_t bytes_sent)
|
||||||
{
|
{
|
||||||
listen();
|
receive();
|
||||||
|
settimeout();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::listen() {
|
void Socket::receive() {
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
data.resize(MAX_LENGTH);
|
data.resize(MAX_LENGTH);
|
||||||
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
|
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
|
||||||
remote_endpoint_,
|
remote_endpoint_,
|
||||||
|
|
@ -104,7 +82,27 @@ void Socket::listen() {
|
||||||
if(!callback(p)) {
|
if(!callback(p)) {
|
||||||
//TODO do something
|
//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_);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
44
src/Socket.h
44
src/Socket.h
|
|
@ -17,30 +17,34 @@
|
||||||
|
|
||||||
#define MAX_LENGTH 1024
|
#define MAX_LENGTH 1024
|
||||||
|
|
||||||
|
typedef std::function<int(Packet)> Listener;
|
||||||
|
|
||||||
class Socket {
|
class Socket {
|
||||||
public:
|
public:
|
||||||
Socket(boost::asio::io_service&);
|
Socket(boost::asio::io_service&);
|
||||||
virtual ~Socket() {
|
virtual ~Socket() {
|
||||||
}
|
}
|
||||||
void init(short, short);
|
void init(short, short);
|
||||||
void send(Packet);
|
void send(Packet);
|
||||||
void listen();
|
void setHostIp(ipAddr);
|
||||||
void setHostIp(ipAddr);
|
void listen();
|
||||||
std::function<int(Packet)> callback = [](Packet a) {
|
Listener callback = [](Packet a) {
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
void receive();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::asio::ip::udp::socket send_socket_;
|
void settimeout();
|
||||||
boost::asio::ip::udp::socket receive_socket_;
|
boost::asio::ip::udp::socket send_socket_;
|
||||||
boost::asio::ip::udp::endpoint broadcast_endpoint_;
|
boost::asio::ip::udp::socket receive_socket_;
|
||||||
boost::asio::ip::udp::endpoint remote_endpoint_;
|
boost::asio::ip::udp::endpoint broadcast_endpoint_;
|
||||||
boost::asio::ip::udp::endpoint wildcard_endpoint_;
|
boost::asio::ip::udp::endpoint remote_endpoint_;
|
||||||
boost::asio::ip::udp::endpoint local_endpoint_;
|
boost::asio::ip::udp::endpoint wildcard_endpoint_;
|
||||||
boost::asio::deadline_timer timer;
|
boost::asio::ip::udp::endpoint local_endpoint_;
|
||||||
bytes data = bytes(MAX_LENGTH);
|
boost::asio::deadline_timer timer;
|
||||||
ipAddr local_ip;
|
bytes data = bytes(MAX_LENGTH);
|
||||||
int initialized = 0;
|
ipAddr local_ip;
|
||||||
|
int initialized = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -143,22 +143,20 @@ int main(int argc, char *argv[]) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Interactive p = Interactive();
|
||||||
if (options.flags.INTERACTIVE) {
|
if (options.flags.INTERACTIVE) {
|
||||||
if (optind < argc) {
|
if (optind < argc) {
|
||||||
cerr << "Command is ignored in interactive mode\n";
|
cerr << "Command is ignored in interactive mode\n";
|
||||||
}
|
}
|
||||||
Interactive p = Interactive();
|
|
||||||
if (!p.loop())
|
if (!p.loop())
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
fprintf(stderr, "Not yet implemented.\n");
|
fprintf(stderr, "Not yet implemented.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else if (optind < argc) {
|
} else if (optind < argc) {
|
||||||
Program p = Program();
|
|
||||||
p.init();
|
|
||||||
vector<string> v;
|
vector<string> v;
|
||||||
while (optind < argc)
|
while (optind < argc)
|
||||||
v.push_back(argv[optind++]);
|
v.push_back(argv[optind++]);
|
||||||
p.run(v);
|
p.single(v);
|
||||||
}
|
}
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue