From f0def9c046cec38815176209a81e92b9f704aec3 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Sun, 27 Sep 2015 14:38:25 +0200 Subject: [PATCH] find Interfaces --- Makefile | 5 ++-- src/Host.cpp | 41 ++++++++++++++++++++++++-- src/Host.h | 2 +- src/Packet.cpp | 2 +- src/Program.cpp | 4 ++- src/Socket.cpp | 75 ++---------------------------------------------- src/Socket.h | 2 +- src/smrtlink.cpp | 7 +++-- 8 files changed, 54 insertions(+), 84 deletions(-) diff --git a/Makefile b/Makefile index f1d2e55..910e52a 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ CC = g++ # compiler flags: # -g adds debugging information to the executable file # -Wall turns on most, but not all, compiler warnings -CFLAGS = -g -Wall -lrt -lpthread -lc -std=c++11 + #-I +CFLAGS = -g -Wall -std=c++11 # the build target executable: TARGET = smrtlink @@ -15,5 +16,5 @@ $(TARGET): src/*.cpp $(CC) $(CFLAGS) -o $(TARGET) src/*.cpp src/*.h clean: - $(RM) $(TARGET) + rm src/$(TARGET).g diff --git a/src/Host.cpp b/src/Host.cpp index 3abe5d1..a3e1328 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -5,6 +5,19 @@ * Author: jdi */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Utils.h" #include "Host.h" Host::Host() { @@ -16,7 +29,31 @@ bytes Host::getMac() { return {0x08,0x3e,0x8e,0x16,0x17,0x2c}; } -bytes Host::getIp() { - return {0,0,0,0}; +bytes Host::getIp(std::string iface) { + struct ifaddrs *ifaddr, *ifa; + int n; + bytes 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 (iface.compare(ifa->ifa_name) == 0) { + data.resize(4); + memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4); + return data; + } + } + } + + freeifaddrs(ifaddr); + + return data; } diff --git a/src/Host.h b/src/Host.h index be268da..8b68c6e 100644 --- a/src/Host.h +++ b/src/Host.h @@ -15,7 +15,7 @@ public: Host(); virtual ~Host() {} bytes getMac(); - bytes getIp(); + bytes getIp(std::string); }; #endif /* HOST_H_ */ diff --git a/src/Packet.cpp b/src/Packet.cpp index dc3145f..be1dfe1 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -75,7 +75,7 @@ void Packet::parse(bytes data) { pull(head, i, tokenId); pull(head, i, checkSum); if (this->getLength() != checkLen) { - printf("Packet Length doesn't match: %hd != %hd\n", data.size(), + printf("Packet Length doesn't match: %lu != %hd\n", data.size(), checkLen); } i = 0; diff --git a/src/Program.cpp b/src/Program.cpp index cbbfca3..999027a 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -21,11 +21,13 @@ Program::Program() { int Program::list() { - printf("List:\n"); //Device d = Device(); //printf(" %d\n", d.getName()); Host h = Host(); + printf("IP:\t"); + utils::printDec(h.getIp(options.interface)); + printf("\nList:\n"); Packet p = Packet(Packet::DISCOVERY); p.setHostMac(h.getMac()); p.setPayload( { }); diff --git a/src/Socket.cpp b/src/Socket.cpp index 0351fbd..ab14c93 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -4,15 +4,10 @@ * Created on: 02.09.2015 * Author: jdi */ -#include -#include #include #include #include #include -#include -#include -#include #include #include #include "Socket.h" @@ -23,10 +18,9 @@ #include "Host.h" Socket::Socket(asio::io_service& io_service) : - send_socket_(io_service), receive_socket_(io_service), resolver( - io_service) { + send_socket_(io_service), receive_socket_(io_service) { } - +//, resolver( io_service) void Socket::init(short dst_port, short src_port) { if (options.flags & FLAG_REVERSE) { @@ -35,71 +29,6 @@ void Socket::init(short dst_port, short src_port) { src_port = p; } - - struct ifaddrs *ifaddr, *ifa; - int family, s, n; - char host[NI_MAXHOST]; - - if (getifaddrs(&ifaddr) == -1) { - perror("getifaddrs"); - exit(EXIT_FAILURE); - } - - /* Walk through linked list, maintaining head pointer so we - can free list later */ - - for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { - if (ifa->ifa_addr == NULL) - continue; - - family = ifa->ifa_addr->sa_family; - - /* Display interface name and family (including symbolic - form of the latter for the common families) */ - - printf("%-8s %s (%d)\n", - ifa->ifa_name, - (family == AF_PACKET) ? "AF_PACKET" : - (family == AF_INET) ? "AF_INET" : - (family == AF_INET6) ? "AF_INET6" : "???", - family); - - /* For an AF_INET* interface address, display the address */ - - if (family == AF_INET || family == AF_INET6) { - s = getnameinfo(ifa->ifa_addr, - (family == AF_INET) ? sizeof(struct sockaddr_in) : - sizeof(struct sockaddr_in6), - host, NI_MAXHOST, - NULL, 0, NI_NUMERICHOST); - if (s != 0) { - printf("getnameinfo() failed: %s\n", gai_strerror(s)); - exit(EXIT_FAILURE); - } - - printf("\t\taddress: <%s>\n", host); - - } /*else if (family == AF_PACKET && ifa->ifa_data != NULL) { - struct rtnl_link_stats *stats = ifa->ifa_data; - - printf("\t\ttx_packets = %10u; rx_packets = %10u\n" - "\t\ttx_bytes = %10u; rx_bytes = %10u\n", - stats->tx_packets, stats->rx_packets, - stats->tx_bytes, stats->rx_bytes); - }*/ - } - - freeifaddrs(ifaddr); - - /* - asio::ip::udp::resolver::query query(asio::ip::host_name(), ""); - asio::ip::udp::resolver::iterator iter = resolver.resolve(query); - asio::ip::udp::resolver::iterator end; - while (iter != end) { - asio::ip::udp::endpoint ep = *iter++; - std::cout << "IP: " << ep << std::endl; - }*/ - wildcard_endpoint_ = asio::ip::udp::endpoint( asio::ip::address::from_string("0.0.0.0"), src_port); local_endpoint_ = asio::ip::udp::endpoint( diff --git a/src/Socket.h b/src/Socket.h index c2c2078..9d6012d 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -29,7 +29,7 @@ public: private: asio::ip::udp::socket send_socket_; asio::ip::udp::socket receive_socket_; - asio::ip::udp::resolver resolver; + //asio::ip::udp::resolver resolver; asio::ip::udp::endpoint broadcast_endpoint_; asio::ip::udp::endpoint remote_endpoint_; asio::ip::udp::endpoint wildcard_endpoint_; diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index ceb0f4a..3b4425a 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -16,6 +16,7 @@ #include #include "Options.h" +#include "Host.h" #include "Program.h" #define no_argument 0 @@ -65,15 +66,15 @@ int main(int argc, char *argv[]) { break; case 'p': - //TODO add password + options.password= std::string(optarg); break; case 'u': - //TODO add username + options.user= std::string(optarg); break; case 'i': - //TODO add interface + options.interface= std::string(optarg); break; default: /* '?' */