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

@ -12,260 +12,273 @@
#include "Types.h"
Packet::Packet(OpCode c) {
srand(time(NULL));
sequenceId = rand() % 1000;
opCode = c;
srand(time(NULL));
sequenceId = sequenceId != 0 ? sequenceId + 1 : rand() % 1000;
opCode = c;
}
void Packet::printHeader() {
std::cout << "Header:\n";
std::cout << "\tID:\t\t" << sequenceId << "\n";
std::cout << "\tVersion:\t" << (int) version << "\n";
std::cout << "\tError:\t\t" << errorCode << "\n";
std::cout << "\tSwitch MAC:\t" << switchMac << "\n";
std::cout << "\tHost MAC: \t" << hostMac << "\n";
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 << "\tChecksum: \t" << std::dec << checkSum << "\n";
std::cout << "Header:\n";
std::cout << "\tID:\t\t" << sequenceId << "\n";
std::cout << "\tVersion:\t" << (int) version << "\n";
std::cout << "\tError:\t\t" << errorCode << "\n";
std::cout << "\tSwitch MAC:\t" << switchMac << "\n";
std::cout << "\tHost MAC: \t" << hostMac << "\n";
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";
}
bytes Packet::getBytes() {
int i = 0;
for (auto d : payload)
push(body, i, d);
push(body, i, (int) PACKET_END);
i = 0;
push(head, i, version);
push(head, i, opCode);
push(head, i, switchMac);
push(head, i, hostMac);
push(head, i, sequenceId);
push(head, i, errorCode);
push(head, i, this->getLength());
push(head, i, fragmentOffset);
push(head, i, flag);
push(head, i, tokenId);
push(head, i, checkSum);
bytes data = head + body;
return data;
int i = 0;
for (auto d : payload)
push(body, i, d);
push(body, i, (int) PACKET_END);
i = 0;
push(head, i, version);
push(head, i, opCode);
push(head, i, switchMac);
push(head, i, hostMac);
push(head, i, sequenceId);
push(head, i, errorCode);
push(head, i, this->getLength());
push(head, i, fragmentOffset);
push(head, i, flag);
push(head, i, tokenId);
push(head, i, checkSum);
bytes data = head + body;
return data;
}
void Packet::parse(bytes data) {
memcpy(&head[0], &data[0], HEADER_LEN);
body.resize(data.size() - HEADER_LEN);
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
int i = 0;
short checkLen = 0x0;
pull(head, i, version);
pull(head, i, opCode);
pull(head, i, switchMac);
pull(head, i, hostMac);
pull(head, i, sequenceId);
pull(head, i, errorCode);
pull(head, i, checkLen);
pull(head, i, fragmentOffset);
pull(head, i, flag);
pull(head, i, tokenId);
pull(head, i, checkSum);
if (this->getLength() != checkLen) {
printf("Packet Length doesn't match: %lu != %hd\n", data.size(),
checkLen);
}
i = 0;
dataset d;
payload = {};
while (i < (int) body.size() - 4) {
pull(body, i, d.type);
pull(body, i, d.len);
pull(body, i, d.value, d.len);
payload.push_back(d);
}
memcpy(&head[0], &data[0], HEADER_LEN);
body.resize(data.size() - HEADER_LEN);
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
int i = 0;
short checkLen = 0x0;
pull(head, i, version);
pull(head, i, opCode);
pull(head, i, switchMac);
pull(head, i, hostMac);
pull(head, i, sequenceId);
pull(head, i, errorCode);
pull(head, i, checkLen);
pull(head, i, fragmentOffset);
pull(head, i, flag);
pull(head, i, tokenId);
pull(head, i, checkSum);
if (this->getLength() != checkLen) {
printf("Packet Length doesn't match: %lu != %hd\n", data.size(),
checkLen);
}
i = 0;
dataset d;
payload = {};
while (i < (int) body.size() - 4) {
pull(body, i, d.type);
pull(body, i, d.len);
pull(body, i, d.value, d.len);
payload.push_back(d);
}
}
const bytes& Packet::getBody() const {
return body;
return body;
}
const bytes& Packet::getHead() const {
return head;
return head;
}
void Packet::setBody(bytes data) {
this->body = data;
this->body = data;
}
void Packet::setHostMac(macAddr mac) {
this->hostMac = mac;
this->hostMac = mac;
}
short Packet::getLength() const {
return HEADER_LEN + body.size();
return HEADER_LEN + body.size();
}
int Packet::getCheckSum() const {
return checkSum;
return checkSum;
}
void Packet::setCheckSum(int checkSum) {
this->checkSum = checkSum;
this->checkSum = checkSum;
}
int Packet::getErrorCode() const {
return errorCode;
}
short Packet::getSequenceId() const {
return sequenceId;
return sequenceId;
}
void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId;
void Packet::setSequenceId(short sId) {
sequenceId = sId;
}
macAddr Packet::getSwitchMac() const {
return switchMac;
return switchMac;
}
void Packet::setSwitchMac(macAddr switchMac) {
this->switchMac = switchMac;
this->switchMac = switchMac;
}
const datasets& Packet::getPayload() const {
return payload;
return 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() {
switch (opCode) {
case DISCOVERY:
return "DISCOVERY";
case GET:
return "GET";
case SET:
return "SET";
case CONFIRM:
return "CONFIRM";
case RETURN:
return "RETURN";
default:
return "NONE";
}
return "NONE";
switch (opCode) {
case DISCOVERY:
return "DISCOVERY";
case GET:
return "GET";
case SET:
return "SET";
case CONFIRM:
return "CONFIRM";
case RETURN:
return "RETURN";
default:
return "NONE";
}
return "NONE";
}
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 s = key;
int i, j = 0;
for (int k = 0; k < len; k++) {
i = (k + 1) % 256;
j = (j + s[i]) % 256;
std::swap(s[i], s[j]);
data[k] = data[k] ^ s[(s[i] + s[j]) % 256];
}
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 s = key;
int i, j = 0;
for (int k = 0; k < len; k++) {
i = (k + 1) % 256;
j = (j + s[i]) % 256;
std::swap(s[i], s[j]);
data[k] = data[k] ^ s[(s[i] + s[j]) % 256];
}
}
void Packet::push(bytes &arr, int &index, byte data) {
if (arr.size() > (unsigned) index) {
arr[index++] = data;
} else {
arr.push_back(data);
index++;
}
if (arr.size() > (unsigned) index) {
arr[index++] = data;
} else {
arr.push_back(data);
index++;
}
}
void Packet::push(bytes &arr, int &index, bytes data) {
for (unsigned j = 0; j < data.size(); j++)
push(arr, index, data[j]);
for (unsigned j = 0; j < data.size(); j++)
push(arr, index, data[j]);
}
void Packet::push(bytes &arr, int &index, ipAddr data) {
for (unsigned j = 0; j < 4; j++)
push(arr, index, (byte) data[j]);
for (unsigned j = 0; j < 4; j++)
push(arr, index, (byte) data[j]);
}
void Packet::push(bytes &arr, int &index, macAddr data) {
for (unsigned j = 0; j < 6; j++)
push(arr, index, (byte) data[j]);
for (unsigned j = 0; j < 6; j++)
push(arr, index, (byte) data[j]);
}
void Packet::push(bytes &arr, int &index, short data) {
byte a = (data >> 8) & 0xFF;
push(arr, index, a);
a = data & 0xFF;
push(arr, index, a);
byte a = (data >> 8) & 0xFF;
push(arr, index, a);
a = data & 0xFF;
push(arr, index, a);
}
void Packet::push(bytes &arr, int &index, int data) {
byte a = (data >> 24) & 0xFF;
push(arr, index, a);
a = (data >> 16) & 0xFF;
push(arr, index, a);
a = (data >> 8) & 0xFF;
push(arr, index, a);
a = data & 0xFF;
push(arr, index, a);
byte a = (data >> 24) & 0xFF;
push(arr, index, a);
a = (data >> 16) & 0xFF;
push(arr, index, a);
a = (data >> 8) & 0xFF;
push(arr, index, a);
a = data & 0xFF;
push(arr, index, a);
}
void Packet::push(bytes &arr, int &index, dataset data) {
push(arr, index, data.type);
push(arr, index, data.len);
push(arr, index, data.value);
push(arr, index, data.type);
push(arr, index, data.len);
push(arr, index, data.value);
}
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) {
ret.resize(len);
memcpy(&ret[0], &arr[index], len);
index += len;
ret.resize(len);
memcpy(&ret[0], &arr[index], len);
index += len;
}
void Packet::pull(bytes &arr, int &index, macAddr &ret) {
memcpy(&ret[0], &arr[index], 6);
index += 6;
memcpy(&ret[0], &arr[index], 6);
index += 6;
}
void Packet::pull(bytes &arr, int &index, ipAddr &ret) {
memcpy(&ret[0], &arr[index], 4);
index += 4;
memcpy(&ret[0], &arr[index], 4);
index += 4;
}
void Packet::pull(bytes &arr, int &index, short &ret) {
ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF;
ret &= 0xFFFF;
ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF;
ret &= 0xFFFF;
}
void Packet::pull(bytes &arr, int &index, int &ret) {
ret = arr[index++] << 24;
ret |= (arr[index++] & 0xFF) << 16;
ret |= (arr[index++] & 0xFF) << 8;
ret |= arr[index++] & 0xFF;
ret = arr[index++] << 24;
ret |= (arr[index++] & 0xFF) << 16;
ret |= (arr[index++] & 0xFF) << 8;
ret |= arr[index++] & 0xFF;
}
void Packet::pull(bytes &arr, int &index, dataset &ret) {
pull(arr, index, ret.type);
pull(arr, index, ret.len);
pull(arr, index, ret.value, (unsigned) ret.len);
pull(arr, index, ret.type);
pull(arr, index, ret.len);
pull(arr, index, ret.value, (unsigned) ret.len);
}

View file

@ -13,66 +13,65 @@
#include "Types.h"
class Packet {
static short sequenceId=0;
class Packet
{
public:
enum OpCode {
DISCOVERY, GET, RETURN, SET, CONFIRM
};
Packet(OpCode);
void encode(bytes&);
bytes getBytes();
void parse(bytes);
void printHeader();
std::string opCodeToString();
short getLength() const;
int getCheckSum() const;
short getSequenceId() const;
macAddr getSwitchMac() const;
const bytes& getBody() const;
const bytes& getHead() const;
const datasets& getPayload() const;
void setBody(bytes);
void setHostMac(macAddr);
void setSwitchMac(macAddr);
void setCheckSum(int);
void setSequenceId(short);
void setPayload(datasets payload);
enum OpCode {DISCOVERY, GET, RETURN, SET, CONFIRM, NONE};
Packet(OpCode);
void encode(bytes&);
bytes getBytes();
void parse(bytes);
void printHeader();
std::string opCodeToString();
short getLength() const;
int getCheckSum() const;
int getErrorCode() const;
short getSequenceId() const;
macAddr getSwitchMac() const;
const bytes& getBody() const;
const bytes& getHead() const;
const datasets& getPayload() const;
void setBody(bytes);
void setHostMac(macAddr);
void setSwitchMac(macAddr);
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);
void push(bytes&, int&, bytes);
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&);
void pull(bytes&, int&, bytes&, unsigned);
void pull(bytes&, int&, ipAddr&);
void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, dataset&);
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 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);
void push(bytes&, int&, bytes);
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&);
void pull(bytes&, int&, bytes&, unsigned );
void pull(bytes&, int&, ipAddr&);
void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, dataset&);
};
#endif /* PACKET_H_ */

