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

more failsafe

parent 2a9bc5ea
......@@ -5,53 +5,85 @@
* Author: jdi
*/
//TODO clean up
#include <cstdio>
//#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fstream>
#include <netdb.h>
#include <ifaddrs.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 "Host.h"
#include "Types/Types.h"
//#include "bytes.h"
macAddr Host::getMac() {
macAddr ret { 0x6a,0x49,0x16,0x17,0x2e,0x8d }; //TODO find actual MAC Address
return ret;
int s;
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() {
struct ifaddrs *ifaddr, *ifa;
int n;
ipAddr data { 0, 0, 0, 0 };
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
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) {
memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
return data;
}
}
}
freeifaddrs(ifaddr);
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;
}
......@@ -15,6 +15,7 @@ public:
Host(){};
macAddr getMac();
ipAddr getIp();
std::string getIface();
};
#endif /* HOST_H_ */
......@@ -17,10 +17,9 @@
int Program::list() {
Host h = Host();
printf("List:\n");
Packet p = Packet(Packet::DISCOVERY);
p.setHostMac(h.getMac());
p.setHostMac(host.getMac());
p.setPayload( { });
bytes a = p.getBytes();
p.encode(a);
......@@ -28,7 +27,7 @@ int Program::list() {
try {
asio::io_service io_service;
Socket s(io_service);
s.setHostIp(h.getIp());
s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback =
[](Packet a) {
......@@ -55,7 +54,6 @@ int Program::list() {
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
}
......@@ -63,9 +61,8 @@ int Program::sniff() {
printf("Listening:\n");
try {
asio::io_service io_service;
Host h = Host();
Socket s(io_service);
s.setHostIp(h.getIp());
s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback =
[](Packet p) {
......@@ -98,7 +95,6 @@ int Program::sniff() {
}
}
}
return 0;
};
s.listen();
......@@ -122,12 +118,11 @@ int Program::setProperty() {
return 0;
}
int Program::getProperty() {
Host h = Host();
printf("Get:\n");
Packet p = Packet(Packet::GET);
macAddr d = { 0x14, 0xcc, 0x20, 0x49, 0x5e, 0x07 };
p.setSwitchMac(d);
p.setHostMac(h.getMac());
p.setHostMac(host.getMac());
datasets t = { { 2305, 0, { } } };
p.setPayload(t);
bytes a = p.getBytes();
......@@ -136,7 +131,7 @@ int Program::getProperty() {
try {
asio::io_service io_service;
Socket s(io_service);
s.setHostIp(h.getIp());
s.setHostIp(host.getIp());
s.init(DST_PORT, SRC_PORT);
s.callback =
[](Packet a) {
......@@ -186,3 +181,7 @@ int Program::reset() {
return 0;
}
void Program::init() {
if(options.interface.compare("")==0)
options.interface = host.getIface();
}
......@@ -9,13 +9,12 @@
#define PROGRAM_H_
#include "Types/Types.h"
#define SRC_PORT 29809
#define DST_PORT 29808
#include "Host.h"
class Program {
public:
Program(){}
void init();
int list();
int sniff();
int encode(std::string);
......@@ -26,6 +25,8 @@ public:
int flash();
int reboot();
int reset();
private:
Host host = Host();
};
#endif /* PROGRAM_H_ */
......@@ -12,6 +12,9 @@
#include "Packet.h"
#include "Types/Types.h"
#define SRC_PORT 29809
#define DST_PORT 29808
#define MAX_LENGTH 1024
class Socket {
......
......@@ -113,6 +113,8 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
p.init();
if (optind < argc) {
std::string cmd = std::string(argv[optind++]);
......
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