diff --git a/Makefile b/Makefile index f759085..161aed4 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,19 @@ CC = g++ -CFLAGS = -g -Wall -std=c++11 +CFLAGS = -Wall -std=c++11 TARGET = smrtlink all: $(TARGET) -$(TARGET): src/*.cpp - $(CC) $(CFLAGS) -o $(TARGET) src/*.cpp src/*.h - -clean: - rm src/$(TARGET).g +$(TARGET): Types.o Program.o + $(CC) $(CFLAGS) -o $(TARGET) *.o + +Types.o: src/Types/*.cpp src/Types*.h + $(CC) $(CFLAGS) -c src/Types/*.cpp + +Program.o: src/*.cpp src/*.h + $(CC) $(CFLAGS) -c src/*.cpp + + +clean: + rm *.o diff --git a/src/Host.cpp b/src/Host.cpp index 82e220a..ccd6193 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -5,8 +5,10 @@ * Author: jdi */ + +//TODO clean up #include -#include +//#include #include #include #include @@ -16,25 +18,21 @@ #include #include #include -#include -#include "Utils.h" +//#include +#include "Options.h" #include "Host.h" - -Host::Host() { - // TODO Auto-generated constructor stub - -} +#include "Types.h" +#include "Types/bytes.h" macAddr Host::getMac() { - macAddr ret { { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c } }; - //TODO find actual MAC Address + macAddr ret { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c }; //TODO find actual MAC Address return ret; } -inetAddr Host::getIp(std::string iface) { +ipAddr Host::getIp() { struct ifaddrs *ifaddr, *ifa; int n; - inetAddr data { { 0, 0, 0, 0 } }; + ipAddr data { 0, 0, 0, 0 }; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); @@ -46,7 +44,7 @@ inetAddr Host::getIp(std::string iface) { continue; if (ifa->ifa_addr->sa_family == AF_INET) { - if (iface.compare(ifa->ifa_name) == 0) { + if (options.interface.compare(ifa->ifa_name) == 0) { memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); return data; } @@ -54,7 +52,6 @@ inetAddr Host::getIp(std::string iface) { } freeifaddrs(ifaddr); - return data; } diff --git a/src/Host.h b/src/Host.h index 8772883..f8b41ed 100644 --- a/src/Host.h +++ b/src/Host.h @@ -12,9 +12,9 @@ class Host { public: - Host(); + Host(){}; macAddr getMac(); - inetAddr getIp(std::string); + ipAddr getIp(); }; #endif /* HOST_H_ */ diff --git a/src/Packet.cpp b/src/Packet.cpp index d699726..737dc31 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -11,7 +11,8 @@ #include #include "Packet.h" #include "Types.h" -#include "Utils.h" +#include "Types/bytes.h" +#include "Types/datasets.h" Packet::Packet(OpCode c) { srand(time(NULL)); @@ -23,9 +24,8 @@ 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); - utils::print(switchMac); - printf("\n\tHost MAC:\t"); - utils::print(hostMac); + 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); @@ -161,32 +161,31 @@ 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 }; + std::vector t = { 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 = t; bytes s = key; - int i, t, j = 0; + int i, j = 0; for (int k = 0; k < len; k++) { i = (k + 1) % 256; j = (j + s[i]) % 256; - t = s[i]; - s[i] = s[j]; - s[j] = t; + std::swap(s[i], s[j]); data[k] = data[k] ^ s[(s[i] + s[j]) % 256]; } } @@ -205,14 +204,14 @@ void Packet::push(bytes &arr, int &index, bytes data) { push(arr, index, data[j]); } -void Packet::push(bytes &arr, int &index, inetAddr &data) { +void Packet::push(bytes &arr, int &index, ipAddr data) { for (unsigned j = 0; j < 4; j++) - push(arr, index, data[j]); + push(arr, index, (byte) data[j]); } -void Packet::push(bytes &arr, int &index, macAddr &data) { +void Packet::push(bytes &arr, int &index, macAddr data) { for (unsigned j = 0; j < 6; j++) - push(arr, index, data[j]); + push(arr, index, (byte) data[j]); } void Packet::push(bytes &arr, int &index, short data) { @@ -254,7 +253,7 @@ void Packet::pull(bytes &arr, int &index, macAddr &ret) { index += 6; } -void Packet::pull(bytes &arr, int &index, inetAddr &ret) { +void Packet::pull(bytes &arr, int &index, ipAddr &ret) { memcpy(&ret[0], &arr[index], 4); index += 4; } diff --git a/src/Packet.h b/src/Packet.h index 3413075..be55666 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -12,6 +12,8 @@ #define PACKET_END 0xFFFF0000 #include "Types.h" +#include "Types/bytes.h" +#include "Types/datasets.h" class Packet { public: @@ -19,8 +21,6 @@ public: DISCOVERY, GET, SET, READ }; Packet(OpCode); - virtual ~Packet() { - } void encode(bytes&); bytes getBytes(); void parse(bytes); @@ -64,15 +64,15 @@ private: void push(bytes&, int&, int); void push(bytes&, int&, byte); void push(bytes&, int&, bytes); - void push(bytes&, int&, inetAddr&); - void push(bytes&, int&, macAddr&); + 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&, inetAddr&); + 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 dff42e5..6b78267 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -7,17 +7,13 @@ #include #include -#include "Utils.h" #include "Options.h" #include "Program.h" #include "Host.h" #include "Socket.h" +#include "Switch.h" #include "Packet.h" -Program::Program() { - // TODO Auto-generated constructor stub -} - int Program::list() { //Device d = Device(); @@ -34,27 +30,21 @@ int Program::list() { try { asio::io_service io_service; Socket s(io_service); - s.setHostIp(h.getIp(options.interface)); + s.setHostIp(h.getIp()); s.init(DST_PORT, SRC_PORT); s.callback = [](Packet a) { if (options.flags & FLAG_HEADER) { - printf("Received Header:\t"); - utils::printHex(a.getHead()); - printf("\n"); + std::cout <<"Received Header:\t"<< a.getHead() <<"\n"; } if (options.flags & FLAG_HEX) { - printf("Received Payload:\t"); - utils::printHex(a.getBody()); - printf("\n"); + std::cout <<"Received Payload:\t"< #include "Packet.h" #include "Types.h" +//#include "Types.h" #define MAX_LENGTH 1024 @@ -22,7 +23,7 @@ public: void init(short, short); void send(bytes); void listen(); - void setHostIp(inetAddr); + void setHostIp(ipAddr); int (*callback)(Packet)=[](Packet a) { return 0; }; @@ -36,7 +37,7 @@ private: asio::ip::udp::endpoint wildcard_endpoint_; asio::ip::udp::endpoint local_endpoint_; bytes data = bytes(MAX_LENGTH); - inetAddr local_ip; + ipAddr local_ip; }; diff --git a/src/Switch.cpp b/src/Switch.cpp index 5ff7d8f..2259e72 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -7,8 +7,7 @@ #include "Switch.h" -Switch::Switch() { - // TODO Auto-generated constructor stub +void Switch::parse(datasets arr){ } diff --git a/src/Switch.h b/src/Switch.h index e0c8d17..0ecdf7f 100644 --- a/src/Switch.h +++ b/src/Switch.h @@ -10,6 +10,8 @@ #include #include "Types.h" +#include "Types/bytes.h" +#include "Types/datasets.h" #define DEFAULT_USER "admin" #define DEFAULT_PASS "admin" @@ -20,7 +22,8 @@ struct vlan { }; struct port { - int id; + byte id; + byte status; struct { std::vector tagged; std::vector untagged; @@ -30,26 +33,25 @@ struct port { class Switch { public: - Switch(); + Switch() { + } void parse(datasets); -private: struct { std::string type; std::string hardware_version; std::string firmware_version; - macAddr mac; + macAddr mac {0x0,0x0,0x0,0x0,0x0,0x0}; } device; struct { std::string password = DEFAULT_PASS; std::string username = DEFAULT_USER; - struct { - std::string hostname; - inetAddr ip_addr; - inetAddr ip_mask; - inetAddr gateway; - byte dhcp; - } network; + std::string hostname; + ipAddr ip_addr {0,0,0,0,}; + ipAddr ip_mask; + ipAddr gateway; + byte dhcp; } settings; +private: std::vector vlans; std::vector ports; }; diff --git a/src/Types.h b/src/Types.h index 4f0f275..194a865 100644 --- a/src/Types.h +++ b/src/Types.h @@ -8,10 +8,62 @@ #ifndef TYPES_H_ #define TYPES_H_ -#include +#include +#include +#include +#include #include #include -#include +#include "Types/bytes.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]; + } + 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; + } + } +}; + +class ipAddr: public std::array { +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; + } + } + + 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) { @@ -29,23 +81,6 @@ std::vector &operator+=(std::vector &A, const std::vector &B) { return A; } -typedef std::array macAddr; -typedef std::array inetAddr; - -typedef std::vector bytes; -typedef unsigned char byte; - -struct dataset { - short type; - short len; - bytes value; -}; - -//typedef std::vector datasets; -typedef std::map datasets; - -//std::function; -//typedef int receiveCallback; struct Options { unsigned flags = 0x00; std::string user; diff --git a/src/Utils.h b/src/Utils.h deleted file mode 100644 index fcf7217..0000000 --- a/src/Utils.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Utils.h - * - * Created on: 24.09.2015 - * Author: jdi - */ - -#ifndef UTILS_H_ -#define UTILS_H_ - -#include -#include -#include "Types.h" - -namespace utils { - -static bytes readHex(std::string d) { - std::string delimiter = ":"; - std::string token; - size_t pos = 0; - bytes arr = { }; - int hex; - byte b; - 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; - arr.push_back(b); - } - sscanf(d.c_str(), "%x", &hex); - b = hex & 0xFF; - arr.push_back(b); - return arr; -} - -static void printHex(bytes d) { - if (d.size() > 0) - printf("%.2X", d[0]); - for (unsigned i = 1; i < d.size(); i++) { - printf(":%.2X", d[i]); - } -} - -static void print(macAddr d) { - printf("%.2X", d[0]); - for (unsigned i = 1; i < 6; i++) { - printf(":%.2X", d[i]); - } -} - -static void print(inetAddr d) { - printf("%.1d", d[0]); - for (unsigned i = 1; i < 4; i++) { - printf(".%.1d", d[i]); - } -} - -static void printDec(bytes d) { - if (d.size() > 0) - printf("%.1d", d[0]); - for (unsigned i = 1; i < d.size(); i++) { - printf(".%.1d", d[i]); - } -} - -} -#endif /* UTILS_H_ */ diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 0e42e09..2db75ea 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -7,13 +7,13 @@ //============================================================================ #include -#include -#include - #include #include -#include -#include +#include +#include + +#include +#include #include "Options.h" #include "Host.h" @@ -158,8 +158,7 @@ int main(int argc, char *argv[]) { break; default: - printf("Unknown command: %s\n", argv[optind]); - optind++; + printf("Unknown command: %s\n", cmd.c_str()); while (optind < argc) { printf("->%s\n", argv[optind]); optind++;