fix hashmap

This commit is contained in:
2025-08-02 16:01:27 +02:00
parent f0abac8be7
commit fd8d7b1f17
6 changed files with 85 additions and 44 deletions

View File

@@ -7,6 +7,9 @@
#include "myclib/string/mystring.h"
#include "utils/config.h"
#define CWS_HTTP_HEADER_MAX 64
#define CWS_HTTP_HEADER_CONTENT_MAX 512
typedef enum cws_http_method_t {
CWS_HTTP_GET, /**< GET method */
CWS_HTTP_POST, /**< POST method */

View File

@@ -48,19 +48,20 @@ static mcl_bucket *mcl_find_bucket(mcl_hashmap *hashmap, void *key, mcl_bucket *
return NULL;
}
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn) {
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size,
size_t value_size) {
mcl_hashmap *hashmap = malloc(sizeof(mcl_hashmap));
if (hashmap == NULL) {
return NULL;
}
/* Initialize hash map with given parameters */
hashmap->hash_fn = hash_fn;
hashmap->equal_fn = equal_fn;
hashmap->free_key_fn = free_key_fn;
hashmap->free_value_fn = free_value_fn;
hashmap->key_size = key_size;
hashmap->value_size = value_size;
/* Clear all buckets in the map */
memset(hashmap->map, 0, sizeof(hashmap->map));
return hashmap;
@@ -95,11 +96,10 @@ void mcl_hm_free(mcl_hashmap *hashmap) {
}
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (hashmap == NULL || key == NULL) {
if (hashmap == NULL || key == NULL || value == NULL) {
return false;
}
/* Try to find existing bucket */
mcl_bucket *prev;
mcl_bucket *existing = mcl_find_bucket(hashmap, key, &prev);
@@ -108,7 +108,12 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (hashmap->free_value_fn != NULL && existing->value != NULL) {
hashmap->free_value_fn(existing->value);
}
existing->value = value;
existing->value = malloc(hashmap->value_size);
if (existing->value == NULL) {
return false;
}
memcpy(existing->value, value, hashmap->value_size);
return true;
}
@@ -116,10 +121,22 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
size_t index = mcl_get_bucket_index(hashmap, key);
mcl_bucket *bucket = &hashmap->map[index];
/* If first bucket is empty, use it */
if (bucket->key == NULL) {
bucket->key = key;
bucket->value = value;
/* First bucket is empty, use it */
bucket->key = malloc(hashmap->key_size);
if (bucket->key == NULL) {
return false;
}
bucket->value = malloc(hashmap->value_size);
if (bucket->value == NULL) {
free(bucket->key);
bucket->key = NULL;
return false;
}
memcpy(bucket->key, key, hashmap->key_size);
memcpy(bucket->value, value, hashmap->value_size);
bucket->next = NULL;
return true;
}
@@ -130,8 +147,21 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
return false;
}
new_bucket->key = key;
new_bucket->value = value;
new_bucket->key = malloc(hashmap->key_size);
if (new_bucket->key == NULL) {
free(new_bucket);
return false;
}
new_bucket->value = malloc(hashmap->value_size);
if (new_bucket->value == NULL) {
free(new_bucket->key);
free(new_bucket);
return false;
}
memcpy(new_bucket->key, key, hashmap->key_size);
memcpy(new_bucket->value, value, hashmap->value_size);
new_bucket->next = bucket->next;
bucket->next = new_bucket;

View File

@@ -60,6 +60,8 @@ typedef struct mcl_hashmap_t {
mcl_equal_fn *equal_fn; /**< Equality comparison function */
mcl_free_key_fn *free_key_fn; /**< Key deallocation function (optional) */
mcl_free_value_fn *free_value_fn; /**< Value deallocation function (optional) */
size_t key_size; /**< Size in bytes of the key */
size_t value_size; /**< Size in bytes of the value */
mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
} mcl_hashmap;
@@ -68,14 +70,18 @@ typedef struct mcl_hashmap_t {
*
* Creates a new hash map and initializes it with the provided function pointers.
* The free functions can be NULL if no automatic memory management is needed.
* Keys and values will be copied into the hashmap using memcpy with the specified sizes.
*
* @param[in] hash_fn Function used to hash keys (required)
* @param[in] equal_fn Function used to compare keys (required)
* @param[in] free_key_fn Function used to free keys (optional, can be NULL)
* @param[in] free_value_fn Function used to free values (optional, can be NULL)
* @param[in] key_size Size in bytes of each key to be stored
* @param[in] value_size Size in bytes of each value to be stored
* @return A pointer to the newly initialized hash map, or NULL on failure
*/
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn);
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size,
size_t value_size);
/**
* @brief Free all resources used by the hash map
@@ -92,11 +98,12 @@ void mcl_hm_free(mcl_hashmap *hashmap);
*
* If the key already exists, the old value is freed (if free_value_fn is provided)
* and replaced with the new value. If the key doesn't exist, a new entry is created.
* Both key and value are copied into the hashmap using memcpy.
*
* @param[in] hashmap Pointer to the hash map
* @param[in] key Pointer to the key to insert (must not be NULL)
* @param[in] value Pointer to the value to insert (can be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key or memory allocation failure)
* @param[in] key Pointer to the key to insert (will be copied, must not be NULL)
* @param[in] value Pointer to the value to insert (will be copied, must not be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure)
*/
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value);

View File

