From b5ed556db2d2f65173dc345c65d2543c39139a15 Mon Sep 17 00:00:00 2001
From: Dmitry Puzyrkov <dmitry.puzyrkov@demlabs.net>
Date: Thu, 18 Apr 2024 15:56:40 +0000
Subject: [PATCH] [+] vpn "common" method for win32, full for others

---
 modules/net/dap_chain_node_cli_cmd.c          |  2 +-
 modules/net/dap_chain_node_cli_cmd_tx.c       |  5 ++-
 .../net/include/dap_chain_node_cli_cmd_tx.h   |  1 +
 modules/service/vpn/CMakeLists.txt            | 10 +++--
 modules/service/vpn/dap_chain_net_srv_vpn.c   | 38 ----------------
 .../vpn/dap_chain_net_srv_vpn_common.c        | 44 +++++++++++++++++++
 .../vpn/include/dap_chain_net_srv_vpn.h       |  3 +-
 .../include/dap_chain_net_srv_vpn_common.h    |  4 ++
 8 files changed, 61 insertions(+), 46 deletions(-)
 create mode 100644 modules/service/vpn/dap_chain_net_srv_vpn_common.c
 create mode 100644 modules/service/vpn/include/dap_chain_net_srv_vpn_common.h

diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c
index 063dc51bf..f3c00230d 100644
--- a/modules/net/dap_chain_node_cli_cmd.c
+++ b/modules/net/dap_chain_node_cli_cmd.c
@@ -7467,7 +7467,7 @@ int com_tx_history(int a_argc, char ** a_argv, void ** reply)
     } else if (l_addr) {
         // history addr and wallet
         char *l_addr_str = dap_chain_addr_to_str(l_addr);
-        json_obj_out = dap_db_history_addr(l_addr,  l_chain, l_hash_out_type, l_addr_str, l_tx_srv_str, l_action);
+        json_obj_out = dap_db_history_addr(l_addr,  l_chain, l_hash_out_type, l_addr_str, l_brief, l_tx_srv_str, l_action);
         if (!json_obj_out) {
             dap_json_rpc_error_add(DAP_CHAIN_NODE_CLI_COM_TX_HISTORY_DAP_DB_HISTORY_ADDR_ERR,
                                     "something went wrong in tx_history");
diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c
index 8e9c26a61..24df4baf6 100644
--- a/modules/net/dap_chain_node_cli_cmd_tx.c
+++ b/modules/net/dap_chain_node_cli_cmd_tx.c
@@ -339,7 +339,7 @@ static void s_tx_header_print(json_object* json_obj_datum, dap_chain_tx_hash_pro
  * @return char*
  */
 json_object* dap_db_history_addr(dap_chain_addr_t *a_addr,  dap_chain_t *a_chain,
-                                 const char *a_hash_out_type, const char * l_addr_str, const char *a_srv, dap_chain_tx_tag_action_type_t a_action)
+                                 const char *a_hash_out_type, const char * l_addr_str, bool a_brief, const char *a_srv, dap_chain_tx_tag_action_type_t a_action)
 {
 
     json_object* json_obj_datum = json_object_new_array();
@@ -666,7 +666,8 @@ json_object* dap_db_history_addr(dap_chain_addr_t *a_addr,  dap_chain_t *a_chain
             l_is_need_correction = false;
         }
         if (json_object_array_length(j_arr_data) > 0) {
-            json_object_object_add(j_obj_tx, "data", j_arr_data);
+            if (!a_brief)
+                json_object_object_add(j_obj_tx, "data", j_arr_data);
             json_object_array_add(json_obj_datum, j_obj_tx);
         }
         else
diff --git a/modules/net/include/dap_chain_node_cli_cmd_tx.h b/modules/net/include/dap_chain_node_cli_cmd_tx.h
index 14473d4be..08978eecb 100644
--- a/modules/net/include/dap_chain_node_cli_cmd_tx.h
+++ b/modules/net/include/dap_chain_node_cli_cmd_tx.h
@@ -46,6 +46,7 @@ json_object * dap_db_history_addr(dap_chain_addr_t * a_addr,
                                     dap_chain_t * a_chain, 
                                     const char *a_hash_out_type, 
                                     const char * l_addr_str, 
+                                    bool a_brief,
                                     const char *a_srv,
                                     dap_chain_tx_tag_action_type_t a_action);
 
diff --git a/modules/service/vpn/CMakeLists.txt b/modules/service/vpn/CMakeLists.txt
index 04be24a93..42a029788 100644
--- a/modules/service/vpn/CMakeLists.txt
+++ b/modules/service/vpn/CMakeLists.txt
@@ -1,16 +1,20 @@
 cmake_minimum_required(VERSION 3.10)
 project (dap_chain_net_srv_vpn)
   
-file(GLOB DAP_CHAIN_NET_SRV_VPN_SRCS *.c)
-
-file(GLOB DAP_CHAIN_NET_SRV_VPN_HEADERS include/*.h)
 
 if(WIN32)
+  file(GLOB DAP_CHAIN_NET_SRV_VPN_SRCS *common.c)
+  file(GLOB DAP_CHAIN_NET_SRV_VPN_HEADERS include/*common.h)
+
   include_directories(../../../os/win32/)
   include_directories(../3rdparty/wepoll/include/)
   include_directories(../3rdparty/uthash/src/)
   include_directories(../../dap-sdk/3rdparty/json-c/)
   include_directories(../3rdparty/libmagic/src/)
+else()
+  file(GLOB DAP_CHAIN_NET_SRV_VPN_SRCS *.c)
+  file(GLOB DAP_CHAIN_NET_SRV_VPN_HEADERS include/*.h)
+
 endif()
 
 add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_NET_SRV_VPN_SRCS} ${DAP_CHAIN_NET_SRV_VPN_HEADERS})
diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c
index 5f25ca0ad..3a005f656 100644
--- a/modules/service/vpn/dap_chain_net_srv_vpn.c
+++ b/modules/service/vpn/dap_chain_net_srv_vpn.c
@@ -856,38 +856,6 @@ static int s_vpn_tun_init()
     return 0;
 }
 
-static bool s_tag_check_vpn(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,  dap_chain_datum_tx_item_groups_t *a_items_grp, dap_chain_tx_tag_action_type_t *a_action)
-{
-    
-    //VPN  open: have SRV_PAY out with vpn uid
-    
-    if (a_items_grp->items_out_cond_srv_pay) {
-        dap_chain_tx_out_cond_t *l_cond_out = a_items_grp->items_out_cond_srv_pay->data; 
-        if (l_cond_out->header.srv_uid.uint64 == DAP_CHAIN_NET_SRV_VPN_ID)
-           if (a_action) *a_action = DAP_CHAIN_TX_TAG_ACTION_OPEN;
-        return true;
-    }
-    
-    //VPN native use: have IN_COND linked with DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY out with vpn uid
-    
-    if (a_items_grp->items_in_cond) {
-       for (dap_list_t *it = a_items_grp->items_in_cond; it; it = it->next) {
-            dap_chain_tx_in_cond_t *l_tx_in = it->data;
-            dap_chain_tx_out_cond_t *l_tx_out_cond = dap_chain_ledger_get_tx_out_cond_linked_to_tx_in_cond(a_ledger, l_tx_in);
-
-            if (l_tx_out_cond && 
-                l_tx_out_cond->header.subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY &&
-                l_tx_out_cond->header.srv_uid.uint64 == DAP_CHAIN_NET_SRV_VPN_ID) {
-                    if (a_action) *a_action = DAP_CHAIN_TX_TAG_ACTION_USE;
-                    return true;
-            }
-        }
-    }
-
-    return false;
-}
-
-
 /**
  * @brief s_vpn_service_create
  * @param g_config
@@ -928,12 +896,6 @@ static int s_vpn_service_create(dap_config_t * g_config)
 
 }
 
-int dap_chain_net_srv_vpn_pre_init()
-{
-    dap_chain_net_srv_uid_t l_uid = { .uint64 = DAP_CHAIN_NET_SRV_VPN_ID };
-    dap_ledger_service_add(l_uid, "vpn", s_tag_check_vpn);
-    return 0;
-}
 
 /**
  * @brief dap_stream_ch_vpn_init Init actions for VPN stream channel
diff --git a/modules/service/vpn/dap_chain_net_srv_vpn_common.c b/modules/service/vpn/dap_chain_net_srv_vpn_common.c
new file mode 100644
index 000000000..fbfdc2962
--- /dev/null
+++ b/modules/service/vpn/dap_chain_net_srv_vpn_common.c
@@ -0,0 +1,44 @@
+
+#include "dap_chain_net_srv_vpn_common.h"
+#include "dap_chain_ledger.h"
+
+
+static bool s_tag_check_vpn(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,  dap_chain_datum_tx_item_groups_t *a_items_grp, dap_chain_tx_tag_action_type_t *a_action)
+{
+    
+    //VPN  open: have SRV_PAY out with vpn uid
+    
+    if (a_items_grp->items_out_cond_srv_pay) {
+        dap_chain_tx_out_cond_t *l_cond_out = a_items_grp->items_out_cond_srv_pay->data; 
+        if (l_cond_out->header.srv_uid.uint64 == DAP_CHAIN_NET_SRV_VPN_ID)
+           if (a_action) *a_action = DAP_CHAIN_TX_TAG_ACTION_OPEN;
+        return true;
+    }
+    
+    //VPN native use: have IN_COND linked with DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY out with vpn uid
+    
+    if (a_items_grp->items_in_cond) {
+       for (dap_list_t *it = a_items_grp->items_in_cond; it; it = it->next) {
+            dap_chain_tx_in_cond_t *l_tx_in = it->data;
+            dap_chain_tx_out_cond_t *l_tx_out_cond = dap_chain_ledger_get_tx_out_cond_linked_to_tx_in_cond(a_ledger, l_tx_in);
+
+            if (l_tx_out_cond && 
+                l_tx_out_cond->header.subtype == DAP_CHAIN_TX_OUT_COND_SUBTYPE_SRV_PAY &&
+                l_tx_out_cond->header.srv_uid.uint64 == DAP_CHAIN_NET_SRV_VPN_ID) {
+                    if (a_action) *a_action = DAP_CHAIN_TX_TAG_ACTION_USE;
+                    return true;
+            }
+        }
+    }
+
+    return false;
+}
+
+
+
+int dap_chain_net_srv_vpn_pre_init()
+{
+    dap_chain_net_srv_uid_t l_uid = { .uint64 = DAP_CHAIN_NET_SRV_VPN_ID };
+    dap_ledger_service_add(l_uid, "vpn", s_tag_check_vpn);
+    return 0;
+}
diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn.h b/modules/service/vpn/include/dap_chain_net_srv_vpn.h
index 959a2d104..b507f84f4 100644
--- a/modules/service/vpn/include/dap_chain_net_srv_vpn.h
+++ b/modules/service/vpn/include/dap_chain_net_srv_vpn.h
@@ -28,6 +28,7 @@
 #include "dap_config.h"
 #include "dap_chain_net_srv.h"
 #include "dap_events.h"
+#include "dap_chain_net_srv_vpn_common.h"
 
 #define DAP_CHAIN_NET_SRV_VPN_CDB_GDB_PREFIX "srv.vpn"
 
@@ -36,8 +37,6 @@
 
 #define DAP_STREAM_CH_ID_NET_SRV_VPN        'S'
 
-#define DAP_CHAIN_NET_SRV_VPN_ID            0x0000000000000001
-
 #define VPN_PACKET_OP_CODE_CONNECTED        0x000000a9
 #define VPN_PACKET_OP_CODE_CONNECT          0x000000aa
 #define VPN_PACKET_OP_CODE_DISCONNECT       0x000000ab
diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn_common.h b/modules/service/vpn/include/dap_chain_net_srv_vpn_common.h
new file mode 100644
index 000000000..abb3ad778
--- /dev/null
+++ b/modules/service/vpn/include/dap_chain_net_srv_vpn_common.h
@@ -0,0 +1,4 @@
+
+#define DAP_CHAIN_NET_SRV_VPN_ID            0x0000000000000001
+
+int dap_chain_net_srv_vpn_pre_init();
-- 
GitLab