minor improvements

This commit is contained in:
/jdi/ 2015-10-06 21:14:01 +02:00
parent 30ebdde28e
commit e077b4ca8a
14 changed files with 172 additions and 220 deletions

View file

@ -11,7 +11,8 @@
#include <ctime>
#include "Packet.h"
#include "Types.h"
#include "Utils.h"
#include "Types/bytes.h"
#include "Types/datasets.h"
Packet::Packet(OpCode c) {
srand(time(NULL));
@ -23,9 +24,8 @@ 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::print(switchMac);
printf("\n\tHost MAC:\t");
utils::print(hostMac);
std::cout << switchMac << "\n";
std::cout << "\tHost MAC:\t"<< hostMac << "\n";
printf("\n\tLength:\t%hd", this->getLength());
printf("\n\tOffset:\t%hd", fragmentOffset);
printf("\n\tFlags:\t%.4hX", flag);
@ -161,32 +161,31 @@ std::string Packet::opCodeToString() {
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 };
std::vector<unsigned char> t = { 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 key = t;
bytes s = key;
int i, t, j = 0;
int i, 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;
std::swap(s[i], s[j]);
data[k] = data[k] ^ s[(s[i] + s[j]) % 256];
}
}
@ -205,14 +204,14 @@ void Packet::push(bytes &arr, int &index, bytes data) {
push(arr, index, data[j]);
}
void Packet::push(bytes &arr, int &index, inetAddr &data) {
void Packet::push(bytes &arr, int &index, ipAddr data) {
for (unsigned j = 0; j < 4; j++)
push(arr, index, 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++)
push(arr, index, data[j]);
push(arr, index, (byte) data[j]);
}
void Packet::push(bytes &arr, int &index, short data) {
@ -254,7 +253,7 @@ void Packet::pull(bytes &arr, int &index, macAddr &ret) {
index += 6;
}
void Packet::pull(bytes &arr, int &index, inetAddr &ret) {
void Packet::pull(bytes &arr, int &index, ipAddr &ret) {
memcpy(&ret[0], &arr[index], 4);
index += 4;
}