From 72c35dbeb156ecf2436ad90e02d0a9b188a844fa Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Sat, 21 Aug 2021 07:56:30 +0300
Subject: [PATCH] [*] Stable odrers check with CDB

---
 dap-sdk/net/client/dap_client_pvt.c           | 31 +++++-----
 modules/net/dap_chain_node_client.c           |  5 +-
 modules/service/vpn/dap_chain_net_srv_vpn.c   |  1 -
 .../service/vpn/dap_chain_net_vpn_client.c    |  4 --
 .../vpn/include/dap_chain_net_srv_vpn.h       |  1 +
 .../vpn/include/dap_chain_net_srv_vpn_cdb.h   | 36 ------------
 .../include/dap_chain_net_srv_vpn_cdb_auth.h  | 58 -------------------
 .../dap_chain_net_srv_vpn_cdb_server_list.h   | 48 ---------------
 8 files changed, 20 insertions(+), 164 deletions(-)
 delete mode 100644 modules/service/vpn/include/dap_chain_net_srv_vpn_cdb.h
 delete mode 100644 modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h
 delete mode 100644 modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_server_list.h

diff --git a/dap-sdk/net/client/dap_client_pvt.c b/dap-sdk/net/client/dap_client_pvt.c
index 559036d3d3..537221e0b2 100644
--- a/dap-sdk/net/client/dap_client_pvt.c
+++ b/dap-sdk/net/client/dap_client_pvt.c
@@ -615,10 +615,6 @@ static bool s_stage_status_after(dap_client_pvt_t * a_client_pvt)
             //    l_is_last_attempt = true;
             //}
 
-            //if (a_client_pvt->is_always_reconnect ){
-            //    l_is_last_attempt = false;
-            //}
-
             log_it(L_ERROR, "Error state( %s), doing callback if present", dap_client_error_str(a_client_pvt->last_error));
             if(a_client_pvt->stage_status_error_callback)
                 a_client_pvt->stage_status_error_callback(a_client_pvt->client, (void*) l_is_last_attempt);
@@ -639,16 +635,20 @@ static bool s_stage_status_after(dap_client_pvt_t * a_client_pvt)
                         log_it(L_ERROR,"Can't run timer for small delay before the next enc_init request");
                     }
                 } else {
-
-                    log_it(L_INFO, "Too many attempts, reconnect attempt in %d seconds with %s:%u",s_timeout*3,
-                           a_client_pvt->uplink_addr,a_client_pvt->uplink_port);                    // Trying the step again
-                    a_client_pvt->stage_status = STAGE_STATUS_IN_PROGRESS;
-                    a_client_pvt->stage_errors = 0;
-
-                    // bigger delay before next request
-                    if(dap_timerfd_start( s_timeout*3000,(dap_timerfd_callback_t) s_stage_status_after,
-                                                   a_client_pvt ) == NULL){
-                        log_it(L_ERROR,"Can't run timer for bigger delay before the next enc_init request");
+                    if (a_client_pvt->is_always_reconnect) {
+                        log_it(L_INFO, "Too many attempts, reconnect attempt in %d seconds with %s:%u",s_timeout*3,
+                               a_client_pvt->uplink_addr,a_client_pvt->uplink_port);                    // Trying the step again
+                        a_client_pvt->stage_status = STAGE_STATUS_IN_PROGRESS;
+                        a_client_pvt->stage_errors = 0;
+
+                        // bigger delay before next request
+                        if(dap_timerfd_start( s_timeout*3000,(dap_timerfd_callback_t) s_stage_status_after,
+                                                       a_client_pvt ) == NULL){
+                            log_it(L_ERROR,"Can't run timer for bigger delay before the next enc_init request");
+                        }
+                    } else {
+                        log_it(L_ERROR, "Connet to %s:%u failed", a_client_pvt->uplink_addr, a_client_pvt->uplink_port);
+                        dap_client_delete_mt(a_client_pvt->client);
                     }
                 }
             }
@@ -870,7 +870,8 @@ static void s_request_error(int a_err_code, void * a_obj)
 
     if(l_client_pvt && l_client_pvt->request_error_callback && l_client_pvt->client)
     {
-        if(l_client_pvt && l_client_pvt->request_error_callback && l_client_pvt->client && l_client_pvt->client->_internal)
+        if(l_client_pvt && l_client_pvt->request_error_callback
+                && l_client_pvt->client && l_client_pvt->client->_internal && l_client_pvt->client->_internal == l_client_pvt)
             l_client_pvt->request_error_callback(l_client_pvt->client, a_err_code);
     }
 }
