diff --git a/Makefile b/Makefile index 161aed4..bf5e2c7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = g++ -CFLAGS = -Wall -std=c++11 +CFLAGS = -Wall -std=c++14 TARGET = smrtlink all: $(TARGET) diff --git a/src/Packet.cpp b/src/Packet.cpp index 737dc31..99eff5a 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include "Packet.h" #include "Types.h" @@ -21,15 +20,16 @@ Packet::Packet(OpCode c) { } void Packet::printHeader() { - printf( - "Header:\n\tOpCode:\t\t%s\n\tID:\t\t%d\n\tVersion:\t%hhd\n\tError:\t\t%.8X\n\tSwitch MAC:\t", - opCodeToString().c_str(), sequenceId, version, errorCode); - std::cout << switchMac << "\n"; - std::cout << "\tHost MAC:\t"<< hostMac << "\n"; - printf("\n\tLength:\t%hd", this->getLength()); - printf("\n\tOffset:\t%hd", fragmentOffset); - printf("\n\tFlags:\t%.4hX", flag); - printf("\n\tChecksum:\t%d", checkSum); + std::cout << "Header:\n\tOpCode:\t\t" << opCodeToString() << "\n"; + std::cout << "\tID:\t\t" << sequenceId << "\n"; + std::cout << "\tVersion:\t" << version << "\n"; + std::cout << "\tError:\t\t" << errorCode << "\n"; + std::cout << "\tSwitch MAC:\t" << switchMac << "\n"; + std::cout << "\tHost MAC: \t" << hostMac << "\n"; + std::cout << "\tLength: \t" << std::dec << this->getLength() << "\n"; + std::cout << "\tOffset: \t" << fragmentOffset << "\n"; + std::cout << "\tFlags: \t" << std::hex << flag << "\n"; + std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n"; } bytes Packet::getBytes() { @@ -50,9 +50,6 @@ bytes Packet::getBytes() { push(head, i, tokenId); push(head, i, checkSum); bytes data = head + body; - //printf("Send Header:\t"); - //utils::printHex(head); - //printf("\n"); return data; } @@ -60,9 +57,6 @@ void Packet::parse(bytes data) { memcpy(&head[0], &data[0], HEADER_LEN); body.resize(data.size() - HEADER_LEN); memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN); - //printf("Receive Header:\t"); - //utils::printHex(head); - //printf("\n"); int i = 0; short checkLen = 0x0; pull(head, i, version); @@ -161,7 +155,7 @@ std::string Packet::opCodeToString() { void Packet::encode(bytes &data) { int len = data.size(); - std::vector t = { 191, 155, 227, 202, 99, 162, 79, 104, 49, + 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, @@ -179,7 +173,6 @@ void Packet::encode(bytes &data) { 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 = t; bytes s = key; int i, j = 0; for (int k = 0; k < len; k++) { diff --git a/src/Program.cpp b/src/Program.cpp index 6b78267..e8c5075 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -4,7 +4,7 @@ * Created on: 04.09.2015 * Author: jdi */ -#include +#include #include #include "Options.h" @@ -16,9 +16,6 @@ int Program::list() { - //Device d = Device(); - //printf(" %d\n", d.getName()); - Host h = Host(); printf("List:\n"); Packet p = Packet(Packet::DISCOVERY); @@ -32,19 +29,26 @@ int Program::list() { Socket s(io_service); s.setHostIp(h.getIp()); s.init(DST_PORT, SRC_PORT); - s.callback = [](Packet a) { - if (options.flags & FLAG_HEADER) { - std::cout <<"Received Header:\t"<< a.getHead() <<"\n"; - } - if (options.flags & FLAG_HEX) { - std::cout <<"Received Payload:\t"< +#include "Types/bytes.h" #include "Switch.h" -void Switch::parse(datasets arr){ - +void Switch::parse(datasets arr) { + device.type = arr[1].value; + settings.hostname = arr[2].value; + device.mac = arr[3].value; + settings.ip_addr = arr[4].value; + settings.ip_mask = arr[5].value; + settings.gateway = arr[6].value; + device.firmware_version = arr[7].value; + device.hardware_version = arr[8].value; + settings.dhcp=arr[9].value[0]; } diff --git a/src/Switch.h b/src/Switch.h index 0ecdf7f..9c368ac 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -16,6 +16,20 @@ #define DEFAULT_USER "admin" #define DEFAULT_PASS "admin" +/* + * + static bitable lookup = { + {1, "type"}, + {2, "hostname"}, + {3, "mac"}, + {4, "ip_addr"}, + {5, "ip_mask"}, + {6, "gateway"}, + {7, "firmware_version"}, + {8, "hardware_version"}, + {9, "dhcp"}}; + */ + struct vlan { int vlan_id; std::string name; @@ -40,16 +54,16 @@ public: std::string type; std::string hardware_version; std::string firmware_version; - macAddr mac {0x0,0x0,0x0,0x0,0x0,0x0}; + macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; } device; struct { std::string password = DEFAULT_PASS; std::string username = DEFAULT_USER; std::string hostname; - ipAddr ip_addr {0,0,0,0,}; + ipAddr ip_addr { 0, 0, 0, 0, }; ipAddr ip_mask; ipAddr gateway; - byte dhcp; + bool dhcp; } settings; private: std::vector vlans; diff --git a/src/Types.h b/src/Types.h index 194a865..b4d2560 100644 --- a/src/Types.h +++ b/src/Types.h @@ -39,6 +39,14 @@ public: else break; } } + + macAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + if(i<6) (*this)[i++]=b; + else break; + } + } }; class ipAddr: public std::array { @@ -56,6 +64,14 @@ public: } } + ipAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + 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++) {