minor improvements

This commit is contained in:
/jdi/ 2015-10-08 00:52:06 +02:00
parent e077b4ca8a
commit f11f281a2c
6 changed files with 91 additions and 58 deletions

View file

@ -1,5 +1,5 @@
CC = g++
CFLAGS = -Wall -std=c++11
CFLAGS = -Wall -std=c++14
TARGET = smrtlink
all: $(TARGET)

View file

@ -7,7 +7,6 @@
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include "Packet.h"
#include "Types.h"
@ -21,15 +20,16 @@ Packet::Packet(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);
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);
printf("\n\tChecksum:\t%d", checkSum);
std::cout << "Header:\n\tOpCode:\t\t" << opCodeToString() << "\n";
std::cout << "\tID:\t\t" << sequenceId << "\n";
std::cout << "\tVersion:\t" << 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";
}
bytes Packet::getBytes() {
@ -50,9 +50,6 @@ bytes Packet::getBytes() {
push(head, i, tokenId);
push(head, i, checkSum);
bytes data = head + body;
//printf("Send Header:\t");
//utils::printHex(head);
//printf("\n");
return data;
}
@ -60,9 +57,6 @@ 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);
@ -161,7 +155,7 @@ std::string Packet::opCodeToString() {
void Packet::encode(bytes &data) {
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,
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,
@ -179,7 +173,6 @@ void Packet::encode(bytes &data) {
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, j = 0;
for (int k = 0; k < len; k++) {

View file

@ -4,7 +4,7 @@
* Created on: 04.09.2015
* Author: jdi
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include "Options.h"
@ -16,9 +16,6 @@
int Program::list() {
//Device d = Device();
//printf(" %d\n", d.getName());
Host h = Host();
printf("List:\n");
Packet p = Packet(Packet::DISCOVERY);
@ -32,19 +29,26 @@ int Program::list() {
Socket s(io_service);
s.setHostIp(h.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback = [](Packet a) {
if (options.flags & FLAG_HEADER) {
std::cout <<"Received Header:\t"<< a.getHead() <<"\n";
}
if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\t"<<a.getBody()<<"\n";
}
datasets d =a.getPayload();
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";
return 1;
};
s.callback =
[](Packet a) {
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
std::cout <<"Received Header:\n"<< a.getHead() <<"\n";
} else {
a.printHeader();
std::cout<<"\n";
}
}
if (options.flags & FLAG_HEX) {
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);
io_service.run();
} catch (std::exception& e) {
@ -65,26 +69,26 @@ int Program::sniff() {
s.callback = [](Packet p) {
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
std::cout <<"Received Header:\t"<< p.getHead() <<"\n";
std::cout <<"Received Header:\n\t"<< p.getHead() <<"\n";
} else {
p.printHeader();
printf("\n");
}
}
if (options.flags & FLAG_HEX) {
std::cout <<"Received Payload:\t"<<p.getBody()<<"\n";
}
for(auto a : p.getPayload()) {
dataset d = a.second;
printf("#%d\tLength: %d\n\tHex: ",d.type,d.len);
std::cout << d.value;
printf("\n\tDec: ");
std::cout <<d.value;
std::cout <<"Received Payload:\n\t"<<p.getBody()<<"\n";
} else {
for(auto a : p.getPayload()) {
dataset d = a.second;
std::cout<<"#"<<d.type<<"\tLength: "<<d.len<<"\n";
std::cout<<std::hex<< "\tHex: " <<d.value<<"\n";
//std::cout<<std::dec<<"\tDec: " << d.value<<"\n";
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();
io_service.run();
} catch (std::exception& e) {
@ -98,11 +102,7 @@ int Program::encode(std::string s) {
bytes d(s);
Packet p = Packet(Packet::DISCOVERY);
p.encode(d);
printf("%x", d[0]);
for (unsigned i = 1; i < d.size(); i++) {
printf(":%x", d[i]);
}
printf("\n");
std::cout << d << std::endl;
return 0;
}

View file

@ -5,9 +5,19 @@
* Author: jdi
*/
#include <string>
#include "Types/bytes.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];
}

View file

@ -16,6 +16,20 @@
#define DEFAULT_USER "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 {
int vlan_id;
std::string name;
@ -40,16 +54,16 @@ public:
std::string type;
std::string hardware_version;
std::string firmware_version;
macAddr mac {0x0,0x0,0x0,0x0,0x0,0x0};
macAddr mac { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
} device;
struct {
std::string password = DEFAULT_PASS;
std::string username = DEFAULT_USER;
std::string hostname;
ipAddr ip_addr {0,0,0,0,};
ipAddr ip_addr { 0, 0, 0, 0, };
ipAddr ip_mask;
ipAddr gateway;
byte dhcp;
bool dhcp;
} settings;
private:
std::vector<vlan> vlans;

View file

@ -39,6 +39,14 @@ public:
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> {
@ -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) {
out << std::dec << (unsigned) arr[0];
for (unsigned i = 1; i < 4; i++) {