diff --git a/modules/net/dap_chain_node_client.c b/modules/net/dap_chain_node_client.c
index b5db413d42..9c8ffa7d18 100644
--- a/modules/net/dap_chain_node_client.c
+++ b/modules/net/dap_chain_node_client.c
@@ -629,7 +629,7 @@ static bool dap_chain_node_client_connect_internal(dap_chain_node_client_t *a_no
     a_node_client->client = dap_client_new(a_node_client->events, s_stage_status_callback,
             s_stage_status_error_callback);
     a_node_client->keep_connection = true;
-    dap_client_set_is_always_reconnect(a_node_client->client, true);
+    dap_client_set_is_always_reconnect(a_node_client->client, false);
     a_node_client->client->_inheritor = a_node_client;
     dap_client_set_active_channels_unsafe(a_node_client->client, a_active_channels);
 
@@ -694,7 +694,8 @@ void dap_chain_node_client_close(dap_chain_node_client_t *a_client)
         // clean client
         DAP_CLIENT_PVT(a_client->client)->stage_status_error_callback = NULL;
         a_client->client->_inheritor = NULL;
-        dap_events_socket_remove_and_delete_mt(a_client->stream_worker->worker, a_client->esocket_uuid);
+        if (a_client->stream_worker)
+            dap_events_socket_remove_and_delete_mt(a_client->stream_worker->worker, a_client->esocket_uuid);
         dap_client_delete_mt(a_client->client);
 #ifndef _WIN32
         pthread_cond_destroy(&a_client->wait_cond);
diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c
index 5034adb19a..c653df47a1 100644
--- a/modules/service/vpn/dap_chain_net_srv_vpn.c
+++ b/modules/service/vpn/dap_chain_net_srv_vpn.c
@@ -79,7 +79,6 @@
 #include "dap_chain_net_srv.h"
 #include "dap_chain_net_srv_client.h"
 #include "dap_chain_net_srv_vpn.h"
-#include "dap_chain_net_srv_vpn_cdb.h"
 #include "dap_chain_net_srv_stream_session.h"
 #include "dap_chain_net_vpn_client.h"
 #include "dap_chain_net_vpn_client_tun.h"
diff --git a/modules/service/vpn/dap_chain_net_vpn_client.c b/modules/service/vpn/dap_chain_net_vpn_client.c
index 8574096e29..4021249ad7 100644
--- a/modules/service/vpn/dap_chain_net_vpn_client.c
+++ b/modules/service/vpn/dap_chain_net_vpn_client.c
@@ -58,16 +58,12 @@
 #include "dap_chain_mempool.h"
 #include "dap_chain_node_cli.h"
 #include "dap_chain_net_srv_vpn.h"
-#include "dap_chain_net_srv_vpn_cdb.h" // for DAP_CHAIN_NET_SRV_VPN_CDB_GDB_PREFIX
 #include "dap_chain_net_vpn_client.h"
 
 #include "dap_stream_ch_pkt.h"
 #include "dap_stream_ch_chain_net_srv.h"
-//#include "dap_stream_ch_chain_net_srv.h"
 #include "dap_chain_net_vpn_client_tun.h"
 #include "dap_chain_net_srv_vpn_cmd.h"
-#include "dap_chain_net_srv_vpn_cdb_server_list.h"
-//#include "dap_chain_net_vpn_client_data.h"
 #include "dap_modules_dynamic_cdb.h"
 
 /*
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 cb396c9695..810330bb7b 100644
--- a/modules/service/vpn/include/dap_chain_net_srv_vpn.h
+++ b/modules/service/vpn/include/dap_chain_net_srv_vpn.h
@@ -29,6 +29,7 @@
 #include "dap_chain_net_srv.h"
 #include "dap_events.h"
 
+#define DAP_CHAIN_NET_SRV_VPN_CDB_GDB_PREFIX "srv.vpn"
 
 #define DAP_STREAM_CH_PKT_TYPE_NET_SRV_VPN_CLIENT    0x01
 #define DAP_STREAM_CH_PKT_TYPE_NET_SRV_VPN_DATA      0x02
diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb.h b/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb.h
deleted file mode 100644
index 94e0772aa5..0000000000
--- a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Authors:
- * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * CellFrame       https://cellframe.net
- * Sources         https://gitlab.demlabs.net/cellframe
- * Copyright  (c) 2017-2019
- * All rights reserved.
-
- This file is part of CellFrame SDK the open source project
-
-    CellFrame SDK is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    CellFrame SDK is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-#pragma once
-#include "dap_http.h"
-#include "dap_enc_http.h"
-#include "dap_config.h"
-
-#define DAP_CHAIN_NET_SRV_VPN_CDB_GDB_PREFIX "srv.vpn"
-
-extern dap_config_t * g_dap_config_cdb;
-
-int dap_chain_net_srv_vpn_cdb_init(dap_http_t * a_http);
-void dap_chain_net_srv_vpn_cdb_deinit();
-void dap_chain_net_srv_vpn_cdb_auth_after(enc_http_delegate_t* a_delegate, const char * a_login, const char * a_pkey_b64 );
diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h b/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h
deleted file mode 100644
index 820d9f8193..0000000000
--- a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Authors:
- * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * CellFrame       https://cellframe.net
- * Sources         https://gitlab.demlabs.net/cellframe
- * Copyright  (c) 2017-2020
- * All rights reserved.
-
- This file is part of CellFrame SDK the open source project
-
-    CellFrame SDK is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    CellFrame SDK is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-#pragma once
-
-#include "dap_http.h"
-#include "dap_enc_http.h"
-
-typedef struct dap_serial_key {
-    struct {
-        char serial[20];
-        time_t activated; // if set, then serial is activated
-        // if zero then time no expired
-        union{
-            time_t expired;
-            int64_t license_length;// in sec
-        };
-        int32_t pkey_type;// dap_enc_key_type_t pkey type
-        size_t ext_size;
-    }DAP_ALIGN_PACKED header;
-    uint8_t ext[];// pkey here
-}DAP_ALIGN_PACKED dap_serial_key_t;
-
-size_t dap_serial_key_len(dap_serial_key_t *a_serial_key);
-dap_serial_key_t* dap_chain_net_srv_vpn_cdb_auth_get_serial_param(const char *a_serial_str, const char **a_group_out);
-
-int dap_chain_net_srv_vpn_cdb_auth_init (const char * a_domain, const char * a_mode, bool a_is_registration_open);
-void dap_chain_net_srv_vpn_cdb_auth_deinit();
-
-void dap_chain_net_srv_vpn_cdb_auth_add_proc(dap_http_t * a_http, const char * a_url);
-void dap_chain_net_srv_vpn_cdb_auth_set_callback(dap_enc_http_callback_t a_callback_success);
-int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int a_arg_index, int a_argc, char ** a_argv, char **a_str_reply);
-int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_user(const char *a_user_str, int a_arg_index, int a_argc, char ** a_argv, char **a_str_reply);
-
-int dap_chain_net_srv_vpn_cdb_auth_check_login(const char * a_login, const char * a_password);
-int dap_chain_net_srv_vpn_cdb_auth_activate_serial(const char * a_serial_raw, const char * a_serial, const char * a_sign, const char * a_pkey);
-int dap_chain_net_srv_vpn_cdb_auth_check_serial(const char * a_serial, const char * a_pkey);
diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_server_list.h b/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_server_list.h
deleted file mode 100644
index 88878e94fc..0000000000
--- a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_server_list.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Authors:
- * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
- * Alexander Lysikov <alexander.lysikov@demlabs.net>
- * DeM Labs Inc.   https://demlabs.net
- * CellFrame       https://cellframe.net
- * Sources         https://gitlab.demlabs.net/cellframe
- * Copyright  (c) 2017-2019
- * All rights reserved.
-
- This file is part of CellFrame SDK the open source project
-
-    CellFrame SDK is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    CellFrame SDK is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#pragma once
-
-#include "dap_enc_http.h"
-#include "dap_chain_net.h"
-#include <stdbool.h>
-
-struct dap_http;
-
-typedef struct dap_orders_callback_params {
-    dap_chain_net_t *net;
-    int multiplicity;
-} dap_orders_callback_params_t;
-
-int get_order_state(dap_chain_node_addr_t a_node_addr);
-
-int dap_chain_net_srv_vpn_cdb_server_list_init(void);
-void dap_chain_net_srv_vpn_cdb_server_list_deinit(void);
-void dap_chain_net_srv_vpn_cdb_server_list_add_proc(struct dap_http * sh, const char * url);
-
-void dap_chain_net_srv_vpn_cdb_server_list_cache_reset(void);
-int dap_chain_net_srv_vpn_cdb_server_list_static_create(dap_chain_net_t *a_net);
-
-- 
GitLab