merging dev
This commit is contained in:
commit
f2d85a67a5
15 changed files with 825 additions and 486 deletions
2
Makefile
2
Makefile
|
@ -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
|
||||
|
|
66
src/Constant.h
Normal file
66
src/Constant.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Options.h
|
||||
*
|
||||
* Created on: 11.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#ifndef OPTIONS_H_
|
||||
#define OPTIONS_H_
|
||||
|
||||
#include "Types.h"
|
||||
|
||||
#define VERSION "smrtlink (v1.1 Linux)\n"
|
||||
#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\
|
||||
[-p password] <command>\n\n"
|
||||
#define HELP "\
|
||||
Option Summary:\n\
|
||||
-h --help This help text\n\
|
||||
-v --version Display version of this tool\n\
|
||||
-d --debug [n] Show debugging messages\n\
|
||||
-r switch ports to emulate switch while sniffing\n\
|
||||
-b --header Show header\n\
|
||||
-x --hex Display Packets as Hex String\n\
|
||||
-j --json Display Packets as JSON\n\
|
||||
-i --interface <iface> only use one Interface\n\
|
||||
-u --user <[password:]username>\n\
|
||||
-p --password <password>\n\
|
||||
Login with Username and Password\n\
|
||||
-f --file <path> choose a settings file\n\n\
|
||||
-t --timeout <n> Timeout in milliseconds. Default: 180\n\
|
||||
-I --interactive Not yet implemented\n\
|
||||
-O --stdout Not yet implemented\n\
|
||||
-w --wait Not yet implemented: blocking until operation is completed\n\
|
||||
-s --permanent Not yet implemented: make changes immediately permanent\n\n\
|
||||
Command Summary:\n\
|
||||
help This help text\n\
|
||||
list list all connected switches\n\
|
||||
sniff [type:<type>] [<filter>]\n\
|
||||
capture and display all incoming or outgoing packets\n\
|
||||
depending on the --reverse option\n\
|
||||
encode use encoding algorithm on hex data separated by colon\n\
|
||||
get Not yet implemented\n\
|
||||
set Not yet implemented\n\
|
||||
get|save Not yet implemented: save config to file\n\
|
||||
set|restore Not yet implemented: restore onfig from file\n\
|
||||
flash Not yet implemented: replace firmware\n\
|
||||
reboot Not yet implemented\n\
|
||||
reset Not yet implemented\n\n\
|
||||
### for questions please contact <smrtlink@jdi.li> ###\n\n"
|
||||
//TODO
|
||||
/*
|
||||
* Stdin
|
||||
* socketmode
|
||||
*
|
||||
*/
|
||||
|
||||
#define FLAG_HEX 1
|
||||
#define FLAG_REVERSE 2
|
||||
#define FLAG_HEADER 4
|
||||
#define FLAG_PERMANENT 8
|
||||
#define FLAG_WAIT 16
|
||||
#define FLAG_DEBUG 32
|
||||
|
||||
extern Options options;
|
||||
|
||||
#endif /* OPTIONS_H_ */
|
91
src/Lookup.h
91
src/Lookup.h
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Switch.cpp
|
||||
*
|
||||
* Created on: 29.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#include "table.h"
|
||||
|
||||
static table rcv_lookup = { //
|
||||
{ 1, table::STRING, "type" }, //+string
|
||||
{ 2, table::STRING, "hostname" }, //+string
|
||||
{ 3, table::HEX, "mac" }, //+byte[6]
|
||||
{ 4, table::DEC, "ip_addr" }, //+byte[4]
|
||||
{ 5, table::DEC, "ip_mask" }, //+byte[4]
|
||||
{ 6, table::DEC, "gateway" }, //+byte[4]
|
||||
{ 7, table::STRING, "firmware_version" },//+
|
||||
{ 8, table::STRING, "hardware_version" },//+
|
||||
{ 9, table::BOOL, "dhcp_enabled" }, //+bool byte
|
||||
{ 10, table::DEC, "ports" }, //+byte, maybe number of ports
|
||||
|
||||
{ 2101, table::HEX, "port_vlan ????????" },
|
||||
|
||||
{ 4352, table::HEX, "igmp_snooping" }, //???
|
||||
{ 4096, table::HEX, "port_settings" }, //+per port
|
||||
{ 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask??
|
||||
|
||||
{ 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool,second byte port id
|
||||
{ 8448, table::BOOL, "port_vlan_enabled" }, //open page
|
||||
{ 8449, table::HEX, "port_vlan" }, //???
|
||||
{ 8450, table::DEC, "port_vlan_max" }, //???
|
||||
{ 8704, table::BOOL, "vlan_enabled" }, //+bool byte
|
||||
{ 8705, table::HEX, "vlan" }, //+one set per vlan
|
||||
{ 8706, table::HEX, "vlan_pvid" }, //+per port
|
||||
{ 8707, table::DEC, "vlan ?????" }, //????
|
||||
|
||||
{ 12288, table::BOOL, "qos_basic_enabled" }, //+bool = QoS Mod
|
||||
{ 12289, table::HEX, "qos_basic" }, //+per port ???
|
||||
{ 12544, table::HEX, "bandwidth_control_ingress" }, //+per port ???
|
||||
{ 12545, table::HEX, "bandwidth_control_egress" }, //+per port ???
|
||||
{ 12800, table::HEX, "storm_control" }, //+per port ???
|
||||
{ 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id???
|
||||
{ 16384, table::HEX, "port_statistics" }, //+per port ???
|
||||
{ 16896, table::HEX, "cable_test" }, //+per port ???
|
||||
{ 17152, table::BOOL, "loop_prevention" }, //+bool byte
|
||||
|
||||
//{0000," "},
|
||||
};
|
||||
|
||||
static table snd_lookup = {
|
||||
|
||||
// TODO find out if id is unique in response
|
||||
{ 2, table::HEX, "system_info" }, //page sysinfo
|
||||
{ 9, table::HEX, "ip_config" }, //page sysinfo
|
||||
{ 10, table::HEX, "ports" }, //after login
|
||||
{ 512, table::STRING, "login_user" }, //string
|
||||
{ 513, table::STRING, "new_user" }, //string
|
||||
{ 514, table::STRING, "login_password" }, //string
|
||||
{ 515, table::STRING, "new_passwoord" }, //string
|
||||
{ 773, table::BOOL, "reboot" }, //bool byte = save config
|
||||
|
||||
{ 1280, table::ACTION, "reset" }, //
|
||||
{ 1536, table::ACTION, "flash" }, //update firmware
|
||||
|
||||
{ 2200, table::HEX, "vlan" }, //vlan
|
||||
{ 2304, table::ACTION, "save" }, //save
|
||||
{ 2305, table::ACTION, "ping" }, //sent before SET ???
|
||||
|
||||
{ 4352, table::HEX, "igmp_snooping" }, //switching
|
||||
{ 4096, table::HEX, "port_settings" }, //switching
|
||||
{ 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask??
|
||||
|
||||
{ 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id
|
||||
{ 8448, table::BOOL, "port_vlan_enabled" }, //open page
|
||||
{ 8449, table::HEX, "port_vlan" }, //???
|
||||
{ 8704, table::BOOL, "vlan_enabled" }, //??? get vlan / set status
|
||||
{ 8705, table::HEX, "vlan" }, // per vlan
|
||||
{ 8706, table::HEX, "vlan_pvid" }, //per port
|
||||
|
||||
{ 12288, table::BOOL, "qos_basic_enabled" }, //bool = QoS Mod
|
||||
{ 12289, table::HEX, "qos_basic" }, //per port
|
||||
{ 12544, table::HEX, "bandwidth_control_ingress" }, //per port
|
||||
{ 12545, table::HEX, "bandwidth_control_egress" }, //per port
|
||||
{ 12800, table::HEX, "storm_control" }, //???
|
||||
{ 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??
|
||||
{ 16384, table::HEX, "port_statistics" }, //???
|
||||
{ 16896, table::HEX, "cable_test" }, //+per port ???
|
||||
{ 17152, table::BOOL, "loop_prevention" }, //bool byte
|
||||
|
||||
//{0000," "},
|
||||
};
|
335
src/Packet.cpp
335
src/Packet.cpp
|
@ -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);
|
||||
}
|
||||
|
|
113
src/Packet.h
113
src/Packet.h
|
@ -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_ */
|
||||
|
|
297
src/Program.cpp
297
src/Program.cpp
|
@ -14,22 +14,23 @@
|
|||
#include "Socket.h"
|
||||
#include "Switch.h"
|
||||
#include "Packet.h"
|
||||
#include "Lookup.h"
|
||||
#include "lookup.h"
|
||||
#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) {
|
||||
<<<<<<< HEAD
|
||||
if (options.flags & FLAG_HEX) {
|
||||
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
|
||||
} else {
|
||||
|
@ -84,78 +85,136 @@ int printPacket(Packet p) {
|
|||
}
|
||||
}
|
||||
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;
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
}
|
||||
|
||||
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() {
|
||||
<<<<<<< HEAD
|
||||
std::cout << "List:\n";
|
||||
Packet p = Packet(Packet::DISCOVERY);
|
||||
p.setHostMac(host.getMac());
|
||||
|
@ -219,39 +278,115 @@ int Program::getProperty() {
|
|||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
return 1;
|
||||
=======
|
||||
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_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.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->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;
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
}
|
||||
|
||||
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)>) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Program::ping(std::function<int(Packet)>){
|
||||
|
@ -260,6 +395,6 @@ int Program::ping(std::function<int(Packet)>){
|
|||
}
|
||||
|
||||
void Program::init() {
|
||||
if (options.interface.compare("") == 0)
|
||||
options.interface = host.getIface();
|
||||
if (options.interface.compare("") == 0)
|
||||
options.interface = host.getIface();
|
||||
}
|
||||
|
|
131
src/Socket.cpp
131
src/Socket.cpp
|
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
98
src/Switch-Json.cpp
Normal file
98
src/Switch-Json.cpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Switch.cpp
|
||||
*
|
||||
* Created on: 29.09.2015
|
||||
* Author: jdi
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include "Types.h"
|
||||
#include "Switch.h"
|
||||
#include "jsonNode.h"
|
||||
#include "Constant.h"
|
||||
#include "table.h"
|
||||
|
||||
int Switch::parse(std::string str) {
|
||||
|
||||
if (json.Parse(str.c_str()).HasParseError())
|
||||
return 1;
|
||||
if (options.flags & FLAG_DEBUG)
|
||||
std::cout << "\nParsing to document succeeded.\n";
|
||||
|
||||
if (json.IsObject()) {
|
||||
if (json.HasMember("hostname"))
|
||||
settings.hostname = json["hostname"].GetString();
|
||||
if (json.HasMember("type"))
|
||||
device.type = json["type"].GetString();
|
||||
if (json.HasMember("hardware_version"))
|
||||
device.hardware_version = json["hardware_version"].GetString();
|
||||
if (json.HasMember("firmware_version"))
|
||||
device.hardware_version = json["firmware_version"].GetString();
|
||||
if (json.HasMember("ports") && json["ports"].IsArray()) {
|
||||
const rapidjson::Value& a = json["ports"];
|
||||
for (rapidjson::SizeType i = 0; i < a.Size(); i++)
|
||||
if (a[i].IsObject()) {
|
||||
port p;
|
||||
if (a[i].HasMember("id") && a[i]["id"].IsInt()) {
|
||||
p.id = a[i]["id"].GetInt();
|
||||
std::cout << a[i]["id"].GetInt() << "\n";
|
||||
}
|
||||
ports.push_back(p);
|
||||
}
|
||||
}
|
||||
if (json.HasMember("vlans") && json["vlans"].IsArray()) {
|
||||
const rapidjson::Value& a = json["vlans"];
|
||||
for (rapidjson::SizeType i = 0; i < a.Size(); i++)
|
||||
if (a[i].IsObject()) {
|
||||
vlan v;
|
||||
if (a[i].HasMember("name") && a[i]["name"].IsString()) {
|
||||
v.name = a[i]["name"].GetString();
|
||||
std::cout << a[i]["name"].GetString() << "\n";
|
||||
}
|
||||
vlans.push_back(v);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
{
|
||||
const rapidjson::Value& a = json["a"];
|
||||
assert(a.IsArray());
|
||||
for (rapidjson::SizeType i = 0; i < a.Size(); i++)
|
||||
printf("a[%d] = %d\n", i, a[i].GetInt());
|
||||
|
||||
int y = a[0].GetInt();
|
||||
(void) y;
|
||||
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Switch::toString() {
|
||||
|
||||
if (!json.IsObject()) {
|
||||
json.SetObject();
|
||||
}
|
||||
rapidjson::Document::AllocatorType& allocator = json.GetAllocator();
|
||||
|
||||
json.AddMember("hostname", jsonNode(settings.hostname, json), allocator);
|
||||
json.AddMember("ip", jsonNode(settings.ip_addr, json), allocator);
|
||||
json.AddMember("netmask", jsonNode(settings.ip_mask, json), allocator);
|
||||
json.AddMember("gateway", jsonNode(settings.gateway, json), allocator);
|
||||
json.AddMember("type", jsonNode(device.type, json), allocator);
|
||||
json.AddMember("hardware_version", jsonNode(device.hardware_version, json),
|
||||
allocator);
|
||||
json.AddMember("firmware_version", jsonNode(device.firmware_version, json),
|
||||
allocator);
|
||||
json.AddMember("ports", jsonNode(ports, json), allocator);
|
||||
json.AddMember("vlans", jsonNode(vlans, json), allocator);
|
||||
|
||||
rapidjson::StringBuffer sb;
|
||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
|
||||
json.Accept(writer);
|
||||
return sb.GetString();
|
||||
}
|
||||
|
|
@ -8,18 +8,23 @@
|
|||
#include <string>
|
||||
#include "Types.h"
|
||||
#include "Switch.h"
|
||||
<<<<<<< HEAD
|
||||
#include "Lookup.h"
|
||||
=======
|
||||
#include "lookup.h"
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
#include "Constant.h"
|
||||
#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) {
|
||||
<<<<<<< HEAD
|
||||
auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
|
||||
std::string id = lookup.exists(d.type)?lookup[d.type]:"unknown";
|
||||
if (d.type == lookup["type"]) {
|
||||
|
@ -63,3 +68,49 @@ int Switch::parse(dataset d) {
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
>>>>>>> 3ef2da7b7a10c8b34f20c452acc0f8d0ffc17db5
|
||||
|
|
30
src/lookup.h
Normal file
30
src/lookup.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
enum {
|
||||
#define LOOKUP_SET(id, num, type) RCV_ ## id = num,
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
#define LOOKUP_SET(id, num, type) SND_ ## id = num,
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
#define LOOKUP_SET(id, num, type) id = num,
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
||||
static table rcv_lookup {
|
||||
#define LOOKUP_SET(id, num, type) { RCV_ ## id, table::type, #id },
|
||||
#include "lookup/rcv.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
||||
static table snd_lookup {
|
||||
#define LOOKUP_SET(id, num, type) { SND_ ## id, table::type, #id },
|
||||
#include "lookup/snd.lst"
|
||||
#include "lookup/general.lst"
|
||||
#undef LOOKUP_SET
|
||||
};
|
||||
|
25
src/lookup/general.lst
Normal file
25
src/lookup/general.lst
Normal file
|
@ -0,0 +1,25 @@
|
|||
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(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
|
||||
|
13
src/lookup/rcv.lst
Normal file
13
src/lookup/rcv.lst
Normal file
|
@ -0,0 +1,13 @@
|
|||
LOOKUP_SET(TYPE, 1, STRING) //+string
|
||||
LOOKUP_SET(HOSTNAME, 2, STRING) //+string
|
||||
LOOKUP_SET(MAC, 3, HEX) //+byte[6]
|
||||
LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4]
|
||||
LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4]
|
||||
LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4]
|
||||
LOOKUP_SET(FIRMWARE_VERSION, 7, STRING)
|
||||
LOOKUP_SET(HARDWARE_VERSION, 8, STRING)
|
||||
LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte
|
||||
|
||||
LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX)
|
||||
|
||||
|
17
src/lookup/snd.lst
Normal file
17
src/lookup/snd.lst
Normal file
|
@ -0,0 +1,17 @@
|
|||
LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo
|
||||
LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo
|
||||
|
||||
|
||||
LOOKUP_SET(LOGIN_USER, 512, STRING) //string
|
||||
LOOKUP_SET(NEW_USER, 513, STRING) //string
|
||||
LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string
|
||||
LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string
|
||||
LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config
|
||||
|
||||
LOOKUP_SET(RESET, 1280, ACTION) //
|
||||
LOOKUP_SET(FLASH, 1536, ACTION) //update firmware
|
||||
LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan
|
||||
LOOKUP_SET(SAVE, 2304, ACTION) //save
|
||||
LOOKUP_SET(PING, 2305, ACTION) //sent before SET ???
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
11
src/table.h
11
src/table.h
|
@ -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_ */
|
||||
|
|
Loading…
Reference in a new issue