From 057a7b4344651e6bb16938e7109f8749025a0d22 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Wed, 20 Jan 2016 20:56:00 +0100 Subject: [PATCH 01/17] updates --- README.md | 12 +++-- src/File.cpp | 2 +- src/Host.cpp | 2 +- src/Lookup.h | 38 +++++++++------- src/Options.h | 66 --------------------------- src/Program.cpp | 36 ++++++++++----- src/Program.h | 1 + src/Socket.cpp | 2 +- src/Switch.cpp | 113 ++++++----------------------------------------- src/Switch.h | 36 ++++++++++++--- src/bytes.cpp | 19 ++++---- src/bytes.h | 10 ++--- src/smrtlink.cpp | 6 ++- src/table.h | 2 +- 14 files changed, 123 insertions(+), 222 deletions(-) delete mode 100644 src/Options.h diff --git a/README.md b/README.md index 4a32078..36f6447 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # smrtlink Command-Line Tool which might in the future be able to configure TP-Link Easy Smart Switches. -The usage is loosely based on the swconfig utility. +The usage is loosely based on the swconfig utility. -###dependencys +### dependencys libboost-filesystem-dev - libboost-system-dev \ No newline at end of file + libboost-system-dev + +### compatible devices ++ TL-SG105E ++ TL-SG108E ++ TL-SG1016DE (not tested) ++ TL-SG1024DE (not tested) diff --git a/src/File.cpp b/src/File.cpp index 654bca2..0e6e944 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -7,7 +7,7 @@ #include #include "File.h" -#include "Options.h" +#include "Constant.h" std::string File::read() { if (!fs::exists(home)) { diff --git a/src/Host.cpp b/src/Host.cpp index 83a06b4..1fbba26 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -21,7 +21,7 @@ #include #include -#include "Options.h" +#include "Constant.h" #include "Host.h" #include "Types.h" diff --git a/src/Lookup.h b/src/Lookup.h index dc07b7f..9a6bfef 100644 --- a/src/Lookup.h +++ b/src/Lookup.h @@ -16,30 +16,33 @@ static table rcv_lookup = { // { 6, table::DEC, "gateway" }, //+byte[4] { 7, table::STRING, "firmware_version" },//+ { 8, table::STRING, "hardware_version" },//+ - { 9, table::DEC, "dhcp" }, //+bool byte + { 9, table::BOOL, "dhcp_enabled" }, //+bool byte { 10, table::DEC, "ports" }, //+byte, maybe number of ports + { 2101, table::HEX, "port_vlan ????????" }, + { 4352, table::HEX, "igmp_snooping" }, //??? { 4096, table::HEX, "port_settings" }, //+per port { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool,second byte port id - { 8448, table::HEX, "port_vlan_1" }, //??? - { 8450, table::HEX, "port_vlan_2" }, //??? - { 8704, table::HEX, "vlan_enabled" }, //+bool byte + { 8448, table::BOOL, "port_vlan_enabled" }, //open page + { 8449, table::HEX, "port_vlan" }, //??? + { 8450, table::DEC, "port_vlan_max" }, //??? + { 8704, table::BOOL, "vlan_enabled" }, //+bool byte { 8705, table::HEX, "vlan" }, //+one set per vlan { 8706, table::HEX, "vlan_pvid" }, //+per port - { 8707, table::HEX, "vlan ?????" }, //???? + { 8707, table::DEC, "vlan ?????" }, //???? - { 12288, table::HEX, "qos_basic_enabled" }, //+bool = QoS Mod + { 12288, table::BOOL, "qos_basic_enabled" }, //+bool = QoS Mod { 12289, table::HEX, "qos_basic" }, //+per port ??? - { 12544, table::HEX, "bandwidth_control_1" }, //+per port ??? - { 12545, table::HEX, "bandwidth_control_2" }, //+per port ??? + { 12544, table::HEX, "bandwidth_control_ingress" }, //+per port ??? + { 12545, table::HEX, "bandwidth_control_egress" }, //+per port ??? { 12800, table::HEX, "storm_control" }, //+per port ??? { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??? { 16384, table::HEX, "port_statistics" }, //+per port ??? { 16896, table::HEX, "cable_test" }, //+per port ??? - { 17152, table::HEX, "loop_prevention" }, //+bool byte + { 17152, table::BOOL, "loop_prevention" }, //+bool byte //{0000," "}, }; @@ -54,7 +57,7 @@ static table snd_lookup = { { 513, table::STRING, "new_user" }, //string { 514, table::STRING, "login_password" }, //string { 515, table::STRING, "new_passwoord" }, //string - { 773, table::HEX, "reboot" }, //bool byte = save config + { 773, table::BOOL, "reboot" }, //bool byte = save config { 1280, table::ACTION, "reset" }, // { 1536, table::ACTION, "flash" }, //update firmware @@ -68,20 +71,21 @@ static table snd_lookup = { { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id - { 8449, table::HEX, "port_vlan ???" }, //??? - { 8448, table::HEX, "port_vlan" }, //open page - { 8704, table::HEX, "vlan_enabled" }, //??? get vlan / set status + { 8448, table::BOOL, "port_vlan_enabled" }, //open page + { 8449, table::HEX, "port_vlan" }, //??? + { 8704, table::BOOL, "vlan_enabled" }, //??? get vlan / set status { 8705, table::HEX, "vlan" }, // per vlan { 8706, table::HEX, "vlan_pvid" }, //per port - { 12288, table::HEX, "qos_basic_enabled" }, //bool = QoS Mod + { 12288, table::BOOL, "qos_basic_enabled" }, //bool = QoS Mod { 12289, table::HEX, "qos_basic" }, //per port - { 12544, table::HEX, "bandwidth_control_1" }, //per port - { 12545, table::HEX, "bandwidth_control_2" }, //per port + { 12544, table::HEX, "bandwidth_control_ingress" }, //per port + { 12545, table::HEX, "bandwidth_control_egress" }, //per port { 12800, table::HEX, "storm_control" }, //??? { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id?? { 16384, table::HEX, "port_statistics" }, //??? - { 17152, table::HEX, "loop_prevention" }, //bool byte + { 16896, table::HEX, "cable_test" }, //+per port ??? + { 17152, table::BOOL, "loop_prevention" }, //bool byte //{0000," "}, }; diff --git a/src/Options.h b/src/Options.h deleted file mode 100644 index 034ee3f..0000000 --- a/src/Options.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Options.h - * - * Created on: 11.09.2015 - * Author: jdi - */ - -#ifndef OPTIONS_H_ -#define OPTIONS_H_ - -#include "Types.h" - -#define VERSION "smrtlink (v1 Linux)\n" -#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\ - [-p password] \n\n" -#define HELP "\ - Option Summary:\n\ - -h --help This help text\n\ - -v --version Display version of this tool\n\ - -d --debug [n] Show debugging messages\n\ - -r switch ports to emulate switch while sniffing\n\ - -b --header Show header\n\ - -x --hex Display Packets as Hex String\n\ - -j --json Display Packets as JSON\n\ - -i --interface only use one Interface\n\ - -u --user <[password:]username>\n\ - -p --password \n\ - Login with Username and Password\n\ - -f --file choose a settings file\n\n\ - -t --timeout Timeout in milliseconds. Default: 180\n\ - -I --interactive Not yet implemented\n\ - -O --stdout Not yet implemented\n\ - -w --wait Not yet implemented: blocking until operation is completed\n\ - -s --permanent Not yet implemented: make changes immediately permanent\n\n\ - Command Summary:\n\ - help This help text\n\ - list list all connected switches\n\ - sniff [type:] []\n\ - capture and display all incoming or outgoing packets\n\ - depending on the --reverse option\n\ - encode use encoding algorithm on hex data separated by colon\n\ - get Not yet implemented\n\ - set Not yet implemented\n\ - get|save Not yet implemented: save config to file\n\ - set|restore Not yet implemented: restore onfig from file\n\ - flash Not yet implemented: replace firmware\n\ - reboot Not yet implemented\n\ - reset Not yet implemented\n\n\ - ### for questions please contact ###\n\n" -//TODO -/* - * Stdin - * socketmode - * - */ - -#define FLAG_HEX 1 -#define FLAG_REVERSE 2 -#define FLAG_HEADER 4 -#define FLAG_PERMANENT 8 -#define FLAG_WAIT 16 -#define FLAG_DEBUG 32 - -extern Options options; - -#endif /* OPTIONS_H_ */ diff --git a/src/Program.cpp b/src/Program.cpp index 70b7e74..f939b5f 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -7,7 +7,7 @@ #include #include -#include "Options.h" +#include "Constant.h" #include "Program.h" #include "File.h" #include "Host.h" @@ -44,10 +44,22 @@ int printPacket(Packet p) { std::cout << "+\t" << s->name << " = " << &d.value[0] << "\n"; break; + case table::BOOL: + std::cout << "+\t" << s->name << " = " << (d.value[0]?"YES":"NO") + << "\n"; + break; case table::HEX: std::cout << "+\t" << s->name << " = " << d.value << "\n"; break; + case table::DEC: + std::cout << "+\t" << s->name << " = "; + if (d.value.size() > 0) + std::cout << std::dec << (unsigned) d.value[0]; + for (unsigned i = 1; i < d.value.size(); i++) + std::cout << std::dec << "." << (unsigned) d.value[i]; + std::cout << "\n"; + break; case table::ACTION: std::cout << "Error:" << s->name << " is marked as 'action' but carries payload." @@ -56,6 +68,7 @@ int printPacket(Packet p) { default: std::cout << "+\t" << s->name << " = " << d.value << "\n"; + break; } } else { //empty std::cout << std::dec << ">\t" << s->name << "\n"; @@ -149,17 +162,13 @@ int Program::getProperty() { p.setPayload( { }); bytes b = p.getBytes(); p.encode(b); - std::cout << "count-x:" << sock.use_count() << "\n"; auto s = sock; try { sock->setHostIp(host.getIp()); sock->init(DST_PORT, SRC_PORT); - - std::cout << "count-y:" << sock.use_count() << "\n"; sock->callback = [this](Packet a) { auto s = sock; - std::cout<<"count-z:"<send(c); - - std::cout<<"count-a:"<send(b); @@ -245,6 +254,11 @@ int Program::reset() { return 0; } +int Program::ping(std::function){ + + return 0; +} + void Program::init() { if (options.interface.compare("") == 0) options.interface = host.getIface(); diff --git a/src/Program.h b/src/Program.h index 1df58ec..1bd97da 100644 --- a/src/Program.h +++ b/src/Program.h @@ -19,6 +19,7 @@ private: std::shared_ptr io_service; std::shared_ptr sock; Host host = Host(); + int ping(std::function); public: Program() { io_service = std::make_shared(); diff --git a/src/Socket.cpp b/src/Socket.cpp index 7ef01d4..942f563 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -9,7 +9,7 @@ #include #include "Socket.h" #include "Packet.h" -#include "Options.h" +#include "Constant.h" #include "Host.h" #include "Types.h" diff --git a/src/Switch.cpp b/src/Switch.cpp index 00d1a55..2fe8980 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -9,8 +9,7 @@ #include "Types.h" #include "Switch.h" #include "Lookup.h" -#include "jsonNode.h" -#include "Options.h" +#include "Constant.h" #include "table.h" int Switch::parse(datasets arr) { @@ -22,129 +21,45 @@ int Switch::parse(datasets arr) { int Switch::parse(dataset d) { auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup; + std::string id = lookup.exists(d.type)?lookup[d.type]:"unknown"; if (d.type == lookup["type"]) { device.type = d.value; } - if (d.type == lookup["mac"]) { + else if (id == "mac") { device.mac = d.value; } - if (d.type == lookup["firmware_version"]) { + else if (d.type == lookup["firmware_version"]) { device.firmware_version = d.value; } - if (d.type == lookup["hardware_version"]) { + else if (id == "hardware_version") { device.hardware_version = d.value; } - if (d.type == lookup["ports"]) { + else if (id == "ports") { device.ports = d.value[0]; } - if (d.type == lookup["hostname"]) { + else if (id == "hostname") { settings.hostname = d.value; } - if (d.type == lookup["ip_addr"]) { + else if (id == "ip_addr") { settings.ip_addr = d.value; } - if (d.type == lookup["ip_mask"]) { + else if (id == "ip_mask") { settings.ip_mask = d.value; } - if (d.type == lookup["gateway"]) { + else if (id == "gateway") { settings.gateway = d.value; } - if (d.type == lookup["dhcp"]) { + else if (id == "dhcp_enabled") { settings.dhcp = d.value[0]; } - if (d.type == lookup["loop_prevention"]) { + else if (id == "loop_prevention") { settings.loop_prevention = d.value[0]; } - if (d.type == lookup["qos_basic_enabled"]) { + else if (id == "qos_basic_enabled") { settings.qos_enabled = d.value[0]; } - if (d.type == lookup["vlan_enabled"]) { + else if (id == "vlan_enabled") { settings.vlan_enabled = d.value[0]; } return 0; } - -int Switch::parse(std::string str) { - - if (json.Parse(str.c_str()).HasParseError()) - return 1; - if (options.flags & FLAG_DEBUG) - std::cout << "\nParsing to document succeeded.\n"; - - if (json.IsObject()) { - if (json.HasMember("hostname")) - settings.hostname = json["hostname"].GetString(); - if (json.HasMember("type")) - device.type = json["type"].GetString(); - if (json.HasMember("hardware_version")) - device.hardware_version = json["hardware_version"].GetString(); - if (json.HasMember("firmware_version")) - device.hardware_version = json["firmware_version"].GetString(); - if (json.HasMember("ports") && json["ports"].IsArray()) { - const rapidjson::Value& a = json["ports"]; - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - if (a[i].IsObject()) { - port p; - if (a[i].HasMember("id") && a[i]["id"].IsInt()) { - p.id = a[i]["id"].GetInt(); - std::cout << a[i]["id"].GetInt() << "\n"; - } - ports.push_back(p); - } - } - if (json.HasMember("vlans") && json["vlans"].IsArray()) { - const rapidjson::Value& a = json["vlans"]; - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - if (a[i].IsObject()) { - vlan v; - if (a[i].HasMember("name") && a[i]["name"].IsString()) { - v.name = a[i]["name"].GetString(); - std::cout << a[i]["name"].GetString() << "\n"; - } - vlans.push_back(v); - } - } - - } - - /* - - - { - const rapidjson::Value& a = json["a"]; - assert(a.IsArray()); - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - printf("a[%d] = %d\n", i, a[i].GetInt()); - - int y = a[0].GetInt(); - (void) y; - - */ - return 0; -} - -std::string Switch::toString() { - - if (!json.IsObject()) { - json.SetObject(); - } - rapidjson::Document::AllocatorType& allocator = json.GetAllocator(); - - json.AddMember("hostname", jsonNode(settings.hostname, json), allocator); - json.AddMember("ip", jsonNode(settings.ip_addr, json), allocator); - json.AddMember("netmask", jsonNode(settings.ip_mask, json), allocator); - json.AddMember("gateway", jsonNode(settings.gateway, json), allocator); - json.AddMember("type", jsonNode(device.type, json), allocator); - json.AddMember("hardware_version", jsonNode(device.hardware_version, json), - allocator); - json.AddMember("firmware_version", jsonNode(device.firmware_version, json), - allocator); - json.AddMember("ports", jsonNode(ports, json), allocator); - json.AddMember("vlans", jsonNode(vlans, json), allocator); - - rapidjson::StringBuffer sb; - rapidjson::PrettyWriter writer(sb); - json.Accept(writer); - return sb.GetString(); -} - diff --git a/src/Switch.h b/src/Switch.h index 0a5c7ea..88a3351 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -17,6 +17,10 @@ #define DEFAULT_USER "admin" #define DEFAULT_PASS "admin" +enum CntStatus { + OPEN, SHORT +}; + struct vlan { int vlan_id; std::string name; @@ -24,17 +28,34 @@ struct vlan { std::vector untagged_member; }; +struct trunk { + int trunk_id; + std::vector member; +}; + struct port { byte id; byte status; int pvid; - //port_settings + struct { + //port_settings + } settings; + struct { + //port_statistics + } stats; + struct { + //bandwidth_control_ingress + //bandwidth_control_egress + } bw_control; + struct { + //storm_control + } storm_control; + struct { + CntStatus status; + int fault_distace; + } test; //qos_basic - //bandwidth_control_1 - //bandwidth_control_2 - //storm_control - //port_statistics - //cable_test + }; class Switch { @@ -45,6 +66,7 @@ public: int parse(dataset); int parse(std::string); std::string toString(); + struct { std::string type; std::string hardware_version; @@ -63,7 +85,9 @@ public: bool loop_prevention; bool qos_enabled; bool vlan_enabled; + //mtu_vlan } settings; + private: rapidjson::Document json; std::vector vlans; diff --git a/src/bytes.cpp b/src/bytes.cpp index bdc8c9d..9dac956 100644 --- a/src/bytes.cpp +++ b/src/bytes.cpp @@ -7,22 +7,23 @@ #include "bytes.h" -bytes::bytes(std::string d) { - vector(); +bytes bytes::readHex(std::string s){ + vector ret; std::string delimiter = ":"; std::string token; size_t pos = 0; int hex; byte b; - resize(0); - while ((pos = d.find(delimiter)) != std::string::npos) { - token = d.substr(0, pos); + ret.resize(0); + while ((pos = s.find(delimiter)) != std::string::npos) { + token = s.substr(0, pos); sscanf(token.c_str(), "%x", &hex); - d.erase(0, pos + delimiter.length()); + s.erase(0, pos + delimiter.length()); b = hex & 0xFF; - push_back(b); + ret.push_back(b); } - sscanf(d.c_str(), "%x", &hex); + sscanf(s.c_str(), "%x", &hex); b = hex & 0xFF; - push_back(b); + ret.push_back(b); + return ret; } diff --git a/src/bytes.h b/src/bytes.h index dcc0ec9..eb129d6 100644 --- a/src/bytes.h +++ b/src/bytes.h @@ -24,11 +24,11 @@ public: using vector::operator[]; bytes() { } - bytes(int n) : - vector(n) { + bytes(int n) : vector(n){ } - bytes(std::string); + bytes(std::string d) : vector(d.begin(), d.end()){ + } bytes(std::initializer_list s) { @@ -42,9 +42,7 @@ public: this->insert(this->begin(), B.begin(), B.end()); } - bytes readHex(std::string s){ - return bytes(s); - } + bytes readHex(std::string); bytes operator=(const vector &B) { this->reserve(B.size()); diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index c23c0a0..8a821ee 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -16,9 +16,10 @@ #include #include -#include "Options.h" +#include "Constant.h" #include "Host.h" #include "Program.h" +#include "Switch.h" #define no_argument 0 #define required_argument 1 @@ -33,6 +34,9 @@ constexpr unsigned int caseArg(const char* str, int h = 0) { int main(int argc, char *argv[]) { int opt, index; + options.user = DEFAULT_USER; + options.password = DEFAULT_PASS; + const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { "permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, diff --git a/src/table.h b/src/table.h index f9171a6..54f8a91 100644 --- a/src/table.h +++ b/src/table.h @@ -13,7 +13,7 @@ class table { public: - enum F {STRING,HEX,DEC,ACTION,EMPTY}; + enum F {STRING,HEX,DEC,ACTION,BOOL,EMPTY}; struct set { short type; F format; From 922ac9f88e6e4829a5e4eea54b14edd11af40350 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 21 Jan 2016 05:20:21 +0100 Subject: [PATCH 02/17] updates --- src/Options.h | 66 --------------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 src/Options.h diff --git a/src/Options.h b/src/Options.h deleted file mode 100644 index 034ee3f..0000000 --- a/src/Options.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Options.h - * - * Created on: 11.09.2015 - * Author: jdi - */ - -#ifndef OPTIONS_H_ -#define OPTIONS_H_ - -#include "Types.h" - -#define VERSION "smrtlink (v1 Linux)\n" -#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\ - [-p password] \n\n" -#define HELP "\ - Option Summary:\n\ - -h --help This help text\n\ - -v --version Display version of this tool\n\ - -d --debug [n] Show debugging messages\n\ - -r switch ports to emulate switch while sniffing\n\ - -b --header Show header\n\ - -x --hex Display Packets as Hex String\n\ - -j --json Display Packets as JSON\n\ - -i --interface only use one Interface\n\ - -u --user <[password:]username>\n\ - -p --password \n\ - Login with Username and Password\n\ - -f --file choose a settings file\n\n\ - -t --timeout Timeout in milliseconds. Default: 180\n\ - -I --interactive Not yet implemented\n\ - -O --stdout Not yet implemented\n\ - -w --wait Not yet implemented: blocking until operation is completed\n\ - -s --permanent Not yet implemented: make changes immediately permanent\n\n\ - Command Summary:\n\ - help This help text\n\ - list list all connected switches\n\ - sniff [type:] []\n\ - capture and display all incoming or outgoing packets\n\ - depending on the --reverse option\n\ - encode use encoding algorithm on hex data separated by colon\n\ - get Not yet implemented\n\ - set Not yet implemented\n\ - get|save Not yet implemented: save config to file\n\ - set|restore Not yet implemented: restore onfig from file\n\ - flash Not yet implemented: replace firmware\n\ - reboot Not yet implemented\n\ - reset Not yet implemented\n\n\ - ### for questions please contact ###\n\n" -//TODO -/* - * Stdin - * socketmode - * - */ - -#define FLAG_HEX 1 -#define FLAG_REVERSE 2 -#define FLAG_HEADER 4 -#define FLAG_PERMANENT 8 -#define FLAG_WAIT 16 -#define FLAG_DEBUG 32 - -extern Options options; - -#endif /* OPTIONS_H_ */ From 693d8d42ffa8c8ab4a9e3ae6e1dbe4a5f1f77146 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Fri, 22 Jan 2016 23:51:17 +0100 Subject: [PATCH 03/17] Program.cpp update --- src/Program.cpp | 125 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 28 deletions(-) diff --git a/src/Program.cpp b/src/Program.cpp index 636ed70..d47fdf7 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -157,6 +157,103 @@ int Program::setProperty() { } 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) { + + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + + 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); + return 0; + }; + + sock->send(c); + return 0; + }; + sock->send(b); + io_service->run(); + } catch (std::exception& e) { + std::cerr << "Exception: " << e.what() << "\n"; + } + return 1; +} + +int Program::save() { + Switch sw = Switch(); + sw.settings.hostname = "testname.lan"; + File f; + f.write(sw.toString()); + return 1; +} + +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"; + return 1; +} + +int Program::flash() { + + return 0; +} + +int Program::reboot() { std::cout << "List:\n"; Packet p = Packet(Packet::DISCOVERY); p.setHostMac(host.getMac()); @@ -228,34 +325,6 @@ int Program::getProperty() { return 1; } -int Program::save() { - Switch sw = Switch(); - sw.settings.hostname = "testname.lan"; - File f; - f.write(sw.toString()); - return 1; -} - -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"; - return 1; -} - -int Program::flash() { - - return 0; -} - -int Program::reboot() { - - return 0; -} - int Program::reset() { return 0; From d3b89e3b11b795f5cba9eeb7e20891a9adf60929 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 04:25:55 +0100 Subject: [PATCH 04/17] 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_ */ From 8200103514a03c7b44581d0d9b91a5bd1dc7b89d Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 09:22:46 +0100 Subject: [PATCH 05/17] get&set --- Makefile | 2 +- src/Constant.h | 73 ++++----- src/Program.cpp | 37 +++-- src/Program.h | 5 +- src/Socket.cpp | 4 +- src/Switch-Json.cpp | 2 +- src/Types.h | 140 ++++++++-------- src/lookup.h | 10 +- src/lookup/general.lst | 42 ++--- src/lookup/rcv.lst | 20 +-- src/lookup/snd.lst | 24 +-- src/smrtlink.cpp | 360 ++++++++++++++++++++++------------------- src/table.cpp | 33 +++- src/table.h | 19 ++- 14 files changed, 415 insertions(+), 356 deletions(-) diff --git a/Makefile b/Makefile index 5743050..1b4e939 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ $(BUILDDIR): $(TARGET): $(OBJECTS) - $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lpthread + $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lboost_program_options -lpthread $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp diff --git a/src/Constant.h b/src/Constant.h index 31e73fc..c86c8f0 100644 --- a/src/Constant.h +++ b/src/Constant.h @@ -14,38 +14,40 @@ #define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\ [-p password] \n\n" #define HELP "\ - Option Summary:\n\ - -h --help This help text\n\ - -v --version Display version of this tool\n\ - -d --debug [n] Show debugging messages\n\ - -r switch ports to emulate switch while sniffing\n\ - -b --header Show header\n\ - -x --hex Display Packets as Hex String\n\ - -j --json Display Packets as JSON\n\ - -i --interface only use one Interface\n\ - -u --user <[password:]username>\n\ - -p --password \n\ - Login with Username and Password\n\ - -f --file choose a settings file\n\n\ - -t --timeout Timeout in milliseconds. Default: 180\n\ - -I --interactive Not yet implemented\n\ - -O --stdout Not yet implemented\n\ - -w --wait Not yet implemented: blocking until operation is completed\n\ - -s --permanent Not yet implemented: make changes immediately permanent\n\n\ - Command Summary:\n\ - help This help text\n\ - list list all connected switches\n\ - sniff [type:] []\n\ - capture and display all incoming or outgoing packets\n\ - depending on the --reverse option\n\ - encode use encoding algorithm on hex data separated by colon\n\ - get Not yet implemented\n\ - set Not yet implemented\n\ - get|save Not yet implemented: save config to file\n\ - set|restore Not yet implemented: restore onfig from file\n\ - flash Not yet implemented: replace firmware\n\ - reboot Not yet implemented\n\ - reset Not yet implemented\n\n\ + Option Summary:\n\ + -h --help This help text\n\ + -V --version Display version of this tool\n\ + -d --debug [n] Show debugging messages\n\ + -v --verbose [n] Show debugging messages\n\ + -r --reverse switch ports to emulate switch while sniffing\n\ + -b --header Show header\n\ + -x --hex Display Packets as Hex String\n\ + -j --json Display Packets as JSON\n\ + -i --interface only use one Interface\n\ + -u --user <[password:]username>\n\ + -p --password \n\ + Login with Username and Password\n\ + -f --file choose a settings file\n\n\ + -t --timeout Timeout in milliseconds. Default: 180\n\ + -I --interactive Not yet implemented\n\ + -O --stdout Not yet implemented\n\ + -w --wait Not yet implemented: blocking until operation is completed\n\ + -s --permanent Not yet implemented: make changes immediately permanent\n\n\ + Command Summary:\n\ + list list all connected switches\n\ + sniff [type:] []\n\ + capture and display all incoming or outgoing packets\n\ + depending on the --reverse option\n\ + encode use encoding algorithm on hex data separated by colon\n\ + get [ [...]]\n\ + Not yet implemented\n\ + set = [= [...]]\n\ + Not yet implemented\n\ + get|save Not yet implemented: save config to file\n\ + set|restore Not yet implemented: restore onfig from file\n\ + flash Not yet implemented: replace firmware\n\ + reboot Not yet implemented\n\ + reset Not yet implemented\n\n\ ### for questions please contact ###\n\n" //TODO /* @@ -54,13 +56,6 @@ * */ -#define FLAG_HEX 1 -#define FLAG_REVERSE 2 -#define FLAG_HEADER 4 -#define FLAG_PERMANENT 8 -#define FLAG_WAIT 16 -#define FLAG_DEBUG 32 - extern Options options; #endif /* OPTIONS_H_ */ diff --git a/src/Program.cpp b/src/Program.cpp index 24bf99a..a2c3ee5 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -18,8 +18,8 @@ #include "table.h" int printHeader(Packet p) { - if (options.flags & FLAG_HEADER) { - if (options.flags & FLAG_HEX) { + if (options.flags.HEADER) { + if (options.flags.HEX) { std::cout << "Received Header:\n\t" << p.getHead() << "\n"; } else { p.printHeader(); @@ -30,12 +30,12 @@ int printHeader(Packet p) { } int printPacket(Packet p) { - if (options.flags & FLAG_HEX) { + if (options.flags.HEX) { std::cout << "Received Payload:\n\t" << p.getBody() << "\n"; } else { for (dataset d : p.getPayload()) { auto lookup = - (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup; + (options.flags.REVERSE) ? snd_lookup : rcv_lookup; if (lookup.exists(d.type)) { table::set s = lookup[d.type]; if (d.len > 0) { @@ -92,7 +92,7 @@ int Program::list() { std::cout << "List:\n"; discover([this](Packet a) { printHeader(a); - if (options.flags & FLAG_HEX) { + if (options.flags.HEX) { std::cout <<"Received Payload:\n"<run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } - return 1; + return 0; } int Program::sniff() { @@ -130,7 +130,7 @@ int Program::sniff() { std::cerr << "Exception: " << e.what() << "\n"; } - return 1; + return 0; } int Program::encode(std::string s) { @@ -141,7 +141,7 @@ int Program::encode(std::string s) { return 0; } -int Program::setProperty() { +int Program::setProperty(std::map prop) { try { std::cout << "List:\n"; @@ -158,7 +158,7 @@ int Program::setProperty() { Switch sw = Switch(); sw.parse(d); - datasets t = { {REBOOT, 1, {0}}}; + datasets t = { {LOOP_PREVENTION, 1, {0}}}; set(a,t, [this](Packet a) { @@ -176,12 +176,11 @@ int Program::setProperty() { } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } - return 1; + return 0; } -int Program::getProperty() { +int Program::getProperty(std::vector prop) { try { - std::cout << "List:\n"; discover([this](Packet a) { @@ -204,7 +203,7 @@ int Program::getProperty() { } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } - return 1; + return 0; } int Program::save() { @@ -212,7 +211,7 @@ int Program::save() { sw.settings.hostname = "testname.lan"; File f; f.write(sw.toString()); - return 1; + return 0; } int Program::restore() { @@ -220,12 +219,12 @@ int Program::restore() { Switch sw; sw.parse(f.read()); sw.print(); - return 1; + return 0; } int Program::flash() { - return 0; + return 1; } int Program::reboot() { @@ -254,12 +253,12 @@ int Program::reboot() { } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } - return 1; + return 0; } int Program::reset() { - return 0; + return 1; } int Program::discover(std::function c) { diff --git a/src/Program.h b/src/Program.h index fcb8e15..02dbedd 100644 --- a/src/Program.h +++ b/src/Program.h @@ -14,6 +14,7 @@ #include "Host.h" #include "Socket.h" + class Program { private: std::shared_ptr io_service; @@ -31,8 +32,8 @@ public: int list(); int sniff(); int encode(std::string); - int setProperty(); - int getProperty(); + int getProperty(std::vector); + int setProperty(std::map); int save(); int restore(); int flash(); diff --git a/src/Socket.cpp b/src/Socket.cpp index 0df5869..2d0e363 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -21,13 +21,13 @@ void Socket::init(short dst_port, short src_port) { if (initialized) return; - if (options.flags & FLAG_REVERSE) { + if (options.flags.REVERSE) { short p = dst_port; dst_port = src_port; src_port = p; } - if (options.flags & FLAG_DEBUG) + if (options.debug_level>=1) std::cout << "Local IP:\t" << local_ip << "\n"; wildcard_endpoint_ = boost::asio::ip::udp::endpoint( diff --git a/src/Switch-Json.cpp b/src/Switch-Json.cpp index 21f1477..c94961c 100644 --- a/src/Switch-Json.cpp +++ b/src/Switch-Json.cpp @@ -16,7 +16,7 @@ int Switch::parse(std::string str) { if (json.Parse(str.c_str()).HasParseError()) return 1; - if (options.flags & FLAG_DEBUG) + if (options.debug_level>=1) std::cout << "\nParsing to document succeeded.\n"; if (json.IsObject()) { diff --git a/src/Types.h b/src/Types.h index 019c07f..3797d1d 100644 --- a/src/Types.h +++ b/src/Types.h @@ -21,105 +21,113 @@ class macAddr: public std::array { public: - friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) { - out << std::hex << std::setw(2) << std::setfill('0') - << (unsigned) arr[0]; - for (unsigned i = 1; i < 6; i++) { - out << ":" << std::setw(2) << std::setfill('0') - << (unsigned) arr[i]; - } - return out; - } + friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) { + out << std::hex << std::setw(2) << std::setfill('0') + << (unsigned) arr[0]; + for (unsigned i = 1; i < 6; i++) { + out << ":" << std::setw(2) << std::setfill('0') + << (unsigned) arr[i]; + } + return out; + } - macAddr() { - *this= {0,0,0,0,0,0}; - } + macAddr() { + *this= {0,0,0,0,0,0}; + } - macAddr(std::initializer_list s) { - int i = 0; + macAddr(std::initializer_list s) { + int i = 0; for (byte b : s) { - if(i<6) (*this)[i++]=b; - else break; + if(i<6) (*this)[i++]=b; + else break; } - } + } - macAddr(bytes bts) { - int i = 0; + macAddr(bytes bts) { + int i = 0; for (byte b : bts) { - if(i<6) (*this)[i++]=b; - else break; + if(i<6) (*this)[i++]=b; + else break; } - } + } }; /* -class mac_addr : public std::array { -public: - typedef std::array super; + class mac_addr : public std::array { + public: + typedef std::array super; - using super::super; - - mac_addr{00, 00, 00, 000}; -}; -*/ + using super::super; + mac_addr{00, 00, 00, 000}; + }; + */ class ipAddr: public std::array { public: - ipAddr() { - *this= {0,0,0,0,0,0}; - } + ipAddr() { + *this= {0,0,0,0,0,0}; + } - ipAddr(std::initializer_list s) { - int i = 0; - for (byte b : s) { - if(i<4) (*this)[i++]=b; - else break; + ipAddr(std::initializer_list s) { + int i = 0; + for (byte b : s) { + if(i<4) (*this)[i++]=b; + else break; } - } + } - ipAddr(bytes bts) { - int i = 0; + ipAddr(bytes bts) { + int i = 0; for (byte b : bts) { - if(i<4) (*this)[i++]=b; - else break; + if(i<4) (*this)[i++]=b; + else break; } - } + } - friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) { - out << std::dec << (unsigned) arr[0]; - for (unsigned i = 1; i < 4; i++) { - out << "." << (unsigned) arr[i]; - } - return out; - } + friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) { + out << std::dec << (unsigned) arr[0]; + for (unsigned i = 1; i < 4; i++) { + out << "." << (unsigned) arr[i]; + } + return out; + } }; template std::vector operator+(const std::vector &A, const std::vector &B) { - std::vector AB; - AB.reserve(A.size() + B.size()); // preallocate memory - AB.insert(AB.end(), A.begin(), A.end()); // add A; - AB.insert(AB.end(), B.begin(), B.end()); // add B; - return AB; + std::vector AB; + AB.reserve(A.size() + B.size()); // preallocate memory + AB.insert(AB.end(), A.begin(), A.end()); // add A; + AB.insert(AB.end(), B.begin(), B.end()); // add B; + return AB; } template std::vector &operator+=(std::vector &A, const std::vector &B) { - A.reserve(A.size() + B.size()); - A.insert(A.end(), B.begin(), B.end()); - return A; + A.reserve(A.size() + B.size()); + A.insert(A.end(), B.begin(), B.end()); + return A; } struct Options { - unsigned flags = 0x00; - std::string user; - std::string password; - std::string interface; - std::string file; - int debug_level=0; - long timeout = 180U; + struct { + bool HEX; + bool JSON; + bool PLAIN; + bool REVERSE; + bool HEADER; + bool PERMANENT; + bool WAIT; + } flags; + std::string user; + std::string password; + std::string interface; + std::string file; + int debug_level = 0; + int verbosity = 0; + long timeout = 180U; }; #endif /* TYPES_H_ */ diff --git a/src/lookup.h b/src/lookup.h index a51f9bf..faed805 100644 --- a/src/lookup.h +++ b/src/lookup.h @@ -1,13 +1,13 @@ enum { -#define LOOKUP_SET(id, num, type) RCV_ ## id = num, +#define LOOKUP_SET(id, name, num, type) RCV_ ## id = num, #include "lookup/rcv.lst" #include "lookup/general.lst" #undef LOOKUP_SET -#define LOOKUP_SET(id, num, type) SND_ ## id = num, +#define LOOKUP_SET(id, name, num, type) SND_ ## id = num, #include "lookup/snd.lst" #include "lookup/general.lst" #undef LOOKUP_SET -#define LOOKUP_SET(id, num, type) id = num, +#define LOOKUP_SET(id, name, num, type) id = num, #include "lookup/rcv.lst" #include "lookup/snd.lst" #include "lookup/general.lst" @@ -15,14 +15,14 @@ enum { }; static table rcv_lookup { -#define LOOKUP_SET(id, num, type) { RCV_ ## id, table::type, #id }, +#define LOOKUP_SET(id, name, num, type) { RCV_ ## id, table::type, #name, #id }, #include "lookup/rcv.lst" #include "lookup/general.lst" #undef LOOKUP_SET }; static table snd_lookup { -#define LOOKUP_SET(id, num, type) { SND_ ## id, table::type, #id }, +#define LOOKUP_SET(id, name, num, type) { SND_ ## id, table::type, #name, #id }, #include "lookup/snd.lst" #include "lookup/general.lst" #undef LOOKUP_SET diff --git a/src/lookup/general.lst b/src/lookup/general.lst index c4a4f05..3dba23d 100644 --- a/src/lookup/general.lst +++ b/src/lookup/general.lst @@ -1,25 +1,25 @@ -LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports +LOOKUP_SET(PORTS, ports, 10, DEC) //+byte, maybe number of ports -LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //??? -LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port -LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask?? +LOOKUP_SET(IGMP_SNOOPING, igmp, 4352, HEX) //??? +LOOKUP_SET(PORTS_SETTINGS, psetings, 4096, HEX) //+per port +LOOKUP_SET(PORT_TRUNK, trunk, 4608, HEX) //byte[5] last byte bitmask?? -LOOKUP_SET(MTU_VLAN, 8192, HEX) //byte[2] first byte bool,second byte port id -LOOKUP_SET(PORT_VLAN_ENABLED, 8448, BOOL) //open page -LOOKUP_SET(PORT_VLAN, 8449, HEX) -LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC) -LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte -LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan -LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port -LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //???? +LOOKUP_SET(MTU_VLAN, 8192,8192, HEX) //byte[2] first byte bool,second byte port id +LOOKUP_SET(PORT_VLAN_ENABLED, 8448,8448, BOOL) //open page +LOOKUP_SET(PORT_VLAN, 8449,8449, HEX) +LOOKUP_SET(PORT_VLAN_MAX, 8450,8450, DEC) +LOOKUP_SET(VLAN_ENABLED, 8704, 8704, BOOL) //+bool byte +LOOKUP_SET(VLAN, 8705,8705, HEX) //+one set per vlan +LOOKUP_SET(VLAN_PVID, 8706,8706, HEX) //+per port +LOOKUP_SET(VLAN_FOOOO, 8707,8707, DEC) //???? -LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod -LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ??? -LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ??? -LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ??? -LOOKUP_SET(STORM_CONTROL, 12800, HEX) //+per port ??? -LOOKUP_SET(PORT_MIRROR, 16640, HEX) //byte[10] second byte port id??? -LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ??? -LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ??? -LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte +LOOKUP_SET(QOS_BASIC_ENABLED, 12288,12288, BOOL) //+bool = QoS Mod +LOOKUP_SET(QOS_BASIC, 12289,12289, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_INGRESS, 12544,12544, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_EGRESS, 12545,12545, HEX) //+per port ??? +LOOKUP_SET(STORM_CONTROL, 12800,12800, HEX) //+per port ??? +LOOKUP_SET(PORT_MIRROR, 16640,16640, HEX) //byte[10] second byte port id??? +LOOKUP_SET(PORT_STATISTICS, 16384,16384, HEX) //+per port ??? +LOOKUP_SET(CABLE_TEST, 16896,16896, HEX) //+per port ??? +LOOKUP_SET(LOOP_PREVENTION, 17152,17152, BOOL) //+bool byte diff --git a/src/lookup/rcv.lst b/src/lookup/rcv.lst index 7380ef2..f7cee17 100644 --- a/src/lookup/rcv.lst +++ b/src/lookup/rcv.lst @@ -1,13 +1,13 @@ -LOOKUP_SET(TYPE, 1, STRING) //+string -LOOKUP_SET(HOSTNAME, 2, STRING) //+string -LOOKUP_SET(MAC, 3, HEX) //+byte[6] -LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4] -LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4] -LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4] -LOOKUP_SET(FIRMWARE_VERSION, 7, STRING) -LOOKUP_SET(HARDWARE_VERSION, 8, STRING) -LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte +LOOKUP_SET(TYPE,type, 1, STRING) //+string +LOOKUP_SET(HOSTNAME,host, 2, STRING) //+string +LOOKUP_SET(MAC,MAC, 3, HEX) //+byte[6] +LOOKUP_SET(IP_ADDR,ip, 4, DEC) //+byte[4] +LOOKUP_SET(IP_MASK,mask, 5, DEC) //+byte[4] +LOOKUP_SET(GATEWAY,gateway, 6, DEC) //+byte[4] +LOOKUP_SET(FIRMWARE_VERSION,FIRMWARE_VERSION, 7, STRING) +LOOKUP_SET(HARDWARE_VERSION,HARDWARE_VERSION, 8, STRING) +LOOKUP_SET(DHCP_ENABLED,dhcp, 9, BOOL) //+bool byte -LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX) +LOOKUP_SET(PORT_VLAN_FOOOOO, 2101,2101, HEX) diff --git a/src/lookup/snd.lst b/src/lookup/snd.lst index d254a39..797512e 100644 --- a/src/lookup/snd.lst +++ b/src/lookup/snd.lst @@ -1,17 +1,17 @@ -LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo -LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo +LOOKUP_SET(SYSTEM_INFO,SYSTEM_INFO, 2, HEX) //page sysinfo +LOOKUP_SET(IP_CONFIG,IP_CONFIG, 9, HEX) //page sysinfo -LOOKUP_SET(LOGIN_USER, 512, STRING) //string -LOOKUP_SET(NEW_USER, 513, STRING) //string -LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string -LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string -LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config +LOOKUP_SET(LOGIN_USER,LOGIN_USER, 512, STRING) //string +LOOKUP_SET(NEW_USER,NEW_USER, 513, STRING) //string +LOOKUP_SET(LOGIN_PASSWORD,LOGIN_PASSWORD, 514, STRING) //string +LOOKUP_SET(NEW_PASSWORD,NEW_PASSWORD, 515, STRING) //string +LOOKUP_SET(REBOOT,REBOOT, 773, BOOL) //bool byte = save config -LOOKUP_SET(RESET, 1280, ACTION) // -LOOKUP_SET(FLASH, 1536, ACTION) //update firmware -LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan -LOOKUP_SET(SAVE, 2304, ACTION) //save -LOOKUP_SET(PING, 2305, ACTION) //sent before SET ??? +LOOKUP_SET(RESET,RESET, 1280, ACTION) // +LOOKUP_SET(FLASH,FLASH, 1536, ACTION) //update firmware +LOOKUP_SET(VLAN_FOOOOOO, 2200,2200, HEX) //vlan +LOOKUP_SET(SAVE,SAVE, 2304, ACTION) //save +LOOKUP_SET(PING,PING, 2305, ACTION) //sent before SET ??? diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 8a821ee..3b3ed8f 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -1,15 +1,17 @@ //============================================================================ // Name : smrtlink.cpp // Author : jdi -// Version : +// Version : 1.1 // Copyright : GPL v2 // Description : SmrtLink in C++, Ansi-style //============================================================================ -#include #include -#include #include +#include + +#include +#include #include #include @@ -20,202 +22,232 @@ #include "Host.h" #include "Program.h" #include "Switch.h" +#include "lookup.h" #define no_argument 0 #define required_argument 1 #define optional_argument 2 -Options options; +using namespace std; + +//Options options; constexpr unsigned int caseArg(const char* str, int h = 0) { - return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; + return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; } int main(int argc, char *argv[]) { - int opt, index; + int opt, index; - options.user = DEFAULT_USER; - options.password = DEFAULT_PASS; + options.user = DEFAULT_USER; + options.password = DEFAULT_PASS; - const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { - "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { - "permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, - 'd' }, { "password", required_argument, 0, 'p' }, { "user", - required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, { - "header", required_argument, 0, 'b' }, { "hex", required_argument, - 0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout", - required_argument, 0, 't' }, { "wait", - required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; + const struct option longopts[] = { { "version", no_argument, 0, 'V' }, { + "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { + "reverse", no_argument, 0, 'r' }, + { "permanent", no_argument, 0, 's' }, { "debug", optional_argument, + 0, 'd' }, { "password", required_argument, 0, 'p' }, { + "user", required_argument, 0, 'u' }, { "interface", + required_argument, 0, 'i' }, { "header", required_argument, + 0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file", + required_argument, 0, 'f' }, { "timeout", required_argument, + 0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0, + 0 }, }; - Program p = Program(); + Program p = Program(); - while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts, - &index)) != -1) { - switch (opt) { + while ((opt = getopt_long(argc, argv, "bhrvVswxp:u:i:f:t:d::", longopts, + &index)) != -1) { + switch (opt) { - case 'h': - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); - break; + case 'h': + fprintf(stderr, VERSION); + fprintf(stderr, USAGE, argv[0]); + fprintf(stderr, HELP); + exit(EXIT_SUCCESS); + break; - case 'v': - fprintf(stderr, VERSION); - exit(EXIT_SUCCESS); - break; + case 'V': + fprintf(stderr, VERSION); + exit(EXIT_SUCCESS); + break; - case 'r': - options.flags |= FLAG_REVERSE; - break; + case 'r': + options.flags.REVERSE = true; + break; - case 'b': - options.flags |= FLAG_HEADER; - break; + case 'b': + options.flags.HEADER = true; + break; - case 'x': - options.flags |= FLAG_HEX; - break; + case 'x': + options.flags.HEX = true; + break; - case 's': - options.flags |= FLAG_PERMANENT; - break; + case 's': + options.flags.PERMANENT = true; + break; - case 'w': - options.flags |= FLAG_WAIT; - break; + case 'w': + options.flags.WAIT = true; + break; - case 'd': - options.flags |= FLAG_DEBUG; - if (optarg != NULL) - options.debug_level = atoi(optarg); - break; + case 'v': + if (optarg != NULL) + options.verbosity = atoi(optarg); + else + options.verbosity++; + break; - case 't': - options.timeout = atol(optarg); - break; + case 'd': + if (optarg != NULL) + options.debug_level = atoi(optarg); + else + options.debug_level++; + break; - case 'f': - options.file = std::string(optarg); - break; + case 't': + options.timeout = atol(optarg); + break; - case 'p': - options.password = std::string(optarg); - break; + case 'f': + options.file = std::string(optarg); + break; - case 'u': - options.user = std::string(optarg); - break; + case 'p': + options.password = std::string(optarg); + break; - case 'i': - options.interface = std::string(optarg); - break; + case 'u': + options.user = std::string(optarg); + break; - default: /* '?' */ - fprintf(stderr, "Unknown option\n"); - fprintf(stderr, USAGE, argv[0]); - exit(EXIT_FAILURE); - } - } + case 'i': + options.interface = std::string(optarg); + break; - /*//TODO stdin - std::ostringstream bucket; - bucket << std::cin.rdbuf(); - p.input = bucket.str(); - */ + default: /* '?' */ + fprintf(stderr, "Unknown option\n"); + fprintf(stderr, USAGE, argv[0]); + exit(EXIT_FAILURE); + } + } - if (optind >= argc) { - fprintf(stderr, "Command expected\n"); - fprintf(stderr, USAGE, argv[0]); - exit(EXIT_FAILURE); - } + /*//TODO stdin + std::ostringstream bucket; + bucket << std::cin.rdbuf(); + p.input = bucket.str(); + */ - p.init(); + if (optind >= argc) { + fprintf(stderr, "Command expected\n"); + fprintf(stderr, USAGE, argv[0]); + exit(EXIT_FAILURE); + } - if (optind < argc) { - std::string cmd = std::string(argv[optind++]); + p.init(); + std::vector vect; + std::map list; + std::cmatch sm; - switch (caseArg(cmd.c_str())) { - case caseArg("set"): - if (p.setProperty()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("reboot"): - if (p.reboot()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("reset"): - if (p.reset()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("save"): - if (p.save()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("restore"): - if (p.restore()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("flash"): - if (p.flash()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; + if (optind < argc) { + std::string cmd = std::string(argv[optind++]); + switch (caseArg(cmd.c_str())) { + case caseArg("reboot"): + if (!p.reboot()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; + case caseArg("reset"): + if (!p.reset()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; + case caseArg("save"): + if (!p.save()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; + case caseArg("restore"): + if (!p.restore()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; + case caseArg("flash"): + if (!p.flash()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; - case caseArg("get"): - if (p.getProperty()) - exit(EXIT_SUCCESS); - break; + case caseArg("list"): + if (!p.list()) + exit(EXIT_SUCCESS); + break; - case caseArg("list"): - if (p.list()) - exit(EXIT_SUCCESS); - break; + case caseArg("sniff"): + if (!p.sniff()) + exit(EXIT_SUCCESS); + break; - case caseArg("sniff"): - if (p.sniff()) - exit(EXIT_SUCCESS); - break; - - case caseArg("encode"): - if (optind < argc) { - std::string s(argv[optind]); - optind++; - if (p.encode(s)) - exit(EXIT_SUCCESS); - } else { - fprintf(stderr, "Argument expected after encode\n"); - exit(EXIT_FAILURE); - } - break; - - case caseArg("help"): - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); - break; - - default: - printf("Unknown command: %s\n", cmd.c_str()); - while (optind < argc) { - printf("->%s\n", argv[optind]); - optind++; - } - exit(EXIT_FAILURE); - } - } - exit(EXIT_FAILURE); + case caseArg("encode"): + if (optind < argc) { + std::string s(argv[optind]); + optind++; + if (p.encode(s)) + exit(EXIT_SUCCESS); + } else { + fprintf(stderr, "Argument expected after encode\n"); + exit(EXIT_FAILURE); + } + break; + case caseArg("set"): + while (optind < argc) { + if (regex_match(argv[optind], sm, + std::regex("^([a-z]+)=(.*)$"))) { + if (!snd_lookup.exists(sm[1])) { + cerr << "Unknown argument " << argv[optind] << endl; + exit(EXIT_FAILURE); + } + list.insert( + std::pair(sm[1], sm[2])); + } else { + cerr << "Invalid Syntax " << argv[optind] << endl; + exit(EXIT_FAILURE); + } + optind++; + } + if (!p.setProperty(list)) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; + case caseArg("get"): + while (optind < argc) { + if (regex_match(argv[optind], sm, std::regex("^([a-z]+)$"))) { + if (!snd_lookup.exists(sm[1])) { + cerr << "Unknown argument " << argv[optind] << endl; + exit(EXIT_FAILURE); + } + vect.push_back(sm[1]); + } else { + cerr << "Invalid argument " << argv[optind] << endl; + exit(EXIT_FAILURE); + } + optind++; + } + if (!p.getProperty(vect)) + exit(EXIT_SUCCESS); + break; + default: + printf("Unknown command: %s\n", cmd.c_str()); + exit(EXIT_FAILURE); + } + } + exit(EXIT_FAILURE); } diff --git a/src/table.cpp b/src/table.cpp index e207490..f159ced 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -8,18 +8,35 @@ #include #include "table.h" + table::table(std::initializer_list l) { - int i = 0; - for (set s : l) { - this->left[s.type] = s; - } + int i = 0; + this->data.resize(l.size()); + for (set s : l) { + this->data[i] = s; + this->left[s.type] = &this->data[i]; + this->right[s.name] = &this->data[i]; + i++; + } } -table::set table::operator[](short n){ - return this->left[n]; +const table::set table::operator[](std::string s){ + return *this->right[s]; +} +const table::set table::operator[](short n){ + return *this->left[n]; +} +bool table::exists(std::string s){ + return !(right.find(s) == right.end()); } bool table::exists(short n){ - return !(left.find(n) == left.end()); + return !(left.find(n) == left.end()); +} +short table::type(std::string s){ + return this->right[s]->type; +} +std::string table::id(short n){ + return this->left[n]->id; } std::string table::name(short n){ - return this->left[n].name; + return this->left[n]->name; } diff --git a/src/table.h b/src/table.h index 377c768..128d26e 100644 --- a/src/table.h +++ b/src/table.h @@ -17,14 +17,21 @@ public: struct set { short type; F format; - std::string name; + std::string name; + std::string id; }; - table(std::initializer_list l); - set operator[](short); - bool exists(short); - std::string name(short); + table(std::initializer_list l); + const table::set operator[](std::string); + const table::set operator[](short); + bool exists(std::string); + bool exists(short); + short type(std::string); + std::string id(short); + std::string name(short); private: - std::map left; + std::vector data; + std::map left; + std::map right; }; #endif /* LOOKUPTABLE_H_ */ From 2952953f5df64ef691e760d82daadbd038d59343 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 09:55:37 +0100 Subject: [PATCH 06/17] repair --- src/smrtlink.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 76511ad..342685f 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -28,6 +28,8 @@ #define required_argument 1 #define optional_argument 2 +using namespace std; + Options options; constexpr unsigned int caseArg(const char* str, int h = 0) { @@ -35,6 +37,7 @@ constexpr unsigned int caseArg(const char* str, int h = 0) { } int main(int argc, char *argv[]) { + int index,opt; options.user = DEFAULT_USER; options.password = DEFAULT_PASS; From a42a0aa8ceea4781124403e46840f13de8b5225f Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 10:05:59 +0100 Subject: [PATCH 07/17] repair --- src/Lookup.h | 91 ------------------------------------------------- src/Program.cpp | 3 +- test.sh | 18 ++++++++++ 3 files changed, 20 insertions(+), 92 deletions(-) delete mode 100644 src/Lookup.h create mode 100755 test.sh diff --git a/src/Lookup.h b/src/Lookup.h deleted file mode 100644 index 9a6bfef..0000000 --- a/src/Lookup.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Switch.cpp - * - * Created on: 29.09.2015 - * Author: jdi - */ - -#include "table.h" - -static table rcv_lookup = { // - { 1, table::STRING, "type" }, //+string - { 2, table::STRING, "hostname" }, //+string - { 3, table::HEX, "mac" }, //+byte[6] - { 4, table::DEC, "ip_addr" }, //+byte[4] - { 5, table::DEC, "ip_mask" }, //+byte[4] - { 6, table::DEC, "gateway" }, //+byte[4] - { 7, table::STRING, "firmware_version" },//+ - { 8, table::STRING, "hardware_version" },//+ - { 9, table::BOOL, "dhcp_enabled" }, //+bool byte - { 10, table::DEC, "ports" }, //+byte, maybe number of ports - - { 2101, table::HEX, "port_vlan ????????" }, - - { 4352, table::HEX, "igmp_snooping" }, //??? - { 4096, table::HEX, "port_settings" }, //+per port - { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? - - { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool,second byte port id - { 8448, table::BOOL, "port_vlan_enabled" }, //open page - { 8449, table::HEX, "port_vlan" }, //??? - { 8450, table::DEC, "port_vlan_max" }, //??? - { 8704, table::BOOL, "vlan_enabled" }, //+bool byte - { 8705, table::HEX, "vlan" }, //+one set per vlan - { 8706, table::HEX, "vlan_pvid" }, //+per port - { 8707, table::DEC, "vlan ?????" }, //???? - - { 12288, table::BOOL, "qos_basic_enabled" }, //+bool = QoS Mod - { 12289, table::HEX, "qos_basic" }, //+per port ??? - { 12544, table::HEX, "bandwidth_control_ingress" }, //+per port ??? - { 12545, table::HEX, "bandwidth_control_egress" }, //+per port ??? - { 12800, table::HEX, "storm_control" }, //+per port ??? - { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??? - { 16384, table::HEX, "port_statistics" }, //+per port ??? - { 16896, table::HEX, "cable_test" }, //+per port ??? - { 17152, table::BOOL, "loop_prevention" }, //+bool byte - - //{0000," "}, - }; - -static table snd_lookup = { - - // TODO find out if id is unique in response - { 2, table::HEX, "system_info" }, //page sysinfo - { 9, table::HEX, "ip_config" }, //page sysinfo - { 10, table::HEX, "ports" }, //after login - { 512, table::STRING, "login_user" }, //string - { 513, table::STRING, "new_user" }, //string - { 514, table::STRING, "login_password" }, //string - { 515, table::STRING, "new_passwoord" }, //string - { 773, table::BOOL, "reboot" }, //bool byte = save config - - { 1280, table::ACTION, "reset" }, // - { 1536, table::ACTION, "flash" }, //update firmware - - { 2200, table::HEX, "vlan" }, //vlan - { 2304, table::ACTION, "save" }, //save - { 2305, table::ACTION, "ping" }, //sent before SET ??? - - { 4352, table::HEX, "igmp_snooping" }, //switching - { 4096, table::HEX, "port_settings" }, //switching - { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? - - { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id - { 8448, table::BOOL, "port_vlan_enabled" }, //open page - { 8449, table::HEX, "port_vlan" }, //??? - { 8704, table::BOOL, "vlan_enabled" }, //??? get vlan / set status - { 8705, table::HEX, "vlan" }, // per vlan - { 8706, table::HEX, "vlan_pvid" }, //per port - - { 12288, table::BOOL, "qos_basic_enabled" }, //bool = QoS Mod - { 12289, table::HEX, "qos_basic" }, //per port - { 12544, table::HEX, "bandwidth_control_ingress" }, //per port - { 12545, table::HEX, "bandwidth_control_egress" }, //per port - { 12800, table::HEX, "storm_control" }, //??? - { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id?? - { 16384, table::HEX, "port_statistics" }, //??? - { 16896, table::HEX, "cable_test" }, //+per port ??? - { 17152, table::BOOL, "loop_prevention" }, //bool byte - - //{0000," "}, - }; diff --git a/src/Program.cpp b/src/Program.cpp index a2c3ee5..2170e9d 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -134,7 +134,8 @@ int Program::sniff() { } int Program::encode(std::string s) { - bytes d(s); + bytes d; + d=d.readHex(s); Packet p = Packet(Packet::NONE); p.encode(d); std::cout << d << std::endl; diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..e4f2d29 --- /dev/null +++ b/test.sh @@ -0,0 +1,18 @@ +#!/bin/sh +clear +echo SmrtLink Test + +echo bin/smrtlink encode 00:11:22:33:44:55:66:77:88:99 +bin/smrtlink encode 00:11:22:33:44:55:66:77:88:9 + +echo bin/smrtlink -i eth0 list +bin/smrtlink -i eth0 list + +echo bin/smrtlink -i eth6 list +bin/smrtlink -i eth6 list + +echo bin/smrtlink list +bin/smrtlink list + +echo bin/smrtlink -i wlan0 list +bin/smrtlink -i wlan0 list From 164edcdad9102343c09145e04fa718bc4deee6ab Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Wed, 27 Jan 2016 23:54:19 +0100 Subject: [PATCH 08/17] implementing -I, -s and reset --- src/Constant.h | 25 +++--- src/Interactive.cpp | 33 +++++++ src/Interactive.h | 18 ++++ src/Program.cpp | 210 +++++++++++++++++++++++++------------------ src/Types.h | 1 + src/bytes.h | 1 + src/lookup/input.lst | 36 ++++++++ src/smrtlink.cpp | 38 +++++--- 8 files changed, 251 insertions(+), 111 deletions(-) create mode 100644 src/Interactive.cpp create mode 100644 src/Interactive.h create mode 100644 src/lookup/input.lst diff --git a/src/Constant.h b/src/Constant.h index c86c8f0..d739a8c 100644 --- a/src/Constant.h +++ b/src/Constant.h @@ -19,20 +19,25 @@ -V --version Display version of this tool\n\ -d --debug [n] Show debugging messages\n\ -v --verbose [n] Show debugging messages\n\ +\ -r --reverse switch ports to emulate switch while sniffing\n\ - -b --header Show header\n\ - -x --hex Display Packets as Hex String\n\ - -j --json Display Packets as JSON\n\ -i --interface only use one Interface\n\ - -u --user <[password:]username>\n\ - -p --password \n\ - Login with Username and Password\n\ - -f --file choose a settings file\n\n\ -t --timeout Timeout in milliseconds. Default: 180\n\ - -I --interactive Not yet implemented\n\ - -O --stdout Not yet implemented\n\ -w --wait Not yet implemented: blocking until operation is completed\n\ - -s --permanent Not yet implemented: make changes immediately permanent\n\n\ + -s --permanent Not yet implemented: make changes immediately permanent\n\ +\ + -b --header Show header\n\ + -t --text Display Packets as readable Text (default)\n\ + -x --hex Display Packets as Hex String\n\ + -j --json Display Packets as JSON\n\n\ +\ + -U --user <[password:]username>\n\ + -P --password \n\ + Login with Username and Password\n\ + -f --file choose a settings file\n\ + -I --stdin Not yet implemented\n\ + -O --stdout Not yet implemented\n\ + -X --interactive gives in interactive Shell. Ignores all commands\n\n\ Command Summary:\n\ list list all connected switches\n\ sniff [type:] []\n\ diff --git a/src/Interactive.cpp b/src/Interactive.cpp new file mode 100644 index 0000000..6875dc2 --- /dev/null +++ b/src/Interactive.cpp @@ -0,0 +1,33 @@ +/* + * Interactive.cpp + * + * Created on: 27.01.2016 + * Author: jedi + */ + +#include +#include + +#include "Interactive.h" + +using namespace std; + +Interactive::Interactive() { + // TODO Auto-generated constructor stub + +} + +Interactive::~Interactive() { + // TODO Auto-generated destructor stub +} + +int Interactive::run() { + string cmd; + while(cmd.compare("quit")){ + cout<< "smrtlink>" << flush; + cin >> cmd; + cout << cmd < 0) { switch (s.format) { case table::STRING: - std::cout << "+\t" << s.name << " = " << &d.value[0] - << "\n"; + cout << "+\t" << s.name << " = " << &d.value[0] << "\n"; break; case table::BOOL: - std::cout << "+\t" << s.name << " = " + cout << "+\t" << s.name << " = " << (d.value[0] ? "YES" : "NO") << "\n"; break; case table::HEX: - std::cout << "+\t" << s.name << " = " << d.value - << "\n"; + cout << "+\t" << s.name << " = " << d.value << "\n"; break; case table::DEC: - std::cout << "+\t" << s.name << " = "; + cout << "+\t" << s.name << " = "; if (d.value.size() > 0) - std::cout << std::dec << (unsigned) d.value[0]; + cout << dec << (unsigned) d.value[0]; for (unsigned i = 1; i < d.value.size(); i++) - std::cout << std::dec << "." - << (unsigned) d.value[i]; - std::cout << "\n"; + cout << dec << "." << (unsigned) d.value[i]; + cout << "\n"; break; case table::ACTION: - std::cout << "Error:" << s.name + cout << "Error:" << s.name << " is marked as 'action' but carries payload." << d.value << "\n"; break; default: - std::cout << "+\t" << s.name << " = " << d.value - << "\n"; + cout << "+\t" << s.name << " = " << d.value << "\n"; break; } } else { //empty - std::cout << std::dec << ">\t" << s.name << "\n"; + cout << dec << ">\t" << s.name << "\n"; } } else { //unknown id if (d.len > 0) { - std::cout << "##\t" << d.type << ":\n\t"; - std::cout << std::hex << d.value << std::dec << "\n"; + cout << "##\t" << d.type << ":\n\t"; + cout << hex << d.value << dec << "\n"; } else { //empty - std::cout << "#>\t" << d.type << "\n"; + cout << "#>\t" << d.type << "\n"; } } } @@ -89,11 +86,11 @@ int printPacket(Packet p) { int Program::list() { try { - std::cout << "List:\n"; + cout << "List:\n"; discover([this](Packet a) { printHeader(a); if (options.flags.HEX) { - std::cout <<"Received Payload:\n"<run(); - } catch (std::exception& e) { - std::cerr << "Exception: " << e.what() << "\n"; + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; } return 0; } @@ -119,80 +116,93 @@ int Program::sniff() { s.setHostIp(host.getIp()); s.init(DST_PORT, SRC_PORT); s.callback = [](Packet p) { - std::cout << p.opCodeToString() << "\n"; + cout << p.opCodeToString() << "\n"; printHeader(p); printPacket(p); return 0; }; s.listen(); io_service.run(); - } catch (std::exception& e) { - std::cerr << "Exception: " << e.what() << "\n"; + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; } return 0; } -int Program::encode(std::string s) { +int Program::encode(string s) { bytes d; - d=d.readHex(s); + d = d.readHex(s); Packet p = Packet(Packet::NONE); p.encode(d); - std::cout << d << std::endl; + cout << d << endl; return 0; } -int Program::setProperty(std::map prop) { +int Program::setProperty(map prop) { try { + datasets data = { }; + for (auto p : prop) { + string s = p.second; + dataset d; + if (rcv_lookup.exists(p.first)) { + d.type = rcv_lookup.type(p.first); + if (rcv_lookup[p.first].format == table::STRING) { + d.len = s.size() + 1; + d.value = s; + data.push_back(d); + } + } + } - 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 = { {LOOP_PREVENTION, 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 0; -} - -int Program::getProperty(std::vector prop) { - try { - std::cout << "List:\n"; - discover([this](Packet a) { - + cout << "List:\n"; + discover([this,data](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"; + datasets t = { {SND_PING, 0, {}}}; + get(a, t, [this,data](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + set(a,data, + + [this](Packet a) { + cout << a.opCodeToString() << "\n"; + printHeader(a); + printPacket(a); + return 0; + }); + return 0; + }); + return 0; + }); + + io_service->run(); + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; + } + return 0; +} + +int Program::getProperty(vector prop) { + try { + datasets data = { }; + for (string s : prop) { + dataset d; + d.type = snd_lookup.type(s); + data.push_back(d); + } + cout << "List:\n"; + discover([this,data](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + sw.print(); + get(a, data, [this](Packet a) { + cout << a.opCodeToString() << "\n"; printHeader(a); printPacket(a); return 0; @@ -201,8 +211,8 @@ int Program::getProperty(std::vector prop) { }); io_service->run(); - } catch (std::exception& e) { - std::cerr << "Exception: " << e.what() << "\n"; + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; } return 0; } @@ -234,16 +244,16 @@ int Program::reboot() { datasets d =a.getPayload(); Switch sw = Switch(); sw.parse(d); - std::cout <run(); - } catch (std::exception& e) { - std::cerr << "Exception: " << e.what() << "\n"; + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; } return 0; } int Program::reset() { - - return 1; + try { + discover([this](Packet a) { + datasets d =a.getPayload(); + Switch sw = Switch(); + sw.parse(d); + cout <run(); + } catch (exception& e) { + cerr << "Exception: " << e.what() << "\n"; + } + return 0; } -int Program::discover(std::function c) { +int Program::discover(function c) { Packet p = Packet(Packet::DISCOVERY); p.setHostMac(host.getMac()); p.setPayload( { }); @@ -271,7 +305,7 @@ int Program::discover(std::function c) { return 0; } -int Program::get(Packet l, datasets t, std::function c) { +int Program::get(Packet l, datasets t, function c) { Packet p = Packet(Packet::GET); p.setSwitchMac(l.getSwitchMac()); p.setHostMac(host.getMac()); @@ -281,15 +315,13 @@ int Program::get(Packet l, datasets t, std::function c) { return 0; } -int Program::set(Packet l, datasets t, std::function c) { +int Program::set(Packet l, datasets t, 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); diff --git a/src/Types.h b/src/Types.h index 3797d1d..489529a 100644 --- a/src/Types.h +++ b/src/Types.h @@ -120,6 +120,7 @@ struct Options { bool HEADER; bool PERMANENT; bool WAIT; + bool INTERACTIVE; } flags; std::string user; std::string password; diff --git a/src/bytes.h b/src/bytes.h index eb129d6..01f597d 100644 --- a/src/bytes.h +++ b/src/bytes.h @@ -28,6 +28,7 @@ public: } bytes(std::string d) : vector(d.begin(), d.end()){ + this->push_back('\0'); } bytes(std::initializer_list s) diff --git a/src/lookup/input.lst b/src/lookup/input.lst new file mode 100644 index 0000000..d673846 --- /dev/null +++ b/src/lookup/input.lst @@ -0,0 +1,36 @@ +LOOKUP_SET(TYPE,type, 1, STRING) //+string +LOOKUP_SET(HOSTNAME,host, 2, STRING) //+string +LOOKUP_SET(MAC,MAC, 3, HEX) //+byte[6] +LOOKUP_SET(IP_ADDR,ip, 4, DEC) //+byte[4] +LOOKUP_SET(IP_MASK,mask, 5, DEC) //+byte[4] +LOOKUP_SET(GATEWAY,gateway, 6, DEC) //+byte[4] +LOOKUP_SET(FIRMWARE_VERSION,FIRMWARE_VERSION, 7, STRING) +LOOKUP_SET(HARDWARE_VERSION,HARDWARE_VERSION, 8, STRING) +LOOKUP_SET(DHCP_ENABLED,dhcp, 9, BOOL) //+bool byte + +LOOKUP_SET(PORT_VLAN_FOOOOO, 2101,2101, HEX) + +LOOKUP_SET(PORTS, ports, 10, DEC) //+byte, maybe number of ports + +LOOKUP_SET(IGMP_SNOOPING, igmp, 4352, HEX) //??? +LOOKUP_SET(PORTS_SETTINGS, psetings, 4096, HEX) //+per port +LOOKUP_SET(PORT_TRUNK, trunk, 4608, HEX) //byte[5] last byte bitmask?? + +LOOKUP_SET(MTU_VLAN, 8192,8192, HEX) //byte[2] first byte bool,second byte port id +LOOKUP_SET(PORT_VLAN_ENABLED, 8448,8448, BOOL) //open page +LOOKUP_SET(PORT_VLAN, 8449,8449, HEX) +LOOKUP_SET(PORT_VLAN_MAX, 8450,8450, DEC) +LOOKUP_SET(VLAN_ENABLED, 8704, 8704, BOOL) //+bool byte +LOOKUP_SET(VLAN, 8705,8705, HEX) //+one set per vlan +LOOKUP_SET(VLAN_PVID, 8706,8706, HEX) //+per port +LOOKUP_SET(VLAN_FOOOO, 8707,8707, DEC) //???? + +LOOKUP_SET(QOS_BASIC_ENABLED, 12288,12288, BOOL) //+bool = QoS Mod +LOOKUP_SET(QOS_BASIC, 12289,12289, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_INGRESS, 12544,12544, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_EGRESS, 12545,12545, HEX) //+per port ??? +LOOKUP_SET(STORM_CONTROL, 12800,12800, HEX) //+per port ??? +LOOKUP_SET(PORT_MIRROR, 16640,16640, HEX) //byte[10] second byte port id??? +LOOKUP_SET(PORT_STATISTICS, 16384,16384, HEX) //+per port ??? +LOOKUP_SET(CABLE_TEST, 16896,16896, HEX) //+per port ??? +LOOKUP_SET(LOOP_PREVENTION, 17152,17152, BOOL) //+bool byte \ No newline at end of file diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 342685f..9e391cb 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -13,12 +13,12 @@ #include #include #include -#include #include #include #include "Constant.h" +#include "Interactive.h" #include "Host.h" #include "Program.h" #include "Switch.h" @@ -54,9 +54,9 @@ int main(int argc, char *argv[]) { 0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; - Program p = Program(); - while ((opt = getopt_long(argc, argv, "bhrvVswxp:u:i:f:t:d::", longopts, + + while ((opt = getopt_long(argc, argv, "bhrvXIswxP:U:i:f:t:d::", longopts, &index)) != -1) { switch (opt) { @@ -92,6 +92,10 @@ int main(int argc, char *argv[]) { options.flags.WAIT = true; break; + case 'X': + options.flags.INTERACTIVE = true; + break; + case 'v': if (optarg != NULL) options.verbosity = atoi(optarg); @@ -114,11 +118,11 @@ int main(int argc, char *argv[]) { options.file = std::string(optarg); break; - case 'p': + case 'P': options.password = std::string(optarg); break; - case 'u': + case 'U': options.user = std::string(optarg); break; @@ -139,18 +143,28 @@ int main(int argc, char *argv[]) { p.input = bucket.str(); */ - if (optind >= argc) { - fprintf(stderr, "Command expected\n"); - fprintf(stderr, USAGE, argv[0]); + if (optind >= argc && !options.flags.INTERACTIVE) { + cerr << "Command expected\n"; + cerr << USAGE; exit(EXIT_FAILURE); } + if (options.flags.INTERACTIVE) { + if (optind < argc) { + cerr << "Command is ignored in interactive mode\n"; + } + Interactive p = Interactive(); + if (!p.run()) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + } + else if (optind < argc) { + Program p = Program(); p.init(); std::vector vect; std::map list; std::cmatch sm; - - if (optind < argc) { std::string cmd = std::string(argv[optind++]); switch (caseArg(cmd.c_str())) { case caseArg("reboot"): @@ -209,7 +223,7 @@ int main(int argc, char *argv[]) { while (optind < argc) { if (regex_match(argv[optind], sm, std::regex("^([a-z]+)=(.*)$"))) { - if (!snd_lookup.exists(sm[1])) { + if (!snd_lookup.exists(sm[1])&&!rcv_lookup.exists(sm[1])) { cerr << "Unknown argument " << argv[optind] << endl; exit(EXIT_FAILURE); } @@ -229,7 +243,7 @@ int main(int argc, char *argv[]) { case caseArg("get"): while (optind < argc) { if (regex_match(argv[optind], sm, std::regex("^([a-z]+)$"))) { - if (!snd_lookup.exists(sm[1])) { + if (!snd_lookup.exists(sm[1])&&!rcv_lookup.exists(sm[1])) { cerr << "Unknown argument " << argv[optind] << endl; exit(EXIT_FAILURE); } From 59796d600b9df515e6276c036fcfcd093f18c67d Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 28 Jan 2016 00:38:40 +0100 Subject: [PATCH 09/17] clean up --- Makefile | 4 ++-- src/Constant.h | 10 +++++----- src/smrtlink.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 1b4e939..cd5dd04 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,8 @@ $(BUILDDIR): $(TARGET): $(OBJECTS) - $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lboost_program_options -lpthread - + $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system +#-lboost_program_options $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp $(CC) $(CFLAGS) $< -o $@ diff --git a/src/Constant.h b/src/Constant.h index d739a8c..d9885e8 100644 --- a/src/Constant.h +++ b/src/Constant.h @@ -11,8 +11,8 @@ #include "Types.h" #define VERSION "smrtlink (v1.1 Linux)\n" -#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\ - [-p password] \n\n" +#define USAGE "usage: %s [-bdhjrvVstwx] [-i interface] [-U [password:]username]\n\ + [-P password] \n\n" #define HELP "\ Option Summary:\n\ -h --help This help text\n\ @@ -24,7 +24,7 @@ -i --interface only use one Interface\n\ -t --timeout Timeout in milliseconds. Default: 180\n\ -w --wait Not yet implemented: blocking until operation is completed\n\ - -s --permanent Not yet implemented: make changes immediately permanent\n\ + -s --permanent make changes immediately permanent\n\ \ -b --header Show header\n\ -t --text Display Packets as readable Text (default)\n\ @@ -48,8 +48,8 @@ Not yet implemented\n\ set = [= [...]]\n\ Not yet implemented\n\ - get|save Not yet implemented: save config to file\n\ - set|restore Not yet implemented: restore onfig from file\n\ + save Not yet implemented: save config to file\n\ + restore Not yet implemented: restore onfig from file\n\ flash Not yet implemented: replace firmware\n\ reboot Not yet implemented\n\ reset Not yet implemented\n\n\ diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 9e391cb..73462ba 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -46,8 +46,8 @@ int main(int argc, char *argv[]) { "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { "permanent", no_argument, 0, 's' }, { "debug", optional_argument, - 0, 'd' }, { "password", required_argument, 0, 'p' }, { - "user", required_argument, 0, 'u' }, { "interface", + 0, 'd' }, { "password", required_argument, 0, 'P' }, { + "user", required_argument, 0, 'U' }, { "interface", required_argument, 0, 'i' }, { "header", required_argument, 0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout", required_argument, @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) { - while ((opt = getopt_long(argc, argv, "bhrvXIswxP:U:i:f:t:d::", longopts, + while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts, &index)) != -1) { switch (opt) { From 5b7014737aa5f376174cc3d92e5e8856f5a13c11 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 6 Feb 2016 02:32:17 +0100 Subject: [PATCH 10/17] interactive shell wip --- Makefile | 3 +- src/Interactive.cpp | 33 ++++++++-- src/Interactive.h | 2 +- src/Packet.cpp | 4 +- src/Packet.h | 2 +- src/Program.cpp | 150 +++++++++++++++++++++++++++++++++++++------ src/Program.h | 2 + src/Socket.cpp | 58 +++++++++-------- src/Switch-Cmd.cpp | 31 +++++++++ src/Switch.h | 6 +- src/Types.h | 11 +++- src/lookup/input.lst | 10 +-- src/smrtlink.cpp | 126 ++++-------------------------------- 13 files changed, 261 insertions(+), 177 deletions(-) create mode 100644 src/Switch-Cmd.cpp diff --git a/Makefile b/Makefile index cd5dd04..a031fb5 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,7 @@ $(BUILDDIR): $(TARGET): $(OBJECTS) - $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -#-lboost_program_options + $(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lreadline -lboost_program_options $(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp $(CC) $(CFLAGS) $< -o $@ diff --git a/src/Interactive.cpp b/src/Interactive.cpp index 6875dc2..1c35283 100644 --- a/src/Interactive.cpp +++ b/src/Interactive.cpp @@ -7,7 +7,14 @@ #include #include +#include +#include +#include +#include +#include + +#include "Program.h" #include "Interactive.h" using namespace std; @@ -21,12 +28,28 @@ Interactive::~Interactive() { // TODO Auto-generated destructor stub } -int Interactive::run() { +int Interactive::loop() { string cmd; - while(cmd.compare("quit")){ - cout<< "smrtlink>" << flush; - cin >> cmd; - cout << cmd < v; + //vector vc; + //const char** argv; + //int argc; + + Program p = Program(); + p.init(); + + while (cmd.compare("quit")) { + cmd = readline("smrtlink> "); + add_history(cmd.c_str()); + v = boost::program_options::split_unix(cmd); + + //vc = vector(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); + } return 0; } diff --git a/src/Interactive.h b/src/Interactive.h index 2bf41be..13547f3 100644 --- a/src/Interactive.h +++ b/src/Interactive.h @@ -12,7 +12,7 @@ class Interactive { public: Interactive(); virtual ~Interactive(); - int run(); + int loop(); }; #endif /* INTERACTIVE_H_ */ diff --git a/src/Packet.cpp b/src/Packet.cpp index 0e61bf7..9bcf06c 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -158,8 +158,8 @@ std::string Packet::opCodeToString() { return "SET"; case CONFIRM: return "CONFIRM"; - case RETURN: - return "RETURN"; + case REPLY: + return "REPLY"; default: return "NONE"; } diff --git a/src/Packet.h b/src/Packet.h index 2e04a2d..395c86d 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -18,7 +18,7 @@ static short sequenceId=0; class Packet { public: - enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE}; + enum OpCode {DISCOVERY, GET, REPLY, SET, CONFIRM, NONE}; Packet(OpCode); static void encode(bytes&); bytes getBytes(); diff --git a/src/Program.cpp b/src/Program.cpp index 33f27d7..bc3c370 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -6,6 +6,9 @@ */ #include #include +#include +#include +#include #include "Constant.h" #include "Program.h" @@ -14,11 +17,117 @@ #include "Socket.h" #include "Switch.h" #include "Packet.h" +#include "Types.h" #include "lookup.h" #include "table.h" +using namespace smrtlink; using namespace std; +int Program::run(vector arg) { + int optind = 0; + std::vector vect; + std::map ll; + std::cmatch sm; + std::string cmd = arg[optind++]; + switch (caseArg(cmd.c_str())) { + 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()) + return 0; + fprintf(stderr, "Not yet implemented.\n"); + return 1; + break; + case caseArg("restore"): + if (!restore()) + return 0; + fprintf(stderr, "Not yet implemented.\n"); + return 1; + break; + case caseArg("flash"): + if (!flash()) + return 0; + fprintf(stderr, "Not yet implemented.\n"); + return 1; + break; + + case caseArg("list"): + if (!list()) + return 0; + break; + + case caseArg("sniff"): + if (!sniff()) + return 0; + break; + + case caseArg("encode"): + if (optind < arg.size()) { + std::string s(arg[optind]); + optind++; + if (encode(s)) + return 0; + } else { + fprintf(stderr, "Argument expected after encode\n"); + return 1; + } + break; + case caseArg("set"): + while (optind < arg.size()) { + if (regex_match(arg[optind].c_str(), sm, + std::regex("^([a-z]+)=(.*)$"))) { + if (!snd_lookup.exists(sm[1]) && !rcv_lookup.exists(sm[1])) { + cerr << "Unknown argument " << arg[optind] << endl; + return 1; + } + ll.insert(std::pair(sm[1], sm[2])); + } else { + cerr << "Invalid Syntax " << arg[optind] << endl; + return 1; + } + optind++; + } + if (!setProperty(ll)) + return 0; + fprintf(stderr, "Not yet implemented.\n"); + return 1; + break; + case caseArg("get"): + while (optind < arg.size()) { + if (regex_match(arg[optind].c_str(), sm, + std::regex("^([a-z]+)$"))) { + if (!snd_lookup.exists(sm[1]) && !rcv_lookup.exists(sm[1])) { + cerr << "Unknown argument " << arg[optind] << endl; + return 1; + } + vect.push_back(sm[1]); + } else { + cerr << "Invalid argument " << arg[optind] << endl; + return 1; + } + optind++; + } + if (!getProperty(vect)) + return 0; + break; + default: + printf("Unknown command: %s\n", cmd.c_str()); + return 1; + } + +} + int printHeader(Packet p) { if (options.flags.HEADER) { if (options.flags.HEX) { @@ -240,26 +349,27 @@ int Program::flash() { int Program::reboot() { try { - discover([this](Packet a) { - datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); - cout <run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; diff --git a/src/Program.h b/src/Program.h index 614047c..d8357a0 100644 --- a/src/Program.h +++ b/src/Program.h @@ -28,6 +28,8 @@ public: sock = std::make_shared(*io_service); } void init(); + int run(std::vector); + int list(); int sniff(); int encode(std::string); diff --git a/src/Socket.cpp b/src/Socket.cpp index 2d0e363..a864893 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -13,12 +13,15 @@ #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) + if (initialized == 3) return; if (options.flags.REVERSE) { @@ -27,7 +30,7 @@ void Socket::init(short dst_port, short src_port) { src_port = p; } - if (options.debug_level>=1) + if (options.debug_level >= 1) std::cout << "Local IP:\t" << local_ip << "\n"; wildcard_endpoint_ = boost::asio::ip::udp::endpoint( @@ -38,29 +41,7 @@ void Socket::init(short dst_port, short src_port) { boost::asio::ip::address_v4::from_string("255.255.255.255"), dst_port); - 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_); - - 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 = 1; + initialized = 3; } void Socket::setHostIp(ipAddr ip) { @@ -68,6 +49,12 @@ 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]; @@ -80,6 +67,25 @@ void Socket::send(Packet p) { } 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; + } + }); + } data.resize(MAX_LENGTH); receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH), remote_endpoint_, @@ -92,7 +98,7 @@ void Socket::listen() { data.resize(bytes_recvd); Packet p = Packet(Packet::NONE); p.encode(data); - // std::cout << "err" << p.getErrorCode() < +#include "Types.h" +#include "Switch.h" +#include "Constant.h" +#include "table.h" + +using namespace smrtlink; +using namespace std; + +int Switch::set(pair str) { + + return 0; +} + +std::string Switch::get(std::string str) { + std::string ret; + switch (caseArg(str.c_str())) { + case caseArg("ip"): + ret = "0.0.0.0"; + break; + } + return ret; +} + diff --git a/src/Switch.h b/src/Switch.h index 1b40071..cb962ab 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -65,9 +65,13 @@ public: int parse(datasets); int parse(dataset); int parse(std::string); - int print(); + int set(std::pair); + + std::string get(std::string); std::string toString(); + int print(); + struct { std::string type; std::string hardware_version; diff --git a/src/Types.h b/src/Types.h index 489529a..e236c10 100644 --- a/src/Types.h +++ b/src/Types.h @@ -95,6 +95,14 @@ public: } }; +namespace smrtlink { + +constexpr unsigned int caseArg(const char* str, int h = 0) { + return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; +} + +} + template std::vector operator+(const std::vector &A, const std::vector &B) { std::vector AB; @@ -117,6 +125,7 @@ struct Options { bool JSON; bool PLAIN; bool REVERSE; + bool HEADER; bool PERMANENT; bool WAIT; @@ -128,7 +137,7 @@ struct Options { std::string file; int debug_level = 0; int verbosity = 0; - long timeout = 180U; + long timeout = 250U; }; #endif /* TYPES_H_ */ diff --git a/src/lookup/input.lst b/src/lookup/input.lst index d673846..5c48cff 100644 --- a/src/lookup/input.lst +++ b/src/lookup/input.lst @@ -1,11 +1,13 @@ -LOOKUP_SET(TYPE,type, 1, STRING) //+string +LOOKUP_SET(type, TYPE, STRING) //+string +LOOKUP_SET(mac, MAC, HEX) //+byte[6] +LOOKUP_SET(firmware,FIRMWARE_VERSION, STRING) +LOOKUP_SET(hardware,HARDWARE_VERSION, STRING) + + LOOKUP_SET(HOSTNAME,host, 2, STRING) //+string -LOOKUP_SET(MAC,MAC, 3, HEX) //+byte[6] LOOKUP_SET(IP_ADDR,ip, 4, DEC) //+byte[4] LOOKUP_SET(IP_MASK,mask, 5, DEC) //+byte[4] LOOKUP_SET(GATEWAY,gateway, 6, DEC) //+byte[4] -LOOKUP_SET(FIRMWARE_VERSION,FIRMWARE_VERSION, 7, STRING) -LOOKUP_SET(HARDWARE_VERSION,HARDWARE_VERSION, 8, STRING) LOOKUP_SET(DHCP_ENABLED,dhcp, 9, BOOL) //+bool byte LOOKUP_SET(PORT_VLAN_FOOOOO, 2101,2101, HEX) diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 73462ba..cf364ef 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -1,14 +1,13 @@ //============================================================================ // Name : smrtlink.cpp // Author : jdi -// Version : +// Version : 1.2 // Copyright : GPL v2 // Description : SmrtLink in C++, Ansi-style //============================================================================ #include #include -#include #include #include @@ -21,6 +20,7 @@ #include "Interactive.h" #include "Host.h" #include "Program.h" +#include "Types.h" #include "Switch.h" #include "lookup.h" @@ -32,12 +32,8 @@ using namespace std; Options options; -constexpr unsigned int caseArg(const char* str, int h = 0) { - return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; -} - int main(int argc, char *argv[]) { - int index,opt; + int index, opt; options.user = DEFAULT_USER; options.password = DEFAULT_PASS; @@ -54,8 +50,6 @@ int main(int argc, char *argv[]) { 0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; - - while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts, &index)) != -1) { switch (opt) { @@ -151,116 +145,20 @@ int main(int argc, char *argv[]) { if (options.flags.INTERACTIVE) { if (optind < argc) { - cerr << "Command is ignored in interactive mode\n"; - } + cerr << "Command is ignored in interactive mode\n"; + } Interactive p = Interactive(); - if (!p.run()) + if (!p.loop()) exit(EXIT_SUCCESS); fprintf(stderr, "Not yet implemented.\n"); exit(EXIT_FAILURE); - } - else if (optind < argc) { + } else if (optind < argc) { Program p = Program(); - p.init(); - std::vector vect; - std::map list; - std::cmatch sm; - std::string cmd = std::string(argv[optind++]); - switch (caseArg(cmd.c_str())) { - case caseArg("reboot"): - if (!p.reboot()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("reset"): - if (!p.reset()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("save"): - if (!p.save()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("restore"): - if (!p.restore()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("flash"): - if (!p.flash()) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - - case caseArg("list"): - if (!p.list()) - exit(EXIT_SUCCESS); - break; - - case caseArg("sniff"): - if (!p.sniff()) - exit(EXIT_SUCCESS); - break; - - case caseArg("encode"): - if (optind < argc) { - std::string s(argv[optind]); - optind++; - if (p.encode(s)) - exit(EXIT_SUCCESS); - } else { - fprintf(stderr, "Argument expected after encode\n"); - exit(EXIT_FAILURE); - } - break; - case caseArg("set"): - while (optind < argc) { - if (regex_match(argv[optind], sm, - std::regex("^([a-z]+)=(.*)$"))) { - if (!snd_lookup.exists(sm[1])&&!rcv_lookup.exists(sm[1])) { - cerr << "Unknown argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - list.insert( - std::pair(sm[1], sm[2])); - } else { - cerr << "Invalid Syntax " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - optind++; - } - if (!p.setProperty(list)) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("get"): - while (optind < argc) { - if (regex_match(argv[optind], sm, std::regex("^([a-z]+)$"))) { - if (!snd_lookup.exists(sm[1])&&!rcv_lookup.exists(sm[1])) { - cerr << "Unknown argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - vect.push_back(sm[1]); - } else { - cerr << "Invalid argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - optind++; - } - if (!p.getProperty(vect)) - exit(EXIT_SUCCESS); - break; - default: - printf("Unknown command: %s\n", cmd.c_str()); - exit(EXIT_FAILURE); - } + p.init(); + vector v; + while (optind < argc) + v.push_back(argv[optind++]); + p.run(v); } exit(EXIT_FAILURE); } From 49ab8a307ee36238e44486a635ef7e19d04970e7 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 6 Feb 2016 02:35:47 +0100 Subject: [PATCH 11/17] interactive shell wip --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 36f6447..cd00218 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,10 @@ Command-Line Tool which might in the future be able to configure TP-Link Easy Sm The usage is loosely based on the swconfig utility. ### dependencys - libboost-filesystem-dev - libboost-system-dev + libboost-filesystem + libboost-system + libboost-program-options + libreadline ### compatible devices + TL-SG105E From a90401ddeb7adad90fca3abe9ac309930970d4e3 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 23 Feb 2016 12:24:49 +0100 Subject: [PATCH 12/17] trying out stuff --- src/Filter.cpp | 14 ++++++++++ src/Filter.h | 16 +++++++++++ src/Interactive.cpp | 41 +++++++++++++++------------ src/Interactive.h | 7 +++-- src/Program.cpp | 8 ++---- src/Program.h | 8 +++--- src/Socket.cpp | 68 ++++++++++++++++++++++----------------------- src/Socket.h | 44 ++++++++++++++++------------- src/smrtlink.cpp | 6 ++-- 9 files changed, 125 insertions(+), 87 deletions(-) create mode 100644 src/Filter.cpp create mode 100644 src/Filter.h diff --git a/src/Filter.cpp b/src/Filter.cpp new file mode 100644 index 0000000..df77b5c --- /dev/null +++ b/src/Filter.cpp @@ -0,0 +1,14 @@ +/* + * Filter.cpp + * + * Created on: 23.02.2016 + * Author: jedi + */ + +#include "Filter.h" + +Filter::Filter() { + // TODO Auto-generated constructor stub + +} + diff --git a/src/Filter.h b/src/Filter.h new file mode 100644 index 0000000..cbd7ee9 --- /dev/null +++ b/src/Filter.h @@ -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_ */ diff --git a/src/Interactive.cpp b/src/Interactive.cpp index 1c35283..50a3bbc 100644 --- a/src/Interactive.cpp +++ b/src/Interactive.cpp @@ -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 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(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(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 v) { + + Program p = Program(); + + p.init(); + + p.run(v); + + return 0; +} + diff --git a/src/Interactive.h b/src/Interactive.h index 13547f3..d1d974a 100644 --- a/src/Interactive.h +++ b/src/Interactive.h @@ -10,9 +10,12 @@ class Interactive { public: - Interactive(); - virtual ~Interactive(); + Interactive() { + } + virtual ~Interactive() { + } int loop(); + int single(std::vector v); }; #endif /* INTERACTIVE_H_ */ diff --git a/src/Program.cpp b/src/Program.cpp index bc3c370..ae6c279 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -34,14 +34,10 @@ int Program::run(vector 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 c) { } void Program::init() { + io_service = std::make_shared(); + sock = std::make_shared < Socket > (*io_service); if (options.interface.compare("") == 0) options.interface = host.getIface(); diff --git a/src/Program.h b/src/Program.h index d8357a0..0cb98ac 100644 --- a/src/Program.h +++ b/src/Program.h @@ -23,12 +23,12 @@ private: int set(Packet,datasets,std::function); int discover(std::function); public: - Program() { - io_service = std::make_shared(); - sock = std::make_shared(*io_service); - } + Program() {} void init(); int run(std::vector); + std::function callback = []() { + return 0; + }; int list(); int sniff(); diff --git a/src/Socket.cpp b/src/Socket.cpp index a864893..74b2a27 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -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_); + } + }); + } +} diff --git a/src/Socket.h b/src/Socket.h index 7abce0d..2904813 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -17,30 +17,34 @@ #define MAX_LENGTH 1024 +typedef std::function 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 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; }; diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index cf364ef..a264d47 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -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 v; while (optind < argc) v.push_back(argv[optind++]); - p.run(v); + p.single(v); } exit(EXIT_FAILURE); } From 507cfe214059728d6b6be4e0e5bd09383cd63309 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 23 Feb 2016 15:54:56 +0100 Subject: [PATCH 13/17] trying out stuff --- src/Filter.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- src/Filter.h | 14 ++++++++++++++ src/Interactive.cpp | 17 ----------------- src/Packet.h | 21 +++++++++++---------- src/Program.cpp | 13 ++++++------- src/Socket.cpp | 30 +++++++++++++++++++++++++----- src/Socket.h | 15 +++++++-------- src/Types.h | 14 ++++++++++++++ 8 files changed, 118 insertions(+), 49 deletions(-) diff --git a/src/Filter.cpp b/src/Filter.cpp index df77b5c..24e7eee 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -8,7 +8,46 @@ #include "Filter.h" Filter::Filter() { - // TODO Auto-generated constructor stub - +} +Filter::Filter(macAddr a) { + switchMac = a; +} +Filter::Filter(Packet::OpCode o) { + oc = o; } +Filter Filter::opcode(Packet::OpCode o) { + oc = o; + return *this; +} +Filter Filter::mac(macAddr a) { + switchMac = a; + return *this; +} +Filter Filter::tokenid(short i) { + tokenId = i; + return *this; +} + +bool Filter::pass(const Packet p) const { + macAddr none { 0, 0, 0, 0, 0, 0 }; + if (this->oc != Packet::NONE && this->oc != p.getOpCode()) + return false; + if (this->switchMac != none && this->switchMac != p.getSwitchMac()) + return false; + if (this->tokenId != -1 && this->tokenId != p.getTokenId()) + return false; + /* if (fragmentOffset != p.getF && fragmentOffset != -1) + return false;*/ + return true; +} + +bool Filter::operator<(const Filter f) const { + if (this->oc != f.oc) + return this->oc < f.oc; + if (this->switchMac != f.switchMac) + return this->switchMac < f.switchMac; + if (this->tokenId < f.tokenId) + return this->tokenId < f.tokenId; + return false; +} diff --git a/src/Filter.h b/src/Filter.h index cbd7ee9..a9d9213 100644 --- a/src/Filter.h +++ b/src/Filter.h @@ -8,9 +8,23 @@ #ifndef FILTER_H_ #define FILTER_H_ +#include "Packet.h" + class Filter { public: Filter(); + Filter(Packet::OpCode); + Filter(macAddr); + Filter opcode(Packet::OpCode); + Filter mac(macAddr); + Filter tokenid(short); + bool pass(const Packet) const; + bool operator<(const Filter) const; +private: + Packet::OpCode oc = Packet::NONE; + macAddr switchMac = { 0, 0, 0, 0, 0, 0 }; + short tokenId = -1; + short fragmentOffset = -1; }; #endif /* FILTER_H_ */ diff --git a/src/Interactive.cpp b/src/Interactive.cpp index 50a3bbc..19ab930 100644 --- a/src/Interactive.cpp +++ b/src/Interactive.cpp @@ -22,14 +22,8 @@ using namespace std; int Interactive::loop() { string cmd; vector v; - //vector vc; - //const char** argv; - //int argc; - Program p = Program(); - p.init(); - while (1) { cmd = readline("smrtlink> "); if (!cmd.compare("quit") || !cmd.compare("q")) @@ -37,27 +31,16 @@ int Interactive::loop() { if (!cmd.empty()) { add_history(cmd.c_str()); v = boost::program_options::split_unix(cmd); - - //vc = vector(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); - } - } return 0; } int Interactive::single(vector v) { - Program p = Program(); - p.init(); - p.run(v); - return 0; } diff --git a/src/Packet.h b/src/Packet.h index 395c86d..a34e1c9 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -13,12 +13,13 @@ #include "Types.h" -static short sequenceId=0; +static short sequenceId = 0; -class Packet -{ +class Packet { public: - enum OpCode {DISCOVERY, GET, REPLY, SET, CONFIRM, NONE}; + enum OpCode { + DISCOVERY, GET, REPLY, SET, CONFIRM, NONE + }; Packet(OpCode); static void encode(bytes&); bytes getBytes(); @@ -37,7 +38,7 @@ public: void setHostMac(macAddr); void setSwitchMac(macAddr); void setCheckSum(int); - void setSequenceId(short ); + void setSequenceId(short); void setPayload(datasets payload); short getTokenId() const; void setTokenId(short tokenId = 0); @@ -50,9 +51,9 @@ private: datasets payload; byte version = 1; byte opCode; - macAddr switchMac { {0, 0, 0, 0, 0, 0}}; - macAddr hostMac { {0, 0, 0, 0, 0, 0}}; - macAddr broadcastMac { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + macAddr switchMac { { 0, 0, 0, 0, 0, 0 } }; + macAddr hostMac { { 0, 0, 0, 0, 0, 0 } }; + macAddr broadcastMac { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } }; short tokenId = 0; short fragmentOffset = 0; int errorCode = 0; @@ -60,7 +61,7 @@ private: short flag = 0; void buildHead(); void buildBody(); - void push(bytes&, int&, short ); + void push(bytes&, int&, short); void push(bytes&, int&, int); void push(bytes&, int&, byte); void push(bytes&, int&, bytes); @@ -70,7 +71,7 @@ private: void pull(bytes&, int&, short &); void pull(bytes&, int&, int&); void pull(bytes&, int&, byte&); - void pull(bytes&, int&, bytes&, unsigned ); + void pull(bytes&, int&, bytes&, unsigned); void pull(bytes&, int&, ipAddr&); void pull(bytes&, int&, macAddr&); void pull(bytes&, int&, dataset&); diff --git a/src/Program.cpp b/src/Program.cpp index ae6c279..d4ce237 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -17,6 +17,7 @@ #include "Socket.h" #include "Switch.h" #include "Packet.h" +#include "Filter.h" #include "Types.h" #include "lookup.h" #include "table.h" @@ -220,18 +221,16 @@ int Program::sniff() { Socket s(io_service); s.setHostIp(host.getIp()); s.init(DST_PORT, SRC_PORT); - s.callback = [](Packet p) { + s.listen([](Packet p) { cout << p.opCodeToString() << "\n"; printHeader(p); printPacket(p); return 0; - }; - s.receive(); + }); io_service.run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } - return 0; } @@ -406,7 +405,7 @@ int Program::discover(function c) { Packet p = Packet(Packet::DISCOVERY); p.setHostMac(host.getMac()); p.setPayload( { }); - sock->callback = c; + sock->listen(c,Filter(Packet::REPLY)); sock->send(p); return 0; } @@ -416,7 +415,7 @@ int Program::get(Packet l, datasets t, function c) { p.setSwitchMac(l.getSwitchMac()); p.setHostMac(host.getMac()); p.setPayload(t); - sock->callback = c; + sock->listen(c,Filter(Packet::REPLY)); sock->send(p); return 0; } @@ -431,7 +430,7 @@ int Program::set(Packet l, datasets t, function c) { datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD, (short) (w.size()), w } }; p.setPayload(ld + t); - sock->callback = c; + sock->listen(c,Filter(Packet::CONFIRM)); sock->send(p); return 0; } diff --git a/src/Socket.cpp b/src/Socket.cpp index 74b2a27..97b3596 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -5,11 +5,14 @@ * Author: jdi */ #include -#include + +#include + #include #include "Socket.h" #include "Packet.h" #include "Constant.h" +#include "Filter.h" #include "Host.h" #include "Types.h" @@ -17,6 +20,10 @@ Socket::Socket(boost::asio::io_service& io_service) : send_socket_(io_service), receive_socket_(io_service), timer(io_service) { } //, resolver( io_service) + +Socket::~Socket() { +} + void Socket::init(short dst_port, short src_port) { if (options.flags.REVERSE) { short p = dst_port; @@ -50,6 +57,15 @@ void Socket::setHostIp(ipAddr ip) { local_ip = ip; } +void Socket::listen(Listener l, Filter f) { + if (callback.find(f) == callback.end()) { + callback.insert(ListenerPair(f, l)); + } else { + callback[f] = l; + } + receive(); +} + void Socket::send(Packet p) { bytes data = p.getBytes(); p.encode(data); @@ -76,11 +92,15 @@ void Socket::receive() { data.resize(bytes_recvd); Packet p = Packet(Packet::NONE); p.encode(data); - // std::cout << "err" << p.getErrorCode() < #include #include "Packet.h" +#include "Filter.h" #include "Types.h" #define SRC_PORT 29809 @@ -18,22 +20,19 @@ #define MAX_LENGTH 1024 typedef std::function Listener; +typedef std::pair ListenerPair; class Socket { public: Socket(boost::asio::io_service&); - virtual ~Socket() { - } + virtual ~Socket(); void init(short, short); void send(Packet); void setHostIp(ipAddr); - void listen(); - Listener callback = [](Packet a) { - return 0; - }; - void receive(); + void listen(Listener l, Filter f = Filter()); private: + void receive(); void settimeout(); boost::asio::ip::udp::socket send_socket_; boost::asio::ip::udp::socket receive_socket_; @@ -44,7 +43,7 @@ private: boost::asio::deadline_timer timer; bytes data = bytes(MAX_LENGTH); ipAddr local_ip; - int initialized = 0; + std::map callback = { }; }; diff --git a/src/Types.h b/src/Types.h index e236c10..1d0a6c5 100644 --- a/src/Types.h +++ b/src/Types.h @@ -50,6 +50,20 @@ public: else break; } } + + bool operator==(const macAddr &A) { + for (unsigned i = 0; i < 6; i++) { + if(A[i]!=(*this)[i])return false; + } + return true; + } + + bool operator!=(const macAddr &A) { + for (unsigned i = 0; i < 6; i++) { + if(A[i]!=(*this)[i])return true; + } + return false; + } }; /* From e494a7db916131447a516cca40dcd7cfc6474594 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 23 Feb 2016 20:51:11 +0100 Subject: [PATCH 14/17] trying out stuff --- src/Filter.cpp | 8 ++-- src/Program.cpp | 100 ++++++++++++++++++++++-------------------------- src/Program.h | 40 ++++++++++--------- src/Socket.cpp | 6 +++ src/Socket.h | 1 + src/Types.h | 8 ++++ 6 files changed, 87 insertions(+), 76 deletions(-) diff --git a/src/Filter.cpp b/src/Filter.cpp index 24e7eee..cb5f87d 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -44,10 +44,10 @@ bool Filter::pass(const Packet p) const { bool Filter::operator<(const Filter f) const { if (this->oc != f.oc) - return this->oc < f.oc; + return this->oc > f.oc; if (this->switchMac != f.switchMac) - return this->switchMac < f.switchMac; - if (this->tokenId < f.tokenId) - return this->tokenId < f.tokenId; + return this->switchMac > f.switchMac; + if (this->tokenId != f.tokenId) + return this->tokenId > f.tokenId; return false; } diff --git a/src/Program.cpp b/src/Program.cpp index d4ce237..9f8a0a2 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -31,50 +31,47 @@ int Program::run(vector arg) { std::map ll; std::cmatch sm; std::string cmd = arg[optind++]; + + io_service->reset(); + sock->clear(); switch (caseArg(cmd.c_str())) { case caseArg("reboot"): - if (!reboot()) - return 0; + if (reboot()) + return 1; break; case caseArg("reset"): if (!reset()) return 0; break; case caseArg("save"): - if (!save()) - return 0; - fprintf(stderr, "Not yet implemented.\n"); - return 1; + if (save()) + return 1; break; case caseArg("restore"): - if (!restore()) - return 0; - fprintf(stderr, "Not yet implemented.\n"); - return 1; + if (restore()) + return 1; break; case caseArg("flash"): - if (!flash()) - return 0; - fprintf(stderr, "Not yet implemented.\n"); - return 1; + if (flash()) + return 1; break; case caseArg("list"): - if (!list()) - return 0; + if (list()) + return 1; break; case caseArg("sniff"): - if (!sniff()) - return 0; + if (sniff()) + return 1; break; case caseArg("encode"): if (optind < arg.size()) { std::string s(arg[optind]); optind++; - if (encode(s)) - return 0; + if (!encode(s)) + return 1; } else { fprintf(stderr, "Argument expected after encode\n"); return 1; @@ -95,10 +92,8 @@ int Program::run(vector arg) { } optind++; } - if (!setProperty(ll)) - return 0; - fprintf(stderr, "Not yet implemented.\n"); - return 1; + if (setProperty(ll)) + return 1; break; case caseArg("get"): while (optind < arg.size()) { @@ -115,14 +110,15 @@ int Program::run(vector arg) { } optind++; } - if (!getProperty(vect)) - return 0; + if (getProperty(vect)) + return 1; break; default: printf("Unknown command: %s\n", cmd.c_str()); return 1; } - + io_service->run(); + return 0; } int printHeader(Packet p) { @@ -199,15 +195,18 @@ int Program::list() { cout <<"Received Payload:\n"<(b,Switch())); + devices[a.getSwitchMac().hash()].parse(d); + devices[a.getSwitchMac().hash()].print(); + File f; - f.write(sw.toString()); - sw.print(); + f.write(devices[a.getSwitchMac().hash()].toString()); } return 0; }); - io_service->run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -217,17 +216,12 @@ int Program::list() { int Program::sniff() { printf("Listening:\n"); try { - boost::asio::io_service io_service; - Socket s(io_service); - s.setHostIp(host.getIp()); - s.init(DST_PORT, SRC_PORT); - s.listen([](Packet p) { + sock->listen([](Packet p) { cout << p.opCodeToString() << "\n"; printHeader(p); printPacket(p); return 0; }); - io_service.run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -284,7 +278,6 @@ int Program::setProperty(map prop) { return 0; }); - io_service->run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -314,7 +307,6 @@ int Program::getProperty(vector prop) { return 0; }); - io_service->run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -347,17 +339,17 @@ int Program::reboot() { discover( [this](Packet a) { datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); - cout <(b,Switch())); + devices[a.getSwitchMac().hash()].parse(d); datasets t = { {SND_PING, 0, {}}}; get(a, t, [this](Packet a) { datasets d =a.getPayload(); - Switch sw = Switch(); - sw.parse(d); + cout <run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -394,33 +385,33 @@ int Program::reset() { }); return 0; }); - io_service->run(); } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } return 0; } -int Program::discover(function c) { +int Program::discover(Listener c) { Packet p = Packet(Packet::DISCOVERY); p.setHostMac(host.getMac()); p.setPayload( { }); - sock->listen(c,Filter(Packet::REPLY)); + sock->listen(c, Filter(Packet::REPLY)); sock->send(p); return 0; } -int Program::get(Packet l, datasets t, function c) { +int Program::get(Packet l, datasets t, Listener c) { Packet p = Packet(Packet::GET); p.setSwitchMac(l.getSwitchMac()); p.setHostMac(host.getMac()); p.setPayload(t); - sock->listen(c,Filter(Packet::REPLY)); + //cout << "listen REPLY from "<listen(c, Filter(Packet::REPLY).mac(l.getSwitchMac())); sock->send(p); return 0; } -int Program::set(Packet l, datasets t, function c) { +int Program::set(Packet l, datasets t, Listener c) { Packet p = Packet(Packet::SET); p.setSwitchMac(l.getSwitchMac()); p.setTokenId(l.getTokenId()); @@ -430,7 +421,8 @@ int Program::set(Packet l, datasets t, function c) { datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD, (short) (w.size()), w } }; p.setPayload(ld + t); - sock->listen(c,Filter(Packet::CONFIRM)); + //cout << "listen CONFIRM from "<listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac())); sock->send(p); return 0; } diff --git a/src/Program.h b/src/Program.h index 0cb98ac..b039623 100644 --- a/src/Program.h +++ b/src/Program.h @@ -9,38 +9,42 @@ #define PROGRAM_H_ #include +#include #include "Types.h" #include "Host.h" +#include "Switch.h" #include "Socket.h" class Program { private: - std::shared_ptr io_service; - std::shared_ptr sock; - Host host = Host(); - int get(Packet,datasets,std::function); - int set(Packet,datasets,std::function); + std::shared_ptr io_service; + std::shared_ptr sock; + Host host = Host(); + std::map devices; + int get(Packet, datasets, std::function); + int set(Packet, datasets, std::function); int discover(std::function); public: - Program() {} - void init(); + Program() { + } + void init(); int run(std::vector); std::function callback = []() { return 0; }; - int list(); - int sniff(); - int encode(std::string); - int getProperty(std::vector); - int setProperty(std::map); - int save(); - int restore(); - int flash(); - int reboot(); - int reset(); - std::string input; + int list(); + int sniff(); + int encode(std::string); + int getProperty(std::vector); + int setProperty(std::map); + int save(); + int restore(); + int flash(); + int reboot(); + int reset(); + std::string input; }; #endif /* PROGRAM_H_ */ diff --git a/src/Socket.cpp b/src/Socket.cpp index 97b3596..27eedd8 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -16,6 +16,8 @@ #include "Host.h" #include "Types.h" +using namespace std; + Socket::Socket(boost::asio::io_service& io_service) : send_socket_(io_service), receive_socket_(io_service), timer(io_service) { } @@ -57,6 +59,10 @@ void Socket::setHostIp(ipAddr ip) { local_ip = ip; } +void Socket::clear(){ + callback.clear(); +} + void Socket::listen(Listener l, Filter f) { if (callback.find(f) == callback.end()) { callback.insert(ListenerPair(f, l)); diff --git a/src/Socket.h b/src/Socket.h index 9eba7f7..0b72ec2 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -27,6 +27,7 @@ public: Socket(boost::asio::io_service&); virtual ~Socket(); void init(short, short); + void clear(); void send(Packet); void setHostIp(ipAddr); void listen(Listener l, Filter f = Filter()); diff --git a/src/Types.h b/src/Types.h index 1d0a6c5..b27657d 100644 --- a/src/Types.h +++ b/src/Types.h @@ -51,6 +51,14 @@ public: } } + int hash() { + int ret=0; + for (unsigned i = 0; i < 6; i++) { + ret = (ret*33) ^ (*this)[i]; + } + return ret; + } + bool operator==(const macAddr &A) { for (unsigned i = 0; i < 6; i++) { if(A[i]!=(*this)[i])return false; From 69198cd6feec6ed87caea6584ba792a0a58839ac Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 23 Feb 2016 21:43:35 +0100 Subject: [PATCH 15/17] 1.2 --- src/Program.cpp | 3 --- src/Socket.cpp | 2 -- 2 files changed, 5 deletions(-) diff --git a/src/Program.cpp b/src/Program.cpp index 9f8a0a2..d14c7d1 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -277,7 +277,6 @@ int Program::setProperty(map prop) { }); return 0; }); - } catch (exception& e) { cerr << "Exception: " << e.what() << "\n"; } @@ -405,7 +404,6 @@ int Program::get(Packet l, datasets t, Listener c) { p.setSwitchMac(l.getSwitchMac()); p.setHostMac(host.getMac()); p.setPayload(t); - //cout << "listen REPLY from "<listen(c, Filter(Packet::REPLY).mac(l.getSwitchMac())); sock->send(p); return 0; @@ -421,7 +419,6 @@ int Program::set(Packet l, datasets t, Listener c) { datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD, (short) (w.size()), w } }; p.setPayload(ld + t); - //cout << "listen CONFIRM from "<listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac())); sock->send(p); return 0; diff --git a/src/Socket.cpp b/src/Socket.cpp index 27eedd8..f6d6282 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -5,9 +5,7 @@ * Author: jdi */ #include - #include - #include #include "Socket.h" #include "Packet.h" From f15abc71be109e41033e48623a20a1a3f2c61829 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 25 Feb 2016 01:47:14 +0100 Subject: [PATCH 16/17] lookup clean up --- src/Program.cpp | 20 ++++++++--------- src/Switch.cpp | 4 ++-- src/lookup.h | 28 +++++------------------- src/lookup/general.lst | 2 ++ src/lookup/input.lst | 38 -------------------------------- src/lookup/map.lst | 49 ++++++++++++++++++++++++++++++++++++++++++ src/table.cpp | 1 - src/table.h | 16 +++++++++----- 8 files changed, 79 insertions(+), 79 deletions(-) delete mode 100644 src/lookup/input.lst create mode 100644 src/lookup/map.lst diff --git a/src/Program.cpp b/src/Program.cpp index d14c7d1..4cc2a9d 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -81,7 +81,7 @@ int Program::run(vector arg) { while (optind < arg.size()) { if (regex_match(arg[optind].c_str(), sm, std::regex("^([a-z]+)=(.*)$"))) { - if (!snd_lookup.exists(sm[1]) && !rcv_lookup.exists(sm[1])) { + if (!lookup.exists(sm[1])) { cerr << "Unknown argument " << arg[optind] << endl; return 1; } @@ -99,7 +99,7 @@ int Program::run(vector arg) { while (optind < arg.size()) { if (regex_match(arg[optind].c_str(), sm, std::regex("^([a-z]+)$"))) { - if (!snd_lookup.exists(sm[1]) && !rcv_lookup.exists(sm[1])) { + if (!lookup.exists(sm[1])) { cerr << "Unknown argument " << arg[optind] << endl; return 1; } @@ -138,7 +138,7 @@ int printPacket(Packet p) { cout << "Received Payload:\n\t" << p.getBody() << "\n"; } else { for (dataset d : p.getPayload()) { - auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup; + //auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup; if (lookup.exists(d.type)) { table::set s = lookup[d.type]; if (d.len > 0) { @@ -243,9 +243,9 @@ int Program::setProperty(map prop) { for (auto p : prop) { string s = p.second; dataset d; - if (rcv_lookup.exists(p.first)) { - d.type = rcv_lookup.type(p.first); - if (rcv_lookup[p.first].format == table::STRING) { + if (lookup.exists(p.first)) { + d.type = lookup.type(p.first); + if (lookup[p.first].format == table::STRING) { d.len = s.size() + 1; d.value = s; data.push_back(d); @@ -260,7 +260,7 @@ int Program::setProperty(map prop) { sw.parse(d); sw.print(); - datasets t = { {SND_PING, 0, {}}}; + datasets t = { {PING, 0, {}}}; get(a, t, [this,data](Packet a) { datasets d =a.getPayload(); Switch sw = Switch(); @@ -288,7 +288,7 @@ int Program::getProperty(vector prop) { datasets data = { }; for (string s : prop) { dataset d; - d.type = snd_lookup.type(s); + d.type = lookup.type(s); data.push_back(d); } cout << "List:\n"; @@ -342,7 +342,7 @@ int Program::reboot() { if (devices.empty()||devices.find(b) == devices.end()) devices.insert(pair(b,Switch())); devices[a.getSwitchMac().hash()].parse(d); - datasets t = { {SND_PING, 0, {}}}; + datasets t = { {PING, 0, {}}}; get(a, t, [this](Packet a) { datasets d =a.getPayload(); cout < #include "table.h" - table::table(std::initializer_list l) { int i = 0; this->data.resize(l.size()); diff --git a/src/table.h b/src/table.h index 128d26e..54af936 100644 --- a/src/table.h +++ b/src/table.h @@ -13,13 +13,19 @@ class table { public: - enum F {STRING,HEX,DEC,ACTION,BOOL,EMPTY}; - struct set { - short type; - F format; + enum A { + RW, RO, WO, NONE, UNKNOWN + }; + enum F { + STRING, HEX, DEC, ACTION, BOOL, EMPTY + }; + struct set { + short type; + F format; + A action; std::string name; std::string id; - }; + }; table(std::initializer_list l); const table::set operator[](std::string); const table::set operator[](short); From 1f3730ac8728be6db5934424481f1360bd2fdf3b Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 25 Feb 2016 19:46:35 +0100 Subject: [PATCH 17/17] reformat --- src/File.cpp | 45 +++++----- src/File.h | 16 ++-- src/Filter.cpp | 5 +- src/Filter.h | 29 +++--- src/Host.cpp | 136 ++++++++++++++-------------- src/Host.h | 11 +-- src/Interactive.h | 14 +-- src/Packet.cpp | 64 +++++++------- src/Packet.h | 119 +++++++++++++------------ src/Program.cpp | 210 +++++++++++++++++++++++--------------------- src/Program.h | 54 ++++++------ src/Socket.cpp | 11 ++- src/Socket.h | 42 ++++----- src/Switch-Cmd.cpp | 4 +- src/Switch-Json.cpp | 134 ++++++++++++++-------------- src/Switch.cpp | 52 +++++------ src/Switch.h | 126 +++++++++++++------------- src/Types.h | 194 ++++++++++++++++++++-------------------- src/bytes.cpp | 38 ++++---- src/bytes.h | 106 +++++++++++----------- src/datasets.h | 40 ++++----- src/jsonNode.cpp | 54 ++++++------ src/jsonNode.h | 36 ++++---- src/smrtlink.cpp | 118 +++++++++++++------------ src/table.cpp | 14 +-- src/table.h | 52 +++++------ 26 files changed, 880 insertions(+), 844 deletions(-) diff --git a/src/File.cpp b/src/File.cpp index 0e6e944..899946e 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -10,31 +10,32 @@ #include "Constant.h" std::string File::read() { - if (!fs::exists(home)) { - fs::create_directory(home); - } + if (!fs::exists(home)) { + fs::create_directory(home); + } - fs::ifstream in(((options.file=="")?home / DEFAULT_FILE:options.file), std::ios::in | std::ios::binary); - if (in) { - std::string contents; - in.seekg(0, std::ios::end); - contents.resize(in.tellg()); - in.seekg(0, std::ios::beg); - in.read(&contents[0], contents.size()); - in.close(); - return (contents); - } - return ""; + fs::ifstream in(((options.file == "") ? home / DEFAULT_FILE : options.file), + std::ios::in | std::ios::binary); + if (in) { + std::string contents; + in.seekg(0, std::ios::end); + contents.resize(in.tellg()); + in.seekg(0, std::ios::beg); + in.read(&contents[0], contents.size()); + in.close(); + return (contents); + } + return ""; } int File::write(std::string content) { - if (!fs::exists(home)) { - fs::create_directory(home); - } - fs::path p = (options.file=="")?home / DEFAULT_FILE:options.file; - fs::ofstream file(p); - file << content<<"\n"; - file.close(); - return 0; + if (!fs::exists(home)) { + fs::create_directory(home); + } + fs::path p = (options.file == "") ? home / DEFAULT_FILE : options.file; + fs::ofstream file(p); + file << content << "\n"; + file.close(); + return 0; } diff --git a/src/File.h b/src/File.h index 5a39bc0..4aa955d 100644 --- a/src/File.h +++ b/src/File.h @@ -17,14 +17,14 @@ namespace fs = boost::filesystem; #define DEFAULT_FILE "config.json" class File { -public: - File() { - home = fs::path(getenv("HOME")) / ".smrtlink"; - } - std::string read(); - int write(std::string); -private: - fs::path home; + public: + File() { + home = fs::path(getenv("HOME")) / ".smrtlink"; + } + std::string read(); + int write(std::string); + private: + fs::path home; }; #endif /* FILE_H_ */ diff --git a/src/Filter.cpp b/src/Filter.cpp index cb5f87d..421b407 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -30,7 +30,8 @@ Filter Filter::tokenid(short i) { } bool Filter::pass(const Packet p) const { - macAddr none { 0, 0, 0, 0, 0, 0 }; + macAddr none { + 0, 0, 0, 0, 0, 0 }; if (this->oc != Packet::NONE && this->oc != p.getOpCode()) return false; if (this->switchMac != none && this->switchMac != p.getSwitchMac()) @@ -44,7 +45,7 @@ bool Filter::pass(const Packet p) const { bool Filter::operator<(const Filter f) const { if (this->oc != f.oc) - return this->oc > f.oc; + return this->oc > f.oc; if (this->switchMac != f.switchMac) return this->switchMac > f.switchMac; if (this->tokenId != f.tokenId) diff --git a/src/Filter.h b/src/Filter.h index a9d9213..9bf7650 100644 --- a/src/Filter.h +++ b/src/Filter.h @@ -11,20 +11,21 @@ #include "Packet.h" class Filter { -public: - Filter(); - Filter(Packet::OpCode); - Filter(macAddr); - Filter opcode(Packet::OpCode); - Filter mac(macAddr); - Filter tokenid(short); - bool pass(const Packet) const; - bool operator<(const Filter) const; -private: - Packet::OpCode oc = Packet::NONE; - macAddr switchMac = { 0, 0, 0, 0, 0, 0 }; - short tokenId = -1; - short fragmentOffset = -1; + public: + Filter(); + Filter(Packet::OpCode); + Filter(macAddr); + Filter opcode(Packet::OpCode); + Filter mac(macAddr); + Filter tokenid(short); + bool pass(const Packet) const; + bool operator<(const Filter) const; + private: + Packet::OpCode oc = Packet::NONE; + macAddr switchMac = { + 0, 0, 0, 0, 0, 0 }; + short tokenId = -1; + short fragmentOffset = -1; }; #endif /* FILTER_H_ */ diff --git a/src/Host.cpp b/src/Host.cpp index 1fbba26..db26e9e 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -26,77 +26,79 @@ #include "Types.h" macAddr Host::getMac() { - int s; - struct ifreq buffer; - macAddr data { 0, 0, 0, 0, 0, 0 }; - if (options.interface != "") { - s = socket(PF_INET, SOCK_DGRAM, 0); - memset(&buffer, 0x00, sizeof(buffer)); - strcpy(buffer.ifr_name, options.interface.c_str()); - ioctl(s, SIOCGIFHWADDR, &buffer); - close(s); - memcpy(&data[0], &buffer.ifr_hwaddr.sa_data[0], 6); - } - return data; + int s; + struct ifreq buffer; + macAddr data { + 0, 0, 0, 0, 0, 0 }; + if (options.interface != "") { + s = socket(PF_INET, SOCK_DGRAM, 0); + memset(&buffer, 0x00, sizeof(buffer)); + strcpy(buffer.ifr_name, options.interface.c_str()); + ioctl(s, SIOCGIFHWADDR, &buffer); + close(s); + memcpy(&data[0], &buffer.ifr_hwaddr.sa_data[0], 6); + } + return data; } ipAddr Host::getIp() { - struct ifaddrs *ifaddr, *ifa; - int n; - ipAddr data { 0, 0, 0, 0 }; - if (getifaddrs(&ifaddr) == -1) { - perror("getifaddrs"); - exit(EXIT_FAILURE); - } - for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { - if (ifa->ifa_addr == NULL) - continue; - if (ifa->ifa_addr->sa_family == AF_INET) - if (options.interface.compare(ifa->ifa_name) == 0) { - memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); - return data; - } - } - /* - for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { - if (ifa->ifa_addr == NULL) - continue; - if (ifa->ifa_addr->sa_family == AF_INET) - if (getIface().compare(ifa->ifa_name) == 0) { - memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); - return data; - } - } - */ - freeifaddrs(ifaddr); - return data; + struct ifaddrs *ifaddr, *ifa; + int n; + ipAddr data { + 0, 0, 0, 0 }; + if (getifaddrs(&ifaddr) == -1) { + perror("getifaddrs"); + exit(EXIT_FAILURE); + } + for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { + if (ifa->ifa_addr == NULL) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) + if (options.interface.compare(ifa->ifa_name) == 0) { + memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); + return data; + } + } + /* + for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { + if (ifa->ifa_addr == NULL) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) + if (getIface().compare(ifa->ifa_name) == 0) { + memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); + return data; + } + } + */ + freeifaddrs(ifaddr); + return data; } std::string Host::getIface() { - std::string defaultInterface; - std::ifstream routeFile("/proc/net/route", std::ios_base::in); - if (!routeFile.good()) - return ""; - std::string line; - std::vector tokens; - if (std::getline(routeFile, line)) - while (std::getline(routeFile, line)) { - std::istringstream stream(line); - std::string delimiter = "\t"; - size_t pos = 0; - std::string token; - while ((pos = line.find(delimiter)) != std::string::npos) { - token = line.substr(0, pos); - tokens.push_back(token); - line.erase(0, pos + delimiter.length()); - } - if ((tokens.size() >= 2) - && (tokens[1] == std::string("00000000"))) { - defaultInterface = tokens[0]; - break; - } - tokens.clear(); - } - routeFile.close(); - return defaultInterface; + std::string defaultInterface; + std::ifstream routeFile("/proc/net/route", std::ios_base::in); + if (!routeFile.good()) + return ""; + std::string line; + std::vector tokens; + if (std::getline(routeFile, line)) + while (std::getline(routeFile, line)) { + std::istringstream stream(line); + std::string delimiter = "\t"; + size_t pos = 0; + std::string token; + while ((pos = line.find(delimiter)) != std::string::npos) { + token = line.substr(0, pos); + tokens.push_back(token); + line.erase(0, pos + delimiter.length()); + } + if ((tokens.size() >= 2) + && (tokens[1] == std::string("00000000"))) { + defaultInterface = tokens[0]; + break; + } + tokens.clear(); + } + routeFile.close(); + return defaultInterface; } diff --git a/src/Host.h b/src/Host.h index 3e7f09f..6c5ee15 100644 --- a/src/Host.h +++ b/src/Host.h @@ -11,11 +11,12 @@ #include "Types.h" class Host { -public: - Host(){}; - macAddr getMac(); - ipAddr getIp(); - std::string getIface(); + public: + Host() { + } + macAddr getMac(); + ipAddr getIp(); + std::string getIface(); }; #endif /* HOST_H_ */ diff --git a/src/Interactive.h b/src/Interactive.h index d1d974a..7f2af8c 100644 --- a/src/Interactive.h +++ b/src/Interactive.h @@ -9,13 +9,13 @@ #define INTERACTIVE_H_ class Interactive { -public: - Interactive() { - } - virtual ~Interactive() { - } - int loop(); - int single(std::vector v); + public: + Interactive() { + } + virtual ~Interactive() { + } + int loop(); + int single(std::vector v); }; #endif /* INTERACTIVE_H_ */ diff --git a/src/Packet.cpp b/src/Packet.cpp index 9bcf06c..a469cea 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -150,42 +150,43 @@ void Packet::setTokenId(short tokenId) { std::string Packet::opCodeToString() { switch (opCode) { - case DISCOVERY: - return "DISCOVERY"; - case GET: - return "GET"; - case SET: - return "SET"; - case CONFIRM: - return "CONFIRM"; - case REPLY: - return "REPLY"; - default: - return "NONE"; + case DISCOVERY: + return "DISCOVERY"; + case GET: + return "GET"; + case SET: + return "SET"; + case CONFIRM: + return "CONFIRM"; + case REPLY: + return "REPLY"; + default: + return "NONE"; } return "NONE"; } void Packet::encode(bytes &data) { int len = data.size(); - bytes key = { 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, - 76, 189, 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, - 188, 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, - 213, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, - 206, 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, - 208, 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, - 113, 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, - 75, 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, - 166, 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, - 255, 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, - 185, 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, - 50, 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, - 38, 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, - 195, 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, - 19, 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, - 65, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, - 102, 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, - 159, 221, 244, 110, 119, 48 }; + bytes key = { + 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, 76, 189, + 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, 188, + 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, 213, + 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, 206, + 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, 208, + 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, 113, + 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, 75, + 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, 166, + 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, 255, + 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, 185, + 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, 50, + 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, 38, + 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, 195, + 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, 19, + 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, 65, + 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, 102, + 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, 159, + 221, 244, 110, 119, 48 }; bytes s = key; int i, j = 0; for (int k = 0; k < len; k++) { @@ -199,7 +200,8 @@ void Packet::encode(bytes &data) { void Packet::push(bytes &arr, int &index, byte data) { if (arr.size() > (unsigned) index) { arr[index++] = data; - } else { + } + else { arr.push_back(data); index++; } diff --git a/src/Packet.h b/src/Packet.h index a34e1c9..466f01c 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -16,65 +16,68 @@ static short sequenceId = 0; class Packet { -public: - enum OpCode { - DISCOVERY, GET, REPLY, SET, CONFIRM, NONE - }; - Packet(OpCode); - static void encode(bytes&); - bytes getBytes(); - void parse(bytes); - void printHeader(); - std::string opCodeToString(); - short getLength() const; - int getCheckSum() const; - int getErrorCode() const; - short getSequenceId() const; - macAddr getSwitchMac() const; - const bytes& getBody() const; - const bytes& getHead() const; - const datasets& getPayload() const; - void setBody(bytes); - void setHostMac(macAddr); - void setSwitchMac(macAddr); - void setCheckSum(int); - void setSequenceId(short); - void setPayload(datasets payload); - short getTokenId() const; - void setTokenId(short tokenId = 0); - byte getOpCode() const; - void setOpCode(byte opCode); + public: + enum OpCode { + DISCOVERY, GET, REPLY, SET, CONFIRM, NONE + }; + Packet(OpCode); + static void encode(bytes&); + bytes getBytes(); + void parse(bytes); + void printHeader(); + std::string opCodeToString(); + short getLength() const; + int getCheckSum() const; + int getErrorCode() const; + short getSequenceId() const; + macAddr getSwitchMac() const; + const bytes& getBody() const; + const bytes& getHead() const; + const datasets& getPayload() const; + void setBody(bytes); + void setHostMac(macAddr); + void setSwitchMac(macAddr); + void setCheckSum(int); + void setSequenceId(short); + void setPayload(datasets payload); + short getTokenId() const; + void setTokenId(short tokenId = 0); + byte getOpCode() const; + void setOpCode(byte opCode); -private: - bytes head = bytes(32); - bytes body = bytes(0); - datasets payload; - byte version = 1; - byte opCode; - macAddr switchMac { { 0, 0, 0, 0, 0, 0 } }; - macAddr hostMac { { 0, 0, 0, 0, 0, 0 } }; - macAddr broadcastMac { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } }; - short tokenId = 0; - short fragmentOffset = 0; - int errorCode = 0; - int checkSum = 0; - short flag = 0; - void buildHead(); - void buildBody(); - void push(bytes&, int&, short); - void push(bytes&, int&, int); - void push(bytes&, int&, byte); - void push(bytes&, int&, bytes); - void push(bytes&, int&, ipAddr); - void push(bytes&, int&, macAddr); - void push(bytes&, int&, dataset); - void pull(bytes&, int&, short &); - void pull(bytes&, int&, int&); - void pull(bytes&, int&, byte&); - void pull(bytes&, int&, bytes&, unsigned); - void pull(bytes&, int&, ipAddr&); - void pull(bytes&, int&, macAddr&); - void pull(bytes&, int&, dataset&); + private: + bytes head = bytes(32); + bytes body = bytes(0); + datasets payload; + byte version = 1; + byte opCode; + macAddr switchMac { + { 0, 0, 0, 0, 0, 0 } }; + macAddr hostMac { + { 0, 0, 0, 0, 0, 0 } }; + macAddr broadcastMac { + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } }; + short tokenId = 0; + short fragmentOffset = 0; + int errorCode = 0; + int checkSum = 0; + short flag = 0; + void buildHead(); + void buildBody(); + void push(bytes&, int&, short); + void push(bytes&, int&, int); + void push(bytes&, int&, byte); + void push(bytes&, int&, bytes); + void push(bytes&, int&, ipAddr); + void push(bytes&, int&, macAddr); + void push(bytes&, int&, dataset); + void pull(bytes&, int&, short &); + void pull(bytes&, int&, int&); + void pull(bytes&, int&, byte&); + void pull(bytes&, int&, bytes&, unsigned); + void pull(bytes&, int&, ipAddr&); + void pull(bytes&, int&, macAddr&); + void pull(bytes&, int&, dataset&); }; #endif /* PACKET_H_ */ diff --git a/src/Program.cpp b/src/Program.cpp index 4cc2a9d..ad871d6 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -35,87 +35,91 @@ int Program::run(vector arg) { io_service->reset(); sock->clear(); switch (caseArg(cmd.c_str())) { - case caseArg("reboot"): - if (reboot()) - return 1; - break; - case caseArg("reset"): - if (!reset()) - return 0; - break; - case caseArg("save"): - if (save()) - return 1; - break; - case caseArg("restore"): - if (restore()) - return 1; - break; - case caseArg("flash"): - if (flash()) - return 1; - break; - - case caseArg("list"): - if (list()) - return 1; - break; - - case caseArg("sniff"): - if (sniff()) - return 1; - break; - - case caseArg("encode"): - if (optind < arg.size()) { - std::string s(arg[optind]); - optind++; - if (!encode(s)) + case caseArg("reboot"): + if (reboot()) return 1; - } else { - fprintf(stderr, "Argument expected after encode\n"); - return 1; - } break; - case caseArg("set"): - while (optind < arg.size()) { - if (regex_match(arg[optind].c_str(), sm, - std::regex("^([a-z]+)=(.*)$"))) { - if (!lookup.exists(sm[1])) { - cerr << "Unknown argument " << arg[optind] << endl; + case caseArg("reset"): + if (!reset()) + return 0; + break; + case caseArg("save"): + if (save()) + return 1; + break; + case caseArg("restore"): + if (restore()) + return 1; + break; + case caseArg("flash"): + if (flash()) + return 1; + break; + + case caseArg("list"): + if (list()) + return 1; + break; + + case caseArg("sniff"): + if (sniff()) + return 1; + break; + + case caseArg("encode"): + if (optind < arg.size()) { + std::string s(arg[optind]); + optind++; + if (!encode(s)) return 1; - } - ll.insert(std::pair(sm[1], sm[2])); - } else { - cerr << "Invalid Syntax " << arg[optind] << endl; + } + else { + fprintf(stderr, "Argument expected after encode\n"); return 1; } - optind++; - } - if (setProperty(ll)) - return 1; break; - case caseArg("get"): - while (optind < arg.size()) { - if (regex_match(arg[optind].c_str(), sm, - std::regex("^([a-z]+)$"))) { - if (!lookup.exists(sm[1])) { - cerr << "Unknown argument " << arg[optind] << endl; + case caseArg("set"): + while (optind < arg.size()) { + if (regex_match(arg[optind].c_str(), sm, + std::regex("^([a-z]+)=(.*)$"))) { + if (!lookup.exists(sm[1])) { + cerr << "Unknown argument " << arg[optind] << endl; + return 1; + } + ll.insert( + std::pair(sm[1], sm[2])); + } + else { + cerr << "Invalid Syntax " << arg[optind] << endl; return 1; } - vect.push_back(sm[1]); - } else { - cerr << "Invalid argument " << arg[optind] << endl; - return 1; + optind++; } - optind++; - } - if (getProperty(vect)) - return 1; + if (setProperty(ll)) + return 1; break; - default: - printf("Unknown command: %s\n", cmd.c_str()); - return 1; + case caseArg("get"): + while (optind < arg.size()) { + if (regex_match(arg[optind].c_str(), sm, + std::regex("^([a-z]+)$"))) { + if (!lookup.exists(sm[1])) { + cerr << "Unknown argument " << arg[optind] << endl; + return 1; + } + vect.push_back(sm[1]); + } + else { + cerr << "Invalid argument " << arg[optind] << endl; + return 1; + } + optind++; + } + if (getProperty(vect)) + return 1; + break; + default: + printf("Unknown command: %s\n", cmd.c_str()); + return 1; } io_service->run(); return 0; @@ -125,7 +129,8 @@ int printHeader(Packet p) { if (options.flags.HEADER) { if (options.flags.HEX) { cout << "Received Header:\n\t" << p.getHead() << "\n"; - } else { + } + else { p.printHeader(); printf("\n"); } @@ -136,48 +141,53 @@ int printHeader(Packet p) { int printPacket(Packet p) { if (options.flags.HEX) { cout << "Received Payload:\n\t" << p.getBody() << "\n"; - } else { + } + else { for (dataset d : p.getPayload()) { //auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup; if (lookup.exists(d.type)) { table::set s = lookup[d.type]; if (d.len > 0) { switch (s.format) { - case table::STRING: - cout << "+\t" << s.name << " = " << &d.value[0] << "\n"; + case table::STRING: + cout << "+\t" << s.name << " = " << &d.value[0] + << "\n"; break; - case table::BOOL: - cout << "+\t" << s.name << " = " - << (d.value[0] ? "YES" : "NO") << "\n"; + case table::BOOL: + cout << "+\t" << s.name << " = " + << (d.value[0] ? "YES" : "NO") << "\n"; break; - case table::HEX: - cout << "+\t" << s.name << " = " << d.value << "\n"; + case table::HEX: + cout << "+\t" << s.name << " = " << d.value << "\n"; break; - case table::DEC: - cout << "+\t" << s.name << " = "; - if (d.value.size() > 0) - cout << dec << (unsigned) d.value[0]; - for (unsigned i = 1; i < d.value.size(); i++) - cout << dec << "." << (unsigned) d.value[i]; - cout << "\n"; + case table::DEC: + cout << "+\t" << s.name << " = "; + if (d.value.size() > 0) + cout << dec << (unsigned) d.value[0]; + for (unsigned i = 1; i < d.value.size(); i++) + cout << dec << "." << (unsigned) d.value[i]; + cout << "\n"; break; - case table::ACTION: - cout << "Error:" << s.name - << " is marked as 'action' but carries payload." - << d.value << "\n"; + case table::ACTION: + cout << "Error:" << s.name + << " is marked as 'action' but carries payload." + << d.value << "\n"; break; - default: - cout << "+\t" << s.name << " = " << d.value << "\n"; + default: + cout << "+\t" << s.name << " = " << d.value << "\n"; break; } - } else { //empty + } + else { //empty cout << dec << ">\t" << s.name << "\n"; } - } else { //unknown id + } + else { //unknown id if (d.len > 0) { cout << "##\t" << d.type << ":\n\t"; cout << hex << d.value << dec << "\n"; - } else { //empty + } + else { //empty cout << "#>\t" << d.type << "\n"; } } @@ -193,7 +203,8 @@ int Program::list() { printHeader(a); if (options.flags.HEX) { cout <<"Received Payload:\n"<listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac())); sock->send(p); diff --git a/src/Program.h b/src/Program.h index b039623..86e08a9 100644 --- a/src/Program.h +++ b/src/Program.h @@ -17,34 +17,34 @@ #include "Socket.h" class Program { -private: - std::shared_ptr io_service; - std::shared_ptr sock; - Host host = Host(); - std::map devices; - int get(Packet, datasets, std::function); - int set(Packet, datasets, std::function); - int discover(std::function); -public: - Program() { - } - void init(); - int run(std::vector); - std::function callback = []() { - return 0; - }; + private: + std::shared_ptr io_service; + std::shared_ptr sock; + Host host = Host(); + std::map devices; + int get(Packet, datasets, std::function); + int set(Packet, datasets, std::function); + int discover(std::function); + public: + Program() { + } + void init(); + int run(std::vector); + std::function callback = []() { + return 0; + }; - int list(); - int sniff(); - int encode(std::string); - int getProperty(std::vector); - int setProperty(std::map); - int save(); - int restore(); - int flash(); - int reboot(); - int reset(); - std::string input; + int list(); + int sniff(); + int encode(std::string); + int getProperty(std::vector); + int setProperty(std::map); + int save(); + int restore(); + int flash(); + int reboot(); + int reset(); + std::string input; }; #endif /* PROGRAM_H_ */ diff --git a/src/Socket.cpp b/src/Socket.cpp index f6d6282..b1804f7 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -57,14 +57,15 @@ void Socket::setHostIp(ipAddr ip) { local_ip = ip; } -void Socket::clear(){ +void Socket::clear() { callback.clear(); } void Socket::listen(Listener l, Filter f) { if (callback.find(f) == callback.end()) { callback.insert(ListenerPair(f, l)); - } else { + } + else { callback[f] = l; } receive(); @@ -92,7 +93,8 @@ void Socket::receive() { if (ec || bytes_recvd == 0) { //listen(); // TODO distinguish error codes - } else { + } + else { data.resize(bytes_recvd); Packet p = Packet(Packet::NONE); p.encode(data); @@ -102,7 +104,8 @@ void Socket::receive() { r.second(p); // std::cout<<"pass"< Listener; typedef std::pair ListenerPair; class Socket { -public: - Socket(boost::asio::io_service&); - virtual ~Socket(); - void init(short, short); - void clear(); - void send(Packet); - void setHostIp(ipAddr); - void listen(Listener l, Filter f = Filter()); + public: + Socket(boost::asio::io_service&); + virtual ~Socket(); + void init(short, short); + void clear(); + void send(Packet); + void setHostIp(ipAddr); + void listen(Listener l, Filter f = Filter()); -private: - void receive(); - 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; - std::map callback = { }; + private: + void receive(); + 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; + std::map callback = { }; }; diff --git a/src/Switch-Cmd.cpp b/src/Switch-Cmd.cpp index 916b69e..765519f 100644 --- a/src/Switch-Cmd.cpp +++ b/src/Switch-Cmd.cpp @@ -22,8 +22,8 @@ int Switch::set(pair str) { std::string Switch::get(std::string str) { std::string ret; switch (caseArg(str.c_str())) { - case caseArg("ip"): - ret = "0.0.0.0"; + case caseArg("ip"): + ret = "0.0.0.0"; break; } return ret; diff --git a/src/Switch-Json.cpp b/src/Switch-Json.cpp index c94961c..ed7f351 100644 --- a/src/Switch-Json.cpp +++ b/src/Switch-Json.cpp @@ -14,85 +14,85 @@ int Switch::parse(std::string str) { - if (json.Parse(str.c_str()).HasParseError()) - return 1; - if (options.debug_level>=1) - std::cout << "\nParsing to document succeeded.\n"; + if (json.Parse(str.c_str()).HasParseError()) + return 1; + if (options.debug_level >= 1) + std::cout << "\nParsing to document succeeded.\n"; - if (json.IsObject()) { - if (json.HasMember("hostname")) - settings.hostname = json["hostname"].GetString(); - if (json.HasMember("type")) - device.type = json["type"].GetString(); - if (json.HasMember("hardware_version")) - device.hardware_version = json["hardware_version"].GetString(); - if (json.HasMember("firmware_version")) - device.hardware_version = json["firmware_version"].GetString(); - if (json.HasMember("ports") && json["ports"].IsArray()) { - const rapidjson::Value& a = json["ports"]; - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - if (a[i].IsObject()) { - port p; - if (a[i].HasMember("id") && a[i]["id"].IsInt()) { - p.id = a[i]["id"].GetInt(); - std::cout << a[i]["id"].GetInt() << "\n"; - } - ports.push_back(p); - } - } - if (json.HasMember("vlans") && json["vlans"].IsArray()) { - const rapidjson::Value& a = json["vlans"]; - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - if (a[i].IsObject()) { - vlan v; - if (a[i].HasMember("name") && a[i]["name"].IsString()) { - v.name = a[i]["name"].GetString(); - std::cout << a[i]["name"].GetString() << "\n"; - } - vlans.push_back(v); - } - } + if (json.IsObject()) { + if (json.HasMember("hostname")) + settings.hostname = json["hostname"].GetString(); + if (json.HasMember("type")) + device.type = json["type"].GetString(); + if (json.HasMember("hardware_version")) + device.hardware_version = json["hardware_version"].GetString(); + if (json.HasMember("firmware_version")) + device.hardware_version = json["firmware_version"].GetString(); + if (json.HasMember("ports") && json["ports"].IsArray()) { + const rapidjson::Value& a = json["ports"]; + for (rapidjson::SizeType i = 0; i < a.Size(); i++) + if (a[i].IsObject()) { + port p; + if (a[i].HasMember("id") && a[i]["id"].IsInt()) { + p.id = a[i]["id"].GetInt(); + std::cout << a[i]["id"].GetInt() << "\n"; + } + ports.push_back(p); + } + } + if (json.HasMember("vlans") && json["vlans"].IsArray()) { + const rapidjson::Value& a = json["vlans"]; + for (rapidjson::SizeType i = 0; i < a.Size(); i++) + if (a[i].IsObject()) { + vlan v; + if (a[i].HasMember("name") && a[i]["name"].IsString()) { + v.name = a[i]["name"].GetString(); + std::cout << a[i]["name"].GetString() << "\n"; + } + vlans.push_back(v); + } + } - } + } - /* + /* - { - const rapidjson::Value& a = json["a"]; - assert(a.IsArray()); - for (rapidjson::SizeType i = 0; i < a.Size(); i++) - printf("a[%d] = %d\n", i, a[i].GetInt()); + { + const rapidjson::Value& a = json["a"]; + assert(a.IsArray()); + for (rapidjson::SizeType i = 0; i < a.Size(); i++) + printf("a[%d] = %d\n", i, a[i].GetInt()); - int y = a[0].GetInt(); - (void) y; + int y = a[0].GetInt(); + (void) y; - */ - return 0; + */ + return 0; } std::string Switch::toString() { - if (!json.IsObject()) { - json.SetObject(); - } - rapidjson::Document::AllocatorType& allocator = json.GetAllocator(); + if (!json.IsObject()) { + json.SetObject(); + } + rapidjson::Document::AllocatorType& allocator = json.GetAllocator(); - json.AddMember("hostname", jsonNode(settings.hostname, json), allocator); - json.AddMember("ip", jsonNode(settings.ip_addr, json), allocator); - json.AddMember("netmask", jsonNode(settings.ip_mask, json), allocator); - json.AddMember("gateway", jsonNode(settings.gateway, json), allocator); - json.AddMember("type", jsonNode(device.type, json), allocator); - json.AddMember("hardware_version", jsonNode(device.hardware_version, json), - allocator); - json.AddMember("firmware_version", jsonNode(device.firmware_version, json), - allocator); - json.AddMember("ports", jsonNode(ports, json), allocator); - json.AddMember("vlans", jsonNode(vlans, json), allocator); + json.AddMember("hostname", jsonNode(settings.hostname, json), allocator); + json.AddMember("ip", jsonNode(settings.ip_addr, json), allocator); + json.AddMember("netmask", jsonNode(settings.ip_mask, json), allocator); + json.AddMember("gateway", jsonNode(settings.gateway, json), allocator); + json.AddMember("type", jsonNode(device.type, json), allocator); + json.AddMember("hardware_version", jsonNode(device.hardware_version, json), + allocator); + json.AddMember("firmware_version", jsonNode(device.firmware_version, json), + allocator); + json.AddMember("ports", jsonNode(ports, json), allocator); + json.AddMember("vlans", jsonNode(vlans, json), allocator); - rapidjson::StringBuffer sb; - rapidjson::PrettyWriter writer(sb); - json.Accept(writer); - return sb.GetString(); + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter writer(sb); + json.Accept(writer); + return sb.GetString(); } diff --git a/src/Switch.cpp b/src/Switch.cpp index 9fecafa..3c928bf 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -27,44 +27,44 @@ int Switch::print() { int Switch::parse(dataset d) { switch (d.type) { - case TYPE: - device.type = d.value; + case TYPE: + device.type = d.value; break; - case MAC: - device.mac = d.value; + case MAC: + device.mac = d.value; break; - case FIRMWARE_VERSION: - device.firmware_version = d.value; + case FIRMWARE_VERSION: + device.firmware_version = d.value; break; - case HARDWARE_VERSION: - device.hardware_version = d.value; + case HARDWARE_VERSION: + device.hardware_version = d.value; break; - case PORTS: - device.ports = d.value[0]; + case PORTS: + device.ports = d.value[0]; break; - case HOSTNAME: - settings.hostname = d.value; + case HOSTNAME: + settings.hostname = d.value; break; - case IP_ADDR: - settings.ip_addr = d.value; + case IP_ADDR: + settings.ip_addr = d.value; break; - case IP_MASK: - settings.ip_mask = d.value; + case IP_MASK: + settings.ip_mask = d.value; break; - case GATEWAY: - settings.gateway = d.value; + case GATEWAY: + settings.gateway = d.value; break; - case DHCP_ENABLED: - settings.dhcp = d.value[0]; + case DHCP_ENABLED: + settings.dhcp = d.value[0]; break; - case LOOP_PREVENTION: - settings.loop_prevention = d.value[0]; + case LOOP_PREVENTION: + settings.loop_prevention = d.value[0]; break; - case QOS_BASIC_ENABLED: - settings.qos_enabled = d.value[0]; + case QOS_BASIC_ENABLED: + settings.qos_enabled = d.value[0]; break; - case VLAN_ENABLED: - settings.vlan_enabled = d.value[0]; + case VLAN_ENABLED: + settings.vlan_enabled = d.value[0]; break; } return 0; diff --git a/src/Switch.h b/src/Switch.h index cb962ab..1065cce 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -18,85 +18,87 @@ #define DEFAULT_PASS "admin" enum CntStatus { - OPEN, SHORT + OPEN, SHORT }; struct vlan { - int vlan_id; - std::string name; - std::vector tagged_member; - std::vector untagged_member; + int vlan_id; + std::string name; + std::vector tagged_member; + std::vector untagged_member; }; struct trunk { - int trunk_id; - std::vector member; + int trunk_id; + std::vector member; }; struct port { - byte id; - byte status; - int pvid; - struct { - //port_settings - } settings; - struct { - //port_statistics - } stats; - struct { - //bandwidth_control_ingress - //bandwidth_control_egress - } bw_control; - struct { - //storm_control - } storm_control; - struct { - CntStatus status; - int fault_distace; - } test; - //qos_basic + byte id; + byte status; + int pvid; + struct { + //port_settings + } settings; + struct { + //port_statistics + } stats; + struct { + //bandwidth_control_ingress + //bandwidth_control_egress + } bw_control; + struct { + //storm_control + } storm_control; + struct { + CntStatus status; + int fault_distace; + } test; + //qos_basic }; class Switch { -public: - Switch() { - } - int parse(datasets); - int parse(dataset); - int parse(std::string); - int set(std::pair); + public: + Switch() { + } + int parse(datasets); + int parse(dataset); + int parse(std::string); + int set(std::pair); - std::string get(std::string); - std::string toString(); + std::string get(std::string); + std::string toString(); - int print(); + int print(); - struct { - std::string type; - std::string hardware_version; - std::string firmware_version; - macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; - int ports; - } device; - struct { - std::string password = DEFAULT_PASS; - std::string username = DEFAULT_USER; - std::string hostname; - ipAddr ip_addr { 0, 0, 0, 0, }; - ipAddr ip_mask; - ipAddr gateway; - bool dhcp; - bool loop_prevention; - bool qos_enabled; - bool vlan_enabled; - //mtu_vlan - } settings; + struct { + std::string type; + std::string hardware_version; + std::string firmware_version; + macAddr mac { + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; + int ports; + } device; + struct { + std::string password = DEFAULT_PASS; + std::string username = DEFAULT_USER; + std::string hostname; + ipAddr ip_addr { + 0, 0, 0, 0, }; + ipAddr ip_mask; + ipAddr gateway; + bool dhcp; + bool loop_prevention; + bool qos_enabled; + bool vlan_enabled; + //mtu_vlan + } settings; -private: - rapidjson::Document json; - std::vector vlans; - std::vector ports; + private: + rapidjson::Document json; + std::vector vlans; + std::vector ports; }; #endif /* SWITCH_H_ */ diff --git a/src/Types.h b/src/Types.h index b27657d..94abc15 100644 --- a/src/Types.h +++ b/src/Types.h @@ -20,117 +20,117 @@ #include "table.h" class macAddr: public std::array { -public: - friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) { - out << std::hex << std::setw(2) << std::setfill('0') - << (unsigned) arr[0]; - for (unsigned i = 1; i < 6; i++) { - out << ":" << std::setw(2) << std::setfill('0') - << (unsigned) arr[i]; + public: + friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) { + out << std::hex << std::setw(2) << std::setfill('0') + << (unsigned) arr[0]; + for (unsigned i = 1; i < 6; i++) { + out << ":" << std::setw(2) << std::setfill('0') + << (unsigned) arr[i]; + } + return out; } - return out; - } - macAddr() { - *this= {0,0,0,0,0,0}; - } - - macAddr(std::initializer_list s) { - int i = 0; - for (byte b : s) { - if(i<6) (*this)[i++]=b; - else break; + macAddr() { + *this= {0,0,0,0,0,0}; } - } - macAddr(bytes bts) { - int i = 0; - for (byte b : bts) { - if(i<6) (*this)[i++]=b; - else break; + macAddr(std::initializer_list s) { + int i = 0; + for (byte b : s) { + if(i<6) (*this)[i++]=b; + else break; + } } - } - int hash() { - int ret=0; - for (unsigned i = 0; i < 6; i++) { - ret = (ret*33) ^ (*this)[i]; + macAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + if(i<6) (*this)[i++]=b; + else break; + } } - return ret; - } - bool operator==(const macAddr &A) { - for (unsigned i = 0; i < 6; i++) { - if(A[i]!=(*this)[i])return false; + int hash() { + int ret=0; + for (unsigned i = 0; i < 6; i++) { + ret = (ret*33) ^ (*this)[i]; + } + return ret; } - return true; - } - bool operator!=(const macAddr &A) { - for (unsigned i = 0; i < 6; i++) { - if(A[i]!=(*this)[i])return true; + bool operator==(const macAddr &A) { + for (unsigned i = 0; i < 6; i++) { + if(A[i]!=(*this)[i])return false; + } + return true; } - return false; - } -}; -/* - class mac_addr : public std::array { - public: - typedef std::array super; + bool operator!=(const macAddr &A) { + for (unsigned i = 0; i < 6; i++) { + if(A[i]!=(*this)[i])return true; + } + return false; + } + }; - using super::super; + /* + class mac_addr : public std::array { + public: + typedef std::array super; - mac_addr{00, 00, 00, 000}; - }; - */ + using super::super; + + mac_addr{00, 00, 00, 000}; + }; + */ class ipAddr: public std::array { -public: + public: - ipAddr() { - *this= {0,0,0,0,0,0}; - } - - ipAddr(std::initializer_list s) { - int i = 0; - for (byte b : s) { - if(i<4) (*this)[i++]=b; - else break; + ipAddr() { + *this= {0,0,0,0,0,0}; } - } - ipAddr(bytes bts) { - int i = 0; - for (byte b : bts) { - if(i<4) (*this)[i++]=b; - else break; + ipAddr(std::initializer_list s) { + int i = 0; + for (byte b : s) { + if(i<4) (*this)[i++]=b; + else break; + } } - } - friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) { - out << std::dec << (unsigned) arr[0]; - for (unsigned i = 1; i < 4; i++) { - out << "." << (unsigned) arr[i]; + ipAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + if(i<4) (*this)[i++]=b; + else break; + } } - return out; - } -}; + + friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) { + out << std::dec << (unsigned) arr[0]; + for (unsigned i = 1; i < 4; i++) { + out << "." << (unsigned) arr[i]; + } + return out; + } + }; namespace smrtlink { -constexpr unsigned int caseArg(const char* str, int h = 0) { - return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; -} + constexpr unsigned int caseArg(const char* str, int h = 0) { + return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; + } } template std::vector operator+(const std::vector &A, const std::vector &B) { std::vector AB; - AB.reserve(A.size() + B.size()); // preallocate memory - AB.insert(AB.end(), A.begin(), A.end()); // add A; - AB.insert(AB.end(), B.begin(), B.end()); // add B; + AB.reserve(A.size() + B.size()); // preallocate memory + AB.insert(AB.end(), A.begin(), A.end()); // add A; + AB.insert(AB.end(), B.begin(), B.end()); // add B; return AB; } @@ -142,24 +142,24 @@ std::vector &operator+=(std::vector &A, const std::vector &B) { } struct Options { - struct { - bool HEX; - bool JSON; - bool PLAIN; - bool REVERSE; + struct { + bool HEX; + bool JSON; + bool PLAIN; + bool REVERSE; - bool HEADER; - bool PERMANENT; - bool WAIT; - bool INTERACTIVE; - } flags; - std::string user; - std::string password; - std::string interface; - std::string file; - int debug_level = 0; - int verbosity = 0; - long timeout = 250U; + bool HEADER; + bool PERMANENT; + bool WAIT; + bool INTERACTIVE; + } flags; + std::string user; + std::string password; + std::string interface; + std::string file; + int debug_level = 0; + int verbosity = 0; + long timeout = 250U; }; #endif /* TYPES_H_ */ diff --git a/src/bytes.cpp b/src/bytes.cpp index 9dac956..c7ab164 100644 --- a/src/bytes.cpp +++ b/src/bytes.cpp @@ -7,23 +7,23 @@ #include "bytes.h" -bytes bytes::readHex(std::string s){ - vector ret; - std::string delimiter = ":"; - std::string token; - size_t pos = 0; - int hex; - byte b; - ret.resize(0); - while ((pos = s.find(delimiter)) != std::string::npos) { - token = s.substr(0, pos); - sscanf(token.c_str(), "%x", &hex); - s.erase(0, pos + delimiter.length()); - b = hex & 0xFF; - ret.push_back(b); - } - sscanf(s.c_str(), "%x", &hex); - b = hex & 0xFF; - ret.push_back(b); - return ret; +bytes bytes::readHex(std::string s) { + vector ret; + std::string delimiter = ":"; + std::string token; + size_t pos = 0; + int hex; + byte b; + ret.resize(0); + while ((pos = s.find(delimiter)) != std::string::npos) { + token = s.substr(0, pos); + sscanf(token.c_str(), "%x", &hex); + s.erase(0, pos + delimiter.length()); + b = hex & 0xFF; + ret.push_back(b); + } + sscanf(s.c_str(), "%x", &hex); + b = hex & 0xFF; + ret.push_back(b); + return ret; } diff --git a/src/bytes.h b/src/bytes.h index 01f597d..5f2fbac 100644 --- a/src/bytes.h +++ b/src/bytes.h @@ -19,67 +19,69 @@ typedef unsigned char byte; class bytes: public std::vector { - typedef std::vector vector; -public: - using vector::operator[]; - bytes() { - } - bytes(int n) : vector(n){ - } + typedef std::vector vector; + public: + using vector::operator[]; + bytes() { + } + bytes(int n) : + vector(n) { + } - bytes(std::string d) : vector(d.begin(), d.end()){ - this->push_back('\0'); - } + bytes(std::string d) : + vector(d.begin(), d.end()) { + this->push_back('\0'); + } - bytes(std::initializer_list s) - { - for (uint8_t b : s) { - this->push_back(b); - } - } + bytes(std::initializer_list s) { + for (uint8_t b : s) { + this->push_back(b); + } + } - bytes(const vector &B) { - this->reserve(B.size()); - this->insert(this->begin(), B.begin(), B.end()); - } + bytes(const vector &B) { + this->reserve(B.size()); + this->insert(this->begin(), B.begin(), B.end()); + } - bytes readHex(std::string); + bytes readHex(std::string); - bytes operator=(const vector &B) { - this->reserve(B.size()); - this->insert(this->begin(), B.begin(), B.end()); - return *this; - } + bytes operator=(const vector &B) { + this->reserve(B.size()); + this->insert(this->begin(), B.begin(), B.end()); + return *this; + } - bytes &operator+=(const bytes &B) { - this->reserve(this->size() + B.size()); - this->insert(this->end(), B.begin(), B.end()); - return *this; - } + bytes &operator+=(const bytes &B) { + this->reserve(this->size() + B.size()); + this->insert(this->end(), B.begin(), B.end()); + return *this; + } - bytes operator+(const bytes &B) { - bytes 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; - } + bytes operator+(const bytes &B) { + bytes 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; + } - friend std::ostream& operator<<(std::ostream& out, const bytes& arr) { - if (arr.size() > 0) { - out << std::hex << std::setw(2) << std::setfill('0') - << (unsigned) arr[0]; - } - for (unsigned i = 1; i < arr.size(); i++) { - out << ":" << std::setw(2) << std::setfill('0') << (unsigned) arr[i]; - } - return out; - } + friend std::ostream& operator<<(std::ostream& out, const bytes& arr) { + if (arr.size() > 0) { + out << std::hex << std::setw(2) << std::setfill('0') + << (unsigned) arr[0]; + } + for (unsigned i = 1; i < arr.size(); i++) { + out << ":" << std::setw(2) << std::setfill('0') + << (unsigned) arr[i]; + } + return out; + } - operator std::string() { - std::string s(this->begin(),this->end()); - return s; - } + operator std::string() { + std::string s(this->begin(), this->end()); + return s; + } }; #endif /* BYTES_H_ */ diff --git a/src/datasets.h b/src/datasets.h index 7b08f4f..dc1edef 100644 --- a/src/datasets.h +++ b/src/datasets.h @@ -12,29 +12,29 @@ #include "bytes.h" struct dataset { - short type; - short len; - bytes value; + short type; + short len; + bytes value; }; -class datasets : public std::vector { -public: - datasets(){}; - datasets(std::initializer_list s) - { - for (dataset b : s) { - //(*this)[b.type]=b; - push_back(b); - } - } +class datasets: public std::vector { + public: + datasets() { + } + datasets(std::initializer_list s) { + for (dataset b : s) { + //(*this)[b.type]=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; - } + 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_ */ diff --git a/src/jsonNode.cpp b/src/jsonNode.cpp index 1ac0ea5..b6ed744 100644 --- a/src/jsonNode.cpp +++ b/src/jsonNode.cpp @@ -11,43 +11,43 @@ #include "jsonNode.h" jsonNode::jsonNode(const std::string &x, doc &root) { - super(rapidjson::kStringType); - char buffer[30]; - int len = sprintf(buffer, "%s", x.c_str()); - this->SetString(buffer, static_cast(len), root.GetAllocator()); - memset(buffer, 0, sizeof(buffer)); + super(rapidjson::kStringType); + char buffer[30]; + int len = sprintf(buffer, "%s", x.c_str()); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); } jsonNode::jsonNode(const ipAddr &x, doc &root) { - super(rapidjson::kStringType); - char buffer[16]; - int len = sprintf(buffer, "%d.%d.%d.%d", x[0], x[1], x[2], x[3]); - this->SetString(buffer, static_cast(len), root.GetAllocator()); - memset(buffer, 0, sizeof(buffer)); + super(rapidjson::kStringType); + char buffer[16]; + int len = sprintf(buffer, "%d.%d.%d.%d", x[0], x[1], x[2], x[3]); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); } jsonNode::jsonNode(const macAddr &x, doc &root) { - super(rapidjson::kStringType); - char buffer[18]; - int len = sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", x[0], x[1], x[2], - x[3], x[4], x[5]); - this->SetString(buffer, static_cast(len), root.GetAllocator()); - memset(buffer, 0, sizeof(buffer)); + super(rapidjson::kStringType); + char buffer[18]; + int len = sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", x[0], x[1], x[2], + x[3], x[4], x[5]); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); } jsonNode::jsonNode(const vlan &x, doc &root) { - super(rapidjson::kObjectType); - AddMember("id", x.vlan_id, root.GetAllocator()); - AddMember("name", jsonNode(x.name, root), root.GetAllocator()); - AddMember("tagged-members", jsonNode(x.tagged_member, root), - root.GetAllocator()); - AddMember("untagged-members", jsonNode(x.untagged_member, root), - root.GetAllocator()); + super(rapidjson::kObjectType); + AddMember("id", x.vlan_id, root.GetAllocator()); + AddMember("name", jsonNode(x.name, root), root.GetAllocator()); + AddMember("tagged-members", jsonNode(x.tagged_member, root), + root.GetAllocator()); + AddMember("untagged-members", jsonNode(x.untagged_member, root), + root.GetAllocator()); } jsonNode::jsonNode(const port &x, doc &root) { - super(rapidjson::kObjectType); - AddMember("id", x.id, root.GetAllocator()); - AddMember("status", x.status, root.GetAllocator()); - AddMember("pvid", x.pvid, root.GetAllocator()); + super(rapidjson::kObjectType); + AddMember("id", x.id, root.GetAllocator()); + AddMember("status", x.status, root.GetAllocator()); + AddMember("pvid", x.pvid, root.GetAllocator()); } diff --git a/src/jsonNode.h b/src/jsonNode.h index 9fccbf5..d41084d 100644 --- a/src/jsonNode.h +++ b/src/jsonNode.h @@ -13,26 +13,26 @@ #include "Switch.h" class jsonNode: public rapidjson::Value { - typedef rapidjson::Value super; - typedef rapidjson::Document doc; -public: - jsonNode(const std::string&, doc&); - jsonNode(const macAddr&, doc&); - jsonNode(const ipAddr&, doc&); - jsonNode(const vlan&, doc&); - jsonNode(const port&, doc&); + typedef rapidjson::Value super; + typedef rapidjson::Document doc; + public: + jsonNode(const std::string&, doc&); + jsonNode(const macAddr&, doc&); + jsonNode(const ipAddr&, doc&); + jsonNode(const vlan&, doc&); + jsonNode(const port&, doc&); - template - jsonNode(const T &templ_arg, doc &root){ - std::cerr<<"Not serializable Type: "< + jsonNode(const T &templ_arg, doc &root) { + std::cerr << "Not serializable Type: " << typeid(T).name() << "\n"; + } - template - jsonNode(const std::vector &x, doc &root) { - super(rapidjson::kArrayType); - for (T y : x) - PushBack(jsonNode(y, root), root.GetAllocator()); - } + template + jsonNode(const std::vector &x, doc &root) { + super(rapidjson::kArrayType); + for (T y : x) + PushBack(jsonNode(y, root), root.GetAllocator()); + } }; #endif /* JSONNODE_H_ */ diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index a264d47..ca4a5c6 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -38,96 +38,101 @@ int main(int argc, char *argv[]) { options.user = DEFAULT_USER; options.password = DEFAULT_PASS; - const struct option longopts[] = { { "version", no_argument, 0, 'V' }, { - "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { - "reverse", no_argument, 0, 'r' }, - { "permanent", no_argument, 0, 's' }, { "debug", optional_argument, - 0, 'd' }, { "password", required_argument, 0, 'P' }, { - "user", required_argument, 0, 'U' }, { "interface", - required_argument, 0, 'i' }, { "header", required_argument, - 0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file", - required_argument, 0, 'f' }, { "timeout", required_argument, - 0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0, - 0 }, }; + const struct option longopts[] = { + { "version", no_argument, 0, 'V' }, { + "verbose", no_argument, 0, 'v' }, + { "help", no_argument, 0, 'h' }, { + "reverse", no_argument, 0, 'r' }, + { "permanent", no_argument, 0, 's' }, { + "debug", optional_argument, 0, 'd' }, + { "password", required_argument, 0, 'P' }, { + "user", required_argument, 0, 'U' }, + { "interface", required_argument, 0, 'i' }, { + "header", required_argument, 0, 'b' }, + { "hex", required_argument, 0, 'x' }, { + "file", required_argument, 0, 'f' }, + { "timeout", required_argument, 0, 't' }, { + "wait", required_argument, 0, 'w' }, + { 0, 0, 0, 0 }, }; while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts, &index)) != -1) { switch (opt) { - case 'h': - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); + case 'h': + fprintf(stderr, VERSION); + fprintf(stderr, USAGE, argv[0]); + fprintf(stderr, HELP); + exit(EXIT_SUCCESS); break; - case 'V': - fprintf(stderr, VERSION); - exit(EXIT_SUCCESS); + case 'V': + fprintf(stderr, VERSION); + exit(EXIT_SUCCESS); break; - case 'r': - options.flags.REVERSE = true; + case 'r': + options.flags.REVERSE = true; break; - case 'b': - options.flags.HEADER = true; + case 'b': + options.flags.HEADER = true; break; - case 'x': - options.flags.HEX = true; + case 'x': + options.flags.HEX = true; break; - case 's': - options.flags.PERMANENT = true; + case 's': + options.flags.PERMANENT = true; break; - case 'w': - options.flags.WAIT = true; + case 'w': + options.flags.WAIT = true; break; - case 'X': - options.flags.INTERACTIVE = true; + case 'X': + options.flags.INTERACTIVE = true; break; - case 'v': - if (optarg != NULL) - options.verbosity = atoi(optarg); - else - options.verbosity++; + case 'v': + if (optarg != NULL) + options.verbosity = atoi(optarg); + else + options.verbosity++; break; - case 'd': - if (optarg != NULL) - options.debug_level = atoi(optarg); - else - options.debug_level++; + case 'd': + if (optarg != NULL) + options.debug_level = atoi(optarg); + else + options.debug_level++; break; - case 't': - options.timeout = atol(optarg); + case 't': + options.timeout = atol(optarg); break; - case 'f': - options.file = std::string(optarg); + case 'f': + options.file = std::string(optarg); break; - case 'P': - options.password = std::string(optarg); + case 'P': + options.password = std::string(optarg); break; - case 'U': - options.user = std::string(optarg); + case 'U': + options.user = std::string(optarg); break; - case 'i': - options.interface = std::string(optarg); + case 'i': + options.interface = std::string(optarg); break; - default: /* '?' */ - fprintf(stderr, "Unknown option\n"); - fprintf(stderr, USAGE, argv[0]); - exit(EXIT_FAILURE); + default: /* '?' */ + fprintf(stderr, "Unknown option\n"); + fprintf(stderr, USAGE, argv[0]); + exit(EXIT_FAILURE); } } @@ -152,7 +157,8 @@ int main(int argc, char *argv[]) { exit(EXIT_SUCCESS); fprintf(stderr, "Not yet implemented.\n"); exit(EXIT_FAILURE); - } else if (optind < argc) { + } + else if (optind < argc) { vector v; while (optind < argc) v.push_back(argv[optind++]); diff --git a/src/table.cpp b/src/table.cpp index 5e49f65..7a14331 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -18,24 +18,24 @@ table::table(std::initializer_list l) { i++; } } -const table::set table::operator[](std::string s){ +const table::set table::operator[](std::string s) { return *this->right[s]; } -const table::set table::operator[](short n){ +const table::set table::operator[](short n) { return *this->left[n]; } -bool table::exists(std::string s){ +bool table::exists(std::string s) { return !(right.find(s) == right.end()); } -bool table::exists(short n){ +bool table::exists(short n) { return !(left.find(n) == left.end()); } -short table::type(std::string s){ +short table::type(std::string s) { return this->right[s]->type; } -std::string table::id(short n){ +std::string table::id(short n) { return this->left[n]->id; } -std::string table::name(short n){ +std::string table::name(short n) { return this->left[n]->name; } diff --git a/src/table.h b/src/table.h index 54af936..96b91b5 100644 --- a/src/table.h +++ b/src/table.h @@ -12,32 +12,32 @@ #include class table { -public: - enum A { - RW, RO, WO, NONE, UNKNOWN - }; - enum F { - STRING, HEX, DEC, ACTION, BOOL, EMPTY - }; - struct set { - short type; - F format; - A action; - std::string name; - std::string id; - }; - table(std::initializer_list l); - const table::set operator[](std::string); - const table::set operator[](short); - bool exists(std::string); - bool exists(short); - short type(std::string); - std::string id(short); - std::string name(short); -private: - std::vector data; - std::map left; - std::map right; + public: + enum A { + RW, RO, WO, NONE, UNKNOWN + }; + enum F { + STRING, HEX, DEC, ACTION, BOOL, EMPTY + }; + struct set { + short type; + F format; + A action; + std::string name; + std::string id; + }; + table(std::initializer_list l); + const table::set operator[](std::string); + const table::set operator[](short); + bool exists(std::string); + bool exists(short); + short type(std::string); + std::string id(short); + std::string name(short); + private: + std::vector data; + std::map left; + std::map right; }; #endif /* LOOKUPTABLE_H_ */