@@ -24,6 +24,7 @@ extern volatile sig_atomic_t cws_server_run;
typedef enum cws_server_ret_t {
CWS_SERVER_OK,
CWS_SERVER_CONFIG,
CWS_SERVER_FD_ERROR,
CWS_SERVER_CLIENT_NOT_FOUND,
CWS_SERVER_CLIENT_DISCONNECTED,
@@ -38,6 +39,7 @@ typedef enum cws_server_ret_t {
CWS_SERVER_EPOLL_DEL_ERROR,
CWS_SERVER_FD_NONBLOCKING_ERROR,
CWS_SERVER_ACCEPT_CLIENT_ERROR,
CWS_SERVER_HASHMAP_INIT,
} cws_server_ret;
/**
@@ -61,7 +63,7 @@ cws_server_ret cws_server_start(cws_config *config);
*
* @param[in,out] sockfd Socket of the commincation endpoint
*/
cws_server_ret cws_server_loop(int sockfd, cws_config *config);
int cws_server_loop(int sockfd, cws_config *config);
/**
* @brief Adds a file descriptor to the interest list

View File

@@ -108,7 +108,8 @@ cws_http *cws_http_parse(char *request_str, int sockfd, cws_config *config) {
mcl_string_append(request->http_version, pch);
/* Parse headers until a \r\n */
request->headers = mcl_hm_init(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn);
request->headers = mcl_hm_init(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * CWS_HTTP_HEADER_MAX,
sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
char *header_colon;
while (pch) {
/* Get header line */

View File

@@ -35,6 +35,10 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host
}
cws_server_ret cws_server_start(cws_config *config) {
if (!config || !config->hostname || !config->port) {
return CWS_SERVER_CONFIG;
}
struct addrinfo hints;
struct addrinfo *res;
@@ -80,17 +84,17 @@ cws_server_ret cws_server_start(cws_config *config) {
return CWS_SERVER_OK;
}
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);
int 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));
if (!clients) {
return -1;
return CWS_SERVER_HASHMAP_INIT;
}
int ret = 0;
cws_server_ret ret = 0;
int epfd = epoll_create1(0);
ret = cws_fd_set_nonblocking(sockfd);
if (ret < 0) {
if (ret != CWS_SERVER_OK) {
mcl_hm_free(clients);
close(epfd);
@@ -98,7 +102,7 @@ cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
}
ret = cws_epoll_add(epfd, sockfd, EPOLLIN | EPOLLET);
if (ret < 0) {
if (ret != CWS_SERVER_OK) {
mcl_hm_free(clients);
close(epfd);
@@ -120,12 +124,12 @@ cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
if (revents[i].data.fd == sockfd) {
ret = cws_server_handle_new_client(sockfd, epfd, clients);
if (ret != CWS_SERVER_OK) {
CWS_LOG_DEBUG("%d", ret);
CWS_LOG_DEBUG("Handle new client: %d", ret);
}
} else {
ret = cws_server_handle_client_data(revents[i].data.fd, epfd, clients, config);
if (ret != CWS_SERVER_OK) {
CWS_LOG_DEBUG("%d", ret);
CWS_LOG_DEBUG("Handle client data: %d", ret);
}
}
}
@@ -154,13 +158,7 @@ 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);
int *key = malloc(sizeof(int));
*key = client_fd;
struct sockaddr_storage *value = malloc(sizeof(struct sockaddr_storage));
*value = their_sa;
mcl_hm_set(clients, key, value);
mcl_hm_set(clients, &client_fd, &their_sa);
return CWS_SERVER_OK;
}
@@ -168,15 +166,19 @@ cws_server_ret cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *c
cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients, cws_config *config) {
char data[4096] = {0};
char ip[INET_ADDRSTRLEN] = {0};
mcl_string *data_str = mcl_string_new("", 4096);
/* Incoming data */
const ssize_t bytes_read = recv(client_fd, data, sizeof(data), 0);
ssize_t total_bytes = 0;
ssize_t bytes_read;
while ((bytes_read = recv(client_fd, data, sizeof(data), 0)) > 0) {
total_bytes += bytes_read;
mcl_string_append(data_str, data);
}
/* Retrieve client ip */
int *client_fd_key = malloc(sizeof(int));
*client_fd_key = client_fd;
mcl_bucket *client = mcl_hm_get(clients, client_fd_key);
free(client_fd_key);
int client_fd_key = client_fd;
mcl_bucket *client = mcl_hm_get(clients, &client_fd_key);
if (!client) {
CWS_LOG_ERROR("Client fd %d not found in hashmap", client_fd);
@@ -189,7 +191,7 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
struct sockaddr_storage client_sas = *(struct sockaddr_storage *)client->value;
cws_utils_get_client_ip(&client_sas, ip);
if (bytes_read == 0) {
if (total_bytes == 0) {
/* Client disconnected */
CWS_LOG_INFO("Client (%s) disconnected", ip);
cws_server_close_client(epfd, client_fd, clients);
@@ -197,7 +199,7 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
return CWS_SERVER_CLIENT_DISCONNECTED;
}
if (bytes_read < 0) {
if (total_bytes < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
/* Error during read, handle it (close client) */
CWS_LOG_INFO("Client (%s) disconnected (error)", ip);
@@ -277,9 +279,5 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *hashmap) {
cws_epoll_del(epfd, client_fd);
int *key_to_find = malloc(sizeof(int));
*key_to_find = client_fd;
mcl_hm_remove(hashmap, key_to_find);
free(key_to_find);
mcl_hm_remove(hashmap, &client_fd);
}