This commit is contained in:
j3d1 2016-01-23 09:22:46 +01:00
parent d3b89e3b11
commit 8200103514
14 changed files with 415 additions and 356 deletions

View file

@ -15,7 +15,7 @@ $(BUILDDIR):
$(TARGET): $(OBJECTS)
$(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lpthread
$(CC) $^ -o $(BUILDDIR)/$@ -lboost_filesystem -lboost_system -lboost_program_options -lpthread
$(OBJECTS): $(BUILDDIR)/%.o : $(SOURCEDIR)/%.cpp

View file

@ -14,38 +14,40 @@
#define USAGE "usage: %s [-bdhjrvswx] [-i interface] [-u [password:]username]\n\
[-p password] <command>\n\n"
#define HELP "\
Option Summary:\n\
-h --help This help text\n\
-v --version Display version of this tool\n\
-d --debug [n] Show debugging messages\n\
-r switch ports to emulate switch while sniffing\n\
-b --header Show header\n\
-x --hex Display Packets as Hex String\n\
-j --json Display Packets as JSON\n\
-i --interface <iface> only use one Interface\n\
-u --user <[password:]username>\n\
-p --password <password>\n\
Login with Username and Password\n\
-f --file <path> choose a settings file\n\n\
-t --timeout <n> Timeout in milliseconds. Default: 180\n\
-I --interactive Not yet implemented\n\
-O --stdout 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\
Command Summary:\n\
help This help text\n\
list list all connected switches\n\
sniff [type:<type>] [<filter>]\n\
capture and display all incoming or outgoing packets\n\
depending on the --reverse option\n\
encode use encoding algorithm on hex data separated by colon\n\
get Not yet implemented\n\
set Not yet implemented\n\
get|save Not yet implemented: save config to file\n\
set|restore Not yet implemented: restore onfig from file\n\
flash Not yet implemented: replace firmware\n\
reboot Not yet implemented\n\
reset Not yet implemented\n\n\
Option Summary:\n\
-h --help This help text\n\
-V --version Display version of this tool\n\
-d --debug [n] Show debugging messages\n\
-v --verbose [n] Show debugging messages\n\
-r --reverse switch ports to emulate switch while sniffing\n\
-b --header Show header\n\
-x --hex Display Packets as Hex String\n\
-j --json Display Packets as JSON\n\
-i --interface <iface> only use one Interface\n\
-u --user <[password:]username>\n\
-p --password <password>\n\
Login with Username and Password\n\
-f --file <path> choose a settings file\n\n\
-t --timeout <n> Timeout in milliseconds. Default: 180\n\
-I --interactive Not yet implemented\n\
-O --stdout 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\
Command Summary:\n\
list list all connected switches\n\
sniff [type:<type>] [<filter>]\n\
capture and display all incoming or outgoing packets\n\
depending on the --reverse option\n\
encode use encoding algorithm on hex data separated by colon\n\
get <property1> [<property1> [...]]\n\
Not yet implemented\n\
set <property1>=<value> [<property2>=<value> [...]]\n\
Not yet implemented\n\
get|save Not yet implemented: save config to file\n\
set|restore Not yet implemented: restore onfig from file\n\
flash Not yet implemented: replace firmware\n\
reboot Not yet implemented\n\
reset Not yet implemented\n\n\
### for questions please contact <smrtlink@jdi.li> ###\n\n"
//TODO
/*
@ -54,13 +56,6 @@
*
*/
#define FLAG_HEX 1
#define FLAG_REVERSE 2
#define FLAG_HEADER 4
#define FLAG_PERMANENT 8
#define FLAG_WAIT 16
#define FLAG_DEBUG 32
extern Options options;
#endif /* OPTIONS_H_ */

View file

@ -18,8 +18,8 @@
#include "table.h"
int printHeader(Packet p) {
if (options.flags & FLAG_HEADER) {
if (options.flags & FLAG_HEX) {
if (options.flags.HEADER) {
if (options.flags.HEX) {
std::cout << "Received Header:\n\t" << p.getHead() << "\n";
} else {
p.printHeader();
@ -30,12 +30,12 @@ int printHeader(Packet p) {
}
int printPacket(Packet p) {
if (options.flags & FLAG_HEX) {
if (options.flags.HEX) {
std::cout << "Received Payload:\n\t" << p.getBody() << "\n";
} else {
for (dataset d : p.getPayload()) {
auto lookup =
(options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup;
(options.flags.REVERSE) ? snd_lookup : rcv_lookup;
if (lookup.exists(d.type)) {
table::set s = lookup[d.type];
if (d.len > 0) {
@ -92,7 +92,7 @@ int Program::list() {
std::cout << "List:\n";
discover([this](Packet a) {
printHeader(a);
if (options.flags & FLAG_HEX) {
if (options.flags.HEX) {
std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
} else {
datasets d =a.getPayload();
@ -102,13 +102,13 @@ int Program::list() {
f.write(sw.toString());
sw.print();
}
return 1;
return 0;
});
io_service->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 0;
}
int Program::sniff() {
@ -130,7 +130,7 @@ int Program::sniff() {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 0;
}
int Program::encode(std::string s) {
@ -141,7 +141,7 @@ int Program::encode(std::string s) {
return 0;
}
int Program::setProperty() {
int Program::setProperty(std::map<std::string,std::string> prop) {
try {
std::cout << "List:\n";
@ -158,7 +158,7 @@ int Program::setProperty() {
Switch sw = Switch();
sw.parse(d);
datasets t = { {REBOOT, 1, {0}}};
datasets t = { {LOOP_PREVENTION, 1, {0}}};
set(a,t,
[this](Packet a) {
@ -176,12 +176,11 @@ int Program::setProperty() {
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 0;
}
int Program::getProperty() {
int Program::getProperty(std::vector<std::string> prop) {
try {
std::cout << "List:\n";
discover([this](Packet a) {
@ -204,7 +203,7 @@ int Program::getProperty() {
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 0;
}
int Program::save() {
@ -212,7 +211,7 @@ int Program::save() {
sw.settings.hostname = "testname.lan";
File f;
f.write(sw.toString());
return 1;
return 0;
}
int Program::restore() {
@ -220,12 +219,12 @@ int Program::restore() {
Switch sw;
sw.parse(f.read());
sw.print();
return 1;
return 0;
}
int Program::flash() {
return 0;
return 1;
}
int Program::reboot() {
@ -254,12 +253,12 @@ int Program::reboot() {
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
return 0;
}
int Program::reset() {
return 0;
return 1;
}
int Program::discover(std::function<int(Packet)> c) {

View file

@ -14,6 +14,7 @@
#include "Host.h"
#include "Socket.h"
class Program {
private:
std::shared_ptr<boost::asio::io_service> io_service;
@ -31,8 +32,8 @@ public:
int list();
int sniff();
int encode(std::string);
int setProperty();
int getProperty();
int getProperty(std::vector<std::string>);
int setProperty(std::map<std::string,std::string>);
int save();
int restore();
int flash();

View file

@ -21,13 +21,13 @@ void Socket::init(short dst_port, short src_port) {
if (initialized)
return;
if (options.flags & FLAG_REVERSE) {
if (options.flags.REVERSE) {
short p = dst_port;
dst_port = src_port;
src_port = p;
}
if (options.flags & FLAG_DEBUG)
if (options.debug_level>=1)
std::cout << "Local IP:\t" << local_ip << "\n";
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(

View file

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

View file

@ -21,105 +21,113 @@
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;
}
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() {
*this= {0,0,0,0,0,0};
}
macAddr(std::initializer_list<byte> s) {
int i = 0;
macAddr(std::initializer_list<byte> s) {
int i = 0;
for (byte b : s) {
if(i<6) (*this)[i++]=b;
else break;
if(i<6) (*this)[i++]=b;
else break;
}
}
}
macAddr(bytes bts) {
int i = 0;
macAddr(bytes bts) {
int i = 0;
for (byte b : bts) {
if(i<6) (*this)[i++]=b;
else break;
if(i<6) (*this)[i++]=b;
else break;
}
}
}
};
/*
class mac_addr : public std::array<unsigned char, 6> {
public:
typedef std::array<unsigned char, 6> super;
class mac_addr : public std::array<unsigned char, 6> {
public:
typedef std::array<unsigned char, 6> super;
using super::super;
mac_addr{00, 00, 00, 000};
};
*/
using super::super;
mac_addr{00, 00, 00, 000};
};
*/
class ipAddr: public std::array<byte, 4> {
public:
ipAddr() {
*this= {0,0,0,0,0,0};
}
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;
ipAddr(std::initializer_list<byte> s) {
int i = 0;
for (byte b : s) {
if(i<4) (*this)[i++]=b;
else break;
}
}
}
ipAddr(bytes bts) {
int i = 0;
ipAddr(bytes bts) {
int i = 0;
for (byte b : bts) {
if(i<4) (*this)[i++]=b;
else break;
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;
}
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) {
std::vector<T> AB;
AB.reserve(A.size() + B.size()); // preallocate memory
AB.insert(AB.end(), A.begin(), A.end()); // add A;
AB.insert(AB.end(), B.begin(), B.end()); // add B;
return AB;
std::vector<T> AB;
AB.reserve(A.size() + B.size()); // preallocate memory
AB.insert(AB.end(), A.begin(), A.end()); // add A;
AB.insert(AB.end(), B.begin(), B.end()); // add B;
return AB;
}
template<typename T>
std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
A.reserve(A.size() + B.size());
A.insert(A.end(), B.begin(), B.end());
return A;
A.reserve(A.size() + B.size());
A.insert(A.end(), B.begin(), B.end());
return A;
}
struct Options {
unsigned flags = 0x00;
std::string user;
std::string password;
std::string interface;
std::string file;
int debug_level=0;
long timeout = 180U;
struct {
bool HEX;
bool JSON;
bool PLAIN;
bool REVERSE;
bool HEADER;
bool PERMANENT;
bool WAIT;
} flags;
std::string user;
std::string password;
std::string interface;
std::string file;
int debug_level = 0;
int verbosity = 0;
long timeout = 180U;
};
#endif /* TYPES_H_ */

View file

@ -1,13 +1,13 @@
enum {
#define LOOKUP_SET(id, num, type) RCV_ ## id = num,
#define LOOKUP_SET(id, name, num, type) RCV_ ## id = num,
#include "lookup/rcv.lst"
#include "lookup/general.lst"
#undef LOOKUP_SET
#define LOOKUP_SET(id, num, type) SND_ ## id = num,
#define LOOKUP_SET(id, name, num, type) SND_ ## id = num,
#include "lookup/snd.lst"
#include "lookup/general.lst"
#undef LOOKUP_SET
#define LOOKUP_SET(id, num, type) id = num,
#define LOOKUP_SET(id, name, num, type) id = num,
#include "lookup/rcv.lst"
#include "lookup/snd.lst"
#include "lookup/general.lst"
@ -15,14 +15,14 @@ enum {
};
static table rcv_lookup {
#define LOOKUP_SET(id, num, type) { RCV_ ## id, table::type, #id },
#define LOOKUP_SET(id, name, num, type) { RCV_ ## id, table::type, #name, #id },
#include "lookup/rcv.lst"
#include "lookup/general.lst"
#undef LOOKUP_SET
};
static table snd_lookup {
#define LOOKUP_SET(id, num, type) { SND_ ## id, table::type, #id },
#define LOOKUP_SET(id, name, num, type) { SND_ ## id, table::type, #name, #id },
#include "lookup/snd.lst"
#include "lookup/general.lst"
#undef LOOKUP_SET

View file

@ -1,25 +1,25 @@
LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports
LOOKUP_SET(PORTS, ports, 10, DEC) //+byte, maybe number of ports
LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //???
LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port
LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask??
LOOKUP_SET(IGMP_SNOOPING, igmp, 4352, HEX) //???
LOOKUP_SET(PORTS_SETTINGS, psetings, 4096, HEX) //+per port
LOOKUP_SET(PORT_TRUNK, trunk, 4608, HEX) //byte[5] last byte bitmask??
LOOKUP_SET(MTU_VLAN, 8192, HEX) //byte[2] first byte bool,second byte port id
LOOKUP_SET(PORT_VLAN_ENABLED, 8448, BOOL) //open page
LOOKUP_SET(PORT_VLAN, 8449, HEX)
LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC)
LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte
LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan
LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port
LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //????
LOOKUP_SET(MTU_VLAN, 8192,8192, HEX) //byte[2] first byte bool,second byte port id
LOOKUP_SET(PORT_VLAN_ENABLED, 8448,8448, BOOL) //open page
LOOKUP_SET(PORT_VLAN, 8449,8449, HEX)
LOOKUP_SET(PORT_VLAN_MAX, 8450,8450, DEC)
LOOKUP_SET(VLAN_ENABLED, 8704, 8704, BOOL) //+bool byte
LOOKUP_SET(VLAN, 8705,8705, HEX) //+one set per vlan
LOOKUP_SET(VLAN_PVID, 8706,8706, HEX) //+per port
LOOKUP_SET(VLAN_FOOOO, 8707,8707, DEC) //????
LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod
LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ???
LOOKUP_SET(STORM_CONTROL, 12800, HEX) //+per port ???
LOOKUP_SET(PORT_MIRROR, 16640, HEX) //byte[10] second byte port id???
LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ???
LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ???
LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte
LOOKUP_SET(QOS_BASIC_ENABLED, 12288,12288, BOOL) //+bool = QoS Mod
LOOKUP_SET(QOS_BASIC, 12289,12289, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_INGRESS, 12544,12544, HEX) //+per port ???
LOOKUP_SET(BW_CONTROL_EGRESS, 12545,12545, HEX) //+per port ???
LOOKUP_SET(STORM_CONTROL, 12800,12800, HEX) //+per port ???
LOOKUP_SET(PORT_MIRROR, 16640,16640, HEX) //byte[10] second byte port id???
LOOKUP_SET(PORT_STATISTICS, 16384,16384, HEX) //+per port ???
LOOKUP_SET(CABLE_TEST, 16896,16896, HEX) //+per port ???
LOOKUP_SET(LOOP_PREVENTION, 17152,17152, BOOL) //+bool byte

View file

@ -1,13 +1,13 @@
LOOKUP_SET(TYPE, 1, STRING) //+string
LOOKUP_SET(HOSTNAME, 2, STRING) //+string
LOOKUP_SET(MAC, 3, HEX) //+byte[6]
LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4]
LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4]
LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4]
LOOKUP_SET(FIRMWARE_VERSION, 7, STRING)
LOOKUP_SET(HARDWARE_VERSION, 8, STRING)
LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte
LOOKUP_SET(TYPE,type, 1, STRING) //+string
LOOKUP_SET(HOSTNAME,host, 2, STRING) //+string
LOOKUP_SET(MAC,MAC, 3, HEX) //+byte[6]
LOOKUP_SET(IP_ADDR,ip, 4, DEC) //+byte[4]
LOOKUP_SET(IP_MASK,mask, 5, DEC) //+byte[4]
LOOKUP_SET(GATEWAY,gateway, 6, DEC) //+byte[4]
LOOKUP_SET(FIRMWARE_VERSION,FIRMWARE_VERSION, 7, STRING)
LOOKUP_SET(HARDWARE_VERSION,HARDWARE_VERSION, 8, STRING)
LOOKUP_SET(DHCP_ENABLED,dhcp, 9, BOOL) //+bool byte
LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX)
LOOKUP_SET(PORT_VLAN_FOOOOO, 2101,2101, HEX)

View file

@ -1,17 +1,17 @@
LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo
LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo
LOOKUP_SET(SYSTEM_INFO,SYSTEM_INFO, 2, HEX) //page sysinfo
LOOKUP_SET(IP_CONFIG,IP_CONFIG, 9, HEX) //page sysinfo
LOOKUP_SET(LOGIN_USER, 512, STRING) //string
LOOKUP_SET(NEW_USER, 513, STRING) //string
LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string
LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string
LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config
LOOKUP_SET(LOGIN_USER,LOGIN_USER, 512, STRING) //string
LOOKUP_SET(NEW_USER,NEW_USER, 513, STRING) //string
LOOKUP_SET(LOGIN_PASSWORD,LOGIN_PASSWORD, 514, STRING) //string
LOOKUP_SET(NEW_PASSWORD,NEW_PASSWORD, 515, STRING) //string
LOOKUP_SET(REBOOT,REBOOT, 773, BOOL) //bool byte = save config
LOOKUP_SET(RESET, 1280, ACTION) //
LOOKUP_SET(FLASH, 1536, ACTION) //update firmware
LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan
LOOKUP_SET(SAVE, 2304, ACTION) //save
LOOKUP_SET(PING, 2305, ACTION) //sent before SET ???
LOOKUP_SET(RESET,RESET, 1280, ACTION) //
LOOKUP_SET(FLASH,FLASH, 1536, ACTION) //update firmware
LOOKUP_SET(VLAN_FOOOOOO, 2200,2200, HEX) //vlan
LOOKUP_SET(SAVE,SAVE, 2304, ACTION) //save
LOOKUP_SET(PING,PING, 2305, ACTION) //sent before SET ???

View file

@ -1,15 +1,17 @@
//============================================================================
// Name : smrtlink.cpp
// Author : jdi
// Version :
// Version : 1.1
// Copyright : GPL v2
// Description : SmrtLink in C++, Ansi-style
//============================================================================
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <regex>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <cstdio>
@ -20,202 +22,232 @@
#include "Host.h"
#include "Program.h"
#include "Switch.h"
#include "lookup.h"
#define no_argument 0
#define required_argument 1
#define optional_argument 2
Options options;
using namespace std;
//Options options;
constexpr unsigned int caseArg(const char* str, int h = 0) {
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
}
int main(int argc, char *argv[]) {
int opt, index;
int opt, index;
options.user = DEFAULT_USER;
options.password = DEFAULT_PASS;
options.user = DEFAULT_USER;
options.password = DEFAULT_PASS;
const struct option longopts[] = { { "version", 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 }, };
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 }, };
Program p = Program();
Program p = Program();
while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts,
&index)) != -1) {
switch (opt) {
while ((opt = getopt_long(argc, argv, "bhrvVswxp:u:i:f:t:d::", longopts,
&index)) != -1) {
switch (opt) {
case 'h':
fprintf(stderr, VERSION);
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP);
exit(EXIT_SUCCESS);
break;
case 'h':
fprintf(stderr, VERSION);
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP);
exit(EXIT_SUCCESS);
break;
case 'v':
fprintf(stderr, VERSION);
exit(EXIT_SUCCESS);
break;
case 'V':
fprintf(stderr, VERSION);
exit(EXIT_SUCCESS);
break;
case 'r':
options.flags |= FLAG_REVERSE;
break;
case 'r':
options.flags.REVERSE = true;
break;
case 'b':
options.flags |= FLAG_HEADER;
break;
case 'b':
options.flags.HEADER = true;
break;
case 'x':
options.flags |= FLAG_HEX;
break;
case 'x':
options.flags.HEX = true;
break;
case 's':
options.flags |= FLAG_PERMANENT;
break;
case 's':
options.flags.PERMANENT = true;
break;
case 'w':
options.flags |= FLAG_WAIT;
break;
case 'w':
options.flags.WAIT = true;
break;
case 'd':
options.flags |= FLAG_DEBUG;
if (optarg != NULL)
options.debug_level = atoi(optarg);
break;
case 'v':
if (optarg != NULL)
options.verbosity = atoi(optarg);
else
options.verbosity++;
break;
case 't':
options.timeout = atol(optarg);
break;
case 'd':
if (optarg != NULL)
options.debug_level = atoi(optarg);
else
options.debug_level++;
break;
case 'f':
options.file = std::string(optarg);
break;
case 't':
options.timeout = atol(optarg);
break;
case 'p':
options.password = std::string(optarg);
break;
case 'f':
options.file = std::string(optarg);
break;
case 'u':
options.user = std::string(optarg);
break;
case 'p':
options.password = std::string(optarg);
break;
case 'i':
options.interface = std::string(optarg);
break;
case 'u':
options.user = std::string(optarg);
break;
default: /* '?' */
fprintf(stderr, "Unknown option\n");
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
case 'i':
options.interface = std::string(optarg);
break;
/*//TODO stdin
std::ostringstream bucket;
bucket << std::cin.rdbuf();
p.input = bucket.str();
*/
default: /* '?' */
fprintf(stderr, "Unknown option\n");
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
fprintf(stderr, "Command expected\n");
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
/*//TODO stdin
std::ostringstream bucket;
bucket << std::cin.rdbuf();
p.input = bucket.str();
*/
p.init();
if (optind >= argc) {
fprintf(stderr, "Command expected\n");
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
if (optind < argc) {
std::string cmd = std::string(argv[optind++]);
p.init();
std::vector<std::string> vect;
std::map<std::string, std::string> list;
std::cmatch sm;
switch (caseArg(cmd.c_str())) {
case caseArg("set"):
if (p.setProperty())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("reboot"):
if (p.reboot())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("reset"):
if (p.reset())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("save"):
if (p.save())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("restore"):
if (p.restore())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("flash"):
if (p.flash())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
if (optind < argc) {
std::string cmd = std::string(argv[optind++]);
switch (caseArg(cmd.c_str())) {
case caseArg("reboot"):
if (!p.reboot())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("reset"):
if (!p.reset())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("save"):
if (!p.save())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("restore"):
if (!p.restore())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("flash"):
if (!p.flash())
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("get"):
if (p.getProperty())
exit(EXIT_SUCCESS);
break;
case caseArg("list"):
if (!p.list())
exit(EXIT_SUCCESS);
break;
case caseArg("list"):
if (p.list())
exit(EXIT_SUCCESS);
break;
case caseArg("sniff"):
if (!p.sniff())
exit(EXIT_SUCCESS);
break;
case caseArg("sniff"):
if (p.sniff())
exit(EXIT_SUCCESS);
break;
case caseArg("encode"):
if (optind < argc) {
std::string s(argv[optind]);
optind++;
if (p.encode(s))
exit(EXIT_SUCCESS);
} else {
fprintf(stderr, "Argument expected after encode\n");
exit(EXIT_FAILURE);
}
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", cmd.c_str());
while (optind < argc) {
printf("->%s\n", argv[optind]);
optind++;
}
exit(EXIT_FAILURE);
}
}
exit(EXIT_FAILURE);
case caseArg("encode"):
if (optind < argc) {
std::string s(argv[optind]);
optind++;
if (p.encode(s))
exit(EXIT_SUCCESS);
} else {
fprintf(stderr, "Argument expected after encode\n");
exit(EXIT_FAILURE);
}
break;
case caseArg("set"):
while (optind < argc) {
if (regex_match(argv[optind], sm,
std::regex("^([a-z]+)=(.*)$"))) {
if (!snd_lookup.exists(sm[1])) {
cerr << "Unknown argument " << argv[optind] << endl;
exit(EXIT_FAILURE);
}
list.insert(
std::pair<std::string, std::string>(sm[1], sm[2]));
} else {
cerr << "Invalid Syntax " << argv[optind] << endl;
exit(EXIT_FAILURE);
}
optind++;
}
if (!p.setProperty(list))
exit(EXIT_SUCCESS);
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
break;
case caseArg("get"):
while (optind < argc) {
if (regex_match(argv[optind], sm, std::regex("^([a-z]+)$"))) {
if (!snd_lookup.exists(sm[1])) {
cerr << "Unknown argument " << argv[optind] << endl;
exit(EXIT_FAILURE);
}
vect.push_back(sm[1]);
} else {
cerr << "Invalid argument " << argv[optind] << endl;
exit(EXIT_FAILURE);
}
optind++;
}
if (!p.getProperty(vect))
exit(EXIT_SUCCESS);
break;
default:
printf("Unknown command: %s\n", cmd.c_str());
exit(EXIT_FAILURE);
}
}
exit(EXIT_FAILURE);
}

View file

@ -8,18 +8,35 @@
#include <string>
#include "table.h"
table::table(std::initializer_list<set> l) {
int i = 0;
for (set s : l) {
this->left[s.type] = s;
}
int i = 0;
this->data.resize(l.size());
for (set s : l) {
this->data[i] = s;
this->left[s.type] = &this->data[i];
this->right[s.name] = &this->data[i];
i++;
}
}
table::set table::operator[](short n){
return this->left[n];
const table::set table::operator[](std::string s){
return *this->right[s];
}
const table::set table::operator[](short n){
return *this->left[n];
}
bool table::exists(std::string s){
return !(right.find(s) == right.end());
}
bool table::exists(short n){
return !(left.find(n) == left.end());
return !(left.find(n) == left.end());
}
short table::type(std::string s){
return this->right[s]->type;
}
std::string table::id(short n){
return this->left[n]->id;
}
std::string table::name(short n){
return this->left[n].name;
return this->left[n]->name;
}

View file

@ -17,14 +17,21 @@ public:
struct set {
short type;
F format;
std::string name;
std::string name;
std::string id;
};
table(std::initializer_list<set> l);
set operator[](short);
bool exists(short);
std::string name(short);
table(std::initializer_list<set> l);
const table::set operator[](std::string);
const table::set operator[](short);
bool exists(std::string);
bool exists(short);
short type(std::string);
std::string id(short);
std::string name(short);
private:
std::map<short, set> left;
std::vector<set> data;
std::map<short, set*> left;
std::map<std::string, set*> right;
};
#endif /* LOOKUPTABLE_H_ */