merging dev
This commit is contained in:
commit
f2d85a67a5
15 changed files with 825 additions and 486 deletions
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
CC = g++
|
||||
CFLAGS = -g -c -std=c++14
|
||||
CFLAGS = -o0 -g -c -std=c++14
|
||||
TARGET = smrtlink
|
||||
SOURCEDIR = src
|
||||
BUILDDIR = bin
|
||||
|
|
66
src/Constant.h
Normal file
66
src/Constant.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Options.h
|
||||
*
|
||||
* Created on: 11.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#ifndef OPTIONS_H_
|
||||
#define OPTIONS_H_
|
||||
|
||||
#include "Types.h"
|
||||
|
||||
#define VERSION "smrtlink (v1.1 Linux)\n"
|
||||
#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\
|
||||
[-p password] <command>\n\n"
|
||||
#define HELP "\
|
||||
Option Summary:\n\
|
||||
-h --help This help text\n\
|
||||
-v --version Display version of this tool\n\
|
||||
-d --debug [n] Show debugging messages\n\
|
||||
-r 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\
|
||||
Command Summary:\n\
|
||||
help This help text\n\
|
||||
list list all connected switches\n\
|
||||
sniff [type:<type>] [<filter>]\n\
|
||||
capture and display all incoming or outgoing packets\n\
|
||||
depending on the --reverse option\n\
|
||||
encode use encoding algorithm on hex data separated by colon\n\
|
||||
get Not yet implemented\n\
|
||||
set Not yet implemented\n\
|
||||
get|save Not yet implemented: save config to file\n\
|
||||
set|restore Not yet implemented: restore onfig from file\n\
|
||||
flash Not yet implemented: replace firmware\n\
|
||||
reboot Not yet implemented\n\
|
||||
reset Not yet implemented\n\n\
|
||||
### for questions please contact <smrtlink@jdi.li> ###\n\n"
|
||||
//TODO
|
||||
/*
|
||||
* Stdin
|
||||
* socketmode
|
||||
*
|
||||
*/
|
||||
|
||||
#define FLAG_HEX 1
|
||||
#define FLAG_REVERSE 2
|
||||
#define FLAG_HEADER 4
|
||||
#define FLAG_PERMANENT 8
|
||||
#define FLAG_WAIT 16
|
||||
#define FLAG_DEBUG 32
|
||||
|
||||
extern Options options;
|
||||
|
||||
#endif /* OPTIONS_H_ */
|
91
src/Lookup.h
91
src/Lookup.h
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Switch.cpp
|
||||
*
|
||||
* Created on: 29.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#include "table.h"
|
||||
|
||||
static table rcv_lookup = { //
|
||||
{ 1, table::STRING, "type" }, //+string
|
||||
{ 2, table::STRING, "hostname" }, //+string
|
||||
{ 3, table::HEX, "mac" }, //+byte[6]
|
||||
{ 4, table::DEC, "ip_addr" }, //+byte[4]
|
||||
{ 5, table::DEC, "ip_mask" }, //+byte[4]
|
||||
{ 6, table::DEC, "gateway" }, //+byte[4]
|
||||
{ 7, table::STRING, "firmware_version" },//+
|
||||
{ 8, table::STRING, "hardware_version" },//+
|
||||
{ 9, table::BOOL, "dhcp_enabled" }, //+bool byte
|
||||
{ 10, table::DEC, "ports" }, //+byte, maybe number of ports
|
||||
|
||||
{ 2101, table::HEX, "port_vlan ????????" },
|
||||
|
||||
{ 4352, table::HEX, "igmp_snooping" }, //???
|
||||
{ 4096, table::HEX, "port_settings" }, //+per port
|
||||
{ 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask??
|
||||
|
||||
{ 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool,second byte port id
|
||||
{ 8448, table::BOOL, "port_vlan_enabled" }, //open page
|
||||
{ 8449, table::HEX, "port_vlan" }, //???
|
||||
{ 8450, table::DEC, "port_vlan_max" }, //???
|
||||
{ 8704, table::BOOL, "vlan_enabled" }, //+bool byte
|
||||
{ 8705, table::HEX, "vlan" }, //+one set per vlan
|
||||
{ 8706, table::HEX, "vlan_pvid" }, //+per port
|
||||
{ 8707, table::DEC, "vlan ?????" }, //????
|
||||
|
||||
{ 12288, table::BOOL, "qos_basic_enabled" }, //+bool = QoS Mod
|
||||
{ 12289, table::HEX, "qos_basic" }, //+per port ???
|
||||
{ 12544, table::HEX, "bandwidth_control_ingress" }, //+per port ???
|
||||
{ 12545, table::HEX, "bandwidth_control_egress" }, //+per port ???
|
||||
{ 12800, table::HEX, "storm_control" }, //+per port ???
|
||||
{ 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id???
|
||||
{ 16384, table::HEX, "port_statistics" }, //+per port ???
|
||||
{ 16896, table::HEX, "cable_test" }, //+per port ???
|
||||
{ 17152, table::BOOL, "loop_prevention" }, //+bool byte
|
||||
|
||||
//{0000," "},
|
||||
};
|
||||
|
||||
static table snd_lookup = {
|
||||
|
||||
// TODO find out if id is unique in response
|
||||
{ 2, table::HEX, "system_info" }, //page sysinfo
|
||||
{ 9, table::HEX, "ip_config" }, //page sysinfo
|
||||
{ 10, table::HEX, "ports" }, //after login
|
||||
{ 512, table::STRING, "login_user" }, //string
|
||||
{ 513, table::STRING, "new_user" }, //string
|
||||
{ 514, table::STRING, "login_password" }, //string
|
||||
{ 515, table::STRING, "new_passwoord" }, //string
|
||||
{ 773, table::BOOL, "reboot" }, //bool byte = save config
|
||||
|
||||
{ 1280, table::ACTION, "reset" }, //
|
||||
{ 1536, table::ACTION, "flash" }, //update firmware
|
||||
|
||||
{ 2200, table::HEX, "vlan" }, //vlan
|
||||
{ 2304, table::ACTION, "save" }, //save
|
||||
{ 2305, table::ACTION, "ping" }, //sent before SET ???
|
||||
|
||||
{ 4352, table::HEX, "igmp_snooping" }, //switching
|
||||
{ 4096, table::HEX, "port_settings" }, //switching
|
||||
{ 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask??
|
||||
|
||||
{ 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id
|
||||
{ 8448, table::BOOL, "port_vlan_enabled" }, //open page
|
||||
{ 8449, table::HEX, "port_vlan" }, //???
|
||||
{ 8704, table::BOOL, "vlan_enabled" }, //??? get vlan / set status
|
||||
{ 8705, table::HEX, "vlan" }, // per vlan
|
||||
{ 8706, table::HEX, "vlan_pvid" }, //per port
|
||||
|
||||
{ 12288, table::BOOL, "qos_basic_enabled" }, //bool = QoS Mod
|
||||
{ 12289, table::HEX, "qos_basic" }, //per port
|
||||
{ 12544, table::HEX, "bandwidth_control_ingress" }, //per port
|
||||
{ 12545, table::HEX, "bandwidth_control_egress" }, //per port
|
||||
{ 12800, table::HEX, "storm_control" }, //???
|
||||
{ 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??
|
||||
{ 16384, table::HEX, "port_statistics" }, //???
|
||||
{ 16896, table::HEX, "cable_test" }, //+per port ???
|
||||
{ 17152, table::BOOL, "loop_prevention" }, //bool byte
|
||||
|
||||
//{0000," "},
|
||||
};
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
Packet::Packet(OpCode c) {
|
||||
srand(time(NULL));
|
||||
sequenceId = rand() % 1000;
|
||||
sequenceId = sequenceId != 0 ? sequenceId + 1 : rand() % 1000;
|
||||
opCode = c;
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,7 @@ void Packet::printHeader() {
|
|||
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 << "\ttokenID: \t" << tokenId << "\n";
|
||||
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
|
||||
}
|
||||
|
||||
|
@ -111,12 +112,16 @@ void Packet::setCheckSum(int checkSum) {
|
|||
this->checkSum = checkSum;
|
||||
}
|
||||
|
||||
int Packet::getErrorCode() const {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
short Packet::getSequenceId() const {
|
||||
return sequenceId;
|
||||
}
|
||||
|
||||
void Packet::setSequenceId(short sequenceId) {
|
||||
this->sequenceId = sequenceId;
|
||||
void Packet::setSequenceId(short sId) {
|
||||
sequenceId = sId;
|
||||
}
|
||||
|
||||
macAddr Packet::getSwitchMac() const {
|
||||
|
@ -135,6 +140,14 @@ void Packet::setPayload(datasets payload) {
|
|||
this->payload = payload;
|
||||
}
|
||||
|
||||
short Packet::getTokenId() const {
|
||||
return tokenId;
|
||||
}
|
||||
|
||||
void Packet::setTokenId(short tokenId) {
|
||||
this->tokenId = tokenId;
|
||||
}
|
||||
|
||||
std::string Packet::opCodeToString() {
|
||||
switch (opCode) {
|
||||
case DISCOVERY:
|
||||
|
|
17
src/Packet.h
17
src/Packet.h
|
@ -13,11 +13,12 @@
|
|||
|
||||
#include "Types.h"
|
||||
|
||||
class Packet {
|
||||
static short sequenceId=0;
|
||||
|
||||
class Packet
|
||||
{
|
||||
public:
|
||||
enum OpCode {
|
||||
DISCOVERY, GET, RETURN, SET, CONFIRM
|
||||
};
|
||||
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
|
||||
Packet(OpCode);
|
||||
void encode(bytes&);
|
||||
bytes getBytes();
|
||||
|
@ -26,6 +27,7 @@ public:
|
|||
std::string opCodeToString();
|
||||
short getLength() const;
|
||||
int getCheckSum() const;
|
||||
int getErrorCode() const;
|
||||
short getSequenceId() const;
|
||||
macAddr getSwitchMac() const;
|
||||
const bytes& getBody() const;
|
||||
|
@ -37,27 +39,25 @@ public:
|
|||
void setCheckSum(int);
|
||||
void setSequenceId(short );
|
||||
void setPayload(datasets payload);
|
||||
short getTokenId() const;
|
||||
void setTokenId(short tokenId = 0);
|
||||
|
||||
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 sequenceId;
|
||||
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);
|
||||
|
@ -65,7 +65,6 @@ private:
|
|||
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&);
|
||||
|
|
139
src/Program.cpp
139
src/Program.cpp
|
@ -14,7 +14,7 @@
|
|||
#include "Socket.h"
|
||||
#include "Switch.h"
|
||||
#include "Packet.h"
|
||||
#include "Lookup.h"
|
||||
#include "lookup.h"
|
||||
#include "table.h"
|
||||
|
||||
int printHeader(Packet p) {
|
||||
|
@ -30,6 +30,7 @@ int printHeader(Packet p) {
|
|||
}
|
||||
|
||||
int printPacket(Packet p) {
|
||||
<<<<<<< HEAD
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||
} else {
|
||||
|
@ -84,6 +85,63 @@ int printPacket(Packet p) {
|
|||
}
|
||||
}
|
||||
return 0;
|
||||
=======
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||
} else {
|
||||
for (dataset d : p.getPayload()) {
|
||||
auto lookup =
|
||||
(options.flags & FLAG_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";
|
||||
break;
|
||||
case table::BOOL:
|
||||
std::cout << "+\t" << s.name << " = "
|
||||
<< (d.value[0] ? "YES" : "NO") << "\n";
|
||||
break;
|
||||
case table::HEX:
|
||||
std::cout << "+\t" << s.name << " = " << d.value
|
||||
<< "\n";
|
||||
break;
|
||||
case table::DEC:
|
||||
std::cout << "+\t" << s.name << " = ";
|
||||
if (d.value.size() > 0)
|
||||
std::cout << std::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";
|
||||
break;
|
||||
case table::ACTION:
|
||||
std::cout << "Error:" << s.name
|
||||
<< " is marked as 'action' but carries payload."
|
||||
<< d.value << "\n";
|
||||
break;
|
||||
default:
|
||||
std::cout << "+\t" << s.name << " = " << d.value
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
} else { //empty
|
||||
std::cout << std::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";
|
||||
} else { //empty
|
||||
std::cout << "#>\t" << d.type << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
}
|
||||
|
||||
int Program::list() {
|
||||
|
@ -109,7 +167,7 @@ int Program::list() {
|
|||
sw.parse(d);
|
||||
File f;
|
||||
f.write(sw.toString());
|
||||
std::cout <<"Devices:\n\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
|
@ -156,6 +214,7 @@ int Program::setProperty() {
|
|||
}
|
||||
|
||||
int Program::getProperty() {
|
||||
<<<<<<< HEAD
|
||||
std::cout << "List:\n";
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
p.setHostMac(host.getMac());
|
||||
|
@ -219,6 +278,77 @@ int Program::getProperty() {
|
|||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 1;
|
||||
=======
|
||||
std::cout << "List:\n";
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
p.setHostMac(host.getMac());
|
||||
p.setPayload( { });
|
||||
bytes b = p.getBytes();
|
||||
p.encode(b);
|
||||
auto s = sock;
|
||||
try {
|
||||
sock->setHostIp(host.getIp());
|
||||
sock->init(DST_PORT, SRC_PORT);
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
auto s = sock;
|
||||
datasets d =a.getPayload();
|
||||
Switch sw = Switch();
|
||||
sw.parse(d);
|
||||
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||
|
||||
Packet p = Packet(Packet::GET);
|
||||
p.setSwitchMac(a.getSwitchMac());
|
||||
p.setHostMac(host.getMac());
|
||||
datasets t = { {SND_PING, 0, {}}};
|
||||
p.setPayload(t);
|
||||
bytes c = p.getBytes();
|
||||
p.encode(c);
|
||||
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
auto s = sock;
|
||||
datasets d =a.getPayload();
|
||||
Switch sw = Switch();
|
||||
sw.parse(d);
|
||||
Packet p = Packet(Packet::SET);
|
||||
p.setSwitchMac(a.getSwitchMac());
|
||||
p.setTokenId(a.getTokenId());
|
||||
p.setHostMac(host.getMac());
|
||||
bytes n = options.user;
|
||||
bytes w = options.password;
|
||||
n.push_back('\0');
|
||||
w.push_back('\0');
|
||||
datasets t = {
|
||||
{ LOGIN_USER, (short)(n.size()), n},
|
||||
{ LOGIN_PASSWORD, (short)(w.size()), w},
|
||||
{ REBOOT, 1, {0}}
|
||||
};
|
||||
p.setPayload(t);
|
||||
bytes c = p.getBytes();
|
||||
p.encode(c);
|
||||
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
std::cout << a.opCodeToString() << "\n";
|
||||
printHeader(a);
|
||||
printPacket(a);
|
||||
return 0;
|
||||
};
|
||||
sock->send(c);
|
||||
return 0;
|
||||
};
|
||||
|
||||
sock->send(c);
|
||||
return 0;
|
||||
};
|
||||
sock->send(b);
|
||||
io_service->run();
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 1;
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
}
|
||||
|
||||
int Program::save() {
|
||||
|
@ -259,6 +389,11 @@ int Program::ping(std::function<int(Packet)>){
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Program::ping(std::function<int(Packet)>){
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Program::init() {
|
||||
if (options.interface.compare("") == 0)
|
||||
options.interface = host.getIface();
|
||||
|
|
|
@ -30,7 +30,6 @@ void Socket::init(short dst_port, short src_port) {
|
|||
if (options.flags & FLAG_DEBUG)
|
||||
std::cout << "Local IP:\t" << local_ip << "\n";
|
||||
|
||||
|
||||
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
||||
local_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
|
@ -89,9 +88,11 @@ void Socket::listen() {
|
|||
// TODO distinguish error codes
|
||||
} else {
|
||||
data.resize(bytes_recvd);
|
||||
Packet p = Packet(Packet::RETURN);
|
||||
Packet p = Packet(Packet::NONE);
|
||||
p.encode(data);
|
||||
// std::cout << "err" << p.getErrorCode() <<std::endl;
|
||||
p.parse(data);
|
||||
//std::cout << "err" << p.getErrorCode() <<std::endl;
|
||||
if(!callback(p)) {
|
||||
//TODO do something
|
||||
}
|
||||
|
|
98
src/Switch-Json.cpp
Normal file
98
src/Switch-Json.cpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Switch.cpp
|
||||
*
|
||||
* Created on: 29.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include "Types.h"
|
||||
#include "Switch.h"
|
||||
#include "jsonNode.h"
|
||||
#include "Constant.h"
|
||||
#include "table.h"
|
||||
|
||||
int Switch::parse(std::string str) {
|
||||
|
||||
if (json.Parse(str.c_str()).HasParseError())
|
||||
return 1;
|
||||
if (options.flags & FLAG_DEBUG)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
{
|
||||
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;
|
||||
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Switch::toString() {
|
||||
|
||||
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);
|
||||
|
||||
rapidjson::StringBuffer sb;
|
||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
|
||||
json.Accept(writer);
|
||||
return sb.GetString();
|
||||
}
|
||||
|
|
@ -8,7 +8,11 @@
|
|||
#include <string>
|
||||
#include "Types.h"
|
||||
#include "Switch.h"
|
||||
<<<<<<< HEAD
|
||||
#include "Lookup.h"
|
||||
=======
|
||||
#include "lookup.h"
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
#include "Constant.h"
|
||||
#include "table.h"
|
||||
|
||||
|
@ -20,6 +24,7 @@ int Switch::parse(datasets arr) {
|
|||
}
|
||||
|
||||
int Switch::parse(dataset d) {
|
||||
<<<<<<< HEAD
|
||||
auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
|
||||
std::string id = lookup.exists(d.type)?lookup[d.type]:"unknown";
|
||||
if (d.type == lookup["type"]) {
|
||||
|
@ -63,3 +68,49 @@ int Switch::parse(dataset d) {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
=======
|
||||
switch (d.type) {
|
||||
case RCV_TYPE:
|
||||
device.type = d.value;
|
||||
break;
|
||||
case RCV_MAC:
|
||||
device.mac = d.value;
|
||||
break;
|
||||
case FIRMWARE_VERSION:
|
||||
device.firmware_version = d.value;
|
||||
break;
|
||||
case HARDWARE_VERSION:
|
||||
device.hardware_version = d.value;
|
||||
break;
|
||||
case PORTS:
|
||||
device.ports = d.value[0];
|
||||
break;
|
||||
case HOSTNAME:
|
||||
settings.hostname = d.value;
|
||||
break;
|
||||
case IP_ADDR:
|
||||
settings.ip_addr = d.value;
|
||||
break;
|
||||
case IP_MASK:
|
||||
settings.ip_mask = d.value;
|
||||
break;
|
||||
case GATEWAY:
|
||||
settings.gateway = d.value;
|
||||
break;
|
||||
case DHCP_ENABLED:
|
||||
settings.dhcp = d.value[0];
|
||||
break;
|
||||
case LOOP_PREVENTION:
|
||||
settings.loop_prevention = d.value[0];
|
||||
break;
|
||||
case QOS_BASIC_ENABLED:
|
||||
settings.qos_enabled = d.value[0];
|
||||
break;
|
||||
case VLAN_ENABLED:
|
||||
settings.vlan_enabled = d.value[0];
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
|
|
30
src/lookup.h
Normal file
30
src/lookup.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
enum {
|
||||
#define LOOKUP_SET(id, num, type) RCV_ ## id = num,
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
#define LOOKUP_SET(id, num, type) SND_ ## id = num,
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
#define LOOKUP_SET(id, num, type) id = num,
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
||||
static table rcv_lookup {
|
||||
#define LOOKUP_SET(id, num, type) { RCV_ ## id, table::type, #id },
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
||||
static table snd_lookup {
|
||||
#define LOOKUP_SET(id, num, type) { SND_ ## id, table::type, #id },
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
25
src/lookup/general.lst
Normal file
25
src/lookup/general.lst
Normal file
|
@ -0,0 +1,25 @@
|
|||
LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports
|
||||
|
||||
LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //???
|
||||
LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port
|
||||
LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask??
|
||||
|
||||
LOOKUP_SET(MTU_VLAN, 8192, HEX) //byte[2] first byte bool,second byte port id
|
||||
LOOKUP_SET(PORT_VLAN_ENABLED, 8448, BOOL) //open page
|
||||
LOOKUP_SET(PORT_VLAN, 8449, HEX)
|
||||
LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC)
|
||||
LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte
|
||||
LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan
|
||||
LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port
|
||||
LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //????
|
||||
|
||||
LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod
|
||||
LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ???
|
||||
LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ???
|
||||
LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ???
|
||||
LOOKUP_SET(STORM_CONTROL, 12800, HEX) //+per port ???
|
||||
LOOKUP_SET(PORT_MIRROR, 16640, HEX) //byte[10] second byte port id???
|
||||
LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ???
|
||||
LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ???
|
||||
LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte
|
||||
|
13
src/lookup/rcv.lst
Normal file
13
src/lookup/rcv.lst
Normal file
|
@ -0,0 +1,13 @@
|
|||
LOOKUP_SET(TYPE, 1, STRING) //+string
|
||||
LOOKUP_SET(HOSTNAME, 2, STRING) //+string
|
||||
LOOKUP_SET(MAC, 3, HEX) //+byte[6]
|
||||
LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4]
|
||||
LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4]
|
||||
LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4]
|
||||
LOOKUP_SET(FIRMWARE_VERSION, 7, STRING)
|
||||
LOOKUP_SET(HARDWARE_VERSION, 8, STRING)
|
||||
LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte
|
||||
|
||||
LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX)
|
||||
|
||||
|
17
src/lookup/snd.lst
Normal file
17
src/lookup/snd.lst
Normal file
|
@ -0,0 +1,17 @@
|
|||
LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo
|
||||
LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo
|
||||
|
||||
|
||||
LOOKUP_SET(LOGIN_USER, 512, STRING) //string
|
||||
LOOKUP_SET(NEW_USER, 513, STRING) //string
|
||||
LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string
|
||||
LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string
|
||||
LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config
|
||||
|
||||
LOOKUP_SET(RESET, 1280, ACTION) //
|
||||
LOOKUP_SET(FLASH, 1536, ACTION) //update firmware
|
||||
LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan
|
||||
LOOKUP_SET(SAVE, 2304, ACTION) //save
|
||||
LOOKUP_SET(PING, 2305, ACTION) //sent before SET ???
|
||||
|
||||
|
|
@ -10,29 +10,16 @@
|
|||
|
||||
table::table(std::initializer_list<set> l) {
|
||||
int i = 0;
|
||||
this->data.resize(l.size());
|
||||
for (set s : l) {
|
||||
this->data[i] = s;
|
||||
this->left[s.type] = &this->data[i];
|
||||
this->right[s.name] = &this->data[i];
|
||||
i++;
|
||||
this->left[s.type] = s;
|
||||
}
|
||||
}
|
||||
short table::operator[](std::string s){
|
||||
return this->right[s]->type;
|
||||
}
|
||||
std::string table::operator[](short n){
|
||||
return this->left[n]->name;
|
||||
}
|
||||
bool table::exists(std::string s){
|
||||
return !(right.find(s) == right.end());
|
||||
table::set table::operator[](short n){
|
||||
return this->left[n];
|
||||
}
|
||||
bool table::exists(short n){
|
||||
return !(left.find(n) == left.end());
|
||||
}
|
||||
const table::set* table::get(std::string s){
|
||||
return this->right[s];
|
||||
}
|
||||
const table::set* table::get(short n){
|
||||
return this->left[n];
|
||||
std::string table::name(short n){
|
||||
return this->left[n].name;
|
||||
}
|
||||
|
|
11
src/table.h
11
src/table.h
|
@ -20,16 +20,11 @@ public:
|
|||
std::string name;
|
||||
};
|
||||
table(std::initializer_list<set> l);
|
||||
short operator[](std::string);
|
||||
std::string operator[](short);
|
||||
bool exists(std::string);
|
||||
set operator[](short);
|
||||
bool exists(short);
|
||||
const set* get(std::string);
|
||||
const set* get(short);
|
||||
std::string name(short);
|
||||
private:
|
||||
std::vector<set> data;
|
||||
std::map<short, set*> left;
|
||||
std::map<std::string, set*> right;
|
||||
std::map<short, set> left;
|
||||
};
|
||||
|
||||
#endif /* LOOKUPTABLE_H_ */
|
||||
|
|
Loading…
Reference in a new issue