From 8618c9ebf5e6d1627d4fa9d4b31de34476ce77c2 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Thu, 3 May 2018 15:10:44 +0200 Subject: [PATCH] fix --- httpd.c | 12 ++++++++++++ libs/handle.c | 20 ++++++++++++++++---- libs/handle.h | 4 ++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/httpd.c b/httpd.c index d5ce937..955d364 100644 --- a/httpd.c +++ b/httpd.c @@ -239,6 +239,18 @@ int main(int argc, char* argv[]) } //return &(((struct sockaddr_in6*)sa)->sin6_addr); /* accept_request(client_sock); */ + + // set timeout to socket + struct timeval timeout; + timeout.tv_sec = 20; + timeout.tv_usec = 0; + + if (setsockopt (client_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout)) < 0) + perror("setsockopt failed\n"); + + if (setsockopt (client_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(timeout)) < 0) + perror("setsockopt failed\n"); + client->sock = client_sock; server_config.connection++; //LOG("Unclosed connection: %d\n", server_config.connection); diff --git a/libs/handle.c b/libs/handle.c index 0666f10..e1cbea9 100644 --- a/libs/handle.c +++ b/libs/handle.c @@ -72,38 +72,50 @@ int response(void* client, const char* data) int antd_send(const void *src, const void* data, int len) { if(!src || !data) return -1; + int ret; antd_client_t * source = (antd_client_t *) src; #ifdef USE_OPENSSL if(usessl()) { //LOG("SSL WRITE\n"); - return SSL_write((SSL*) source->ssl, data, len); + ret = SSL_write((SSL*) source->ssl, data, len); } else { #endif - return send(source->sock, data, len, 0); + ret = send(source->sock, data, len, 0); #ifdef USE_OPENSSL } #endif + if(ret <= 0) + { + antd_close(src); + } + return ret; } int antd_recv(const void *src, void* data, int len) { if(!src) return -1; + int ret; antd_client_t * source = (antd_client_t *) src; #ifdef USE_OPENSSL if(usessl()) { //LOG("SSL READ\n"); - return SSL_read((SSL*) source->ssl, data, len); + ret = SSL_read((SSL*) source->ssl, data, len); } else { #endif - return recv(((int) source->sock), data, len, 0); + ret = recv(((int) source->sock), data, len, 0); #ifdef USE_OPENSSL } #endif + if(ret == 0) + { + antd_close(src); + } + return ret; } int antd_close(void* src) { diff --git a/libs/handle.h b/libs/handle.h index b3ac433..f231ba5 100644 --- a/libs/handle.h +++ b/libs/handle.h @@ -77,7 +77,7 @@ void unknow(void*); int ws_enable(dictionary); char* read_line(void* sock); int read_buf(void* sock,char* buf,int i); -int antd_send(const void *source, const void* data, int len); -int antd_recv(const void *source, void* data, int len); +int antd_send( void *source, const void* data, int len); +int antd_recv( void *source, void* data, int len); int antd_close(void* source); #endif