From c0b80761239b3904a261a36ce15cb0442c43d692 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20De=20Keersmaeker?=
 <francois.dekeersmaeker@uclouvain.be>
Date: Fri, 9 Aug 2024 09:08:04 +0000
Subject: [PATCH] Translator: added argument for nfqueue name

---
 .gitignore                                 |  2 --
 CMakeLists.txt                             |  1 -
 src/translator/templates/CMakeLists.txt.j2 | 14 +++++++-------
 src/translator/translator.py               | 11 +++++++++--
 4 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index 712e7d2..d1eba16 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,5 @@
 build
 bin
 
-devices
-
 # Python cache
 __pycache__
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 08bb192..50f80e2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,7 +37,6 @@ set(PARSERS header dns dhcp http igmp ssdp coap)
 
 # Subdirectories containing code
 add_subdirectory(src)
-add_subdirectory(devices)
 IF( NOT OPENWRT_CROSSCOMPILING )
     add_subdirectory(test)
 ENDIF()
diff --git a/src/translator/templates/CMakeLists.txt.j2 b/src/translator/templates/CMakeLists.txt.j2
index b29d1b4..4c39ceb 100644
--- a/src/translator/templates/CMakeLists.txt.j2
+++ b/src/translator/templates/CMakeLists.txt.j2
@@ -4,12 +4,12 @@ cmake_minimum_required(VERSION 3.20)
 set(EXECUTABLE_OUTPUT_PATH ${BIN_DIR})
 
 # Nfqueue C file for device {{device}}
-add_executable({{device}} nfqueues.c)
-target_link_libraries({{device}} pthread)
+add_executable({{nfqueue_name}} nfqueues.c)
+target_link_libraries({{nfqueue_name}} pthread)
 IF( OPENWRT_CROSSCOMPILING )
-target_link_libraries({{device}} jansson mnl nfnetlink nftnl nftables netfilter_queue netfilter_log)
+target_link_libraries({{nfqueue_name}} jansson mnl nfnetlink nftnl nftables netfilter_queue netfilter_log)
 ENDIF()
-target_link_libraries({{device}} nfqueue packet_utils rule_utils)
-target_link_libraries({{device}} ${PARSERS})
-target_include_directories({{device}} PRIVATE ${INCLUDE_DIR} ${INCLUDE_PARSERS_DIR})
-install(TARGETS {{device}} DESTINATION ${EXECUTABLE_OUTPUT_PATH})
+target_link_libraries({{nfqueue_name}} nfqueue packet_utils rule_utils)
+target_link_libraries({{nfqueue_name}} ${PARSERS})
+target_include_directories({{nfqueue_name}} PRIVATE ${INCLUDE_DIR} ${INCLUDE_PARSERS_DIR})
+install(TARGETS {{nfqueue_name}} DESTINATION ${EXECUTABLE_OUTPUT_PATH})
diff --git a/src/translator/translator.py b/src/translator/translator.py
index 336aad1..6bf4f35 100644
--- a/src/translator/translator.py
+++ b/src/translator/translator.py
@@ -128,6 +128,7 @@ if __name__ == "__main__":
     description = "Translate a device YAML profile to the corresponding pair of NFTables firewall script and NFQueue C source code."
     parser = argparse.ArgumentParser(description=description)
     parser.add_argument("profile", type=str, help="Path to the device YAML profile")
+    parser.add_argument("-n", "--name", type=str, help="Name of the device's NFQueue")
     parser.add_argument("-q", "--nfqueue", type=uint16, default=0, help="NFQueue start index for this profile's policies (must be an integer between 0 and 65535)")
     parser.add_argument("-o", "--output", type=directory, help="Output directory for the generated files")
     # Verdict modes
@@ -173,6 +174,9 @@ if __name__ == "__main__":
         # Get device info
         device = profile["device-info"]
 
+        # Set device's NFQueue name if not provided as argument
+        args.name = args.name if args.name is not None else device["name"]
+
         # Base nfqueue id, will be incremented at each interaction
         nfq_id = args.nfqueue
 
@@ -184,7 +188,7 @@ if __name__ == "__main__":
         }
     
     
-        # Loop over the device's individual policies
+        ## Loop over the device's individual policies
         if "single-policies" in profile:
             for policy_name in profile["single-policies"]:
                 profile_data = profile["single-policies"][policy_name]
@@ -259,7 +263,10 @@ if __name__ == "__main__":
                 fw.write(main)
 
             # Create CMake file
-            cmake_dict = {"device": device["name"]}
+            cmake_dict = {
+                "device":  device["name"],
+                "nfqueue_name": args.name
+            }
             env.get_template("CMakeLists.txt.j2").stream(cmake_dict).dump(f"{args.output}/CMakeLists.txt")
 
 
-- 
GitLab