From 9b68cf5b9375e19e2db11a21f7ed0fd9b2b71c7f Mon Sep 17 00:00:00 2001 From: Anatolii Kurotych <akurotych@gmail.com> Date: Fri, 14 Dec 2018 13:27:47 +0200 Subject: [PATCH] [*] Merge old changes in stream_ctl to dap_stream_ctl --- stream/CMakeLists.txt | 2 - stream/dap_stream.h | 2 +- stream/dap_stream_ctl.c | 52 ++++++++----- stream/dap_stream_ctl.h | 3 +- stream/dap_stream_pkt.c | 4 +- stream/dap_stream_pkt.h | 2 +- stream/stream_ctl.c | 169 ---------------------------------------- stream/stream_ctl.h | 34 -------- 8 files changed, 37 insertions(+), 231 deletions(-) delete mode 100644 stream/stream_ctl.c delete mode 100644 stream/stream_ctl.h diff --git a/stream/CMakeLists.txt b/stream/CMakeLists.txt index 543ba53..e913b7f 100644 --- a/stream/CMakeLists.txt +++ b/stream/CMakeLists.txt @@ -12,6 +12,4 @@ target_link_libraries(dap_stream dap_core dap_udp_server dap_crypto dap_http_server dap_enc_server dap_session dap_stream_ch) target_include_directories(dap_stream INTERFACE .) -set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) -set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) diff --git a/stream/dap_stream.h b/stream/dap_stream.h index 3d0c874..fc39417 100644 --- a/stream/dap_stream.h +++ b/stream/dap_stream.h @@ -42,7 +42,7 @@ typedef struct dap_http_client dap_http_client_t; typedef struct dap_http dap_http_t; typedef struct dap_stream dap_stream_t; typedef struct dap_stream_pkt dap_stream_pkt_t; -#define STREAM_BUF_SIZE_MAX 20480 +#define STREAM_BUF_SIZE_MAX 500000 #define STREAM_KEEPALIVE_TIMEOUT 3 // How often send keeplive messages (seconds) #define STREAM_KEEPALIVE_PASSES 3 // How many messagges without answers need for disconnect client and close session diff --git a/stream/dap_stream_ctl.c b/stream/dap_stream_ctl.c index d0c5150..f3c62b9 100644 --- a/stream/dap_stream_ctl.c +++ b/stream/dap_stream_ctl.c @@ -34,6 +34,7 @@ #include "dap_stream_session.h" #include "dap_stream_ctl.h" +#include "http_status_code.h" #define LOG_TAG "dap_stream_ctl" @@ -47,12 +48,21 @@ const char* connection_type_str[] = bool stream_check_proto_version(unsigned int ver); void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg); +static struct { + size_t size; + dap_enc_key_type_t type; +} s_socket_forward_key; + + /** * @brief stream_ctl_init Initialize stream control module * @return Zero if ok others if not */ -int dap_stream_ctl_init() +int dap_stream_ctl_init(dap_enc_key_type_t socket_forward_key_type, + size_t socket_forward_key_size) { + s_socket_forward_key.type = socket_forward_key_type; + s_socket_forward_key.size = socket_forward_key_size; log_it(L_NOTICE,"Initialized stream control module"); return 0; } @@ -83,11 +93,11 @@ void dap_stream_ctl_add_proc(struct dap_http * sh, const char * url) */ void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg) { - bool * isOk = (bool *) arg; + http_status_code_t * return_code = (http_status_code_t*)arg; - unsigned int db_id=0; + unsigned int db_id=0; // unsigned int proto_version; - dap_stream_session_t * ss=NULL; + dap_stream_session_t * ss=NULL; // unsigned int action_cmd=0; bool l_new_session = false; @@ -101,7 +111,7 @@ void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg) }else{ log_it(L_ERROR,"ctl command unknown: %s",dg->url_path); enc_http_delegate_delete(dg); - *isOk=false; + *return_code = Http_Status_MethodNotAllowed; return; } if(l_new_session){ @@ -109,34 +119,34 @@ void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg) ss = dap_stream_session_pure_new(); char *key_str = calloc(1, KEX_KEY_STR_SIZE); dap_random_string_fill(key_str, KEX_KEY_STR_SIZE); - ss->key = dap_enc_key_new_generate(DAP_ENC_KEY_TYPE_IAES, key_str, strlen(key_str), NULL, 0, 0); + ss->key = dap_enc_key_new_generate(s_socket_forward_key.type, key_str, KEX_KEY_STR_SIZE, + NULL, 0, s_socket_forward_key.size); enc_http_reply_f(dg,"%u %s",ss->id,key_str); - dg->isOk=true; + *return_code = Http_Status_OK; log_it(L_INFO," New stream session %u initialized",ss->id); free(key_str); }else{ log_it(L_ERROR,"Wrong request: \"%s\"",dg->in_query); - dg->isOk=false; + *return_code = Http_Status_BadRequest; } - *isOk=dg->isOk; unsigned int conn_t = 0; char *ct_str = strstr(dg->in_query, "connection_type"); if (ct_str) { - sscanf(ct_str, "connection_type=%u", &conn_t); - if (conn_t < 0 || conn_t >= STREAM_SESSION_END_TYPE) - { - log_it(L_WARNING,"Error connection type : %i",conn_t); - conn_t = STEAM_SESSION_HTTP; - } - - if (ss) - { - ss->conn_type = conn_t; - } + sscanf(ct_str, "connection_type=%u", &conn_t); + if (conn_t < 0 || conn_t >= STREAM_SESSION_END_TYPE) + { + log_it(L_WARNING,"Error connection type : %i",conn_t); + conn_t = STEAM_SESSION_HTTP; + } + + if (ss) + { + ss->conn_type = conn_t; + } } @@ -146,7 +156,7 @@ void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg) enc_http_delegate_delete(dg); }else{ log_it(L_ERROR,"No encryption layer was initialized well"); - *isOk=false; + *return_code = Http_Status_BadRequest; } } diff --git a/stream/dap_stream_ctl.h b/stream/dap_stream_ctl.h index 5b082c1..94a5d62 100644 --- a/stream/dap_stream_ctl.h +++ b/stream/dap_stream_ctl.h @@ -23,6 +23,7 @@ typedef struct dap_http dap_http_t; #define KEX_KEY_STR_SIZE 128 -int dap_stream_ctl_init(); +int dap_stream_ctl_init(dap_enc_key_type_t socket_forward_key_type, + size_t socket_forward_key_size); void dap_stream_ctl_deinit(); void dap_stream_ctl_add_proc(struct dap_http * sh, const char * url); diff --git a/stream/dap_stream_pkt.c b/stream/dap_stream_pkt.c index cfee70a..12d2186 100644 --- a/stream/dap_stream_pkt.c +++ b/stream/dap_stream_pkt.c @@ -83,7 +83,7 @@ size_t encode_dummy(const void * buf, const size_t buf_size, void * buf_out){ */ size_t dap_stream_pkt_read(struct dap_stream * sid,struct dap_stream_pkt * pkt, void * buf_out, size_t buf_out_size) { - size_t ds = dap_enc_iaes256_cbc_decrypt_fast(sid->session->key,pkt->data,pkt->hdr.size,buf_out, buf_out_size); + size_t ds = sid->session->key->dec_na(sid->session->key,pkt->data,pkt->hdr.size,buf_out, buf_out_size); // log_it(L_DEBUG,"Stream decoded %lu bytes ( last bytes 0x%02x 0x%02x 0x%02x 0x%02x ) ", ds, // *((uint8_t *)buf_out+ds-4),*((uint8_t *)buf_out+ds-3),*((uint8_t *)buf_out+ds-2),*((uint8_t *)buf_out+ds-1) // ); @@ -117,7 +117,7 @@ size_t dap_stream_pkt_write(struct dap_stream * sid, const void * data, uint32_t memset(&pkt_hdr,0,sizeof(pkt_hdr)); memcpy(pkt_hdr.sig,dap_sig,sizeof(pkt_hdr.sig)); - pkt_hdr.size = dap_enc_iaes256_cbc_encrypt_fast(sid->session->key, data,data_size,sid->buf, STREAM_BUF_SIZE_MAX); + pkt_hdr.size = sid->session->key->enc_na(sid->session->key, data,data_size,sid->buf, STREAM_BUF_SIZE_MAX); if(sid->conn_udp){ ret+=dap_udp_client_write(sid->conn,&pkt_hdr,sizeof(pkt_hdr)); diff --git a/stream/dap_stream_pkt.h b/stream/dap_stream_pkt.h index 5245732..d935bba 100644 --- a/stream/dap_stream_pkt.h +++ b/stream/dap_stream_pkt.h @@ -22,7 +22,7 @@ #include <stdint.h> #include <stddef.h> -#define STREAM_PKT_SIZE_MAX 100000 +#define STREAM_PKT_SIZE_MAX 500000 struct dap_stream; #define DATA_PACKET 0x00 diff --git a/stream/stream_ctl.c b/stream/stream_ctl.c deleted file mode 100644 index 77457f4..0000000 --- a/stream/stream_ctl.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc - All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <stdbool.h> -#include <string.h> -#include "dap_common.h" - -#include "stream.h" - -#include "dap_enc_http.h" -#include "dap_enc_key.h" - -#include "dap_http.h" -#include "dap_http_client.h" -#include "dap_client_remote.h" -#include "dap_http_simple.h" - -#include "http_status_code.h" - -#include "stream_session.h" -#include "stream_ctl.h" - -#define LOG_TAG "stream_ctl" - -const char* connection_type_str[] = -{ - [STEAM_SESSION_HTTP] = "http", - [STREAM_SESSION_UDP] = "udp" -}; - -static struct { - size_t size; - dap_enc_key_type_t type; -} _socket_forward_key; - - -#define DAPMP_VERSION 13 -bool stream_check_proto_version(unsigned int ver); -void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg); - -/** - * @brief stream_ctl_init - * @param socket_forward_key_type - * @param socket_forward_key_size - Can be null for some alghoritms - * @return - */ -int stream_ctl_init(dap_enc_key_type_t socket_forward_key_type, - size_t socket_forward_key_size) -{ - _socket_forward_key.type = socket_forward_key_type; - _socket_forward_key.size = socket_forward_key_size; - log_it(L_NOTICE,"Initialized stream control module"); - return 0; -} - -/** - * @brief stream_ctl_deinit Deinit stream control module - */ -void stream_ctl_deinit() -{ - -} - -/** - * @brief stream_ctl_add_proc Add stream control url processor - * @param sh HTTP server instance - * @param url URL string - */ -void stream_ctl_add_proc(struct dap_http * sh, const char * url) -{ - dap_http_simple_proc_add(sh,url,4096,stream_ctl_proc); -} - - -/** - * @brief stream_ctl_headers_read Process CTL request - * @param cl_st HTTP server instance - * @param arg Not used - */ -void stream_ctl_proc(struct dap_http_simple *cl_st, void * arg) -{ - http_status_code_t * return_code = (http_status_code_t*)arg; - - unsigned int db_id=0; - // unsigned int proto_version; - dap_stream_session_t * ss=NULL; - // unsigned int action_cmd=0; - bool l_new_session = false; - - enc_http_delegate_t *dg = enc_http_request_decode(cl_st); - - if(dg){ - if (strcmp(dg->url_path,"socket_forward")==0){ - l_new_session = true; - }else if (strcmp(dg->url_path,"stream_ctl")==0) { - l_new_session = true; - }else{ - log_it(L_ERROR,"ctl command unknown: %s",dg->url_path); - enc_http_delegate_delete(dg); - *return_code = Http_Status_MethodNotAllowed; - return; - } - if(l_new_session){ - - ss = dap_stream_session_pure_new(); - char *key_str = calloc(1, KEX_KEY_STR_SIZE); - dap_random_string_fill(key_str, KEX_KEY_STR_SIZE); - ss->key = dap_enc_key_new_generate(_socket_forward_key.type, key_str, strlen(key_str), NULL, 0, _socket_forward_key.size); - enc_http_reply_f(dg,"%u %s",ss->id,key_str); - *return_code = Http_Status_OK; - - log_it(L_INFO," New stream session %u initialized",ss->id); - - free(key_str); - }else{ - log_it(L_ERROR,"Wrong request: \"%s\"",dg->in_query); - *return_code = Http_Status_BadRequest; - } - - unsigned int conn_t = 0; - char *ct_str = strstr(dg->in_query, "connection_type"); - if (ct_str) - { - sscanf(ct_str, "connection_type=%u", &conn_t); - if (conn_t < 0 || conn_t >= STREAM_SESSION_END_TYPE) - { - log_it(L_WARNING,"Error connection type : %i",conn_t); - conn_t = STEAM_SESSION_HTTP; - } - - if (ss) - { - ss->conn_type = conn_t; - } - - } - - log_it(L_INFO,"setup connection_type: %s", connection_type_str[conn_t]); - - enc_http_reply_encode(cl_st,dg); - enc_http_delegate_delete(dg); - }else{ - log_it(L_ERROR,"No encryption layer was initialized well"); - *return_code = Http_Status_BadRequest; - } -} - - -bool stream_check_proto_version(unsigned int ver) -{ - return ver<=DAPMP_VERSION; -} diff --git a/stream/stream_ctl.h b/stream/stream_ctl.h deleted file mode 100644 index 2b44efd..0000000 --- a/stream/stream_ctl.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc - All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef _STREAM_CTL_H -#define _STREAM_CTL_H - -struct dap_http; - -#define KEX_KEY_STR_SIZE 128 - -int stream_ctl_init(dap_enc_key_type_t socket_forward_key_type, - size_t socket_forward_key_size); - -extern void stream_ctl_deinit(); -extern void stream_ctl_add_proc(struct dap_http * sh, const char * url); - -#endif -- GitLab