This commit is contained in:
j3d1 2016-01-23 04:25:55 +01:00
parent 693d8d42ff
commit d3b89e3b11
10 changed files with 150 additions and 156 deletions

View file

@ -15,7 +15,7 @@ $(BUILDDIR):
$(TARGET): $(OBJECTS) $(TARGET): $(OBJECTS)
$(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lpthread
$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp

View file

@ -277,6 +277,14 @@ void Packet::pull(bytes &arr, int &index, int &ret) {
ret |= arr[index++] & 0xFF; ret |= arr[index++] & 0xFF;
} }
byte Packet::getOpCode() const {
return opCode;
}
void Packet::setOpCode(byte opCode) {
this->opCode = opCode;
}
void Packet::pull(bytes &arr, int &index, dataset &ret) { void Packet::pull(bytes &arr, int &index, dataset &ret) {
pull(arr, index, ret.type); pull(arr, index, ret.type);
pull(arr, index, ret.len); pull(arr, index, ret.len);

View file

@ -20,7 +20,7 @@ class Packet
public: public:
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE}; enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
Packet(OpCode); Packet(OpCode);
void encode(bytes&); static void encode(bytes&);
bytes getBytes(); bytes getBytes();
void parse(bytes); void parse(bytes);
void printHeader(); void printHeader();
@ -41,6 +41,8 @@ public:
void setPayload(datasets payload); void setPayload(datasets payload);
short getTokenId() const; short getTokenId() const;
void setTokenId(short tokenId = 0); void setTokenId(short tokenId = 0);
byte getOpCode() const;
void setOpCode(byte opCode);
private: private:
bytes head = bytes(32); bytes head = bytes(32);

View file

@ -88,19 +88,9 @@ int printPacket(Packet p) {
} }
int Program::list() { int Program::list() {
std::cout << "List:\n";
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac());
p.setPayload( { });
bytes b = p.getBytes();
p.encode(b);
try { try {
sock->setHostIp(host.getIp()); std::cout << "List:\n";
sock->init(DST_PORT, SRC_PORT); discover([this](Packet a) {
sock->callback =
[this](Packet a) {
printHeader(a); printHeader(a);
if (options.flags & FLAG_HEX) { if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n"; std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
@ -110,11 +100,10 @@ int Program::list() {
sw.parse(d); sw.parse(d);
File f; File f;
f.write(sw.toString()); f.write(sw.toString());
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n"; sw.print();
} }
return 1; return 1;
}; });
sock->send(b);
io_service->run(); io_service->run();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
@ -146,83 +135,71 @@ int Program::sniff() {
int Program::encode(std::string s) { int Program::encode(std::string s) {
bytes d(s); bytes d(s);
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::NONE);
p.encode(d); p.encode(d);
std::cout << d << std::endl; std::cout << d << std::endl;
return 0; return 0;
} }
int Program::setProperty() { int Program::setProperty() {
return 0;
}
int Program::getProperty() {
try { try {
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
std::cout << "List:\n"; std::cout << "List:\n";
Packet p = Packet(Packet::DISCOVERY); discover(
p.setHostMac(host.getMac());
p.setPayload( { });
bytes b = p.getBytes();
p.encode(b);
sock->callback =
[this](Packet a) { [this](Packet a) {
datasets d =a.getPayload(); datasets d =a.getPayload();
Switch sw = Switch(); Switch sw = Switch();
sw.parse(d); sw.parse(d);
sw.print();
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
Packet p = Packet(Packet::GET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {SND_PING, 0, {}}}; datasets t = { {SND_PING, 0, {}}};
p.setPayload(t); get(a, t, [this](Packet a) {
bytes c = p.getBytes();
p.encode(c);
sock->callback =
[this](Packet a) {
datasets d =a.getPayload(); datasets d =a.getPayload();
Switch sw = Switch(); Switch sw = Switch();
sw.parse(d); sw.parse(d);
Packet p = Packet(Packet::SET); datasets t = { {REBOOT, 1, {0}}};
p.setSwitchMac(a.getSwitchMac()); set(a,t,
p.setTokenId(a.getTokenId());
p.setHostMac(host.getMac());
bytes n = options.user;
bytes w = options.password;
n.push_back('\0');
w.push_back('\0');
datasets t = {
{ LOGIN_USER, (short)(n.size()), n},
{ LOGIN_PASSWORD, (short)(w.size()), w},
{ REBOOT, 1, {0}}
};
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
sock->callback =
[this](Packet a) { [this](Packet a) {
std::cout << a.opCodeToString() << "\n"; std::cout << a.opCodeToString() << "\n";
printHeader(a); printHeader(a);
printPacket(a); printPacket(a);
return 0; return 0;
}; });
sock->send(c);
return 0; return 0;
}; });
return 0;
});
sock->send(c); io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
}
int Program::getProperty() {
try {
std::cout << "List:\n";
discover([this](Packet a) {
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
sw.print();
datasets t = { {SYSTEM_INFO, 0, {}}};
get(a, t, [this](Packet a) {
std::cout << a.opCodeToString() << "\n";
printHeader(a);
printPacket(a);
return 0; return 0;
}; });
sock->send(b); return 0;
});
io_service->run(); io_service->run();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
@ -242,9 +219,7 @@ int Program::restore() {
File f; File f;
Switch sw; Switch sw;
sw.parse(f.read()); sw.parse(f.read());
std::cout << "Devices:\n\t" << sw.settings.hostname << " (" sw.print();
<< sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: "
<< sw.settings.ip_addr << "\n";
return 1; return 1;
} }
@ -254,70 +229,27 @@ int Program::flash() {
} }
int Program::reboot() { int Program::reboot() {
std::cout << "List:\n";
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac());
p.setPayload( { });
bytes b = p.getBytes();
p.encode(b);
auto s = sock;
try { try {
sock->setHostIp(host.getIp()); discover([this](Packet a) {
sock->init(DST_PORT, SRC_PORT);
sock->callback =
[this](Packet a) {
auto s = sock;
datasets d =a.getPayload(); datasets d =a.getPayload();
Switch sw = Switch(); Switch sw = Switch();
sw.parse(d); sw.parse(d);
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n"; std::cout <<sw.settings.hostname<<"\t";
Packet p = Packet(Packet::GET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {SND_PING, 0, {}}}; datasets t = { {SND_PING, 0, {}}};
p.setPayload(t); get(a, t, [this](Packet a) {
bytes c = p.getBytes();
p.encode(c);
sock->callback =
[this](Packet a) {
auto s = sock;
datasets d =a.getPayload(); datasets d =a.getPayload();
Switch sw = Switch(); Switch sw = Switch();
sw.parse(d); sw.parse(d);
Packet p = Packet(Packet::SET); datasets t = { {REBOOT, 1, {0}}};
p.setSwitchMac(a.getSwitchMac()); set(a,t,[this](Packet a) {
p.setTokenId(a.getTokenId()); if( a.getOpCode()==Packet::CONFIRM)
p.setHostMac(host.getMac()); std::cout<< "rebooting now.\n";
bytes n = options.user;
bytes w = options.password;
n.push_back('\0');
w.push_back('\0');
datasets t = {
{ LOGIN_USER, (short)(n.size()), n},
{ LOGIN_PASSWORD, (short)(w.size()), w},
{ REBOOT, 1, {0}}
};
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
sock->callback =
[this](Packet a) {
std::cout << a.opCodeToString() << "\n";
printHeader(a);
printPacket(a);
return 0; return 0;
}; });
sock->send(c);
return 0; return 0;
}; });
sock->send(c);
return 0; return 0;
}; });
sock->send(b);
io_service->run(); io_service->run();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
@ -330,12 +262,46 @@ int Program::reset() {
return 0; return 0;
} }
int Program::ping(std::function<int(Packet)>) { int Program::discover(std::function<int(Packet)> c) {
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac());
p.setPayload( { });
sock->callback = c;
sock->send(p);
return 0;
}
int Program::get(Packet l, datasets t, std::function<int(Packet)> c) {
Packet p = Packet(Packet::GET);
p.setSwitchMac(l.getSwitchMac());
p.setHostMac(host.getMac());
p.setPayload(t);
sock->callback = c;
sock->send(p);
return 0;
}
int Program::set(Packet l, datasets t, std::function<int(Packet)> c) {
Packet p = Packet(Packet::SET);
p.setSwitchMac(l.getSwitchMac());
p.setTokenId(l.getTokenId());
p.setHostMac(host.getMac());
bytes n = options.user;
bytes w = options.password;
n.push_back('\0');
w.push_back('\0');
datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD,
(short) (w.size()), w } };
p.setPayload(ld + t);
sock->callback = c;
sock->send(p);
return 0; return 0;
} }
void Program::init() { void Program::init() {
if (options.interface.compare("") == 0) if (options.interface.compare("") == 0)
options.interface = host.getIface(); options.interface = host.getIface();
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
} }

View file

@ -19,7 +19,9 @@ private:
std::shared_ptr<boost::asio::io_service> io_service; std::shared_ptr<boost::asio::io_service> io_service;
std::shared_ptr<Socket> sock; std::shared_ptr<Socket> sock;
Host host = Host(); Host host = Host();
int ping(std::function<int(Packet)>); int get(Packet,datasets,std::function<int(Packet)>);
int set(Packet,datasets,std::function<int(Packet)>);
int discover(std::function<int(Packet)>);
public: public:
Program() { Program() {
io_service = std::make_shared<boost::asio::io_service>(); io_service = std::make_shared<boost::asio::io_service>();

View file

@ -67,7 +67,9 @@ void Socket::setHostIp(ipAddr ip) {
local_ip = ip; local_ip = ip;
} }
void Socket::send(bytes data) { void Socket::send(Packet p) {
bytes data = p.getBytes();
p.encode(data);
unsigned char * a = &data[0]; unsigned char * a = &data[0];
send_socket_.async_send_to(boost::asio::buffer(a, data.size()), send_socket_.async_send_to(boost::asio::buffer(a, data.size()),
broadcast_endpoint_, broadcast_endpoint_,

View file

@ -23,7 +23,7 @@ public:
virtual ~Socket() { virtual ~Socket() {
} }
void init(short, short); void init(short, short);
void send(bytes); void send(Packet);
void listen(); void listen();
void setHostIp(ipAddr); void setHostIp(ipAddr);
std::function<int(Packet)> callback = [](Packet a) { std::function<int(Packet)> callback = [](Packet a) {

View file

@ -19,6 +19,12 @@ int Switch::parse(datasets arr) {
return 0; return 0;
} }
int Switch::print() {
std::cout << "\t" << settings.hostname << " (" << device.type << ")\tMAC: "
<< device.mac << "\tIP: " << settings.ip_addr << "\n";
return 0;
}
int Switch::parse(dataset d) { int Switch::parse(dataset d) {
switch (d.type) { switch (d.type) {
case RCV_TYPE: case RCV_TYPE:
@ -63,4 +69,3 @@ int Switch::parse(dataset d) {
} }
return 0; return 0;
} }

View file

@ -65,6 +65,7 @@ public:
int parse(datasets); int parse(datasets);
int parse(dataset); int parse(dataset);
int parse(std::string); int parse(std::string);
int print();
std::string toString(); std::string toString();
struct { struct {

View file

@ -27,6 +27,14 @@ public:
push_back(b); push_back(b);
} }
} }
datasets operator+(const datasets &B) {
datasets AB;
AB.reserve(this->size() + B.size());
AB.insert(AB.end(), this->begin(), this->end());
AB.insert(AB.end(), B.begin(), B.end());
return AB;
}
}; };
#endif /* DATASETS_H_ */ #endif /* DATASETS_H_ */