|
|
@ -17,6 +17,14 @@ extern "C" { |
|
|
|
#define IPBUFLEN 64 |
|
|
|
#define EMPTY_MAC "00:00:00:00:00:00" |
|
|
|
|
|
|
|
/*!
|
|
|
|
\class Neighbor |
|
|
|
\brief The Neighbor class is used to manage ARP table information. |
|
|
|
*/ |
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a Neighbor object. If \a interface is specified, all operations will be limited to that interface. |
|
|
|
*/ |
|
|
|
Neighbor::Neighbor(QString interface) : |
|
|
|
QObject(0), |
|
|
|
m_interface(interface) |
|
|
@ -24,7 +32,10 @@ m_interface(interface) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
QString Neighbor::macOfIP(QString ip) const { |
|
|
|
/*!
|
|
|
|
Returns the MAC address that has been learned for the IP address \a ip. If the IP address is not in the ARP table or an error retrieving the neighbor occurred, the string "00:00:00:00:00:00" will be returned. If there was an error, *ok will be set to false, otherwise it will be true. |
|
|
|
*/ |
|
|
|
QString Neighbor::macOfIP(QString ip, bool *ok) const { |
|
|
|
struct nl_sock *sock = NULL; |
|
|
|
struct rtnl_neigh *neigh = NULL; |
|
|
|
struct nl_cache *neigh_cache, *link_cache = NULL; |
|
|
@ -41,17 +52,31 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
params.dp_buflen = BUFLEN; |
|
|
|
|
|
|
|
if(!(sock = Utils::connect())) { |
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
|
if(!(Utils::alloc_neigh_cache(sock, &neigh_cache))) { |
|
|
|
if(Utils::alloc_neigh_cache(sock, &neigh_cache)) { |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
|
if(!(Utils::alloc_link_cache(sock, &link_cache))) { |
|
|
|
if(Utils::alloc_link_cache(sock, &link_cache)) { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
@ -59,6 +84,11 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
nl_cache_free(link_cache); |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
@ -74,6 +104,11 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
rtnl_neigh_put(neigh); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
@ -89,6 +124,11 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
rtnl_neigh_put(neigh); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
@ -99,6 +139,11 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
rtnl_neigh_put(neigh); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
@ -133,22 +178,32 @@ QString Neighbor::macOfIP(QString ip) const { |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(mac == NULL || count == 0) { |
|
|
|
if(ok != NULL) { |
|
|
|
*ok = true; |
|
|
|
} |
|
|
|
|
|
|
|
return QString(EMPTY_MAC); |
|
|
|
} |
|
|
|
|
|
|
|
QString macStr = mac; |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = true; |
|
|
|
} |
|
|
|
|
|
|
|
return macStr; |
|
|
|
} |
|
|
|
|
|
|
|
QStringList Neighbor::list(QString interfaceString) { |
|
|
|
/*!
|
|
|
|
Returns a string hash consisting of IP addresses (both IPv4 and IPv6) in the ARP cache as the keys, and their corresponding MAC addresses as the values. By specifying \a interfaceString the returned list is confined to addresses seen from that named interface. If there are no entries found or if there was an error obtaining the list, an empty hash with no keys are returned. If there was an error, *ok will be set to false, otherwise it will be true. |
|
|
|
*/ |
|
|
|
QHash<QString, QString> Neighbor::list(QString interfaceString, bool *ok) { |
|
|
|
struct nl_sock *sock = NULL; |
|
|
|
struct rtnl_neigh *neigh = NULL; |
|
|
|
struct nl_cache *neigh_cache, *link_cache = NULL; |
|
|
|
struct nl_dump_params params; |
|
|
|
char buf[BUFLEN]; |
|
|
|
char ip[IPBUFLEN]; |
|
|
|
QStringList neighborList; |
|
|
|
char buf[BUFLEN] = {0}; |
|
|
|
QHash<QString, QString> neighborList; |
|
|
|
|
|
|
|
memset(¶ms, 0, sizeof(struct nl_dump_params)); |
|
|
|
|
|
|
@ -158,18 +213,32 @@ QStringList Neighbor::list(QString interfaceString) { |
|
|
|
params.dp_buflen = BUFLEN; |
|
|
|
|
|
|
|
if(!(sock = Utils::connect())) { |
|
|
|
return QStringList(); |
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
if(!(Utils::alloc_link_cache(sock, &link_cache))) { |
|
|
|
if(Utils::alloc_link_cache(sock, &link_cache)) { |
|
|
|
nl_socket_free(sock); |
|
|
|
return QStringList(); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
if(!(Utils::alloc_neigh_cache(sock, &neigh_cache))) { |
|
|
|
if(Utils::alloc_neigh_cache(sock, &neigh_cache)) { |
|
|
|
nl_cache_free(link_cache); |
|
|
|
nl_socket_free(sock); |
|
|
|
return QStringList(); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
if(!(neigh = rtnl_neigh_alloc())) { |
|
|
@ -177,7 +246,12 @@ QStringList Neighbor::list(QString interfaceString) { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
nl_cache_free(link_cache); |
|
|
|
nl_socket_free(sock); |
|
|
|
return QStringList(); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
if(!interfaceString.isEmpty()) { |
|
|
@ -191,15 +265,17 @@ QStringList Neighbor::list(QString interfaceString) { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
rtnl_neigh_put(neigh); |
|
|
|
nl_socket_free(sock); |
|
|
|
return QStringList(); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = false; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
rtnl_neigh_set_ifindex(neigh, ival); |
|
|
|
} |
|
|
|
|
|
|
|
memset(buf, 0, BUFLEN); |
|
|
|
memset(ip, 0, IPBUFLEN); |
|
|
|
|
|
|
|
nl_cache_dump_filter(neigh_cache, ¶ms, OBJ_CAST(neigh)); |
|
|
|
|
|
|
|
QString bufString = buf; |
|
|
@ -212,9 +288,9 @@ QStringList Neighbor::list(QString interfaceString) { |
|
|
|
|
|
|
|
QStringList neighborFields = neighbor.split(' '); |
|
|
|
|
|
|
|
if(neighborFields.count() > 0) { |
|
|
|
if(neighborFields[0].contains('.') || neighborFields[0].contains(':')) { |
|
|
|
neighborList << neighborFields[0]; |
|
|
|
if(neighborFields.count() > 3) { |
|
|
|
if((neighborFields[0].contains('.') || neighborFields[0].contains(':')) && neighborFields[4].contains(':')) { |
|
|
|
neighborList.insert(neighborFields[0], neighborFields[4]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -224,9 +300,16 @@ QStringList Neighbor::list(QString interfaceString) { |
|
|
|
nl_cache_free(neigh_cache); |
|
|
|
nl_socket_free(sock); |
|
|
|
|
|
|
|
if(ok != NULL) { |
|
|
|
*ok = true; |
|
|
|
} |
|
|
|
|
|
|
|
return neighborList; |
|
|
|
} |
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the name of the interface used when creating the Neighbor object. If a name was not provided, an empty string will be returned. |
|
|
|
*/ |
|
|
|
const QString& Neighbor::name() const { |
|
|
|
return m_interface; |
|
|
|
} |
|
|
|