minor improvements
This commit is contained in:
parent
e077b4ca8a
commit
f11f281a2c
6 changed files with 91 additions and 58 deletions
2
Makefile
2
Makefile
|
|
@ -1,5 +1,5 @@
|
||||||
CC = g++
|
CC = g++
|
||||||
CFLAGS = -Wall -std=c++11
|
CFLAGS = -Wall -std=c++14
|
||||||
TARGET = smrtlink
|
TARGET = smrtlink
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstdio>
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "Packet.h"
|
#include "Packet.h"
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
@ -21,15 +20,16 @@ Packet::Packet(OpCode c) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Packet::printHeader() {
|
void Packet::printHeader() {
|
||||||
printf(
|
std::cout << "Header:\n\tOpCode:\t\t" << opCodeToString() << "\n";
|
||||||
"Header:\n\tOpCode:\t\t%s\n\tID:\t\t%d\n\tVersion:\t%hhd\n\tError:\t\t%.8X\n\tSwitch MAC:\t",
|
std::cout << "\tID:\t\t" << sequenceId << "\n";
|
||||||
opCodeToString().c_str(), sequenceId, version, errorCode);
|
std::cout << "\tVersion:\t" << version << "\n";
|
||||||
std::cout << switchMac << "\n";
|
std::cout << "\tError:\t\t" << errorCode << "\n";
|
||||||
std::cout << "\tHost MAC:\t"<< hostMac << "\n";
|
std::cout << "\tSwitch MAC:\t" << switchMac << "\n";
|
||||||
printf("\n\tLength:\t%hd", this->getLength());
|
std::cout << "\tHost MAC: \t" << hostMac << "\n";
|
||||||
printf("\n\tOffset:\t%hd", fragmentOffset);
|
std::cout << "\tLength: \t" << std::dec << this->getLength() << "\n";
|
||||||
printf("\n\tFlags:\t%.4hX", flag);
|
std::cout << "\tOffset: \t" << fragmentOffset << "\n";
|
||||||
printf("\n\tChecksum:\t%d", checkSum);
|
std::cout << "\tFlags: \t" << std::hex << flag << "\n";
|
||||||
|
std::cout << "\tChecksum: \t" << std::dec << checkSum << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes Packet::getBytes() {
|
bytes Packet::getBytes() {
|
||||||
|
|
@ -50,9 +50,6 @@ bytes Packet::getBytes() {
|
||||||
push(head, i, tokenId);
|
push(head, i, tokenId);
|
||||||
push(head, i, checkSum);
|
push(head, i, checkSum);
|
||||||
bytes data = head + body;
|
bytes data = head + body;
|
||||||
//printf("Send Header:\t");
|
|
||||||
//utils::printHex(head);
|
|
||||||
//printf("\n");
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,9 +57,6 @@ void Packet::parse(bytes data) {
|
||||||
memcpy(&head[0], &data[0], HEADER_LEN);
|
memcpy(&head[0], &data[0], HEADER_LEN);
|
||||||
body.resize(data.size() - HEADER_LEN);
|
body.resize(data.size() - HEADER_LEN);
|
||||||
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
|
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
|
||||||
//printf("Receive Header:\t");
|
|
||||||
//utils::printHex(head);
|
|
||||||
//printf("\n");
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
short checkLen = 0x0;
|
short checkLen = 0x0;
|
||||||
pull(head, i, version);
|
pull(head, i, version);
|
||||||
|
|
@ -161,7 +155,7 @@ std::string Packet::opCodeToString() {
|
||||||
|
|
||||||
void Packet::encode(bytes &data) {
|
void Packet::encode(bytes &data) {
|
||||||
int len = data.size();
|
int len = data.size();
|
||||||
std::vector<unsigned char> t = { 191, 155, 227, 202, 99, 162, 79, 104, 49,
|
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,
|
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,
|
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,
|
44, 41, 97, 108, 213, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14,
|
||||||
|
|
@ -179,7 +173,6 @@ void Packet::encode(bytes &data) {
|
||||||
138, 216, 57, 93, 65, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9,
|
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,
|
146, 171, 149, 53, 102, 61, 114, 69, 217, 175, 103, 228, 35, 180,
|
||||||
252, 200, 192, 165, 159, 221, 244, 110, 119, 48 };
|
252, 200, 192, 165, 159, 221, 244, 110, 119, 48 };
|
||||||
bytes key = t;
|
|
||||||
bytes s = key;
|
bytes s = key;
|
||||||
int i, j = 0;
|
int i, j = 0;
|
||||||
for (int k = 0; k < len; k++) {
|
for (int k = 0; k < len; k++) {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
* Created on: 04.09.2015
|
* Created on: 04.09.2015
|
||||||
* Author: jdi
|
* Author: jdi
|
||||||
*/
|
*/
|
||||||
#include <cstdio>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "Options.h"
|
#include "Options.h"
|
||||||
|
|
@ -16,9 +16,6 @@
|
||||||
|
|
||||||
int Program::list() {
|
int Program::list() {
|
||||||
|
|
||||||
//Device d = Device();
|
|
||||||
//printf(" %d\n", d.getName());
|
|
||||||
|
|
||||||
Host h = Host();
|
Host h = Host();
|
||||||
printf("List:\n");
|
printf("List:\n");
|
||||||
Packet p = Packet(Packet::DISCOVERY);
|
Packet p = Packet(Packet::DISCOVERY);
|
||||||
|
|
@ -32,19 +29,26 @@ int Program::list() {
|
||||||
Socket s(io_service);
|
Socket s(io_service);
|
||||||
s.setHostIp(h.getIp());
|
s.setHostIp(h.getIp());
|
||||||
s.init(DST_PORT, SRC_PORT);
|
s.init(DST_PORT, SRC_PORT);
|
||||||
s.callback = [](Packet a) {
|
s.callback =
|
||||||
if (options.flags & FLAG_HEADER) {
|
[](Packet a) {
|
||||||
std::cout <<"Received Header:\t"<< a.getHead() <<"\n";
|
if (options.flags & FLAG_HEADER) {
|
||||||
}
|
if (options.flags & FLAG_HEX) {
|
||||||
if (options.flags & FLAG_HEX) {
|
std::cout <<"Received Header:\n"<< a.getHead() <<"\n";
|
||||||
std::cout <<"Received Payload:\t"<<a.getBody()<<"\n";
|
} else {
|
||||||
}
|
a.printHeader();
|
||||||
datasets d =a.getPayload();
|
std::cout<<"\n";
|
||||||
std::cout <<"\t"<<d[2].value <<"("<< d[1].value<<")\tMAC: "<<d[3].value<<"\tIP: "<<d[4].value<<"\n";
|
}
|
||||||
Switch s = Switch();
|
}
|
||||||
std::cout <<"\t"<<s.settings.hostname<<"("<< s.device.type<<")\tMAC: "<<s.device.mac<<"\tIP: "<<s.settings.ip_addr<<"\n";
|
if (options.flags & FLAG_HEX) {
|
||||||
return 1;
|
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
|
||||||
};
|
} else {
|
||||||
|
datasets d =a.getPayload();
|
||||||
|
Switch s = Switch();
|
||||||
|
s.parse(d);
|
||||||
|
std::cout <<"Devices:\n\t"<<s.settings.hostname<<" ("<< s.device.type<<")\tMAC: "<<s.device.mac<<"\tIP: "<<s.settings.ip_addr<<"\n";
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
s.send(a);
|
s.send(a);
|
||||||
io_service.run();
|
io_service.run();
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
|
|
@ -65,26 +69,26 @@ int Program::sniff() {
|
||||||
s.callback = [](Packet p) {
|
s.callback = [](Packet p) {
|
||||||
if (options.flags & FLAG_HEADER) {
|
if (options.flags & FLAG_HEADER) {
|
||||||
if (options.flags & FLAG_HEX) {
|
if (options.flags & FLAG_HEX) {
|
||||||
std::cout <<"Received Header:\t"<< p.getHead() <<"\n";
|
std::cout <<"Received Header:\n\t"<< p.getHead() <<"\n";
|
||||||
} else {
|
} else {
|
||||||
p.printHeader();
|
p.printHeader();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.flags & FLAG_HEX) {
|
if (options.flags & FLAG_HEX) {
|
||||||
std::cout <<"Received Payload:\t"<<p.getBody()<<"\n";
|
std::cout <<"Received Payload:\n\t"<<p.getBody()<<"\n";
|
||||||
}
|
} else {
|
||||||
for(auto a : p.getPayload()) {
|
for(auto a : p.getPayload()) {
|
||||||
dataset d = a.second;
|
dataset d = a.second;
|
||||||
printf("#%d\tLength: %d\n\tHex: ",d.type,d.len);
|
std::cout<<"#"<<d.type<<"\tLength: "<<d.len<<"\n";
|
||||||
std::cout << d.value;
|
std::cout<<std::hex<< "\tHex: " <<d.value<<"\n";
|
||||||
printf("\n\tDec: ");
|
//std::cout<<std::dec<<"\tDec: " << d.value<<"\n";
|
||||||
std::cout <<d.value;
|
|
||||||
d.value.push_back(0U);
|
d.value.push_back(0U);
|
||||||
printf("\n\tString: %s\n",&d.value[0]);
|
std::cout<<"\tString: " <<&d.value[0]<<"\n";
|
||||||
}
|
}
|
||||||
return 0;
|
}
|
||||||
};
|
return 0;
|
||||||
|
};
|
||||||
s.listen();
|
s.listen();
|
||||||
io_service.run();
|
io_service.run();
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
|
|
@ -98,11 +102,7 @@ int Program::encode(std::string s) {
|
||||||
bytes d(s);
|
bytes d(s);
|
||||||
Packet p = Packet(Packet::DISCOVERY);
|
Packet p = Packet(Packet::DISCOVERY);
|
||||||
p.encode(d);
|
p.encode(d);
|
||||||
printf("%x", d[0]);
|
std::cout << d << std::endl;
|
||||||
for (unsigned i = 1; i < d.size(); i++) {
|
|
||||||
printf(":%x", d[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,19 @@
|
||||||
* Author: jdi
|
* Author: jdi
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "Types/bytes.h"
|
||||||
#include "Switch.h"
|
#include "Switch.h"
|
||||||
|
|
||||||
void Switch::parse(datasets arr){
|
void Switch::parse(datasets arr) {
|
||||||
|
device.type = arr[1].value;
|
||||||
|
settings.hostname = arr[2].value;
|
||||||
|
device.mac = arr[3].value;
|
||||||
|
settings.ip_addr = arr[4].value;
|
||||||
|
settings.ip_mask = arr[5].value;
|
||||||
|
settings.gateway = arr[6].value;
|
||||||
|
device.firmware_version = arr[7].value;
|
||||||
|
device.hardware_version = arr[8].value;
|
||||||
|
settings.dhcp=arr[9].value[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
20
src/Switch.h
20
src/Switch.h
|
|
@ -16,6 +16,20 @@
|
||||||
#define DEFAULT_USER "admin"
|
#define DEFAULT_USER "admin"
|
||||||
#define DEFAULT_PASS "admin"
|
#define DEFAULT_PASS "admin"
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
static bitable lookup = {
|
||||||
|
{1, "type"},
|
||||||
|
{2, "hostname"},
|
||||||
|
{3, "mac"},
|
||||||
|
{4, "ip_addr"},
|
||||||
|
{5, "ip_mask"},
|
||||||
|
{6, "gateway"},
|
||||||
|
{7, "firmware_version"},
|
||||||
|
{8, "hardware_version"},
|
||||||
|
{9, "dhcp"}};
|
||||||
|
*/
|
||||||
|
|
||||||
struct vlan {
|
struct vlan {
|
||||||
int vlan_id;
|
int vlan_id;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
@ -40,16 +54,16 @@ public:
|
||||||
std::string type;
|
std::string type;
|
||||||
std::string hardware_version;
|
std::string hardware_version;
|
||||||
std::string firmware_version;
|
std::string firmware_version;
|
||||||
macAddr mac {0x0,0x0,0x0,0x0,0x0,0x0};
|
macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
|
||||||
} device;
|
} device;
|
||||||
struct {
|
struct {
|
||||||
std::string password = DEFAULT_PASS;
|
std::string password = DEFAULT_PASS;
|
||||||
std::string username = DEFAULT_USER;
|
std::string username = DEFAULT_USER;
|
||||||
std::string hostname;
|
std::string hostname;
|
||||||
ipAddr ip_addr {0,0,0,0,};
|
ipAddr ip_addr { 0, 0, 0, 0, };
|
||||||
ipAddr ip_mask;
|
ipAddr ip_mask;
|
||||||
ipAddr gateway;
|
ipAddr gateway;
|
||||||
byte dhcp;
|
bool dhcp;
|
||||||
} settings;
|
} settings;
|
||||||
private:
|
private:
|
||||||
std::vector<vlan> vlans;
|
std::vector<vlan> vlans;
|
||||||
|
|
|
||||||
16
src/Types.h
16
src/Types.h
|
|
@ -39,6 +39,14 @@ public:
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macAddr(bytes bts) {
|
||||||
|
int i = 0;
|
||||||
|
for (byte b : bts) {
|
||||||
|
if(i<6) (*this)[i++]=b;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ipAddr: public std::array<byte, 4> {
|
class ipAddr: public std::array<byte, 4> {
|
||||||
|
|
@ -56,6 +64,14 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ipAddr(bytes bts) {
|
||||||
|
int i = 0;
|
||||||
|
for (byte b : bts) {
|
||||||
|
if(i<4) (*this)[i++]=b;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) {
|
friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) {
|
||||||
out << std::dec << (unsigned) arr[0];
|
out << std::dec << (unsigned) arr[0];
|
||||||
for (unsigned i = 1; i < 4; i++) {
|
for (unsigned i = 1; i < 4; i++) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue