Browse Source

split neighbor and interface functions into different classes, add static interface list function

master
Brad Parker 11 years ago
parent
commit
a806500be9
  1. 97
      interface.cpp
  2. 19
      interface.h
  3. 15
      ip.h
  4. 22
      iptest/iptest.cpp
  5. 4
      libip.pro
  6. 79
      neighbor.cpp
  7. 17
      neighbor.h

97
ip.cpp → interface.cpp

@ -10,27 +10,51 @@ extern "C" {
#include <pthread.h>
}
#include "ip.h"
#include "interface.h"
#include <iostream>
#include <QStringList>
#define BUFLEN 65535
#define MACBUFLEN 18
#define ADDRBUFLEN BUFLEN
IP::IP() :
QObject(0)
Interface::Interface(QString interface) :
QObject(0),
m_interface(interface)
{
}
QStringList IP::addresses(const char *interface) {
bool Interface::hasCarrier() {
struct rtnl_link *link;
struct nl_sock *sock;
QByteArray interfaceArray = m_interface.toLatin1();
const char *interface = interfaceArray.constData();
sock = nl_cli_alloc_socket();
nl_cli_connect(sock, NETLINK_ROUTE);
if(rtnl_link_get_kernel(sock, 0, interface, &link) < 0) {
std::cerr << "error looking up interface" << std::endl;
}
unsigned int flags = rtnl_link_get_flags(link);
bool carrier = flags & IFF_RUNNING;
nl_socket_free(sock);
return carrier;
}
QStringList Interface::addresses() {
struct nl_sock *sock;
struct rtnl_addr *addr;
struct nl_cache *addr_cache, *link_cache;
struct nl_dump_params params;
char buf[BUFLEN];
char addrs[ADDRBUFLEN];
QByteArray interfaceArray = m_interface.toLatin1();
const char *interface = interfaceArray.constData();
memset(&params, 0, sizeof(struct nl_dump_params));
@ -85,63 +109,40 @@ QStringList IP::addresses(const char *interface) {
return addrsList;
}
QString IP::macOfIP(const char *ip) {
QStringList Interface::list() {
struct nl_sock *sock;
struct rtnl_neigh *neigh;
struct nl_cache *neigh_cache;
struct nl_dump_params params;
char buf[BUFLEN];
char mac[MACBUFLEN];
memset(&params, 0, sizeof(struct nl_dump_params));
params.dp_type = NL_DUMP_LINE;
params.dp_fd = NULL;
params.dp_buf = buf;
params.dp_buflen = BUFLEN;
char *ipAddr = strndup(ip, strlen(ip));
struct nl_cache *link_cache;
QStringList linkList;
sock = nl_cli_alloc_socket();
nl_cli_connect(sock, NETLINK_ROUTE);
neigh_cache = nl_cli_neigh_alloc_cache(sock);
neigh = nl_cli_neigh_alloc();
//case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
nl_cli_neigh_parse_dst(neigh, ipAddr);
int ret = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache);
memset(buf, 0, BUFLEN);
memset(mac, 0, MACBUFLEN);
if(ret != 0) {
std::cerr << "could not allocate link cache" << std::endl;
nl_socket_free(sock);
return linkList;
}
nl_cache_dump_filter(neigh_cache, &params, OBJ_CAST(neigh));
int index = 1;
//char data[128] = {0};
unsigned short count = 0;
char *rest = NULL;
char *field = strtok_r(buf, " ", &rest);
forever {
struct rtnl_link *link = rtnl_link_get(link_cache, index);
while(field != NULL) {
++count;
if(link != NULL) {
char *name = rtnl_link_get_name(link);
if(count == 5) {
if(strstr(field, ":") != NULL) {
memcpy(mac, field, MACBUFLEN - 1);
}
linkList << name;
}else{
break;
}
field = strtok_r(NULL, " ", &rest);
++index;
}
nl_object_free(OBJ_CAST(neigh));
nl_cache_free(neigh_cache);
nl_cache_free(link_cache);
nl_socket_free(sock);
free(ipAddr);
if(mac == NULL || count == 0) {
return "00:00:00:00:00:00";
}
QString macStr = mac;
return macStr;
return linkList;
}

19
interface.h

@ -0,0 +1,19 @@
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QObject>
class Q_DECL_EXPORT Interface : public QObject {
Q_OBJECT
public:
Interface(QString interface);
static QStringList list();
QStringList addresses();
bool hasCarrier();
private:
QString m_interface;
};
#endif // INTERFACE_H

15
ip.h

@ -1,15 +0,0 @@
#ifndef IP_H
#define IP_H
#include <QObject>
class Q_DECL_EXPORT IP : public QObject {
Q_OBJECT
public:
IP();
QString macOfIP(const char *ip);
QStringList addresses(const char *interface);
};
#endif // IP

22
iptest/iptest.cpp

@ -2,22 +2,30 @@
#include <iostream>
#include <QTimer>
#include <QStringList>
#include "ip.h"
#include "interface.h"
#include "neighbor.h"
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
IP ip;
Interface interface("wlp3s0");
Neighbor neighbor;
std::cout << "MAC: " << qPrintable(ip.macOfIP("10.0.4.1")) << std::endl;
QStringList addresses = ip.addresses("wlp3s0");
std::cout << "MAC: " << qPrintable(neighbor.macOfIP("10.0.1.1")) << std::endl;
QStringList addresses = interface.addresses();
foreach(QString address, addresses) {
std::cout << "Address: " << qPrintable(address) << std::endl;
}
std::cout << "Link of wlp3s0 is: " << ip.hasCarrier("wlp3s0") << std::endl;
std::cout << "Link of enp0s25 is: " << ip.hasCarrier("enp0s25") << std::endl;
std::cout << "Link of wlp3s0 is: " << ip.hasCarrier("wlp3s0") << std::endl;
std::cout << "Link of wlp3s0 is: " << interface.hasCarrier() << std::endl;
std::cout << "Interfaces:" << std::endl;
QStringList links = Interface::list();
foreach(QString link, links) {
std::cout << qPrintable(link) << std::endl;
}
QTimer::singleShot(0, qApp, SLOT(quit()));

4
libip.pro

@ -12,5 +12,5 @@ QMAKE_CFLAGS += -Wall -Wextra -Werror
LIBS += -lnl-route-3 -lnl-3 -lnl-cli-3
HEADERS += ip.h
SOURCES += ip.cpp
HEADERS += interface.h neighbor.h
SOURCES += interface.cpp neighbor.cpp

79
neighbor.cpp

@ -0,0 +1,79 @@
extern "C" {
#include <netlink/cli/utils.h>
#include <netlink/cli/neigh.h>
#include <netlink/cli/addr.h>
#include <netlink/cli/link.h>
#include <pthread.h>
}
#include "neighbor.h"
#include <iostream>
#include <QStringList>
#define BUFLEN 65535
#define MACBUFLEN 18
Neighbor::Neighbor(QString interface) :
QObject(0),
m_interface(interface)
{
}
QString Neighbor::macOfIP(QString ip) {
struct nl_sock *sock;
struct rtnl_neigh *neigh;
struct nl_cache *neigh_cache;
struct nl_dump_params params;
char buf[BUFLEN];
char mac[MACBUFLEN];
QByteArray ipArray = ip.toLatin1();
char *ipAddr = ipArray.data();
memset(&params, 0, sizeof(struct nl_dump_params));
params.dp_type = NL_DUMP_LINE;
params.dp_fd = NULL;
params.dp_buf = buf;
params.dp_buflen = BUFLEN;
sock = nl_cli_alloc_socket();
nl_cli_connect(sock, NETLINK_ROUTE);
neigh_cache = nl_cli_neigh_alloc_cache(sock);
neigh = nl_cli_neigh_alloc();
nl_cli_neigh_parse_dst(neigh, ipAddr);
memset(buf, 0, BUFLEN);
memset(mac, 0, MACBUFLEN);
nl_cache_dump_filter(neigh_cache, &params, OBJ_CAST(neigh));
unsigned short count = 0;
char *rest = NULL;
char *field = strtok_r(buf, " ", &rest);
while(field != NULL) {
++count;
if(count == 5) {
if(strstr(field, ":") != NULL) {
memcpy(mac, field, MACBUFLEN - 1);
}
}
field = strtok_r(NULL, " ", &rest);
}
nl_object_free(OBJ_CAST(neigh));
nl_cache_free(neigh_cache);
nl_socket_free(sock);
if(mac == NULL || count == 0) {
return "00:00:00:00:00:00";
}
QString macStr = mac;
return macStr;
}

17
neighbor.h

@ -0,0 +1,17 @@
#ifndef NEIGHBOR_H
#define NEIGHBOR_H
#include <QObject>
class Q_DECL_EXPORT Neighbor : public QObject {
Q_OBJECT
public:
Neighbor(QString interface = QString());
QString macOfIP(QString ip);
private:
QString m_interface;
};
#endif // NEIGHBOR_H
Loading…
Cancel
Save