some fixes

This commit is contained in:
/jdi/ 2015-10-01 01:36:09 +02:00
parent 4c16611390
commit 30ebdde28e
13 changed files with 162 additions and 85 deletions

View file

@ -25,16 +25,16 @@ Host::Host() {
} }
byteArray<6> Host::getMac() { macAddr Host::getMac() {
byteArray<6> ret = { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c }; macAddr ret { { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c } };
//TODO find actual MAC Address //TODO find actual MAC Address
return ret; return ret;
} }
byteArray<4> Host::getIp(std::string iface) { inetAddr Host::getIp(std::string iface) {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int n; int n;
byteArray<4> data = { 0, 0, 0, 0 }; inetAddr data { { 0, 0, 0, 0 } };
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs"); perror("getifaddrs");

View file

@ -13,8 +13,8 @@
class Host { class Host {
public: public:
Host(); Host();
byteArray<6> getMac(); macAddr getMac();
byteArray<4> getIp(std::string); inetAddr getIp(std::string);
}; };
#endif /* HOST_H_ */ #endif /* HOST_H_ */

View file

@ -26,14 +26,18 @@
-p Password\n\ -p Password\n\
-f --file Not yet implemented:.choose a settings file\n\ -f --file Not yet implemented:.choose a settings file\n\
-t --timeout Not yet implemented\n\ -t --timeout Not yet implemented\n\
-w --wait Not yet implemented: blocking until operation is completed\n\
-s --permanent Not yet implemented: make changes immediately permanent\n\n\ -s --permanent Not yet implemented: make changes immediately permanent\n\n\
Command Summary:\n\ Command Summary:\n\
help This help text\n\ help This help text\n\
list list all connected switches\n\ list list all connected switches\n\
sniff capture and display all incoming or outgoing packets\n\ sniff capture and display all incoming or outgoing packets\n\
depending on the --reverse option\n\ depending on the --reverse option\n\
encode use encoding algorithm on hex data separated by colon\n\
get Not yet implemented\n\ get Not yet implemented\n\
set Not yet implemented\n\ set Not yet implemented\n\
save Not yet implemented: save config to file\n\
restore Not yet implemented: restore onfig from file\n\
flash Not yet implemented: replace firmware\n\ flash Not yet implemented: replace firmware\n\
reboot Not yet implemented\n\ reboot Not yet implemented\n\
reset Not yet implemented\n\n" reset Not yet implemented\n\n"
@ -41,7 +45,8 @@
#define FLAG_HEX 1 #define FLAG_HEX 1
#define FLAG_REVERSE 2 #define FLAG_REVERSE 2
#define FLAG_HEADER 4 #define FLAG_HEADER 4
#define FLAG_PERMANENT 4 #define FLAG_PERMANENT 8
#define FLAG_WAIT 16
extern Options options; extern Options options;

View file

@ -103,7 +103,7 @@ void Packet::setBody(bytes data) {
this->body = data; this->body = data;
} }
void Packet::setHostMac(byteArray<6> mac) { void Packet::setHostMac(macAddr mac) {
this->hostMac = mac; this->hostMac = mac;
} }
@ -127,11 +127,11 @@ void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId; this->sequenceId = sequenceId;
} }
const byteArray<6>& Packet::getSwitchMac() const { macAddr Packet::getSwitchMac() const {
return switchMac; return switchMac;
} }
void Packet::setSwitchMac(byteArray<6> switchMac) { void Packet::setSwitchMac(macAddr switchMac) {
this->switchMac = switchMac; this->switchMac = switchMac;
} }
@ -187,12 +187,12 @@ void Packet::encode(bytes &data) {
t = s[i]; t = s[i];
s[i] = s[j]; s[i] = s[j];
s[j] = t; s[j] = t;
data[k] = ((data[k] ^ s[(s[i] + s[j]) % 256])); data[k] = data[k] ^ s[(s[i] + s[j]) % 256];
} }
} }
void Packet::push(bytes &arr, int &index, byte data) { void Packet::push(bytes &arr, int &index, byte data) {
if (arr.size() > index) { if (arr.size() > (unsigned) index) {
arr[index++] = data; arr[index++] = data;
} else { } else {
arr.push_back(data); arr.push_back(data);
@ -200,15 +200,18 @@ void Packet::push(bytes &arr, int &index, byte data) {
} }
} }
void Packet::push(bytes &arr, int &index, bytes data) { void Packet::push(bytes &arr, int &index, bytes data) {
for (unsigned j = 0; j < data.size(); j++) for (unsigned j = 0; j < data.size(); j++)
push(arr, index, data[j]); push(arr, index, data[j]);
} }
template<size_t N> void Packet::push(bytes &arr, int &index, inetAddr &data) {
void Packet::push(bytes &arr, int &index, byteArray<N> data) { for (unsigned j = 0; j < 4; j++)
for (unsigned j = 0; j < N; j++) push(arr, index, data[j]);
}
void Packet::push(bytes &arr, int &index, macAddr &data) {
for (unsigned j = 0; j < 6; j++)
push(arr, index, data[j]); push(arr, index, data[j]);
} }
@ -246,10 +249,14 @@ void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned len) {
index += len; index += len;
} }
template<size_t N> void Packet::pull(bytes &arr, int &index, macAddr &ret) {
void Packet::pull(bytes &arr, int &index, byteArray<N> &ret) { memcpy(&ret[0], &arr[index], 6);
memcpy(&ret[0], &arr[index], N); index += 6;
index += N; }
void Packet::pull(bytes &arr, int &index, inetAddr &ret) {
memcpy(&ret[0], &arr[index], 4);
index += 4;
} }
void Packet::pull(bytes &arr, int &index, short &ret) { void Packet::pull(bytes &arr, int &index, short &ret) {

View file

@ -29,13 +29,13 @@ public:
short getLength() const; short getLength() const;
int getCheckSum() const; int getCheckSum() const;
short getSequenceId() const; short getSequenceId() const;
const byteArray<6>& getSwitchMac() const; macAddr getSwitchMac() const;
const bytes& getBody() const; const bytes& getBody() const;
const bytes& getHead() const; const bytes& getHead() const;
const datasets& getPayload() const; const datasets& getPayload() const;
void setBody(bytes); void setBody(bytes);
void setHostMac(byteArray<6>); void setHostMac(macAddr);
void setSwitchMac(byteArray<6>); void setSwitchMac(macAddr);
void setCheckSum(int); void setCheckSum(int);
void setSequenceId(short); void setSequenceId(short);
void setPayload(const datasets& payload); void setPayload(const datasets& payload);
@ -47,9 +47,9 @@ private:
byte version = 1; byte version = 1;
byte opCode; byte opCode;
byteArray<6> switchMac = { 0, 0, 0, 0, 0, 0 }; macAddr switchMac {{ 0, 0, 0, 0, 0, 0 }};
byteArray<6> hostMac = { 0, 0, 0, 0, 0, 0 }; macAddr hostMac {{ 0, 0, 0, 0, 0, 0 }};
byteArray<6> broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; macAddr broadcastMac {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }};
short sequenceId; short sequenceId;
short tokenId = 0; short tokenId = 0;
short fragmentOffset = 0; short fragmentOffset = 0;
@ -64,12 +64,16 @@ private:
void push(bytes&, int&, int); void push(bytes&, int&, int);
void push(bytes&, int&, byte); void push(bytes&, int&, byte);
void push(bytes&, int&, bytes); void push(bytes&, int&, bytes);
void push(bytes&, int&, inetAddr&);
void push(bytes&, int&, macAddr&);
void push(bytes&, int&, dataset); void push(bytes&, int&, dataset);
void pull(bytes&, int&, short&); void pull(bytes&, int&, short&);
void pull(bytes&, int&, int&); void pull(bytes&, int&, int&);
void pull(bytes&, int&, byte&); void pull(bytes&, int&, byte&);
void pull(bytes&, int&, bytes&, unsigned); void pull(bytes&, int&, bytes&, unsigned);
void pull(bytes&, int&, inetAddr&);
void pull(bytes&, int&, macAddr&);
void pull(bytes&, int&, dataset&); void pull(bytes&, int&, dataset&);
}; };

