rename hashmap

This commit is contained in:
2024-11-10 20:26:01 +01:00
parent 3cc1613d93
commit 7db494eaaf
4 changed files with 134 additions and 53 deletions

View File

@@ -1,6 +1,7 @@
#ifndef __HASHMAP_C__ #ifndef __HASHMAP_C__
#define __HASHMAP_C__ #define __HASHMAP_C__
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
@@ -8,11 +9,12 @@
/* Each process on Linux can have a maximum of 1024 open file descriptors */ /* Each process on Linux can have a maximum of 1024 open file descriptors */
#define HASHMAP_MAX_CLIENTS 1024 #define HASHMAP_MAX_CLIENTS 1024
struct hashmap { typedef struct bucket {
int sockfd; int sockfd; /**< Client socket descriptor */
struct sockaddr_storage sas; struct sockaddr_storage sas; /**< Associated socket address */
struct hashmap *next; struct bucket *next; /**< Next node in case of collision */
}; struct bucket *prev; /**< Previous node in case of collision */
} bucket_t;
/** /**
* @brief Calculates the hash code of a given file descriptor * @brief Calculates the hash code of a given file descriptor
@@ -25,35 +27,60 @@ int hash(int sockfd);
/** /**
* @brief Initializes the hash map * @brief Initializes the hash map
* *
* @param map[out] The hash map uninitialized * @param bucket[out] The hash map uninitialized
*/ */
void hm_init(struct hashmap *map); void hm_init(bucket_t *bucket);
/** /**
* @brief Inserts a key in the hash map * @brief Inserts a key in the hash map
* *
* @param map[out] The hash map * @param bucket[out] The hash map
* @param sockfd[in] The file descriptor (value) * @param sockfd[in] The file descriptor (value)
* @param sas[in] The sockaddr (value) * @param sas[in] The sockaddr (value)
*/ */
void hm_push(struct hashmap *map, int sockfd, struct sockaddr_storage *sas); void hm_push(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
/**
* @brief Removes a key from the hash map
*
* @param bucket[out] The hash map
* @param sockfd[in] The key
*/
void hm_remove(bucket_t *bucket, int sockfd);
/** /**
* @brief Searches for a key in the hash map * @brief Searches for a key in the hash map
* *
* @param map[in] The hash map * @param bucket[in] The hash map
* @param sockfd[in] The file descriptor (key) * @param sockfd[in] The file descriptor (key)
* @return struct hashmap* Returns NULL or the key pointer * @return struct hashmap* Returns NULL or the key pointer
*/ */
struct hashmap *hm_lookup(struct hashmap *map, int sockfd); bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
/** /**
* @brief Cleans the hash map * @brief Cleans the hash map
* *
* @param map[out] The hash map * @param bucket[out] The hash map
*/ */
void hm_free(struct hashmap *map); void hm_free(bucket_t *bucket);
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas); /**
* @brief Checks if a file descriptor is in the bucket array (not linked list)
*
* @param bucket[in]
* @param sockfd[in]
* @return true If the file descriptor is in the bucket array
* @return false If the file descriptor is not in the bucket array (check with hm_lookup())
*/
bool hm_is_in_bucket_array(bucket_t *bucket, int sockfd);
/**
* @brief This function will add a key even if it exists (use hm_push() instead)
*
* @param bucket
* @param sockfd
* @param sas
*/
void hm_insert(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
#endif #endif

View File

@@ -14,6 +14,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "hashmap.h"
/* On which port the server will run */ /* On which port the server will run */
#define PORT 3030 #define PORT 3030
@@ -86,4 +88,11 @@ void setnonblocking(int sockfd);
*/ */
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size); int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
/**
* @brief Closes all the file descriptors opened
*
* @param map[in] The hash map
*/
void close_fds(bucket_t *bucket);
#endif #endif

View File

@@ -2,64 +2,84 @@
int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; } int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; }
void hm_init(struct hashmap *map) { void hm_init(bucket_t *bucket) {
/* Initialize everything to 0 for the struct, then -1 for fd and next to NULL */ /* Initialize everything to 0 for the struct, then -1 for fd and next to NULL */
memset(map, 0, sizeof(struct hashmap) * HASHMAP_MAX_CLIENTS); memset(bucket, 0, sizeof(bucket_t) * HASHMAP_MAX_CLIENTS);
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) { for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
map[i].sockfd = -1; bucket[i].sockfd = -1;
map[i].next = NULL; bucket[i].next = NULL;
bucket[i].prev = NULL;
} }
} }
/* This function will add a key even if it exists (use hm_push() instead) */ void hm_insert(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas) {
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas) {
int index = hash(sockfd); int index = hash(sockfd);
if (map[index].sockfd == -1) { if (bucket[index].sockfd == -1) {
/* Current slot is empty */ /* Current slot is empty */
map[index].sockfd = sockfd; bucket[index].sockfd = sockfd;
map[index].sas = *sas; bucket[index].sas = *sas;
map[index].next = NULL;
} else { } else {
/* Append the new key to the head (not the first element because it belongs to the array) of the linked /* Append the new key to the head (not the first element because it belongs to the array) of the linked
* list */ * list */
struct hashmap *p = &map[index]; bucket_t *p = &bucket[index];
struct hashmap *new = malloc(sizeof(struct hashmap)); bucket_t *new = malloc(sizeof(bucket_t));
new->sockfd = sockfd; new->sockfd = sockfd;
new->sas = *sas; new->sas = *sas;
new->next = p->next; new->next = p->next;
new->prev = p;
p->next = new; p->next = new;
} }
} }
struct hashmap *hm_lookup(struct hashmap *map, int sockfd) { bucket_t *hm_lookup(bucket_t *bucket, int sockfd) {
int index = hash(sockfd); int index = hash(sockfd);
if (map[index].sockfd != sockfd) { if (bucket[index].sockfd != sockfd) {
struct hashmap *p; bucket_t *p;
for (p = map[index].next; p != NULL && p->sockfd != sockfd; p = p->next); for (p = bucket[index].next; p != NULL && p->sockfd != sockfd; p = p->next);
return p; return p;
} else { } else {
return &map[index]; return &bucket[index];
} }
return NULL; return NULL;
} }
void hm_push(struct hashmap *map, int sockfd, struct sockaddr_storage *sas) { void hm_push(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas) {
if (hm_lookup(map, sockfd) == NULL) { if (hm_lookup(bucket, sockfd) == NULL) {
hm_insert(map, sockfd, sas); hm_insert(bucket, sockfd, sas);
} }
} }
void hm_free(struct hashmap *map) { void hm_remove(bucket_t *bucket, int sockfd) {
struct hashmap *p, *next; if (hm_is_in_bucket_array(bucket, sockfd)) {
/* Instead of doing this I could copy the memory of the next node into the head */
int index = hash(sockfd);
bucket[index].sockfd = -1;
return;
}
/* Key not in the bucket array, let's search in the linked list */
bucket_t *p = hm_lookup(bucket, sockfd);
if (p == NULL) return;
p->prev->next = p->next;
if (p->next != NULL) {
/* If there's a next node update the previous node */
p->next->prev = p->prev;
}
free(p);
}
void hm_free(bucket_t *bucket) {
bucket_t *p, *next;
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) { for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
if (map[i].next != NULL) { if (bucket[i].next != NULL) {
/* Free the malloc */ /* Free the malloc */
p = map[i].next; p = bucket[i].next;
next = p->next; next = p->next;
do { do {
free(p); free(p);
@@ -69,3 +89,10 @@ void hm_free(struct hashmap *map) {
} }
} }
} }
bool hm_is_in_bucket_array(bucket_t *bucket, int sockfd) {
int index = hash(sockfd);
if (bucket[index].sockfd == sockfd) return true;
return false;
}

View File

@@ -41,10 +41,13 @@ int start_server(const char *hostname, const char *service) {
void setup_hints(struct addrinfo *hints, size_t len, const char *hostname) { void setup_hints(struct addrinfo *hints, size_t len, const char *hostname) {
memset(hints, 0, len); memset(hints, 0, len);
hints->ai_family = AF_UNSPEC; // IPv4 or IPv6 /* IPv4 or IPv6 */
hints->ai_socktype = SOCK_STREAM; // TCP hints->ai_family = AF_UNSPEC;
/* TCP */
hints->ai_socktype = SOCK_STREAM;
if (hostname == NULL) { if (hostname == NULL) {
hints->ai_flags = AI_PASSIVE; // fill in IP for me /* Fill in IP for me */
hints->ai_flags = AI_PASSIVE;
} }
} }
@@ -52,7 +55,7 @@ void handle_clients(int sockfd) {
struct sockaddr_storage their_sa; struct sockaddr_storage their_sa;
socklen_t theirsa_size = sizeof their_sa; socklen_t theirsa_size = sizeof their_sa;
struct hashmap clients[HASHMAP_MAX_CLIENTS]; bucket_t clients[HASHMAP_MAX_CLIENTS];
hm_init(clients); hm_init(clients);
int epfd = epoll_create1(0); int epfd = epoll_create1(0);
@@ -73,7 +76,7 @@ void handle_clients(int sockfd) {
for (int i = 0; i < nfds; ++i) { for (int i = 0; i < nfds; ++i) {
if (revents[i].data.fd == sockfd) { if (revents[i].data.fd == sockfd) {
// new client /* New client */
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size); client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
setnonblocking(client_fd); setnonblocking(client_fd);
@@ -83,14 +86,14 @@ void handle_clients(int sockfd) {
int bytes_sent = send(client_fd, msg, msg_len, 0); int bytes_sent = send(client_fd, msg, msg_len, 0);
fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent); fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent);
} else { } else {
// incoming data /* Incoming data */
client_fd = revents[i].data.fd; client_fd = revents[i].data.fd;
int bytes_read = recv(client_fd, data, sizeof data, 0); int bytes_read = recv(client_fd, data, sizeof data, 0);
if (bytes_read == 0) { if (bytes_read == 0) {
// client disconnected /* Client disconnected */
char ip[INET_ADDRSTRLEN]; char ip[INET_ADDRSTRLEN];
struct hashmap *client = hm_lookup(clients, client_fd); bucket_t *client = hm_lookup(clients, client_fd);
get_client_ip(&client->sas, ip); get_client_ip(&client->sas, ip);
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip); fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
@@ -112,13 +115,7 @@ void handle_clients(int sockfd) {
free(revents); free(revents);
close(epfd); close(epfd);
close_fds(clients);
/* Close all the file descriptors */
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
close(clients[i].sockfd);
/* TODO: close collisions */
}
hm_free(clients); hm_free(clients);
} }
@@ -127,6 +124,7 @@ void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
event.events = events; event.events = events;
event.data.fd = sockfd; event.data.fd = sockfd;
int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event); int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
if (status != 0) { if (status != 0) {
fprintf(stderr, RED BOLD "[server] epoll_ctl_add(): %s\n" RESET, strerror(errno)); fprintf(stderr, RED BOLD "[server] epoll_ctl_add(): %s\n" RESET, strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -135,6 +133,7 @@ void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
void epoll_ctl_del(int epfd, int sockfd) { void epoll_ctl_del(int epfd, int sockfd) {
int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL); int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
if (status != 0) { if (status != 0) {
fprintf(stdout, RED BOLD "[server] epoll_ctl_del(): %s\n" RESET, strerror(errno)); fprintf(stdout, RED BOLD "[server] epoll_ctl_del(): %s\n" RESET, strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -143,6 +142,7 @@ void epoll_ctl_del(int epfd, int sockfd) {
void setnonblocking(int sockfd) { void setnonblocking(int sockfd) {
int status = fcntl(sockfd, F_SETFL, O_NONBLOCK); int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
if (status == -1) { if (status == -1) {
fprintf(stderr, RED BOLD "[server] fcntl(): %s\n" RESET, gai_strerror(status)); fprintf(stderr, RED BOLD "[server] fcntl(): %s\n" RESET, gai_strerror(status));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -161,3 +161,21 @@ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *
return client_fd; return client_fd;
} }
void close_fds(bucket_t *bucket) {
bucket_t *p, *next;
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
close(bucket[i].sockfd);
if (bucket[i].next != NULL) {
/* Close the fds */
p = bucket[i].next;
next = p->next;
do {
close(p->sockfd);
p = next;
next = p != NULL ? p->next : NULL;
} while (p != NULL);
}
}
}