From 316bb1294602c3c23f5629670851d6b5e5e4ed14 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sat, 13 Jun 2015 10:31:31 -0700 Subject: [PATCH] Add interfaces_info example. --- examples/CMakeLists.txt | 2 ++ examples/interfaces_info.cpp | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 examples/interfaces_info.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6cf51ea..5249d45 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -17,6 +17,7 @@ IF(libtins_FOUND) dns_stats wps_detect traceroute + interfaces_info ) ELSE(HAVE_CXX11) MESSAGE(WARNING "Disabling some examples since C++11 support is disabled.") @@ -36,6 +37,7 @@ IF(libtins_FOUND) ADD_EXECUTABLE(dns_queries EXCLUDE_FROM_ALL dns_queries.cpp) ADD_EXECUTABLE(dns_spoof EXCLUDE_FROM_ALL dns_spoof.cpp) ADD_EXECUTABLE(wps_detect EXCLUDE_FROM_ALL wps_detect.cpp) + ADD_EXECUTABLE(interfaces_info EXCLUDE_FROM_ALL interfaces_info.cpp) ENDIF(HAVE_CXX11) ADD_EXECUTABLE(beacon_display EXCLUDE_FROM_ALL beacon_display.cpp) diff --git a/examples/interfaces_info.cpp b/examples/interfaces_info.cpp new file mode 100644 index 0000000..14bb7a2 --- /dev/null +++ b/examples/interfaces_info.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include + +using namespace Tins; +using namespace std; + +int main() { + // Get all interfaces and iterate over them. + for (const NetworkInterface& iface : NetworkInterface::all()) { + // Get the name of this interface + string name = iface.name(); + + // "stringify" the status of the interface + string status = iface.is_up() ? "up" : "down"; + + // Get this interface's information (addresses). + NetworkInterface::Info info = iface.info(); + + // Now print all of this info. + cout << name << ": " << endl; + cout << " HW address: " << info.hw_addr << endl + << " IP address: " << info.ip_addr << endl + << " Netmask: " << info.netmask << endl + << " Broadcast: " << info.bcast_addr << endl + << " Iface index: " << iface.id() << endl + << " Status: " << "interface " << status << endl << endl; + } +} \ No newline at end of file