You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
858 B

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 "utils.h"
#include <iostream>
#include <QTimer>
#include <QtCore/QCoreApplication>
int Utils::interfaceIndex(QString interface) {
QByteArray array = interface.toLatin1();
struct nl_sock *sock = nl_cli_alloc_socket();
nl_cli_connect(sock, NETLINK_ROUTE);
const char *interfaceData = array.constData();
struct nl_cache *link_cache;
int ret = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache);
if(ret != 0) {
std::cerr << "could not allocate link cache" << std::endl;
QTimer::singleShot(0, qApp, SLOT(quit()));
return -1;
}
int index = rtnl_link_name2i(link_cache, interfaceData);
nl_cache_free(link_cache);
nl_socket_free(sock);
return index;
}