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.
147 lines
3.6 KiB
147 lines
3.6 KiB
// gcc -Wall -Werror -O2 -s -pedantic-errors -std=gnu99 -I/usr/include/libnl3 -o nl-neigh-list nl-neigh-list.c -lnl-route-3 -lnl-3 -lnl-cli-3
|
|
// armeb-linux-gcc -Wall -Werror -O2 -s -std=gnu99 -I/usr/include/libnl3 -o nl-neigh-list nl-neigh-list.c -L. -lnl-route-3 -lnl-3 -lnl-cli-3 -lnl-nf-3 -lnl-genl-3
|
|
// with libs static compiled: armeb-linux-gcc -Wall -Werror -O2 -s -std=gnu99 -I/usr/include/libnl3 -o nl-neigh-list nl-neigh-list.c -L. -Wl,-Bstatic -lnl-route-3 -lnl-3 -lnl-cli-3 -lnl-nf-3 -lnl-genl-3 -Wl,-Bdynamic -lpthread -ldl -lm
|
|
|
|
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 "ip.h"
|
|
#include <iostream>
|
|
#include <QStringList>
|
|
|
|
#define BUFLEN 65535
|
|
#define MACBUFLEN 18
|
|
#define ADDRBUFLEN BUFLEN
|
|
|
|
IP::IP() :
|
|
QObject(0)
|
|
{
|
|
|
|
}
|
|
|
|
QStringList IP::addresses(const char *interface) {
|
|
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];
|
|
|
|
memset(¶ms, 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 *ipInterface = strndup(interface, strlen(interface));
|
|
char *family = const_cast<char*>("inet");
|
|
|
|
sock = nl_cli_alloc_socket();
|
|
nl_cli_connect(sock, NETLINK_ROUTE);
|
|
addr_cache = nl_cli_addr_alloc_cache(sock);
|
|
link_cache = nl_cli_link_alloc_cache(sock);
|
|
addr = nl_cli_addr_alloc();
|
|
|
|
nl_cli_addr_parse_family(addr, family);
|
|
nl_cli_addr_parse_dev(addr, link_cache, ipInterface);
|
|
|
|
memset(buf, 0, BUFLEN);
|
|
memset(addrs, 0, ADDRBUFLEN);
|
|
|
|
nl_cache_dump_filter(addr_cache, ¶ms, OBJ_CAST(addr));
|
|
|
|
buf[BUFLEN - 1] = '\0';
|
|
|
|
QString addrsString = buf;
|
|
QStringList addrsLines = addrsString.split('\n');
|
|
QStringList addrsList;
|
|
|
|
foreach(QString addrString, addrsLines) {
|
|
QStringList fields = addrString.split(' ');
|
|
|
|
if(fields.count() > 0) {
|
|
QString address = fields.at(0);
|
|
|
|
if(!address.isEmpty()) {
|
|
addrsList << fields.at(0);
|
|
}
|
|
}else{
|
|
std::cerr << "empty line: " << qPrintable(fields.join(' ')) << std::endl;
|
|
}
|
|
}
|
|
|
|
nl_object_free(OBJ_CAST(addr));
|
|
nl_cache_free(addr_cache);
|
|
nl_cache_free(link_cache);
|
|
nl_socket_free(sock);
|
|
free(ipInterface);
|
|
|
|
return addrsList;
|
|
}
|
|
|
|
QString IP::macOfIP(const char *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];
|
|
|
|
memset(¶ms, 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));
|
|
|
|
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);
|
|
|
|
memset(buf, 0, BUFLEN);
|
|
memset(mac, 0, MACBUFLEN);
|
|
|
|
nl_cache_dump_filter(neigh_cache, ¶ms, OBJ_CAST(neigh));
|
|
|
|
//char data[128] = {0};
|
|
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);
|
|
free(ipAddr);
|
|
|
|
if(mac == NULL || count == 0) {
|
|
return "00:00:00:00:00:00";
|
|
}
|
|
|
|
QString macStr = mac;
|
|
|
|
return macStr;
|
|
}
|
|
|