From 80c0706dd938260f203399762e0072b2e5d6ce39 Mon Sep 17 00:00:00 2001 From: Brad Parker Date: Tue, 27 Aug 2013 10:02:02 -0400 Subject: [PATCH] remove Utils class's CLI dependency, add extra error checking and hide Utils class from public view --- interface.cpp | 1 - interface.h | 3 ++- libip.pro | 2 ++ libip_global.h | 15 +++++++++++++++ neighbor.h | 3 ++- utils.cpp | 26 +++++++++++++------------- utils.h | 3 ++- wireless.h | 3 ++- 8 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 libip_global.h diff --git a/interface.cpp b/interface.cpp index ed00d31..9500d1a 100644 --- a/interface.cpp +++ b/interface.cpp @@ -8,7 +8,6 @@ extern "C" { #include #include #include - #include #include #include diff --git a/interface.h b/interface.h index 0b5dc48..de9f463 100644 --- a/interface.h +++ b/interface.h @@ -2,8 +2,9 @@ #define LIBIP_INTERFACE_H #include +#include "libip_global.h" -class Q_DECL_EXPORT Interface : public QObject { +class LIBIP_EXPORT Interface : public QObject { Q_OBJECT public: diff --git a/libip.pro b/libip.pro index 8745e71..13eaed6 100644 --- a/libip.pro +++ b/libip.pro @@ -7,6 +7,8 @@ TEMPLATE = lib TARGET = ip INCLUDEPATH += /usr/include/libnl3 +DEFINES += LIBIP_LIBRARY + QMAKE_CXXFLAGS += -Wall -Wextra -Werror -fsanitize=address -fPIE QMAKE_CFLAGS += -Wall -Wextra -Werror -fsanitize=address -fPIE diff --git a/libip_global.h b/libip_global.h new file mode 100644 index 0000000..0ca9592 --- /dev/null +++ b/libip_global.h @@ -0,0 +1,15 @@ +#include + +#if defined(LIBIP_LIBRARY) +# define LIBIP_EXPORT Q_DECL_EXPORT +#else +# define LIBIP_EXPORT Q_DECL_IMPORT +#endif + +#ifdef WIN32 +#define LIBIP_LOCAL +#define LIBIP_PUBLIC +#else +#define LIBIP_LOCAL __attribute__((visibility("hidden"))) +#define LIBIP_PUBLIC __attribute__((visibility("default"))) +#endif diff --git a/neighbor.h b/neighbor.h index a29862b..d7fb164 100644 --- a/neighbor.h +++ b/neighbor.h @@ -2,8 +2,9 @@ #define LIBIP_NEIGHBOR_H #include +#include "libip_global.h" -class Q_DECL_EXPORT Neighbor : public QObject { +class LIBIP_EXPORT Neighbor : public QObject { Q_OBJECT public: diff --git a/utils.cpp b/utils.cpp index 2048762..698c787 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,9 +1,10 @@ extern "C" { -#include -#include -#include -#include #include +#include +#include +#include +#include +#include } #include "utils.h" @@ -70,19 +71,18 @@ int Utils::alloc_neigh_cache(struct nl_sock *sock, struct nl_cache **cache) { int Utils::interfaceIndex(QString interface) { QByteArray array = interface.toLatin1(); + struct nl_sock *sock = Utils::connect(); - struct nl_sock *sock = nl_cli_alloc_socket(); - nl_cli_connect(sock, NETLINK_ROUTE); + if(!sock) { + std::cerr << "could not allocate socket" << std::endl; + return -1; + } 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())); + struct nl_cache *link_cache = NULL; + if(!(Utils::alloc_link_cache(sock, &link_cache))) { + nl_socket_free(sock); return -1; } diff --git a/utils.h b/utils.h index 655534d..238e975 100644 --- a/utils.h +++ b/utils.h @@ -2,8 +2,9 @@ #define LIBIP_UTILS_H #include +#include "libip_global.h" -class Q_DECL_EXPORT Utils : public QObject { +class LIBIP_LOCAL Utils : public QObject { Q_OBJECT public: diff --git a/wireless.h b/wireless.h index b16a316..77de780 100644 --- a/wireless.h +++ b/wireless.h @@ -3,8 +3,9 @@ #include #include +#include "libip_global.h" -class Q_DECL_EXPORT Wireless : public QObject { +class LIBIP_EXPORT Wireless : public QObject { Q_OBJECT public: