This commit is contained in:
/jdi/ 2015-10-27 23:51:59 +01:00
parent f7d6c3109a
commit ba786e388c
6 changed files with 90 additions and 149 deletions

View file

@ -5,42 +5,41 @@
* Author: jdi
*/
#include "lookupTable.h"
#include "table.h"
static lookupTable rcv_lookup = { { 1, "type" }, //string
{ 2, "hostname" }, //string
{ 3, "mac" }, //byte[6]
{ 4, "ip_addr" }, //byte[4]
{ 5, "ip_mask" }, //byte[4]
{ 6, "gateway" }, //byte[4]
{ 7, "firmware_version" }, //string
{ 8, "hardware_version" }, //string
{ 9, "dhcp" }, //bool byte
{ 8704, "802.1q vlan" }, //???
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" }, //string
{ 8, table::STRING, "hardware_version" }, //string
{ 9, table::DEC, "dhcp" }, //bool byte
{ 8704, table::HEX, "802.1q vlan" }, //???
//{0000," "},
};
static lookupTable snd_lookup = {
static table snd_lookup = {
// TODO find out if id is unique in response
{ 10, "???" }, //after login
{ 2, "???" }, //after login
{ 512, "login_user" }, //string
{ 513, "new_user" }, //string
{ 514, "login_password" }, //string
{ 515, "new_passwoord" }, //string
{ 2200, "vlan" },
{ 2305, "???" }, //sent before login and before change hostname
{ 4608, "port_trunk" }, //byte[5] last byte bitmask??
{ 8192, "mtu_vlan" }, //byte[2] first byte bool, second byte port id
{ 8449, "port_vlan" }, //???
{ 10, table::HEX, "???" }, //after login
{ 2, table::HEX, "???" }, //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
{ 2200, table::HEX, "vlan" }, { 2305, table::HEX, "???" }, //sent before login and before change hostname
{ 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask??
{ 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id
{ 8449, table::HEX, "port_vlan" }, //???
{ 8704, "802.1q vlan" }, //??? get vlan / set status
{ 8705, "802.1q vlan" }, //???
{ 8706, "802.1q vlan pvid" }, //????
{ 8704, table::HEX, "802.1q vlan" }, //??? get vlan / set status
{ 8705, table::HEX, "802.1q vlan" }, //???
{ 8706, table::HEX, "802.1q vlan pvid" }, //????
{ 12288, "QoS Basic" }, //bool = QoS Mod
{ 16640, "port_mirror" }, //byte[10] second byte port id??
{ 17152, "loop_prevention" }, //bool byte
{ 12288, table::HEX, "QoS Basic" }, //bool = QoS Mod
{ 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??
{ 17152, table::HEX, "loop_prevention" }, //bool byte
//{0000," "},
};

View file

@ -11,7 +11,7 @@
#include "Types.h"
#define VERSION "smrtlink (v1 Linux)\n"
#define USAGE "usage: %s [-bdhrvswx] [-i interface] [-u [password:]username]\n\
#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\
[-p password] <command>\n\n"
#define HELP "\
Option Summary:\n\
@ -21,6 +21,7 @@
-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\
@ -38,8 +39,8 @@
encode use encoding algorithm on hex data separated by colon\n\
get Not yet implemented\n\
set Not yet implemented\n\
save Not yet implemented: save config to file\n\
restore Not yet implemented: restore onfig from file\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\

View file

@ -86,7 +86,21 @@ int Program::sniff() {
auto lookup=(options.flags & FLAG_REVERSE)?snd_lookup:rcv_lookup;
if(lookup.exists(d.type)) {
if(d.len>0) {
std::cout<<std::dec<<"\t++"<<std::hex<<d.type<<"++ :"<<d.value<<std::dec<<"\n";
const table::set *s = lookup.get(d.type);
switch(s->format) {
case table::STRING:
std::cout<<std::dec<<"\t"<<s->name<<": "<<&d.value[0]<<std::dec<<"\n";
break;
case table::HEX:
std::cout<<std::dec<<"\t"<<s->name<<": "<<std::hex<<d.value<<std::dec<<"\n";
break;
case table::DEC:
std::cout<<std::dec<<"\t"<<s->name<<": "<<std::dec<<d.value<<std::dec<<"\n";
break;
default:
std::cout<<std::dec<<"\t"<<s->name<<": "<<std::hex<<d.value<<std::dec<<"\n";
}
} else {
std::cout<<std::dec<<"#"<<d.type<<"\tLength: "<<d.len<<"\n";
std::cout<<std::hex<< "\tHex: " <<d.value<<"\n";
@ -120,6 +134,7 @@ int Program::encode(std::string s) {
int Program::setProperty() {
return 0;
}
int Program::getProperty() {
printf("Get:\n");
Packet p = Packet(Packet::GET);
@ -164,6 +179,7 @@ int Program::getProperty() {
return 1;
}
int Program::save() {
Switch sw = Switch();
sw.settings.hostname = "testname.lan";
@ -171,6 +187,7 @@ int Program::save() {
f.write(sw.toString());
return 1;
}
int Program::restore() {
File f;
Switch sw;
@ -180,18 +197,22 @@ int Program::restore() {
<< sw.settings.ip_addr << "\n";
return 1;
}
int Program::flash() {
return 0;
}
int Program::reboot() {
return 0;
}
int Program::reset() {
return 0;
}
void Program::init() {
if (options.interface.compare("") == 0)
options.interface = host.getIface();

View file

@ -67,62 +67,45 @@ int Switch::parse(std::string str) {
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);
}
}
}
/*
json.AddMember("ports", jsonNode(ports, json), allocator);
json.AddMember("vlans", jsonNode(vlans, json), allocator);
*/
// Since version 0.2, you can use single lookup to check the existing of member and its value:
/*
rapidjson::Value::MemberIterator hello = json.FindMember("hello");
assert(hello != json.MemberEnd());
assert(hello->value.IsString());
assert(strcmp("world", hello->value.GetString()) == 0);
(void) hello;
assert(json["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().
printf("t = %s\n", json["t"].GetBool() ? "true" : "false");
assert(json["f"].IsBool());
printf("f = %s\n", json["f"].GetBool() ? "true" : "false");
printf("n = %s\n", json["n"].IsNull() ? "null" : "?");
assert(json["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.
assert(json["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
printf("i = %d\n", json["i"].GetInt()); // Alternative (int)document["i"]
assert(json["pi"].IsNumber());
assert(json["pi"].IsDouble());
printf("pi = %g\n", json["pi"].GetDouble());
{
const rapidjson::Value& a = json["a"]; // Using a reference for consecutive access is handy and faster.
const rapidjson::Value& a = json["a"];
assert(a.IsArray());
for (rapidjson::SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
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;
// Iterating array with iterators
printf("a = ");
for (rapidjson::Value::ConstValueIterator itr = a.Begin();
itr != a.End(); ++itr)
printf("%d ", itr->GetInt());
printf("\n");
}
// Iterating object members
static const char* kTypeNames[] = { "Null", "False", "True", "Object",
"Array", "String", "Number" };
for (rapidjson::Value::ConstMemberIterator itr = json.MemberBegin();
itr != json.MemberEnd(); ++itr)
printf("Type of member %s is %s\n", itr->name.GetString(),
kTypeNames[itr->value.GetType()]);
*/
return 0;
}

View file

@ -1,32 +0,0 @@
/*
* lookupTable.h
*
* Created on: 11.10.2015
* Author: jdi
*/
#include <string>
#include "lookupTable.h"
lookupTable::lookupTable(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++;
}
}
const short& lookupTable::operator[](std::string s) {
return this->right[s]->type;
}
const std::string& lookupTable::operator[](short n) {
return this->left[n]->name;
}
bool lookupTable::exists(std::string s) {
return !(right.find(s) == right.end());
}
bool lookupTable::exists(short n) {
return !(left.find(n) == left.end());
}

View file

@ -1,31 +0,0 @@
/*
* lookupTable.h
*
* Created on: 11.10.2015
* Author: jdi
*/
#ifndef LOOKUPTABLE_H_
#define LOOKUPTABLE_H_
#include <map>
#include <vector>
class lookupTable {
public:
struct set {
short type;
std::string name;
};
lookupTable(std::initializer_list<set> l);
const short& operator[](std::string s);
const std::string& operator[](short n);
bool exists(std::string s);
bool exists(short n);
private:
std::vector<set> data;
std::map<short, set*> left;
std::map<std::string, set*> right;
};
#endif /* LOOKUPTABLE_H_ */