Compare commits
18 commits
Author | SHA1 | Date | |
---|---|---|---|
1f3730ac87 | |||
f15abc71be | |||
69198cd6fe | |||
e494a7db91 | |||
507cfe2140 | |||
a90401ddeb | |||
49ab8a307e | |||
5b7014737a | |||
59796d600b | |||
164edcdad9 | |||
a42a0aa8ce | |||
2952953f5d | |||
0c289aff9c | |||
8200103514 | |||
d3b89e3b11 | |||
693d8d42ff | |||
922ac9f88e | |||
057a7b4344 |
37 changed files with 1602 additions and 1153 deletions
3
Makefile
3
Makefile
|
@ -15,8 +15,7 @@ $(BUILDDIR):
|
|||
|
||||
|
||||
$(TARGET): $(OBJECTS)
|
||||
$(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system
|
||||
|
||||
$(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lreadline -lboost_program_options
|
||||
|
||||
$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp
|
||||
$(CC) $(CFLAGS) $< -o $@
|
||||
|
|
|
@ -3,8 +3,10 @@ Command-Line Tool which might in the future be able to configure TP-Link Easy Sm
|
|||
The usage is loosely based on the swconfig utility.
|
||||
|
||||
### dependencys
|
||||
libboost-filesystem-dev
|
||||
libboost-system-dev
|
||||
libboost-filesystem
|
||||
libboost-system
|
||||
libboost-program-options
|
||||
libreadline
|
||||
|
||||
### compatible devices
|
||||
+ TL-SG105E
|
||||
|
|
|
@ -11,38 +11,45 @@
|
|||
#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 USAGE "usage: %s [-bdhjrvVstwx] [-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\
|
||||
-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\
|
||||
-v --verbose [n] Show debugging messages\n\
|
||||
\
|
||||
-r --reverse switch ports to emulate switch while sniffing\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 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\
|
||||
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\
|
||||
get <property1> [<property1> [...]]\n\
|
||||
Not yet implemented\n\
|
||||
set <property1>=<value> [<property2>=<value> [...]]\n\
|
||||
Not yet implemented\n\
|
||||
save Not yet implemented: save config to file\n\
|
||||
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\
|
||||
|
@ -54,13 +61,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#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_ */
|
||||
|
|
|
@ -14,7 +14,8 @@ std::string File::read() {
|
|||
fs::create_directory(home);
|
||||
}
|
||||
|
||||
fs::ifstream in(((options.file=="")?home / DEFAULT_FILE:options.file), std::ios::in | std::ios::binary);
|
||||
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);
|
||||
|
|
54
src/Filter.cpp
Normal file
54
src/Filter.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Filter.cpp
|
||||
*
|
||||
* Created on: 23.02.2016
|
||||
* Author: jedi
|
||||
*/
|
||||
|
||||
#include "Filter.h"
|
||||
|
||||
Filter::Filter() {
|
||||
}
|
||||
Filter::Filter(macAddr a) {
|
||||
switchMac = a;
|
||||
}
|
||||
Filter::Filter(Packet::OpCode o) {
|
||||
oc = o;
|
||||
}
|
||||
|
||||
Filter Filter::opcode(Packet::OpCode o) {
|
||||
oc = o;
|
||||
return *this;
|
||||
}
|
||||
Filter Filter::mac(macAddr a) {
|
||||
switchMac = a;
|
||||
return *this;
|
||||
}
|
||||
Filter Filter::tokenid(short i) {
|
||||
tokenId = i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Filter::pass(const Packet p) const {
|
||||
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())
|
||||
return false;
|
||||
if (this->tokenId != -1 && this->tokenId != p.getTokenId())
|
||||
return false;
|
||||
/* if (fragmentOffset != p.getF && fragmentOffset != -1)
|
||||
return false;*/
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Filter::operator<(const Filter f) const {
|
||||
if (this->oc != f.oc)
|
||||
return this->oc > f.oc;
|
||||
if (this->switchMac != f.switchMac)
|
||||
return this->switchMac > f.switchMac;
|
||||
if (this->tokenId != f.tokenId)
|
||||
return this->tokenId > f.tokenId;
|
||||
return false;
|
||||
}
|
31
src/Filter.h
Normal file
31
src/Filter.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Filter.h
|
||||
*
|
||||
* Created on: 23.02.2016
|
||||
* Author: jedi
|
||||
*/
|
||||
|
||||
#ifndef FILTER_H_
|
||||
#define FILTER_H_
|
||||
|
||||
#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;
|
||||
};
|
||||
|
||||
#endif /* FILTER_H_ */
|
|
@ -28,7 +28,8 @@
|
|||
macAddr Host::getMac() {
|
||||
int s;
|
||||
struct ifreq buffer;
|
||||
macAddr data { 0, 0, 0, 0, 0, 0 };
|
||||
macAddr data {
|
||||
0, 0, 0, 0, 0, 0 };
|
||||
if (options.interface != "") {
|
||||
s = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
memset(&buffer, 0x00, sizeof(buffer));
|
||||
|
@ -43,7 +44,8 @@ macAddr Host::getMac() {
|
|||
ipAddr Host::getIp() {
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int n;
|
||||
ipAddr data { 0, 0, 0, 0 };
|
||||
ipAddr data {
|
||||
0, 0, 0, 0 };
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
perror("getifaddrs");
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
|
||||
class Host {
|
||||
public:
|
||||
Host(){};
|
||||
Host() {
|
||||
}
|
||||
macAddr getMac();
|
||||
ipAddr getIp();
|
||||
std::string getIface();
|
||||
|
|
46
src/Interactive.cpp
Normal file
46
src/Interactive.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Interactive.cpp
|
||||
*
|
||||
* Created on: 27.01.2016
|
||||
* Author: jedi
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include<boost/program_options/parsers.hpp>
|
||||
#include<readline/readline.h>
|
||||
#include<readline/history.h>
|
||||
|
||||
#include "Program.h"
|
||||
#include "Interactive.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int Interactive::loop() {
|
||||
string cmd;
|
||||
vector<string> v;
|
||||
Program p = Program();
|
||||
p.init();
|
||||
while (1) {
|
||||
cmd = readline("smrtlink> ");
|
||||
if (!cmd.compare("quit") || !cmd.compare("q"))
|
||||
return 0;
|
||||
if (!cmd.empty()) {
|
||||
add_history(cmd.c_str());
|
||||
v = boost::program_options::split_unix(cmd);
|
||||
p.run(v);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Interactive::single(vector<string> v) {
|
||||
Program p = Program();
|
||||
p.init();
|
||||
p.run(v);
|
||||
return 0;
|
||||
}
|
||||
|
21
src/Interactive.h
Normal file
21
src/Interactive.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Interactive.h
|
||||
*
|
||||
* Created on: 27.01.2016
|
||||
* Author: jedi
|
||||
*/
|
||||
|
||||
#ifndef INTERACTIVE_H_
|
||||
#define INTERACTIVE_H_
|
||||
|
||||
class Interactive {
|
||||
public:
|
||||
Interactive() {
|
||||
}
|
||||
virtual ~Interactive() {
|
||||
}
|
||||
int loop();
|
||||
int single(std::vector<std::string> v);
|
||||
};
|
||||
|
||||
#endif /* INTERACTIVE_H_ */
|
|
@ -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_ */
|
|
@ -158,8 +158,8 @@ std::string Packet::opCodeToString() {
|
|||
return "SET";
|
||||
case CONFIRM:
|
||||
return "CONFIRM";
|
||||
case RETURN:
|
||||
return "RETURN";
|
||||
case REPLY:
|
||||
return "REPLY";
|
||||
default:
|
||||
return "NONE";
|
||||
}
|
||||
|
@ -168,24 +168,25 @@ std::string Packet::opCodeToString() {
|
|||
|
||||
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++;
|
||||
}
|
||||
|
@ -277,6 +279,14 @@ void Packet::pull(bytes &arr, int &index, int &ret) {
|
|||
ret |= arr[index++] & 0xFF;
|
||||
}
|
||||
|
||||
byte Packet::getOpCode() const {
|
||||
return opCode;
|
||||
}
|
||||
|
||||
void Packet::setOpCode(byte opCode) {
|
||||
this->opCode = opCode;
|
||||
}
|
||||
|
||||
void Packet::pull(bytes &arr, int &index, dataset &ret) {
|
||||
pull(arr, index, ret.type);
|
||||
pull(arr, index, ret.len);
|
||||
|
|
20
src/Packet.h
20
src/Packet.h
|
@ -15,12 +15,13 @@
|
|||
|
||||
static short sequenceId = 0;
|
||||
|
||||
class Packet
|
||||
{
|
||||
class Packet {
|
||||
public:
|
||||
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
|
||||
enum OpCode {
|
||||
DISCOVERY, GET, REPLY, SET, CONFIRM, NONE
|
||||
};
|
||||
Packet(OpCode);
|
||||
void encode(bytes&);
|
||||
static void encode(bytes&);
|
||||
bytes getBytes();
|
||||
void parse(bytes);
|
||||
void printHeader();
|
||||
|
@ -41,6 +42,8 @@ public:
|
|||
void setPayload(datasets payload);
|
||||
short getTokenId() const;
|
||||
void setTokenId(short tokenId = 0);
|
||||
byte getOpCode() const;
|
||||
void setOpCode(byte opCode);
|
||||
|
||||
private:
|
||||
bytes head = bytes(32);
|
||||
|
@ -48,9 +51,12 @@ private:
|
|||
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}};
|
||||
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;
|
||||
|
|
453
src/Program.cpp
453
src/Program.cpp
|
@ -6,6 +6,9 @@
|
|||
*/
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#include "Constant.h"
|
||||
#include "Program.h"
|
||||
|
@ -14,14 +17,120 @@
|
|||
#include "Socket.h"
|
||||
#include "Switch.h"
|
||||
#include "Packet.h"
|
||||
#include "Filter.h"
|
||||
#include "Types.h"
|
||||
#include "lookup.h"
|
||||
#include "table.h"
|
||||
|
||||
using namespace smrtlink;
|
||||
using namespace std;
|
||||
|
||||
int Program::run(vector<string> arg) {
|
||||
int optind = 0;
|
||||
std::vector<std::string> vect;
|
||||
std::map<std::string, std::string> ll;
|
||||
std::cmatch sm;
|
||||
std::string cmd = arg[optind++];
|
||||
|
||||
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))
|
||||
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;
|
||||
return 1;
|
||||
}
|
||||
ll.insert(
|
||||
std::pair<std::string, std::string>(sm[1], sm[2]));
|
||||
}
|
||||
else {
|
||||
cerr << "Invalid Syntax " << arg[optind] << endl;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
int printHeader(Packet p) {
|
||||
if (options.flags & FLAG_HEADER) {
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout << "Received Header:\n\t" << p.getHead() << "\n";
|
||||
} else {
|
||||
if (options.flags.HEADER) {
|
||||
if (options.flags.HEX) {
|
||||
cout << "Received Header:\n\t" << p.getHead() << "\n";
|
||||
}
|
||||
else {
|
||||
p.printHeader();
|
||||
printf("\n");
|
||||
}
|
||||
|
@ -30,56 +139,56 @@ int printHeader(Packet p) {
|
|||
}
|
||||
|
||||
int printPacket(Packet p) {
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||
} else {
|
||||
if (options.flags.HEX) {
|
||||
cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||
}
|
||||
else {
|
||||
for (dataset d : p.getPayload()) {
|
||||
auto lookup =
|
||||
(options.flags & FLAG_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]
|
||||
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";
|
||||
}
|
||||
} else { //unknown id
|
||||
else { //empty
|
||||
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";
|
||||
} else { //empty
|
||||
std::cout << "#>\t" << d.type << "\n";
|
||||
cout << "##\t" << d.type << ":\n\t";
|
||||
cout << hex << d.value << dec << "\n";
|
||||
}
|
||||
else { //empty
|
||||
cout << "#>\t" << d.type << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,144 +197,130 @@ int printPacket(Packet p) {
|
|||
}
|
||||
|
||||
int Program::list() {
|
||||
|
||||
std::cout << "List:\n";
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
p.setHostMac(host.getMac());
|
||||
p.setPayload( { });
|
||||
bytes b = p.getBytes();
|
||||
p.encode(b);
|
||||
|
||||
try {
|
||||
sock->setHostIp(host.getIp());
|
||||
sock->init(DST_PORT, SRC_PORT);
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
cout << "List:\n";
|
||||
discover([this](Packet a) {
|
||||
printHeader(a);
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
|
||||
} else {
|
||||
if (options.flags.HEX) {
|
||||
cout <<"Received Payload:\n"<<a.getBody()<<"\n";
|
||||
}
|
||||
else {
|
||||
datasets d =a.getPayload();
|
||||
Switch sw = Switch();
|
||||
sw.parse(d);
|
||||
|
||||
int b = a.getSwitchMac().hash();
|
||||
if (devices.empty()||devices.find(b) == devices.end())
|
||||
devices.insert(pair<int,Switch>(b,Switch()));
|
||||
devices[a.getSwitchMac().hash()].parse(d);
|
||||
devices[a.getSwitchMac().hash()].print();
|
||||
|
||||
File f;
|
||||
f.write(sw.toString());
|
||||
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||
f.write(devices[a.getSwitchMac().hash()].toString());
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
sock->send(b);
|
||||
io_service->run();
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
return 0;
|
||||
});
|
||||
} catch (exception& e) {
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::sniff() {
|
||||
printf("Listening:\n");
|
||||
try {
|
||||
boost::asio::io_service io_service;
|
||||
Socket s(io_service);
|
||||
s.setHostIp(host.getIp());
|
||||
s.init(DST_PORT, SRC_PORT);
|
||||
s.callback = [](Packet p) {
|
||||
std::cout << p.opCodeToString() << "\n";
|
||||
sock->listen([](Packet p) {
|
||||
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;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Program::encode(std::string s) {
|
||||
bytes d(s);
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
int Program::encode(string s) {
|
||||
bytes d;
|
||||
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() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::getProperty() {
|
||||
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;
|
||||
int Program::setProperty(map<string, string> prop) {
|
||||
try {
|
||||
sock->setHostIp(host.getIp());
|
||||
sock->init(DST_PORT, SRC_PORT);
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
auto s = sock;
|
||||
datasets data = { };
|
||||
for (auto p : prop) {
|
||||
string s = p.second;
|
||||
dataset d;
|
||||
if (lookup.exists(p.first)) {
|
||||
d.type = lookup.type(p.first);
|
||||
if (lookup[p.first].format == table::STRING) {
|
||||
d.len = s.size() + 1;
|
||||
d.value = s;
|
||||
data.push_back(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "List:\n";
|
||||
discover([this,data](Packet a) {
|
||||
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";
|
||||
sw.print();
|
||||
|
||||
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 t = { {PING, 0, {}}};
|
||||
get(a, t, [this,data](Packet a) {
|
||||
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);
|
||||
set(a,data,
|
||||
|
||||
sock->callback =
|
||||
[this](Packet a) {
|
||||
std::cout << a.opCodeToString() << "\n";
|
||||
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";
|
||||
});
|
||||
} catch (exception& e) {
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::getProperty(vector<string> prop) {
|
||||
try {
|
||||
datasets data = { };
|
||||
for (string s : prop) {
|
||||
dataset d;
|
||||
d.type = 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;
|
||||
});
|
||||
return 0;
|
||||
});
|
||||
|
||||
} catch (exception& e) {
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::save() {
|
||||
|
@ -233,40 +328,120 @@ int Program::save() {
|
|||
sw.settings.hostname = "testname.lan";
|
||||
File f;
|
||||
f.write(sw.toString());
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::restore() {
|
||||
File f;
|
||||
Switch sw;
|
||||
sw.parse(f.read());
|
||||
std::cout << "Devices:\n\t" << sw.settings.hostname << " ("
|
||||
<< sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: "
|
||||
<< sw.settings.ip_addr << "\n";
|
||||
return 1;
|
||||
sw.print();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::flash() {
|
||||
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Program::reboot() {
|
||||
|
||||
try {
|
||||
discover(
|
||||
[this](Packet a) {
|
||||
datasets d =a.getPayload();
|
||||
int b = a.getSwitchMac().hash();
|
||||
if (devices.empty()||devices.find(b) == devices.end())
|
||||
devices.insert(pair<int,Switch>(b,Switch()));
|
||||
devices[a.getSwitchMac().hash()].parse(d);
|
||||
datasets t = { {PING, 0, {}}};
|
||||
get(a, t, [this](Packet a) {
|
||||
datasets d =a.getPayload();
|
||||
cout <<devices[a.getSwitchMac().hash()].settings.hostname<<"\t";
|
||||
devices[a.getSwitchMac().hash()].parse(d);
|
||||
datasets t = { {REBOOT, 1, {options.flags.PERMANENT?(byte)1:(byte)0}}};
|
||||
set(a,t,[this](Packet a) {
|
||||
cout<< "rebooting now.\n";
|
||||
return 0;
|
||||
});
|
||||
return 0;
|
||||
});
|
||||
return 0;
|
||||
});
|
||||
} catch (exception& e) {
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::reset() {
|
||||
|
||||
try {
|
||||
discover([this](Packet a) {
|
||||
datasets d =a.getPayload();
|
||||
Switch sw = Switch();
|
||||
sw.parse(d);
|
||||
cout <<sw.settings.hostname<<"\t";
|
||||
datasets t = { {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;
|
||||
});
|
||||
} catch (exception& e) {
|
||||
cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::ping(std::function<int(Packet)>) {
|
||||
int Program::discover(Listener c) {
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
p.setHostMac(host.getMac());
|
||||
p.setPayload( { });
|
||||
sock->listen(c, Filter(Packet::REPLY));
|
||||
sock->send(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::get(Packet l, datasets t, Listener c) {
|
||||
Packet p = Packet(Packet::GET);
|
||||
p.setSwitchMac(l.getSwitchMac());
|
||||
p.setHostMac(host.getMac());
|
||||
p.setPayload(t);
|
||||
sock->listen(c, Filter(Packet::REPLY).mac(l.getSwitchMac()));
|
||||
sock->send(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::set(Packet l, datasets t, Listener 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;
|
||||
datasets ld = {
|
||||
{ LOGIN_USER, (short) (n.size()), n }, {
|
||||
LOGIN_PASSWORD, (short) (w.size()), w } };
|
||||
p.setPayload(ld + t);
|
||||
sock->listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac()));
|
||||
sock->send(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Program::init() {
|
||||
io_service = std::make_shared<boost::asio::io_service>();
|
||||
sock = std::make_shared < Socket > (*io_service);
|
||||
if (options.interface.compare("") == 0)
|
||||
options.interface = host.getIface();
|
||||
|
||||
sock->setHostIp(host.getIp());
|
||||
sock->init(DST_PORT, SRC_PORT);
|
||||
}
|
||||
|
|
|
@ -9,9 +9,11 @@
|
|||
#define PROGRAM_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "Types.h"
|
||||
#include "Host.h"
|
||||
#include "Switch.h"
|
||||
#include "Socket.h"
|
||||
|
||||
class Program {
|
||||
|
@ -19,18 +21,24 @@ private:
|
|||
std::shared_ptr<boost::asio::io_service> io_service;
|
||||
std::shared_ptr<Socket> sock;
|
||||
Host host = Host();
|
||||
int ping(std::function<int(Packet)>);
|
||||
std::map<int, Switch> devices;
|
||||
int get(Packet, datasets, std::function<int(Packet)>);
|
||||
int set(Packet, datasets, std::function<int(Packet)>);
|
||||
int discover(std::function<int(Packet)>);
|
||||
public:
|
||||
Program() {
|
||||
io_service = std::make_shared<boost::asio::io_service>();
|
||||
sock = std::make_shared<Socket>(*io_service);
|
||||
}
|
||||
void init();
|
||||
int run(std::vector<std::string>);
|
||||
std::function<int()> callback = []() {
|
||||
return 0;
|
||||
};
|
||||
|
||||
int list();
|
||||
int sniff();
|
||||
int encode(std::string);
|
||||
int setProperty();
|
||||
int getProperty();
|
||||
int getProperty(std::vector<std::string>);
|
||||
int setProperty(std::map<std::string, std::string>);
|
||||
int save();
|
||||
int restore();
|
||||
int flash();
|
||||
|
|
|
@ -5,29 +5,33 @@
|
|||
* Author: jdi
|
||||
*/
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <unistd.h>
|
||||
#include "Socket.h"
|
||||
#include "Packet.h"
|
||||
#include "Constant.h"
|
||||
#include "Filter.h"
|
||||
#include "Host.h"
|
||||
#include "Types.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Socket::Socket(boost::asio::io_service& io_service) :
|
||||
send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
|
||||
}
|
||||
//, resolver( io_service)
|
||||
void Socket::init(short dst_port, short src_port) {
|
||||
if (initialized)
|
||||
return;
|
||||
|
||||
if (options.flags & FLAG_REVERSE) {
|
||||
Socket::~Socket() {
|
||||
}
|
||||
|
||||
void Socket::init(short dst_port, short src_port) {
|
||||
if (options.flags.REVERSE) {
|
||||
short p = dst_port;
|
||||
dst_port = src_port;
|
||||
src_port = p;
|
||||
}
|
||||
|
||||
if (options.flags & FLAG_DEBUG)
|
||||
if (options.debug_level >= 1)
|
||||
std::cout << "Local IP:\t" << local_ip << "\n";
|
||||
|
||||
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
|
@ -47,37 +51,40 @@ void Socket::init(short dst_port, short src_port) {
|
|||
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
|
||||
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
|
||||
receive_socket_.bind(wildcard_endpoint_);
|
||||
|
||||
if (options.timeout != 0) {
|
||||
timer.expires_from_now(
|
||||
boost::posix_time::milliseconds(options.timeout));
|
||||
timer.async_wait([this](const boost::system::error_code& error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
receive_socket_.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
void Socket::setHostIp(ipAddr ip) {
|
||||
local_ip = ip;
|
||||
}
|
||||
|
||||
void Socket::send(bytes data) {
|
||||
void Socket::clear() {
|
||||
callback.clear();
|
||||
}
|
||||
|
||||
void Socket::listen(Listener l, Filter f) {
|
||||
if (callback.find(f) == callback.end()) {
|
||||
callback.insert(ListenerPair(f, l));
|
||||
}
|
||||
else {
|
||||
callback[f] = l;
|
||||
}
|
||||
receive();
|
||||
}
|
||||
|
||||
void Socket::send(Packet p) {
|
||||
bytes data = p.getBytes();
|
||||
p.encode(data);
|
||||
unsigned char * a = &data[0];
|
||||
send_socket_.async_send_to(boost::asio::buffer(a, data.size()),
|
||||
broadcast_endpoint_,
|
||||
[this](boost::system::error_code ec, std::size_t bytes_sent)
|
||||
{
|
||||
listen();
|
||||
receive();
|
||||
settimeout();
|
||||
});
|
||||
}
|
||||
|
||||
void Socket::listen() {
|
||||
void Socket::receive() {
|
||||
data.resize(MAX_LENGTH);
|
||||
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
|
||||
remote_endpoint_,
|
||||
|
@ -86,17 +93,43 @@ void Socket::listen() {
|
|||
if (ec || bytes_recvd == 0) {
|
||||
//listen();
|
||||
// TODO distinguish error codes
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
data.resize(bytes_recvd);
|
||||
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
|
||||
for(auto r : callback) {
|
||||
if(r.first.pass(p)) {
|
||||
r.second(p);
|
||||
// std::cout<<"pass"<<std::endl;
|
||||
break;
|
||||
}
|
||||
listen();
|
||||
else {
|
||||
// std::cout<<"no pass"<<std::endl;
|
||||
}
|
||||
}
|
||||
receive();
|
||||
settimeout();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Socket::settimeout() {
|
||||
if (options.timeout != 0) {
|
||||
timer.expires_from_now(
|
||||
boost::posix_time::milliseconds(options.timeout));
|
||||
timer.async_wait(
|
||||
[this](const boost::system::error_code& error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
receive_socket_.close();
|
||||
receive_socket_.open(boost::asio::ip::udp::v4());
|
||||
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
|
||||
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
|
||||
receive_socket_.bind(wildcard_endpoint_);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
20
src/Socket.h
20
src/Socket.h
|
@ -8,8 +8,10 @@
|
|||
#ifndef SOCKET_H_
|
||||
#define SOCKET_H_
|
||||
|
||||
#include <map>
|
||||
#include <boost/asio.hpp>
|
||||
#include "Packet.h"
|
||||
#include "Filter.h"
|
||||
#include "Types.h"
|
||||
|
||||
#define SRC_PORT 29809
|
||||
|
@ -17,20 +19,22 @@
|
|||
|
||||
#define MAX_LENGTH 1024
|
||||
|
||||
typedef std::function<int(Packet)> Listener;
|
||||
typedef std::pair<Filter, Listener> ListenerPair;
|
||||
|
||||
class Socket {
|
||||
public:
|
||||
Socket(boost::asio::io_service&);
|
||||
virtual ~Socket() {
|
||||
}
|
||||
virtual ~Socket();
|
||||
void init(short, short);
|
||||
void send(bytes);
|
||||
void listen();
|
||||
void clear();
|
||||
void send(Packet);
|
||||
void setHostIp(ipAddr);
|
||||
std::function<int(Packet)> callback = [](Packet a) {
|
||||
return 0;
|
||||
};
|
||||
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_;
|
||||
|
@ -40,7 +44,7 @@ private:
|
|||
boost::asio::deadline_timer timer;
|
||||
bytes data = bytes(MAX_LENGTH);
|
||||
ipAddr local_ip;
|
||||
int initialized = 0;
|
||||
std::map<Filter, Listener> callback = { };
|
||||
|
||||
};
|
||||
|
||||
|
|
31
src/Switch-Cmd.cpp
Normal file
31
src/Switch-Cmd.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Switch.cpp
|
||||
*
|
||||
* Created on: 29.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include "Types.h"
|
||||
#include "Switch.h"
|
||||
#include "Constant.h"
|
||||
#include "table.h"
|
||||
|
||||
using namespace smrtlink;
|
||||
using namespace std;
|
||||
|
||||
int Switch::set(pair<string, string> str) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Switch::get(std::string str) {
|
||||
std::string ret;
|
||||
switch (caseArg(str.c_str())) {
|
||||
case caseArg("ip"):
|
||||
ret = "0.0.0.0";
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ int Switch::parse(std::string str) {
|
|||
|
||||
if (json.Parse(str.c_str()).HasParseError())
|
||||
return 1;
|
||||
if (options.flags & FLAG_DEBUG)
|
||||
if (options.debug_level >= 1)
|
||||
std::cout << "\nParsing to document succeeded.\n";
|
||||
|
||||
if (json.IsObject()) {
|
||||
|
|
|
@ -19,12 +19,18 @@ int Switch::parse(datasets arr) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int Switch::print() {
|
||||
std::cout << "\t" << settings.hostname << " (" << device.type << ")\tMAC: "
|
||||
<< device.mac << "\tIP: " << settings.ip_addr << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Switch::parse(dataset d) {
|
||||
switch (d.type) {
|
||||
case RCV_TYPE:
|
||||
case TYPE:
|
||||
device.type = d.value;
|
||||
break;
|
||||
case RCV_MAC:
|
||||
case MAC:
|
||||
device.mac = d.value;
|
||||
break;
|
||||
case FIRMWARE_VERSION:
|
||||
|
@ -63,4 +69,3 @@ int Switch::parse(dataset d) {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
11
src/Switch.h
11
src/Switch.h
|
@ -65,20 +65,27 @@ public:
|
|||
int parse(datasets);
|
||||
int parse(dataset);
|
||||
int parse(std::string);
|
||||
int set(std::pair<std::string, std::string>);
|
||||
|
||||
std::string get(std::string);
|
||||
std::string toString();
|
||||
|
||||
int print();
|
||||
|
||||
struct {
|
||||
std::string type;
|
||||
std::string hardware_version;
|
||||
std::string firmware_version;
|
||||
macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
|
||||
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_addr {
|
||||
0, 0, 0, 0, };
|
||||
ipAddr ip_mask;
|
||||
ipAddr gateway;
|
||||
bool dhcp;
|
||||
|
|
46
src/Types.h
46
src/Types.h
|
@ -50,6 +50,28 @@ public:
|
|||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
int hash() {
|
||||
int ret=0;
|
||||
for (unsigned i = 0; i < 6; i++) {
|
||||
ret = (ret*33) ^ (*this)[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool operator==(const macAddr &A) {
|
||||
for (unsigned i = 0; i < 6; i++) {
|
||||
if(A[i]!=(*this)[i])return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator!=(const macAddr &A) {
|
||||
for (unsigned i = 0; i < 6; i++) {
|
||||
if(A[i]!=(*this)[i])return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -63,7 +85,6 @@ public:
|
|||
};
|
||||
*/
|
||||
|
||||
|
||||
class ipAddr: public std::array<byte, 4> {
|
||||
public:
|
||||
|
||||
|
@ -96,6 +117,14 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
namespace smrtlink {
|
||||
|
||||
constexpr unsigned int caseArg(const char* str, int h = 0) {
|
||||
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) {
|
||||
std::vector<T> AB;
|
||||
|
@ -113,13 +142,24 @@ std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
|
|||
}
|
||||
|
||||
struct Options {
|
||||
unsigned flags = 0x00;
|
||||
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;
|
||||
long timeout = 180U;
|
||||
int verbosity = 0;
|
||||
long timeout = 250U;
|
||||
};
|
||||
|
||||
#endif /* TYPES_H_ */
|
||||
|
|
13
src/bytes.h
13
src/bytes.h
|
@ -24,14 +24,16 @@ public:
|
|||
using vector::operator[];
|
||||
bytes() {
|
||||
}
|
||||
bytes(int n) : vector(n){
|
||||
bytes(int n) :
|
||||
vector(n) {
|
||||
}
|
||||
|
||||
bytes(std::string d) : vector(d.begin(), d.end()){
|
||||
bytes(std::string d) :
|
||||
vector(d.begin(), d.end()) {
|
||||
this->push_back('\0');
|
||||
}
|
||||
|
||||
bytes(std::initializer_list<uint8_t> s)
|
||||
{
|
||||
bytes(std::initializer_list<uint8_t> s) {
|
||||
for (uint8_t b : s) {
|
||||
this->push_back(b);
|
||||
}
|
||||
|
@ -70,7 +72,8 @@ public:
|
|||
<< (unsigned) arr[0];
|
||||
}
|
||||
for (unsigned i = 1; i < arr.size(); i++) {
|
||||
out << ":" << std::setw(2) << std::setfill('0') << (unsigned) arr[i];
|
||||
out << ":" << std::setw(2) << std::setfill('0')
|
||||
<< (unsigned) arr[i];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
|
@ -19,14 +19,22 @@ struct dataset {
|
|||
|
||||
class datasets: public std::vector<dataset> {
|
||||
public:
|
||||
datasets(){};
|
||||
datasets(std::initializer_list<dataset> s)
|
||||
{
|
||||
datasets() {
|
||||
}
|
||||
datasets(std::initializer_list<dataset> 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;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* DATASETS_H_ */
|
||||
|
|
28
src/lookup.h
28
src/lookup.h
|
@ -1,30 +1,12 @@
|
|||
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"
|
||||
#define LOOKUP_SET(id, name, num, type, write) id = num,
|
||||
#include "lookup/map.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"
|
||||
static table lookup {
|
||||
#define LOOKUP_SET(id, name, num, type, write) { id, table::type, table::write, #name, #id},
|
||||
#include "lookup/map.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
||||
|
|
|
@ -1,25 +1,27 @@
|
|||
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(PORTS, ports, 10, DEC) //+byte, maybe number of ports
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
|
|
49
src/lookup/map.lst
Normal file
49
src/lookup/map.lst
Normal file
|
@ -0,0 +1,49 @@
|
|||
LOOKUP_SET(TYPE, type, 1, STRING, RO) //+string
|
||||
LOOKUP_SET(HOSTNAME, host, 2, STRING, RW) //+string //SYSTEM_INFO,SYSTEM_INFO, 2, HEX) //page sysinfo
|
||||
LOOKUP_SET(MAC, mac, 3, HEX, RO) //+byte[6]
|
||||
LOOKUP_SET(IP_ADDR, ip, 4, DEC, RW) //+byte[4]
|
||||
LOOKUP_SET(IP_MASK, mask, 5, DEC, RW) //+byte[4]
|
||||
LOOKUP_SET(GATEWAY, gateway, 6, DEC, RW) //+byte[4]
|
||||
LOOKUP_SET(FIRMWARE_VERSION,firmware, 7, STRING, RO)
|
||||
LOOKUP_SET(HARDWARE_VERSION,hardware, 8, STRING, RO)
|
||||
LOOKUP_SET(DHCP_ENABLED, dhcp, 9, BOOL, RW) //+bool byte //IP_CONFIG,IP_CONFIG, 9, HEX) //page sysinfo
|
||||
|
||||
LOOKUP_SET(PORTS, ports, 10, DEC, RO) //+byte, maybe number of ports
|
||||
|
||||
LOOKUP_SET(LOGIN_USER, luser, 512, STRING, NONE) //string
|
||||
LOOKUP_SET(NEW_USER, user, 513, STRING, WO) //string
|
||||
LOOKUP_SET(LOGIN_PASSWORD, lpw, 514, STRING, NONE) //string
|
||||
LOOKUP_SET(NEW_PASSWORD, password, 515, STRING, WO) //string
|
||||
LOOKUP_SET(REBOOT, reboot, 773, BOOL, NONE) //bool byte = save config
|
||||
|
||||
LOOKUP_SET(RESET, reset, 1280, ACTION, NONE) //
|
||||
LOOKUP_SET(FLASH, flash, 1536, ACTION, NONE) //update firmware
|
||||
|
||||
LOOKUP_SET(PORT_VLAN_FOOOOO,vlan21, 2101, HEX, RO)
|
||||
LOOKUP_SET(VLAN_FOOOOOO, vlan22, 2200, HEX, RO) //vlan
|
||||
|
||||
LOOKUP_SET(SAVE, save, 2304, ACTION, NONE) //save
|
||||
LOOKUP_SET(PING, ping, 2305, ACTION, NONE) //sent before SET ???
|
||||
|
||||
LOOKUP_SET(IGMP_SNOOPING, igmp, 4352, HEX, RW) //???
|
||||
LOOKUP_SET(PORTS_SETTINGS, psetings, 4096, HEX, RW) //+per port
|
||||
LOOKUP_SET(PORT_TRUNK, trunk, 4608, HEX, RW) //byte[5] last byte bitmask??
|
||||
|
||||
LOOKUP_SET(MTU_VLAN, 8192, 8192, HEX, UNKNOWN) //byte[2] first byte bool,second byte port id
|
||||
LOOKUP_SET(PORT_VLAN_ENABLED,8448, 8448, BOOL, UNKNOWN) //open page
|
||||
LOOKUP_SET(PORT_VLAN, 8449, 8449, HEX, UNKNOWN)
|
||||
LOOKUP_SET(PORT_VLAN_MAX, 8450, 8450, DEC, UNKNOWN)
|
||||
LOOKUP_SET(VLAN_ENABLED, 8704, 8704, BOOL, UNKNOWN) //+bool byte
|
||||
LOOKUP_SET(VLAN, 8705, 8705, HEX, UNKNOWN) //+one set per vlan
|
||||
LOOKUP_SET(VLAN_PVID, 8706, 8706, HEX, UNKNOWN) //+per port
|
||||
LOOKUP_SET(VLAN_FOOOO, 8707, 8707, DEC, UNKNOWN) //????
|
||||
|
||||
LOOKUP_SET(QOS_BASIC_ENABLED,12288, 12288, BOOL, UNKNOWN) //+bool = QoS Mod
|
||||
LOOKUP_SET(QOS_BASIC, 12289, 12289, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(BW_CONTROL_INGRESS,12544, 12544, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(BW_CONTROL_EGRESS,12545, 12545, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(STORM_CONTROL, 12800, 12800, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(PORT_MIRROR, 16640, 16640, HEX, UNKNOWN) //byte[10] second byte port id???
|
||||
LOOKUP_SET(PORT_STATISTICS, 16384, 16384, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(CABLE_TEST, 16896, 16896, HEX, UNKNOWN) //+per port ???
|
||||
LOOKUP_SET(LOOP_PREVENTION, 17152, 17152, BOOL, UNKNOWN) //+bool byte
|
|
@ -1,13 +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(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, HEX)
|
||||
LOOKUP_SET(PORT_VLAN_FOOOOO, 2101,2101, HEX)
|
||||
|
||||
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo
|
||||
LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo
|
||||
LOOKUP_SET(SYSTEM_INFO,SYSTEM_INFO, 2, HEX) //page sysinfo
|
||||
LOOKUP_SET(IP_CONFIG,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(LOGIN_USER,LOGIN_USER, 512, STRING) //string
|
||||
LOOKUP_SET(NEW_USER,NEW_USER, 513, STRING) //string
|
||||
LOOKUP_SET(LOGIN_PASSWORD,LOGIN_PASSWORD, 514, STRING) //string
|
||||
LOOKUP_SET(NEW_PASSWORD,NEW_PASSWORD, 515, STRING) //string
|
||||
LOOKUP_SET(REBOOT,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 ???
|
||||
LOOKUP_SET(RESET,RESET, 1280, ACTION) //
|
||||
LOOKUP_SET(FLASH,FLASH, 1536, ACTION) //update firmware
|
||||
LOOKUP_SET(VLAN_FOOOOOO, 2200,2200, HEX) //vlan
|
||||
LOOKUP_SET(SAVE,SAVE, 2304, ACTION) //save
|
||||
LOOKUP_SET(PING,PING, 2305, ACTION) //sent before SET ???
|
||||
|
||||
|
||||
|
|
174
src/smrtlink.cpp
174
src/smrtlink.cpp
|
@ -1,55 +1,61 @@
|
|||
//============================================================================
|
||||
// Name : smrtlink.cpp
|
||||
// Author : jdi
|
||||
// Version :
|
||||
// Version : 1.2
|
||||
// Copyright : GPL v2
|
||||
// Description : SmrtLink in C++, Ansi-style
|
||||
//============================================================================
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#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 "Types.h"
|
||||
#include "Switch.h"
|
||||
#include "lookup.h"
|
||||
|
||||
#define no_argument 0
|
||||
#define required_argument 1
|
||||
#define optional_argument 2
|
||||
|
||||
using namespace std;
|
||||
|
||||
Options options;
|
||||
|
||||
constexpr unsigned int caseArg(const char* str, int h = 0) {
|
||||
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int opt, index;
|
||||
int index, opt;
|
||||
|
||||
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,
|
||||
'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 }, };
|
||||
|
||||
Program p = Program();
|
||||
|
||||
while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts,
|
||||
while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts,
|
||||
&index)) != -1) {
|
||||
switch (opt) {
|
||||
|
||||
|
@ -60,35 +66,47 @@ int main(int argc, char *argv[]) {
|
|||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
case 'V':
|
||||
fprintf(stderr, VERSION);
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
options.flags |= FLAG_REVERSE;
|
||||
options.flags.REVERSE = true;
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
options.flags |= FLAG_HEADER;
|
||||
options.flags.HEADER = true;
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
options.flags |= FLAG_HEX;
|
||||
options.flags.HEX = true;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
options.flags |= FLAG_PERMANENT;
|
||||
options.flags.PERMANENT = true;
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
options.flags |= FLAG_WAIT;
|
||||
options.flags.WAIT = true;
|
||||
break;
|
||||
|
||||
case 'X':
|
||||
options.flags.INTERACTIVE = true;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
if (optarg != NULL)
|
||||
options.verbosity = atoi(optarg);
|
||||
else
|
||||
options.verbosity++;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
options.flags |= FLAG_DEBUG;
|
||||
if (optarg != NULL)
|
||||
options.debug_level = atoi(optarg);
|
||||
else
|
||||
options.debug_level++;
|
||||
break;
|
||||
|
||||
case 't':
|
||||
|
@ -99,11 +117,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;
|
||||
|
||||
|
@ -124,97 +142,27 @@ 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);
|
||||
}
|
||||
|
||||
p.init();
|
||||
|
||||
Interactive p = Interactive();
|
||||
if (options.flags.INTERACTIVE) {
|
||||
if (optind < argc) {
|
||||
std::string cmd = std::string(argv[optind++]);
|
||||
|
||||
switch (caseArg(cmd.c_str())) {
|
||||
case caseArg("set"):
|
||||
if (p.setProperty())
|
||||
cerr << "Command is ignored in interactive mode\n";
|
||||
}
|
||||
if (!p.loop())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case caseArg("reboot"):
|
||||
if (p.reboot())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case caseArg("reset"):
|
||||
if (p.reset())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case caseArg("save"):
|
||||
if (p.save())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case caseArg("restore"):
|
||||
if (p.restore())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case caseArg("flash"):
|
||||
if (p.flash())
|
||||
exit(EXIT_SUCCESS);
|
||||
fprintf(stderr, "Not yet implemented.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
|
||||
case caseArg("get"):
|
||||
if (p.getProperty())
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case caseArg("list"):
|
||||
if (p.list())
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case caseArg("sniff"):
|
||||
if (p.sniff())
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case caseArg("encode"):
|
||||
if (optind < argc) {
|
||||
std::string s(argv[optind]);
|
||||
optind++;
|
||||
if (p.encode(s))
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
fprintf(stderr, "Argument expected after encode\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
|
||||
case caseArg("help"):
|
||||
fprintf(stderr, VERSION);
|
||||
fprintf(stderr, USAGE, argv[0]);
|
||||
fprintf(stderr, HELP);
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Unknown command: %s\n", cmd.c_str());
|
||||
while (optind < argc) {
|
||||
printf("->%s\n", argv[optind]);
|
||||
optind++;
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else if (optind < argc) {
|
||||
vector<string> v;
|
||||
while (optind < argc)
|
||||
v.push_back(argv[optind++]);
|
||||
p.single(v);
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
|
@ -10,16 +10,32 @@
|
|||
|
||||
table::table(std::initializer_list<set> l) {
|
||||
int i = 0;
|
||||
this->data.resize(l.size());
|
||||
for (set s : l) {
|
||||
this->left[s.type] = s;
|
||||
this->data[i] = s;
|
||||
this->left[s.type] = &this->data[i];
|
||||
this->right[s.name] = &this->data[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
table::set table::operator[](short n){
|
||||
return this->left[n];
|
||||
const table::set table::operator[](std::string s) {
|
||||
return *this->right[s];
|
||||
}
|
||||
const table::set table::operator[](short n) {
|
||||
return *this->left[n];
|
||||
}
|
||||
bool table::exists(std::string s) {
|
||||
return !(right.find(s) == right.end());
|
||||
}
|
||||
bool table::exists(short n) {
|
||||
return !(left.find(n) == left.end());
|
||||
}
|
||||
std::string table::name(short n){
|
||||
return this->left[n].name;
|
||||
short table::type(std::string s) {
|
||||
return this->right[s]->type;
|
||||
}
|
||||
std::string table::id(short n) {
|
||||
return this->left[n]->id;
|
||||
}
|
||||
std::string table::name(short n) {
|
||||
return this->left[n]->name;
|
||||
}
|
||||
|
|
19
src/table.h
19
src/table.h
|
@ -13,18 +13,31 @@
|
|||
|
||||
class table {
|
||||
public:
|
||||
enum F {STRING,HEX,DEC,ACTION,BOOL,EMPTY};
|
||||
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<set> l);
|
||||
set operator[](short);
|
||||
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::map<short, set> left;
|
||||
std::vector<set> data;
|
||||
std::map<short, set*> left;
|
||||
std::map<std::string, set*> right;
|
||||
};
|
||||
|
||||
#endif /* LOOKUPTABLE_H_ */
|
||||
|
|
18
test.sh
Executable file
18
test.sh
Executable file
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
clear
|
||||
echo SmrtLink Test
|
||||
|
||||
echo bin/smrtlink encode 00:11:22:33:44:55:66:77:88:99
|
||||
bin/smrtlink encode 00:11:22:33:44:55:66:77:88:9
|
||||
|
||||
echo bin/smrtlink -i eth0 list
|
||||
bin/smrtlink -i eth0 list
|
||||
|
||||
echo bin/smrtlink -i eth6 list
|
||||
bin/smrtlink -i eth6 list
|
||||
|
||||
echo bin/smrtlink list
|
||||
bin/smrtlink list
|
||||
|
||||
echo bin/smrtlink -i wlan0 list
|
||||
bin/smrtlink -i wlan0 list
|
Loading…
Reference in a new issue