refactor: change project structure
This commit is contained in:
31
src/utils/net_utils.c
Normal file
31
src/utils/net_utils.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "utils/net_utils.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
|
||||
volatile sig_atomic_t cws_server_run = 1;
|
||||
|
||||
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_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
||||
if (sa->ss_family == AF_INET) {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
||||
cws_utils_convert_ip(AF_INET, &sin->sin_addr, ip, INET_ADDRSTRLEN);
|
||||
} else if (sa->ss_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
|
||||
cws_utils_convert_ip(AF_INET6, &sin6->sin6_addr, ip, INET6_ADDRSTRLEN);
|
||||
}
|
||||
}
|
||||
|
||||
cws_server_ret cws_fd_set_nonblocking(int sockfd) {
|
||||
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
|
||||
if (status == -1) {
|
||||
return CWS_SERVER_FD_NONBLOCKING_ERROR;
|
||||
}
|
||||
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user