choose ip by interface
This commit is contained in:
parent
87864b9975
commit
5544792bfb
4 changed files with 20 additions and 7 deletions
2
Makefile
2
Makefile
|
|
@ -1,5 +1,5 @@
|
||||||
CC = g++
|
CC = g++
|
||||||
CFLAGS = -g -Wall -std=c++11
|
CFLAGS = -g -Wall -std=c++11
|
||||||
TARGET = smrtlink
|
TARGET = smrtlink
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,7 @@ int Program::list() {
|
||||||
//printf(" %d\n", d.getName());
|
//printf(" %d\n", d.getName());
|
||||||
|
|
||||||
Host h = Host();
|
Host h = Host();
|
||||||
printf("IP:\t");
|
printf("List:\n");
|
||||||
utils::printDec(h.getIp(options.interface));
|
|
||||||
printf("\nList:\n");
|
|
||||||
Packet p = Packet(Packet::DISCOVERY);
|
Packet p = Packet(Packet::DISCOVERY);
|
||||||
p.setHostMac(h.getMac());
|
p.setHostMac(h.getMac());
|
||||||
p.setPayload( { });
|
p.setPayload( { });
|
||||||
|
|
@ -37,6 +35,7 @@ int Program::list() {
|
||||||
try {
|
try {
|
||||||
asio::io_service io_service;
|
asio::io_service io_service;
|
||||||
Socket s(io_service);
|
Socket s(io_service);
|
||||||
|
s.setHostIp(h.getIp(options.interface));
|
||||||
s.init(DST_PORT, SRC_PORT);
|
s.init(DST_PORT, SRC_PORT);
|
||||||
s.callback = [](Packet a) {
|
s.callback = [](Packet a) {
|
||||||
if (options.flags & FLAG_HEADER) {
|
if (options.flags & FLAG_HEADER) {
|
||||||
|
|
@ -70,7 +69,9 @@ int Program::sniff() {
|
||||||
printf("Listening:\n");
|
printf("Listening:\n");
|
||||||
try {
|
try {
|
||||||
asio::io_service io_service;
|
asio::io_service io_service;
|
||||||
|
Host h = Host();
|
||||||
Socket s(io_service);
|
Socket s(io_service);
|
||||||
|
s.setHostIp(h.getIp(options.interface));
|
||||||
s.init(DST_PORT,SRC_PORT);
|
s.init(DST_PORT,SRC_PORT);
|
||||||
s.callback = [](Packet p) {
|
s.callback = [](Packet p) {
|
||||||
if (options.flags & FLAG_HEADER) {
|
if (options.flags & FLAG_HEADER) {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <array>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <asio.hpp>
|
#include <asio.hpp>
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
|
|
@ -29,10 +30,15 @@ void Socket::init(short dst_port, short src_port) {
|
||||||
src_port = p;
|
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(
|
wildcard_endpoint_ = asio::ip::udp::endpoint(
|
||||||
asio::ip::address::from_string("0.0.0.0"), src_port);
|
asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
||||||
local_endpoint_ = asio::ip::udp::endpoint(
|
local_endpoint_ = asio::ip::udp::endpoint(asio::ip::address_v4(ip),
|
||||||
asio::ip::address::from_string("192.168.0.3"), src_port);
|
src_port);
|
||||||
broadcast_endpoint_ = asio::ip::udp::endpoint(
|
broadcast_endpoint_ = asio::ip::udp::endpoint(
|
||||||
asio::ip::address_v4::from_string("255.255.255.255"), dst_port);
|
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) {
|
void Socket::send(bytes data) {
|
||||||
unsigned char * a = &data[0];
|
unsigned char * a = &data[0];
|
||||||
send_socket_.async_send_to(asio::buffer(a, data.size()),
|
send_socket_.async_send_to(asio::buffer(a, data.size()),
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ public:
|
||||||
void init(short, short);
|
void init(short, short);
|
||||||
void send(bytes);
|
void send(bytes);
|
||||||
void listen();
|
void listen();
|
||||||
|
void setHostIp(bytes);
|
||||||
int (*callback)(Packet)=[](Packet a) {
|
int (*callback)(Packet)=[](Packet a) {
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
@ -35,6 +36,7 @@ private:
|
||||||
asio::ip::udp::endpoint wildcard_endpoint_;
|
asio::ip::udp::endpoint wildcard_endpoint_;
|
||||||
asio::ip::udp::endpoint local_endpoint_;
|
asio::ip::udp::endpoint local_endpoint_;
|
||||||
bytes data = bytes(MAX_LENGTH);
|
bytes data = bytes(MAX_LENGTH);
|
||||||
|
bytes local_ip = bytes(4);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue