implementing -I, -s and reset

This commit is contained in:
j3d1 2016-01-27 23:54:19 +01:00
parent a42a0aa8ce
commit 164edcdad9
8 changed files with 251 additions and 111 deletions

View file

@ -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 <iface> only use one Interface\n\
-u --user <[password:]username>\n\
-p --password <password>\n\
Login with Username and Password\n\
-f --file <path> choose a settings file\n\n\
-t --timeout <n> 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 <password>\n\
Login with Username and Password\n\
-f --file <path> 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:<type>] [<filter>]\n\

33
src/Interactive.cpp Normal file
View file

@ -0,0 +1,33 @@
/*
* Interactive.cpp
*
* Created on: 27.01.2016
* Author: jedi
*/
#include <iostream>
#include <string>
#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 <<endl;;
}
return 0;
}

18
src/Interactive.h Normal file
View file

@ -0,0 +1,18 @@
/*
* Interactive.h
*
* Created on: 27.01.2016
* Author: jedi
*/
#ifndef INTERACTIVE_H_
#define INTERACTIVE_H_
class Interactive {
public:
Interactive();
virtual ~Interactive();
int run();
};
#endif /* INTERACTIVE_H_ */

View file

@ -17,10 +17,12 @@
#include "lookup.h"
#include "table.h"
using namespace std;
int printHeader(Packet p) {
if (options.flags.HEADER) {
if (options.flags.HEX) {
std::cout << "Received Header:\n\t" << p.getHead() << "\n";
cout << "Received Header:\n\t" << p.getHead() << "\n";
} else {
p.printHeader();
printf("\n");
@ -31,55 +33,50 @@ int printHeader(Packet p) {
int printPacket(Packet p) {
if (options.flags.HEX) {
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
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) {
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"<<a.getBody()<<"\n";
cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else {
datasets d =a.getPayload();
Switch sw = Switch();
@ -105,8 +102,8 @@ int Program::list() {
return 0;
});
io_service->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<std::string,std::string> prop) {
int Program::setProperty(map<string, string> 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<std::string> 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<string> 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<std::string> 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 <<sw.settings.hostname<<"\t";
cout <<sw.settings.hostname<<"\t";
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}}};
datasets t = { {REBOOT, 1, {options.flags.PERMANENT?(byte)1:(byte)0}}};
set(a,t,[this](Packet a) {
if( a.getOpCode()==Packet::CONFIRM)
std::cout<< "rebooting now.\n";
cout<< "rebooting now.\n";
return 0;
});
return 0;
@ -251,18 +261,42 @@ int Program::reboot() {
return 0;
});
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::reset() {
return 1;
try {
discover([this](Packet a) {
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
cout <<sw.settings.hostname<<"\t";
datasets t = { {SND_PING, 0, {}}};
get(a, t, [this](Packet a) {
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
datasets t = { {RESET, 0, {}}};
set(a,t,[this](Packet a) {
if( a.getOpCode()==Packet::CONFIRM)
cout<< "reseting config.\n";
return 0;
});
return 0;
});
return 0;
});
io_service->run();
} catch (exception& e) {
cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
int Program::discover(std::function<int(Packet)> c) {
int Program::discover(function<int(Packet)> c) {
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac());
p.setPayload( { });
@ -271,7 +305,7 @@ int Program::discover(std::function<int(Packet)> c) {
return 0;
}
int Program::get(Packet l, datasets t, std::function<int(Packet)> c) {
int Program::get(Packet l, datasets t, function<int(Packet)> 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<int(Packet)> c) {
return 0;
}
int Program::set(Packet l, datasets t, std::function<int(Packet)> c) {
int Program::set(Packet l, datasets t, function<int(Packet)> 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);

View file

@ -120,6 +120,7 @@ struct Options {
bool HEADER;
bool PERMANENT;
bool WAIT;
bool INTERACTIVE;
} flags;
std::string user;
std::string password;

View file

@ -28,6 +28,7 @@ public:
}
bytes(std::string d) : vector(d.begin(), d.end()){
this->push_back('\0');
}
bytes(std::initializer_list<uint8_t> s)

36
src/lookup/input.lst Normal file
View file

@ -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

View file

@ -13,12 +13,12 @@
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <getopt.h>
#include <unistd.h>
#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<std::string> vect;
std::map<std::string, std::string> 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);
}