mirror of
https://github.com/hmaxnl/netex.git
synced 2025-04-19 11:38:13 +02:00
103 lines
3.2 KiB
C
103 lines
3.2 KiB
C
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <pthread.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "netex.h"
|
|
#include "config.h"
|
|
|
|
#define CONFIG_PATH "srv_config.json"
|
|
|
|
// https://stackoverflow.com/questions/15673846/how-to-give-to-a-client-specific-ip-address-in-c
|
|
|
|
CONFIGURATION* srv_configuration;
|
|
int server_sockfd;
|
|
struct sockaddr_in server_sockaddr;
|
|
pthread_t listen_thread;
|
|
bool b_listen;
|
|
|
|
void server_listen();
|
|
|
|
int initialize_server(int argc, char* argv[])
|
|
{
|
|
// Config
|
|
srv_configuration = config_load_from_path(CONFIG_PATH);
|
|
if (srv_configuration == NULL)
|
|
{
|
|
WARN("Fallback to default config!");
|
|
srv_configuration = config_create();
|
|
config_set_int(srv_configuration, "Connection.Port", 6920);
|
|
config_set_string(srv_configuration, "Connection.Host", "localhost");
|
|
config_set_string(srv_configuration, "Connection.Crypto.CertPath", "cert.pem");
|
|
if (config_save_to_path(srv_configuration, CONFIG_PATH) != 0)
|
|
{
|
|
WARN("Failed to save configuration to disk, default config is only available in memory!");
|
|
}
|
|
}
|
|
netex_init();
|
|
|
|
// Socket
|
|
const int64_t portnum = config_get_int(srv_configuration, "Connection.Port");
|
|
char* hostname = config_get_string(srv_configuration, "Connection.Host");
|
|
server_sockfd = setup_socket(portnum, hostname);
|
|
free(hostname);
|
|
if (server_sockfd <= 0)
|
|
{
|
|
WARN("Could not create server sockedfd!");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int server_execute(void)
|
|
{
|
|
const int thread_create_result = pthread_create(&listen_thread, NULL, server_listen, NULL);
|
|
if (thread_create_result != 0)
|
|
{
|
|
ERROR("Failed to create main listening thread, pthread error: %i", thread_create_result);
|
|
return -1;
|
|
}
|
|
int listen_thread_result;
|
|
const int join_result = pthread_join(listen_thread, (void**)&listen_thread_result);
|
|
if (join_result == 0)
|
|
WARN("Listen thread has exited with erros!");
|
|
return 0;
|
|
}
|
|
|
|
void server_listen()
|
|
{
|
|
PRINT_LINE("Listening...");
|
|
int some_return_code = 0;
|
|
while (true)
|
|
{
|
|
const int listen_result = listen(server_sockfd, 1);
|
|
if (listen_result == -1)
|
|
{
|
|
some_return_code = errno;
|
|
WARN("Failed while listening, error: %i", some_return_code);
|
|
break;
|
|
}
|
|
const int accepted_fd = accept(server_sockfd, NULL, NULL);
|
|
if (accepted_fd == -1)
|
|
{
|
|
const int accept_error = errno;
|
|
WARN("Could not establish connection, error: %i", accept_error);
|
|
continue;
|
|
}
|
|
// Test response
|
|
PRINT_LINE("Accepted clientfd: %i", accepted_fd);
|
|
const char* header = "HTTP/1.1 200 OK\r\nContent-Length: 14\r\nConnection: close\r\n\r\nHello, world!\n";
|
|
const int header_len = strlen(header);
|
|
const int send_result = send(accepted_fd, header, header_len, 0);
|
|
if (send_result == -1)
|
|
WARN("Error sending message to client!");
|
|
close(accepted_fd);
|
|
//TODO: Send client to thread that will handle further connection things.
|
|
}
|
|
pthread_exit((void*)some_return_code);
|
|
} |