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++ 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

View file

@ -12,260 +12,273 @@
#include "Types.h" #include "Types.h"
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;
} }
void Packet::printHeader() { void Packet::printHeader() {
std::cout << "Header:\n"; std::cout << "Header:\n";
std::cout << "\tID:\t\t" << sequenceId << "\n"; std::cout << "\tID:\t\t" << sequenceId << "\n";
std::cout << "\tVersion:\t" << (int) version << "\n"; std::cout << "\tVersion:\t" << (int) version << "\n";
std::cout << "\tError:\t\t" << errorCode << "\n"; std::cout << "\tError:\t\t" << errorCode << "\n";
std::cout << "\tSwitch MAC:\t" << switchMac << "\n"; std::cout << "\tSwitch MAC:\t" << switchMac << "\n";
std::cout << "\tHost MAC: \t" << hostMac << "\n"; std::cout << "\tHost MAC: \t" << hostMac << "\n";
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 << "\tChecksum: \t" << std::dec << checkSum << "\n"; std::cout << "\ttokenID: \t" << tokenId << "\n";
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
} }
bytes Packet::getBytes() { bytes Packet::getBytes() {
int i = 0; int i = 0;
for (auto d : payload) for (auto d : payload)
push(body, i, d); push(body, i, d);
push(body, i, (int) PACKET_END); push(body, i, (int) PACKET_END);
i = 0; i = 0;
push(head, i, version); push(head, i, version);
push(head, i, opCode); push(head, i, opCode);
push(head, i, switchMac); push(head, i, switchMac);
push(head, i, hostMac); push(head, i, hostMac);
push(head, i, sequenceId); push(head, i, sequenceId);
push(head, i, errorCode); push(head, i, errorCode);
push(head, i, this->getLength()); push(head, i, this->getLength());
push(head, i, fragmentOffset); push(head, i, fragmentOffset);
push(head, i, flag); push(head, i, flag);
push(head, i, tokenId); push(head, i, tokenId);
push(head, i, checkSum); push(head, i, checkSum);
bytes data = head + body; bytes data = head + body;
return data; return data;
} }
void Packet::parse(bytes data) { void Packet::parse(bytes data) {
memcpy(&head[0], &data[0], HEADER_LEN); memcpy(&head[0], &data[0], HEADER_LEN);
body.resize(data.size() - HEADER_LEN); body.resize(data.size() - HEADER_LEN);
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN); memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
int i = 0; int i = 0;
short checkLen = 0x0; short checkLen = 0x0;
pull(head, i, version); pull(head, i, version);
pull(head, i, opCode); pull(head, i, opCode);
pull(head, i, switchMac); pull(head, i, switchMac);
pull(head, i, hostMac); pull(head, i, hostMac);
pull(head, i, sequenceId); pull(head, i, sequenceId);
pull(head, i, errorCode); pull(head, i, errorCode);
pull(head, i, checkLen); pull(head, i, checkLen);
pull(head, i, fragmentOffset); pull(head, i, fragmentOffset);
pull(head, i, flag); pull(head, i, flag);
pull(head, i, tokenId); pull(head, i, tokenId);
pull(head, i, checkSum); pull(head, i, checkSum);
if (this->getLength() != checkLen) { if (this->getLength() != checkLen) {
printf("Packet Length doesn't match: %lu != %hd\n", data.size(), printf("Packet Length doesn't match: %lu != %hd\n", data.size(),
checkLen); checkLen);
} }
i = 0; i = 0;
dataset d; dataset d;
payload = {}; payload = {};
while (i < (int) body.size() - 4) { while (i < (int) body.size() - 4) {
pull(body, i, d.type); pull(body, i, d.type);
pull(body, i, d.len); pull(body, i, d.len);
pull(body, i, d.value, d.len); pull(body, i, d.value, d.len);
payload.push_back(d); payload.push_back(d);
} }
} }
const bytes& Packet::getBody() const { const bytes& Packet::getBody() const {
return body; return body;
} }
const bytes& Packet::getHead() const { const bytes& Packet::getHead() const {
return head; return head;
} }
void Packet::setBody(bytes data) { void Packet::setBody(bytes data) {
this->body = data; this->body = data;
} }
void Packet::setHostMac(macAddr mac) { void Packet::setHostMac(macAddr mac) {
this->hostMac = mac; this->hostMac = mac;
} }
short Packet::getLength() const { short Packet::getLength() const {
return HEADER_LEN + body.size(); return HEADER_LEN + body.size();
} }
int Packet::getCheckSum() const { int Packet::getCheckSum() const {
return checkSum; return checkSum;
} }
void Packet::setCheckSum(int checkSum) { 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 {
return switchMac; return switchMac;
} }
void Packet::setSwitchMac(macAddr switchMac) { void Packet::setSwitchMac(macAddr switchMac) {
this->switchMac = switchMac; this->switchMac = switchMac;
} }
const datasets& Packet::getPayload() const { const datasets& Packet::getPayload() const {
return payload; return payload;
} }
void Packet::setPayload(datasets payload) { 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:
return "DISCOVERY"; return "DISCOVERY";
case GET: case GET:
return "GET"; return "GET";
case SET: case SET:
return "SET"; return "SET";
case CONFIRM: case CONFIRM:
return "CONFIRM"; return "CONFIRM";
case RETURN: case RETURN:
return "RETURN"; return "RETURN";
default: default:
return "NONE"; return "NONE";
} }
return "NONE"; return "NONE";
} }
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 = { 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 102, 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165,
159, 221, 244, 110, 119, 48 }; 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++) {
i = (k + 1) % 256; i = (k + 1) % 256;
j = (j + s[i]) % 256; j = (j + s[i]) % 256;
std::swap(s[i], s[j]); std::swap(s[i], s[j]);
data[k] = data[k] ^ s[(s[i] + s[j]) % 256]; data[k] = data[k] ^ s[(s[i] + s[j]) % 256];
} }
} }
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++;
} }
} }
void Packet::push(bytes &arr, int &index, bytes data) { void Packet::push(bytes &arr, int &index, bytes data) {
for (unsigned j = 0; j < data.size(); j++) for (unsigned j = 0; j < data.size(); j++)
push(arr, index, data[j]); push(arr, index, data[j]);
} }
void Packet::push(bytes &arr, int &index, ipAddr data) { void Packet::push(bytes &arr, int &index, ipAddr data) {
for (unsigned j = 0; j < 4; j++) for (unsigned j = 0; j < 4; j++)
push(arr, index, (byte) data[j]); push(arr, index, (byte) data[j]);
} }
void Packet::push(bytes &arr, int &index, macAddr data) { void Packet::push(bytes &arr, int &index, macAddr data) {
for (unsigned j = 0; j < 6; j++) for (unsigned j = 0; j < 6; j++)
push(arr, index, (byte) data[j]); push(arr, index, (byte) data[j]);
} }
void Packet::push(bytes &arr, int &index, short data) { void Packet::push(bytes &arr, int &index, short data) {
byte a = (data >> 8) & 0xFF; byte a = (data >> 8) & 0xFF;
push(arr, index, a); push(arr, index, a);
a = data & 0xFF; a = data & 0xFF;
push(arr, index, a); push(arr, index, a);
} }
void Packet::push(bytes &arr, int &index, int data) { void Packet::push(bytes &arr, int &index, int data) {
byte a = (data >> 24) & 0xFF; byte a = (data >> 24) & 0xFF;
push(arr, index, a); push(arr, index, a);
a = (data >> 16) & 0xFF; a = (data >> 16) & 0xFF;
push(arr, index, a); push(arr, index, a);
a = (data >> 8) & 0xFF; a = (data >> 8) & 0xFF;
push(arr, index, a); push(arr, index, a);
a = data & 0xFF; a = data & 0xFF;
push(arr, index, a); push(arr, index, a);
} }
void Packet::push(bytes &arr, int &index, dataset data) { void Packet::push(bytes &arr, int &index, dataset data) {
push(arr, index, data.type); push(arr, index, data.type);
push(arr, index, data.len); push(arr, index, data.len);
push(arr, index, data.value); push(arr, index, data.value);
} }
void Packet::pull(bytes &arr, int &index, byte &ret) { void Packet::pull(bytes &arr, int &index, byte &ret) {
ret = arr[index++]; ret = arr[index++];
} }
void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned len) { void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned len) {
ret.resize(len); ret.resize(len);
memcpy(&ret[0], &arr[index], len); memcpy(&ret[0], &arr[index], len);
index += len; index += len;
} }
void Packet::pull(bytes &arr, int &index, macAddr &ret) { void Packet::pull(bytes &arr, int &index, macAddr &ret) {
memcpy(&ret[0], &arr[index], 6); memcpy(&ret[0], &arr[index], 6);
index += 6; index += 6;
} }
void Packet::pull(bytes &arr, int &index, ipAddr &ret) { void Packet::pull(bytes &arr, int &index, ipAddr &ret) {
memcpy(&ret[0], &arr[index], 4); memcpy(&ret[0], &arr[index], 4);
index += 4; index += 4;
} }
void Packet::pull(bytes &arr, int &index, short &ret) { void Packet::pull(bytes &arr, int &index, short &ret) {
ret = (arr[index++] << 8); ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF; ret |= arr[index++] & 0xFF;
ret &= 0xFFFF; ret &= 0xFFFF;
} }
void Packet::pull(bytes &arr, int &index, int &ret) { void Packet::pull(bytes &arr, int &index, int &ret) {
ret = arr[index++] << 24; ret = arr[index++] << 24;
ret |= (arr[index++] & 0xFF) << 16; ret |= (arr[index++] & 0xFF) << 16;
ret |= (arr[index++] & 0xFF) << 8; ret |= (arr[index++] & 0xFF) << 8;
ret |= arr[index++] & 0xFF; ret |= arr[index++] & 0xFF;
} }
void Packet::pull(bytes &arr, int &index, dataset &ret) { void Packet::pull(bytes &arr, int &index, dataset &ret) {
pull(arr, index, ret.type); pull(arr, index, ret.type);
pull(arr, index, ret.len); pull(arr, index, ret.len);
pull(arr, index, ret.value, (unsigned) ret.len); pull(arr, index, ret.value, (unsigned) ret.len);
} }

View file

@ -13,66 +13,65 @@
#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);
}; void encode(bytes&);
Packet(OpCode); bytes getBytes();
void encode(bytes&); void parse(bytes);
bytes getBytes(); void printHeader();
void parse(bytes); std::string opCodeToString();
void printHeader(); short getLength() const;
std::string opCodeToString(); int getCheckSum() const;
short getLength() const; int getErrorCode() const;
int getCheckSum() const; short getSequenceId() const;
short getSequenceId() const; macAddr getSwitchMac() const;
macAddr getSwitchMac() const; const bytes& getBody() const;
const bytes& getBody() const; const bytes& getHead() const;
const bytes& getHead() const; const datasets& getPayload() const;
const datasets& getPayload() const; void setBody(bytes);
void setBody(bytes); void setHostMac(macAddr);
void setHostMac(macAddr); void setSwitchMac(macAddr);
void setSwitchMac(macAddr); 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 tokenId = 0;
short sequenceId; short fragmentOffset = 0;
short tokenId = 0; int errorCode = 0;
short fragmentOffset = 0; int checkSum = 0;
int errorCode = 0; short flag = 0;
int checkSum = 0; void buildHead();
short flag = 0; void buildBody();
void push(bytes&, int&, short );
void buildHead(); void push(bytes&, int&, int);
void buildBody(); void push(bytes&, int&, byte);
void push(bytes&, int&, bytes);
void push(bytes&, int&, short); void push(bytes&, int&, ipAddr);
void push(bytes&, int&, int); void push(bytes&, int&, macAddr);
void push(bytes&, int&, byte); void push(bytes&, int&, dataset);
void push(bytes&, int&, bytes); void pull(bytes&, int&, short &);
void push(bytes&, int&, ipAddr); void pull(bytes&, int&, int&);
void push(bytes&, int&, macAddr); void pull(bytes&, int&, byte&);
void push(bytes&, int&, dataset); void pull(bytes&, int&, bytes&, unsigned );
void pull(bytes&, int&, ipAddr&);
void pull(bytes&, int&, short&); void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, int&); void pull(bytes&, int&, dataset&);
void pull(bytes&, int&, byte&);
void pull(bytes&, int&, bytes&, unsigned);
void pull(bytes&, int&, ipAddr&);
void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, dataset&);
}; };
#endif /* PACKET_H_ */ #endif /* PACKET_H_ */

