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_ */