From ef243d6f8252d5477e18d38f09863638680941c9 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Thu, 15 Oct 2015 00:00:07 +0200 Subject: [PATCH] more failsafe --- src/Host.cpp | 62 ++++++++++++++++++++++++++++++++++++------------ src/Host.h | 1 + src/Program.cpp | 19 +++++++-------- src/Program.h | 7 +++--- src/Socket.h | 3 +++ src/smrtlink.cpp | 2 ++ 6 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/Host.cpp b/src/Host.cpp index c3cc0ef..57372e9 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -5,53 +5,85 @@ * Author: jdi */ - //TODO clean up #include -//#include #include #include #include -#include -#include -#include +#include #include #include #include -//#include +#include +#include +#include +#include +#include +#include + #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 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; +} diff --git a/src/Host.h b/src/Host.h index 1e4d223..7387804 100644 --- a/src/Host.h +++ b/src/Host.h @@ -15,6 +15,7 @@ public: Host(){}; macAddr getMac(); ipAddr getIp(); + std::string getIface(); }; #endif /* HOST_H_ */ diff --git a/src/Program.cpp b/src/Program.cpp index 0a6cd84..e173b09 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -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(); +} diff --git a/src/Program.h b/src/Program.h index 32733d1..4e7e84e 100644 --- a/src/Program.h +++ b/src/Program.h @@ -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_ */ diff --git a/src/Socket.h b/src/Socket.h index c61b704..fbb7f67 100644 --- a/src/Socket.h +++ b/src/Socket.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 { diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 0792109..971241b 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -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++]);