reformat
This commit is contained in:
parent
f15abc71be
commit
1f3730ac87
26 changed files with 880 additions and 844 deletions
|
|
@ -14,7 +14,8 @@ std::string File::read() {
|
||||||
fs::create_directory(home);
|
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) {
|
if (in) {
|
||||||
std::string contents;
|
std::string contents;
|
||||||
in.seekg(0, std::ios::end);
|
in.seekg(0, std::ios::end);
|
||||||
|
|
@ -31,9 +32,9 @@ int File::write(std::string content) {
|
||||||
if (!fs::exists(home)) {
|
if (!fs::exists(home)) {
|
||||||
fs::create_directory(home);
|
fs::create_directory(home);
|
||||||
}
|
}
|
||||||
fs::path p = (options.file=="")?home / DEFAULT_FILE:options.file;
|
fs::path p = (options.file == "") ? home / DEFAULT_FILE : options.file;
|
||||||
fs::ofstream file(p);
|
fs::ofstream file(p);
|
||||||
file << content<<"\n";
|
file << content << "\n";
|
||||||
file.close();
|
file.close();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,13 @@ namespace fs = boost::filesystem;
|
||||||
#define DEFAULT_FILE "config.json"
|
#define DEFAULT_FILE "config.json"
|
||||||
|
|
||||||
class File {
|
class File {
|
||||||
public:
|
public:
|
||||||
File() {
|
File() {
|
||||||
home = fs::path(getenv("HOME")) / ".smrtlink";
|
home = fs::path(getenv("HOME")) / ".smrtlink";
|
||||||
}
|
}
|
||||||
std::string read();
|
std::string read();
|
||||||
int write(std::string);
|
int write(std::string);
|
||||||
private:
|
private:
|
||||||
fs::path home;
|
fs::path home;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,8 @@ Filter Filter::tokenid(short i) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Filter::pass(const Packet p) const {
|
bool Filter::pass(const Packet p) const {
|
||||||
macAddr none { 0, 0, 0, 0, 0, 0 };
|
macAddr none {
|
||||||
|
0, 0, 0, 0, 0, 0 };
|
||||||
if (this->oc != Packet::NONE && this->oc != p.getOpCode())
|
if (this->oc != Packet::NONE && this->oc != p.getOpCode())
|
||||||
return false;
|
return false;
|
||||||
if (this->switchMac != none && this->switchMac != p.getSwitchMac())
|
if (this->switchMac != none && this->switchMac != p.getSwitchMac())
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
#include "Packet.h"
|
#include "Packet.h"
|
||||||
|
|
||||||
class Filter {
|
class Filter {
|
||||||
public:
|
public:
|
||||||
Filter();
|
Filter();
|
||||||
Filter(Packet::OpCode);
|
Filter(Packet::OpCode);
|
||||||
Filter(macAddr);
|
Filter(macAddr);
|
||||||
|
|
@ -20,9 +20,10 @@ public:
|
||||||
Filter tokenid(short);
|
Filter tokenid(short);
|
||||||
bool pass(const Packet) const;
|
bool pass(const Packet) const;
|
||||||
bool operator<(const Filter) const;
|
bool operator<(const Filter) const;
|
||||||
private:
|
private:
|
||||||
Packet::OpCode oc = Packet::NONE;
|
Packet::OpCode oc = Packet::NONE;
|
||||||
macAddr switchMac = { 0, 0, 0, 0, 0, 0 };
|
macAddr switchMac = {
|
||||||
|
0, 0, 0, 0, 0, 0 };
|
||||||
short tokenId = -1;
|
short tokenId = -1;
|
||||||
short fragmentOffset = -1;
|
short fragmentOffset = -1;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,8 @@
|
||||||
macAddr Host::getMac() {
|
macAddr Host::getMac() {
|
||||||
int s;
|
int s;
|
||||||
struct ifreq buffer;
|
struct ifreq buffer;
|
||||||
macAddr data { 0, 0, 0, 0, 0, 0 };
|
macAddr data {
|
||||||
|
0, 0, 0, 0, 0, 0 };
|
||||||
if (options.interface != "") {
|
if (options.interface != "") {
|
||||||
s = socket(PF_INET, SOCK_DGRAM, 0);
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
memset(&buffer, 0x00, sizeof(buffer));
|
memset(&buffer, 0x00, sizeof(buffer));
|
||||||
|
|
@ -43,7 +44,8 @@ macAddr Host::getMac() {
|
||||||
ipAddr Host::getIp() {
|
ipAddr Host::getIp() {
|
||||||
struct ifaddrs *ifaddr, *ifa;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
int n;
|
int n;
|
||||||
ipAddr data { 0, 0, 0, 0 };
|
ipAddr data {
|
||||||
|
0, 0, 0, 0 };
|
||||||
if (getifaddrs(&ifaddr) == -1) {
|
if (getifaddrs(&ifaddr) == -1) {
|
||||||
perror("getifaddrs");
|
perror("getifaddrs");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,9 @@
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
class Host {
|
class Host {
|
||||||
public:
|
public:
|
||||||
Host(){};
|
Host() {
|
||||||
|
}
|
||||||
macAddr getMac();
|
macAddr getMac();
|
||||||
ipAddr getIp();
|
ipAddr getIp();
|
||||||
std::string getIface();
|
std::string getIface();
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
#define INTERACTIVE_H_
|
#define INTERACTIVE_H_
|
||||||
|
|
||||||
class Interactive {
|
class Interactive {
|
||||||
public:
|
public:
|
||||||
Interactive() {
|
Interactive() {
|
||||||
}
|
}
|
||||||
virtual ~Interactive() {
|
virtual ~Interactive() {
|
||||||
|
|
|
||||||
|
|
@ -168,24 +168,25 @@ std::string Packet::opCodeToString() {
|
||||||
|
|
||||||
void Packet::encode(bytes &data) {
|
void Packet::encode(bytes &data) {
|
||||||
int len = data.size();
|
int len = data.size();
|
||||||
bytes key = { 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30,
|
bytes key = {
|
||||||
76, 189, 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58,
|
191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, 76, 189,
|
||||||
188, 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108,
|
131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, 188,
|
||||||
213, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224,
|
132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, 213,
|
||||||
206, 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73,
|
42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, 206,
|
||||||
208, 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60,
|
235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, 208,
|
||||||
113, 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170,
|
209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, 113,
|
||||||
75, 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136,
|
233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, 75,
|
||||||
166, 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248,
|
77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, 166,
|
||||||
255, 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229,
|
210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, 255,
|
||||||
185, 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240,
|
55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, 185,
|
||||||
50, 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226,
|
37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, 50,
|
||||||
38, 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109,
|
251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, 38,
|
||||||
195, 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10,
|
101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, 195,
|
||||||
19, 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93,
|
117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, 19,
|
||||||
65, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53,
|
150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, 65,
|
||||||
102, 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165,
|
154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, 102,
|
||||||
159, 221, 244, 110, 119, 48 };
|
61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, 159,
|
||||||
|
221, 244, 110, 119, 48 };
|
||||||
bytes s = key;
|
bytes s = key;
|
||||||
int i, j = 0;
|
int i, j = 0;
|
||||||
for (int k = 0; k < len; k++) {
|
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) {
|
void Packet::push(bytes &arr, int &index, byte data) {
|
||||||
if (arr.size() > (unsigned) index) {
|
if (arr.size() > (unsigned) index) {
|
||||||
arr[index++] = data;
|
arr[index++] = data;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
arr.push_back(data);
|
arr.push_back(data);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/Packet.h
13
src/Packet.h
|
|
@ -16,7 +16,7 @@
|
||||||
static short sequenceId = 0;
|
static short sequenceId = 0;
|
||||||
|
|
||||||
class Packet {
|
class Packet {
|
||||||
public:
|
public:
|
||||||
enum OpCode {
|
enum OpCode {
|
||||||
DISCOVERY, GET, REPLY, SET, CONFIRM, NONE
|
DISCOVERY, GET, REPLY, SET, CONFIRM, NONE
|
||||||
};
|
};
|
||||||
|
|
@ -45,15 +45,18 @@ public:
|
||||||
byte getOpCode() const;
|
byte getOpCode() const;
|
||||||
void setOpCode(byte opCode);
|
void setOpCode(byte opCode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bytes head = bytes(32);
|
bytes head = bytes(32);
|
||||||
bytes body = bytes(0);
|
bytes body = bytes(0);
|
||||||
datasets payload;
|
datasets payload;
|
||||||
byte version = 1;
|
byte version = 1;
|
||||||
byte opCode;
|
byte opCode;
|
||||||
macAddr switchMac { { 0, 0, 0, 0, 0, 0 } };
|
macAddr switchMac {
|
||||||
macAddr hostMac { { 0, 0, 0, 0, 0, 0 } };
|
{ 0, 0, 0, 0, 0, 0 } };
|
||||||
macAddr broadcastMac { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };
|
macAddr hostMac {
|
||||||
|
{ 0, 0, 0, 0, 0, 0 } };
|
||||||
|
macAddr broadcastMac {
|
||||||
|
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };
|
||||||
short tokenId = 0;
|
short tokenId = 0;
|
||||||
short fragmentOffset = 0;
|
short fragmentOffset = 0;
|
||||||
int errorCode = 0;
|
int errorCode = 0;
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,8 @@ int Program::run(vector<string> arg) {
|
||||||
optind++;
|
optind++;
|
||||||
if (!encode(s))
|
if (!encode(s))
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
fprintf(stderr, "Argument expected after encode\n");
|
fprintf(stderr, "Argument expected after encode\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -85,8 +86,10 @@ int Program::run(vector<string> arg) {
|
||||||
cerr << "Unknown argument " << arg[optind] << endl;
|
cerr << "Unknown argument " << arg[optind] << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
ll.insert(std::pair<std::string, std::string>(sm[1], sm[2]));
|
ll.insert(
|
||||||
} else {
|
std::pair<std::string, std::string>(sm[1], sm[2]));
|
||||||
|
}
|
||||||
|
else {
|
||||||
cerr << "Invalid Syntax " << arg[optind] << endl;
|
cerr << "Invalid Syntax " << arg[optind] << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -104,7 +107,8 @@ int Program::run(vector<string> arg) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
vect.push_back(sm[1]);
|
vect.push_back(sm[1]);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
cerr << "Invalid argument " << arg[optind] << endl;
|
cerr << "Invalid argument " << arg[optind] << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +129,8 @@ int printHeader(Packet p) {
|
||||||
if (options.flags.HEADER) {
|
if (options.flags.HEADER) {
|
||||||
if (options.flags.HEX) {
|
if (options.flags.HEX) {
|
||||||
cout << "Received Header:\n\t" << p.getHead() << "\n";
|
cout << "Received Header:\n\t" << p.getHead() << "\n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
p.printHeader();
|
p.printHeader();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -136,7 +141,8 @@ int printHeader(Packet p) {
|
||||||
int printPacket(Packet p) {
|
int printPacket(Packet p) {
|
||||||
if (options.flags.HEX) {
|
if (options.flags.HEX) {
|
||||||
cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
for (dataset d : p.getPayload()) {
|
for (dataset d : p.getPayload()) {
|
||||||
//auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup;
|
//auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup;
|
||||||
if (lookup.exists(d.type)) {
|
if (lookup.exists(d.type)) {
|
||||||
|
|
@ -144,7 +150,8 @@ int printPacket(Packet p) {
|
||||||
if (d.len > 0) {
|
if (d.len > 0) {
|
||||||
switch (s.format) {
|
switch (s.format) {
|
||||||
case table::STRING:
|
case table::STRING:
|
||||||
cout << "+\t" << s.name << " = " << &d.value[0] << "\n";
|
cout << "+\t" << s.name << " = " << &d.value[0]
|
||||||
|
<< "\n";
|
||||||
break;
|
break;
|
||||||
case table::BOOL:
|
case table::BOOL:
|
||||||
cout << "+\t" << s.name << " = "
|
cout << "+\t" << s.name << " = "
|
||||||
|
|
@ -170,14 +177,17 @@ int printPacket(Packet p) {
|
||||||
cout << "+\t" << s.name << " = " << d.value << "\n";
|
cout << "+\t" << s.name << " = " << d.value << "\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else { //empty
|
}
|
||||||
|
else { //empty
|
||||||
cout << dec << ">\t" << s.name << "\n";
|
cout << dec << ">\t" << s.name << "\n";
|
||||||
}
|
}
|
||||||
} else { //unknown id
|
}
|
||||||
|
else { //unknown id
|
||||||
if (d.len > 0) {
|
if (d.len > 0) {
|
||||||
cout << "##\t" << d.type << ":\n\t";
|
cout << "##\t" << d.type << ":\n\t";
|
||||||
cout << hex << d.value << dec << "\n";
|
cout << hex << d.value << dec << "\n";
|
||||||
} else { //empty
|
}
|
||||||
|
else { //empty
|
||||||
cout << "#>\t" << d.type << "\n";
|
cout << "#>\t" << d.type << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -193,7 +203,8 @@ int Program::list() {
|
||||||
printHeader(a);
|
printHeader(a);
|
||||||
if (options.flags.HEX) {
|
if (options.flags.HEX) {
|
||||||
cout <<"Received Payload:\n"<<a.getBody()<<"\n";
|
cout <<"Received Payload:\n"<<a.getBody()<<"\n";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
datasets d =a.getPayload();
|
datasets d =a.getPayload();
|
||||||
|
|
||||||
int b = a.getSwitchMac().hash();
|
int b = a.getSwitchMac().hash();
|
||||||
|
|
@ -416,8 +427,9 @@ int Program::set(Packet l, datasets t, Listener c) {
|
||||||
p.setHostMac(host.getMac());
|
p.setHostMac(host.getMac());
|
||||||
bytes n = options.user;
|
bytes n = options.user;
|
||||||
bytes w = options.password;
|
bytes w = options.password;
|
||||||
datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD,
|
datasets ld = {
|
||||||
(short) (w.size()), w } };
|
{ LOGIN_USER, (short) (n.size()), n }, {
|
||||||
|
LOGIN_PASSWORD, (short) (w.size()), w } };
|
||||||
p.setPayload(ld + t);
|
p.setPayload(ld + t);
|
||||||
sock->listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac()));
|
sock->listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac()));
|
||||||
sock->send(p);
|
sock->send(p);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<boost::asio::io_service> io_service;
|
std::shared_ptr<boost::asio::io_service> io_service;
|
||||||
std::shared_ptr<Socket> sock;
|
std::shared_ptr<Socket> sock;
|
||||||
Host host = Host();
|
Host host = Host();
|
||||||
|
|
@ -25,7 +25,7 @@ private:
|
||||||
int get(Packet, datasets, std::function<int(Packet)>);
|
int get(Packet, datasets, std::function<int(Packet)>);
|
||||||
int set(Packet, datasets, std::function<int(Packet)>);
|
int set(Packet, datasets, std::function<int(Packet)>);
|
||||||
int discover(std::function<int(Packet)>);
|
int discover(std::function<int(Packet)>);
|
||||||
public:
|
public:
|
||||||
Program() {
|
Program() {
|
||||||
}
|
}
|
||||||
void init();
|
void init();
|
||||||
|
|
|
||||||
|
|
@ -57,14 +57,15 @@ void Socket::setHostIp(ipAddr ip) {
|
||||||
local_ip = ip;
|
local_ip = ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::clear(){
|
void Socket::clear() {
|
||||||
callback.clear();
|
callback.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::listen(Listener l, Filter f) {
|
void Socket::listen(Listener l, Filter f) {
|
||||||
if (callback.find(f) == callback.end()) {
|
if (callback.find(f) == callback.end()) {
|
||||||
callback.insert(ListenerPair(f, l));
|
callback.insert(ListenerPair(f, l));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
callback[f] = l;
|
callback[f] = l;
|
||||||
}
|
}
|
||||||
receive();
|
receive();
|
||||||
|
|
@ -92,7 +93,8 @@ void Socket::receive() {
|
||||||
if (ec || bytes_recvd == 0) {
|
if (ec || bytes_recvd == 0) {
|
||||||
//listen();
|
//listen();
|
||||||
// TODO distinguish error codes
|
// TODO distinguish error codes
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
data.resize(bytes_recvd);
|
data.resize(bytes_recvd);
|
||||||
Packet p = Packet(Packet::NONE);
|
Packet p = Packet(Packet::NONE);
|
||||||
p.encode(data);
|
p.encode(data);
|
||||||
|
|
@ -102,7 +104,8 @@ void Socket::receive() {
|
||||||
r.second(p);
|
r.second(p);
|
||||||
// std::cout<<"pass"<<std::endl;
|
// std::cout<<"pass"<<std::endl;
|
||||||
break;
|
break;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// std::cout<<"no pass"<<std::endl;
|
// std::cout<<"no pass"<<std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ typedef std::function<int(Packet)> Listener;
|
||||||
typedef std::pair<Filter, Listener> ListenerPair;
|
typedef std::pair<Filter, Listener> ListenerPair;
|
||||||
|
|
||||||
class Socket {
|
class Socket {
|
||||||
public:
|
public:
|
||||||
Socket(boost::asio::io_service&);
|
Socket(boost::asio::io_service&);
|
||||||
virtual ~Socket();
|
virtual ~Socket();
|
||||||
void init(short, short);
|
void init(short, short);
|
||||||
|
|
@ -32,7 +32,7 @@ public:
|
||||||
void setHostIp(ipAddr);
|
void setHostIp(ipAddr);
|
||||||
void listen(Listener l, Filter f = Filter());
|
void listen(Listener l, Filter f = Filter());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void receive();
|
void receive();
|
||||||
void settimeout();
|
void settimeout();
|
||||||
boost::asio::ip::udp::socket send_socket_;
|
boost::asio::ip::udp::socket send_socket_;
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ int Switch::parse(std::string str) {
|
||||||
|
|
||||||
if (json.Parse(str.c_str()).HasParseError())
|
if (json.Parse(str.c_str()).HasParseError())
|
||||||
return 1;
|
return 1;
|
||||||
if (options.debug_level>=1)
|
if (options.debug_level >= 1)
|
||||||
std::cout << "\nParsing to document succeeded.\n";
|
std::cout << "\nParsing to document succeeded.\n";
|
||||||
|
|
||||||
if (json.IsObject()) {
|
if (json.IsObject()) {
|
||||||
|
|
|
||||||
12
src/Switch.h
12
src/Switch.h
|
|
@ -59,13 +59,13 @@ struct port {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Switch {
|
class Switch {
|
||||||
public:
|
public:
|
||||||
Switch() {
|
Switch() {
|
||||||
}
|
}
|
||||||
int parse(datasets);
|
int parse(datasets);
|
||||||
int parse(dataset);
|
int parse(dataset);
|
||||||
int parse(std::string);
|
int parse(std::string);
|
||||||
int set(std::pair<std::string,std::string>);
|
int set(std::pair<std::string, std::string>);
|
||||||
|
|
||||||
std::string get(std::string);
|
std::string get(std::string);
|
||||||
std::string toString();
|
std::string toString();
|
||||||
|
|
@ -76,14 +76,16 @@ public:
|
||||||
std::string type;
|
std::string type;
|
||||||
std::string hardware_version;
|
std::string hardware_version;
|
||||||
std::string firmware_version;
|
std::string firmware_version;
|
||||||
macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
|
macAddr mac {
|
||||||
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
|
||||||
int ports;
|
int ports;
|
||||||
} device;
|
} device;
|
||||||
struct {
|
struct {
|
||||||
std::string password = DEFAULT_PASS;
|
std::string password = DEFAULT_PASS;
|
||||||
std::string username = DEFAULT_USER;
|
std::string username = DEFAULT_USER;
|
||||||
std::string hostname;
|
std::string hostname;
|
||||||
ipAddr ip_addr { 0, 0, 0, 0, };
|
ipAddr ip_addr {
|
||||||
|
0, 0, 0, 0, };
|
||||||
ipAddr ip_mask;
|
ipAddr ip_mask;
|
||||||
ipAddr gateway;
|
ipAddr gateway;
|
||||||
bool dhcp;
|
bool dhcp;
|
||||||
|
|
@ -93,7 +95,7 @@ public:
|
||||||
//mtu_vlan
|
//mtu_vlan
|
||||||
} settings;
|
} settings;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
rapidjson::Document json;
|
rapidjson::Document json;
|
||||||
std::vector<vlan> vlans;
|
std::vector<vlan> vlans;
|
||||||
std::vector<port> ports;
|
std::vector<port> ports;
|
||||||
|
|
|
||||||
14
src/Types.h
14
src/Types.h
|
|
@ -20,7 +20,7 @@
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
|
|
||||||
class macAddr: public std::array<byte, 6> {
|
class macAddr: public std::array<byte, 6> {
|
||||||
public:
|
public:
|
||||||
friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) {
|
friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) {
|
||||||
out << std::hex << std::setw(2) << std::setfill('0')
|
out << std::hex << std::setw(2) << std::setfill('0')
|
||||||
<< (unsigned) arr[0];
|
<< (unsigned) arr[0];
|
||||||
|
|
@ -72,9 +72,9 @@ public:
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
class mac_addr : public std::array<unsigned char, 6> {
|
class mac_addr : public std::array<unsigned char, 6> {
|
||||||
public:
|
public:
|
||||||
typedef std::array<unsigned char, 6> super;
|
typedef std::array<unsigned char, 6> super;
|
||||||
|
|
@ -86,7 +86,7 @@ public:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class ipAddr: public std::array<byte, 4> {
|
class ipAddr: public std::array<byte, 4> {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ipAddr() {
|
ipAddr() {
|
||||||
*this= {0,0,0,0,0,0};
|
*this= {0,0,0,0,0,0};
|
||||||
|
|
@ -115,13 +115,13 @@ public:
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace smrtlink {
|
namespace smrtlink {
|
||||||
|
|
||||||
constexpr unsigned int caseArg(const char* str, int h = 0) {
|
constexpr unsigned int caseArg(const char* str, int h = 0) {
|
||||||
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
|
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
#include "bytes.h"
|
#include "bytes.h"
|
||||||
|
|
||||||
bytes bytes::readHex(std::string s){
|
bytes bytes::readHex(std::string s) {
|
||||||
vector ret;
|
vector ret;
|
||||||
std::string delimiter = ":";
|
std::string delimiter = ":";
|
||||||
std::string token;
|
std::string token;
|
||||||
|
|
|
||||||
16
src/bytes.h
16
src/bytes.h
|
|
@ -20,19 +20,20 @@ typedef unsigned char byte;
|
||||||
|
|
||||||
class bytes: public std::vector<unsigned char> {
|
class bytes: public std::vector<unsigned char> {
|
||||||
typedef std::vector<unsigned char> vector;
|
typedef std::vector<unsigned char> vector;
|
||||||
public:
|
public:
|
||||||
using vector::operator[];
|
using vector::operator[];
|
||||||
bytes() {
|
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');
|
this->push_back('\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes(std::initializer_list<uint8_t> s)
|
bytes(std::initializer_list<uint8_t> s) {
|
||||||
{
|
|
||||||
for (uint8_t b : s) {
|
for (uint8_t b : s) {
|
||||||
this->push_back(b);
|
this->push_back(b);
|
||||||
}
|
}
|
||||||
|
|
@ -71,13 +72,14 @@ public:
|
||||||
<< (unsigned) arr[0];
|
<< (unsigned) arr[0];
|
||||||
}
|
}
|
||||||
for (unsigned i = 1; i < arr.size(); i++) {
|
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;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator std::string() {
|
operator std::string() {
|
||||||
std::string s(this->begin(),this->end());
|
std::string s(this->begin(), this->end());
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,11 @@ struct dataset {
|
||||||
bytes value;
|
bytes value;
|
||||||
};
|
};
|
||||||
|
|
||||||
class datasets : public std::vector<dataset> {
|
class datasets: public std::vector<dataset> {
|
||||||
public:
|
public:
|
||||||
datasets(){};
|
datasets() {
|
||||||
datasets(std::initializer_list<dataset> s)
|
}
|
||||||
{
|
datasets(std::initializer_list<dataset> s) {
|
||||||
for (dataset b : s) {
|
for (dataset b : s) {
|
||||||
//(*this)[b.type]=b;
|
//(*this)[b.type]=b;
|
||||||
push_back(b);
|
push_back(b);
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
class jsonNode: public rapidjson::Value {
|
class jsonNode: public rapidjson::Value {
|
||||||
typedef rapidjson::Value super;
|
typedef rapidjson::Value super;
|
||||||
typedef rapidjson::Document doc;
|
typedef rapidjson::Document doc;
|
||||||
public:
|
public:
|
||||||
jsonNode(const std::string&, doc&);
|
jsonNode(const std::string&, doc&);
|
||||||
jsonNode(const macAddr&, doc&);
|
jsonNode(const macAddr&, doc&);
|
||||||
jsonNode(const ipAddr&, doc&);
|
jsonNode(const ipAddr&, doc&);
|
||||||
|
|
@ -23,8 +23,8 @@ public:
|
||||||
jsonNode(const port&, doc&);
|
jsonNode(const port&, doc&);
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
jsonNode(const T &templ_arg, doc &root){
|
jsonNode(const T &templ_arg, doc &root) {
|
||||||
std::cerr<<"Not serializable Type: "<<typeid(T).name()<<"\n";
|
std::cerr << "Not serializable Type: " << typeid(T).name() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
|
||||||
|
|
@ -38,17 +38,22 @@ int main(int argc, char *argv[]) {
|
||||||
options.user = DEFAULT_USER;
|
options.user = DEFAULT_USER;
|
||||||
options.password = DEFAULT_PASS;
|
options.password = DEFAULT_PASS;
|
||||||
|
|
||||||
const struct option longopts[] = { { "version", no_argument, 0, 'V' }, {
|
const struct option longopts[] = {
|
||||||
"verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, {
|
{ "version", no_argument, 0, 'V' }, {
|
||||||
|
"verbose", no_argument, 0, 'v' },
|
||||||
|
{ "help", no_argument, 0, 'h' }, {
|
||||||
"reverse", no_argument, 0, 'r' },
|
"reverse", no_argument, 0, 'r' },
|
||||||
{ "permanent", no_argument, 0, 's' }, { "debug", optional_argument,
|
{ "permanent", no_argument, 0, 's' }, {
|
||||||
0, 'd' }, { "password", required_argument, 0, 'P' }, {
|
"debug", optional_argument, 0, 'd' },
|
||||||
"user", required_argument, 0, 'U' }, { "interface",
|
{ "password", required_argument, 0, 'P' }, {
|
||||||
required_argument, 0, 'i' }, { "header", required_argument,
|
"user", required_argument, 0, 'U' },
|
||||||
0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file",
|
{ "interface", required_argument, 0, 'i' }, {
|
||||||
required_argument, 0, 'f' }, { "timeout", required_argument,
|
"header", required_argument, 0, 'b' },
|
||||||
0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0,
|
{ "hex", required_argument, 0, 'x' }, {
|
||||||
0 }, };
|
"file", required_argument, 0, 'f' },
|
||||||
|
{ "timeout", required_argument, 0, 't' }, {
|
||||||
|
"wait", required_argument, 0, 'w' },
|
||||||
|
{ 0, 0, 0, 0 }, };
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts,
|
while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts,
|
||||||
&index)) != -1) {
|
&index)) != -1) {
|
||||||
|
|
@ -152,7 +157,8 @@ int main(int argc, char *argv[]) {
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
fprintf(stderr, "Not yet implemented.\n");
|
fprintf(stderr, "Not yet implemented.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else if (optind < argc) {
|
}
|
||||||
|
else if (optind < argc) {
|
||||||
vector<string> v;
|
vector<string> v;
|
||||||
while (optind < argc)
|
while (optind < argc)
|
||||||
v.push_back(argv[optind++]);
|
v.push_back(argv[optind++]);
|
||||||
|
|
|
||||||
|
|
@ -18,24 +18,24 @@ table::table(std::initializer_list<set> l) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const table::set table::operator[](std::string s){
|
const table::set table::operator[](std::string s) {
|
||||||
return *this->right[s];
|
return *this->right[s];
|
||||||
}
|
}
|
||||||
const table::set table::operator[](short n){
|
const table::set table::operator[](short n) {
|
||||||
return *this->left[n];
|
return *this->left[n];
|
||||||
}
|
}
|
||||||
bool table::exists(std::string s){
|
bool table::exists(std::string s) {
|
||||||
return !(right.find(s) == right.end());
|
return !(right.find(s) == right.end());
|
||||||
}
|
}
|
||||||
bool table::exists(short n){
|
bool table::exists(short n) {
|
||||||
return !(left.find(n) == left.end());
|
return !(left.find(n) == left.end());
|
||||||
}
|
}
|
||||||
short table::type(std::string s){
|
short table::type(std::string s) {
|
||||||
return this->right[s]->type;
|
return this->right[s]->type;
|
||||||
}
|
}
|
||||||
std::string table::id(short n){
|
std::string table::id(short n) {
|
||||||
return this->left[n]->id;
|
return this->left[n]->id;
|
||||||
}
|
}
|
||||||
std::string table::name(short n){
|
std::string table::name(short n) {
|
||||||
return this->left[n]->name;
|
return this->left[n]->name;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class table {
|
class table {
|
||||||
public:
|
public:
|
||||||
enum A {
|
enum A {
|
||||||
RW, RO, WO, NONE, UNKNOWN
|
RW, RO, WO, NONE, UNKNOWN
|
||||||
};
|
};
|
||||||
|
|
@ -34,7 +34,7 @@ public:
|
||||||
short type(std::string);
|
short type(std::string);
|
||||||
std::string id(short);
|
std::string id(short);
|
||||||
std::string name(short);
|
std::string name(short);
|
||||||
private:
|
private:
|
||||||
std::vector<set> data;
|
std::vector<set> data;
|
||||||
std::map<short, set*> left;
|
std::map<short, set*> left;
|
||||||
std::map<std::string, set*> right;
|
std::map<std::string, set*> right;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue