From 91c994737736057f0050b824e354bb8c44212fe4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zsombor=20Bodn=C3=A1r?= <zsombor.bodnar@kszk.bme.hu>
Date: Sun, 19 Sep 2021 17:04:07 +0200
Subject: [PATCH] SM-19 Don't drop packet when multiple MUEBs exists in the
 same room

---
 sql/prepared_statements.sql | 2 +-
 src/callouts.cc             | 4 +---
 src/framework_functions.cc  | 6 ++++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/sql/prepared_statements.sql b/sql/prepared_statements.sql
index bf14b59..b39ed51 100644
--- a/sql/prepared_statements.sql
+++ b/sql/prepared_statements.sql
@@ -17,7 +17,7 @@ select ip_override from mueb where mac_address = :mac and ip_override is not nul
 select ip_address from port p join room r using(room_id) where p.port_id = :port_id and p.switch_id = :switch_id;
 
 -- clear_port
-update mueb set port_id = null, switch_id = null where port_id = :port_id and switch_id = :switch_id;
+update mueb m set port_id   = null, switch_id = null from port p where m.port_id = p.port_id and m.switch_id = p.switch_id and p.room_id = (select r.room_id from room r join port p using (room_id) where p.port_id = :port_id and p.switch_id = :switch_id);
 
 -- insert_mueb
 insert into mueb (mac_address) values (:mac_address) on conflict (mac_address) do update set port_id = :port_id, switch_id = :switch_id where mueb.mac_address = :mac_address;
diff --git a/src/callouts.cc b/src/callouts.cc
index aae944d..f01e974 100644
--- a/src/callouts.cc
+++ b/src/callouts.cc
@@ -90,7 +90,7 @@ int pkt4_receive(isc::hooks::CalloutHandle& handle) {
   values[0] = mac_address.c_str();
   values[1] = room_id;
 
-  // Drop packet if multiple MUEBs(excluding current) is in the same room
+  // Check if multiple MUEBs(excluding current) is in the same room
   isc::db::PgSqlResult r2(PQexecPrepared(*g_pg_sql_connection,
                                          "mueb_count_in_room", 2, values,
                                          nullptr, nullptr, 0));
@@ -98,8 +98,6 @@ int pkt4_receive(isc::hooks::CalloutHandle& handle) {
   try {
     if (std::stoi(PQgetvalue(r2, 0, 0)) > 0) {
       LOG_WARN(kea_hook_logger, KEA_HOOK_MULTIPLE_MUEB).arg(room_id);
-      handle.setStatus(isc::hooks::CalloutHandle::NEXT_STEP_DROP);
-      return 0;
     }
 
     isc::db::PgSqlResult r3(PQexecPrepared(*g_pg_sql_connection, "ip_conflict",
diff --git a/src/framework_functions.cc b/src/framework_functions.cc
index 6ff9045..6b43f81 100644
--- a/src/framework_functions.cc
+++ b/src/framework_functions.cc
@@ -72,8 +72,10 @@ int load(isc::hooks::LibraryHandle& handle) {
          {2,
           {isc::db::OID_TEXT, isc::db::OID_TEXT},
           "clear_port",
-          "update mueb set port_id = null, switch_id = null where port_id = $1 "
-          "and switch_id = $2"},
+          "update mueb m set port_id   = null, switch_id = null from port p "
+          "where m.port_id = p.port_id and m.switch_id = p.switch_id and "
+          "p.room_id = (select r.room_id from room r join port p using "
+          "(room_id) where p.port_id = $1 and p.switch_id = $2)"},
          {3,
           {829, isc::db::OID_TEXT, isc::db::OID_TEXT},
           "insert_or_update_mueb",
-- 
GitLab