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

NetworkInterface class is working.

This commit is contained in:
Matias Fontanini
2012-08-09 09:52:44 -03:00
parent dd1d1c3c49
commit 29c5a696e5
7 changed files with 196 additions and 25 deletions

View File

@@ -79,6 +79,10 @@ public:
return !(*this == rhs);
}
const size_t size() const {
return address_size;
}
friend std::ostream &operator<<(std::ostream &os, const HWAddress &addr) {
std::transform(
addr.buffer,

View File

@@ -38,6 +38,11 @@ namespace Tins {
operator uint32_t() const;
operator std::string() const;
bool operator==(const std::string &rhs) const;
bool operator!=(const std::string &rhs) const {
return !(*this == rhs);
}
friend std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
return output << (std::string)addr;
}

View File

@@ -25,26 +25,89 @@
#include <string>
#include <stdint.h>
#include "hwaddress.h"
#include "ipaddress.h"
namespace Tins {
/**
* \class NetworkInterface
* \brief Abstraction of a network interface
*/
class NetworkInterface {
public:
/**
* \brief The type used to store the interface's identifier.
*/
typedef uint32_t id_type;
typedef HWAddress<6> address_type;
NetworkInterface(const std::string &name);
NetworkInterface(id_type id);
/**
* \brief The type of this interface's address.
*/
typedef HWAddress<6> address_type;
/**
* \brief Struct that holds an interface's addresses.
*/
struct Info {
IPv4Address ip_addr, netmask, bcast_addr;
address_type hw_addr;
};
/**
* \brief Constructor from std::string.
*
* \param name The name of the interface this object will abstract.
*/
NetworkInterface(const std::string &name);
/**
* \brief Constructs a NetworkInterface from an ip address.
*
* This abstracted interface will be the one that would be the gateway
* when sending a packet to the given ip.
*
* \param ip The ip address being looked up.
*/
NetworkInterface(IPv4Address ip);
/**
* \brief Getter for this interface's identifier.
*
* \return id_type containing the identifier.
*/
id_type id() const {
return iface_id;
}
address_type address();
/**
* \brief Retrieves this interface's name.
*
* \return std::string containing this interface's name.
*/
std::string name() const;
/**
* \brief Retrieve this interface's addresses.
*
* This method iterates through all the interface's until the
* correct one is found. Therefore it's O(N), being N the amount
* of interfaces in the system.
*/
Info addresses() const;
/**
* \brief Compares this interface for equality.
*
* \param rhs The interface being compared.
*/
bool operator==(const NetworkInterface &rhs) const {
return iface_id == rhs.iface_id;
}
/**
* \brief Compares this interface for inequality.
*
* \param rhs The interface being compared.
*/
bool operator!=(const NetworkInterface &rhs) const {
return !(*this == rhs);
}

View File

@@ -291,7 +291,7 @@ namespace Tins {
struct ifaddrs *if_it = 0;
getifaddrs(&ifaddrs);
for(if_it = ifaddrs; if_it; if_it = if_it->ifa_next) {
if(!functor(if_it))
if(functor(if_it))
break;
}
if(ifaddrs)