Subject: [PATCH] net-snmp: tcpTable-Libnl2 compact patch

---
 agent/mibgroup/mibII/tcpTable.c | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/agent/mibgroup/mibII/tcpTable.c b/agent/mibgroup/mibII/tcpTable.c
index 38589f9..bd5e280 100644
--- a/agent/mibgroup/mibII/tcpTable.c
+++ b/agent/mibgroup/mibII/tcpTable.c
@@ -632,11 +632,29 @@ static void nl_socket_free(struct nl_handle *ns)
 }
 #endif /* HAVE_LIBNL3 */
 
+/*
+  libnl 2.x defines LIBNL_VERSION 3.x is a different beast with LIBNL_VER_MAJ.
+  But for our purposes here, the api used is the same.  Additionally, and the
+  CFLAGS and LDFLAGS would differ for libnl-3 anyway (hopefully not enabling
+  the HAVE_ define).
+*/
+#ifdef LIBNL_VERSION
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE nl_sock
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE_ALLOC nl_socket_alloc
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE nl_socket_free
+  #define TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(x) (x)
+#else
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE nl_handle
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE_ALLOC nl_handle_alloc
+  #define TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE nl_handle_destroy
+  #define TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(x)
+#endif
+
 static int
 tcpTable_load_netlink(void)
 {
 	/* TODO: perhaps use permanent nl socket ? */
-	struct nl_sock *nl = nl_socket_alloc();
+	struct TCPTABLE_LIBNL_STRUCT_NL_HANDLE *nl = TCPTABLE_LIBNL_STRUCT_NL_HANDLE_ALLOC();
 	struct inet_diag_req req = {
 		.idiag_family = AF_INET,
 		.idiag_states = TCP_ALL,
@@ -656,9 +674,9 @@ tcpTable_load_netlink(void)
 
 	err = nl_connect(nl, NETLINK_INET_DIAG);
 	if (err < 0) {
-		DEBUGMSGTL(("mibII/tcpTable", "Failed to connect to netlink: %s\n", nl_geterror(err)));
-		snmp_log(LOG_ERR, "snmpd: Couldn't connect to netlink: %s\n", nl_geterror(err));
-		nl_socket_free(nl);
+                DEBUGMSGTL(("mibII/tcpTable", "Failed to connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err))));
+                snmp_log(LOG_ERR, "snmpd: Couldn't connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err)));
+                TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE(nl);
 		return -1;
 	}
 
@@ -667,9 +685,9 @@ tcpTable_load_netlink(void)
 
 	err = nl_send_auto_complete(nl, nm);
 	if (err < 0) {
-		DEBUGMSGTL(("mibII/tcpTable", "nl_send_autocomplete(): %s\n", nl_geterror(err)));
-		snmp_log(LOG_ERR, "snmpd: nl_send_autocomplete(): %s\n", nl_geterror(err));
-		nl_socket_free(nl);
+                DEBUGMSGTL(("mibII/tcpTable", "Failed to connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err))));
+                snmp_log(LOG_ERR, "snmpd: Couldn't connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err)));
+                TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE(nl);
 		return -1;
 	}
 	nlmsg_free(nm);
@@ -677,10 +695,10 @@ tcpTable_load_netlink(void)
 	while (running) {
 		struct nlmsghdr *h;
 		if ((len = nl_recv(nl, &peer, &buf, NULL)) <= 0) {
-			DEBUGMSGTL(("mibII/tcpTable", "nl_recv(): %s\n", nl_geterror(len)));
-			snmp_log(LOG_ERR, "snmpd: nl_recv(): %s\n", nl_geterror(len));
-			nl_socket_free(nl);
-			return -1;
+                        DEBUGMSGTL(("mibII/tcpTable", "Failed to connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err))));
+                        snmp_log(LOG_ERR, "snmpd: Couldn't connect to netlink: %s\n", nl_geterror(TCPTABLE_LIBNL_STRUCT_NL_ERROR_ARG(err)));
+                        TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE(nl);
+                        return -1;
 		}
 
 		h = (struct nlmsghdr*)buf;
@@ -738,7 +756,7 @@ tcpTable_load_netlink(void)
 		free(buf);
 	}
 
-	nl_socket_free(nl);
+        TCPTABLE_LIBNL_STRUCT_NL_HANDLE_FREE(nl);
 
 	if (tcp_head) {
 		DEBUGMSGTL(("mibII/tcpTable", "Loaded TCP Table using netlink\n"));
-- 
2.7.4

