Files
ipoim/server.c
Nikita Tyukalov, ASUS, Linux 93c83dc40f Fixes
- Removed void* arithmetic
- Fixed connection_send does not return anything
- Fixed server_events invalid args in main.c (and changed its
  declaration)
2026-03-25 01:13:23 +03:00

101 lines
2.2 KiB
C

#include "server.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#include "connection.h"
#include "utils.h"
/*
* Macros
*/
#define LISTEN_BACKLOG (2048)
/*
* Data
*/
static int _fd = -1;
/*
* Private API
*/
bool _accept() {
int fd = accept(_fd, 0, 0);
if (fd == -1) {
fprintf(stderr, "[!] accept failed: %d\n", errno);
return false;
}
uint32_t id = connection_add(fd);
if (!id) {
close(fd);
fprintf(stderr, "[!] connection_add failed\n");
// no returning false, because it would terminate the app
}
else {
const char* data = "Test data lalalala";
connection_send(id, data, strlen(data));
}
return true;
}
/*
* Public API
*/
int server_open(const char* host, int port) {
struct hostent *he = gethostbyname(host);
if (!he) {
fprintf(stderr, "[!] gethostbyname failed: %d (h_errno)\n", h_errno);
return -1;
}
_fd = socket(
AF_INET,
SOCK_STREAM | SOCK_NONBLOCK,
IPPROTO_TCP
);
if (_fd == -1) {
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);
fprintf(stderr, "[!] setsockopt failed: %d\n", errno);
return -1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htobe16(port);
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (bind(_fd, (const void *)&addr, sizeof(addr)) == -1) {
close(_fd);
fprintf(stderr, "[!] bind failed: %d\n", errno);
return -1;
}
if (listen(_fd, LISTEN_BACKLOG) == -1) {
close(_fd);
fprintf(stderr, "[!] listen failed: %d\n", errno);
return -1;
}
return _fd;
}
void server_close() {
shutdown(_fd, SHUT_RDWR);
close(_fd);
}
bool server_event(int fd, uint32_t events) {
if (events & EPOLLIN) {
return _accept();
}
// we assume any event other than EPOLLIN is error
return false;
}