diff --git a/core_server/CMakeLists.txt b/core_server/CMakeLists.txt index 585d54f3e00c69387c58fa31291f1466d3e3804c..d3ad6940701727d3d99255882c91f42ff30b05a6 100644 --- a/core_server/CMakeLists.txt +++ b/core_server/CMakeLists.txt @@ -5,9 +5,15 @@ set(DAP_CORE_SERVER_SRCS dap_server.c ) include_directories("${INCLUDE_DIRECTORIES} ${dap_core_INCLUDE_DIRS}") include_directories("${INCLUDE_DIRECTORIES} ${dap_crypto_INCLUDE_DIRS}") +include_directories("${INCLUDE_DIRECTORIES} ${dap_enc_server_INCLUDE_DIRS}") +include_directories("${INCLUDE_DIRECTORIES} ${dap_client_INCLUDE_DIRS}") + +add_definitions ("-DDAP_SERVER") add_definitions ("${dap_core_DEFINITIONS}") add_definitions ("${dap_crypto_DEFINITIONS}") +add_definitions ("${dap_enc_server_DEFINITIONS}") +add_definitions ("${dap_client_DEFINITIONS}") add_library(${PROJECT_NAME} STATIC ${DAP_CORE_SERVER_SRCS}) set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) diff --git a/core_server/dap_server.c b/core_server/dap_server.c index 1dd86fd0aaae8baa85f947331d385cf35d5302eb..7a0e14e7c5a9a67658924257e2faf7811bfdad4f 100644 --- a/core_server/dap_server.c +++ b/core_server/dap_server.c @@ -47,11 +47,11 @@ #include <errno.h> #include <signal.h> -#include "common.h" +#include "dap_common.h" #include "dap_server.h" #include <ev.h> -#define LOG_TAG "server" +#define LOG_TAG "dap_server" static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int revents); @@ -73,7 +73,7 @@ int dap_server_init() signal(SIGPIPE, SIG_IGN); async_watcher.data = malloc(sizeof(ev_async_data_t)); - log_it(NOTICE,"Initialized socket server module"); + log_it(L_NOTICE,"Initialized socket server module"); return 0; } @@ -83,7 +83,6 @@ int dap_server_init() */ void dap_server_deinit() { - dap_client_deinit(); } @@ -102,7 +101,7 @@ dap_server_t * dap_server_new() */ void dap_server_delete(dap_server_t * sh) { - dap_client_t * dap_cur, * tmp; + dap_client_remote_t * dap_cur, * tmp; if(sh->address) free(sh->address); @@ -111,7 +110,7 @@ void dap_server_delete(dap_server_t * sh) if(sh->server_delete_callback) sh->server_delete_callback(sh,NULL); - free(sh->internal); + free(sh->_inheritor); free(sh); } @@ -143,7 +142,7 @@ static void async_cb (EV_P_ ev_async *w, int revents) static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int revents) { dap_server_t* sh = watcher->data; - dap_client_t* dap_cur = dap_client_find(watcher->fd, sh); + dap_client_remote_t* dap_cur = dap_client_find(watcher->fd, sh); if ( revents & EV_READ ) { @@ -161,7 +160,7 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve } else if(bytes_read < 0) { - log_it(ERROR,"Bytes read Error %s",strerror(errno)); + log_it(L_ERROR,"Bytes read Error %s",strerror(errno)); dap_cur->signal_close = true; } @@ -183,13 +182,13 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve else { for(size_t total_sent = 0; total_sent < dap_cur->buf_out_size;) { - //log_it(DEBUG, "Output: %u from %u bytes are sent ", total_sent, dap_cur->buf_out_size); + //log_it(L_DEBUG, "Output: %u from %u bytes are sent ", total_sent, dap_cur->buf_out_size); int bytes_sent = send(dap_cur->socket, dap_cur->buf_out + total_sent, dap_cur->buf_out_size - total_sent, MSG_DONTWAIT | MSG_NOSIGNAL ); if(bytes_sent < 0) { - log_it(ERROR,"Some error occured in send() function"); + log_it(L_ERROR,"Some error occured in send() function"); break; } total_sent += bytes_sent; @@ -200,7 +199,7 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve if(dap_cur->signal_close) { - log_it(INFO, "Close Socket %d", watcher->fd); + log_it(L_INFO, "Close Socket %d", watcher->fd); dap_client_remove(dap_cur, sh); ev_io_stop(listener_clients_loop, watcher); free(watcher); @@ -211,9 +210,9 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve static void accept_cb (struct ev_loop* loop, struct ev_io* watcher, int revents) { int client_fd = accept(watcher->fd, 0, 0); - log_it(INFO, "Client accept socket %", client_fd); + log_it(L_INFO, "Client accept socket %", client_fd); if( client_fd < 0 ) - log_it(ERROR, "error accept"); + log_it(L_ERROR, "error accept"); set_nonblock_socket(client_fd); ev_async_data_t *ev_data = async_watcher.data; @@ -241,25 +240,25 @@ dap_server_t* dap_server_listen(const char * addr, uint16_t port, dap_server_typ sh->socket_listener = socket (AF_INET, SOCK_STREAM, 0); if (-1 == set_nonblock_socket(sh->socket_listener)) { - log_it(WARNING,"error server socket nonblock"); + log_it(L_WARNING,"error server socket nonblock"); exit(EXIT_FAILURE); } if (sh->socket_listener < 0){ - log_it (ERROR,"Socket error %s",strerror(errno)); + log_it (L_ERROR,"Socket error %s",strerror(errno)); dap_server_delete(sh); return NULL; } - log_it(NOTICE,"Socket created..."); + log_it(L_NOTICE,"Socket created..."); int reuse = 1; if (setsockopt(sh->socket_listener, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) - log_it(WARNING, "Can't set up REUSEADDR flag to the socket"); + log_it(L_WARNING, "Can't set up REUSEADDR flag to the socket"); #ifdef SO_REUSEPORT if (setsockopt(sh->socket_listener, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) - log_it(WARNING, "Can't set up REUSEPORT flag to the socket"); + log_it(L_WARNING, "Can't set up REUSEPORT flag to the socket"); #endif sh->listener_addr.sin_family = AF_INET; @@ -267,11 +266,11 @@ dap_server_t* dap_server_listen(const char * addr, uint16_t port, dap_server_typ inet_pton(AF_INET,addr, &(sh->listener_addr.sin_addr)); if(bind (sh->socket_listener, (struct sockaddr *) &(sh->listener_addr), sizeof(sh->listener_addr)) < 0) { - log_it(ERROR,"Bind error: %s",strerror(errno)); + log_it(L_ERROR,"Bind error: %s",strerror(errno)); dap_server_delete(sh); return NULL; }else { - log_it(INFO,"Binded %s:%u",addr,port); + log_it(L_INFO,"Binded %s:%u",addr,port); listen(sh->socket_listener, 100000); pthread_mutex_init(&sh->mutex_on_hash, NULL); @@ -282,7 +281,7 @@ dap_server_t* dap_server_listen(const char * addr, uint16_t port, dap_server_typ void* thread_loop(void * arg) { - log_it(NOTICE, "Start loop listener socket thread"); + log_it(L_NOTICE, "Start loop listener socket thread"); ev_loop(listener_clients_loop, 0); return NULL; } diff --git a/core_server/dap_server.h b/core_server/dap_server.h index c9b177aecca89bbd25a7de9cdcdacef12dd9eb89..2909b798c69478e844f20ea9309cae2603efd935 100644 --- a/core_server/dap_server.h +++ b/core_server/dap_server.h @@ -44,25 +44,25 @@ typedef struct dap_server{ uint16_t port; // Listen port char * address; // Listen address - dap_client_t * clients; // Hashmap of clients + dap_client_remote_t * clients; // Hashmap of clients int socket_listener; // Socket for listener int epoll_fd; // Epoll fd struct sockaddr_in listener_addr; // Kernel structure for listener's binded address - void * internal; // Pointer to the internal data, HTTP for example + void * _inheritor; // Pointer to the internal data, HTTP for example dap_thread_t proc_thread; pthread_mutex_t mutex_on_hash; dap_server_callback_t server_delete_callback; - dap_client_callback_t client_new_callback; // Create new client callback - dap_client_callback_t client_delete_callback; // Delete client callback - dap_client_callback_t client_read_callback; // Read function - dap_client_callback_t client_write_callback; // Write function - dap_client_callback_t client_error_callback; // Error processing function + dap_client_remote_callback_t client_new_callback; // Create new client callback + dap_client_remote_callback_t client_delete_callback; // Delete client callback + dap_client_remote_callback_t client_read_callback; // Read function + dap_client_remote_callback_t client_write_callback; // Write function + dap_client_remote_callback_t client_error_callback; // Error processing function } dap_server_t; diff --git a/enc_server/CMakeLists.txt b/enc_server/CMakeLists.txt index 6c7035ba76ce02f05946be79b1d539fb72e81305..734b672bb1acee6e3504c9efd6d0555ec8e3378b 100644 --- a/enc_server/CMakeLists.txt +++ b/enc_server/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.8) project (dap_enc_server) -set(DAP_ENC_SERVER_SRCS enc_http.c enc_ks.c ) +set(DAP_ENC_SERVER_SRCS dap_enc_http.c dap_enc_ks.c ) include_directories("${INCLUDE_DIRECTORIES} ${dap_core_INCLUDE_DIRS}") include_directories("${INCLUDE_DIRECTORIES} ${dap_crypto_INCLUDE_DIRS}") diff --git a/enc_server/enc_http.c b/enc_server/dap_enc_http.c similarity index 84% rename from enc_server/enc_http.c rename to enc_server/dap_enc_http.c index a937ce2d86b8e04a399b99a5fcfcc492cedd6485..a6edd7849e177f1bf4252ef47220dbb57248ef79 100644 --- a/enc_server/enc_http.c +++ b/enc_server/dap_enc_http.c @@ -21,19 +21,19 @@ #include <stdio.h> #include <string.h> #include <stdint.h> -#include "common.h" -#include "config.h" +#include "dap_common.h" +#include "dap_config.h" #include "dap_http.h" #include "dap_http_client.h" #include "dap_http_simple.h" -#include "enc.h" -#include "enc_ks.h" -#include "enc_key.h" -#include "enc_http.h" +#include "dap_enc.h" +#include "dap_enc_ks.h" +#include "dap_enc_key.h" +#include "dap_enc_http.h" -#define LOG_TAG "enc_http" +#define LOG_TAG "dap_enc_http" RSA* public_key_server = NULL; @@ -77,7 +77,7 @@ void enc_http_deinit() */ void enc_http_proc(struct dap_http_simple *cl_st, void * arg) { - log_it(DEBUG,"Proc enc http request"); + log_it(L_DEBUG,"Proc enc http request"); bool * isOk= (bool*)arg; if(strcmp(cl_st->http->url_path,"hsd9jslagd92abgjalp9h") == 0 ) { @@ -85,7 +85,7 @@ void enc_http_proc(struct dap_http_simple *cl_st, void * arg) enc_key_t* key_session = enc_key_generate(ENC_KEY_RSA_SESSION, key_session_pair); - enc_ks_key_t * key_ks = enc_ks_add(key_session); + dap_enc_ks_key_t * key_ks = dap_enc_ks_add(key_session); char *pubKey; char *sendMsg = malloc( @@ -107,7 +107,7 @@ void enc_http_proc(struct dap_http_simple *cl_st, void * arg) unsigned int sig_len = 0; if(RSA_sign(NID_sha256, (unsigned char*) pubKey, 200, sig, &sig_len, private_key_server) != 1) { - log_it(ERROR, "ERROR ENCRYPT"); + log_it(L_ERROR, "ERROR ENCRYPT"); } char *sig_64_out = (char*) malloc (1024); @@ -134,11 +134,11 @@ void enc_http_proc(struct dap_http_simple *cl_st, void * arg) *isOk=true; }else if(strcmp(cl_st->http->url_path,"gd4y5yh78w42aaagh")==0 ){ - //log_it(INFO, "SEND CONFIG KEY"); + //log_it(L_INFO, "SEND CONFIG KEY"); dap_http_simple_reply_f(cl_st,"%s",my_config.key_public); *isOk=true; }else{ - log_it(ERROR,"Wrong path '%s' in the request to enc_http module",cl_st->http->url_path); + log_it(L_ERROR,"Wrong path '%s' in the request to enc_http module",cl_st->http->url_path); *isOk=false; } } @@ -155,7 +155,7 @@ void enc_http_add_proc(struct dap_http * sh, const char * url) enc_http_delegate_t *enc_http_request_decode(struct dap_http_simple *cl_st) { - enc_key_t * key= enc_ks_find_http(cl_st->http); + enc_key_t * key= dap_enc_ks_find_http(cl_st->http); if(key){ enc_http_delegate_t * dg = DAP_NEW_Z(enc_http_delegate_t); dg->key=key; @@ -169,16 +169,16 @@ enc_http_delegate_t *enc_http_request_decode(struct dap_http_simple *cl_st) if(cl_st->request_size){ dg->request=calloc(1,cl_st->request_size+1); dg->request_size=enc_decode(key, cl_st->request, cl_st->request_size,dg->request,ENC_DATA_TYPE_RAW); - log_it(DEBUG,"Request after decode '%s'",dg->request_str); - // log_it(DEBUG,"Request before decode: '%s' after decode '%s'",cl_st->request_str,dg->request_str); + log_it(L_DEBUG,"Request after decode '%s'",dg->request_str); + // log_it(L_DEBUG,"Request before decode: '%s' after decode '%s'",cl_st->request_str,dg->request_str); } size_t url_path_size=strlen(cl_st->http->url_path); if(url_path_size){ dg->url_path=calloc(1,url_path_size+1); dg->url_path_size=enc_decode(key, cl_st->http->url_path,url_path_size,dg->url_path,ENC_DATA_TYPE_B64); - log_it(DEBUG,"URL path after decode '%s'",dg->url_path ); - // log_it(DEBUG,"URL path before decode: '%s' after decode '%s'",cl_st->http->url_path,dg->url_path ); + log_it(L_DEBUG,"URL path after decode '%s'",dg->url_path ); + // log_it(L_DEBUG,"URL path before decode: '%s' after decode '%s'",cl_st->http->url_path,dg->url_path ); } size_t in_query_size=strlen(cl_st->http->in_query_string); @@ -186,24 +186,24 @@ enc_http_delegate_t *enc_http_request_decode(struct dap_http_simple *cl_st) if(in_query_size){ dg->in_query=calloc(1,in_query_size+1); dg->in_query_size=enc_decode(key, cl_st->http->in_query_string,in_query_size,dg->in_query,ENC_DATA_TYPE_B64); - log_it(DEBUG,"Query string after decode '%s'",dg->in_query); + log_it(L_DEBUG,"Query string after decode '%s'",dg->in_query); } dg->response = calloc(1,cl_st->reply_size_max+1); dg->response_size_max=cl_st->reply_size_max; return dg; }else{ - log_it(WARNING,"No Key was found in the request"); + log_it(L_WARNING,"No Key was found in the request"); return NULL; } } void enc_http_reply_encode(struct dap_http_simple *cl_st,enc_http_delegate_t * dg) { - enc_key_t * key = enc_ks_find_http(cl_st->http); + enc_key_t * key = dap_enc_ks_find_http(cl_st->http); if( key == NULL ) { - log_it(ERROR, "Not find key"); + log_it(L_ERROR, "Not find key"); return; } if(dg->response){ @@ -214,7 +214,7 @@ void enc_http_reply_encode(struct dap_http_simple *cl_st,enc_http_delegate_t * d #else if (dg->response_size > part) { #endif - //log_it(DEBUG, "enc_http_reply_encode RSA WAY 1"); + //log_it(L_DEBUG, "enc_http_reply_encode RSA WAY 1"); char *out_enc_buffer = calloc (out_enc_mem_size, sizeof(char)); size_t copy_size, enc_size = 0; size_t end = dg->response_size; @@ -224,12 +224,12 @@ void enc_http_reply_encode(struct dap_http_simple *cl_st,enc_http_delegate_t * d copy_size = (end <= part) ? end : part; if(enc_size > out_enc_mem_size) { - log_it(WARNING, "Enc size > out_enc_mem_size"); + log_it(L_WARNING, "Enc size > out_enc_mem_size"); char *old = out_enc_buffer; out_enc_buffer = (char*)realloc(out_enc_buffer, out_enc_mem_size * 2); if(!out_enc_buffer) { free(old); - log_it(ERROR, "Can not memmory allocate"); + log_it(L_ERROR, "Can not memmory allocate"); return; } memset(out_enc_buffer + out_enc_mem_size, 0, out_enc_mem_size); @@ -244,13 +244,13 @@ void enc_http_reply_encode(struct dap_http_simple *cl_st,enc_http_delegate_t * d } cl_st->reply = calloc(1, enc_size); cl_st->reply_size = enc_size; - //log_it(INFO, "\n\n\nCOLLECTED DATA is {%s} size={%d}",out_enc_buffer, _enc_size); + //log_it(L_INFO, "\n\n\nCOLLECTED DATA is {%s} size={%d}",out_enc_buffer, _enc_size); memcpy(cl_st->reply, out_enc_buffer, enc_size); free (out_enc_buffer); } else if(dg->response_size>0){ - //log_it(DEBUG, "enc_http_reply_encode RSA WAY 2"); + //log_it(L_DEBUG, "enc_http_reply_encode RSA WAY 2"); if(cl_st->reply) free(cl_st->reply); @@ -302,7 +302,7 @@ size_t enc_http_reply_f(enc_http_delegate_t * dg, const char * data, ...) va_end(ap); return enc_http_reply(dg,buf,mem_size); }else - log_it(ERROR, "Can not memmory allocate"); + log_it(L_ERROR, "Can not memmory allocate"); return 0; } diff --git a/enc_server/enc_http.h b/enc_server/dap_enc_http.h similarity index 100% rename from enc_server/enc_http.h rename to enc_server/dap_enc_http.h diff --git a/enc_server/enc_ks.c b/enc_server/dap_enc_ks.c similarity index 72% rename from enc_server/enc_ks.c rename to enc_server/dap_enc_ks.c index 97b3bd5aba69e4f5d144d6a2f2488e8d67f8dce5..07671b5180e5bc3600183c5e73deb688661844c9 100644 --- a/enc_server/enc_ks.c +++ b/enc_server/dap_enc_ks.c @@ -19,30 +19,30 @@ */ #include "uthash.h" -#include "common.h" +#include "dap_common.h" #include "dap_http_client.h" #include "dap_http_header.h" -#include "enc.h" -#include "enc_ks.h" -#include "enc_key.h" +#include "dap_enc.h" +#include "dap_enc_ks.h" +#include "dap_enc_key.h" #define LOG_TAG "enc_ks" -enc_ks_key_t * ks=NULL; +dap_enc_ks_key_t * ks=NULL; -int enc_ks_init() +int dap_enc_ks_init() { return 0; } -void _enc_key_free(enc_ks_key_t **ptr); +void _enc_key_free(dap_enc_ks_key_t **ptr); -void enc_ks_deinit() +void dap_enc_ks_deinit() { if (ks) { - enc_ks_key_t *cur_item, *tmp; + dap_enc_ks_key_t *cur_item, *tmp; HASH_ITER(hh, ks, cur_item, tmp) { HASH_DEL(ks, cur_item); _enc_key_free(&cur_item); @@ -50,26 +50,26 @@ void enc_ks_deinit() } } -enc_ks_key_t * enc_ks_find(const char * v_id) +dap_enc_ks_key_t * dap_enc_ks_find(const char * v_id) { - enc_ks_key_t * ret=NULL; + dap_enc_ks_key_t * ret=NULL; HASH_FIND_STR(ks,v_id,ret); return ret; } -enc_key_t * enc_ks_find_http(struct dap_http_client * http) +enc_key_t * dap_enc_ks_find_http(struct dap_http_client * http) { dap_http_header_t * hdr_key_id=dap_http_header_find(http->in_headers,"KeyID"); if(hdr_key_id){ - enc_ks_key_t * ks_key=enc_ks_find(hdr_key_id->value); + dap_enc_ks_key_t * ks_key=dap_enc_ks_find(hdr_key_id->value); if(ks_key) return ks_key->key; else{ - //log_it(WARNING, "Not found keyID"); + //log_it(L_WARNING, "Not found keyID"); return NULL; } }else{ - log_it(WARNING, "No KeyID in HTTP headers"); + log_it(L_WARNING, "No KeyID in HTTP headers"); return NULL; } } @@ -86,9 +86,9 @@ enc_key_t * enc_ks_find_http(struct dap_http_client * http) return ret; }*/ -enc_ks_key_t * enc_ks_add(struct enc_key * key) +dap_enc_ks_key_t * dap_enc_ks_add(struct enc_key * key) { - enc_ks_key_t * ret = DAP_NEW_Z(enc_ks_key_t); + dap_enc_ks_key_t * ret = DAP_NEW_Z(dap_enc_ks_key_t); ret->key=key; pthread_mutex_init(&ret->mutex,NULL); int i; @@ -98,16 +98,16 @@ enc_ks_key_t * enc_ks_add(struct enc_key * key) return ret; } -void enc_ks_delete(const char *id) +void dap_enc_ks_delete(const char *id) { - enc_ks_key_t *delItem = enc_ks_find(id); + dap_enc_ks_key_t *delItem = dap_enc_ks_find(id); if (delItem) { HASH_DEL (ks, delItem); _enc_key_free(&delItem); } } -void _enc_key_free(enc_ks_key_t **ptr) +void _enc_key_free(dap_enc_ks_key_t **ptr) { if (*ptr){ if((*ptr)->key) diff --git a/enc_server/enc_ks.h b/enc_server/dap_enc_ks.h similarity index 75% rename from enc_server/enc_ks.h rename to enc_server/dap_enc_ks.h index c3cda573cff0f572ceb4362b01e7f39edbdc8194..9ec92d4e5ce8e89feb7226288cbe6caf64b01f2b 100644 --- a/enc_server/enc_ks.h +++ b/enc_server/dap_enc_ks.h @@ -23,25 +23,25 @@ #include <time.h> #include <pthread.h> #include "uthash.h" -struct dap_http_client; -struct enc_key; -typedef struct enc_ks_key{ +struct dap_http_client; +typedef struct dap_enc_key dap_enc_key_t; +typedef struct dap_enc_ks_key{ char id[33]; - struct enc_key *key; + dap_enc_key_t *key; time_t time_created; pthread_mutex_t mutex; UT_hash_handle hh; // makes this structure hashable with UTHASH library -} enc_ks_key_t; +} dap_enc_ks_key_t; -extern int enc_ks_init(); -extern void enc_ks_deinit(); +int dap_enc_ks_init(); +void dap_enc_ks_deinit(); -extern enc_ks_key_t * enc_ks_find(const char * v_id); -extern struct enc_key * enc_ks_find_http(struct dap_http_client * http); +dap_enc_ks_key_t * dap_enc_ks_find(const char * v_id); +struct enc_key * dap_enc_ks_find_http(struct dap_http_client * http); //extern enc_ks_key_t * enc_ks_new(); -extern enc_ks_key_t * enc_ks_add(struct enc_key * key); -extern void enc_ks_delete(const char *id); +dap_enc_ks_key_t * dap_enc_ks_add(struct enc_key * key); +void dap_enc_ks_delete(const char *id); #endif diff --git a/http_server/CMakeLists.txt b/http_server/CMakeLists.txt index b0d2f241984d1f5277c43c0af88871ddf987679c..3445fbefa234f4060e696e9c0f60f681f7eabda9 100644 --- a/http_server/CMakeLists.txt +++ b/http_server/CMakeLists.txt @@ -5,11 +5,15 @@ set(HTTP_SRCS dap_http.c dap_http_folder.c dap_http_simple.c dap_http_simple.h include_directories("${INCLUDE_DIRECTORIES} ${dap_core_INCLUDE_DIRS}") include_directories("${INCLUDE_DIRECTORIES} ${dap_crypto_INCLUDE_DIRS}") +include_directories("${INCLUDE_DIRECTORIES} ${dap_enc_server_INCLUDE_DIRS}") include_directories("${INCLUDE_DIRECTORIES} ${dap_client_INCLUDE_DIRS}") +include_directories("${INCLUDE_DIRECTORIES} ${dap_http_INCLUDE_DIRS}") include_directories("${INCLUDE_DIRECTORIES} ${dap_core_server_INCLUDE_DIRS}") add_definitions ("${dap_core_DEFINITIONS}") +add_definitions ("${dap_http_DEFINITIONS}") add_definitions ("${dap_crypto_DEFINITIONS}") +add_definitions ("${dap_enc_server_DEFINITIONS}") add_definitions ("${dap_client_DEFINITIONS}") add_definitions ("${dap_core_server_DEFINITIONS}") diff --git a/http_server/dap_http.c b/http_server/dap_http.c index 9e407619d8bf6bc866f2c3dfb175585dbb7b654c..bc0aba8f9c258badb393088d8aaaea2c31ac5ec4 100644 --- a/http_server/dap_http.c +++ b/http_server/dap_http.c @@ -36,7 +36,7 @@ #include <dirent.h> #include <netdb.h> -#include "common.h" +#include "dap_common.h" #include "dap_client.h" #include "dap_server.h" @@ -45,7 +45,7 @@ #include "dap_http_client.h" -#define LOG_TAG "http" +#define LOG_TAG "dap_http" /** @@ -55,14 +55,14 @@ int dap_http_init() { if(dap_http_header_init()!=0){ // Init submodule for headers manipulations - log_it(CRITICAL,"Can't init HTTP headers processing submodule"); + log_it(L_CRITICAL,"Can't init HTTP headers processing submodule"); return -1; } if(dap_http_client_init()!=0){ // Init submodule for HTTP client event processing - log_it(CRITICAL,"Can't init HTTP client submodule"); + log_it(L_CRITICAL,"Can't init HTTP client submodule"); return -2; } - log_it(NOTICE,"Initialized HTTP server module"); + log_it(L_NOTICE,"Initialized HTTP server module"); return 0; } @@ -84,7 +84,7 @@ void dap_http_deinit() */ int dap_http_new(dap_server_t *sh, const char * server_name) { - sh->internal= calloc(1,sizeof(dap_http_t)); + sh->_inheritor= calloc(1,sizeof(dap_http_t)); dap_http_t *shttp = DAP_HTTP(sh); @@ -114,8 +114,8 @@ void dap_http_delete(dap_server_t *sh,void * arg) HASH_ITER(hh, shttp->url_proc , up, tmp) { HASH_DEL(shttp->url_proc, up); - if(up->internal) - free(up->internal); + if(up->_inheritor) + free(up->_inheritor); free(up); } @@ -150,9 +150,9 @@ void dap_http_add_proc(dap_http_t * sh, const char * url_path, void * internal up->headers_read_callback=headers_read_callback; up->headers_write_callback=headers_write_callback; up->error_callback=error_callback; - up->internal=internal; + up->_inheritor=internal; HASH_ADD_STR(sh->url_proc,url,up); - log_it(DEBUG,"Added URL processor for '%s' path",up->url); + log_it(L_DEBUG,"Added URL processor for '%s' path",up->url); } diff --git a/http_server/dap_http.h b/http_server/dap_http.h index f44bb41b9509fe502e30aaf2672f24c5b11fc65a..ff21cd1ddeb2949e3e17c26f5625aecd299b8215 100644 --- a/http_server/dap_http.h +++ b/http_server/dap_http.h @@ -47,7 +47,7 @@ typedef struct dap_http_url_proc{ dap_http_client_callback_t access_callback; - void * internal; // Internal data specific to the current URL processor + void * _inheritor; // Internal data specific to the current URL processor UT_hash_handle hh; // makes this structure hashable with UTHASH library } dap_http_url_proc_t; @@ -58,7 +58,7 @@ typedef struct dap_http { dap_http_url_proc_t * url_proc; } dap_http_t; -#define DAP_HTTP(a) ((dap_http_t *) (a)->internal) +#define DAP_HTTP(a) ((dap_http_t *) (a)->_inheritor) extern int dap_http_init(); // Init module extern void dap_http_deinit(); // Deinit module diff --git a/http_server/dap_http_folder.c b/http_server/dap_http_folder.c index ddec9659f0f4ebb394fa6b354a513432458b946b..f765272c5747bf3a9686503999b8df68d9e6d8a4 100644 --- a/http_server/dap_http_folder.c +++ b/http_server/dap_http_folder.c @@ -26,7 +26,7 @@ #include <magic.h> #include <errno.h> -#include "common.h" +#include "dap_common.h" #include "dap_client.h" #include "dap_http.h" #include "dap_http_client.h" @@ -37,7 +37,7 @@ typedef struct dap_http_url_proc_folder{ char local_path[4096]; magic_t mime_detector; } dap_http_url_proc_folder_t; -#define URL_PROC_FOLDER(a) ((dap_http_url_proc_folder_t*) (a)->internal ) +#define URL_PROC_FOLDER(a) ((dap_http_url_proc_folder_t*) (a)->_inhertior ) typedef struct dap_http_file{ FILE * fd; @@ -46,7 +46,7 @@ typedef struct dap_http_file{ dap_http_client_t * client; } dap_http_file_t; -#define DAP_HTTP_FILE(a) ((dap_http_file_t*) (a)->internal ) +#define DAP_HTTP_FILE(a) ((dap_http_file_t*) (a)->_inheritor ) void dap_http_folder_headers_read(dap_http_client_t * cl_ht, void * arg); void dap_http_folder_headers_write(dap_http_client_t * cl_ht, void * arg); @@ -54,7 +54,7 @@ void dap_http_folder_data_read(dap_http_client_t * cl_ht, void * arg); void dap_http_folder_data_write(dap_http_client_t * cl_ht, void * arg); -#define LOG_TAG "http_folder" +#define LOG_TAG "dap_http_folder" int dap_http_folder_init() { @@ -78,15 +78,15 @@ void dap_http_folder_deinit() int dap_http_folder_add(dap_http_t *sh, const char * url_path, const char * local_path) { if (!local_path) { - log_it(ERROR,"Directory Path parameter is empty!"); + log_it(L_ERROR,"Directory Path parameter is empty!"); return -11; } DIR * dirptr = opendir(local_path); if (dirptr == NULL) { - log_it(ERROR,"Directory Not Found!"); + log_it(L_ERROR,"Directory Not Found!"); return -11; }else{ - log_it(NOTICE, "File service for %s => %s ",url_path,local_path); + log_it(L_NOTICE, "File service for %s => %s ",url_path,local_path); closedir(dirptr); } @@ -95,13 +95,13 @@ int dap_http_folder_add(dap_http_t *sh, const char * url_path, const char * loca up_folder->mime_detector=magic_open(MAGIC_SYMLINK|MAGIC_MIME|MAGIC_PRESERVE_ATIME); if(up_folder->mime_detector==NULL){ - log_it(CRITICAL,"Can't init MIME detection library"); + log_it(L_CRITICAL,"Can't init MIME detection library"); free(up_folder); return -1; } if( 0!= magic_load(up_folder->mime_detector, NULL)){ - log_it(CRITICAL, "Can't load MIME magic detection database"); + log_it(L_CRITICAL, "Can't load MIME magic detection database"); magic_close(up_folder->mime_detector); free(up_folder); return -2; @@ -136,17 +136,17 @@ void dap_http_folder_headers_write(dap_http_client_t * cl_ht, void * arg) { (void) arg; // Get specific data for folder URL processor - dap_http_url_proc_folder_t * up_folder=(dap_http_url_proc_folder_t*) cl_ht->proc->internal; + dap_http_url_proc_folder_t * up_folder=(dap_http_url_proc_folder_t*) cl_ht->proc->_inheritor; // Init specific file response data for HTTP client instance - cl_ht->internal=(dap_http_file_t *) calloc (1,sizeof(dap_http_file_t)); + cl_ht->_inheritor=DAP_NEW_Z(dap_http_file_t); dap_http_file_t* cl_ht_file=DAP_HTTP_FILE(cl_ht); cl_ht_file->client=cl_ht; // Produce local path for file to open snprintf(cl_ht_file->local_path,sizeof(cl_ht_file->local_path),"%s/%s", up_folder->local_path, cl_ht->url_path ); - log_it(DEBUG, "Check %s file", cl_ht_file->local_path); + log_it(L_DEBUG, "Check %s file", cl_ht_file->local_path); struct stat file_stat; if(stat(cl_ht_file->local_path,&file_stat)==0){ @@ -154,7 +154,7 @@ void dap_http_folder_headers_write(dap_http_client_t * cl_ht, void * arg) cl_ht->out_content_length=file_stat.st_size; cl_ht_file->fd=fopen(cl_ht_file->local_path,"r"); if(cl_ht_file->fd == NULL){ - log_it(ERROR, "Can't open %s: %s",cl_ht_file->local_path,strerror(errno)); + log_it(L_ERROR, "Can't open %s: %s",cl_ht_file->local_path,strerror(errno)); cl_ht->reply_status_code=404; strncpy(cl_ht->reply_reason_phrase,"Not Found",sizeof(cl_ht->reply_reason_phrase)); }else{ @@ -164,13 +164,13 @@ void dap_http_folder_headers_write(dap_http_client_t * cl_ht, void * arg) const char * mime_type = magic_file( up_folder->mime_detector,cl_ht_file->local_path); if(mime_type){ strncpy(cl_ht->out_content_type,mime_type,sizeof(cl_ht->out_content_type)); - log_it(DEBUG,"MIME type detected: '%s'",mime_type); + log_it(L_DEBUG,"MIME type detected: '%s'",mime_type); }else - log_it(WARNING,"Can't detect MIME type of %s file: %s",cl_ht_file->local_path,magic_error(up_folder->mime_detector)); + log_it(L_WARNING,"Can't detect MIME type of %s file: %s",cl_ht_file->local_path,magic_error(up_folder->mime_detector)); } }else{ - log_it(WARNING, "Can't get file info: %s",strerror(errno)); + log_it(L_WARNING, "Can't get file info: %s",strerror(errno)); cl_ht->reply_status_code=404; strncpy(cl_ht->reply_reason_phrase,"Not Found",sizeof(cl_ht->reply_reason_phrase)); } @@ -201,7 +201,7 @@ void dap_http_folder_data_write(dap_http_client_t * cl_ht, void * arg) cl_ht_file->position+=cl_ht->client->buf_out_size; if(feof(cl_ht_file->fd)!=0){ - log_it(INFO, "All the file %s is sent out",cl_ht_file->local_path); + log_it(L_INFO, "All the file %s is sent out",cl_ht_file->local_path); //strncat(cl_ht->client->buf_out+cl_ht->client->buf_out_size,"\r\n",sizeof(cl_ht->client->buf_out)); fclose(cl_ht_file->fd); dap_client_ready_to_write(cl_ht->client,false); diff --git a/http_server/dap_http_simple.c b/http_server/dap_http_simple.c index 6c53bee67a59b7f213a38aebc530ff187d4bfcc1..6e5efee585b7b035f9a3ca93249efc87f5186889 100644 --- a/http_server/dap_http_simple.c +++ b/http_server/dap_http_simple.c @@ -23,22 +23,21 @@ #include <stdbool.h> #include <string.h> -#include "common.h" +#include "dap_common.h" #include "dap_http.h" #include "dap_http_client.h" #include "dap_http_simple.h" -#include "enc_key.h" -#include "enc_ks.h" -#include "enc_http.h" -#include "config.h" +#include "dap_enc_key.h" +#include "dap_enc_ks.h" +#include "dap_enc_http.h" +#include "dap_config.h" #include <ev.h> #include <sys/queue.h> -#define LAST_USE_KEY(key) ((rsa_key_t*)key->internal)->last_time_use_key #define LOG_TAG "dap_http_simple" void dap_http_simple_headers_read(dap_http_client_t * cl_ht, void * arg ); -void dap_http_simple_data_write(dap_http_client_t * cl_ht,void * arg); +void dap_http_simple_data_write(dap_http_client_t * a_http_client,void * a_arg); void dap_http_simple_data_read(dap_http_client_t * cl_ht,void * arg); void* dap_http_simple_proc(dap_http_simple_t * cl_sh); @@ -57,15 +56,17 @@ typedef struct tailq_entry { } tailq_entry_t; TAILQ_HEAD(, tailq_entry) tailq_head; -#define DAP_HTTP_SIMPLE_URL_PROC(a) ((dap_http_simple_url_proc_t*) (a)->internal) +#define DAP_HTTP_SIMPLE_URL_PROC(a) ((dap_http_simple_url_proc_t*) (a)->_inheritor) static struct ev_loop* http_simple_loop; static ev_async async_watcher_http_simple; static pthread_mutex_t mutex_on_queue_http_response = PTHREAD_MUTEX_INITIALIZER; - +static dap_config_t * s_config = NULL; int dap_http_simple_module_init() { + s_config = dap_config_open("http_simple"); + pthread_mutex_init(&mutex_on_queue_http_response, NULL); http_simple_loop = ev_loop_new(0); @@ -95,7 +96,7 @@ static void async_control_proc (EV_P_ ev_async *w, int revents) static void* loop_http_simple_proc(void *arg) { - log_it(NOTICE, "Start loop http simple thread"); + log_it(L_NOTICE, "Start loop http simple thread"); ev_loop(http_simple_loop, 0); return NULL; } @@ -126,33 +127,31 @@ void dap_http_simple_proc_add(dap_http_t *sh, const char * url_path, size_t repl */ void* dap_http_simple_proc(dap_http_simple_t * cl_sh) { - log_it(INFO, "dap http simple proc"); + log_it(L_INFO, "dap http simple proc"); bool is_ok=true; bool key_is_expiried = false; - enc_key_t * key = enc_ks_find_http(cl_sh->http); - if(key && key->type == ENC_KEY_RSA_SESSION) - { - if( LAST_USE_KEY(key) != 0 && key->type == ENC_KEY_RSA_SESSION // if == 0 it's first use key - && ( time(NULL) - LAST_USE_KEY(key) ) > my_config.TTL_session_key * 60) - { + dap_enc_key_t * key = dap_enc_ks_find_http(cl_sh->http); + if(key){ + if( key->last_used_timestamp && ( (time(NULL) - key->last_used_timestamp ) + > dap_config_get_item_int32(s_config,"session","key_ttl") ) ) { + enc_http_delegate_t * dg = enc_http_request_decode(cl_sh); - if( dg == NULL ) - { - log_it(ERROR, "dg is NULL"); + if( dg == NULL ) { + log_it(L_ERROR, "dg is NULL"); return NULL; } - log_it(WARNING, "Key has been expiried"); + log_it(L_WARNING, "Key has been expiried"); strcpy(cl_sh->reply_mime,"text/plain"); enc_http_reply_f(dg,"Key has been expiried"); enc_http_reply_encode(cl_sh,dg); enc_http_delegate_delete(dg); key_is_expiried = true; + } else{ + key->last_used_timestamp = time(NULL); } - else - LAST_USE_KEY(key) = time(NULL); } @@ -160,7 +159,7 @@ void* dap_http_simple_proc(dap_http_simple_t * cl_sh) DAP_HTTP_SIMPLE_URL_PROC(cl_sh->http->proc)->proc_callback(cl_sh,&is_ok); if(is_ok){ - log_it(DEBUG, "Request was processed well"); + log_it(L_DEBUG, "Request was processed well"); if(cl_sh->reply_proc_post_callback){ void * enc_data = calloc(1,cl_sh->reply_size*2); @@ -176,7 +175,7 @@ void* dap_http_simple_proc(dap_http_simple_t * cl_sh) cl_sh->http->reply_status_code=200; //cl_sh->http->client->ready_to_write=true; }else{ - log_it(ERROR, "Request was processed with ERROR"); + log_it(L_ERROR, "Request was processed with ERROR"); strcpy(cl_sh->http->reply_reason_phrase,"ERROR"); cl_sh->http->reply_status_code=500; //cl_sh->http->client->ready_to_read=false; @@ -196,7 +195,7 @@ void* dap_http_simple_proc(dap_http_simple_t * cl_sh) */ void dap_http_simple_headers_read(dap_http_client_t * cl_ht, void * arg ) { - cl_ht->internal = DAP_NEW_Z(dap_http_simple_t); + cl_ht->_inheritor = DAP_NEW_Z(dap_http_simple_t); DAP_HTTP_SIMPLE(cl_ht)->http = cl_ht; DAP_HTTP_SIMPLE(cl_ht)->reply_size_max = DAP_HTTP_SIMPLE_URL_PROC( cl_ht->proc )->reply_size_max; @@ -207,11 +206,11 @@ void dap_http_simple_headers_read(dap_http_client_t * cl_ht, void * arg ) if(cl_ht->in_content_length< DAP_HTTP_SIMPLE_REQUEST_MAX) DAP_HTTP_SIMPLE(cl_ht)->request = calloc(1,cl_ht->in_content_length+1); else - log_it(ERROR, "Too big content-length %u in request", cl_ht->in_content_length); + log_it(L_ERROR, "Too big content-length %u in request", cl_ht->in_content_length); } else { - log_it(DEBUG,"No data section, execution proc callback"); + log_it(L_DEBUG,"No data section, execution proc callback"); queue_http_request_put(DAP_HTTP_SIMPLE(cl_ht)); } } @@ -232,7 +231,7 @@ void dap_http_simple_data_read(dap_http_client_t * cl_ht,void * arg) if(shs->request_size >=cl_ht->in_content_length) { // bool isOK=true; - log_it(DEBUG,"Data collected"); + log_it(L_DEBUG,"Data collected"); *ret=cl_ht->client->buf_in_size; queue_http_request_put(shs); } @@ -241,23 +240,22 @@ void dap_http_simple_data_read(dap_http_client_t * cl_ht,void * arg) /** * @brief dap_http_simple_data_write - * @param cl_ht - * @param arg + * @param a_http_client + * @param a_arg */ -void dap_http_simple_data_write(dap_http_client_t * cl_ht,void * arg) +void dap_http_simple_data_write(dap_http_client_t * a_http_client,void * a_arg) { - (void) arg; - dap_http_simple_t * cl_st = DAP_HTTP_SIMPLE(cl_ht); + (void) a_arg; + dap_http_simple_t * cl_st = DAP_HTTP_SIMPLE(a_http_client); - cl_st->reply_sent += dap_client_write(cl_ht->client, + cl_st->reply_sent += dap_client_write(a_http_client->client, cl_st->reply + cl_st->reply_sent, - cl_ht->out_content_length - cl_st->reply_sent); + a_http_client->out_content_length - cl_st->reply_sent); - if(cl_st->reply_sent>=cl_ht->out_content_length) - { - log_it(INFO, "All the reply (%u) is sent out",cl_ht->out_content_length); + if(cl_st->reply_sent>=a_http_client->out_content_length) { + log_it(L_INFO, "All the reply (%u) is sent out",a_http_client->out_content_length); //cl_ht->client->signal_close=cl_ht->keep_alive; - cl_ht->client->signal_close=true; + a_http_client->client->signal_close=true; //dap_client_ready_to_write(cl_ht->client,false); } } diff --git a/http_server/dap_http_simple.h b/http_server/dap_http_simple.h index 51fba93d2b3f89f06954df92caa86a1f50dcabd1..f4785894dbe3f650cb4fdff65fc808dbcac53d45 100644 --- a/http_server/dap_http_simple.h +++ b/http_server/dap_http_simple.h @@ -49,7 +49,7 @@ typedef struct dap_http_simple{ dap_http_simple_callback_t reply_proc_post_callback; } dap_http_simple_t; -#define DAP_HTTP_SIMPLE(a) ((dap_http_simple_t*) (a)->internal ) +#define DAP_HTTP_SIMPLE(a) ((dap_http_simple_t*) (a)->_inheritor ) extern void dap_http_simple_proc_add(dap_http_t *sh, const char * url_path, size_t reply_size_max, dap_http_simple_callback_t cb); // Add simple processor