Add client_ctx_t in client_handler
This commit is contained in:
6
Makefile
6
Makefile
@ -40,8 +40,8 @@ bin/%.o: src/%.c
|
|||||||
bin/lib/%.o: src/lib/%.c
|
bin/lib/%.o: src/lib/%.c
|
||||||
$(CC) -c -o $@ $(CFLAGS) $<
|
$(CC) -c -o $@ $(CFLAGS) $<
|
||||||
|
|
||||||
bin/sesimos: bin/server.o bin/client.o bin/logger.o \
|
bin/sesimos: bin/server.o bin/client.o bin/logger.o bin/cache_handler.o \
|
||||||
bin/lib/cache.o bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
|
||||||
bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \
|
bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \
|
||||||
bin/lib/utils.o bin/lib/websocket.o
|
bin/lib/utils.o bin/lib/websocket.o
|
||||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||||
@ -56,7 +56,7 @@ bin/client.o: src/client.h src/defs.h src/server.h src/lib/utils.h src/lib/confi
|
|||||||
|
|
||||||
bin/logger.o: src/logger.h
|
bin/logger.o: src/logger.h
|
||||||
|
|
||||||
bin/lib/cache.o: src/cache_handler.h src/lib/utils.h src/lib/uri.h src/lib/compress.h src/logger.h
|
bin/cache_handler.o: src/cache_handler.h src/lib/utils.h src/lib/uri.h src/lib/compress.h src/logger.h
|
||||||
|
|
||||||
bin/lib/compress.o: src/lib/compress.h
|
bin/lib/compress.o: src/lib/compress.h
|
||||||
|
|
||||||
|
36
src/client.c
36
src/client.c
@ -716,10 +716,11 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
|
int client_connection_handler(client_ctx_t *ctx, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
|
||||||
struct timespec begin, end;
|
struct timespec begin, end;
|
||||||
int ret;
|
int ret;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
sock *client = &ctx->socket;
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &begin);
|
clock_gettime(CLOCK_MONOTONIC, &begin);
|
||||||
|
|
||||||
@ -748,7 +749,6 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx->cc[0] = 0;
|
ctx->cc[0] = 0;
|
||||||
ctx->geoip[0] = 0;
|
|
||||||
geoip_lookup_country(&client->addr.sock, ctx->cc);
|
geoip_lookup_country(&client->addr.sock, ctx->cc);
|
||||||
|
|
||||||
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
|
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
|
||||||
@ -802,11 +802,9 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *client_handler(sock *client) {
|
void *client_handler(client_ctx_t *ctx) {
|
||||||
struct sockaddr_in6 *server_addr;
|
struct sockaddr_in6 *server_addr;
|
||||||
struct sockaddr_storage server_addr_storage;
|
struct sockaddr_storage server_addr_storage;
|
||||||
|
|
||||||
client_ctx_t ctx;
|
|
||||||
char log_client_prefix[256], log_conn_prefix[512];
|
char log_client_prefix[256], log_conn_prefix[512];
|
||||||
|
|
||||||
logger_set_name("client");
|
logger_set_name("client");
|
||||||
@ -814,33 +812,33 @@ void *client_handler(sock *client) {
|
|||||||
//signal(SIGINT, client_terminate);
|
//signal(SIGINT, client_terminate);
|
||||||
//signal(SIGTERM, client_terminate);
|
//signal(SIGTERM, client_terminate);
|
||||||
|
|
||||||
inet_ntop(client->addr.ipv6.sin6_family, &client->addr.ipv6.sin6_addr, ctx._c_addr, sizeof(ctx._c_addr));
|
inet_ntop(ctx->socket.addr.ipv6.sin6_family, &ctx->socket.addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr));
|
||||||
if (strncmp(ctx._c_addr, "::ffff:", 7) == 0) {
|
if (strncmp(ctx->_c_addr, "::ffff:", 7) == 0) {
|
||||||
ctx.addr = ctx._c_addr + 7;
|
ctx->addr = ctx->_c_addr + 7;
|
||||||
} else {
|
} else {
|
||||||
ctx.addr = ctx._c_addr;
|
ctx->addr = ctx->_c_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
socklen_t len = sizeof(server_addr_storage);
|
socklen_t len = sizeof(server_addr_storage);
|
||||||
getsockname(client->socket, (struct sockaddr *) &server_addr_storage, &len);
|
getsockname(ctx->socket.socket, (struct sockaddr *) &server_addr_storage, &len);
|
||||||
server_addr = (struct sockaddr_in6 *) &server_addr_storage;
|
server_addr = (struct sockaddr_in6 *) &server_addr_storage;
|
||||||
inet_ntop(server_addr->sin6_family, (void *) &server_addr->sin6_addr, ctx._s_addr, sizeof(ctx._s_addr));
|
inet_ntop(server_addr->sin6_family, (void *) &server_addr->sin6_addr, ctx->_s_addr, sizeof(ctx->_s_addr));
|
||||||
if (strncmp(ctx._s_addr, "::ffff:", 7) == 0) {
|
if (strncmp(ctx->_s_addr, "::ffff:", 7) == 0) {
|
||||||
ctx.s_addr = ctx._s_addr + 7;
|
ctx->s_addr = ctx->_s_addr + 7;
|
||||||
} else {
|
} else {
|
||||||
ctx.s_addr = ctx._s_addr;
|
ctx->s_addr = ctx->_s_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
|
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) ctx->socket.enc ? HTTPS_STR : HTTP_STR,
|
||||||
ntohs(server_addr->sin6_port), CLR_STR, color_table[0], INET6_ADDRSTRLEN, ctx.addr,
|
ntohs(server_addr->sin6_port), CLR_STR, color_table[0], INET6_ADDRSTRLEN, ctx->addr,
|
||||||
ntohs(client->addr.ipv6.sin6_port), CLR_STR);
|
ntohs(ctx->socket.addr.ipv6.sin6_port), CLR_STR);
|
||||||
|
|
||||||
sprintf(log_conn_prefix, "[%*s]%s", INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
|
sprintf(log_conn_prefix, "[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, log_client_prefix);
|
||||||
logger_set_prefix(log_conn_prefix);
|
logger_set_prefix(log_conn_prefix);
|
||||||
|
|
||||||
info("Started thread");
|
info("Started thread");
|
||||||
|
|
||||||
client_connection_handler(&ctx, client, 0, log_conn_prefix, log_client_prefix);
|
client_connection_handler(ctx, 0, log_conn_prefix, log_client_prefix);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -11,24 +11,23 @@
|
|||||||
|
|
||||||
#include "lib/config.h"
|
#include "lib/config.h"
|
||||||
#include "lib/sock.h"
|
#include "lib/sock.h"
|
||||||
#include "lib/geoip.h"
|
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
sock socket;
|
||||||
char *addr;
|
char *addr;
|
||||||
char *s_addr;
|
char *s_addr;
|
||||||
unsigned char s_keep_alive:1;
|
unsigned char s_keep_alive:1;
|
||||||
unsigned char c_keep_alive:1;
|
unsigned char c_keep_alive:1;
|
||||||
char cc[3];
|
char cc[3];
|
||||||
char host[256];
|
char host[256];
|
||||||
char geoip[GEOIP_MAX_JSON_SIZE + 1];
|
|
||||||
char _c_addr[INET6_ADDRSTRLEN + 1];
|
char _c_addr[INET6_ADDRSTRLEN + 1];
|
||||||
char _s_addr[INET6_ADDRSTRLEN + 1];
|
char _s_addr[INET6_ADDRSTRLEN + 1];
|
||||||
} client_ctx_t;
|
} client_ctx_t;
|
||||||
|
|
||||||
host_config_t *get_host_config(const char *host);
|
host_config_t *get_host_config(const char *host);
|
||||||
|
|
||||||
void *client_handler(sock *client);
|
void *client_handler(client_ctx_t *client);
|
||||||
|
|
||||||
#endif //SESIMOS_CLIENT_H
|
#endif //SESIMOS_CLIENT_H
|
||||||
|
@ -156,9 +156,9 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
|
|||||||
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : "");
|
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : "");
|
||||||
const char *content_type = http_get_header_field(&req->hdr, "Content-Type");
|
const char *content_type = http_get_header_field(&req->hdr, "Content-Type");
|
||||||
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : "");
|
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : "");
|
||||||
if (conn->ctx->geoip[0] != 0) {
|
//if (conn->ctx->geoip[0] != 0) {
|
||||||
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_INFO", conn->ctx->geoip);
|
// param_ptr = fastcgi_add_param(param_ptr, "REMOTE_INFO", conn->ctx->geoip);
|
||||||
}
|
//}
|
||||||
|
|
||||||
for (int i = 0; i < req->hdr.field_num; i++) {
|
for (int i = 0; i < req->hdr.field_num; i++) {
|
||||||
const http_field *f = &req->hdr.fields[i];
|
const http_field *f = &req->hdr.fields[i];
|
||||||
|
@ -13,10 +13,8 @@
|
|||||||
|
|
||||||
#include "cache_handler.h"
|
#include "cache_handler.h"
|
||||||
#include "lib/config.h"
|
#include "lib/config.h"
|
||||||
#include "lib/sock.h"
|
|
||||||
#include "lib/proxy.h"
|
#include "lib/proxy.h"
|
||||||
#include "lib/geoip.h"
|
#include "lib/geoip.h"
|
||||||
#include "lib/utils.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
@ -38,14 +36,14 @@ volatile sig_atomic_t alive = 1;
|
|||||||
const char *config_file;
|
const char *config_file;
|
||||||
|
|
||||||
static int sockets[NUM_SOCKETS];
|
static int sockets[NUM_SOCKETS];
|
||||||
static sock clients[MAX_CHILDREN];
|
|
||||||
static pthread_t children[MAX_CHILDREN];
|
static pthread_t children[MAX_CHILDREN];
|
||||||
static SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
|
static SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
|
||||||
|
|
||||||
|
static client_ctx_t clients[MAX_CLIENTS];
|
||||||
|
|
||||||
static int clean() {
|
static int clean() {
|
||||||
remove("/var/sesimos/server/cache");
|
remove("/var/sesimos/server/cache");
|
||||||
rmdir("/var/sesimos/server/");
|
rmdir("/var/sesimos/server/");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,7 +269,8 @@ int main(int argc, char *const argv[]) {
|
|||||||
for (j = 0; j < MAX_CHILDREN; j++) {
|
for (j = 0; j < MAX_CHILDREN; j++) {
|
||||||
if (children[j] == 0) break;
|
if (children[j] == 0) break;
|
||||||
}
|
}
|
||||||
sock *client = &clients[j];
|
client_ctx_t *client_ctx = &clients[j];
|
||||||
|
sock *client = &client_ctx->socket;
|
||||||
|
|
||||||
client->ctx = contexts[0];
|
client->ctx = contexts[0];
|
||||||
socklen_t addr_len = sizeof(client->addr);
|
socklen_t addr_len = sizeof(client->addr);
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define CLIENT_TIMEOUT 3600
|
#define CLIENT_TIMEOUT 3600
|
||||||
#define SERVER_TIMEOUT_INIT 4
|
#define SERVER_TIMEOUT_INIT 4
|
||||||
#define SERVER_TIMEOUT 3600
|
#define SERVER_TIMEOUT 3600
|
||||||
|
#define MAX_CLIENTS 4096
|
||||||
|
|
||||||
#define CNX_HANDLER_WORKERS 8
|
#define CNX_HANDLER_WORKERS 8
|
||||||
#define REQ_HANDLER_WORKERS 16
|
#define REQ_HANDLER_WORKERS 16
|
||||||
|
Reference in New Issue
Block a user