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

find Interfaces

parent d61bfbad
......@@ -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
......@@ -5,6 +5,19 @@
* Author: jdi
*/
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <linux/if_link.h>
#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;
}
......@@ -15,7 +15,7 @@ public:
Host();
virtual ~Host() {}
bytes getMac();
bytes getIp();
bytes getIp(std::string);
};
#endif /* HOST_H_ */
......@@ -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;
......
......@@ -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( { });
......
......@@ -4,15 +4,10 @@
* Created on: 02.09.2015
* Author: jdi
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <asio.hpp>
#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(
......
......@@ -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_;
......
......@@ -16,6 +16,7 @@
#include <stdio.h>
#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: /* '?' */
......
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