refactor: move to myclib
This commit is contained in:
@@ -2,9 +2,8 @@
|
|||||||
#define CWS_SERVER_H
|
#define CWS_SERVER_H
|
||||||
|
|
||||||
#include <myclib/myhashmap.h>
|
#include <myclib/myhashmap.h>
|
||||||
#include <netdb.h>
|
#include <myclib/mysocket.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "utils/config.h"
|
#include "utils/config.h"
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,11 @@
|
|||||||
#ifndef CWS_UTILS_H
|
#ifndef CWS_UTILS_H
|
||||||
#define CWS_UTILS_H
|
#define CWS_UTILS_H
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <myclib/mysocket.h>
|
||||||
#include <netdb.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Prints each IP address associated with a host
|
|
||||||
*
|
|
||||||
* @param[in] hostname Hostname
|
|
||||||
* @param[in] port 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[in] sa The sockaddr_storage of the client
|
|
||||||
* @param[out] ip The IP of the client
|
|
||||||
*/
|
|
||||||
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
|
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Remove whitespaces from a string
|
|
||||||
*
|
|
||||||
* @param[in] str The string pointer
|
|
||||||
* @return Returns the new string without whitespaces
|
|
||||||
*/
|
|
||||||
char *cws_strip(char *str);
|
|
||||||
|
|
||||||
/* Functions used for hash maps */
|
/* Functions used for hash maps */
|
||||||
unsigned int my_str_hash_fn(const void *key);
|
unsigned int my_str_hash_fn(const void *key);
|
||||||
bool my_str_equal_fn(const void *a, const void *b);
|
bool my_str_equal_fn(const void *a, const void *b);
|
||||||
|
|||||||
@@ -5,10 +5,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "utils/colors.h"
|
#include "utils/debug.h"
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
static void cws_http_init(cws_http **request) {
|
static void cws_http_init(cws_http **request) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -6,15 +5,15 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
#include "utils/colors.h"
|
|
||||||
#include "utils/config.h"
|
#include "utils/config.h"
|
||||||
|
#include "utils/debug.h"
|
||||||
|
|
||||||
void cws_signal_handler(int signo) { cws_server_run = 0; }
|
void cws_signal_handler(int signo) { cws_server_run = 0; }
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct sigaction act = {.sa_handler = cws_signal_handler, .sa_flags = 0, .sa_mask = {{0}}};
|
struct sigaction act = {.sa_handler = cws_signal_handler, .sa_flags = 0, .sa_mask = {{0}}};
|
||||||
if (sigaction(SIGINT, &act, NULL)) {
|
if (sigaction(SIGINT, &act, NULL)) {
|
||||||
CWS_LOG_ERROR("sigaction(): %s", strerror(errno));
|
CWS_LOG_ERROR("sigaction()");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "server/worker.h"
|
#include "server/worker.h"
|
||||||
#include "utils/colors.h"
|
#include "utils/debug.h"
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
volatile sig_atomic_t cws_server_run = 1;
|
volatile sig_atomic_t cws_server_run = 1;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "http/http.h"
|
#include "http/http.h"
|
||||||
#include "utils/colors.h"
|
#include "utils/debug.h"
|
||||||
|
|
||||||
static int cws_worker_setup_epoll(cws_worker *worker) {
|
static int cws_worker_setup_epoll(cws_worker *worker) {
|
||||||
worker->epfd = epoll_create1(0);
|
worker->epfd = epoll_create1(0);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <cyaml/cyaml.h>
|
#include <cyaml/cyaml.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "utils/colors.h"
|
#include "utils/debug.h"
|
||||||
|
|
||||||
static const cyaml_config_t cyaml_config = {
|
static const cyaml_config_t cyaml_config = {
|
||||||
.log_fn = cyaml_log,
|
.log_fn = cyaml_log,
|
||||||
|
|||||||
@@ -1,48 +1,12 @@
|
|||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#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); }
|
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;
|
|
||||||
|
|
||||||
memset(&ai, 0, sizeof ai);
|
|
||||||
|
|
||||||
ai.ai_family = AF_UNSPEC;
|
|
||||||
ai.ai_socktype = SOCK_STREAM;
|
|
||||||
|
|
||||||
int status = getaddrinfo(hostname, port, &ai, &res);
|
|
||||||
if (status < 0) {
|
|
||||||
CWS_LOG_ERROR("getaddrinfo(): %s", gai_strerror(status));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char ipv4[INET_ADDRSTRLEN];
|
|
||||||
char ipv6[INET6_ADDRSTRLEN];
|
|
||||||
|
|
||||||
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;
|
|
||||||
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;
|
|
||||||
cws_utils_convert_ip(AF_INET6, &sin6->sin6_addr, ipv6, INET6_ADDRSTRLEN);
|
|
||||||
CWS_LOG_INFO("%s", ipv6);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
||||||
if (sa->ss_family == AF_INET) {
|
if (sa->ss_family == AF_INET) {
|
||||||
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
||||||
@@ -53,20 +17,6 @@ void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *cws_strip(char *str) {
|
|
||||||
char *end;
|
|
||||||
|
|
||||||
while (isspace((int)*str)) str++;
|
|
||||||
|
|
||||||
if (*str == 0) return str;
|
|
||||||
|
|
||||||
end = str + strlen(str) - 1;
|
|
||||||
while (end > str && isspace((int)*end)) end--;
|
|
||||||
*(end + 1) = '\0';
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int my_str_hash_fn(const void *key) {
|
unsigned int my_str_hash_fn(const void *key) {
|
||||||
char *key_str = (char *)key;
|
char *key_str = (char *)key;
|
||||||
size_t key_len = strlen(key_str);
|
size_t key_len = strlen(key_str);
|
||||||
|
|||||||
Reference in New Issue
Block a user