reformat
This commit is contained in:
parent
f15abc71be
commit
1f3730ac87
26 changed files with 880 additions and 844 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -22,7 +22,8 @@ public:
|
|||
bool operator<(const Filter) const;
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
|
||||
class Host {
|
||||
public:
|
||||
Host(){};
|
||||
Host() {
|
||||
}
|
||||
macAddr getMac();
|
||||
ipAddr getIp();
|
||||
std::string getIface();
|
||||
|
|
|
|||
|
|
@ -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++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,9 +51,12 @@ private:
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -64,7 +64,8 @@ void Socket::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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
12
src/bytes.h
12
src/bytes.h
|
|
@ -24,15 +24,16 @@ 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,7 +72,8 @@ 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ struct dataset {
|
|||
|
||||
class datasets: public std::vector<dataset> {
|
||||
public:
|
||||
datasets(){};
|
||||
datasets(std::initializer_list<dataset> s)
|
||||
{
|
||||
datasets() {
|
||||
}
|
||||
datasets(std::initializer_list<dataset> s) {
|
||||
for (dataset b : s) {
|
||||
//(*this)[b.type]=b;
|
||||
push_back(b);
|
||||
|
|
|
|||
|
|
@ -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++]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue