1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 12:14:26 +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

@@ -184,12 +184,12 @@ include/network_interface.h:
include/rawpdu.h:
include/utils.h:
src/ieee802-3.o: src/ieee802-3.cpp include/ieee802-3.h include/pdu.h \
src/ieee802_3.o: src/ieee802_3.cpp include/ieee802_3.h include/pdu.h \
include/packetsender.h include/utils.h include/ipaddress.h \
include/hwaddress.h include/network_interface.h include/llc.h \
include/utils.h
include/ieee802-3.h:
include/ieee802_3.h:
include/pdu.h:

View File

@@ -53,7 +53,13 @@ public:
};
/**
* \brief Default constructor.
* Returns a NetworkInterface object associated with the default
* interface.
*/
static NetworkInterface default_interface();
/**
* Default constructor.
*/
NetworkInterface();
@@ -65,11 +71,15 @@ public:
NetworkInterface(const std::string &name);
/**
* \brief Constructor to allow implicit conversions from const char*.
* \brief Constructor to allow implicit conversions from string
* literals.
*
* \param name The name of the interface this object will abstract.
*/
NetworkInterface(const char *name);
template<size_t n>
NetworkInterface(const char (&name)[n]) {
iface_id = resolve_index(name);
}
/**
* \brief Constructs a NetworkInterface from an ip address.

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

View File

@@ -67,6 +67,25 @@ src/dns.o: src/dns.cpp ../include/dns.h ../include/pdu.h \
../include/network_interface.h:
../include/utils.h:
src/dot11.o: src/dot11.cpp ../include/dot11.h ../include/pdu.h \
../include/packetsender.h ../include/utils.h ../include/ipaddress.h \
../include/hwaddress.h ../include/network_interface.h ../include/utils.h
../include/dot11.h:
../include/pdu.h:
../include/packetsender.h:
../include/utils.h:
../include/ipaddress.h:
../include/hwaddress.h:
../include/network_interface.h:
../include/utils.h:
src/ethernetII_test.o: src/ethernetII_test.cpp ../include/ethernetII.h \
../include/pdu.h ../include/packetsender.h ../include/utils.h \