token ID added

This commit is contained in:
j3d1 2016-01-21 04:58:21 +01:00
parent 4c371de8f9
commit 3ef2da7b7a
9 changed files with 575 additions and 573 deletions

View file

@ -1,5 +1,5 @@
CC = g++
CFLAGS = -g -c -std=c++14
CFLAGS = -o0 -g -c -std=c++14
TARGET = smrtlink
SOURCEDIR = src
BUILDDIR = bin

View file

@ -13,7 +13,7 @@
Packet::Packet(OpCode c) {
srand(time(NULL));
sequenceId = rand() % 1000;
sequenceId = sequenceId != 0 ? sequenceId + 1 : rand() % 1000;
opCode = c;
}
@ -27,6 +27,7 @@ void Packet::printHeader() {
std::cout << "\tLength: \t" << std::dec << this->getLength() << "\n";
std::cout << "\tOffset: \t" << fragmentOffset << "\n";
std::cout << "\tFlags: \t" << std::hex << flag << "\n";
std::cout << "\ttokenID: \t" << tokenId << "\n";
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
}
@ -111,12 +112,16 @@ void Packet::setCheckSum(int checkSum) {
this->checkSum = checkSum;
}
int Packet::getErrorCode() const {
return errorCode;
}
short Packet::getSequenceId() const {
return sequenceId;
}
void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId;
void Packet::setSequenceId(short sId) {
sequenceId = sId;
}
macAddr Packet::getSwitchMac() const {
@ -135,6 +140,14 @@ void Packet::setPayload(datasets payload) {
this->payload = payload;
}
short Packet::getTokenId() const {
return tokenId;
}
void Packet::setTokenId(short tokenId) {
this->tokenId = tokenId;
}
std::string Packet::opCodeToString() {
switch (opCode) {
case DISCOVERY:

View file

@ -13,11 +13,12 @@
#include "Types.h"
class Packet {
static short sequenceId=0;
class Packet
{
public:
enum OpCode {
DISCOVERY, GET, RETURN, SET, CONFIRM
};
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
Packet(OpCode);
void encode(bytes&);
bytes getBytes();
@ -26,6 +27,7 @@ public:
std::string opCodeToString();
short getLength() const;
int getCheckSum() const;
int getErrorCode() const;
short getSequenceId() const;
macAddr getSwitchMac() const;
const bytes& getBody() const;
@ -37,27 +39,25 @@ public:
void setCheckSum(int);
void setSequenceId(short );
void setPayload(datasets payload);
short getTokenId() const;
void setTokenId(short tokenId = 0);
private:
bytes head = bytes(32);
bytes body = bytes(0);
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}};
short sequenceId;
short tokenId = 0;
short fragmentOffset = 0;
int errorCode = 0;
int checkSum = 0;
short flag = 0;
void buildHead();
void buildBody();
void push(bytes&, int&, short );
void push(bytes&, int&, int);
void push(bytes&, int&, byte);
@ -65,7 +65,6 @@ private:
void push(bytes&, int&, ipAddr);
void push(bytes&, int&, macAddr);
void push(bytes&, int&, dataset);
void pull(bytes&, int&, short &);
void pull(bytes&, int&, int&);
void pull(bytes&, int&, byte&);

View file

@ -37,41 +37,42 @@ int printPacket(Packet p) {
auto lookup =
(options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
if (lookup.exists(d.type)) {
const table::set *s = lookup.get(d.type);
table::set s = lookup[d.type];
if (d.len > 0) {
switch (s->format) {
switch (s.format) {
case table::STRING:
std::cout << "+\t" << s->name << " = " << &d.value[0]
std::cout << "+\t" << s.name << " = " << &d.value[0]
<< "\n";
break;
case table::BOOL:
std::cout << "+\t" << s->name << " = " << (d.value[0]?"YES":"NO")
<< "\n";
std::cout << "+\t" << s.name << " = "
<< (d.value[0] ? "YES" : "NO") << "\n";
break;
case table::HEX:
std::cout << "+\t" << s->name << " = " << d.value
std::cout << "+\t" << s.name << " = " << d.value
<< "\n";
break;
case table::DEC:
std::cout << "+\t" << s->name << " = ";
std::cout << "+\t" << s.name << " = ";
if (d.value.size() > 0)
std::cout << std::dec << (unsigned) d.value[0];
for (unsigned i = 1; i < d.value.size(); i++)
std::cout << std::dec << "." << (unsigned) d.value[i];
std::cout << std::dec << "."
<< (unsigned) d.value[i];
std::cout << "\n";
break;
case table::ACTION:
std::cout << "Error:" << s->name
std::cout << "Error:" << s.name
<< " is marked as 'action' but carries payload."
<< d.value << "\n";
break;
default:
std::cout << "+\t" << s->name << " = " << d.value
std::cout << "+\t" << s.name << " = " << d.value
<< "\n";
break;
}
} else { //empty
std::cout << std::dec << ">\t" << s->name << "\n";
std::cout << std::dec << ">\t" << s.name << "\n";
}
} else { //unknown id
if (d.len > 0) {
@ -109,7 +110,7 @@ int Program::list() {
sw.parse(d);
File f;
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;
};
@ -172,15 +173,16 @@ int Program::getProperty() {
datasets d =a.getPayload();
Switch sw = Switch();
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);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {snd_lookup["ping"], 0, {}}};
datasets t = { {SND_PING, 0, {}}};
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
sock->callback =
[this](Packet a) {
auto s = sock;
@ -189,12 +191,17 @@ int Program::getProperty() {
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 = {
{snd_lookup["login_user"], (short)(options.user.length()), options.user},
{snd_lookup["login_password"], (short)(options.password.length()), options.password}
{ LOGIN_USER, (short)(n.size()), n},
{ LOGIN_PASSWORD, (short)(w.size()), w},
{ REBOOT, 1, {0}}
};
std::cout<<options.user<<std::endl<<options.password<<std::endl;
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);

View file

@ -30,7 +30,6 @@ void Socket::init(short dst_port, short src_port) {
if (options.flags & FLAG_DEBUG)
std::cout << "Local IP:\t" << local_ip << "\n";
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
local_endpoint_ = boost::asio::ip::udp::endpoint(
@ -89,9 +88,11 @@ void Socket::listen() {
// TODO distinguish error codes
} else {
data.resize(bytes_recvd);
Packet p = Packet(Packet::RETURN);
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
}

View file

@ -10,29 +10,16 @@
table::table(std::initializer_list<set> l) {
int i = 0;
this->data.resize(l.size());
for (set s : l) {
this->data[i] = s;
this->left[s.type] = &this->data[i];
this->right[s.name] = &this->data[i];
i++;
this->left[s.type] = s;
}
}
short table::operator[](std::string s){
return this->right[s]->type;
}
std::string table::operator[](short n){
return this->left[n]->name;
}
bool table::exists(std::string s){
return !(right.find(s) == right.end());
table::set table::operator[](short n){
return this->left[n];
}
bool table::exists(short n){
return !(left.find(n) == left.end());
}
const table::set* table::get(std::string s){
return this->right[s];
}
const table::set* table::get(short n){
return this->left[n];
std::string table::name(short n){
return this->left[n].name;
}

View file

@ -20,16 +20,11 @@ public:
std::string name;
};
table(std::initializer_list<set> l);
short operator[](std::string);
std::string operator[](short);
bool exists(std::string);
set operator[](short);
bool exists(short);
const set* get(std::string);
const set* get(short);
std::string name(short);
private:
std::vector<set> data;
std::map<short, set*> left;
std::map<std::string, set*> right;
std::map<short, set> left;
};
#endif /* LOOKUPTABLE_H_ */