more failsafe

This commit is contained in:
/jdi/ 2015-10-15 00:00:07 +02:00
parent 2a9bc5ea8d
commit ef243d6f82
6 changed files with 66 additions and 28 deletions

View file

@ -5,53 +5,85 @@
* Author: jdi * Author: jdi
*/ */
//TODO clean up //TODO clean up
#include <cstdio> #include <cstdio>
//#include <cerrno>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <sys/types.h> #include <fstream>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <unistd.h> #include <unistd.h>
//#include <linux/if_link.h> #include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_link.h>
#include <net/if.h>
#include "Options.h" #include "Options.h"
#include "Host.h" #include "Host.h"
#include "Types/Types.h" #include "Types/Types.h"
//#include "bytes.h"
macAddr Host::getMac() { macAddr Host::getMac() {
macAddr ret { 0x6a,0x49,0x16,0x17,0x2e,0x8d }; //TODO find actual MAC Address int s;
return ret; struct ifreq buffer;
macAddr data { 0, 0, 0, 0, 0, 0 };
s = socket(PF_INET, SOCK_DGRAM, 0);
memset(&buffer, 0x00, sizeof(buffer));
strcpy(buffer.ifr_name, options.interface.c_str());
ioctl(s, SIOCGIFHWADDR, &buffer);
close(s);
memcpy(&data[0], &buffer.ifr_hwaddr.sa_data[0], 6);
return data;
} }
ipAddr Host::getIp() { ipAddr Host::getIp() {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int n; int n;
ipAddr data { 0, 0, 0, 0 }; ipAddr data { 0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs"); perror("getifaddrs");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL) if (ifa->ifa_addr == NULL)
continue; continue;
if (ifa->ifa_addr->sa_family == AF_INET)
if (ifa->ifa_addr->sa_family == AF_INET) {
if (options.interface.compare(ifa->ifa_name) == 0) { if (options.interface.compare(ifa->ifa_name) == 0) {
memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
return data; return data;
} }
}
} }
freeifaddrs(ifaddr); freeifaddrs(ifaddr);
return data; return data;
} }
std::string Host::getIface() {
std::string defaultInterface;
std::ifstream routeFile("/proc/net/route", std::ios_base::in);
if (!routeFile.good())
return "";
std::string line;
std::vector<std::string> tokens;
if (std::getline(routeFile, line))
while (std::getline(routeFile, line)) {
std::istringstream stream(line);
std::string delimiter = "\t";
size_t pos = 0;
std::string token;
while ((pos = line.find(delimiter)) != std::string::npos) {
token = line.substr(0, pos);
tokens.push_back(token);
line.erase(0, pos + delimiter.length());
}
if ((tokens.size() >= 2)
&& (tokens[1] == std::string("00000000"))) {
defaultInterface = tokens[0];
break;
}
tokens.clear();
}
routeFile.close();
return defaultInterface;
}

View file

@ -15,6 +15,7 @@ public:
Host(){}; Host(){};
macAddr getMac(); macAddr getMac();
ipAddr getIp(); ipAddr getIp();
std::string getIface();
}; };
#endif /* HOST_H_ */ #endif /* HOST_H_ */

View file

@ -17,10 +17,9 @@
int Program::list() { int Program::list() {
Host h = Host();
printf("List:\n"); printf("List:\n");
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(h.getMac()); p.setHostMac(host.getMac());
p.setPayload( { }); p.setPayload( { });
bytes a = p.getBytes(); bytes a = p.getBytes();
p.encode(a); p.encode(a);
@ -28,7 +27,7 @@ int Program::list() {
try { try {
asio::io_service io_service; asio::io_service io_service;
Socket s(io_service); Socket s(io_service);
s.setHostIp(h.getIp()); s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT); s.init(DST_PORT, SRC_PORT);
s.callback = s.callback =
[](Packet a) { [](Packet a) {
@ -55,7 +54,6 @@ int Program::list() {
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n"; std::cerr << "Exception: " << e.what() << "\n";
} }
return 1; return 1;
} }
@ -63,9 +61,8 @@ int Program::sniff() {
printf("Listening:\n"); printf("Listening:\n");
try { try {
asio::io_service io_service; asio::io_service io_service;
Host h = Host();
Socket s(io_service); Socket s(io_service);
s.setHostIp(h.getIp()); s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT); s.init(DST_PORT, SRC_PORT);
s.callback = s.callback =
[](Packet p) { [](Packet p) {
@ -98,7 +95,6 @@ int Program::sniff() {
} }
} }
} }
return 0; return 0;
}; };
s.listen(); s.listen();
@ -122,12 +118,11 @@ int Program::setProperty() {
return 0; return 0;
} }
int Program::getProperty() { int Program::getProperty() {
Host h = Host();
printf("Get:\n"); printf("Get:\n");
Packet p = Packet(Packet::GET); Packet p = Packet(Packet::GET);
macAddr d = { 0x14, 0xcc, 0x20, 0x49, 0x5e, 0x07 }; macAddr d = { 0x14, 0xcc, 0x20, 0x49, 0x5e, 0x07 };
p.setSwitchMac(d); p.setSwitchMac(d);
p.setHostMac(h.getMac()); p.setHostMac(host.getMac());
datasets t = { { 2305, 0, { } } }; datasets t = { { 2305, 0, { } } };
p.setPayload(t); p.setPayload(t);
bytes a = p.getBytes(); bytes a = p.getBytes();
@ -136,7 +131,7 @@ int Program::getProperty() {
try { try {
asio::io_service io_service; asio::io_service io_service;
Socket s(io_service); Socket s(io_service);
s.setHostIp(h.getIp()); s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT); s.init(DST_PORT, SRC_PORT);
s.callback = s.callback =
[](Packet a) { [](Packet a) {
@ -186,3 +181,7 @@ int Program::reset() {
return 0; return 0;
} }
void Program::init() {
if(options.interface.compare("")==0)
options.interface = host.getIface();
}

View file

@ -9,13 +9,12 @@
#define PROGRAM_H_ #define PROGRAM_H_
#include "Types/Types.h" #include "Types/Types.h"
#include "Host.h"
#define SRC_PORT 29809
#define DST_PORT 29808
class Program { class Program {
public: public:
Program(){} Program(){}
void init();
int list(); int list();
int sniff(); int sniff();
int encode(std::string); int encode(std::string);
@ -26,6 +25,8 @@ public:
int flash(); int flash();
int reboot(); int reboot();
int reset(); int reset();
private:
Host host = Host();
}; };
#endif /* PROGRAM_H_ */ #endif /* PROGRAM_H_ */

View file

@ -12,6 +12,9 @@
#include "Packet.h" #include "Packet.h"
#include "Types/Types.h" #include "Types/Types.h"
#define SRC_PORT 29809
#define DST_PORT 29808
#define MAX_LENGTH 1024 #define MAX_LENGTH 1024
class Socket { class Socket {

View file

@ -113,6 +113,8 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
p.init();
if (optind < argc) { if (optind < argc) {
std::string cmd = std::string(argv[optind++]); std::string cmd = std::string(argv[optind++]);