View file

@ -71,13 +71,13 @@ int Program::sniff() {
Host h = Host(); Host h = Host();
Socket s(io_service); Socket s(io_service);
s.setHostIp(h.getIp(options.interface)); s.setHostIp(h.getIp(options.interface));
s.init(DST_PORT,SRC_PORT); s.init(DST_PORT, SRC_PORT);
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) {
printf("Received Header:\t"); printf("Received Header:\t");
utils::printHex(p.getHead()); utils::printHex(p.getHead());
}else{ } else {
p.printHeader(); p.printHeader();
} }
printf("\n"); printf("\n");
@ -107,29 +107,43 @@ int Program::sniff() {
return 1; return 1;
} }
int Program::encode(std::string s){ int Program::encode(std::string s) {
std::string delimiter = ":"; bytes d = utils::readHex(s);
std::string token;
size_t pos = 0;
bytes arr = { };
int hex;
byte b;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
sscanf(token.c_str(), "%x", &hex);
s.erase(0, pos + delimiter.length());
b = hex & 0xFF;
arr.push_back(b);
}
sscanf(s.c_str(), "%x", &hex);
b = hex & 0xFF;
arr.push_back(b);
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::DISCOVERY);
p.encode(arr); p.encode(d);
printf("%x", arr[0]); printf("%x", d[0]);
for (unsigned i = 1; i < arr.size(); i++) { for (unsigned i = 1; i < d.size(); i++) {
printf(":%x", arr[i]); printf(":%x", d[i]);
} }
printf("\n"); printf("\n");
return 0;
}
int Program::setProperty(){
return 0;
}
int Program::getProperty(){
return 0;
}
int Program::save(){
return 0;
}
int Program::restore(){
return 0;
}
int Program::flash(){
return 0;
}
int Program::reboot(){
return 0;
}
int Program::reset(){
return 0;
} }

