From 0014d5e0f7bac798ea2ff658b06a202d734dc6b9 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Mon, 13 Aug 2012 15:28:42 -0300 Subject: [PATCH] Done minor modifications on Utils. --- depends.d | 4 ++-- include/network_interface.h | 16 +++++++++++++--- src/network_interface.cpp | 18 ++++++++++-------- src/utils.cpp | 13 ++++++------- tests/depends.d | 19 +++++++++++++++++++ 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/depends.d b/depends.d index dcaa680..92ee362 100644 --- a/depends.d +++ b/depends.d @@ -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: diff --git a/include/network_interface.h b/include/network_interface.h index c05ed12..86999ea 100644 --- a/include/network_interface.h +++ b/include/network_interface.h @@ -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 + NetworkInterface(const char (&name)[n]) { + iface_id = resolve_index(name); + } /** * \brief Constructs a NetworkInterface from an ip address. diff --git a/src/network_interface.cpp b/src/network_interface.cpp index 1f68f97..bcf7266 100644 --- a/src/network_interface.cpp +++ b/src/network_interface.cpp @@ -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 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"); } } diff --git a/src/utils.cpp b/src/utils.cpp index fdcd0b1..2778c2c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #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(response->inner_pdu()); + std::auto_ptr packet(ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr)); + std::auto_ptr response(sender->send_recv(packet.get())); + if(response.get()) { + ARP *arp_resp = response->find_inner_pdu(); if(arp_resp) *address = arp_resp->sender_hw_addr(); - delete response; return arp_resp; } else diff --git a/tests/depends.d b/tests/depends.d index 0c4f0d4..d9f264b 100644 --- a/tests/depends.d +++ b/tests/depends.d @@ -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 \