From 176abdb332048da8575e9113171ea15663ba5685 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zsombor=20Bodn=C3=A1r?= <zsombor.bodnar@kszk.bme.hu>
Date: Wed, 4 May 2022 20:43:17 +0200
Subject: [PATCH] Refactor

Delete g_pg_sql_connection pointer when the library unloads
---
 README.md                  | 2 +-
 src/callouts.cc            | 8 ++++----
 src/framework_functions.cc | 6 ++++++
 src/globals.h              | 3 ---
 src/logger.h               | 2 +-
 5 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index c5b1e40..09b7122 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# kea-hook
+# isc-kea-hook
 
 Hook library for ISC Kea(https://kea.isc.org/), the library controls MUEB IP assignment.
 
diff --git a/src/callouts.cc b/src/callouts.cc
index f01e974..d3c2885 100644
--- a/src/callouts.cc
+++ b/src/callouts.cc
@@ -128,7 +128,7 @@ int lease4_select(isc::hooks::CalloutHandle& handle) {
   handle.getArgument("fake_allocation", fake_allocation);
 
   LOG_DEBUG(kea_hook_logger, 0, KEA_HOOK_DHCP_STATE)
-      .arg((fake_allocation) ? "---[DISCOVER]---" : "---[REQUEST]---");
+      .arg(fake_allocation ? "---[DISCOVER]---" : "---[REQUEST]---");
 
   const auto& hwaddr_ptr = query4_ptr->getHWAddr();
   const auto& mac_address = hwaddr_ptr->toText(false);
@@ -138,7 +138,7 @@ int lease4_select(isc::hooks::CalloutHandle& handle) {
       hwaddr_ptr->hwaddr_[2] != 0xEC) {
     LOG_INFO(kea_hook_logger, KEA_HOOK_NOT_MUEB).arg(mac_address);
     LOG_DEBUG(kea_hook_logger, 0, KEA_HOOK_DHCP_STATE)
-        .arg((fake_allocation) ? "---<DISCOVER>---" : "---<REQUEST>---");
+        .arg(fake_allocation ? "---<DISCOVER>---" : "---<REQUEST>---");
 
     return 0;
   }
@@ -194,7 +194,7 @@ int lease4_select(isc::hooks::CalloutHandle& handle) {
 
     LOG_DEBUG(kea_hook_logger, 0, KEA_HOOK_QUERIED_IP).arg(ip_address);
     LOG_DEBUG(kea_hook_logger, 0, KEA_HOOK_DHCP_STATE)
-        .arg((fake_allocation) ? "---<DISCOVER>---" : "---<REQUEST>---");
+        .arg(fake_allocation ? "---<DISCOVER>---" : "---<REQUEST>---");
 
     // Save to DB when the DHCP state is DHCPREQUEST
     if (!fake_allocation) {
@@ -245,7 +245,7 @@ int lease4_renew(isc::hooks::CalloutHandle& handle) {
   handle.getArgument("query4", query4Ptr);
 
   const auto& isRequest = query4Ptr->getType() == isc::dhcp::DHCPREQUEST;
-  std::string tmp{(isRequest) ? "---[REQUEST" : "---[DISCOVER"};
+  std::string tmp{isRequest ? "---[REQUEST" : "---[DISCOVER"};
 
   LOG_DEBUG(kea_hook_logger, 0, KEA_HOOK_DHCP_STATE).arg(tmp + "(RENEW)]---");
 
diff --git a/src/framework_functions.cc b/src/framework_functions.cc
index 6b43f81..e570647 100644
--- a/src/framework_functions.cc
+++ b/src/framework_functions.cc
@@ -100,5 +100,11 @@ int load(isc::hooks::LibraryHandle& handle) {
   return 0;
 }
 
+int unload() {
+  delete g_pg_sql_connection;
+
+  return 0;
+}
+
 int multi_threading_compatible() { return 0; }
 }
\ No newline at end of file
diff --git a/src/globals.h b/src/globals.h
index 070f70c..ad74961 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -3,9 +3,6 @@
 
 #include <pgsql/pgsql_connection.h>
 
-#include <map>
-#include <string>
-
 extern isc::db::PgSqlConnection* g_pg_sql_connection;
 
 #endif  // KEA_HOOK_SRC_GLOBALS_H_
\ No newline at end of file
diff --git a/src/logger.h b/src/logger.h
index 34e21c3..f8ac638 100644
--- a/src/logger.h
+++ b/src/logger.h
@@ -9,4 +9,4 @@
 
 extern isc::log::Logger kea_hook_logger;
 
-#endif // KEA_HOOK_SRC_LOGGER_H_
\ No newline at end of file
+#endif  // KEA_HOOK_SRC_LOGGER_H_
\ No newline at end of file
-- 
GitLab