Brad Parker
11 years ago
commit
bd422aa3fe
3 changed files with 113 additions and 0 deletions
@ -0,0 +1,76 @@ |
|||
// 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 <pthread.h> |
|||
} |
|||
|
|||
#include "ip.h" |
|||
|
|||
IP::IP() : |
|||
QObject(0), |
|||
m_buf(), |
|||
m_mac() |
|||
{ |
|||
|
|||
} |
|||
|
|||
const char* IP::get_mac(const char *ip) { |
|||
struct nl_sock *sock; |
|||
struct rtnl_neigh *neigh; |
|||
struct nl_cache *neigh_cache; |
|||
struct nl_dump_params params; |
|||
|
|||
memset(¶ms, 0, sizeof(struct nl_dump_params)); |
|||
|
|||
params.dp_type = NL_DUMP_LINE; |
|||
params.dp_fd = NULL; |
|||
params.dp_buf = m_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(m_buf, 0, BUFLEN); |
|||
memset(m_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(m_buf, " ", &rest); |
|||
|
|||
while(field != NULL) { |
|||
++count; |
|||
|
|||
if(count == 5) { |
|||
if(strstr(field, ":") != NULL) { |
|||
memcpy(m_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(m_mac == NULL || count == 0) { |
|||
return "00:00:00:00:00:00"; |
|||
} |
|||
|
|||
return m_mac; |
|||
} |
@ -0,0 +1,21 @@ |
|||
#ifndef IP_H |
|||
#define IP_H |
|||
|
|||
#include <QObject> |
|||
|
|||
#define BUFLEN 65535 |
|||
#define MACBUFLEN 18 |
|||
|
|||
class Q_DECL_EXPORT IP : public QObject { |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
IP(); |
|||
const char* get_mac(const char *ip); |
|||
|
|||
private: |
|||
char m_buf[BUFLEN]; |
|||
char m_mac[MACBUFLEN]; |
|||
}; |
|||
|
|||
#endif // IP
|
@ -0,0 +1,16 @@ |
|||
###################################################################### |
|||
# Automatically generated by qmake (3.0) Wed Aug 14 06:38:20 2013 |
|||
###################################################################### |
|||
|
|||
QT += core |
|||
TEMPLATE = lib |
|||
TARGET = ip |
|||
INCLUDEPATH += /usr/include/libnl3 |
|||
|
|||
QMAKE_CXXFLAGS += -Wall -Wextra -Werror |
|||
QMAKE_CFLAGS += -Wall -Wextra -Werror |
|||
|
|||
LIBS += -lnl-route-3 -lnl-3 -lnl-cli-3 |
|||
|
|||
HEADERS += ip.h |
|||
SOURCES += ip.cpp |
Loading…
Reference in new issue