]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
allow linking against libnl 2.0
authorPat Erley <pat-lkml@erley.org>
Sat, 13 Dec 2008 00:04:05 +0000 (01:04 +0100)
committerJohannes Berg <johannes@sipsolutions.net>
Sat, 13 Dec 2008 00:04:05 +0000 (01:04 +0100)
This converts iw to use libnl-2, and adds compatibility with libnl-1.
There is not currently a good way to detect the libnl version during
compilation, as the versioning in the netlink/version.h is defined as a
string "2.0" rather than a major and a minor number, so we must detect
it in the Makefile.

Signed-off-by: Pat Erley <pat-lkml@erley.org>
Makefile
iw.c
iw.h

index 8a97bae4e4cff265b282076af6f5699d4a041f0a..e9473c83534f4c0edd4ee85983581f7e3499793a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,14 +10,28 @@ MKDIR ?= mkdir -p
 INSTALL ?= install
 CC ?= "gcc"
 
-CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration `pkg-config --cflags libnl-1`
+CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration
 CFLAGS += -O2 -g
-LIBS += `pkg-config --libs libnl-1`
-NLVERSION = 1.0
 
 OBJS = iw.o info.o phy.o interface.o station.o util.o mpath.o reg.o mesh.o genl.o
 ALL = iw
 
+NL1FOUND := $(shell pkg-config --atleast-version=1 libnl-1 && echo Y)
+NL2FOUND := $(shell pkg-config --atleast-version=2 libnl-2.0 && echo Y)
+
+ifeq ($(NL1FOUND),Y)
+NLLIBNAME = libnl-1
+endif
+
+ifeq ($(NL2FOUND),Y)
+CFLAGS += -DCONFIG_LIBNL20
+LIBS += -lnl-genl
+NLLIBNAME = libnl-2.0
+endif
+
+LIBS += `pkg-config --libs $(NLLIBNAME)`
+CFLAGS += `pkg-config --cflags $(NLLIBNAME)`
+
 ifeq ($(V),1)
 Q=
 NQ=true
@@ -29,8 +43,13 @@ endif
 all: version_check $(ALL)
 
 version_check:
-       @if ! pkg-config --atleast-version=$(NLVERSION) libnl-1; then echo "You need at least libnl version $(NLVERSION)"; exit 1; fi
-
+ifeq ($(NL2FOUND),Y)
+else
+ifeq ($(NL1FOUND),Y)
+else
+       $(error No libnl found)
+endif
+endif
 
 version.h: version.sh
        @$(NQ) ' GEN  version.h'
diff --git a/iw.c b/iw.c
index afae6437f89b82c02e6c7e775af0ec19e9333f16..117399c63ec898cb20cb830ccc531992a02f0271 100644 (file)
--- a/iw.c
+++ b/iw.c
 #include "iw.h"
 #include "version.h"
 
+#ifndef CONFIG_LIBNL20
+/* libnl 2.0 compatibility code */
+
+static inline struct nl_handle *nl_socket_alloc(void)
+{
+       return nl_handle_alloc();
+}
+
+static inline void nl_socket_free(struct nl_handle *h)
+{
+       nl_handle_destroy(h);
+}
+
+static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
+{
+       struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
+       if (!tmp)
+               return -ENOMEM;
+       *cache = tmp;
+       return 0;
+}
+#define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
+#endif /* CONFIG_LIBNL20 */
+
 static int debug = 0;
 
 static int nl80211_init(struct nl80211_state *state)
 {
        int err;
 
-       state->nl_handle = nl_handle_alloc();
+       state->nl_handle = nl_socket_alloc();
        if (!state->nl_handle) {
                fprintf(stderr, "Failed to allocate netlink handle.\n");
                return -ENOMEM;
@@ -41,8 +65,7 @@ static int nl80211_init(struct nl80211_state *state)
                goto out_handle_destroy;
        }
 
-       state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
-       if (!state->nl_cache) {
+       if (genl_ctrl_alloc_cache(state->nl_handle, &state->nl_cache)) {
                fprintf(stderr, "Failed to allocate generic netlink cache.\n");
                err = -ENOMEM;
                goto out_handle_destroy;
@@ -60,7 +83,7 @@ static int nl80211_init(struct nl80211_state *state)
  out_cache_free:
        nl_cache_free(state->nl_cache);
  out_handle_destroy:
-       nl_handle_destroy(state->nl_handle);
+       nl_socket_free(state->nl_handle);
        return err;
 }
 
@@ -68,7 +91,7 @@ static void nl80211_cleanup(struct nl80211_state *state)
 {
        genl_family_put(state->nl80211);
        nl_cache_free(state->nl_cache);
-       nl_handle_destroy(state->nl_handle);
+       nl_socket_free(state->nl_handle);
 }
 
 __COMMAND(NULL, NULL, NULL, 0, 0, 0, CIB_NONE, NULL);
diff --git a/iw.h b/iw.h
index 0ac454939064cfdf66b7adb0236ee2d26fa1f6bf..14b7fa5b5b810ce77e125de3090459edc78bdc1e 100644 (file)
--- a/iw.h
+++ b/iw.h
 #define ETH_ALEN 6
 
 struct nl80211_state {
+#ifdef CONFIG_LIBNL20
+       struct nl_sock *nl_handle;
+#else
        struct nl_handle *nl_handle;
+#endif
        struct nl_cache *nl_cache;
        struct genl_family *nl80211;
 };