refactor: move to myclib
This commit is contained in:
@@ -5,10 +5,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utils/colors.h"
|
||||
#include "utils/debug.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
static void cws_http_init(cws_http **request) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -6,15 +5,15 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server/server.h"
|
||||
#include "utils/colors.h"
|
||||
#include "utils/config.h"
|
||||
#include "utils/debug.h"
|
||||
|
||||
void cws_signal_handler(int signo) { cws_server_run = 0; }
|
||||
|
||||
int main(void) {
|
||||
struct sigaction act = {.sa_handler = cws_signal_handler, .sa_flags = 0, .sa_mask = {{0}}};
|
||||
if (sigaction(SIGINT, &act, NULL)) {
|
||||
CWS_LOG_ERROR("sigaction(): %s", strerror(errno));
|
||||
CWS_LOG_ERROR("sigaction()");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
#include "server/server.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server/worker.h"
|
||||
#include "utils/colors.h"
|
||||
#include "utils/debug.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
volatile sig_atomic_t cws_server_run = 1;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "http/http.h"
|
||||
#include "utils/colors.h"
|
||||
#include "utils/debug.h"
|
||||
|
||||
static int cws_worker_setup_epoll(cws_worker *worker) {
|
||||
worker->epfd = epoll_create1(0);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <cyaml/cyaml.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "utils/colors.h"
|
||||
#include "utils/debug.h"
|
||||
|
||||
static const cyaml_config_t cyaml_config = {
|
||||
.log_fn = cyaml_log,
|
||||
|
||||
@@ -1,48 +1,12 @@
|
||||
#include "utils/utils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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); }
|
||||
|
||||
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) {
|
||||
if (sa->ss_family == AF_INET) {
|
||||
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) {
|
||||
char *key_str = (char *)key;
|
||||
size_t key_len = strlen(key_str);
|
||||
|
||||
Reference in New Issue
Block a user