From d3b89e3b11b795f5cba9eeb7e20891a9adf60929 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 04:25:55 +0100 Subject: [PATCH] clean up --- Makefile | 2 +- src/Packet.cpp | 8 ++ src/Packet.h | 4 +- src/Program.cpp | 266 +++++++++++++++++++++--------------------------- src/Program.h | 4 +- src/Socket.cpp | 4 +- src/Socket.h | 2 +- src/Switch.cpp | 7 +- src/Switch.h | 1 + src/datasets.h | 8 ++ 10 files changed, 150 insertions(+), 156 deletions(-) diff --git a/Makefile b/Makefile index 2404b58..5743050 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ $(BUILDDIR): $(TARGET): $(OBJECTS) - $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system + $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lpthread $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp diff --git a/src/Packet.cpp b/src/Packet.cpp index ce3ad13..0e61bf7 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -277,6 +277,14 @@ void Packet::pull(bytes &arr, int &index, int &ret) { 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) { pull(arr, index, ret.type); pull(arr, index, ret.len); diff --git a/src/Packet.h b/src/Packet.h index 327e713..2e04a2d 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -20,7 +20,7 @@ class Packet public: enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE}; Packet(OpCode); - void encode(bytes&); + static void encode(bytes&); bytes getBytes(); void parse(bytes); void printHeader(); @@ -41,6 +41,8 @@ public: void setPayload(datasets payload); short getTokenId() const; void setTokenId(short tokenId = 0); + byte getOpCode() const; + void setOpCode(byte opCode); private: bytes head = bytes(32); diff --git a/src/Program.cpp b/src/Program.cpp index d47fdf7..24bf99a 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -88,33 +88,22 @@ int printPacket(Packet p) { } 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 { - sock->setHostIp(host.getIp()); - sock->init(DST_PORT, SRC_PORT); - sock->callback = - [this](Packet a) { - printHeader(a); - if (options.flags & FLAG_HEX) { - std::cout <<"Received Payload:\n"<send(b); + std::cout << "List:\n"; + discover([this](Packet a) { + printHeader(a); + if (options.flags & FLAG_HEX) { + std::cout <<"Received Payload:\n"<run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; @@ -146,83 +135,71 @@ int Program::sniff() { int Program::encode(std::string s) { bytes d(s); - Packet p = Packet(Packet::DISCOVERY); + Packet p = Packet(Packet::NONE); p.encode(d); std::cout << d << std::endl; return 0; } int Program::setProperty() { - return 0; + try { + + std::cout << "List:\n"; + discover( + [this](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + sw.print(); + + datasets t = { {SND_PING, 0, {}}}; + get(a, t, [this](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + + datasets t = { {REBOOT, 1, {0}}}; + set(a,t, + + [this](Packet a) { + std::cout << a.opCodeToString() << "\n"; + printHeader(a); + printPacket(a); + return 0; + }); + return 0; + }); + return 0; + }); + + io_service->run(); + } catch (std::exception& e) { + std::cerr << "Exception: " << e.what() << "\n"; + } + return 1; } int Program::getProperty() { try { - sock->setHostIp(host.getIp()); - sock->init(DST_PORT, SRC_PORT); std::cout << "List:\n"; - Packet p = Packet(Packet::DISCOVERY); - p.setHostMac(host.getMac()); - p.setPayload( { }); - bytes b = p.getBytes(); - p.encode(b); - sock->callback = - [this](Packet a) { + discover([this](Packet a) { - datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + sw.print(); - std::cout <<"\t"<callback = - [this](Packet a) { - - datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); - - Packet p = Packet(Packet::SET); - p.setSwitchMac(a.getSwitchMac()); - 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) { - std::cout << a.opCodeToString() << "\n"; - printHeader(a); - printPacket(a); - return 0; - }; - sock->send(c); + 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(c); - return 0; - }; - sock->send(b); io_service->run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; @@ -242,9 +219,7 @@ int Program::restore() { File f; Switch sw; sw.parse(f.read()); - std::cout << "Devices:\n\t" << sw.settings.hostname << " (" - << sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: " - << sw.settings.ip_addr << "\n"; + sw.print(); return 1; } @@ -254,70 +229,27 @@ int Program::flash() { } 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 { - sock->setHostIp(host.getIp()); - sock->init(DST_PORT, SRC_PORT); - sock->callback = - [this](Packet a) { - auto s = sock; - datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); - std::cout <<"\t"<callback = - [this](Packet a) { - auto s = sock; + discover([this](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + std::cout <callback = - [this](Packet a) { - std::cout << a.opCodeToString() << "\n"; - printHeader(a); - printPacket(a); - return 0; - }; - sock->send(c); + datasets t = { {REBOOT, 1, {0}}}; + set(a,t,[this](Packet a) { + if( a.getOpCode()==Packet::CONFIRM) + std::cout<< "rebooting now.\n"; + return 0; + }); return 0; - }; - - sock->send(c); - return 0; - }; - sock->send(b); + }); + return 0; + }); io_service->run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; @@ -330,12 +262,46 @@ int Program::reset() { return 0; } -int Program::ping(std::function) { +int Program::discover(std::function 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 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 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; } void Program::init() { if (options.interface.compare("") == 0) options.interface = host.getIface(); + + sock->setHostIp(host.getIp()); + sock->init(DST_PORT, SRC_PORT); } diff --git a/src/Program.h b/src/Program.h index 1bd97da..fcb8e15 100644 --- a/src/Program.h +++ b/src/Program.h @@ -19,7 +19,9 @@ private: std::shared_ptr io_service; std::shared_ptr sock; Host host = Host(); - int ping(std::function); + int get(Packet,datasets,std::function); + int set(Packet,datasets,std::function); + int discover(std::function); public: Program() { io_service = std::make_shared(); diff --git a/src/Socket.cpp b/src/Socket.cpp index 191971e..0df5869 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -67,7 +67,9 @@ void Socket::setHostIp(ipAddr 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]; send_socket_.async_send_to(boost::asio::buffer(a, data.size()), broadcast_endpoint_, diff --git a/src/Socket.h b/src/Socket.h index 6bda28a..7abce0d 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -23,7 +23,7 @@ public: virtual ~Socket() { } void init(short, short); - void send(bytes); + void send(Packet); void listen(); void setHostIp(ipAddr); std::function callback = [](Packet a) { diff --git a/src/Switch.cpp b/src/Switch.cpp index 89419a8..0d6966e 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -19,6 +19,12 @@ int Switch::parse(datasets arr) { 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) { switch (d.type) { case RCV_TYPE: @@ -63,4 +69,3 @@ int Switch::parse(dataset d) { } return 0; } - diff --git a/src/Switch.h b/src/Switch.h index 88a3351..1b40071 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -65,6 +65,7 @@ public: int parse(datasets); int parse(dataset); int parse(std::string); + int print(); std::string toString(); struct { diff --git a/src/datasets.h b/src/datasets.h index bc9ac83..7b08f4f 100644 --- a/src/datasets.h +++ b/src/datasets.h @@ -27,6 +27,14 @@ public: 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_ */