token ID added
This commit is contained in:
parent
4c371de8f9
commit
3ef2da7b7a
9 changed files with 575 additions and 573 deletions
2
Makefile
2
Makefile
|
|
@ -1,5 +1,5 @@
|
||||||
CC = g++
|
CC = g++
|
||||||
CFLAGS = -g -c -std=c++14
|
CFLAGS = -o0 -g -c -std=c++14
|
||||||
TARGET = smrtlink
|
TARGET = smrtlink
|
||||||
SOURCEDIR = src
|
SOURCEDIR = src
|
||||||
BUILDDIR = bin
|
BUILDDIR = bin
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
Packet::Packet(OpCode c) {
|
Packet::Packet(OpCode c) {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
sequenceId = rand() % 1000;
|
sequenceId = sequenceId != 0 ? sequenceId + 1 : rand() % 1000;
|
||||||
opCode = c;
|
opCode = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,6 +27,7 @@ void Packet::printHeader() {
|
||||||
std::cout << "\tLength: \t" << std::dec << this->getLength() << "\n";
|
std::cout << "\tLength: \t" << std::dec << this->getLength() << "\n";
|
||||||
std::cout << "\tOffset: \t" << fragmentOffset << "\n";
|
std::cout << "\tOffset: \t" << fragmentOffset << "\n";
|
||||||
std::cout << "\tFlags: \t" << std::hex << flag << "\n";
|
std::cout << "\tFlags: \t" << std::hex << flag << "\n";
|
||||||
|
std::cout << "\ttokenID: \t" << tokenId << "\n";
|
||||||
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
|
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,12 +112,16 @@ void Packet::setCheckSum(int checkSum) {
|
||||||
this->checkSum = checkSum;
|
this->checkSum = checkSum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Packet::getErrorCode() const {
|
||||||
|
return errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
short Packet::getSequenceId() const {
|
short Packet::getSequenceId() const {
|
||||||
return sequenceId;
|
return sequenceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Packet::setSequenceId(short sequenceId) {
|
void Packet::setSequenceId(short sId) {
|
||||||
this->sequenceId = sequenceId;
|
sequenceId = sId;
|
||||||
}
|
}
|
||||||
|
|
||||||
macAddr Packet::getSwitchMac() const {
|
macAddr Packet::getSwitchMac() const {
|
||||||
|
|
@ -135,6 +140,14 @@ void Packet::setPayload(datasets payload) {
|
||||||
this->payload = payload;
|
this->payload = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
short Packet::getTokenId() const {
|
||||||
|
return tokenId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Packet::setTokenId(short tokenId) {
|
||||||
|
this->tokenId = tokenId;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Packet::opCodeToString() {
|
std::string Packet::opCodeToString() {
|
||||||
switch (opCode) {
|
switch (opCode) {
|
||||||
case DISCOVERY:
|
case DISCOVERY:
|
||||||
|
|
|
||||||
17
src/Packet.h
17
src/Packet.h
|
|
@ -13,11 +13,12 @@
|
||||||
|
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
class Packet {
|
static short sequenceId=0;
|
||||||
|
|
||||||
|
class Packet
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
enum OpCode {
|
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
|
||||||
DISCOVERY, GET, RETURN, SET, CONFIRM
|
|
||||||
};
|
|
||||||
Packet(OpCode);
|
Packet(OpCode);
|
||||||
void encode(bytes&);
|
void encode(bytes&);
|
||||||
bytes getBytes();
|
bytes getBytes();
|
||||||
|
|
@ -26,6 +27,7 @@ public:
|
||||||
std::string opCodeToString();
|
std::string opCodeToString();
|
||||||
short getLength() const;
|
short getLength() const;
|
||||||
int getCheckSum() const;
|
int getCheckSum() const;
|
||||||
|
int getErrorCode() const;
|
||||||
short getSequenceId() const;
|
short getSequenceId() const;
|
||||||
macAddr getSwitchMac() const;
|
macAddr getSwitchMac() const;
|
||||||
const bytes& getBody() const;
|
const bytes& getBody() const;
|
||||||
|
|
@ -37,27 +39,25 @@ public:
|
||||||
void setCheckSum(int);
|
void setCheckSum(int);
|
||||||
void setSequenceId(short );
|
void setSequenceId(short );
|
||||||
void setPayload(datasets payload);
|
void setPayload(datasets payload);
|
||||||
|
short getTokenId() const;
|
||||||
|
void setTokenId(short tokenId = 0);
|
||||||
|
|
||||||
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 { {0, 0, 0, 0, 0, 0}};
|
||||||
macAddr hostMac { {0, 0, 0, 0, 0, 0}};
|
macAddr hostMac { {0, 0, 0, 0, 0, 0}};
|
||||||
macAddr broadcastMac { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
|
macAddr broadcastMac { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
|
||||||
short sequenceId;
|
|
||||||
short tokenId = 0;
|
short tokenId = 0;
|
||||||
short fragmentOffset = 0;
|
short fragmentOffset = 0;
|
||||||
int errorCode = 0;
|
int errorCode = 0;
|
||||||
int checkSum = 0;
|
int checkSum = 0;
|
||||||
short flag = 0;
|
short flag = 0;
|
||||||
|
|
||||||
void buildHead();
|
void buildHead();
|
||||||
void buildBody();
|
void buildBody();
|
||||||
|
|
||||||
void push(bytes&, int&, short );
|
void push(bytes&, int&, short );
|
||||||
void push(bytes&, int&, int);
|
void push(bytes&, int&, int);
|
||||||
void push(bytes&, int&, byte);
|
void push(bytes&, int&, byte);
|
||||||
|
|
@ -65,7 +65,6 @@ private:
|
||||||
void push(bytes&, int&, ipAddr);
|
void push(bytes&, int&, ipAddr);
|
||||||
void push(bytes&, int&, macAddr);
|
void push(bytes&, int&, macAddr);
|
||||||
void push(bytes&, int&, dataset);
|
void push(bytes&, int&, dataset);
|
||||||
|
|
||||||
void pull(bytes&, int&, short &);
|
void pull(bytes&, int&, short &);
|
||||||
void pull(bytes&, int&, int&);
|
void pull(bytes&, int&, int&);
|
||||||
void pull(bytes&, int&, byte&);
|
void pull(bytes&, int&, byte&);
|
||||||
|
|
|
||||||
|
|
@ -37,41 +37,42 @@ int printPacket(Packet p) {
|
||||||
auto lookup =
|
auto lookup =
|
||||||
(options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
|
(options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
|
||||||
if (lookup.exists(d.type)) {
|
if (lookup.exists(d.type)) {
|
||||||
const table::set *s = lookup.get(d.type);
|
table::set s = lookup[d.type];
|
||||||
if (d.len > 0) {
|
if (d.len > 0) {
|
||||||
switch (s->format) {
|
switch (s.format) {
|
||||||
case table::STRING:
|
case table::STRING:
|
||||||
std::cout << "+\t" << s->name << " = " << &d.value[0]
|
std::cout << "+\t" << s.name << " = " << &d.value[0]
|
||||||
<< "\n";
|
<< "\n";
|
||||||
break;
|
break;
|
||||||
case table::BOOL:
|
case table::BOOL:
|
||||||
std::cout << "+\t" << s->name << " = " << (d.value[0]?"YES":"NO")
|
std::cout << "+\t" << s.name << " = "
|
||||||
<< "\n";
|
<< (d.value[0] ? "YES" : "NO") << "\n";
|
||||||
break;
|
break;
|
||||||
case table::HEX:
|
case table::HEX:
|
||||||
std::cout << "+\t" << s->name << " = " << d.value
|
std::cout << "+\t" << s.name << " = " << d.value
|
||||||
<< "\n";
|
<< "\n";
|
||||||
break;
|
break;
|
||||||
case table::DEC:
|
case table::DEC:
|
||||||
std::cout << "+\t" << s->name << " = ";
|
std::cout << "+\t" << s.name << " = ";
|
||||||
if (d.value.size() > 0)
|
if (d.value.size() > 0)
|
||||||
std::cout << std::dec << (unsigned) d.value[0];
|
std::cout << std::dec << (unsigned) d.value[0];
|
||||||
for (unsigned i = 1; i < d.value.size(); i++)
|
for (unsigned i = 1; i < d.value.size(); i++)
|
||||||
std::cout << std::dec << "." << (unsigned) d.value[i];
|
std::cout << std::dec << "."
|
||||||
|
<< (unsigned) d.value[i];
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
break;
|
break;
|
||||||
case table::ACTION:
|
case table::ACTION:
|
||||||
std::cout << "Error:" << s->name
|
std::cout << "Error:" << s.name
|
||||||
<< " is marked as 'action' but carries payload."
|
<< " is marked as 'action' but carries payload."
|
||||||
<< d.value << "\n";
|
<< d.value << "\n";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << "+\t" << s->name << " = " << d.value
|
std::cout << "+\t" << s.name << " = " << d.value
|
||||||
<< "\n";
|
<< "\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else { //empty
|
} else { //empty
|
||||||
std::cout << std::dec << ">\t" << s->name << "\n";
|
std::cout << std::dec << ">\t" << s.name << "\n";
|
||||||
}
|
}
|
||||||
} else { //unknown id
|
} else { //unknown id
|
||||||
if (d.len > 0) {
|
if (d.len > 0) {
|
||||||
|
|
@ -109,7 +110,7 @@ int Program::list() {
|
||||||
sw.parse(d);
|
sw.parse(d);
|
||||||
File f;
|
File f;
|
||||||
f.write(sw.toString());
|
f.write(sw.toString());
|
||||||
std::cout <<"Devices:\n\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
@ -172,15 +173,16 @@ int Program::getProperty() {
|
||||||
datasets d =a.getPayload();
|
datasets d =a.getPayload();
|
||||||
Switch sw = Switch();
|
Switch sw = Switch();
|
||||||
sw.parse(d);
|
sw.parse(d);
|
||||||
std::cout <<"Devices:\n\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
std::cout <<"\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
|
||||||
|
|
||||||
Packet p = Packet(Packet::GET);
|
Packet p = Packet(Packet::GET);
|
||||||
p.setSwitchMac(a.getSwitchMac());
|
p.setSwitchMac(a.getSwitchMac());
|
||||||
p.setHostMac(host.getMac());
|
p.setHostMac(host.getMac());
|
||||||
datasets t = { {snd_lookup["ping"], 0, {}}};
|
datasets t = { {SND_PING, 0, {}}};
|
||||||
p.setPayload(t);
|
p.setPayload(t);
|
||||||
bytes c = p.getBytes();
|
bytes c = p.getBytes();
|
||||||
p.encode(c);
|
p.encode(c);
|
||||||
|
|
||||||
sock->callback =
|
sock->callback =
|
||||||
[this](Packet a) {
|
[this](Packet a) {
|
||||||
auto s = sock;
|
auto s = sock;
|
||||||
|
|
@ -189,12 +191,17 @@ int Program::getProperty() {
|
||||||
sw.parse(d);
|
sw.parse(d);
|
||||||
Packet p = Packet(Packet::SET);
|
Packet p = Packet(Packet::SET);
|
||||||
p.setSwitchMac(a.getSwitchMac());
|
p.setSwitchMac(a.getSwitchMac());
|
||||||
|
p.setTokenId(a.getTokenId());
|
||||||
p.setHostMac(host.getMac());
|
p.setHostMac(host.getMac());
|
||||||
|
bytes n = options.user;
|
||||||
|
bytes w = options.password;
|
||||||
|
n.push_back('\0');
|
||||||
|
w.push_back('\0');
|
||||||
datasets t = {
|
datasets t = {
|
||||||
{snd_lookup["login_user"], (short)(options.user.length()), options.user},
|
{ LOGIN_USER, (short)(n.size()), n},
|
||||||
{snd_lookup["login_password"], (short)(options.password.length()), options.password}
|
{ LOGIN_PASSWORD, (short)(w.size()), w},
|
||||||
|
{ REBOOT, 1, {0}}
|
||||||
};
|
};
|
||||||
std::cout<<options.user<<std::endl<<options.password<<std::endl;
|
|
||||||
p.setPayload(t);
|
p.setPayload(t);
|
||||||
bytes c = p.getBytes();
|
bytes c = p.getBytes();
|
||||||
p.encode(c);
|
p.encode(c);
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ void Socket::init(short dst_port, short src_port) {
|
||||||
if (options.flags & FLAG_DEBUG)
|
if (options.flags & FLAG_DEBUG)
|
||||||
std::cout << "Local IP:\t" << local_ip << "\n";
|
std::cout << "Local IP:\t" << local_ip << "\n";
|
||||||
|
|
||||||
|
|
||||||
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
|
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||||
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
||||||
local_endpoint_ = boost::asio::ip::udp::endpoint(
|
local_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||||
|
|
@ -89,9 +88,11 @@ void Socket::listen() {
|
||||||
// TODO distinguish error codes
|
// TODO distinguish error codes
|
||||||
} else {
|
} else {
|
||||||
data.resize(bytes_recvd);
|
data.resize(bytes_recvd);
|
||||||
Packet p = Packet(Packet::RETURN);
|
Packet p = Packet(Packet::NONE);
|
||||||
p.encode(data);
|
p.encode(data);
|
||||||
|
// std::cout << "err" << p.getErrorCode() <<std::endl;
|
||||||
p.parse(data);
|
p.parse(data);
|
||||||
|
//std::cout << "err" << p.getErrorCode() <<std::endl;
|
||||||
if(!callback(p)) {
|
if(!callback(p)) {
|
||||||
//TODO do something
|
//TODO do something
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,29 +10,16 @@
|
||||||
|
|
||||||
table::table(std::initializer_list<set> l) {
|
table::table(std::initializer_list<set> l) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
this->data.resize(l.size());
|
|
||||||
for (set s : l) {
|
for (set s : l) {
|
||||||
this->data[i] = s;
|
this->left[s.type] = s;
|
||||||
this->left[s.type] = &this->data[i];
|
|
||||||
this->right[s.name] = &this->data[i];
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
short table::operator[](std::string s){
|
table::set table::operator[](short n){
|
||||||
return this->right[s]->type;
|
return this->left[n];
|
||||||
}
|
|
||||||
std::string table::operator[](short n){
|
|
||||||
return this->left[n]->name;
|
|
||||||
}
|
|
||||||
bool table::exists(std::string s){
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
const table::set* table::get(std::string s){
|
std::string table::name(short n){
|
||||||
return this->right[s];
|
return this->left[n].name;
|
||||||
}
|
|
||||||
const table::set* table::get(short n){
|
|
||||||
return this->left[n];
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
src/table.h
11
src/table.h
|
|
@ -20,16 +20,11 @@ public:
|
||||||
std::string name;
|
std::string name;
|
||||||
};
|
};
|
||||||
table(std::initializer_list<set> l);
|
table(std::initializer_list<set> l);
|
||||||
short operator[](std::string);
|
set operator[](short);
|
||||||
std::string operator[](short);
|
|
||||||
bool exists(std::string);
|
|
||||||
bool exists(short);
|
bool exists(short);
|
||||||
const set* get(std::string);
|
std::string name(short);
|
||||||
const set* get(short);
|
|
||||||
private:
|
private:
|
||||||
std::vector<set> data;
|
std::map<short, set> left;
|
||||||
std::map<short, set*> left;
|
|
||||||
std::map<std::string, set*> right;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* LOOKUPTABLE_H_ */
|
#endif /* LOOKUPTABLE_H_ */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue