initial http parsing

This commit is contained in:
2024-11-11 00:50:39 +01:00
parent 7db494eaaf
commit e5e40c795c
17 changed files with 176 additions and 32 deletions

12
include/utils/colors.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef __COLORS_H__
#define __COLORS_H__
/* ANSI color escape sequences */
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define BOLD "\033[1m"
#define RESET "\033[0m"
#endif

90
include/utils/hashmap.h Normal file
View File

@@ -0,0 +1,90 @@
#ifndef __HASHMAP_C__
#define __HASHMAP_C__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
/* Each process on Linux can have a maximum of 1024 open file descriptors */
#define HASHMAP_MAX_CLIENTS 1024
/**
* @brief Hash map struct
*
*/
typedef struct bucket {
int sockfd; /**< Client socket descriptor */
struct sockaddr_storage sas; /**< Associated socket address */
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
*
* @param sockfd[in] The file descriptor
* @return Returns the hash code
*/
int hash(int sockfd);
/**
* @brief Initializes the hash map
*
* @param bucket[out] The hash map uninitialized
*/
void hm_init(bucket_t *bucket);
/**
* @brief Inserts a key in the hash map
*
* @param bucket[out] The hash map
* @param sockfd[in] The file descriptor (value)
* @param sas[in] The sockaddr (value)
*/
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
*
* @param bucket[in] The hash map
* @param sockfd[in] The file descriptor (key)
* @return Returns NULL or the key pointer
*/
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
/**
* @brief Cleans the hash map
*
* @param bucket[out] The hash map
*/
void hm_free(bucket_t *bucket);
/**
* @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

29
include/utils/utils.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef __UTILS_H__
#define __UTILS_H__
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
/**
* @brief Prints each IP address associated with a host
*
* @param hostname[in] Hostname
* @param port[in] Port
*/
void print_ips(const char *hostname, const char *port);
/**
* @brief Retrieves the client ip from the sockaddr_storage and put it in the ip str
*
* @param sa[in] The sockaddr_storage of the client
* @param ip[out] The IP of the client
*/
void get_client_ip(struct sockaddr_storage *sa, char *ip);
#endif