From 708e54989d02fdc9eabb7c62201758781c34851d Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Sat, 10 Feb 2018 12:24:01 +0100 Subject: [PATCH] ssl alpha support --- http_server.c | 12 +++++------- http_server.h | 2 +- httpd.c | 21 +++++++++++---------- libs/handle.c | 13 ++++++++++--- libs/plugin.c | 5 +++-- libs/utils.c | 1 + 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/http_server.c b/http_server.c index 2813234..8c93b38 100644 --- a/http_server.c +++ b/http_server.c @@ -17,9 +17,8 @@ void accept_request(void* client) struct stat st; //char *query_string = NULL; - LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock); - numchars = get_line(((antd_client_t*)client)->sock, buf, sizeof(buf)); - printf("BUF: %s\n", buf); + //LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock); + numchars = read_buf(client, buf, sizeof(buf)); i = 0; j = 0; while (!ISspace(buf[j]) && (i < sizeof(method) - 1)) { @@ -236,7 +235,7 @@ void error_die(const char *sc) * Returns: the number of bytes stored (excluding null) */ /**********************************************************************/ //This function is deprecate -int get_line(int sock, char *buf, int size) +/*int get_line(int sock, char *buf, int size) { int i = 0; char c = '\0'; @@ -266,7 +265,7 @@ int get_line(int sock, char *buf, int size) buf[i] = '\0'; return(i); -} +}*/ /**********************************************************************/ @@ -462,7 +461,6 @@ dictionary decode_request(void* client,const char* method, char* url) while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf)) { line = buf; - printf("LINE1: %s \n", line); trim(line, '\n'); trim(line, '\r'); token = strsep(&line,":"); @@ -867,7 +865,7 @@ int execute_plugin(void* client, const char *path, const char *method, dictionar memcpy(pfunc,rpath+npos+1,fpos); pfunc[fpos-1]='\0'; } - LOG("Client %d\n",client ); + LOG("Client %d\n",((antd_client_t*)client)->sock ); LOG("Path : '%s'\n", rpath); LOG("Method:%s\n", method); LOG("Plugin name '%s'\n",pname); diff --git a/http_server.h b/http_server.h index aa92366..34eeb2e 100644 --- a/http_server.h +++ b/http_server.h @@ -28,7 +28,7 @@ void accept_request(void*); void cat(void*, FILE *); void cannot_execute(void*); void error_die(const char *); -int get_line(int, char *, int); +//int get_line(int, char *, int); void not_found(void*); void serve_file(void*, const char *); int startup(unsigned *); diff --git a/httpd.c b/httpd.c index d2c4926..4b3355e 100644 --- a/httpd.c +++ b/httpd.c @@ -136,9 +136,9 @@ void load_config(const char* file) { LOG("Using configuration : %s\n", file); #ifdef USE_OPENSSL - LOG("Enable %d\n", server_config.usessl); - LOG("cert %s\n", server_config.sslcert); - LOG("key %s\n", server_config.sslkey); + LOG("SSL enable %d\n", server_config.usessl); + LOG("SSL cert %s\n", server_config.sslcert); + LOG("SSL key %s\n", server_config.sslkey); #endif } init_file_system(); @@ -185,7 +185,7 @@ int main(int argc, char* argv[]) while (1) { - antd_client_t client; + antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t)); client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len); if (client_sock == -1) { @@ -195,26 +195,27 @@ int main(int argc, char* argv[]) /* accept_request(client_sock); */ #ifdef USE_OPENSSL - client.ssl = NULL; + client->ssl = NULL; if(server_config.usessl == 1) { - client.ssl = (void*)SSL_new(ctx); - SSL_set_fd((SSL*)client.ssl, client_sock); + client->ssl = (void*)SSL_new(ctx); + SSL_set_fd((SSL*)client->ssl, client_sock); - if (SSL_accept((SSL*)client.ssl) <= 0) { + if (SSL_accept((SSL*)client->ssl) <= 0) { ERR_print_errors_fp(stderr); continue; } } #endif - client.sock = client_sock; - if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)&client) != 0) + client->sock = client_sock; + if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0) perror("pthread_create"); else { //reclaim the stack data when thread finish pthread_detach(newthread) ; } + //accept_request(&client); } close(server_sock); diff --git a/libs/handle.c b/libs/handle.c index 6870569..b88efc2 100644 --- a/libs/handle.c +++ b/libs/handle.c @@ -70,10 +70,12 @@ int response(void* client, const char* data) } int antd_send(const void *src, const void* data, int len, int _ssl) { + if(!src) return -1; antd_client_t * source = (antd_client_t *) src; #ifdef USE_OPENSSL if(_ssl) { + //LOG("SSL WRITE\n"); return SSL_write((SSL*) source->ssl, data, len); } else @@ -86,10 +88,12 @@ int antd_send(const void *src, const void* data, int len, int _ssl) } int antd_recv(const void *src, void* data, int len, int _ssl) { + if(!src) return -1; antd_client_t * source = (antd_client_t *) src; #ifdef USE_OPENSSL if(_ssl) { + //LOG("SSL READ\n"); return SSL_read((SSL*) source->ssl, data, len); } else @@ -102,15 +106,18 @@ int antd_recv(const void *src, void* data, int len, int _ssl) } int antd_close(void* src) { + if(!src) return -1; antd_client_t * source = (antd_client_t *) src; #ifdef USE_OPENSSL if(source->ssl && usessl()){ SSL_free((SSL*) source->ssl); - LOG("Freeing SSL\n"); + //LOG("Freeing SSL\n"); } #endif - printf("Close sock %d\n", source->sock); - close(source->sock); + //printf("Close sock %d\n", source->sock); + int ret = close(source->sock); + free(src); + return ret; } int __ti(void* client,int data) { diff --git a/libs/plugin.c b/libs/plugin.c index d5893e8..473caa7 100644 --- a/libs/plugin.c +++ b/libs/plugin.c @@ -34,12 +34,13 @@ sqldb getdb() } #endif -#ifdef USE_OPENSSL +/*#ifdef USE_OPENSSL int usessl() { + LOG("CALLED from plugin \n"); return __plugin__.usessl; } - #endif + #endif*/ char* route(const char* repath) { diff --git a/libs/utils.c b/libs/utils.c index 2a59ea9..6c47d82 100644 --- a/libs/utils.c +++ b/libs/utils.c @@ -81,6 +81,7 @@ char* __s(const char* fstring,...) */ void trim(char* str, const char delim) { + if(!str) return; char * p = str; int l = strlen(p);