2015-10-22 11:39:11 +02:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include "http_server.h"
|
2018-02-09 13:13:11 +01:00
|
|
|
#include "libs/ini.h"
|
2018-10-03 23:42:42 +02:00
|
|
|
static antd_scheduler_t scheduler;
|
2018-10-05 19:01:39 +02:00
|
|
|
static int server_sock = -1;
|
2015-10-22 11:39:11 +02:00
|
|
|
|
2018-02-10 11:22:41 +01:00
|
|
|
#ifdef USE_OPENSSL
|
2018-02-10 16:57:21 +01:00
|
|
|
static int ssl_session_ctx_id = 1;
|
2018-03-02 19:04:00 +01:00
|
|
|
SSL_CTX *ctx;
|
2018-02-10 11:22:41 +01:00
|
|
|
void init_openssl()
|
|
|
|
{
|
|
|
|
SSL_load_error_strings();
|
|
|
|
OpenSSL_add_ssl_algorithms();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_openssl()
|
|
|
|
{
|
|
|
|
EVP_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX *create_context()
|
|
|
|
{
|
|
|
|
const SSL_METHOD *method;
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
method = SSLv23_server_method();
|
|
|
|
|
|
|
|
ctx = SSL_CTX_new(method);
|
|
|
|
if (!ctx) {
|
2018-10-08 19:32:23 +02:00
|
|
|
perror("Unable to create SSL context");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
2018-02-10 11:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void configure_context(SSL_CTX *ctx)
|
|
|
|
{
|
2018-03-19 12:06:22 +01:00
|
|
|
#if defined(SSL_CTX_set_ecdh_auto)
|
2018-02-10 11:22:41 +01:00
|
|
|
SSL_CTX_set_ecdh_auto(ctx, 1);
|
2018-03-19 12:06:22 +01:00
|
|
|
#else
|
|
|
|
SSL_CTX_set_tmp_ecdh(ctx, EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
|
|
|
|
#endif
|
|
|
|
//SSL_CTX_set_ecdh_auto(ctx, 1);
|
2018-02-10 16:57:21 +01:00
|
|
|
/* Set some options and the session id.
|
|
|
|
* SSL_OP_NO_SSLv2: SSLv2 is insecure, disable it.
|
|
|
|
* SSL_OP_NO_TICKET: We don't want TLS tickets used because this is an SSL server caching example.
|
|
|
|
* It should be fine to use tickets in addition to server side caching.
|
|
|
|
*/
|
|
|
|
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_TICKET);
|
|
|
|
SSL_CTX_set_session_id_context(ctx, (void *)&ssl_session_ctx_id, sizeof(ssl_session_ctx_id));
|
2018-02-10 11:22:41 +01:00
|
|
|
/* Set the key and cert */
|
2018-06-26 13:40:53 +02:00
|
|
|
/* use the full chain bundle of certificate */
|
2018-10-05 19:01:39 +02:00
|
|
|
//if (SSL_CTX_use_certificate_file(ctx, server_config->sslcert, SSL_FILETYPE_PEM) <= 0) {
|
|
|
|
if (SSL_CTX_use_certificate_chain_file(ctx, config()->sslcert) <= 0) {
|
2018-06-26 13:40:53 +02:00
|
|
|
ERR_print_errors_fp(stderr);
|
2018-02-10 16:57:21 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-02-10 11:22:41 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 19:01:39 +02:00
|
|
|
if (SSL_CTX_use_PrivateKey_file(ctx, config()->sslkey, SSL_FILETYPE_PEM) <= 0 ) {
|
2018-02-10 11:22:41 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2018-02-10 16:57:21 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (!SSL_CTX_check_private_key(ctx)) {
|
|
|
|
LOG("Failed to validate cert \n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
2018-02-10 11:22:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-22 23:13:53 +02:00
|
|
|
void stop_serve(int dummy) {
|
2018-10-03 23:42:42 +02:00
|
|
|
UNUSED(dummy);
|
2018-10-05 19:01:39 +02:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
//Blocks the SIG_IGN signal (by adding SIG_IGN to newMask)
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGPIPE);
|
|
|
|
sigaddset(&mask, SIGABRT);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, NULL);
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_scheduler_destroy(&scheduler);
|
2018-10-05 19:01:39 +02:00
|
|
|
unload_all_plugin();
|
|
|
|
destroy_config();
|
2018-03-02 19:04:00 +01:00
|
|
|
#ifdef USE_OPENSSL
|
2018-10-09 12:15:48 +02:00
|
|
|
FIPS_mode_set(0);
|
2018-03-02 19:04:00 +01:00
|
|
|
SSL_CTX_free(ctx);
|
2018-10-09 12:15:48 +02:00
|
|
|
FIPS_mode_set(0);
|
|
|
|
CONF_modules_unload(1);
|
|
|
|
EVP_cleanup();
|
|
|
|
ENGINE_cleanup();
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
ERR_remove_state(0);
|
|
|
|
ERR_free_strings();
|
2018-03-02 19:04:00 +01:00
|
|
|
#endif
|
2018-10-10 12:42:47 +02:00
|
|
|
if(server_sock != -1)
|
|
|
|
close(server_sock);
|
2018-10-05 19:01:39 +02:00
|
|
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
2015-10-22 23:13:53 +02:00
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// load the config first
|
|
|
|
if(argc==1)
|
|
|
|
load_config(CONFIG);
|
|
|
|
else
|
|
|
|
load_config(argv[1]);
|
2018-10-05 19:01:39 +02:00
|
|
|
unsigned port = config()->port;
|
2015-10-22 11:39:11 +02:00
|
|
|
int client_sock = -1;
|
|
|
|
struct sockaddr_in client_name;
|
|
|
|
socklen_t client_name_len = sizeof(client_name);
|
2018-03-14 10:51:46 +01:00
|
|
|
char* client_ip = NULL;
|
2015-10-22 11:39:11 +02:00
|
|
|
// ignore the broken PIPE error when writing
|
|
|
|
//or reading to/from a closed socked connection
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2015-10-22 13:31:35 +02:00
|
|
|
signal(SIGABRT, SIG_IGN);
|
2015-10-22 23:13:53 +02:00
|
|
|
signal(SIGINT, stop_serve);
|
2018-02-10 11:22:41 +01:00
|
|
|
|
|
|
|
#ifdef USE_OPENSSL
|
2018-10-05 19:01:39 +02:00
|
|
|
if( config()->usessl == 1 )
|
2018-02-10 11:22:41 +01:00
|
|
|
{
|
|
|
|
init_openssl();
|
|
|
|
ctx = create_context();
|
|
|
|
|
|
|
|
configure_context(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2015-10-22 11:39:11 +02:00
|
|
|
server_sock = startup(&port);
|
|
|
|
LOG("httpd running on port %d\n", port);
|
2018-10-03 23:42:42 +02:00
|
|
|
// default to 4 workers
|
2018-10-05 19:01:39 +02:00
|
|
|
antd_scheduler_init(&scheduler, config()->n_workers);
|
2018-10-10 12:42:47 +02:00
|
|
|
// use blocking server_sock
|
|
|
|
// make the scheduler wait for event on another thread
|
|
|
|
// this allow to ged rid of high cpu usage on
|
|
|
|
// endless loop without doing anything
|
|
|
|
// set_nonblock(server_sock);
|
|
|
|
pthread_t scheduler_th;
|
|
|
|
if (pthread_create(&scheduler_th, NULL,(void *(*)(void *))antd_wait, (void*)&scheduler) != 0)
|
|
|
|
{
|
|
|
|
perror("pthread_create: cannot create worker\n");
|
|
|
|
stop_serve(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reclaim data when exit
|
|
|
|
pthread_detach(scheduler_th);
|
|
|
|
}
|
2018-10-14 20:41:44 +02:00
|
|
|
antd_task_t* task = NULL;
|
2018-10-03 23:42:42 +02:00
|
|
|
while (scheduler.status)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
|
|
|
if (client_sock == -1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-03 23:42:42 +02:00
|
|
|
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
|
2018-10-05 19:01:39 +02:00
|
|
|
antd_request_t* request = (antd_request_t*)malloc(sizeof(*request));
|
|
|
|
request->client = client;
|
|
|
|
request->request = dict();
|
2018-03-14 10:51:46 +01:00
|
|
|
/*
|
|
|
|
get the remote IP
|
|
|
|
*/
|
2018-09-12 11:06:19 +02:00
|
|
|
client->ip = NULL;
|
2018-03-14 10:51:46 +01:00
|
|
|
if (client_name.sin_family == AF_INET)
|
|
|
|
{
|
|
|
|
client_ip = inet_ntoa(client_name.sin_addr);
|
2018-09-12 11:06:19 +02:00
|
|
|
client->ip = strdup(client_ip);
|
2018-03-14 10:51:46 +01:00
|
|
|
LOG("Client IP: %s\n", client_ip);
|
2018-10-08 19:32:23 +02:00
|
|
|
//LOG("socket: %d\n", client_sock);
|
2018-03-14 10:51:46 +01:00
|
|
|
}
|
2018-05-03 15:10:44 +02:00
|
|
|
|
|
|
|
// set timeout to socket
|
2018-10-05 19:01:39 +02:00
|
|
|
set_nonblock(client_sock);
|
|
|
|
/*struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 5000;
|
2018-05-03 15:10:44 +02:00
|
|
|
|
|
|
|
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");
|
2018-10-05 19:01:39 +02:00
|
|
|
*/
|
2018-03-08 12:38:53 +01:00
|
|
|
client->sock = client_sock;
|
2018-02-10 11:22:41 +01:00
|
|
|
#ifdef USE_OPENSSL
|
2018-02-10 12:24:01 +01:00
|
|
|
client->ssl = NULL;
|
2018-10-07 01:03:05 +02:00
|
|
|
client->status = 0;
|
2018-10-14 10:57:09 +02:00
|
|
|
client->attempt = 0;
|
2018-10-05 19:01:39 +02:00
|
|
|
if(config()->usessl == 1)
|
2018-02-10 11:22:41 +01:00
|
|
|
{
|
2018-02-10 12:24:01 +01:00
|
|
|
client->ssl = (void*)SSL_new(ctx);
|
2018-03-14 16:47:39 +01:00
|
|
|
if(!client->ssl) continue;
|
2018-10-07 01:03:05 +02:00
|
|
|
SSL_set_fd((SSL*)client->ssl, client->sock);
|
2018-02-10 11:22:41 +01:00
|
|
|
|
2018-10-07 01:03:05 +02:00
|
|
|
/*if (SSL_accept((SSL*)client->ssl) <= 0) {
|
|
|
|
LOG("EROOR accept\n");
|
2018-02-10 11:22:41 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2018-03-08 11:39:44 +01:00
|
|
|
antd_close(client);
|
2018-02-10 11:22:41 +01:00
|
|
|
continue;
|
2018-10-07 01:03:05 +02:00
|
|
|
}*/
|
2018-02-10 11:22:41 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-10-03 23:42:42 +02:00
|
|
|
// create callback for the server
|
2018-10-14 20:41:44 +02:00
|
|
|
task = antd_create_task(accept_request,(void*)request, finish_request );
|
|
|
|
task->type = LIGHT;
|
|
|
|
antd_add_task(&scheduler, task);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-03-02 19:04:00 +01:00
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
close(server_sock);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|