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::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) { if (in) {
std::string contents; std::string contents;
in.seekg(0, std::ios::end); in.seekg(0, std::ios::end);

View file

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

View file

@ -22,7 +22,8 @@ public:
bool operator<(const Filter) const; bool operator<(const Filter) const;
private: private:
Packet::OpCode oc = Packet::NONE; 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 tokenId = -1;
short fragmentOffset = -1; short fragmentOffset = -1;
}; };

View file

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

View file

@ -12,7 +12,8 @@
class Host { class Host {
public: public:
Host(){}; Host() {
}
macAddr getMac(); macAddr getMac();
ipAddr getIp(); ipAddr getIp();
std::string getIface(); std::string getIface();

View file

@ -168,24 +168,25 @@ std::string Packet::opCodeToString() {
void Packet::encode(bytes &data) { void Packet::encode(bytes &data) {
int len = data.size(); int len = data.size();
bytes key = { 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, bytes key = {
76, 189, 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, 76, 189,
188, 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, 188,
213, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, 213,
206, 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, 206,
208, 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, 208,
113, 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, 113,
75, 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, 75,
166, 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, 166,
255, 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, 255,
185, 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, 185,
50, 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, 50,
38, 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, 38,
195, 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, 195,
19, 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, 19,
65, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, 65,
102, 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, 102,
159, 221, 244, 110, 119, 48 }; 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, 159,
221, 244, 110, 119, 48 };
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++) {
@ -199,7 +200,8 @@ void Packet::encode(bytes &data) {
void Packet::push(bytes &arr, int &index, byte data) { void Packet::push(bytes &arr, int &index, byte data) {
if (arr.size() > (unsigned) index) { if (arr.size() > (unsigned) index) {
arr[index++] = data; arr[index++] = data;
} else { }
else {
arr.push_back(data); arr.push_back(data);
index++; index++;
} }

View file

@ -51,9 +51,12 @@ private:
datasets payload; datasets payload;
byte version = 1; byte version = 1;
byte opCode; byte opCode;
macAddr switchMac { { 0, 0, 0, 0, 0, 0 } }; macAddr switchMac {
macAddr hostMac { { 0, 0, 0, 0, 0, 0 } }; { 0, 0, 0, 0, 0, 0 } };
macAddr broadcastMac { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } }; macAddr hostMac {
{ 0, 0, 0, 0, 0, 0 } };
macAddr broadcastMac {
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };
short tokenId = 0; short tokenId = 0;
short fragmentOffset = 0; short fragmentOffset = 0;
int errorCode = 0; int errorCode = 0;

View file

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

View file

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

View file

@ -76,14 +76,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 };
int ports; int ports;
} 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;
bool dhcp; bool dhcp;

View file

@ -24,15 +24,16 @@ public:
using vector::operator[]; using vector::operator[];
bytes() { 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'); this->push_back('\0');
} }
bytes(std::initializer_list<uint8_t> s) bytes(std::initializer_list<uint8_t> s) {
{
for (uint8_t b : s) { for (uint8_t b : s) {
this->push_back(b); this->push_back(b);
} }
@ -71,7 +72,8 @@ public:
<< (unsigned) arr[0]; << (unsigned) arr[0];
} }
for (unsigned i = 1; i < arr.size(); i++) { 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; return out;
} }

View file

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

View file

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