From 5544792bfb0224bfa1b10095541324264a655f52 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Sun, 27 Sep 2015 23:41:23 +0200 Subject: [PATCH] choose ip by interface --- Makefile | 2 +- src/Program.cpp | 7 ++++--- src/Socket.cpp | 16 +++++++++++++--- src/Socket.h | 2 ++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 7a9d2d9..f759085 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = g++ -CFLAGS = -g -Wall -std=c++11 +CFLAGS = -g -Wall -std=c++11 TARGET = smrtlink all: $(TARGET) diff --git a/src/Program.cpp b/src/Program.cpp index 999027a..dca4294 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -25,9 +25,7 @@ int Program::list() { //printf(" %d\n", d.getName()); Host h = Host(); - printf("IP:\t"); - utils::printDec(h.getIp(options.interface)); - printf("\nList:\n"); + printf("List:\n"); Packet p = Packet(Packet::DISCOVERY); p.setHostMac(h.getMac()); p.setPayload( { }); @@ -37,6 +35,7 @@ int Program::list() { try { asio::io_service io_service; Socket s(io_service); + s.setHostIp(h.getIp(options.interface)); s.init(DST_PORT, SRC_PORT); s.callback = [](Packet a) { if (options.flags & FLAG_HEADER) { @@ -70,7 +69,9 @@ int Program::sniff() { printf("Listening:\n"); try { asio::io_service io_service; + Host h = Host(); Socket s(io_service); + s.setHostIp(h.getIp(options.interface)); s.init(DST_PORT,SRC_PORT); s.callback = [](Packet p) { if (options.flags & FLAG_HEADER) { diff --git a/src/Socket.cpp b/src/Socket.cpp index ab14c93..0e5255b 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Socket.h" @@ -29,10 +30,15 @@ void Socket::init(short dst_port, short src_port) { src_port = p; } + printf("IP:\t"); + utils::printDec(local_ip); + printf("\n"); + + std::array ip = { local_ip[0], local_ip[1], local_ip[21], local_ip[3] }; wildcard_endpoint_ = asio::ip::udp::endpoint( - asio::ip::address::from_string("0.0.0.0"), src_port); - local_endpoint_ = asio::ip::udp::endpoint( - asio::ip::address::from_string("192.168.0.3"), src_port); + asio::ip::address_v4::from_string("0.0.0.0"), src_port); + local_endpoint_ = asio::ip::udp::endpoint(asio::ip::address_v4(ip), + src_port); broadcast_endpoint_ = asio::ip::udp::endpoint( asio::ip::address_v4::from_string("255.255.255.255"), dst_port); @@ -48,6 +54,10 @@ void Socket::init(short dst_port, short src_port) { } +void Socket::setHostIp(bytes ip) { + local_ip=ip; +} + void Socket::send(bytes data) { unsigned char * a = &data[0]; send_socket_.async_send_to(asio::buffer(a, data.size()), diff --git a/src/Socket.h b/src/Socket.h index 9d6012d..6892b1a 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -22,6 +22,7 @@ public: void init(short, short); void send(bytes); void listen(); + void setHostIp(bytes); int (*callback)(Packet)=[](Packet a) { return 0; }; @@ -35,6 +36,7 @@ private: asio::ip::udp::endpoint wildcard_endpoint_; asio::ip::udp::endpoint local_endpoint_; bytes data = bytes(MAX_LENGTH); + bytes local_ip = bytes(4); };