choose ip by interface

This commit is contained in:
/jdi/ 2015-09-27 23:41:23 +02:00
parent 87864b9975
commit 5544792bfb
4 changed files with 20 additions and 7 deletions

View file

@ -1,5 +1,5 @@
CC = g++
CFLAGS = -g -Wall -std=c++11
CFLAGS = -g -Wall -std=c++11
TARGET = smrtlink
all: $(TARGET)

View file

@ -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) {

View file

@ -8,6 +8,7 @@
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <array>
#include <unistd.h>
#include <asio.hpp>
#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<unsigned char, 4> 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()),

View file

@ -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);
};