some changes

This commit is contained in:
/jdi/ 2015-09-30 00:01:23 +02:00
parent 5544792bfb
commit aca82979c3
13 changed files with 102 additions and 79 deletions

View file

@ -1,18 +0,0 @@
/*
* Device.cpp
*
* Created on: 01.09.2015
* Author: jdi
*/
#include "Device.h"
Device::Device() {
// TODO Auto-generated constructor stub
}
Device::~Device() {
// TODO Auto-generated destructor stub
}

View file

@ -1,23 +0,0 @@
/*
* Device.h
*
* Created on: 01.09.2015
* Author: jdi
*/
#ifndef DEVICE_H_
#define DEVICE_H_
#include <string>
class Device {
public:
Device();
virtual ~Device();
int getName(void) {
return 5;
}
};
#endif /* DEVICE_H_ */

View file

@ -25,14 +25,16 @@ Host::Host() {
} }
bytes Host::getMac() { byteArray<6> Host::getMac() {
return {0x08,0x3e,0x8e,0x16,0x17,0x2c}; byteArray<6> ret = { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c };
//TODO find actual MAC Address
return ret;
} }
bytes Host::getIp(std::string iface) { byteArray<4> Host::getIp(std::string iface) {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int n; int n;
bytes data = { 0, 0, 0, 0 }; byteArray<4> data = { 0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs"); perror("getifaddrs");
@ -45,7 +47,6 @@ bytes Host::getIp(std::string iface) {
if (ifa->ifa_addr->sa_family == AF_INET) { if (ifa->ifa_addr->sa_family == AF_INET) {
if (iface.compare(ifa->ifa_name) == 0) { if (iface.compare(ifa->ifa_name) == 0) {
data.resize(4);
memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
return data; return data;
} }

View file

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

View file

@ -14,6 +14,7 @@
#define USAGE "usage: %s [-bhrvx] [-i interface] [-u [password:]username]\n\ #define USAGE "usage: %s [-bhrvx] [-i interface] [-u [password:]username]\n\
[-p password] <command>\n\n" [-p password] <command>\n\n"
#define HELP "\ #define HELP "\
### for questions please contact <smrtlink@jdi.li> ###\n\
Option Summary:\n\ Option Summary:\n\
-h --help This help text\n\ -h --help This help text\n\
-v --version Display version of this tool\n\ -v --version Display version of this tool\n\
@ -22,16 +23,25 @@
-x --hex Display Packets as Hex String\n\ -x --hex Display Packets as Hex String\n\
-i --interface only use one Interface\n\ -i --interface only use one Interface\n\
-u --user Login with user\n\ -u --user Login with user\n\
-p Password\n\n\ -p Password\n\
-f --file Not yet implemented:.choose a settings file\n\
-t --timeout Not yet implemented\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\n" depending on the --reverse option\n\
get Not yet implemented\n\
set Not yet implemented\n\
flash Not yet implemented: replace firmware\n\
reboot Not yet implemented\n\
reset Not yet implemented\n\n"
#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
extern Options options; extern Options options;

View file

@ -20,14 +20,16 @@ Packet::Packet(OpCode c) {
} }
void Packet::printHeader() { void Packet::printHeader() {
printf("Header:\n\tOpCode:\t\t%s\n\tID:\t\t%d\n\tVersion:\t%hhd\n\tError:\t\t%.8X\n\tSwitch MAC:\t", opCodeToString().c_str(),sequenceId, version, errorCode); printf(
utils::printHex(switchMac); "Header:\n\tOpCode:\t\t%s\n\tID:\t\t%d\n\tVersion:\t%hhd\n\tError:\t\t%.8X\n\tSwitch MAC:\t",
opCodeToString().c_str(), sequenceId, version, errorCode);
utils::print(switchMac);
printf("\n\tHost MAC:\t"); printf("\n\tHost MAC:\t");
utils::printHex(hostMac); utils::print(hostMac);
printf("\n\tLength:\t%hd",this->getLength()); printf("\n\tLength:\t%hd", this->getLength());
printf("\n\tOffset:\t%hd",fragmentOffset); printf("\n\tOffset:\t%hd", fragmentOffset);
printf("\n\tFlags:\t%.4hX",flag); printf("\n\tFlags:\t%.4hX", flag);
printf("\n\tChecksum:\t%d",checkSum); printf("\n\tChecksum:\t%d", checkSum);
} }
bytes Packet::getBytes() { bytes Packet::getBytes() {
@ -65,8 +67,8 @@ void Packet::parse(bytes data) {
short checkLen = 0x0; short checkLen = 0x0;
pull(head, i, version); pull(head, i, version);
pull(head, i, opCode); pull(head, i, opCode);
pull(head, i, switchMac, switchMac.size()); pull(head, i, switchMac);
pull(head, i, hostMac, hostMac.size()); pull(head, i, hostMac);
pull(head, i, sequenceId); pull(head, i, sequenceId);
pull(head, i, errorCode); pull(head, i, errorCode);
pull(head, i, checkLen); pull(head, i, checkLen);
@ -101,7 +103,7 @@ void Packet::setBody(bytes data) {
this->body = data; this->body = data;
} }
void Packet::setHostMac(bytes mac) { void Packet::setHostMac(byteArray<6> mac) {
this->hostMac = mac; this->hostMac = mac;
} }
@ -125,11 +127,11 @@ void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId; this->sequenceId = sequenceId;
} }
const bytes& Packet::getSwitchMac() const { const byteArray<6>& Packet::getSwitchMac() const {
return switchMac; return switchMac;
} }
void Packet::setSwitchMac(bytes switchMac) { void Packet::setSwitchMac(byteArray<6> switchMac) {
this->switchMac = switchMac; this->switchMac = switchMac;
} }
@ -198,11 +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, byteArray<N> data) {
for (unsigned j = 0; j < N; j++)
push(arr, index, data[j]);
}
void Packet::push(bytes &arr, int &index, short data) { void Packet::push(bytes &arr, int &index, short data) {
byte a = (data >> 8) & 0xFF; byte a = (data >> 8) & 0xFF;
push(arr, index, a); push(arr, index, a);
@ -237,6 +246,12 @@ 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, byteArray<N> &ret) {
memcpy(&ret[0], &arr[index], N);
index += N;
}
void Packet::pull(bytes &arr, int &index, short &ret) { void Packet::pull(bytes &arr, int &index, short &ret) {
ret = (arr[index++] << 8); ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF; ret |= arr[index++] & 0xFF;

View file

@ -29,15 +29,15 @@ public:
short getLength() const; short getLength() const;
int getCheckSum() const; int getCheckSum() const;
short getSequenceId() const; short getSequenceId() const;
const bytes& getSwitchMac() const; const byteArray<6>& 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(bytes); void setHostMac(byteArray<6>);
void setSwitchMac(byteArray<6>);
void setCheckSum(int); void setCheckSum(int);
void setSequenceId(short); void setSequenceId(short);
void setSwitchMac(bytes);
void setPayload(const datasets& payload); void setPayload(const datasets& payload);
private: private:
@ -47,9 +47,9 @@ private:
byte version = 1; byte version = 1;
byte opCode; byte opCode;
bytes switchMac = { 0, 0, 0, 0, 0, 0 }; byteArray<6> switchMac = { 0, 0, 0, 0, 0, 0 };
bytes hostMac = { 0, 0, 0, 0, 0, 0 }; // TODO set Mac byteArray<6> hostMac = { 0, 0, 0, 0, 0, 0 };
bytes broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; byteArray<6> broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
short sequenceId; short sequenceId;
short tokenId = 0; short tokenId = 0;
short fragmentOffset = 0; short fragmentOffset = 0;

View file

@ -10,7 +10,6 @@
#include "Utils.h" #include "Utils.h"
#include "Options.h" #include "Options.h"
#include "Program.h" #include "Program.h"
#include "Device.h"
#include "Host.h" #include "Host.h"
#include "Socket.h" #include "Socket.h"
#include "Packet.h" #include "Packet.h"

View file

@ -34,10 +34,9 @@ void Socket::init(short dst_port, short src_port) {
utils::printDec(local_ip); utils::printDec(local_ip);
printf("\n"); printf("\n");
std::array<unsigned char, 4> ip = { local_ip[0], local_ip[1], local_ip[21], local_ip[3] };
wildcard_endpoint_ = asio::ip::udp::endpoint( wildcard_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address_v4::from_string("0.0.0.0"), src_port); asio::ip::address_v4::from_string("0.0.0.0"), src_port);
local_endpoint_ = asio::ip::udp::endpoint(asio::ip::address_v4(ip), local_endpoint_ = asio::ip::udp::endpoint(asio::ip::address_v4(local_ip),
src_port); src_port);
broadcast_endpoint_ = asio::ip::udp::endpoint( broadcast_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address_v4::from_string("255.255.255.255"), dst_port); asio::ip::address_v4::from_string("255.255.255.255"), dst_port);

View file

@ -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);
bytes local_ip = bytes(4); byteArray<4> local_ip = bytes(4);
}; };

View file

@ -10,6 +10,7 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <array>
#include <map> #include <map>
template<typename T> template<typename T>
@ -28,6 +29,8 @@ std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
return A; return A;
} }
template<size_t N>
using byteArray = std::array<unsigned char, N>;
typedef std::vector<unsigned char> bytes; typedef std::vector<unsigned char> bytes;
typedef unsigned char byte; typedef unsigned char byte;
@ -47,6 +50,8 @@ struct Options {
std::string user; std::string user;
std::string password; std::string password;
std::string interface; std::string interface;
std::string file;
long timeout;
}; };
#endif /* TYPES_H_ */ #endif /* TYPES_H_ */

View file

@ -22,6 +22,20 @@ static void printHex(bytes d) {
} }
} }
static void print(byteArray<6> d) {
printf("%.2X", d[0]);
for (unsigned i = 1; i < 6; i++) {
printf(":%.2X", d[i]);
}
}
static void print(byteArray<4> d) {
printf("%.1d", d[0]);
for (unsigned i = 1; i < 4; i++) {
printf(".%.1d", d[i]);
}
}
static void printDec(bytes d) { static void printDec(bytes d) {
if (d.size() > 0) if (d.size() > 0)
printf("%.1d", d[0]); printf("%.1d", d[0]);

View file

@ -30,14 +30,16 @@ int main(int argc, char *argv[]) {
const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
"help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, {
"password", required_argument, 0, 'p' }, { "user", "permanent", no_argument, 0, 's' }, { "password", required_argument,
0, 'p' }, { "user",
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' }, { 0, 0, 0, 0 }, }; 0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout",
required_argument, 0, 't' }, { 0, 0, 0, 0 }, };
Program p = Program(); Program p = Program();
while ((opt = getopt_long(argc, argv, "bhrvxp:u:i:", longopts, &index)) while ((opt = getopt_long(argc, argv, "bhrsvxp:u:i:f:t:", longopts, &index))
!= -1) { != -1) {
switch (opt) { switch (opt) {
@ -65,16 +67,28 @@ int main(int argc, char *argv[]) {
options.flags |= FLAG_HEX; options.flags |= FLAG_HEX;
break; break;
case 's':
options.flags |= FLAG_PERMANENT;
break;
case 't':
options.timeout = atoi(optarg);
break;
case 'f':
options.file = std::string(optarg);
break;
case 'p': case 'p':
options.password= std::string(optarg); options.password = std::string(optarg);
break; break;
case 'u': case 'u':
options.user= std::string(optarg); options.user = std::string(optarg);
break; break;
case 'i': case 'i':
options.interface= std::string(optarg); options.interface = std::string(optarg);
break; break;
default: /* '?' */ default: /* '?' */
@ -96,6 +110,14 @@ int main(int argc, char *argv[]) {
fprintf(stderr, USAGE, argv[0]); fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP); fprintf(stderr, HELP);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else if (strcmp(argv[optind], "get") == 0
|| strcmp(argv[optind], "set") == 0
|| strcmp(argv[optind], "reboot") == 0
|| strcmp(argv[optind], "reset") == 0
|| strcmp(argv[optind], "flash") == 0) {
optind++;
fprintf(stderr, "Not yet implemented.\n");
exit(EXIT_FAILURE);
} else if (strcmp(argv[optind], "list") == 0) { } else if (strcmp(argv[optind], "list") == 0) {
optind++; optind++;
if (p.list()) if (p.list())