View file

@ -18,248 +18,255 @@
#include "table.h"
int printHeader(Packet p) {
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
std::cout << "Received Header:\n\t" << p.getHead() << "\n";
} else {
p.printHeader();
printf("\n");
}
}
return 0;
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
std::cout << "Received Header:\n\t" << p.getHead() << "\n";
} else {
p.printHeader();
printf("\n");
}
}
return 0;
}
int printPacket(Packet p) {
if (options.flags & FLAG_HEX) {
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
} else {
for (dataset d : p.getPayload()) {
auto lookup =
(options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
if (lookup.exists(d.type)) {
const table::set *s = lookup.get(d.type);
if (d.len > 0) {
switch (s->format) {
case table::STRING:
std::cout << "+\t" << s->name << " = " << &d.value[0]
<< "\n";
break;
case table::BOOL:
std::cout << "+\t" << s->name << " = " << (d.value[0]?"YES":"NO")
<< "\n";
break;
case table::HEX:
std::cout << "+\t" << s->name << " = " << d.value
<< "\n";
break;
case table::DEC:
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 << "\n";
break;
case table::ACTION:
std::cout << "Error:" << s->name
<< " is marked as 'action' but carries payload."
<< d.value << "\n";
break;
default:
std::cout << "+\t" << s->name << " = " << d.value
<< "\n";
break;
}
} else { //empty
std::cout << std::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";
}
}
}
}
return 0;
if (options.flags & FLAG_HEX) {
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
} else {
for (dataset d : p.getPayload()) {
auto lookup =
(options.flags & FLAG_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]
<< "\n";
break;
case table::BOOL:
std::cout << "+\t" << s.name << " = "
<< (d.value[0] ? "YES" : "NO") << "\n";
break;
case table::HEX:
std::cout << "+\t" << s.name << " = " << d.value
<< "\n";
break;
case table::DEC:
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 << "\n";
break;
case table::ACTION:
std::cout << "Error:" << s.name
<< " is marked as 'action' but carries payload."
<< d.value << "\n";
break;
default:
std::cout << "+\t" << s.name << " = " << d.value
<< "\n";
break;
}
} else { //empty
std::cout << std::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";
}
}
}
}
return 0;
}
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);
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) {
printHeader(a);
if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else {
datasets d =a.getPayload();
Switch sw = Switch();
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";
}
return 1;
};
sock->send(b);
io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
try {
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
sock->callback =
[this](Packet a) {
printHeader(a);
if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else {
datasets d =a.getPayload();
Switch sw = Switch();
sw.parse(d);
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";
}
return 1;
};
sock->send(b);
io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
}
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";
printHeader(p);
printPacket(p);
return 0;
};
s.listen();
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
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";
printHeader(p);
printPacket(p);
return 0;
};
s.listen();
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 1;
}
int Program::encode(std::string s) {
bytes d(s);
Packet p = Packet(Packet::DISCOVERY);
p.encode(d);
std::cout << d << std::endl;
return 0;
bytes d(s);
Packet p = Packet(Packet::DISCOVERY);
p.encode(d);
std::cout << d << std::endl;
return 0;
}
int Program::setProperty() {
return 0;
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;
try {
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
sock->callback =
[this](Packet a) {
auto s = sock;
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 << "List:\n";
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(host.getMac());
p.setPayload( { });
bytes b = p.getBytes();
p.encode(b);
auto s = sock;
try {
sock->setHostIp(host.getIp());
sock->init(DST_PORT, SRC_PORT);
sock->callback =
[this](Packet a) {
auto s = sock;
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";
Packet p = Packet(Packet::GET);
p.setSwitchMac(a.getSwitchMac());
p.setHostMac(host.getMac());
datasets t = { {snd_lookup["ping"], 0, {}}};
p.setPayload(t);
bytes c = p.getBytes();
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);
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) {
std::cout << a.opCodeToString() << "\n";
printHeader(a);
printPacket(a);
return 0;
};
sock->send(c);
return 0;
};
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.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);
return 0;
};
sock->send(b);
io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
sock->callback =
[this](Packet a) {
std::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";
}
return 1;
}
int Program::save() {
Switch sw = Switch();
sw.settings.hostname = "testname.lan";
File f;
f.write(sw.toString());
return 1;
Switch sw = Switch();
sw.settings.hostname = "testname.lan";
File f;
f.write(sw.toString());
return 1;
}
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;
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;
}
int Program::flash() {
return 0;
return 0;
}
int Program::reboot() {
return 0;
return 0;
}
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() {
if (options.interface.compare("") == 0)
options.interface = host.getIface();
if (options.interface.compare("") == 0)
options.interface = host.getIface();
}

