From ad92736b747d9a012e722339427036931c5b20e6 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Sun, 11 Oct 2015 23:07:04 +0200 Subject: [PATCH] /Types/* added --- src/Host.cpp | 6 +-- src/Host.h | 2 +- src/Lookup.h | 65 +++++++++++++++++++++++++++++++ src/Options.h | 2 +- src/Packet.cpp | 48 +++++++++++------------ src/Packet.h | 6 +-- src/Program.cpp | 5 ++- src/Program.h | 2 +- src/Socket.cpp | 8 ++-- src/Socket.h | 3 +- src/Switch.cpp | 19 ++++++++- src/Switch.h | 37 +----------------- src/{ => Types}/Types.h | 4 +- src/Types/bytes.cpp | 28 ++++++++++++++ src/Types/bytes.h | 86 +++++++++++++++++++++++++++++++++++++++++ src/Types/datasets.cpp | 8 ++++ src/Types/datasets.h | 31 +++++++++++++++ src/smrtlink.cpp | 27 +++++++++++++ 18 files changed, 306 insertions(+), 81 deletions(-) create mode 100644 src/Lookup.h rename src/{ => Types}/Types.h (98%) create mode 100644 src/Types/bytes.cpp create mode 100644 src/Types/bytes.h create mode 100644 src/Types/datasets.cpp create mode 100644 src/Types/datasets.h diff --git a/src/Host.cpp b/src/Host.cpp index ccd6193..c3cc0ef 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -21,11 +21,11 @@ //#include #include "Options.h" #include "Host.h" -#include "Types.h" -#include "Types/bytes.h" +#include "Types/Types.h" +//#include "bytes.h" macAddr Host::getMac() { - macAddr ret { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c }; //TODO find actual MAC Address + macAddr ret { 0x6a,0x49,0x16,0x17,0x2e,0x8d }; //TODO find actual MAC Address return ret; } diff --git a/src/Host.h b/src/Host.h index f8b41ed..1e4d223 100644 --- a/src/Host.h +++ b/src/Host.h @@ -8,7 +8,7 @@ #ifndef HOST_H_ #define HOST_H_ -#include "Types.h" +#include "Types/Types.h" class Host { public: diff --git a/src/Lookup.h b/src/Lookup.h new file mode 100644 index 0000000..4c04786 --- /dev/null +++ b/src/Lookup.h @@ -0,0 +1,65 @@ +/* + * Switch.cpp + * + * Created on: 29.09.2015 + * Author: jdi + */ + +#include + +class lookupTable: public boost::bimap { + struct set{ + short n; + std::string s; + }; +public: + lookupTable(std::initializer_list l) { + for (set s : l) { + this->left[s.n]=s.s; + } + } + const short& operator[](std::string s){ + return this->right[s]; + } + const std::string& operator[](short n){ + return this->left[n]; + } +}; + +static lookupTable rcv_lookup = { { 1, "type" }, //string + { 2, "hostname" }, //string + { 3, "mac" }, //byte[6] + { 4, "ip_addr" }, //byte[4] + { 5, "ip_mask" }, //byte[4] + { 6, "gateway" }, //byte[4] + { 7, "firmware_version" }, //string + { 8, "hardware_version" }, //string + { 9, "dhcp" }, //bool byte + { 8704, "802.1q vlan" }, //??? + //{0000," "}, + }; + +static lookupTable snd_lookup = { + +// TODO find out if id is unique in response + { 10, "???" }, //after login + { 2, "???" }, //after login + { 512, "login_user" }, //string + { 513, "new_user" }, //string + { 514, "login_password" }, //string + { 515, "new_passwoord" }, //string + { 2200, "vlan" }, + { 2305, "???" }, //sent before login and before change hostname + { 4608, "port_trunk" }, //byte[5] last byte bitmask?? + { 8192, "mtu_vlan" }, //byte[2] first byte bool, second byte port id + { 8449, "port_vlan" }, //??? + + { 8704, "802.1q vlan" }, //??? get vlan / set status + { 8705, "802.1q vlan" }, //??? + { 8706, "802.1q vlan pvid" }, //???? + + { 12288, "QoS Basic" }, //bool = QoS Mod + { 16640, "port_mirror" }, //byte[10] second byte port id?? + { 17152, "loop_prevention" }, //bool byte + //{0000," "}, + }; diff --git a/src/Options.h b/src/Options.h index c5fd8f8..c59a0e5 100644 --- a/src/Options.h +++ b/src/Options.h @@ -8,7 +8,7 @@ #ifndef OPTIONS_H_ #define OPTIONS_H_ -#include "Types.h" +#include "Types/Types.h" #define VERSION "smrtlink (v1 Linux)\n" #define USAGE "usage: %s [-bhrvx] [-i interface] [-u [password:]username]\n\ diff --git a/src/Packet.cpp b/src/Packet.cpp index 8f5a21d..9b0aa66 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -9,9 +9,7 @@ #include #include #include "Packet.h" -#include "Types.h" -#include "Types/bytes.h" -#include "Types/datasets.h" +#include "Types/Types.h" Packet::Packet(OpCode c) { srand(time(NULL)); @@ -22,7 +20,7 @@ Packet::Packet(OpCode c) { void Packet::printHeader() { std::cout << "Header:\n"; std::cout << "\tID:\t\t" << sequenceId << "\n"; - std::cout << "\tVersion:\t" << version << "\n"; + std::cout << "\tVersion:\t" << (int) version << "\n"; std::cout << "\tError:\t\t" << errorCode << "\n"; std::cout << "\tSwitch MAC:\t" << switchMac << "\n"; std::cout << "\tHost MAC: \t" << hostMac << "\n"; @@ -34,8 +32,8 @@ void Packet::printHeader() { bytes Packet::getBytes() { int i = 0; - for (unsigned j = 0; j < payload.size(); j++) - push(body, i, payload[j]); + for (auto d : payload) + push(body, i, d.second); push(body, i, (int) PACKET_END); i = 0; push(head, i, version); @@ -133,7 +131,7 @@ const datasets& Packet::getPayload() const { return payload; } -void Packet::setPayload(const datasets& payload) { +void Packet::setPayload(datasets payload) { this->payload = payload; } @@ -157,24 +155,24 @@ std::string Packet::opCodeToString() { 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++) { diff --git a/src/Packet.h b/src/Packet.h index 89d1047..93d96e9 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -11,9 +11,7 @@ #define HEADER_LEN 32 #define PACKET_END 0xFFFF0000 -#include "Types.h" -#include "Types/bytes.h" -#include "Types/datasets.h" +#include "Types/Types.h" class Packet { public: @@ -38,7 +36,7 @@ public: void setSwitchMac(macAddr); void setCheckSum(int); void setSequenceId(short); - void setPayload(const datasets& payload); + void setPayload(datasets payload); private: bytes head = bytes(32); diff --git a/src/Program.cpp b/src/Program.cpp index 6bb0563..1cf8ca2 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -13,6 +13,7 @@ #include "Socket.h" #include "Switch.h" #include "Packet.h" +#include "Lookup.h" int Program::list() { @@ -123,11 +124,11 @@ int Program::setProperty() { int Program::getProperty() { Host h = Host(); printf("Get:\n"); - Packet p = Packet(Packet::DISCOVERY); + Packet p = Packet(Packet::GET); macAddr d = {0x14,0xcc,0x20,0x49,0x5e,0x07}; p.setSwitchMac(d); p.setHostMac(h.getMac()); - datasets t = { { 2200, 0, { } } }; + datasets t = { { 2305, 0, { } } }; p.setPayload(t); bytes a = p.getBytes(); p.encode(a); diff --git a/src/Program.h b/src/Program.h index dea2950..32733d1 100644 --- a/src/Program.h +++ b/src/Program.h @@ -8,7 +8,7 @@ #ifndef PROGRAM_H_ #define PROGRAM_H_ -#include "Types.h" +#include "Types/Types.h" #define SRC_PORT 29809 #define DST_PORT 29808 diff --git a/src/Socket.cpp b/src/Socket.cpp index 6fd9d71..9357ee0 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -4,18 +4,18 @@ * Created on: 02.09.2015 * Author: jdi */ -#include -#include -#include +//#include +//#include +//#include #include #include #include #include #include "Socket.h" #include "Packet.h" -#include "Types.h" #include "Options.h" #include "Host.h" +#include "Types/Types.h" Socket::Socket(asio::io_service& io_service) : send_socket_(io_service), receive_socket_(io_service) { diff --git a/src/Socket.h b/src/Socket.h index 98ae975..c61b704 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -10,8 +10,7 @@ #include #include "Packet.h" -#include "Types.h" -//#include "Types.h" +#include "Types/Types.h" #define MAX_LENGTH 1024 diff --git a/src/Switch.cpp b/src/Switch.cpp index 1a59381..e693c59 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -6,8 +6,10 @@ */ #include -#include "Types/bytes.h" +#include "Types/Types.h" #include "Switch.h" +#include "Lookup.h" +#include "Options.h" void Switch::parse(datasets arr) { device.type = arr[1].value; @@ -18,6 +20,19 @@ void Switch::parse(datasets arr) { settings.gateway = arr[6].value; device.firmware_version = arr[7].value; device.hardware_version = arr[8].value; - settings.dhcp=arr[9].value[0]; + settings.dhcp = arr[9].value[0]; +} + +void Switch::parse(dataset d) { + auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup; + if(d.type==lookup["type"]){ + device.type = d.value; + } + if(d.type==lookup["mac"]){ + device.mac = d.value; + } + if(d.type==lookup["type"]){ + device.type = d.value; + } } diff --git a/src/Switch.h b/src/Switch.h index 31cf571..2daa2c2 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -10,45 +10,11 @@ #include #include -#include "Types.h" -#include "Types/bytes.h" -#include "Types/datasets.h" +#include "Types/Types.h" #define DEFAULT_USER "admin" #define DEFAULT_PASS "admin" -static std::map rcv_lookup= { - { 1, "type"},//string - { 2, "hostname"},//string - { 3, "mac"},//byte[6] - { 4, "ip_addr"},//byte[4] - { 5, "ip_mask"},//byte[4] - { 6, "gateway"},//byte[4] - { 7, "firmware_version"},//string - { 8, "hardware_version"},//string - { 9, "dhcp"}};//bool byte - -static std::map snd_lookup= { - {10,"???"},//after login - {2,"???"},//after login - {512,"login_user"},//string - {513,"new_user"},//string - {514,"login_password"},//string - {515,"new_passwoord"},//string - {2200,"vlan"}, - {2305,"???"},//sent before login and before change hostname - {4608,"port_trunk"},//byte[5] last byte bitmask?? - {8192,"mtu_vlan"},//byte[2] first byte bool, second byte port id - {8449,"port_vlan"},//??? - {8705,"802.1q vlan"},//??? - {8706,"802.1q vlan pvid"},//???? - {12288,"QoS Basic"},//bool = QoS Mod - {16640,"port_mirror"},//byte[10] second byte port id?? - {17152,"loop_prevention"},//bool byte - //{0000," "}, -}; - - struct vlan { int vlan_id; std::string name; @@ -69,6 +35,7 @@ public: Switch() { } void parse(datasets); + void parse(dataset); struct { std::string type; std::string hardware_version; diff --git a/src/Types.h b/src/Types/Types.h similarity index 98% rename from src/Types.h rename to src/Types/Types.h index b4d2560..8822db9 100644 --- a/src/Types.h +++ b/src/Types/Types.h @@ -14,7 +14,9 @@ #include #include #include -#include "Types/bytes.h" + +#include "bytes.h" +#include "datasets.h" class macAddr: public std::array { public: diff --git a/src/Types/bytes.cpp b/src/Types/bytes.cpp new file mode 100644 index 0000000..bdc8c9d --- /dev/null +++ b/src/Types/bytes.cpp @@ -0,0 +1,28 @@ +/* + * Bytes.cpp + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#include "bytes.h" + +bytes::bytes(std::string d) { + vector(); + 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); + sscanf(token.c_str(), "%x", &hex); + d.erase(0, pos + delimiter.length()); + b = hex & 0xFF; + push_back(b); + } + sscanf(d.c_str(), "%x", &hex); + b = hex & 0xFF; + push_back(b); +} diff --git a/src/Types/bytes.h b/src/Types/bytes.h new file mode 100644 index 0000000..dcc0ec9 --- /dev/null +++ b/src/Types/bytes.h @@ -0,0 +1,86 @@ +/* + * Bytes.h + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#ifndef BYTES_H_ +#define BYTES_H_ + +#include +#include +#include +#include +#include +#include +#include + +typedef unsigned char byte; + +class bytes: public std::vector { + typedef std::vector vector; +public: + using vector::operator[]; + bytes() { + } + bytes(int n) : + vector(n) { + } + + bytes(std::string); + + 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 readHex(std::string s){ + return bytes(s); + } + + 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) { + 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; + } + + operator std::string() { + std::string s(this->begin(),this->end()); + return s; + } +}; + +#endif /* BYTES_H_ */ diff --git a/src/Types/datasets.cpp b/src/Types/datasets.cpp new file mode 100644 index 0000000..63529b4 --- /dev/null +++ b/src/Types/datasets.cpp @@ -0,0 +1,8 @@ +/* + * Datasets.cpp + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#include "datasets.h" diff --git a/src/Types/datasets.h b/src/Types/datasets.h new file mode 100644 index 0000000..e570b18 --- /dev/null +++ b/src/Types/datasets.h @@ -0,0 +1,31 @@ +/* + * Datasets.h + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#ifndef DATASETS_H_ +#define DATASETS_H_ + +#include +#include "bytes.h" + +struct dataset { + short type; + short len; + bytes value; +}; + +class datasets : public std::map { +public: + datasets(){}; + datasets(std::initializer_list s) + { + for (dataset b : s) { + (*this)[b.type]=b; + } + } +}; + +#endif /* DATASETS_H_ */ diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index a5ee5c6..0792109 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -118,11 +118,38 @@ int main(int argc, char *argv[]) { 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;