This commit is contained in:
j3d1 2016-01-20 20:56:00 +01:00
parent 5ff845e011
commit 057a7b4344
14 changed files with 123 additions and 222 deletions

View file

@ -1,7 +1,13 @@
# smrtlink
Command-Line Tool which might in the future be able to configure TP-Link Easy Smart Switches.
The usage is loosely based on the swconfig utility.
The usage is loosely based on the swconfig utility.
###dependencys
### dependencys
libboost-filesystem-dev
libboost-system-dev
libboost-system-dev
### compatible devices
+ TL-SG105E
+ TL-SG108E
+ TL-SG1016DE (not tested)
+ TL-SG1024DE (not tested)

View file

@ -7,7 +7,7 @@
#include <iostream>
#include "File.h"
#include "Options.h"
#include "Constant.h"
std::string File::read() {
if (!fs::exists(home)) {

View file

@ -21,7 +21,7 @@
#include <linux/if_link.h>
#include <net/if.h>
#include "Options.h"
#include "Constant.h"
#include "Host.h"
#include "Types.h"

View file

@ -16,30 +16,33 @@ static table rcv_lookup = { //
{ 6, table::DEC, "gateway" }, //+byte[4]
{ 7, table::STRING, "firmware_version" },//+
{ 8, table::STRING, "hardware_version" },//+
{ 9, table::DEC, "dhcp" }, //+bool byte
{ 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::HEX, "port_vlan_1" }, //???
{ 8450, table::HEX, "port_vlan_2" }, //???
{ 8704, table::HEX, "vlan_enabled" }, //+bool byte
{ 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::HEX, "vlan ?????" }, //????
{ 8707, table::DEC, "vlan ?????" }, //????
{ 12288, table::HEX, "qos_basic_enabled" }, //+bool = QoS Mod
{ 12288, table::BOOL, "qos_basic_enabled" }, //+bool = QoS Mod
{ 12289, table::HEX, "qos_basic" }, //+per port ???
{ 12544, table::HEX, "bandwidth_control_1" }, //+per port ???
{ 12545, table::HEX, "bandwidth_control_2" }, //+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::HEX, "loop_prevention" }, //+bool byte
{ 17152, table::BOOL, "loop_prevention" }, //+bool byte
//{0000," "},
};
@ -54,7 +57,7 @@ static table snd_lookup = {
{ 513, table::STRING, "new_user" }, //string
{ 514, table::STRING, "login_password" }, //string
{ 515, table::STRING, "new_passwoord" }, //string
{ 773, table::HEX, "reboot" }, //bool byte = save config
{ 773, table::BOOL, "reboot" }, //bool byte = save config
{ 1280, table::ACTION, "reset" }, //
{ 1536, table::ACTION, "flash" }, //update firmware
@ -68,20 +71,21 @@ static table snd_lookup = {
{ 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 ???" }, //???
{ 8448, table::HEX, "port_vlan" }, //open page
{ 8704, table::HEX, "vlan_enabled" }, //??? get vlan / set status
{ 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::HEX, "qos_basic_enabled" }, //bool = QoS Mod
{ 12288, table::BOOL, "qos_basic_enabled" }, //bool = QoS Mod
{ 12289, table::HEX, "qos_basic" }, //per port
{ 12544, table::HEX, "bandwidth_control_1" }, //per port
{ 12545, table::HEX, "bandwidth_control_2" }, //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" }, //???
{ 17152, table::HEX, "loop_prevention" }, //bool byte
{ 16896, table::HEX, "cable_test" }, //+per port ???
{ 17152, table::BOOL, "loop_prevention" }, //bool byte
//{0000," "},
};

View file

@ -1,66 +0,0 @@
/*
* Options.h
*
* Created on: 11.09.2015
* Author: jdi
*/
#ifndef OPTIONS_H_
#define OPTIONS_H_
#include "Types.h"
#define VERSION "smrtlink (v1 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_ */

View file

@ -7,7 +7,7 @@
#include <iostream>
#include <algorithm>
#include "Options.h"
#include "Constant.h"
#include "Program.h"
#include "File.h"
#include "Host.h"
@ -44,10 +44,22 @@ int printPacket(Packet p) {
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."
@ -56,6 +68,7 @@ int printPacket(Packet p) {
default:
std::cout << "+\t" << s->name << " = " << d.value
<< "\n";
break;
}
} else { //empty
std::cout << std::dec << ">\t" << s->name << "\n";
@ -149,17 +162,13 @@ int Program::getProperty() {
p.setPayload( { });
bytes b = p.getBytes();
p.encode(b);
std::cout << "count-x:" << sock.use_count() << "\n";
auto s = sock;
try {
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
std::cout << "count-y:" << sock.use_count() << "\n";
sock->callback =
[this](Packet a) {
auto s = sock;
std::cout<<"count-z:"<<sock.use_count()<<"\n";
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
@ -168,7 +177,7 @@ int Program::getProperty() {
Packet p = Packet(Packet::GET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {2305, 0, {}}};
datasets t = { {snd_lookup["ping"], 0, {}}};
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
@ -181,7 +190,11 @@ int Program::getProperty() {
Packet p = Packet(Packet::SET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {snd_lookup["login_user"], 0, {}}};
datasets t = {
{snd_lookup["login_user"], (short)(options.user.length()), options.user},
{snd_lookup["login_password"], (short)(options.password.length()), options.password}
};
std::cout<<options.user<<std::endl<<options.password<<std::endl;
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
@ -197,11 +210,7 @@ int Program::getProperty() {
return 0;
};
std::cout<<"count-b:"<<sock.use_count()<<"\n";
sock->send(c);
std::cout<<"count-a:"<<sock.use_count()<<"\n";
return 0;
};
sock->send(b);
@ -245,6 +254,11 @@ int Program::reset() {
return 0;
}
int Program::ping(std::function<int(Packet)>){
return 0;
}
void Program::init() {
if (options.interface.compare("") == 0)
options.interface = host.getIface();

View file

@ -19,6 +19,7 @@ private:
std::shared_ptr<boost::asio::io_service> io_service;
std::shared_ptr<Socket> sock;
Host host = Host();
int ping(std::function<int(Packet)>);
public:
Program() {
io_service = std::make_shared<boost::asio::io_service>();

View file

@ -9,7 +9,7 @@
#include <unistd.h>
#include "Socket.h"
#include "Packet.h"
#include "Options.h"
#include "Constant.h"
#include "Host.h"
#include "Types.h"

View file

@ -9,8 +9,7 @@
#include "Types.h"
#include "Switch.h"
#include "Lookup.h"
#include "jsonNode.h"
#include "Options.h"
#include "Constant.h"
#include "table.h"
int Switch::parse(datasets arr) {
@ -22,129 +21,45 @@ int Switch::parse(datasets arr) {
int Switch::parse(dataset d) {
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"]) {
device.type = d.value;
}
if (d.type == lookup["mac"]) {
else if (id == "mac") {
device.mac = d.value;
}
if (d.type == lookup["firmware_version"]) {
else if (d.type == lookup["firmware_version"]) {
device.firmware_version = d.value;
}
if (d.type == lookup["hardware_version"]) {
else if (id == "hardware_version") {
device.hardware_version = d.value;
}
if (d.type == lookup["ports"]) {
else if (id == "ports") {
device.ports = d.value[0];
}
if (d.type == lookup["hostname"]) {
else if (id == "hostname") {
settings.hostname = d.value;
}
if (d.type == lookup["ip_addr"]) {
else if (id == "ip_addr") {
settings.ip_addr = d.value;
}
if (d.type == lookup["ip_mask"]) {
else if (id == "ip_mask") {
settings.ip_mask = d.value;
}
if (d.type == lookup["gateway"]) {
else if (id == "gateway") {
settings.gateway = d.value;
}
if (d.type == lookup["dhcp"]) {
else if (id == "dhcp_enabled") {
settings.dhcp = d.value[0];
}
if (d.type == lookup["loop_prevention"]) {
else if (id == "loop_prevention") {
settings.loop_prevention = d.value[0];
}
if (d.type == lookup["qos_basic_enabled"]) {
else if (id == "qos_basic_enabled") {
settings.qos_enabled = d.value[0];
}
if (d.type == lookup["vlan_enabled"]) {
else if (id == "vlan_enabled") {
settings.vlan_enabled = d.value[0];
}
return 0;
}
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();
}

View file

@ -17,6 +17,10 @@
#define DEFAULT_USER "admin"
#define DEFAULT_PASS "admin"
enum CntStatus {
OPEN, SHORT
};
struct vlan {
int vlan_id;
std::string name;
@ -24,17 +28,34 @@ struct vlan {
std::vector<byte> untagged_member;
};
struct trunk {
int trunk_id;
std::vector<byte> member;
};
struct port {
byte id;
byte status;
int pvid;
//port_settings
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
//bandwidth_control_1
//bandwidth_control_2
//storm_control
//port_statistics
//cable_test
};
class Switch {
@ -45,6 +66,7 @@ public:
int parse(dataset);
int parse(std::string);
std::string toString();
struct {
std::string type;
std::string hardware_version;
@ -63,7 +85,9 @@ public:
bool loop_prevention;
bool qos_enabled;
bool vlan_enabled;
//mtu_vlan
} settings;
private:
rapidjson::Document json;
std::vector<vlan> vlans;

View file

@ -7,22 +7,23 @@
#include "bytes.h"
bytes::bytes(std::string d) {
vector();
bytes bytes::readHex(std::string s){
vector ret;
std::string delimiter = ":";
std::string token;
size_t pos = 0;
int hex;
byte b;
resize(0);
while ((pos = d.find(delimiter)) != std::string::npos) {
token = d.substr(0, pos);
ret.resize(0);
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
sscanf(token.c_str(), "%x", &hex);
d.erase(0, pos + delimiter.length());
s.erase(0, pos + delimiter.length());
b = hex & 0xFF;
push_back(b);
ret.push_back(b);
}
sscanf(d.c_str(), "%x", &hex);
sscanf(s.c_str(), "%x", &hex);
b = hex & 0xFF;
push_back(b);
ret.push_back(b);
return ret;
}

View file

@ -24,11 +24,11 @@ public:
using vector::operator[];
bytes() {
}
bytes(int n) :
vector(n) {
bytes(int n) : vector(n){
}
bytes(std::string);
bytes(std::string d) : vector(d.begin(), d.end()){
}
bytes(std::initializer_list<uint8_t> s)
{
@ -42,9 +42,7 @@ public:
this->insert(this->begin(), B.begin(), B.end());
}
bytes readHex(std::string s){
return bytes(s);
}
bytes readHex(std::string);
bytes operator=(const vector &B) {
this->reserve(B.size());

View file

@ -16,9 +16,10 @@
#include <getopt.h>
#include <unistd.h>
#include "Options.h"
#include "Constant.h"
#include "Host.h"
#include "Program.h"
#include "Switch.h"
#define no_argument 0
#define required_argument 1
@ -33,6 +34,9 @@ constexpr unsigned int caseArg(const char* str, int h = 0) {
int main(int argc, char *argv[]) {
int opt, index;
options.user = DEFAULT_USER;
options.password = DEFAULT_PASS;
const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
"help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, {
"permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0,

View file

@ -13,7 +13,7 @@
class table {
public:
enum F {STRING,HEX,DEC,ACTION,EMPTY};
enum F {STRING,HEX,DEC,ACTION,BOOL,EMPTY};
struct set {
short type;
F format;