diff --git a/include/server/server.h b/include/server/server.h index 9e36cd6..828827b 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "myclib/hashmap/myhashmap.h" #include "utils/config.h" @@ -14,7 +15,7 @@ /* Size of the epoll_event array */ #define CWS_SERVER_EPOLL_MAXEVENTS 64 -#define CWS_SERVER_EPOLL_TIMEOUT 1000 +#define CWS_SERVER_EPOLL_TIMEOUT 3000 #define CWS_SERVER_MAX_REQUEST_SIZE (16 * 1024) /* 16KB */ @@ -43,6 +44,11 @@ typedef enum cws_server_ret_t { CWS_SERVER_REQUEST_TOO_LARGE, } cws_server_ret; +typedef struct cws_client_t { + struct sockaddr_storage addr; + time_t last_activity; +} cws_client; + /** * @brief Setups hints object * diff --git a/src/http/http.c b/src/http/http.c index 4967cfc..1f0d9fb 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -64,7 +64,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config return NULL; } - CWS_LOG_DEBUG("method: %s", pch); + //CWS_LOG_DEBUG("method: %s", pch); int ret = cws_http_parse_method(request, pch); if (ret < 0) { @@ -85,7 +85,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config return NULL; } - CWS_LOG_DEBUG("location: %s", pch); + //CWS_LOG_DEBUG("location: %s", pch); mcl_string_append(request->location, pch); mcl_string_append(request->location_path, config->www); @@ -95,7 +95,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config } else { mcl_string_append(request->location_path, mcl_string_cstr(request->location)); } - CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path)); + //CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path)); /* Parse HTTP version */ pch = strtok_r(NULL, " \r\n", &saveptr); @@ -105,7 +105,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config return NULL; } - CWS_LOG_DEBUG("version: %s", pch); + //CWS_LOG_DEBUG("version: %s", pch); mcl_string_append(request->http_version, pch); /* Parse headers until a \r\n */ diff --git a/src/server/server.c b/src/server/server.c index 32d77c4..e707bdf 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -83,7 +83,7 @@ cws_server_ret cws_server_start(cws_config *config) { } cws_server_ret cws_server_loop(int sockfd, cws_config *config) { - mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn, sizeof(int), sizeof(struct sockaddr_storage)); + mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn, sizeof(int), sizeof(cws_client)); if (!clients) { return CWS_SERVER_HASHMAP_INIT; } @@ -116,9 +116,12 @@ cws_server_ret cws_server_loop(int sockfd, cws_config *config) { } while (cws_server_run) { + CWS_LOG_DEBUG("Waiting for epoll events..."); int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT); + CWS_LOG_DEBUG("epoll_wait returned %d events", nfds); if (nfds == 0) { + /* TODO: Check for inactive clients */ continue; } @@ -160,7 +163,12 @@ cws_server_ret cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *c cws_fd_set_nonblocking(client_fd); cws_epoll_add(epfd, client_fd, EPOLLIN); - mcl_hm_set(clients, &client_fd, &their_sa); + + cws_client client = { + .addr = their_sa, + .last_activity = time(NULL), + }; + mcl_hm_set(clients, &client_fd, &client); return CWS_SERVER_OK; } @@ -205,8 +213,9 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma return CWS_SERVER_CLIENT_NOT_FOUND; } - struct sockaddr_storage *client_sas = (struct sockaddr_storage *)client->value; - cws_utils_get_client_ip(client_sas, ip); + cws_client *client_info = (cws_client *)client->value; + cws_utils_get_client_ip(&client_info->addr, ip); + client_info->last_activity = time(NULL); if (bytes_read == 0) { /* Client disconnected */ diff --git a/src/utils/utils.c b/src/utils/utils.c index 163d369..543174e 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -8,6 +8,8 @@ #include "utils/colors.h" +static void cws_utils_convert_ip(int family, void *addr, char *ip, size_t ip_len) { inet_ntop(family, addr, ip, ip_len); } + void cws_utils_print_ips(const char *hostname, const char *port) { struct addrinfo ai; struct addrinfo *res; @@ -29,11 +31,11 @@ void cws_utils_print_ips(const char *hostname, const char *port) { for (struct addrinfo *p = res; p != NULL; p = p->ai_next) { if (p->ai_family == AF_INET) { struct sockaddr_in *sin = (struct sockaddr_in *)p->ai_addr; - inet_ntop(AF_INET, &sin->sin_addr, ipv4, INET_ADDRSTRLEN); + cws_utils_convert_ip(AF_INET, &sin->sin_addr, ipv4, INET_ADDRSTRLEN); CWS_LOG_INFO("%s", ipv4); } else if (p->ai_family == AF_INET6) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)p->ai_addr; - inet_ntop(AF_INET6, &sin6->sin6_addr, ipv6, INET6_ADDRSTRLEN); + cws_utils_convert_ip(AF_INET6, &sin6->sin6_addr, ipv6, INET6_ADDRSTRLEN); CWS_LOG_INFO("%s", ipv6); } } @@ -42,9 +44,13 @@ void cws_utils_print_ips(const char *hostname, const char *port) { } void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) { - struct sockaddr_in *sin = (struct sockaddr_in *)sa; - - inet_ntop(AF_INET, &sin->sin_addr, ip, INET_ADDRSTRLEN); + if (sa->ss_family == AF_INET) { + struct sockaddr_in *sin = (struct sockaddr_in *)sa; + cws_utils_convert_ip(AF_INET, &sin->sin_addr, ip, INET_ADDRSTRLEN); + } else if (sa->ss_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; + cws_utils_convert_ip(AF_INET6, &sin6->sin6_addr, ip, INET6_ADDRSTRLEN); + } } char *cws_strip(char *str) {