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

@ -1,12 +1,19 @@
CC = g++
CFLAGS = -g -Wall -std=c++11
CFLAGS = -Wall -std=c++11
TARGET = smrtlink
all: $(TARGET)
$(TARGET): src/*.cpp
$(CC) $(CFLAGS) -o $(TARGET) src/*.cpp src/*.h
$(TARGET): Types.o Program.o
$(CC) $(CFLAGS) -o $(TARGET) *.o
Types.o: src/Types/*.cpp src/Types*.h
$(CC) $(CFLAGS) -c src/Types/*.cpp
Program.o: src/*.cpp src/*.h
$(CC) $(CFLAGS) -c src/*.cpp
clean:
rm src/$(TARGET).g
rm *.o

View file

@ -5,8 +5,10 @@
* Author: jdi
*/
//TODO clean up
#include <cstdio>
#include <cerrno>
//#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <iostream>
@ -16,25 +18,21 @@
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <linux/if_link.h>
#include "Utils.h"
//#include <linux/if_link.h>
#include "Options.h"
#include "Host.h"
Host::Host() {
// TODO Auto-generated constructor stub
}
#include "Types.h"
#include "Types/bytes.h"
macAddr Host::getMac() {
macAddr ret { { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c } };
//TODO find actual MAC Address
macAddr ret { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c }; //TODO find actual MAC Address
return ret;
}
inetAddr Host::getIp(std::string iface) {
ipAddr Host::getIp() {
struct ifaddrs *ifaddr, *ifa;
int n;
inetAddr data { { 0, 0, 0, 0 } };
ipAddr data { 0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
@ -46,7 +44,7 @@ inetAddr Host::getIp(std::string iface) {
continue;
if (ifa->ifa_addr->sa_family == AF_INET) {
if (iface.compare(ifa->ifa_name) == 0) {
if (options.interface.compare(ifa->ifa_name) == 0) {
memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
return data;
}
@ -54,7 +52,6 @@ inetAddr Host::getIp(std::string iface) {
}
freeifaddrs(ifaddr);
return data;
}

View file

@ -12,9 +12,9 @@
class Host {
public:
Host();
Host(){};
macAddr getMac();
inetAddr getIp(std::string);
ipAddr getIp();
};
#endif /* HOST_H_ */

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;
}

View file

@ -12,6 +12,8 @@
#define PACKET_END 0xFFFF0000
#include "Types.h"
#include "Types/bytes.h"
#include "Types/datasets.h"
class Packet {
public:
@ -19,8 +21,6 @@ public:
DISCOVERY, GET, SET, READ
};
Packet(OpCode);
virtual ~Packet() {
}
void encode(bytes&);
bytes getBytes();
void parse(bytes);
@ -64,15 +64,15 @@ private:
void push(bytes&, int&, int);
void push(bytes&, int&, byte);
void push(bytes&, int&, bytes);
void push(bytes&, int&, inetAddr&);
void push(bytes&, int&, macAddr&);
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&, inetAddr&);
void pull(bytes&, int&, ipAddr&);
void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, dataset&);
};

View file

@ -7,17 +7,13 @@
#include <cstdio>
#include <algorithm>
#include "Utils.h"
#include "Options.h"
#include "Program.h"
#include "Host.h"
#include "Socket.h"
#include "Switch.h"
#include "Packet.h"
Program::Program() {
// TODO Auto-generated constructor stub
}
int Program::list() {
//Device d = Device();
@ -34,25 +30,19 @@ int Program::list() {
try {
asio::io_service io_service;
Socket s(io_service);
s.setHostIp(h.getIp(options.interface));
s.setHostIp(h.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback = [](Packet a) {
if (options.flags & FLAG_HEADER) {
printf("Received Header:\t");
utils::printHex(a.getHead());
printf("\n");
std::cout <<"Received Header:\t"<< a.getHead() <<"\n";
}
if (options.flags & FLAG_HEX) {
printf("Received Payload:\t");
utils::printHex(a.getBody());
printf("\n");
std::cout <<"Received Payload:\t"<<a.getBody()<<"\n";
}
datasets d =a.getPayload();
printf("\t%s (%s)\tMAC: ", &d[2].value[0], &d[1].value[0]);
utils::printHex(d[3].value);
printf("\tIP: ");
utils::printDec(d[4].value);
printf("\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";
return 1;
};
s.send(a);
@ -70,29 +60,26 @@ int Program::sniff() {
asio::io_service io_service;
Host h = Host();
Socket s(io_service);
s.setHostIp(h.getIp(options.interface));
s.setHostIp(h.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback = [](Packet p) {
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
printf("Received Header:\t");
utils::printHex(p.getHead());
std::cout <<"Received Header:\t"<< p.getHead() <<"\n";
} else {
p.printHeader();
}
printf("\n");
}
}
if (options.flags & FLAG_HEX) {
printf("Received Payload:\t");
utils::printHex(p.getBody());
printf("\n");
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);
utils::printHex(d.value);
std::cout << d.value;
printf("\n\tDec: ");
utils::printDec(d.value);
std::cout <<d.value;
d.value.push_back(0U);
printf("\n\tString: %s\n",&d.value[0]);
}
@ -108,7 +95,7 @@ int Program::sniff() {
}
int Program::encode(std::string s) {
bytes d = utils::readHex(s);
bytes d(s);
Packet p = Packet(Packet::DISCOVERY);
p.encode(d);
printf("%x", d[0]);
@ -119,7 +106,6 @@ int Program::encode(std::string s) {
return 0;
}
int Program::setProperty() {
return 0;
}
@ -128,11 +114,11 @@ int Program::getProperty(){
return 0;
}
int Program::save() {
//File = fopen(otions.file)
return 0;
}
int Program::restore() {
//File = fopen(otions.file)
return 0;
}
int Program::flash() {

View file

@ -15,9 +15,7 @@
class Program {
public:
Program();
virtual ~Program() {
}
Program(){}
int list();
int sniff();
int encode(std::string);

View file

@ -14,7 +14,6 @@
#include "Socket.h"
#include "Packet.h"
#include "Types.h"
#include "Utils.h"
#include "Options.h"
#include "Host.h"
@ -30,9 +29,7 @@ void Socket::init(short dst_port, short src_port) {
src_port = p;
}
printf("IP:\t");
utils::print(local_ip);
printf("\n");
std::cout <<"IP:\t"<<local_ip<<"\n";
wildcard_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address_v4::from_string("0.0.0.0"), src_port);
@ -44,16 +41,16 @@ void Socket::init(short dst_port, short src_port) {
send_socket_.open(asio::ip::udp::v4());
send_socket_.set_option(asio::socket_base::broadcast(true));
send_socket_.set_option(asio::socket_base::reuse_address(true));
send_socket_.bind(local_endpoint_); //TODO reuse Address
send_socket_.bind(local_endpoint_);
receive_socket_.open(asio::ip::udp::v4());
receive_socket_.set_option(asio::socket_base::broadcast(true));
receive_socket_.set_option(asio::socket_base::reuse_address(true));
receive_socket_.bind(wildcard_endpoint_); //TODO reuse Address
receive_socket_.bind(wildcard_endpoint_);
}
void Socket::setHostIp(inetAddr ip) {
void Socket::setHostIp(ipAddr ip) {
local_ip=ip;
}

View file

@ -11,6 +11,7 @@
#include <asio.hpp>
#include "Packet.h"
#include "Types.h"
//#include "Types.h"
#define MAX_LENGTH 1024
@ -22,7 +23,7 @@ public:
void init(short, short);
void send(bytes);
void listen();
void setHostIp(inetAddr);
void setHostIp(ipAddr);
int (*callback)(Packet)=[](Packet a) {
return 0;
};
@ -36,7 +37,7 @@ private:
asio::ip::udp::endpoint wildcard_endpoint_;
asio::ip::udp::endpoint local_endpoint_;
bytes data = bytes(MAX_LENGTH);
inetAddr local_ip;
ipAddr local_ip;
};

View file

@ -7,8 +7,7 @@
#include "Switch.h"
Switch::Switch() {
// TODO Auto-generated constructor stub
void Switch::parse(datasets arr){
}

View file

@ -10,6 +10,8 @@
#include <string>
#include "Types.h"
#include "Types/bytes.h"
#include "Types/datasets.h"
#define DEFAULT_USER "admin"
#define DEFAULT_PASS "admin"
@ -20,7 +22,8 @@ struct vlan {
};
struct port {
int id;
byte id;
byte status;
struct {
std::vector<vlan*> tagged;
std::vector<vlan*> untagged;
@ -30,26 +33,25 @@ struct port {
class Switch {
public:
Switch();
Switch() {
}
void parse(datasets);
private:
struct {
std::string type;
std::string hardware_version;
std::string firmware_version;
macAddr mac;
macAddr mac {0x0,0x0,0x0,0x0,0x0,0x0};
} device;
struct {
std::string password = DEFAULT_PASS;
std::string username = DEFAULT_USER;
struct {
std::string hostname;
inetAddr ip_addr;
inetAddr ip_mask;
inetAddr gateway;
ipAddr ip_addr {0,0,0,0,};
ipAddr ip_mask;
ipAddr gateway;
byte dhcp;
} network;
} settings;
private:
std::vector<vlan> vlans;
std::vector<port> ports;
};

View file

@ -8,10 +8,62 @@
#ifndef TYPES_H_
#define TYPES_H_
#include <functional>
#include <initializer_list>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include <map>
#include "Types/bytes.h"
class macAddr: public std::array<byte, 6> {
public:
friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) {
out << std::hex << std::setw(2) << std::setfill('0')
<< (unsigned) arr[0];
for (unsigned i = 1; i < 6; i++) {
out << ":" << std::setw(2) << std::setfill('0')
<< (unsigned) arr[i];
}
return out;
}
macAddr() {
*this= {0,0,0,0,0,0};
}
macAddr(std::initializer_list<byte> s) {
int i = 0;
for (byte b : s) {
if(i<6) (*this)[i++]=b;
else break;
}
}
};
class ipAddr: public std::array<byte, 4> {
public:
ipAddr() {
*this= {0,0,0,0,0,0};
}
ipAddr(std::initializer_list<byte> s) {
int i = 0;
for (byte b : s) {
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++) {
out << "." << (unsigned) arr[i];
}
return out;
}
};
template<typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) {
@ -29,23 +81,6 @@ std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
return A;
}
typedef std::array<unsigned char, 6> macAddr;
typedef std::array<unsigned char, 4> inetAddr;
typedef std::vector<unsigned char> bytes;
typedef unsigned char byte;
struct dataset {
short type;
short len;
bytes value;
};
//typedef std::vector<dataset> datasets;
typedef std::map<short, dataset> datasets;
//std::function<int()>;
//typedef int receiveCallback;
struct Options {
unsigned flags = 0x00;
std::string user;

View file

@ -1,68 +0,0 @@
/*
* Utils.h
*
* Created on: 24.09.2015
* Author: jdi
*/
#ifndef UTILS_H_
#define UTILS_H_
#include <string>
#include <cstring>
#include "Types.h"
namespace utils {
static bytes readHex(std::string d) {
std::string delimiter = ":";
std::string token;
size_t pos = 0;
bytes arr = { };
int hex;
byte b;
while ((pos = d.find(delimiter)) != std::string::npos) {
token = d.substr(0, pos);
sscanf(token.c_str(), "%x", &hex);
d.erase(0, pos + delimiter.length());
b = hex & 0xFF;
arr.push_back(b);
}
sscanf(d.c_str(), "%x", &hex);
b = hex & 0xFF;
arr.push_back(b);
return arr;
}
static void printHex(bytes d) {
if (d.size() > 0)
printf("%.2X", d[0]);
for (unsigned i = 1; i < d.size(); i++) {
printf(":%.2X", d[i]);
}
}
static void print(macAddr d) {
printf("%.2X", d[0]);
for (unsigned i = 1; i < 6; i++) {
printf(":%.2X", d[i]);
}
}
static void print(inetAddr d) {
printf("%.1d", d[0]);
for (unsigned i = 1; i < 4; i++) {
printf(".%.1d", d[i]);
}
}
static void printDec(bytes d) {
if (d.size() > 0)
printf("%.1d", d[0]);
for (unsigned i = 1; i < d.size(); i++) {
printf(".%.1d", d[i]);
}
}
}
#endif /* UTILS_H_ */

View file

@ -7,13 +7,13 @@
//============================================================================
#include <cstring>
#include <getopt.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <getopt.h>
#include <unistd.h>
#include "Options.h"
#include "Host.h"
@ -158,8 +158,7 @@ int main(int argc, char *argv[]) {
break;
default:
printf("Unknown command: %s\n", argv[optind]);
optind++;
printf("Unknown command: %s\n", cmd.c_str());
while (optind < argc) {
printf("->%s\n", argv[optind]);
optind++;