View file

@ -18,248 +18,255 @@
#include "table.h" #include "table.h"
int printHeader(Packet p) { int printHeader(Packet p) {
if (options.flags & FLAG_HEADER) { if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) { if (options.flags & FLAG_HEX) {
std::cout << "Received Header:\n\t" << p.getHead() << "\n"; std::cout << "Received Header:\n\t" << p.getHead() << "\n";
} else { } else {
p.printHeader(); p.printHeader();
printf("\n"); printf("\n");
} }
} }
return 0; return 0;
} }
int printPacket(Packet p) { int printPacket(Packet p) {
if (options.flags & FLAG_HEX) { if (options.flags & FLAG_HEX) {
std::cout << "Received Payload:\n\t" << p.getBody() << "\n"; std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
} else { } else {
for (dataset d : p.getPayload()) { for (dataset d : p.getPayload()) {
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 << "."
std::cout << "\n"; << (unsigned) d.value[i];
break; std::cout << "\n";
case table::ACTION: break;
std::cout << "Error:" << s->name case table::ACTION:
<< " is marked as 'action' but carries payload." std::cout << "Error:" << s.name
<< d.value << "\n"; << " is marked as 'action' but carries payload."
break; << d.value << "\n";
default: break;
std::cout << "+\t" << s->name << " = " << d.value default:
<< "\n"; std::cout << "+\t" << s.name << " = " << d.value
break; << "\n";
} break;
} else { //empty }
std::cout << std::dec << ">\t" << s->name << "\n"; } else { //empty
} std::cout << std::dec << ">\t" << s.name << "\n";
} else { //unknown id }
if (d.len > 0) { } else { //unknown id
std::cout << "##\t" << d.type << ":\n\t"; if (d.len > 0) {
std::cout << std::hex << d.value << std::dec << "\n"; std::cout << "##\t" << d.type << ":\n\t";
} else { //empty std::cout << std::hex << d.value << std::dec << "\n";
std::cout << "#>\t" << d.type << "\n"; } else { //empty
} std::cout << "#>\t" << d.type << "\n";
} }
} }
} }
return 0; }
return 0;
} }
int Program::list() { int Program::list() {
std::cout << "List:\n"; std::cout << "List:\n";
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac()); p.setHostMac(host.getMac());
p.setPayload( { }); p.setPayload( { });
bytes b = p.getBytes(); bytes b = p.getBytes();
p.encode(b); p.encode(b);
try { try {
sock->setHostIp(host.getIp()); sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT); sock->init(DST_PORT, SRC_PORT);
sock->callback = sock->callback =
[this](Packet a) { [this](Packet a) {
printHeader(a); printHeader(a);
if (options.flags & FLAG_HEX) { if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n"; std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else { } else {
datasets d =a.getPayload(); datasets d =a.getPayload();
Switch sw = Switch(); Switch sw = Switch();
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;
}; };
sock->send(b); sock->send(b);
io_service->run(); io_service->run();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
} }
return 1; return 1;
} }
int Program::sniff() { int Program::sniff() {
printf("Listening:\n"); printf("Listening:\n");
try { try {
boost::asio::io_service io_service; boost::asio::io_service io_service;
Socket s(io_service); Socket s(io_service);
s.setHostIp(host.getIp()); s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT); s.init(DST_PORT, SRC_PORT);
s.callback = [](Packet p) { s.callback = [](Packet p) {
std::cout << p.opCodeToString() << "\n"; std::cout << p.opCodeToString() << "\n";
printHeader(p); printHeader(p);
printPacket(p); printPacket(p);
return 0; return 0;
}; };
s.listen(); s.listen();
io_service.run(); io_service.run();
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
} }
return 1; return 1;
} }
int Program::encode(std::string s) { int Program::encode(std::string s) {
bytes d(s); bytes d(s);
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::DISCOVERY);
p.encode(d); p.encode(d);
std::cout << d << std::endl; std::cout << d << std::endl;
return 0; return 0;
} }
int Program::setProperty() { int Program::setProperty() {
return 0; return 0;
} }
int Program::getProperty() { int Program::getProperty() {
std::cout << "List:\n"; std::cout << "List:\n";
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac()); p.setHostMac(host.getMac());
p.setPayload( { }); p.setPayload( { });
bytes b = p.getBytes(); bytes b = p.getBytes();
p.encode(b); p.encode(b);
auto s = sock; auto s = sock;
try { try {
sock->setHostIp(host.getIp()); sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT); sock->init(DST_PORT, SRC_PORT);
sock->callback = sock->callback =
[this](Packet a) { [this](Packet a) {
auto s = sock; auto s = sock;
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 =
[this](Packet a) {
auto s = sock;
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
Packet p = Packet(Packet::SET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = {
{snd_lookup["login_user"], (short)(options.user.length()), options.user},
{snd_lookup["login_password"], (short)(options.password.length()), options.password}
};
std::cout<<options.user<<std::endl<<options.password<<std::endl;
p.setPayload(t);
bytes c = p.getBytes();
p.encode(c);
sock->callback = sock->callback =
[this](Packet a) { [this](Packet a) {
std::cout << a.opCodeToString() << "\n"; auto s = sock;
printHeader(a); datasets d =a.getPayload();
printPacket(a); Switch sw = Switch();
return 0; sw.parse(d);
}; Packet p = Packet(Packet::SET);
sock->send(c); p.setSwitchMac(a.getSwitchMac());
return 0; 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);
sock->send(c); sock->callback =
return 0; [this](Packet a) {
}; std::cout << a.opCodeToString() << "\n";
sock->send(b); printHeader(a);
io_service->run(); printPacket(a);
} catch (std::exception& e) { return 0;
std::cerr << "Exception: " << e.what() << "\n"; };
} sock->send(c);
return 1; return 0;
};
sock->send(c);
return 0;
};
sock->send(b);
io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
} }
int Program::save() { int Program::save() {
Switch sw = Switch(); Switch sw = Switch();
sw.settings.hostname = "testname.lan"; sw.settings.hostname = "testname.lan";
File f; File f;
f.write(sw.toString()); f.write(sw.toString());
return 1; return 1;
} }
int Program::restore() { int Program::restore() {
File f; File f;
Switch sw; Switch sw;
sw.parse(f.read()); sw.parse(f.read());
std::cout << "Devices:\n\t" << sw.settings.hostname << " (" std::cout << "Devices:\n\t" << sw.settings.hostname << " ("
<< sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: " << sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: "
<< sw.settings.ip_addr << "\n"; << sw.settings.ip_addr << "\n";
return 1; return 1;
} }
int Program::flash() { int Program::flash() {
return 0; return 0;
} }
int Program::reboot() { int Program::reboot() {
return 0; return 0;
} }
int Program::reset() { int Program::reset() {
return 0; return 0;
} }
int Program::ping(std::function<int(Packet)>){ int Program::ping(std::function<int(Packet)>) {
return 0; return 0;
} }
void Program::init() { void Program::init() {
if (options.interface.compare("") == 0) if (options.interface.compare("") == 0)
options.interface = host.getIface(); options.interface = host.getIface();
} }