View file

@ -21,6 +21,13 @@ public:
int list(); int list();
int sniff(); int sniff();
int encode(std::string); int encode(std::string);
int setProperty();
int getProperty();
int save();
int restore();
int flash();
int reboot();
int reset();
}; };
#endif /* PROGRAM_H_ */ #endif /* PROGRAM_H_ */

View file

@ -31,7 +31,7 @@ void Socket::init(short dst_port, short src_port) {
} }
printf("IP:\t"); printf("IP:\t");
utils::printDec(local_ip); utils::print(local_ip);
printf("\n"); printf("\n");
wildcard_endpoint_ = asio::ip::udp::endpoint( wildcard_endpoint_ = asio::ip::udp::endpoint(
@ -53,7 +53,7 @@ void Socket::init(short dst_port, short src_port) {
} }
void Socket::setHostIp(bytes ip) { void Socket::setHostIp(inetAddr ip) {
local_ip=ip; local_ip=ip;
} }

View file

@ -22,7 +22,7 @@ public:
void init(short, short); void init(short, short);
void send(bytes); void send(bytes);
void listen(); void listen();
void setHostIp(bytes); void setHostIp(inetAddr);
int (*callback)(Packet)=[](Packet a) { int (*callback)(Packet)=[](Packet a) {
return 0; return 0;
}; };
@ -36,7 +36,7 @@ private:
asio::ip::udp::endpoint wildcard_endpoint_; asio::ip::udp::endpoint wildcard_endpoint_;
asio::ip::udp::endpoint local_endpoint_; asio::ip::udp::endpoint local_endpoint_;
bytes data = bytes(MAX_LENGTH); bytes data = bytes(MAX_LENGTH);
byteArray<4> local_ip = bytes(4); inetAddr local_ip;
}; };

View file

@ -37,16 +37,16 @@ private:
std::string type; std::string type;
std::string hardware_version; std::string hardware_version;
std::string firmware_version; std::string firmware_version;
byteArray<6> mac; macAddr mac;
} device; } device;
struct { struct {
std::string password = DEFAULT_PASS; std::string password = DEFAULT_PASS;
std::string username = DEFAULT_USER; std::string username = DEFAULT_USER;
struct { struct {
std::string hostname; std::string hostname;
byteArray<4> ip_addr; inetAddr ip_addr;
byteArray<4> ip_mask; inetAddr ip_mask;
byteArray<4> gateway; inetAddr gateway;
byte dhcp; byte dhcp;
} network; } network;
} settings; } settings;

View file

@ -29,8 +29,9 @@ std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
return A; return A;
} }
template<size_t N> typedef std::array<unsigned char, 6> macAddr;
using byteArray = std::array<unsigned char, N>; typedef std::array<unsigned char, 4> inetAddr;
typedef std::vector<unsigned char> bytes; typedef std::vector<unsigned char> bytes;
typedef unsigned char byte; typedef unsigned char byte;

