Added argument parser and set all logs to STDERR

This commit is contained in:
Nikita Tyukalov, ASUS, Linux
2026-03-24 20:44:22 +03:00
parent 1cc2ef41d1
commit c8b0a5fe5a
5 changed files with 170 additions and 17 deletions

View File

@@ -34,7 +34,7 @@ bool _accept() {
int server_open(const char* host, int port) {
struct hostent *he = gethostbyname(host);
if (!he) {
printf("[!] gethostbyname failed: %d (h_errno)\n", h_errno);
fprintf(stderr, "[!] gethostbyname failed: %d (h_errno)\n", h_errno);
return -1;
}
_fd = socket(
@@ -43,13 +43,13 @@ int server_open(const char* host, int port) {
IPPROTO_TCP
);
if (_fd == -1) {
printf("[!] socket failed: %d\n", errno);
fprintf(stderr, "[!] socket failed: %d\n", errno);
return -1;
}
const int opt = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
close(_fd);
printf("[!] setsockopt failed: %d\n", errno);
fprintf(stderr, "[!] setsockopt failed: %d\n", errno);
return -1;
}
struct sockaddr_in addr;
@@ -58,12 +58,12 @@ int server_open(const char* host, int port) {
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (bind(_fd, (const void *)&addr, sizeof(addr)) == -1) {
close(_fd);
printf("[!] bind failed: %d\n", errno);
fprintf(stderr, "[!] bind failed: %d\n", errno);
return -1;
}
if (listen(_fd, LISTEN_BACKLOG) == -1) {
close(_fd);
printf("[!] listen failed: %d\n", errno);
fprintf(stderr, "[!] listen failed: %d\n", errno);
return -1;
}
return _fd;