View file

@ -14,88 +14,89 @@
#include "Types.h" #include "Types.h"
Socket::Socket(boost::asio::io_service& io_service) : Socket::Socket(boost::asio::io_service& io_service) :
send_socket_(io_service), receive_socket_(io_service), timer(io_service) { send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
} }
//, resolver( io_service) //, resolver( io_service)
void Socket::init(short dst_port, short src_port) { void Socket::init(short dst_port, short src_port) {
if (initialized) if (initialized)
return; return;
if (options.flags & FLAG_REVERSE) { if (options.flags & FLAG_REVERSE) {
short p = dst_port; short p = dst_port;
dst_port = src_port; dst_port = src_port;
src_port = p; src_port = p;
} }
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(
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
local_endpoint_ = boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4(local_ip), src_port);
broadcast_endpoint_ = boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4::from_string("255.255.255.255"),
dst_port);
wildcard_endpoint_ = boost::asio::ip::udp::endpoint( send_socket_.open(boost::asio::ip::udp::v4());
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port); send_socket_.set_option(boost::asio::socket_base::broadcast(true));
local_endpoint_ = boost::asio::ip::udp::endpoint( send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
boost::asio::ip::address_v4(local_ip), src_port); send_socket_.bind(local_endpoint_);
broadcast_endpoint_ = boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4::from_string("255.255.255.255"),
dst_port);
send_socket_.open(boost::asio::ip::udp::v4()); receive_socket_.open(boost::asio::ip::udp::v4());
send_socket_.set_option(boost::asio::socket_base::broadcast(true)); receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
send_socket_.set_option(boost::asio::socket_base::reuse_address(true)); receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_); receive_socket_.bind(wildcard_endpoint_);
receive_socket_.open(boost::asio::ip::udp::v4()); if (options.timeout != 0) {
receive_socket_.set_option(boost::asio::socket_base::broadcast(true)); timer.expires_from_now(
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true)); boost::posix_time::milliseconds(options.timeout));
receive_socket_.bind(wildcard_endpoint_); timer.async_wait([this](const boost::system::error_code& error)
{
if (!error)
{
receive_socket_.close();
}
});
}
if (options.timeout != 0) { initialized = 1;
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) { void Socket::setHostIp(ipAddr ip) {
local_ip = ip; local_ip = ip;
} }
void Socket::send(bytes data) { void Socket::send(bytes data) {
unsigned char * a = &data[0]; unsigned char * a = &data[0];
send_socket_.async_send_to(boost::asio::buffer(a, data.size()), send_socket_.async_send_to(boost::asio::buffer(a, data.size()),
broadcast_endpoint_, broadcast_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_sent) [this](boost::system::error_code ec, std::size_t bytes_sent)
{ {
listen(); listen();
}); });
} }
void Socket::listen() { void Socket::listen() {
data.resize(MAX_LENGTH); data.resize(MAX_LENGTH);
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH), receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
remote_endpoint_, remote_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_recvd) [this](boost::system::error_code ec, std::size_t bytes_recvd)
{ {
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::RETURN); Packet p = Packet(Packet::NONE);
p.encode(data); p.encode(data);
p.parse(data); // std::cout << "err" << p.getErrorCode() <<std::endl;
if(!callback(p)) { p.parse(data);
//TODO do something //std::cout << "err" << p.getErrorCode() <<std::endl;
} if(!callback(p)) {
listen(); //TODO do something
} }
}); listen();
}
});
} }

View file

@ -13,54 +13,54 @@
#include "table.h" #include "table.h"
int Switch::parse(datasets arr) { int Switch::parse(datasets arr) {
for (dataset a : arr) { for (dataset a : arr) {
parse(a); parse(a);
} }
return 0; return 0;
} }
int Switch::parse(dataset d) { int Switch::parse(dataset d) {
switch (d.type) { switch (d.type) {
case RCV_TYPE: case RCV_TYPE:
device.type = d.value; device.type = d.value;
break; break;
case RCV_MAC: case RCV_MAC:
device.mac = d.value; device.mac = d.value;
break; break;
case FIRMWARE_VERSION: case FIRMWARE_VERSION:
device.firmware_version = d.value; device.firmware_version = d.value;
break; break;
case HARDWARE_VERSION: case HARDWARE_VERSION:
device.hardware_version = d.value; device.hardware_version = d.value;
break; break;
case PORTS: case PORTS:
device.ports = d.value[0]; device.ports = d.value[0];
break; break;
case HOSTNAME: case HOSTNAME:
settings.hostname = d.value; settings.hostname = d.value;
break; break;
case IP_ADDR: case IP_ADDR:
settings.ip_addr = d.value; settings.ip_addr = d.value;
break; break;
case IP_MASK: case IP_MASK:
settings.ip_mask = d.value; settings.ip_mask = d.value;
break; break;
case GATEWAY: case GATEWAY:
settings.gateway = d.value; settings.gateway = d.value;
break; break;
case DHCP_ENABLED: case DHCP_ENABLED:
settings.dhcp = d.value[0]; settings.dhcp = d.value[0];
break; break;
case LOOP_PREVENTION: case LOOP_PREVENTION:
settings.loop_prevention = d.value[0]; settings.loop_prevention = d.value[0];
break; break;
case QOS_BASIC_ENABLED: case QOS_BASIC_ENABLED:
settings.qos_enabled = d.value[0]; settings.qos_enabled = d.value[0];
break; break;
case VLAN_ENABLED: case VLAN_ENABLED:
settings.vlan_enabled = d.value[0]; settings.vlan_enabled = d.value[0];
break; break;
} }
return 0; return 0;
} }

View file

@ -1,25 +1,25 @@
LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports
LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //??? LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //???
LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port
LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask?? 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(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_ENABLED, 8448, BOOL) //open page
LOOKUP_SET(PORT_VLAN, 8449, HEX) LOOKUP_SET(PORT_VLAN, 8449, HEX)
LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC) LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC)
LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte
LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan
LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port
LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //???? LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //????
LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod
LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ??? LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ??? LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ??? LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ???
LOOKUP_SET(STORM_CONTROL, 12800, 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_MIRROR, 16640, HEX) //byte[10] second byte port id???
LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ??? LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ???
LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ??? LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ???
LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte

View file

@ -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];
} }

View file

@ -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_ */