adjust naming convention and initial config support

This commit is contained in:
2025-04-24 17:24:13 +02:00
parent b4f71b0c53
commit dd9e2f557b
19 changed files with 217 additions and 263 deletions

View File

@@ -1,17 +0,0 @@
#ifndef CWS_CLIENT_H
#define CWS_CLIENT_H
#include <arpa/inet.h>
#include <errno.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>
#include <unistd.h>
int test_client_connection(const char *hostname, const char *service);
#endif

View File

@@ -1,33 +1,35 @@
#ifndef CWS_HTTP_H
#define CWS_HTTP_H
#define WWW "../www" /**< Directory used to get html files */
#define CWS_WWW "../www" /**< Directory used to get html files */
/** In the future I'll move conf stuff under a server struct, I can skip just because I want something that works */
#define LOCATION_LEN 512
#define LOCATION_PATH_LEN 1024
#define HTTP_VERSION_LEN 8
#define USER_AGENT_LEN 1024
#define HOST_LEN 1024
#define CWS_HTTP_LOCATION_LEN 512
#define CWS_HTTP_LOCATION_PATH_LEN 1024
#define CWS_HTTP_VERSION_LEN 8
#define CWS_HTTP_USER_AGENT_LEN 1024
#define CWS_HTTP_HOST_LEN 1024
enum http_method {
GET, /**< GET method */
POST, /**< POST method */
};
typedef enum cws_http_method_t {
CWS_HTTP_GET, /**< GET method */
CWS_HTTP_POST, /**< POST method */
CWS_HTTP_PUT,
CWS_HTTP_DELETE,
} cws_http_method;
/* In the future I'll add HEAD, PUT, DELETE */
/**
* @brief HTTP request struct
*
*/
typedef struct http {
int sockfd; /**< Socket file descriptor */
enum http_method method; /**< HTTP request method */
char location[LOCATION_LEN]; /**< Resource requested */
char location_path[LOCATION_PATH_LEN]; /**< Resource path */
char http_version[HTTP_VERSION_LEN]; /**< HTTP version */
char user_agent[USER_AGENT_LEN]; /**< User-Agent */
char host[HOST_LEN]; /**< Host */
} http_t;
typedef struct cws_http_t {
int sockfd; /**< Socket file descriptor */
cws_http_method method; /**< HTTP request method */
char location[CWS_HTTP_LOCATION_LEN]; /**< Resource requested */
char location_path[CWS_HTTP_LOCATION_PATH_LEN]; /**< Full resource path */
char http_version[CWS_HTTP_VERSION_LEN]; /**< HTTP version */
char user_agent[CWS_HTTP_USER_AGENT_LEN]; /**< User-Agent */
char host[CWS_HTTP_HOST_LEN]; /**< Host */
} cws_http;
/* Connection */
/* Accept-Encoding */
/* Accept-Language */
@@ -35,18 +37,18 @@ typedef struct http {
/**
* @brief Parses a HTTP request
*
* @param request_str[in] The http request sent to the server
* @param[in] request_str The http request sent to the server
* @return Returns a http_t pointer to the request
*/
http_t *http_parse(char *request_str, int sockfd);
cws_http *cws_http_parse(char *request_str, int sockfd);
void http_parse_method(http_t *request, const char *method);
void http_get_content_type(http_t *request, char *content_type);
void cws_http_parse_method(cws_http *request, const char *method);
void cws_http_get_content_type(cws_http *request, char *content_type);
void http_send_response(http_t *request);
void http_send_not_found(http_t *request);
void http_send_not_implemented(http_t *request);
void cws_http_send_response(cws_http *request);
void cws_http_send_not_found(cws_http *request);
void cws_http_send_not_implemented(cws_http *request);
void http_free(http_t *request);
void cws_http_free(cws_http *request);
#endif

View File

@@ -7,87 +7,87 @@
#include "utils/hashmap.h"
/* Clients max queue */
#define BACKLOG 10
#define CWS_SERVER_BACKLOG 10
/* Size of the epoll_event array */
#define EPOLL_MAXEVENTS 10
#define CWS_SERVER_EPOLL_MAXEVENTS 10
/* Wait forever (epoll_wait()) */
#define EPOLL_TIMEOUT -1
#define CWS_SERVER_EPOLL_TIMEOUT -1
/**
* @brief Runs the server
*
* @param hostname[in] The hostname of the server (default localhost, it could be NULL)
* @param service[in] The service (found in /etc/services) or the port where to run
* @param[in] hostname The hostname of the server (default localhost, it could be NULL)
* @param[in] service The service (found in /etc/services) or the port where to run
* @return 0 on success, -1 on error
*/
int start_server(const char *hostname, const char *service);
int cws_server_start(const char *hostname, const char *service);
/**
* @brief Setups hints object
*
* @param hints[out] The hints addrinfo
* @param len[in] The length of hints
* @param hostname[in] The hostname (could be NULL)
* @param[out] hints The hints addrinfo
* @param[in] len The length of hints
* @param[in] hostname The hostname (could be NULL)
*/
void setup_hints(struct addrinfo *hints, size_t len, const char *hostname);
void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *hostname);
/**
* @brief Main server loop
*
* @param sockfd[in,out] Socket of the commincation endpoint
* @param[in,out] sockfd Socket of the commincation endpoint
*/
void handle_clients(int sockfd);
void cws_server_loop(int sockfd);
/**
* @brief Adds a file descriptor to the interest list
*
* @param epfd[in] epoll file descriptor
* @param sockfd[in] The file descriptor to watch
* @param events[in] The events to follow
* @param[in] epfd epoll file descriptor
* @param[in] sockfd The file descriptor to watch
* @param[in] events The events to follow
*/
void epoll_ctl_add(int epfd, int sockfd, uint32_t events);
void cws_epoll_add(int epfd, int sockfd, uint32_t events);
/**
* @brief Removes a file descriptor from the interest list
*
* @param epfd[in] epoll file descriptor
* @param sockfd[in] The file descriptor to remove
* @param[in] epfd epoll file descriptor
* @param[in] sockfd The file descriptor to remove
*/
void epoll_ctl_del(int epfd, int sockfd);
void cws_epoll_del(int epfd, int sockfd);
/**
* @brief Makes a file descriptor non-blocking
*
* @param sockfd[in] The file descriptor to make non-blocking
* @param[in] sockfd The file descriptor to make non-blocking
*/
void setnonblocking(int sockfd);
void cws_fd_set_nonblocking(int sockfd);
/**
* @brief Handles the new client
*
* @param sockfd[in] Server's file descriptor
* @param their_sa[out] Populates the struct with client's information
* @param theirsa_size[in] Size of the struct
* @param[in] sockfd Server's file descriptor
* @param[out] their_sa Populates the struct with client's information
* @param[in] theirsa_size Size of the struct
* @return Returns -1 on error or the file descriptor on success
*/
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
/**
* @brief Closes all the file descriptors opened
*
* @param bucket[in] The hash map
* @param[in] bucket The hash map
*/
void close_fds(bucket_t *bucket);
void cws_server_close_all_fds(cws_bucket *bucket);
/**
* @brief Disconnect a client
*
* @param epfd[in] Epoll file descriptor
* @param client_fd[in] Client file descriptor
* @param bucket[in] Clients hash map
* @param[in] epfd Epoll file descriptor
* @param[in] client_fd Client file descriptor
* @param[in] bucket Clients hash map
*/
void close_client(int epfd, int client_fd, bucket_t *bucket);
void cws_server_close_client(int epfd, int client_fd, cws_bucket *bucket);
#endif

7
include/utils/config.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef CWS_CONFIG_H
#define CWS_CONFIG_H
typedef struct cws_config_t {
} cws_config;
#endif

View File

@@ -2,81 +2,79 @@
#define CWS_HASHMAP_H
#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
#define CWS_HASHMAP_MAX_CLIENTS 1024
/**
* @brief Hash map struct
*
*/
typedef struct bucket {
typedef struct cws_bucket_t {
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;
struct cws_bucket_t *next; /**< Next node in case of collision */
struct cws_bucket_t *prev; /**< Previous node in case of collision */
} cws_bucket;
/**
* @brief Calculates the hash code of a given file descriptor
*
* @param sockfd[in] The file descriptor
* @param[in] sockfd The file descriptor
* @return Returns the hash code
*/
int hash(int sockfd);
int cws_hm_hash(int sockfd);
/**
* @brief Initializes the hash map
*
* @param bucket[out] The hash map uninitialized
* @param[out] bucket The hash map uninitialized
*/
void hm_init(bucket_t *bucket);
void cws_hm_init(cws_bucket *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)
* @param[out] bucket The hash map
* @param[in] sockfd The file descriptor (value)
* @param[in] sas The sockaddr (value)
*/
void hm_push(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
void cws_hm_push(cws_bucket *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
* @param[out] bucket The hash map
* @param[in] sockfd The key
*/
void hm_remove(bucket_t *bucket, int sockfd);
void cws_hm_remove(cws_bucket *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)
* @param[in] bucket The hash map
* @param[in] sockfd The file descriptor (key)
* @return Returns NULL or the key pointer
*/
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
cws_bucket *cws_hm_lookup(cws_bucket *bucket, int sockfd);
/**
* @brief Cleans the hash map
*
* @param bucket[out] The hash map
* @param[out] bucket The hash map
*/
void hm_free(bucket_t *bucket);
void cws_hm_free(cws_bucket *bucket);
/**
* @brief Checks if a file descriptor is in the bucket array (not linked list)
*
* @param bucket[in]
* @param sockfd[in]
* @param[in] bucket
* @param[in] sockfd
* @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);
bool cws_hm_is_in_bucket_array(cws_bucket *bucket, int sockfd);
/**
* @brief This function will add a key even if it exists (use hm_push() instead)
@@ -85,6 +83,6 @@ bool hm_is_in_bucket_array(bucket_t *bucket, int sockfd);
* @param sockfd
* @param sas
*/
void hm_insert(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
void cws_hm_insert(cws_bucket *bucket, int sockfd, struct sockaddr_storage *sas);
#endif

View File

@@ -4,26 +4,23 @@
#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
* @param[in] hostname Hostname
* @param[in] port Port
*/
void print_ips(const char *hostname, const char *port);
void cws_utils_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
* @param[in] sa The sockaddr_storage of the client
* @param[out] ip The IP of the client
*/
void get_client_ip(struct sockaddr_storage *sa, char *ip);
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
#endif