View file

@ -14,6 +14,26 @@
namespace utils { 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) { static void printHex(bytes d) {
if (d.size() > 0) if (d.size() > 0)
printf("%.2X", d[0]); printf("%.2X", d[0]);
@ -22,14 +42,14 @@ static void printHex(bytes d) {
} }
} }
static void print(byteArray<6> d) { static void print(macAddr d) {
printf("%.2X", d[0]); printf("%.2X", d[0]);
for (unsigned i = 1; i < 6; i++) { for (unsigned i = 1; i < 6; i++) {
printf(":%.2X", d[i]); printf(":%.2X", d[i]);
} }
} }
static void print(byteArray<4> d) { static void print(inetAddr d) {
printf("%.1d", d[0]); printf("%.1d", d[0]);
for (unsigned i = 1; i < 4; i++) { for (unsigned i = 1; i < 4; i++) {
printf(".%.1d", d[i]); printf(".%.1d", d[i]);

View file

@ -3,7 +3,7 @@
// Author : jdi // Author : jdi
// Version : // Version :
// Copyright : GPL v2 // Copyright : GPL v2
// Description : Hello World in C++, Ansi-style // Description : SmrtLink in C++, Ansi-style
//============================================================================ //============================================================================
#include <cstring> #include <cstring>
@ -25,6 +25,10 @@
Options options; Options options;
constexpr unsigned int caseArg(const char* str, int h = 0) {
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int opt, index; int opt, index;
@ -35,7 +39,8 @@ int main(int argc, char *argv[]) {
required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, { required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, {
"header", required_argument, 0, 'b' }, { "hex", required_argument, "header", required_argument, 0, 'b' }, { "hex", required_argument,
0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout", 0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout",
required_argument, 0, 't' }, { 0, 0, 0, 0 }, }; required_argument, 0, 't' }, { "wait",
required_argument, 0, 'w' }, { 0, 0, 0, 0 }, };
Program p = Program(); Program p = Program();
@ -71,6 +76,10 @@ int main(int argc, char *argv[]) {
options.flags |= FLAG_PERMANENT; options.flags |= FLAG_PERMANENT;
break; break;
case 'w':
options.flags |= FLAG_WAIT;
break;
case 't': case 't':
options.timeout = atoi(optarg); options.timeout = atoi(optarg);
break; break;
@ -105,29 +114,31 @@ int main(int argc, char *argv[]) {
} }
if (optind < argc) { if (optind < argc) {
if (strcmp(argv[optind], "help") == 0) { std::string cmd = std::string(argv[optind++]);
fprintf(stderr, VERSION);
fprintf(stderr, USAGE, argv[0]); switch (caseArg(cmd.c_str())) {
fprintf(stderr, HELP); case caseArg("get"):
exit(EXIT_SUCCESS); case caseArg("set"):
} else if (strcmp(argv[optind], "get") == 0 case caseArg("reboot"):
|| strcmp(argv[optind], "set") == 0 case caseArg("reset"):
|| strcmp(argv[optind], "reboot") == 0 case caseArg("save"):
|| strcmp(argv[optind], "reset") == 0 case caseArg("restore"):
|| strcmp(argv[optind], "flash") == 0) { case caseArg("flash"):
optind++;
fprintf(stderr, "Not yet implemented.\n"); fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (strcmp(argv[optind], "list") == 0) { break;
optind++;
case caseArg("list"):
if (p.list()) if (p.list())
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (strcmp(argv[optind], "sniff") == 0) { break;
optind++;
case caseArg("sniff"):
if (p.sniff()) if (p.sniff())
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (strcmp(argv[optind], "encode") == 0) { break;
optind++;
case caseArg("encode"):
if (optind < argc) { if (optind < argc) {
std::string s(argv[optind]); std::string s(argv[optind]);
optind++; optind++;
@ -137,7 +148,16 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Argument expected after encode\n"); fprintf(stderr, "Argument expected after encode\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { break;
case caseArg("help"):
fprintf(stderr, VERSION);
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP);
exit(EXIT_SUCCESS);
break;
default:
printf("Unknown command: %s\n", argv[optind]); printf("Unknown command: %s\n", argv[optind]);
optind++; optind++;
while (optind < argc) { while (optind < argc) {
@ -148,6 +168,5 @@ int main(int argc, char *argv[]) {
} }
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }