Commit aca82979 authored by /jdi/'s avatar /jdi/
Browse files

some changes

parent 5544792b
/*
* 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
}
/*
* 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_ */
......@@ -25,14 +25,16 @@ Host::Host() {
}
bytes Host::getMac() {
return {0x08,0x3e,0x8e,0x16,0x17,0x2c};
byteArray<6> Host::getMac() {
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;
int n;
bytes data = { 0, 0, 0, 0 };
byteArray<4> data = { 0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
......@@ -45,7 +47,6 @@ bytes Host::getIp(std::string iface) {
if (ifa->ifa_addr->sa_family == AF_INET) {
if (iface.compare(ifa->ifa_name) == 0) {
data.resize(4);
memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
return data;
}
......
......@@ -13,9 +13,8 @@
class Host {
public:
Host();
virtual ~Host() {}
bytes getMac();
bytes getIp(std::string);
byteArray<6> getMac();
byteArray<4> getIp(std::string);
};
#endif /* HOST_H_ */
......@@ -14,6 +14,7 @@
#define USAGE "usage: %s [-bhrvx] [-i interface] [-u [password:]username]\n\
[-p password] <command>\n\n"
#define HELP "\
### for questions please contact <smrtlink@jdi.li> ###\n\
Option Summary:\n\
-h --help This help text\n\
-v --version Display version of this tool\n\
......@@ -22,16 +23,25 @@
-x --hex Display Packets as Hex String\n\
-i --interface only use one Interface\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\
help This help text\n\
list list all connected switches\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_REVERSE 2
#define FLAG_HEADER 4
#define FLAG_PERMANENT 4
extern Options options;
......
......@@ -20,14 +20,16 @@ Packet::Packet(OpCode c) {
}
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);
utils::printHex(switchMac);
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);
utils::print(switchMac);
printf("\n\tHost MAC:\t");
utils::printHex(hostMac);
printf("\n\tLength:\t%hd",this->getLength());
printf("\n\tOffset:\t%hd",fragmentOffset);
printf("\n\tFlags:\t%.4hX",flag);
printf("\n\tChecksum:\t%d",checkSum);
utils::print(hostMac);
printf("\n\tLength:\t%hd", this->getLength());
printf("\n\tOffset:\t%hd", fragmentOffset);
printf("\n\tFlags:\t%.4hX", flag);
printf("\n\tChecksum:\t%d", checkSum);
}
bytes Packet::getBytes() {
......@@ -65,8 +67,8 @@ void Packet::parse(bytes data) {
short checkLen = 0x0;
pull(head, i, version);
pull(head, i, opCode);
pull(head, i, switchMac, switchMac.size());
pull(head, i, hostMac, hostMac.size());
pull(head, i, switchMac);
pull(head, i, hostMac);
pull(head, i, sequenceId);
pull(head, i, errorCode);
pull(head, i, checkLen);
......@@ -101,7 +103,7 @@ void Packet::setBody(bytes data) {
this->body = data;
}
void Packet::setHostMac(bytes mac) {
void Packet::setHostMac(byteArray<6> mac) {
this->hostMac = mac;
}
......@@ -125,11 +127,11 @@ void Packet::setSequenceId(short sequenceId) {
this->sequenceId = sequenceId;
}
const bytes& Packet::getSwitchMac() const {
const byteArray<6>& Packet::getSwitchMac() const {
return switchMac;
}
void Packet::setSwitchMac(bytes switchMac) {
void Packet::setSwitchMac(byteArray<6> 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) {
for (unsigned j = 0; j < data.size(); 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) {
byte a = (data >> 8) & 0xFF;
push(arr, index, a);
......@@ -237,6 +246,12 @@ void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned 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) {
ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF;
......
......@@ -29,15 +29,15 @@ public:
short getLength() const;
int getCheckSum() const;
short getSequenceId() const;
const bytes& getSwitchMac() const;
const byteArray<6>& getSwitchMac() const;
const bytes& getBody() const;
const bytes& getHead() const;
const datasets& getPayload() const;
void setBody(bytes);
void setHostMac(bytes);
void setHostMac(byteArray<6>);
void setSwitchMac(byteArray<6>);
void setCheckSum(int);
void setSequenceId(short);
void setSwitchMac(bytes);
void setPayload(const datasets& payload);
private:
......@@ -47,9 +47,9 @@ private:
byte version = 1;
byte opCode;
bytes switchMac = { 0, 0, 0, 0, 0, 0 };
bytes hostMac = { 0, 0, 0, 0, 0, 0 }; // TODO set Mac
bytes broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
byteArray<6> switchMac = { 0, 0, 0, 0, 0, 0 };
byteArray<6> hostMac = { 0, 0, 0, 0, 0, 0 };
byteArray<6> broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
short sequenceId;
short tokenId = 0;
short fragmentOffset = 0;
......
......@@ -10,7 +10,6 @@
#include "Utils.h"
#include "Options.h"
#include "Program.h"
#include "Device.h"
#include "Host.h"
#include "Socket.h"
#include "Packet.h"
......
......@@ -34,10 +34,9 @@ void Socket::init(short dst_port, short src_port) {
utils::printDec(local_ip);
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(
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);
broadcast_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address_v4::from_string("255.255.255.255"), dst_port);
......
......@@ -36,7 +36,7 @@ private:
asio::ip::udp::endpoint wildcard_endpoint_;
asio::ip::udp::endpoint local_endpoint_;
bytes data = bytes(MAX_LENGTH);
bytes local_ip = bytes(4);
byteArray<4> local_ip = bytes(4);
};
......
......@@ -10,6 +10,7 @@
#include <functional>
#include <vector>
#include <array>
#include <map>
template<typename T>
......@@ -28,6 +29,8 @@ std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
return A;
}
template<size_t N>
using byteArray = std::array<unsigned char, N>;
typedef std::vector<unsigned char> bytes;
typedef unsigned char byte;
......@@ -47,6 +50,8 @@ struct Options {
std::string user;
std::string password;
std::string interface;
std::string file;
long timeout;
};
#endif /* TYPES_H_ */
......@@ -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) {
if (d.size() > 0)
printf("%.1d", d[0]);
......
......@@ -30,14 +30,16 @@ int main(int argc, char *argv[]) {
const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
"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' }, {
"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();
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) {
switch (opt) {
......@@ -65,16 +67,28 @@ int main(int argc, char *argv[]) {
options.flags |= FLAG_HEX;
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':
options.password= std::string(optarg);
options.password = std::string(optarg);
break;
case 'u':
options.user= std::string(optarg);
options.user = std::string(optarg);
break;
case 'i':
options.interface= std::string(optarg);
options.interface = std::string(optarg);
break;
default: /* '?' */
......@@ -96,6 +110,14 @@ int main(int argc, char *argv[]) {
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP);
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) {
optind++;
if (p.list())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment