From 3de0d359ebc0350f3b9d318e8bff0ec566b34a37 Mon Sep 17 00:00:00 2001
From: Benny Baumann <BenBE@geshi.org>
Date: Wed, 30 Dec 2020 15:42:14 +0100
Subject: [PATCH] Basic interface for using libnl3 (for interface monitoring)

---
 Makefile              |  3 +++
 src/app/mumta.cpp     |  4 ++++
 src/app/mumta.ldflags |  1 +
 src/lib/nl/nl.cpp     | 34 ++++++++++++++++++++++++++++++++++
 src/lib/nl/nl.hpp     |  3 +++
 5 files changed, 45 insertions(+)
 create mode 100644 src/lib/nl/nl.cpp
 create mode 100644 src/lib/nl/nl.hpp

diff --git a/Makefile b/Makefile
index 7c25e5a..e816a01 100644
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,9 @@ CFLAGS += -Wno-error=sign-conversion
 CXXFLAGS += -Wno-error=sign-conversion
 endif
 
+CFLAGS += `pkg-config --cflags libnl-3.0`
+CXXFLAGS += `pkg-config --cflags libnl-3.0`
+
 CC ?= gcc
 CXX ?= g++
 
diff --git a/src/app/mumta.cpp b/src/app/mumta.cpp
index 002e811..001c8fa 100644
--- a/src/app/mumta.cpp
+++ b/src/app/mumta.cpp
@@ -4,6 +4,7 @@
 #include <thread>
 
 #include "lib/ev/ev.hpp"
+#include "lib/nl/nl.hpp"
 
 #include "mumta/evloop.hpp"
 
@@ -14,6 +15,9 @@ int main() {
     if(!check_version_libev()) {
         return 1;
     }
+    if (!check_version_libnl()) {
+        return 1;
+    }
 
     dctl_status_msg("Initializing");
     dctl_status_msg("Reading configuration");
diff --git a/src/app/mumta.ldflags b/src/app/mumta.ldflags
index 5cee85c..8c91bf4 100644
--- a/src/app/mumta.ldflags
+++ b/src/app/mumta.ldflags
@@ -1,3 +1,4 @@
 `pkg-config --cflags --libs libsystemd`
+`pkg-config --cflags --libs libnl-3.0`
 -lev
 -pthread
diff --git a/src/lib/nl/nl.cpp b/src/lib/nl/nl.cpp
new file mode 100644
index 0000000..f8b14f9
--- /dev/null
+++ b/src/lib/nl/nl.cpp
@@ -0,0 +1,34 @@
+#include <lib/nl/nl.hpp>
+
+#include <iomanip>
+#include <sstream>
+#include <string>
+
+#include <netlink/utils.h>
+
+#include "service/daemonctl.hpp"
+
+bool check_version_libnl()
+{
+    auto nl_major{nl_ver_maj};
+    auto nl_minor{nl_ver_min};
+
+    constexpr auto exp_major{LIBNL_VER_MAJ};
+    constexpr auto exp_minor{LIBNL_VER_MIN};
+
+    std::stringstream str;
+    str << "Checking dependency: libnl: detected " << std::dec << nl_major << "." << std::setw(2) << std::setfill('0') << nl_minor << ", compiled " << std::dec << exp_major << "." << std::setw(2) << std::setfill('0') << exp_minor;
+    dctl_status_msg(str.str().c_str());
+
+    if (nl_major != exp_major) {
+        dctl_status_err("Checking dependency: libnl: failed version check: Major API version mismatch.\n");
+        return false;
+    }
+
+    if (nl_minor < exp_minor) {
+        dctl_status_err("Checking dependency: libnl: failed version check: Minor API version too old.\n");
+        return false;
+    }
+
+    return true;
+}
diff --git a/src/lib/nl/nl.hpp b/src/lib/nl/nl.hpp
new file mode 100644
index 0000000..8f0b05a
--- /dev/null
+++ b/src/lib/nl/nl.hpp
@@ -0,0 +1,3 @@
+#pragma once
+
+bool check_version_libnl();
-- 
GitLab