This commit is contained in:
j3d1 2016-02-25 19:46:35 +01:00
parent f15abc71be
commit 1f3730ac87
26 changed files with 880 additions and 844 deletions

View file

@ -14,7 +14,8 @@ std::string File::read() {
fs::create_directory(home);
}
fs::ifstream in(((options.file=="")?home / DEFAULT_FILE:options.file), std::ios::in | std::ios::binary);
fs::ifstream in(((options.file == "") ? home / DEFAULT_FILE : options.file),
std::ios::in | std::ios::binary);
if (in) {
std::string contents;
in.seekg(0, std::ios::end);
@ -31,9 +32,9 @@ int File::write(std::string content) {
if (!fs::exists(home)) {
fs::create_directory(home);
}
fs::path p = (options.file=="")?home / DEFAULT_FILE:options.file;
fs::path p = (options.file == "") ? home / DEFAULT_FILE : options.file;
fs::ofstream file(p);
file << content<<"\n";
file << content << "\n";
file.close();
return 0;
}

View file

@ -17,13 +17,13 @@ namespace fs = boost::filesystem;
#define DEFAULT_FILE "config.json"
class File {
public:
public:
File() {
home = fs::path(getenv("HOME")) / ".smrtlink";
}
std::string read();
int write(std::string);
private:
private:
fs::path home;
};

View file

@ -30,7 +30,8 @@ Filter Filter::tokenid(short i) {
}
bool Filter::pass(const Packet p) const {
macAddr none { 0, 0, 0, 0, 0, 0 };
macAddr none {
0, 0, 0, 0, 0, 0 };
if (this->oc != Packet::NONE && this->oc != p.getOpCode())
return false;
if (this->switchMac != none && this->switchMac != p.getSwitchMac())

View file

@ -11,7 +11,7 @@
#include "Packet.h"
class Filter {
public:
public:
Filter();
Filter(Packet::OpCode);
Filter(macAddr);
@ -20,9 +20,10 @@ public:
Filter tokenid(short);
bool pass(const Packet) const;
bool operator<(const Filter) const;
private:
private:
Packet::OpCode oc = Packet::NONE;
macAddr switchMac = { 0, 0, 0, 0, 0, 0 };
macAddr switchMac = {
0, 0, 0, 0, 0, 0 };
short tokenId = -1;
short fragmentOffset = -1;
};

View file

@ -28,7 +28,8 @@
macAddr Host::getMac() {
int s;
struct ifreq buffer;
macAddr data { 0, 0, 0, 0, 0, 0 };
macAddr data {
0, 0, 0, 0, 0, 0 };
if (options.interface != "") {
s = socket(PF_INET, SOCK_DGRAM, 0);
memset(&buffer, 0x00, sizeof(buffer));
@ -43,7 +44,8 @@ macAddr Host::getMac() {
ipAddr Host::getIp() {
struct ifaddrs *ifaddr, *ifa;
int n;
ipAddr data { 0, 0, 0, 0 };
ipAddr data {
0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);

View file

@ -11,8 +11,9 @@
#include "Types.h"
class Host {
public:
Host(){};
public:
Host() {
}
macAddr getMac();
ipAddr getIp();
std::string getIface();

View file

@ -9,7 +9,7 @@
#define INTERACTIVE_H_
class Interactive {
public:
public:
Interactive() {
}
virtual ~Interactive() {

View file

@ -168,24 +168,25 @@ 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 };
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++) {
@ -199,7 +200,8 @@ void Packet::encode(bytes &data) {
void Packet::push(bytes &arr, int &index, byte data) {
if (arr.size() > (unsigned) index) {
arr[index++] = data;
} else {
}
else {
arr.push_back(data);
index++;
}

View file

@ -16,7 +16,7 @@
static short sequenceId = 0;
class Packet {
public:
public:
enum OpCode {
DISCOVERY, GET, REPLY, SET, CONFIRM, NONE
};
@ -45,15 +45,18 @@ public:
byte getOpCode() const;
void setOpCode(byte opCode);
private:
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 } };
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;

View file

@ -72,7 +72,8 @@ int Program::run(vector<string> arg) {
optind++;
if (!encode(s))
return 1;
} else {
}
else {
fprintf(stderr, "Argument expected after encode\n");
return 1;
}
@ -85,8 +86,10 @@ int Program::run(vector<string> arg) {
cerr << "Unknown argument " << arg[optind] << endl;
return 1;
}
ll.insert(std::pair<std::string, std::string>(sm[1], sm[2]));
} else {
ll.insert(
std::pair<std::string, std::string>(sm[1], sm[2]));
}
else {
cerr << "Invalid Syntax " << arg[optind] << endl;
return 1;
}
@ -104,7 +107,8 @@ int Program::run(vector<string> arg) {
return 1;
}
vect.push_back(sm[1]);
} else {
}
else {
cerr << "Invalid argument " << arg[optind] << endl;
return 1;
}
@ -125,7 +129,8 @@ int printHeader(Packet p) {
if (options.flags.HEADER) {
if (options.flags.HEX) {
cout << "Received Header:\n\t" << p.getHead() << "\n";
} else {
}
else {
p.printHeader();
printf("\n");
}
@ -136,7 +141,8 @@ int printHeader(Packet p) {
int printPacket(Packet p) {
if (options.flags.HEX) {
cout << "Received Payload:\n\t" << p.getBody() << "\n";
} else {
}
else {
for (dataset d : p.getPayload()) {
//auto lookup = (options.flags.REVERSE) ? snd_lookup : rcv_lookup;
if (lookup.exists(d.type)) {
@ -144,7 +150,8 @@ int printPacket(Packet p) {
if (d.len > 0) {
switch (s.format) {
case table::STRING:
cout << "+\t" << s.name << " = " << &d.value[0] << "\n";
cout << "+\t" << s.name << " = " << &d.value[0]
<< "\n";
break;
case table::BOOL:
cout << "+\t" << s.name << " = "
@ -170,14 +177,17 @@ int printPacket(Packet p) {
cout << "+\t" << s.name << " = " << d.value << "\n";
break;
}
} else { //empty
}
else { //empty
cout << dec << ">\t" << s.name << "\n";
}
} else { //unknown id
}
else { //unknown id
if (d.len > 0) {
cout << "##\t" << d.type << ":\n\t";
cout << hex << d.value << dec << "\n";
} else { //empty
}
else { //empty
cout << "#>\t" << d.type << "\n";
}
}
@ -193,7 +203,8 @@ int Program::list() {
printHeader(a);
if (options.flags.HEX) {
cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else {
}
else {
datasets d =a.getPayload();
int b = a.getSwitchMac().hash();
@ -416,8 +427,9 @@ int Program::set(Packet l, datasets t, Listener c) {
p.setHostMac(host.getMac());
bytes n = options.user;
bytes w = options.password;
datasets ld = { { LOGIN_USER, (short) (n.size()), n }, { LOGIN_PASSWORD,
(short) (w.size()), w } };
datasets ld = {
{ LOGIN_USER, (short) (n.size()), n }, {
LOGIN_PASSWORD, (short) (w.size()), w } };
p.setPayload(ld + t);
sock->listen(c, Filter(Packet::CONFIRM).mac(l.getSwitchMac()));
sock->send(p);

View file

@ -17,7 +17,7 @@
#include "Socket.h"
class Program {
private:
private:
std::shared_ptr<boost::asio::io_service> io_service;
std::shared_ptr<Socket> sock;
Host host = Host();
@ -25,7 +25,7 @@ private:
int get(Packet, datasets, std::function<int(Packet)>);
int set(Packet, datasets, std::function<int(Packet)>);
int discover(std::function<int(Packet)>);
public:
public:
Program() {
}
void init();

View file

@ -57,14 +57,15 @@ void Socket::setHostIp(ipAddr ip) {
local_ip = ip;
}
void Socket::clear(){
void Socket::clear() {
callback.clear();
}
void Socket::listen(Listener l, Filter f) {
if (callback.find(f) == callback.end()) {
callback.insert(ListenerPair(f, l));
} else {
}
else {
callback[f] = l;
}
receive();
@ -92,7 +93,8 @@ void Socket::receive() {
if (ec || bytes_recvd == 0) {
//listen();
// TODO distinguish error codes
} else {
}
else {
data.resize(bytes_recvd);
Packet p = Packet(Packet::NONE);
p.encode(data);
@ -102,7 +104,8 @@ void Socket::receive() {
r.second(p);
// std::cout<<"pass"<<std::endl;
break;
} else {
}
else {
// std::cout<<"no pass"<<std::endl;
}
}

View file

@ -23,7 +23,7 @@ typedef std::function<int(Packet)> Listener;
typedef std::pair<Filter, Listener> ListenerPair;
class Socket {
public:
public:
Socket(boost::asio::io_service&);
virtual ~Socket();
void init(short, short);
@ -32,7 +32,7 @@ public:
void setHostIp(ipAddr);
void listen(Listener l, Filter f = Filter());
private:
private:
void receive();
void settimeout();
boost::asio::ip::udp::socket send_socket_;

View file

@ -16,7 +16,7 @@ int Switch::parse(std::string str) {
if (json.Parse(str.c_str()).HasParseError())
return 1;
if (options.debug_level>=1)
if (options.debug_level >= 1)
std::cout << "\nParsing to document succeeded.\n";
if (json.IsObject()) {

View file

@ -59,13 +59,13 @@ struct port {
};
class Switch {
public:
public:
Switch() {
}
int parse(datasets);
int parse(dataset);
int parse(std::string);
int set(std::pair<std::string,std::string>);
int set(std::pair<std::string, std::string>);
std::string get(std::string);
std::string toString();
@ -76,14 +76,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 };
int ports;
} 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;
bool dhcp;
@ -93,7 +95,7 @@ public:
//mtu_vlan
} settings;
private:
private:
rapidjson::Document json;
std::vector<vlan> vlans;
std::vector<port> ports;

View file

@ -20,7 +20,7 @@
#include "table.h"
class macAddr: public std::array<byte, 6> {
public:
public:
friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) {
out << std::hex << std::setw(2) << std::setfill('0')
<< (unsigned) arr[0];
@ -72,9 +72,9 @@ public:
}
return false;
}
};
};
/*
/*
class mac_addr : public std::array<unsigned char, 6> {
public:
typedef std::array<unsigned char, 6> super;
@ -86,7 +86,7 @@ public:
*/
class ipAddr: public std::array<byte, 4> {
public:
public:
ipAddr() {
*this= {0,0,0,0,0,0};
@ -115,13 +115,13 @@ public:
}
return out;
}
};
};
namespace smrtlink {
constexpr unsigned int caseArg(const char* str, int h = 0) {
constexpr unsigned int caseArg(const char* str, int h = 0) {
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
}
}
}

View file

@ -7,7 +7,7 @@
#include "bytes.h"
bytes bytes::readHex(std::string s){
bytes bytes::readHex(std::string s) {
vector ret;
std::string delimiter = ":";
std::string token;

View file

@ -20,19 +20,20 @@ typedef unsigned char byte;
class bytes: public std::vector<unsigned char> {
typedef std::vector<unsigned char> vector;
public:
public:
using vector::operator[];
bytes() {
}
bytes(int n) : vector(n){
bytes(int n) :
vector(n) {
}
bytes(std::string d) : vector(d.begin(), d.end()){
bytes(std::string d) :
vector(d.begin(), d.end()) {
this->push_back('\0');
}
bytes(std::initializer_list<uint8_t> s)
{
bytes(std::initializer_list<uint8_t> s) {
for (uint8_t b : s) {
this->push_back(b);
}
@ -71,13 +72,14 @@ public:
<< (unsigned) arr[0];
}
for (unsigned i = 1; i < arr.size(); i++) {
out << ":" << std::setw(2) << std::setfill('0') << (unsigned) arr[i];
out << ":" << std::setw(2) << std::setfill('0')
<< (unsigned) arr[i];
}
return out;
}
operator std::string() {
std::string s(this->begin(),this->end());
std::string s(this->begin(), this->end());
return s;
}
};

View file

@ -17,11 +17,11 @@ struct dataset {
bytes value;
};
class datasets : public std::vector<dataset> {
public:
datasets(){};
datasets(std::initializer_list<dataset> s)
{
class datasets: public std::vector<dataset> {
public:
datasets() {
}
datasets(std::initializer_list<dataset> s) {
for (dataset b : s) {
//(*this)[b.type]=b;
push_back(b);

View file

@ -15,7 +15,7 @@
class jsonNode: public rapidjson::Value {
typedef rapidjson::Value super;
typedef rapidjson::Document doc;
public:
public:
jsonNode(const std::string&, doc&);
jsonNode(const macAddr&, doc&);
jsonNode(const ipAddr&, doc&);
@ -23,8 +23,8 @@ public:
jsonNode(const port&, doc&);
template<class T>
jsonNode(const T &templ_arg, doc &root){
std::cerr<<"Not serializable Type: "<<typeid(T).name()<<"\n";
jsonNode(const T &templ_arg, doc &root) {
std::cerr << "Not serializable Type: " << typeid(T).name() << "\n";
}
template<class T>

View file

@ -38,17 +38,22 @@ int main(int argc, char *argv[]) {
options.user = DEFAULT_USER;
options.password = DEFAULT_PASS;
const struct option longopts[] = { { "version", no_argument, 0, 'V' }, {
"verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, {
const struct option longopts[] = {
{ "version", no_argument, 0, 'V' }, {
"verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' }, {
"reverse", no_argument, 0, 'r' },
{ "permanent", no_argument, 0, 's' }, { "debug", optional_argument,
0, 'd' }, { "password", required_argument, 0, 'P' }, {
"user", required_argument, 0, 'U' }, { "interface",
required_argument, 0, 'i' }, { "header", required_argument,
0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file",
required_argument, 0, 'f' }, { "timeout", required_argument,
0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0,
0 }, };
{ "permanent", no_argument, 0, 's' }, {
"debug", optional_argument, 0, 'd' },
{ "password", required_argument, 0, 'P' }, {
"user", required_argument, 0, 'U' },
{ "interface", required_argument, 0, 'i' }, {
"header", required_argument, 0, 'b' },
{ "hex", required_argument, 0, 'x' }, {
"file", required_argument, 0, 'f' },
{ "timeout", required_argument, 0, 't' }, {
"wait", required_argument, 0, 'w' },
{ 0, 0, 0, 0 }, };
while ((opt = getopt_long(argc, argv, "bhrvVXIswxP:U:i:f:t:d::", longopts,
&index)) != -1) {
@ -152,7 +157,8 @@ int main(int argc, char *argv[]) {
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
} else if (optind < argc) {
}
else if (optind < argc) {
vector<string> v;
while (optind < argc)
v.push_back(argv[optind++]);

View file

@ -18,24 +18,24 @@ table::table(std::initializer_list<set> l) {
i++;
}
}
const table::set table::operator[](std::string s){
const table::set table::operator[](std::string s) {
return *this->right[s];
}
const table::set table::operator[](short n){
const table::set table::operator[](short n) {
return *this->left[n];
}
bool table::exists(std::string s){
bool table::exists(std::string s) {
return !(right.find(s) == right.end());
}
bool table::exists(short n){
bool table::exists(short n) {
return !(left.find(n) == left.end());
}
short table::type(std::string s){
short table::type(std::string s) {
return this->right[s]->type;
}
std::string table::id(short n){
std::string table::id(short n) {
return this->left[n]->id;
}
std::string table::name(short n){
std::string table::name(short n) {
return this->left[n]->name;
}

View file

@ -12,7 +12,7 @@
#include <vector>
class table {
public:
public:
enum A {
RW, RO, WO, NONE, UNKNOWN
};
@ -34,7 +34,7 @@ public:
short type(std::string);
std::string id(short);
std::string name(short);
private:
private:
std::vector<set> data;
std::map<short, set*> left;
std::map<std::string, set*> right;