This commit is contained in:
/jdi/ 2015-09-27 12:23:12 +02:00
parent 364bb25ab7
commit b76fb38bc2
16 changed files with 363 additions and 196 deletions

257
src/Packet.cpp Normal file
View file

@ -0,0 +1,257 @@
/*
* Packet.cpp
*
* Created on: 03.09.2015
* Author: jdi
*/
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include "Packet.h"
#include "Types.h"
#include "Utils.h"
Packet::Packet(OpCode c) {
srand(time(NULL));
sequenceId = rand() % 1000;
opCode = c;
}
void Packet::printHeader() {
printf("Header:\n\tOpCode:\t\t%s\n\tID:\t\t%d\n\tVersion:\t%hhd\n\tError:\t\t%.8X\n\tSwitch MAC:\t", opCodeToString().c_str(),sequenceId, version, errorCode);
utils::printHex(switchMac);
printf("\n\tHost MAC:\t");
utils::printHex(hostMac);
printf("\n\tLength:\t%hd",this->getLength());
printf("\n\tOffset:\t%hd",fragmentOffset);
printf("\n\tFlags:\t%.4hX",flag);
printf("\n\tChecksum:\t%d",checkSum);
}
bytes Packet::getBytes() {
int i = 0;
for (unsigned j = 0; j < payload.size(); j++)
push(body, i, payload[j]);
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;
//printf("Send Header:\t");
//utils::printHex(head);
//printf("\n");
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);
//printf("Receive Header:\t");
//utils::printHex(head);
//printf("\n");
int i = 0;
short checkLen = 0x0;
pull(head, i, version);
pull(head, i, opCode);
pull(head, i, switchMac, switchMac.size());
pull(head, i, hostMac, hostMac.size());
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: %hd != %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[d.type] = d;
}
}
const bytes& Packet::getBody() const {
return body;
}
const bytes& Packet::getHead() const {
return head;
}
void Packet::setBody(bytes data) {
this->body = data;
}
void Packet::setHostMac(bytes mac) {
this->hostMac = mac;
}
short Packet::getLength() const {
return HEADER_LEN + body.size();
}
int Packet::getCheckSum() const {
return checkSum;
}
void Packet::setCheckSum(int checkSum) {
this->checkSum = checkSum;
}
short Packet::getSequenceId() const {
return sequenceId;
}
void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId;
}
const bytes& Packet::getSwitchMac() const {
return switchMac;
}
void Packet::setSwitchMac(bytes switchMac) {
this->switchMac = switchMac;
}
const datasets& Packet::getPayload() const {
return payload;
}
void Packet::setPayload(const datasets& payload) {
this->payload = payload;
}
std::string Packet::opCodeToString() {
switch (opCode) {
case DISCOVERY:
return "DISCOVERY";
case GET:
return "GET";
case SET:
return "SET";
case READ:
return "READ";
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, t, j = 0;
for (int k = 0; k < len; k++) {
i = (k + 1) % 256;
j = (j + s[i]) % 256;
t = s[i];
s[i] = s[j];
s[j] = t;
data[k] = ((data[k] ^ s[(s[i] + s[j]) % 256]));
}
}
void Packet::push(bytes &arr, int &index, byte data) {
if (arr.size() > 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]);
}
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);
}
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);
}
void Packet::push(bytes &arr, int &index, dataset data) {
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++];
}
void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned len) {
ret.resize(len);
memcpy(&ret[0], &arr[index], len);
index += len;
}
void Packet::pull(bytes &arr, int &index, short &ret) {
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;
}
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);
}