From 4b5f52b658a5f422e73fe36aa8f57ce61672e8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Al=D0=B5x=D0=B0nder=20Lysik=D0=BEv?= <alexander.lysikov@demlabs.net> Date: Fri, 13 Dec 2019 21:40:44 +0500 Subject: [PATCH] fixed loading of price_list and repair vpnclient by 50% --- dap_chain_net_srv_vpn.c | 248 ++++++++++++++++++++++++------------- dap_chain_net_vpn_client.c | 116 ++++++++++++++++- dap_chain_net_vpn_client.h | 7 +- 3 files changed, 278 insertions(+), 93 deletions(-) diff --git a/dap_chain_net_srv_vpn.c b/dap_chain_net_srv_vpn.c index 43872b6..5ca360a 100755 --- a/dap_chain_net_srv_vpn.c +++ b/dap_chain_net_srv_vpn.c @@ -190,96 +190,173 @@ int dap_chain_net_srv_vpn_init(dap_config_t * g_config) size_t l_price_length = strlen(l_price_str); size_t l_iter = 0; char * l_pos_old = l_price_str; + + // l_price_str example: kelvin-testnet:0.00001:KELT:3600:SEC:mywallet0 + char **l_parse_str = dap_strsplit(l_price_str, ":", -1); + size_t l_parse_str_n = dap_str_countv(l_parse_str); + if(6 != l_parse_str_n) { + log_it(L_ERROR, "Wrong price element size=%d (must be 6)", l_parse_str_n); + dap_strfreev(l_parse_str); + continue; + } + // price: create,fill and added to list dap_chain_net_srv_price_t *l_price = DAP_NEW_Z(dap_chain_net_srv_price_t); - for (char * l_pos = index(l_price_str,':'); ; l_pos = index(l_pos+1,':') ){ - if( l_iter == 0) - break; - size_t l_parse_token_size = l_pos?(size_t) (l_pos - l_pos_old)-1: l_price_length- (size_t)(l_pos_old - l_pricelist[i]) ; - char * l_parse_token = strndup(l_pos_old, l_parse_token_size); - if( l_parse_token_size ==0 ){ - log_it(L_ERROR, "Wrong price element size nil"); - DAP_DELETE(l_parse_token); - break; - } - if ( l_iter == 0){ - l_price->net_name = strdup(l_parse_token); - - l_price->net = dap_chain_net_by_name( l_price->net_name); - if( ! l_price->net ){ - log_it(L_ERROR, "Error parse pricelist: can't find network \"%s\"", l_price->net_name); - DAP_DELETE( l_price->net); - DAP_DELETE(l_price); - DAP_DELETE(l_parse_token); - break; - } - }else if (l_iter == 1){ - l_price->value_coins = atof( l_parse_token); - l_price->value_datoshi =(uint64_t) dap_chain_coins_to_balance((long double)l_price->value_coins); - if ( ! l_price->value_datoshi ){ - log_it(L_ERROR, "Error parse pricelist: text on 2nd position \"%s\" is not floating number", l_parse_token); - DAP_DELETE( l_price->net); - DAP_DELETE(l_price); - DAP_DELETE(l_parse_token); - break; - } - }else if (l_iter == 2){ - strncpy( l_price->token, l_parse_token, sizeof (l_price->token)-1); - - }else if (l_iter == 3){ - l_price->units = strtoul( l_parse_token,NULL,10); - if ( !l_price->units ){ - log_it(L_ERROR, "Error parse pricelist: text on 4th position \"%s\" is not unsigned integer", l_parse_token); - DAP_DELETE( l_price->net); - DAP_DELETE(l_price); - DAP_DELETE(l_parse_token); - break; - } - }else if (l_iter == 4){ - if ( strcmp(l_parse_token,"SEC") == 0 ){ - l_price->units_uid.enm = SERV_UNIT_SEC; - }else if ( strcmp(l_parse_token,"DAY") == 0 ){ - l_price->units_uid.enm = SERV_UNIT_DAY; - }else if ( strcmp(l_parse_token,"MB") == 0 ){ - l_price->units_uid.enm = SERV_UNIT_MB; - }else { - log_it(L_ERROR, "Error parse pricelist: wrong unit type \"%s\"", l_parse_token); - DAP_DELETE( l_price->net); - DAP_DELETE(l_price); - DAP_DELETE(l_parse_token); - break; - } - }else if (l_iter == 5){ - l_price->wallet = dap_chain_wallet_open( l_parse_token, dap_config_get_item_str_default(g_config,"resources","wallets_path",NULL) ); - if (! l_price->wallet ){ - log_it(L_ERROR, "Error parse pricelist: can't open wallet \"%s\"", l_parse_token); - DAP_DELETE( l_price->net); - DAP_DELETE(l_price); - DAP_DELETE(l_parse_token); - break; - } - }else{ - DAP_DELETE(l_parse_token); - break; - } - l_iter++; - l_pos_old = l_pos; - if (! l_pos ){ - DAP_DELETE(l_parse_token); - break; - } + // value + l_price->value_coins = atof(l_parse_str[1]); + l_price->value_datoshi = (uint64_t) dap_chain_coins_to_balance((long double) l_price->value_coins); + if(!l_price->value_datoshi) { + log_it(L_ERROR, "Error parse pricelist: text on 2nd position \"%s\" is not floating number", + l_parse_str[1]); + DAP_DELETE(l_price); + dap_strfreev(l_parse_str); + continue; + } + // token + strncpy( l_price->token, l_parse_str[2], sizeof (l_price->token)-1); + // units + l_price->units = strtoul(l_parse_str[3], NULL, 10); + if(!l_price->units) { + log_it(L_ERROR, "Error parse pricelist: text on 4th position \"%s\" is not unsigned integer", + l_parse_str[3]); + DAP_DELETE(l_price); + dap_strfreev(l_parse_str); + continue; + } + // units_uid + if(strcmp(l_parse_str[4], "SEC") == 0) { + l_price->units_uid.enm = SERV_UNIT_SEC; + } else if(strcmp(l_parse_str[4], "DAY") == 0) { + l_price->units_uid.enm = SERV_UNIT_DAY; + } else if(strcmp(l_parse_str[4], "MB") == 0) { + l_price->units_uid.enm = SERV_UNIT_MB; + } else { + log_it(L_ERROR, "Error parse pricelist: wrong unit type \"%s\"", l_parse_str[4]); + DAP_DELETE(l_price); + dap_strfreev(l_parse_str); + continue; } - if( l_iter == 6){ - log_it(L_NOTICE, "All price parsed well, added to service %s", l_price_str); - if (l_srv->pricelist) - l_srv->pricelist->prev = l_price; - l_price->next = l_srv->pricelist; - l_srv->pricelist = l_price; + // wallet + l_price->wallet = dap_chain_wallet_open(l_parse_str[5], + dap_config_get_item_str_default(g_config, "resources", "wallets_path", NULL)); + if(!l_price->wallet) { + log_it(L_ERROR, "Error parse pricelist: can't open wallet \"%s\"", l_parse_str[5]); + DAP_DELETE(l_price); + dap_strfreev(l_parse_str); + continue; } + // net name + l_price->net_name = dap_strdup(l_parse_str[0]); + l_price->net = dap_chain_net_by_name(l_price->net_name); + if(!l_price->net) { + log_it(L_ERROR, "Error parse pricelist: can't find network \"%s\"", l_price->net_name); + DAP_DELETE(l_price); + DAP_DELETE(l_price->net); + dap_strfreev(l_parse_str); + continue; + } + dap_strfreev(l_parse_str); - } + // adding new price to head of the list + log_it(L_NOTICE, "All price parsed well, added to service %s", l_price_str); + if(l_srv->pricelist) { + l_srv->pricelist->prev = l_price; + l_price->next = l_srv->pricelist; + } + l_srv->pricelist = l_price; + + /*for (char * l_pos = index(l_price_str,':'); ; l_pos = index(l_pos+1,':') ){ + if( l_iter == 0) + break; - return 0; + size_t l_parse_token_size = l_pos?(size_t) (l_pos - l_pos_old)-1: l_price_length- (size_t)(l_pos_old - l_pricelist[i]) ; + char * l_parse_token = strndup(l_pos_old, l_parse_token_size); + if( l_parse_token_size ==0 ){ + log_it(L_ERROR, "Wrong price element size nil"); + DAP_DELETE(l_parse_token); + break; + } + if ( l_iter == 0){ + l_price->net_name = strdup(l_parse_token); + + l_price->net = dap_chain_net_by_name( l_price->net_name); + if( ! l_price->net ){ + log_it(L_ERROR, "Error parse pricelist: can't find network \"%s\"", l_price->net_name); + DAP_DELETE( l_price->net); + DAP_DELETE(l_price); + DAP_DELETE(l_parse_token); + break; + } + }else if (l_iter == 1){ + l_price->value_coins = atof( l_parse_token); + l_price->value_datoshi =(uint64_t) dap_chain_coins_to_balance((long double)l_price->value_coins); + if ( ! l_price->value_datoshi ){ + log_it(L_ERROR, "Error parse pricelist: text on 2nd position \"%s\" is not floating number", l_parse_token); + DAP_DELETE( l_price->net); + DAP_DELETE(l_price); + DAP_DELETE(l_parse_token); + break; + } + }else if (l_iter == 2){ + strncpy( l_price->token, l_parse_token, sizeof (l_price->token)-1); + + }else if (l_iter == 3){ + l_price->units = strtoul( l_parse_token,NULL,10); + if ( !l_price->units ){ + log_it(L_ERROR, "Error parse pricelist: text on 4th position \"%s\" is not unsigned integer", l_parse_token); + DAP_DELETE( l_price->net); + DAP_DELETE(l_price); + DAP_DELETE(l_parse_token); + break; + } + }else if (l_iter == 4){ + if ( strcmp(l_parse_token,"SEC") == 0 ){ + l_price->units_uid.enm = SERV_UNIT_SEC; + }else if ( strcmp(l_parse_token,"DAY") == 0 ){ + l_price->units_uid.enm = SERV_UNIT_DAY; + }else if ( strcmp(l_parse_token,"MB") == 0 ){ + l_price->units_uid.enm = SERV_UNIT_MB; + }else { + log_it(L_ERROR, "Error parse pricelist: wrong unit type \"%s\"", l_parse_token); + DAP_DELETE( l_price->net); + DAP_DELETE(l_price); + DAP_DELETE(l_parse_token); + break; + } + }else if (l_iter == 5){ + l_price->wallet = dap_chain_wallet_open( l_parse_token, dap_config_get_item_str_default(g_config,"resources","wallets_path",NULL) ); + if (! l_price->wallet ){ + log_it(L_ERROR, "Error parse pricelist: can't open wallet \"%s\"", l_parse_token); + DAP_DELETE( l_price->net); + DAP_DELETE(l_price); + DAP_DELETE(l_parse_token); + break; + } + }else{ + DAP_DELETE(l_parse_token); + break; + } + l_iter++; + l_pos_old = l_pos; + if (! l_pos ){ + DAP_DELETE(l_parse_token); + break; + } + } + if( l_iter == 6){ + log_it(L_NOTICE, "All price parsed well, added to service %s", l_price_str); + if (l_srv->pricelist) + l_srv->pricelist->prev = l_price; + l_price->next = l_srv->pricelist; + l_srv->pricelist = l_price; + } + + } + + return 0;*/ + } + if(l_srv->pricelist) + return 0; } return -1; } @@ -325,7 +402,7 @@ static int s_callback_response_success(dap_chain_net_srv_t * a_srv, uint32_t a_u // dap_stream_ch_chain_net_srv_pkt_request_t * l_request = (dap_stream_ch_chain_net_srv_pkt_request_t *) a_request; // dap_chain_net_srv_stream_session_t * l_srv_session = (dap_chain_net_srv_stream_session_t *) a_srv_client->ch->stream->session->_inheritor; dap_chain_net_srv_stream_session_t * l_srv_session = (dap_chain_net_srv_stream_session_t *) a_srv_client->ch->stream->session->_inheritor; - dap_chain_net_srv_usage_t * l_usage_active= dap_chain_net_srv_usage_find(l_srv_session->usages,a_usage_id); + dap_chain_net_srv_usage_t * l_usage_active= dap_chain_net_srv_usage_find(l_srv_session,a_usage_id); dap_chain_net_srv_ch_vpn_t * l_srv_ch_vpn =(dap_chain_net_srv_ch_vpn_t*) a_srv_client->ch->stream->channel[DAP_CHAIN_NET_SRV_VPN_ID] ? a_srv_client->ch->stream->channel[DAP_CHAIN_NET_SRV_VPN_ID]->internal : NULL; @@ -484,6 +561,7 @@ void s_new(dap_stream_ch_t* a_stream_ch, void* a_arg) dap_chain_net_srv_stream_session_create(a_stream_ch->stream->session); dap_chain_net_srv_uid_t l_uid = { .uint64 = DAP_CHAIN_NET_SRV_VPN_ID }; l_srv_vpn->net_srv = dap_chain_net_srv_get(l_uid); + l_srv_vpn->ch = a_stream_ch; dap_chain_net_srv_stream_session_t * l_srv_session = (dap_chain_net_srv_stream_session_t *) a_stream_ch->stream->session->_inheritor; pthread_mutex_init(&l_srv_vpn->mutex, NULL); diff --git a/dap_chain_net_vpn_client.c b/dap_chain_net_vpn_client.c index 6ffa1cf..405a28d 100644 --- a/dap_chain_net_vpn_client.c +++ b/dap_chain_net_vpn_client.c @@ -39,6 +39,7 @@ #include "dap_strfuncs.h" #include "dap_client.h" +#include "dap_chain_common.h" #include "dap_chain_node_client.h" #include "dap_stream_ch_proc.h" @@ -49,6 +50,8 @@ #include "dap_stream_ch_pkt.h" #include "dap_chain_net_vpn_client_tun.h" + + typedef enum dap_http_client_state { DAP_HTTP_CLIENT_STATE_NONE = 0, DAP_HTTP_CLIENT_STATE_START = 1, @@ -72,7 +75,7 @@ dap_stream_ch_t* dap_chain_net_vpn_client_get_stream_ch(void) { if(!s_vpn_client) return NULL; - dap_stream_ch_t *l_stream = dap_client_get_stream_ch(s_vpn_client->client, DAP_STREAM_CH_ID_NET_SRV_VPN ); + dap_stream_ch_t *l_stream = dap_client_get_stream_ch(s_vpn_client->client, DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT ); return l_stream; } @@ -111,6 +114,48 @@ void s_stage_callback() }*/ +/** + * Connect to VPN client + * + * return: 0 Ok, 1 Already started, <0 Error + */ +static void dap_chain_net_vpn_response_success_callback(dap_stream_ch_chain_net_srv_pkt_success_t *a_success, void *a_arg) +{ + dap_stream_ch_chain_net_srv_pkt_success_t *l_success = (dap_stream_ch_chain_net_srv_pkt_success_t*)a_arg; + if(!l_success) + return; + // send receipt to server + { + //dap_chain_datum_tx_receipt_t * l_receipt; + //dap_chain_net_srv_uid_t l_srv_uid = a_success->hdr.srv_uid; + //dap_chain_net_srv_price_unit_uid_t l_units_type = SERV_UNIT_MB; + //uint64_t l_units = 100; + //uint64_t l_value_datoshi = 1000000; + //l_receipt = dap_chain_datum_tx_receipt_create(l_srv_uid, l_units_type, l_units, l_value_datoshi); + //DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_SIGN_RESPONSE + } + + // send first packet to server + //if(0) + { + dap_stream_ch_t *l_ch = dap_chain_net_vpn_client_get_stream_ch(); + if(l_ch) { // Is present in hash table such destination address + size_t l_ipv4_str_len = 0; //dap_strlen(a_ipv4_str); + ch_vpn_pkt_t *pkt_out = (ch_vpn_pkt_t*) calloc(1, sizeof(pkt_out->header) + l_ipv4_str_len); + + pkt_out->header.op_code = VPN_PACKET_OP_CODE_VPN_ADDR_REQUEST; + //pkt_out->header.sock_id = l_stream->stream->events_socket->socket; + //pkt_out->header.op_connect.addr_size = l_ipv4_str_len; //remoteAddrBA.length(); + //pkt_out->header.op_connect.port = a_port; + //memcpy(pkt_out->data, a_ipv4_str, l_ipv4_str_len); + dap_stream_ch_pkt_write(l_ch, DAP_STREAM_CH_PKT_TYPE_NET_SRV_VPN_DATA, pkt_out, + pkt_out->header.op_data.data_size + sizeof(pkt_out->header)); + dap_stream_ch_set_ready_to_write(l_ch, false); + DAP_DELETE(pkt_out); + } + } +} + /** * Start VPN client * @@ -136,7 +181,9 @@ int dap_chain_net_vpn_client_start(dap_chain_net_t *a_net, const char *a_ipv4_st s_node_info->hdr.ext_port = a_port; dap_client_stage_t l_stage_target = STAGE_STREAM_STREAMING; //DAP_CLIENT_STAGE_STREAM_CTL;//STAGE_STREAM_STREAMING; - const char l_active_channels[] = { DAP_STREAM_CH_ID_NET_SRV_VPN , 0 }; + const char l_active_channels[] = { 'R', DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT, 0 }; + //const char l_active_channels[] = { DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT , 'R', 0 }; + //const char l_active_channels[] = { 'R', 0 }; if(a_ipv4_str) inet_pton(AF_INET, a_ipv4_str, &(s_node_info->hdr.ext_addr_v4)); if(a_ipv6_str) @@ -167,8 +214,35 @@ int dap_chain_net_vpn_client_start(dap_chain_net_t *a_net, const char *a_ipv4_st l_ret = dap_chain_net_vpn_client_tun_init(a_ipv4_str); + + { + //dap_stream_ch_t *l_ch = dap_chain_net_vpn_client_get_stream_ch(); + dap_stream_ch_t *l_ch = dap_client_get_stream_ch(s_vpn_client->client, 'R' ); + if(l_ch) { + dap_chain_net_srv_stream_session_t * l_srv_session = l_ch && l_ch->stream && l_ch->stream->session ? + l_ch->stream->session->_inheritor : NULL; + if(l_srv_session) { + l_srv_session->response_success_callback = dap_chain_net_vpn_response_success_callback; + l_srv_session->response_success_callback_data = l_ch; + } + //l_ch->internal = ; + dap_stream_ch_chain_net_srv_pkt_request_t l_request = { 0 }; + l_request.hdr.net_id.uint64 = a_net->pub.id.uint64; + l_request.hdr.srv_uid.uint64 = DAP_CHAIN_NET_SRV_VPN_ID; + //l_request.hdr.tx_cond = 0; + + size_t pkt_out_len = sizeof(dap_stream_ch_chain_net_srv_pkt_request_t); + void *pkt_out = (void*)&l_request; + + dap_stream_ch_pkt_write(l_ch, DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_REQUEST, pkt_out, pkt_out_len); + dap_stream_ch_set_ready_to_write(l_ch, false); + //DAP_DELETE(pkt_out); + } + + } + // send first packet to server -// if(0) + if(0) { dap_stream_ch_t *l_ch = dap_chain_net_vpn_client_get_stream_ch(); if(l_ch) { // Is present in hash table such destination address @@ -182,7 +256,7 @@ int dap_chain_net_vpn_client_start(dap_chain_net_t *a_net, const char *a_ipv4_st //memcpy(pkt_out->data, a_ipv4_str, l_ipv4_str_len); dap_stream_ch_pkt_write(l_ch, DAP_STREAM_CH_PKT_TYPE_NET_SRV_VPN_DATA, pkt_out, pkt_out->header.op_data.data_size + sizeof(pkt_out->header)); - dap_stream_ch_set_ready_to_write(l_ch, true); + dap_stream_ch_set_ready_to_write(l_ch, false); DAP_DELETE(pkt_out); } } @@ -282,8 +356,9 @@ static void send_pong_pkt(dap_stream_ch_t* a_ch) * @param a_ch * @param a_arg */ -void dap_chain_net_vpn_client_pkt_in(dap_stream_ch_t* a_ch, dap_stream_ch_pkt_t* a_pkt) +void dap_chain_net_vpn_client_pkt_in(dap_stream_ch_t* a_ch, void* a_arg) { + dap_stream_ch_pkt_t * a_pkt = (dap_stream_ch_pkt_t *) a_arg; ch_vpn_pkt_t * l_sf_pkt = (ch_vpn_pkt_t *) a_pkt->data; size_t l_sf_pkt_data_size = a_pkt->hdr.size - sizeof(l_sf_pkt->header); @@ -525,7 +600,7 @@ void dap_chain_net_vpn_client_pkt_in(dap_stream_ch_t* a_ch, dap_stream_ch_pkt_t* * @brief dap_chain_net_vpn_client_pkt_out * @param a_ch */ -void dap_chain_net_vpn_client_pkt_out(dap_stream_ch_t* a_ch) +void dap_chain_net_vpn_client_pkt_out(dap_stream_ch_t* a_ch, void* a_arg) { ch_vpn_socket_proxy_t * l_cur = NULL, *l_tmp; bool l_is_smth_out = false; @@ -584,10 +659,39 @@ void dap_chain_net_vpn_client_pkt_out(dap_stream_ch_t* a_ch) }*/ } +/** + * @brief dap_chain_net_vpn_client_pkt_new Callback to constructor of object of Ch + * @param ch + * @param arg + */ +static void dap_chain_net_vpn_client_pkt_new(dap_stream_ch_t* a_ch, void* a_arg) +{ + (void) a_arg; + //dap_stream_ch_chain_t + printf("aaa dap_chain_net_vpn_client_pkt_new ch=0x%x", a_ch); + a_ch->internal = DAP_NEW_Z(dap_chain_net_srv_ch_vpn_t); + + dap_chain_net_srv_ch_vpn_t * l_srv_vpn = CH_VPN(a_ch); +} + +/** + * @brief dap_chain_net_vpn_client_pkt_delete + * @param ch + * @param arg + */ +void dap_chain_net_vpn_client_pkt_delete(dap_stream_ch_t* a_ch, void* a_arg) +{ + (void) a_arg; + printf("aaa dap_chain_net_vpn_client_pkt_delete ch=0x%x", a_ch); +} + int dap_chain_net_vpn_client_init(dap_config_t * g_config) { pthread_mutex_init(&sf_socks_mutex, NULL); + //dap_chain_net_srv_uid_t l_uid = { .uint64 = DAP_CHAIN_NET_SRV_VPN_ID }; + dap_stream_ch_proc_add(DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT, dap_chain_net_vpn_client_pkt_new, dap_chain_net_vpn_client_pkt_delete, + dap_chain_net_vpn_client_pkt_in, dap_chain_net_vpn_client_pkt_out); return 0; } diff --git a/dap_chain_net_vpn_client.h b/dap_chain_net_vpn_client.h index a75a307..a1c3637 100644 --- a/dap_chain_net_vpn_client.h +++ b/dap_chain_net_vpn_client.h @@ -29,6 +29,9 @@ #include "dap_chain_net.h" #include "dap_chain_net_srv_vpn.h" +//#define DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT DAP_STREAM_CH_ID_NET_SRV_VPN //'R' //DAP_CHAIN_NET_SRV_VPN_ID //'V' +#define DAP_STREAM_CH_ID_NET_SRV_VPN_CLIENT DAP_STREAM_CH_ID_NET_SRV_VPN + dap_stream_ch_t* dap_chain_net_vpn_client_get_stream_ch(void); int dap_chain_net_vpn_client_start(dap_chain_net_t *a_net, const char *a_ipv4_str, const char *a_ipv6_str, int a_port); @@ -39,5 +42,5 @@ int dap_chain_net_vpn_client_init(dap_config_t * g_config); void dap_chain_net_vpn_client_deinit(); -void dap_chain_net_vpn_client_pkt_out(dap_stream_ch_t* a_ch); -void dap_chain_net_vpn_client_pkt_in(dap_stream_ch_t* a_ch, dap_stream_ch_pkt_t* a_pkt); +//void dap_chain_net_vpn_client_pkt_out(dap_stream_ch_t* a_ch); +//void dap_chain_net_vpn_client_pkt_in(dap_stream_ch_t* a_ch, dap_stream_ch_pkt_t* a_pkt); -- GitLab