1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Done minor modifications on Utils.

This commit is contained in:
Matias Fontanini
2012-08-13 15:28:42 -03:00
parent e2223bf406
commit 0014d5e0f7
5 changed files with 50 additions and 20 deletions

View File

@@ -61,6 +61,11 @@ struct InterfaceInfoCollector {
/** \endcond */
namespace Tins {
// static
NetworkInterface NetworkInterface::default_interface() {
return NetworkInterface(IPv4Address(0));
}
NetworkInterface::NetworkInterface() : iface_id(0) {
}
@@ -69,26 +74,23 @@ NetworkInterface::NetworkInterface(const std::string &name) {
iface_id = resolve_index(name.c_str());
}
NetworkInterface::NetworkInterface(const char *name) {
iface_id = resolve_index(name);
}
NetworkInterface::NetworkInterface(IPv4Address ip) : iface_id(0) {
typedef std::vector<Utils::RouteEntry> entries_type;
if(ip == "127.0.0.1")
iface_id = resolve_index("lo");
else {
Utils::RouteEntry *best_match = 0;
entries_type entries;
uint32_t ip_int = ip;
route_entries(std::back_inserter(entries));
Utils::route_entries(std::back_inserter(entries));
for(entries_type::const_iterator it(entries.begin()); it != entries.end(); ++it) {
if((ip_int & it->mask) == it->destination) {
iface_id = if_nametoindex(it->interface.c_str());
break;
if(!best_match || it->mask > best_match->mask)
iface_id = if_nametoindex(it->interface.c_str());
}
}
if(!iface_id)
if(best_match)
throw std::runtime_error("Error looking up interface");
}
}

View File

@@ -22,6 +22,7 @@
#include <stdexcept>
#include <sstream>
#include <stdexcept>
#include <memory>
#include <cassert>
#include <cstring>
#ifndef WIN32
@@ -134,7 +135,6 @@ uint32_t Tins::Utils::resolve_ip(const string &to_resolve) {
}
Tins::PDU *Tins::Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4Address ip_src) {
ICMP *icmp = new ICMP(ICMP::ECHO_REQUEST);
if(!ip_src) {
try {
NetworkInterface iface(ip);
@@ -143,6 +143,7 @@ Tins::PDU *Tins::Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4A
return 0;
}
}
ICMP *icmp = new ICMP(ICMP::ECHO_REQUEST);
IP ip_packet(ip, ip_src, icmp);
return sender->send_recv(&ip_packet);
}
@@ -152,14 +153,12 @@ bool Tins::Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
{
IPv4Address my_ip;
NetworkInterface::Info info(iface.addresses());
PDU *packet = ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr);
PDU *response = sender->send_recv(packet);
delete packet;
if(response) {
ARP *arp_resp = dynamic_cast<ARP*>(response->inner_pdu());
std::auto_ptr<PDU> packet(ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr));
std::auto_ptr<PDU> response(sender->send_recv(packet.get()));
if(response.get()) {
ARP *arp_resp = response->find_inner_pdu<ARP>();
if(arp_resp)
*address = arp_resp->sender_hw_addr();
delete response;
return arp_resp;
}
else