View file

@ -14,88 +14,89 @@
#include "Types.h"
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)
void Socket::init(short dst_port, short src_port) {
if (initialized)
return;
if (initialized)
return;
if (options.flags & FLAG_REVERSE) {
short p = dst_port;
dst_port = src_port;
src_port = p;
}
if (options.flags & FLAG_REVERSE) {
short p = dst_port;
dst_port = src_port;
src_port = p;
}
if (options.flags & FLAG_DEBUG)
std::cout << "Local IP:\t" << local_ip << "\n";
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(
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(
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);
send_socket_.open(boost::asio::ip::udp::v4());
send_socket_.set_option(boost::asio::socket_base::broadcast(true));
send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_);
send_socket_.open(boost::asio::ip::udp::v4());
send_socket_.set_option(boost::asio::socket_base::broadcast(true));
send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_);
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_);
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_);
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();
}
});
}
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;
initialized = 1;
}
void Socket::setHostIp(ipAddr ip) {
local_ip = ip;
local_ip = ip;
}
void Socket::send(bytes 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();
});
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();
});
}
void Socket::listen() {
data.resize(MAX_LENGTH);
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
remote_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_recvd)
{
if (ec || bytes_recvd == 0) {
//listen();
// TODO distinguish error codes
} else {
data.resize(bytes_recvd);
Packet p = Packet(Packet::RETURN);
p.encode(data);
p.parse(data);
if(!callback(p)) {
//TODO do something
}
listen();
}
});
data.resize(MAX_LENGTH);
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
remote_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_recvd)
{
if (ec || bytes_recvd == 0) {
//listen();
// TODO distinguish error codes
} 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
}
listen();
}
});
}

View file

@ -13,54 +13,54 @@
#include "table.h"
int Switch::parse(datasets arr) {
for (dataset a : arr) {
parse(a);
}
return 0;
for (dataset a : arr) {
parse(a);
}
return 0;
}
int Switch::parse(dataset d) {
switch (d.type) {
case RCV_TYPE:
device.type = d.value;
break;
case RCV_MAC:
device.mac = d.value;
break;
case FIRMWARE_VERSION:
device.firmware_version = d.value;
break;
case HARDWARE_VERSION:
device.hardware_version = d.value;
break;
case PORTS:
device.ports = d.value[0];
break;
case HOSTNAME:
settings.hostname = d.value;
break;
case IP_ADDR:
settings.ip_addr = d.value;
break;
case IP_MASK:
settings.ip_mask = d.value;
break;
case GATEWAY:
settings.gateway = d.value;
break;
case DHCP_ENABLED:
settings.dhcp = d.value[0];
break;
case LOOP_PREVENTION:
settings.loop_prevention = d.value[0];
break;
case QOS_BASIC_ENABLED:
settings.qos_enabled = d.value[0];
break;
case VLAN_ENABLED:
settings.vlan_enabled = d.value[0];
break;
}
return 0;
switch (d.type) {
case RCV_TYPE:
device.type = d.value;
break;
case RCV_MAC:
device.mac = d.value;
break;
case FIRMWARE_VERSION:
device.firmware_version = d.value;
break;
case HARDWARE_VERSION:
device.hardware_version = d.value;
break;
case PORTS:
device.ports = d.value[0];
break;
case HOSTNAME:
settings.hostname = d.value;
break;
case IP_ADDR:
settings.ip_addr = d.value;
break;
case IP_MASK:
settings.ip_mask = d.value;
break;
case GATEWAY:
settings.gateway = d.value;
break;
case DHCP_ENABLED:
settings.dhcp = d.value[0];
break;
case LOOP_PREVENTION:
settings.loop_prevention = d.value[0];
break;
case QOS_BASIC_ENABLED:
settings.qos_enabled = d.value[0];
break;
case VLAN_ENABLED:
settings.vlan_enabled = d.value[0];
break;
}
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(PORTS_SETTINGS, 4096, HEX) //+per port
LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask??
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(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(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(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

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