Commit 5544792b authored by /jdi/'s avatar /jdi/
Browse files

choose ip by interface

parent 87864b99
CC = g++
CFLAGS = -g -Wall -std=c++11
CFLAGS = -g -Wall -std=c++11
TARGET = smrtlink
all: $(TARGET)
......
......@@ -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) {
......
......@@ -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()),
......
......@@ -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);
};
......
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