Compare commits

...
Sign in to create a new pull request.

8 commits

9 changed files with 449 additions and 26 deletions

View file

@ -8,6 +8,10 @@ file(GLOB_RECURSE SRC_FILES "${PROJECT_SOURCE_DIR}/src/*.c")
add_library(cosms-core STATIC ${SRC_FILES})
target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include)
if (WIN32)
target_link_libraries(cosms-core PRIVATE ws2_32)
endif()
if (MSVC)
target_compile_options(cosms-core PRIVATE /W4)
else()

View file

@ -38,7 +38,7 @@ typedef enum {
struct cosms_core_file {
int mode;
#if defined(__GNUC__)
#if defined(__linux__)
int native_file;
#elif defined(_WIN32)
HANDLE native_file;
@ -50,7 +50,7 @@ struct cosms_core_file {
*
* @param file Pointer to file struct (must not be NULL)
* @param path to the file to open
* @param mode to open the file in (read, write, apped, create or a combination of these)
* @param mode to open the file in (read, write, append, create or a combination of these)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
@ -65,7 +65,7 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
/**
* @brief Gets the size of an opened file.
*
* @param file Pointer to file struct that has been opned (must not be NULL)
* @param file Pointer to file struct that has been opened (must not be NULL)
* @param size Pointer to the variable to store the size in (must not be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/

View file

@ -0,0 +1,122 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_CORE_SERVER
#define COSMS_CORE_SERVER
#if defined(__linux__)
#include <arpa/inet.h>
#define COSMS_CORE_SERVER_INVALID_SOCKET -1
#elif defined(_WIN32)
#include <winsock2.h>
#define COSMS_CORE_SERVER_INVALID_SOCKET INVALID_SOCKET
#endif
typedef enum {
COSMS_CORE_SERVER_CONNECTION_CLOSED = 1,
COSMS_CORE_SERVER_OK = 0,
COSMS_CORE_SERVER_FAILED_TO_CREATE_SOCKET = -1,
COSMS_CORE_SERVER_COULD_NOT_BIND_ADDRESS = -2,
COSMS_CORE_SERVER_UNKNOWN_ERROR = -3,
COSMS_CORE_SERVER_FAILED_TO_RECEIVE = -4,
COSMS_CORE_SERVER_FAILED_TO_SEND = -5,
COSMS_CORE_SERVER_FAILED_TO_ACCEPT = -6,
COSMS_CORE_SERVER_COULD_NOT_SET_MODE = -7
} CosmsCoreServerError;
struct cosms_core_server {
unsigned short port, domain;
int type;
#if defined(__linux__)
int listening_socket;
#elif defined(_WIN32)
SOCKET listening_socket;
#endif
};
struct cosms_core_server_client {
struct sockaddr_in address;
int address_length;
#if defined(__linux__)
int socket;
#elif defined(_WIN32)
SOCKET socket;
#endif
};
/**
* @brief Creates a new server of specified type.
*
* @param new_server Pointer to server struct (must not be NULL)
* @param domain The communication domain the server should use
* @param type of communication sementics to use
* @param port to run the server on
* @return COSMS_CORE_SERVER_OK on success, or a negative error code
*/
CosmsCoreServerError cosms_core_server_create(struct cosms_core_server *new_server, unsigned short domain, int type, unsigned short port);
/**
* @brief Stops and destroy's the server.
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
CosmsCoreServerError cosms_core_server_destroy(struct cosms_core_server *current_server);
/**
* @brief Sets server in listening state so it can listen for incomming connections (tcp only).
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param backlog How many clients will be put in the backlog before connections are declined
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
CosmsCoreServerError cosms_core_server_listen(struct cosms_core_server *current_server, unsigned int backlog);
/**
* @brief Accepts incomming connection (tcp only)
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param client Pointer to client struct to store the connected client in
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
CosmsCoreServerError cosms_core_server_accept(struct cosms_core_server *current_server, struct cosms_core_server_client *client);
/**
* @brief Receives message from client.
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param client Pointer to client struct (must be connected to server when using tcp)
* @param buffer to store the received message in (must not be NULL)
* @param buffer_size the size of the buffer to store the message in
* @param received_bytes the amount of bytes received from the client (can be NULL)
* @return COSMS_CORE_SERVER_OK or COSMS_CORE_SERVER_CONNECTION_CLOSED on success, or negative error code
*/
CosmsCoreServerError cosms_core_server_receive(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *received_bytes);
/**
* @brief Sends message to client.
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param client Pointer to client struct (must be connected to server when using tcp)
* @param buffer containing the message to send to the client
* @param buffer_size the size of the buffer to send to the client
* @param send_bytes the amount of bytes send to the client (can be NULL)
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
CosmsCoreServerError cosms_core_server_send(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *send_bytes);
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*
* @param error The error code to convert to a string message.
* @return The error as a string on success, or NULL
*/
const char *cosms_core_server_error_string(CosmsCoreServerError error);
#endif /* COSMS_CORE_SERVER */

View file

@ -12,7 +12,7 @@
#include <stdint.h>
#include <limits.h>
#if defined(__GNUC__)
#if defined(__linux__)
#define _FILE_OFFSET_BITS_ 64
#include <unistd.h>
@ -27,7 +27,7 @@
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
file->mode = mode;
#if defined(__GNUC__)
#if defined(__linux__)
int flags = 0;
if (mode & (COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE)) {
flags |= O_RDWR;
@ -121,7 +121,7 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char
* @brief Closes an open file.
*/
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
#if defined(__GNUC__)
#if defined(__linux__)
int error = close(file->native_file);
if (error == -1) {
if (errno == EBADF) {
@ -148,7 +148,7 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
* @brief Gets the size of an opened file.
*/
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
#if defined(__GNUC__)
#if defined(__linux__)
struct stat file_stat;
if (fstat(file->native_file, &file_stat) != 0) {
if (errno == EBADF) {
@ -184,7 +184,7 @@ CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buff
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
#if defined(__linux__)
int read_bytes = read(file->native_file, buffer, bytes_to_read);
if (read_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_READ;
@ -211,7 +211,7 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char *
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
#if defined(__linux__)
struct stat file_stat;
if (fstat(file->native_file, &file_stat) != 0) {
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
@ -293,7 +293,7 @@ CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buf
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
#if defined(__linux__)
int written_bytes = write(file->native_file, buffer, bytes_to_write);
if (written_bytes == -1) {
return COSMS_CORE_FILE_FAILED_TO_WRITE;
@ -323,7 +323,7 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
return COSMS_CORE_FILE_INVALID_OPERATION;
}
#if defined(__GNUC__)
#if defined(__linux__)
unsigned long long remaining_bytes = bytes_to_write;
while (remaining_bytes != 0) {
unsigned int current_bytes_to_write;
@ -372,7 +372,7 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
* @brief Deletes specified file from system.
*/
CosmsCoreFileError cosms_core_file_delete(const char *path) {
#if defined(__GNUC__)
#if defined(__linux__)
if (unlink(path) == -1) {
switch (errno) {
case ENOENT:
@ -417,40 +417,40 @@ CosmsCoreFileError cosms_core_file_delete(const char *path) {
const char *cosms_core_file_error_string(CosmsCoreFileError error) {
switch(error) {
case COSMS_CORE_FILE_OK:
return "cosms-file everything ok";
return "cosms-core-file everything ok";
case COSMS_CORE_FILE_NOT_FOUND:
return "cosms-file path not found";
return "cosms-core-file path not found";
case COSMS_CORE_FILE_NO_ACCESS:
return "cosms-file no access";
return "cosms-core-file no access";
case COSMS_CORE_FILE_LIMIT_REACHED:
return "cosms-file to many open files";
return "cosms-core-file to many open files";
case COSMS_CORE_FILE_COULD_NOT_READ_SIZE:
return "cosms-file failed to read size";
return "cosms-core-file failed to read size";
case COSMS_CORE_FILE_UNKOWN_ERROR:
return "cosms-file unkown error occured";
return "cosms-core-file unkown error occured";
case COSMS_CORE_FILE_FAILED_TO_ALLOCATE:
return "cosms-file failed to allocate memory";
return "cosms-core-file failed to allocate memory";
case COSMS_CORE_FILE_FAILED_TO_READ:
return "cosms-file failed to read content of file";
return "cosms-core-file failed to read content of file";
case COSMS_CORE_FILE_FAILED_TO_WRITE:
return "cosms-file failed to write content to file";
return "cosms-core-file failed to write content to file";
case COSMS_CORE_FILE_STILL_OPEN:
return "cosms-file still open";
return "cosms-core-file still open";
case COSMS_CORE_FILE_INVALID_FILE:
return "cosms-file invalid native file given";
return "cosms-core-file invalid native file given";
case COSMS_CORE_FILE_INVALID_OPERATION:
return "cosms-file invalid operation for current file";
return "cosms-core-file invalid operation for current file";
default:
return NULL;

187
src/networking/server.c Normal file
View file

@ -0,0 +1,187 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "cosms-core/networking/server.h"
#if defined(__linux__)
#include <unistd.h>
#elif defined(_WIN32)
static int cosms_core_server_initialized_winsock_instances = 0;
#endif
/**
* @brief Creates a new server of specified type.
*/
CosmsCoreServerError cosms_core_server_create(struct cosms_core_server *new_server, unsigned short domain, int type, unsigned short port) {
new_server->domain = domain;
new_server->type = type;
new_server->port = port;
struct sockaddr_in address;
address.sin_family = domain;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
#if defined(_WIN32)
if (cosms_core_server_initialized_winsock_instances == 0) {
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
return COSMS_CORE_SERVER_FAILED_TO_CREATE_SOCKET;
}
}
cosms_core_server_initialized_winsock_instances += 1;
#endif
new_server->listening_socket = socket(domain, type, 0);
if (new_server->listening_socket == COSMS_CORE_SERVER_INVALID_SOCKET) {
return COSMS_CORE_SERVER_FAILED_TO_CREATE_SOCKET;
}
int result = bind(new_server->listening_socket, (struct sockaddr*)&address, sizeof(address));
if (result == -1) {
return COSMS_CORE_SERVER_COULD_NOT_BIND_ADDRESS;
}
if (port == 0) {
int address_length = sizeof(address);
getsockname(new_server->listening_socket, (struct sockaddr*)&address, &address_length);
new_server->port = ntohs(address.sin_port);
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Stops and destroy's the server.
*/
CosmsCoreServerError cosms_core_server_destroy(struct cosms_core_server *current_server) {
#if defined(__linux__)
int result = close(current_server->listening_socket);
if (result == -1) {
return COSMS_CORE_SERVER_UNKNOWN_ERROR;
}
#elif defined(_WIN32)
int result = closesocket(current_server->listening_socket);
if (result == SOCKET_ERROR) {
return COSMS_CORE_SERVER_UNKNOWN_ERROR;
}
cosms_core_server_initialized_winsock_instances -= 1;
if (cosms_core_server_initialized_winsock_instances == 0) {
WSACleanup();
}
#endif
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Sets server in listening state so it can listen for incomming connections (tcp only).
*/
CosmsCoreServerError cosms_core_server_listen(struct cosms_core_server *current_server, unsigned int backlog) {
int result = listen(current_server->listening_socket, backlog);
if (result == -1) {
return COSMS_CORE_SERVER_COULD_NOT_SET_MODE;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Accepts incomming connection (tcp only).
*/
CosmsCoreServerError cosms_core_server_accept(struct cosms_core_server *current_server, struct cosms_core_server_client *client) {
client->socket = accept(current_server->listening_socket, (struct sockaddr*)&client->address, &client->address_length);
if (client->socket == COSMS_CORE_SERVER_INVALID_SOCKET) {
return COSMS_CORE_SERVER_FAILED_TO_ACCEPT;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Receives message from client.
*/
CosmsCoreServerError cosms_core_server_receive(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *received_bytes) {
int bytes_received = 0;
if (current_server->type == SOCK_STREAM) {
bytes_received = recv(client->socket, buffer, buffer_size, 0);
} else {
bytes_received = recvfrom(current_server->listening_socket, buffer, buffer_size, 0, (struct sockaddr*)&client->address, &client->address_length);
}
if (bytes_received == -1) {
return COSMS_CORE_SERVER_FAILED_TO_RECEIVE;
}
if (bytes_received == 0) {
return COSMS_CORE_SERVER_CONNECTION_CLOSED;
}
if (received_bytes != NULL) {
(*received_bytes) = bytes_received;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Sends message to client.
*/
CosmsCoreServerError cosms_core_server_send(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *send_bytes) {
int bytes_send = 0;
if (current_server->type == SOCK_STREAM) {
bytes_send = send(client->socket, buffer, buffer_size, 0);
} else {
bytes_send = sendto(current_server->listening_socket, buffer, buffer_size, 0, (struct sockaddr*)&client->address, client->address_length);
}
if (send_bytes != NULL) {
(*send_bytes) = bytes_send;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*/
const char *cosms_core_server_error_string(CosmsCoreServerError error) {
switch (error) {
case COSMS_CORE_SERVER_CONNECTION_CLOSED:
return "cosms-core-server connection was closed";
case COSMS_CORE_SERVER_OK:
return "cosms-core-server everything ok";
case COSMS_CORE_SERVER_FAILED_TO_CREATE_SOCKET:
return "cosms-core-server failed to create new socket";
case COSMS_CORE_SERVER_COULD_NOT_BIND_ADDRESS:
return "cosms-core-server could not bind address to new socket";
case COSMS_CORE_SERVER_UNKNOWN_ERROR:
return "cosms-core-server unkown error occured";
case COSMS_CORE_SERVER_FAILED_TO_RECEIVE:
return "cosms-core-server";
case COSMS_CORE_SERVER_FAILED_TO_SEND:
return "cosms-core-server ";
case COSMS_CORE_SERVER_FAILED_TO_ACCEPT:
return "cosms-core-server failed to accept new connection";
case COSMS_CORE_SERVER_COULD_NOT_SET_MODE:
return "cosms-core-server could not set mode of server socket";
default:
return NULL;
}
}

View file

@ -13,7 +13,7 @@
#include <stdlib.h>
#include <string.h>
#if defined (__GNUC__)
#if defined (__linux__)
#define COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE -1
#elif defined(_WIN32)
#define COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE NULL

View file

@ -0,0 +1,82 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "unit/networking/server.h"
#include <cosms-core/networking/server.h>
#include <winsock2.h>
COSMS_CORE_TEST_TEST(server_create_tcp_server,
struct cosms_core_server tcp_server;
CosmsCoreServerError error = cosms_core_server_create(&tcp_server, AF_INET, SOCK_STREAM, 0);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_destroy(&tcp_server);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
return NULL;
)
COSMS_CORE_TEST_TEST(server_create_udp_server,
struct cosms_core_server udp_server;
CosmsCoreServerError error = cosms_core_server_create(&udp_server, AF_INET, SOCK_DGRAM, 0);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_destroy(&udp_server);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
return NULL;
)
COSMS_CORE_TEST_TEST(server_listen_on_tcp_server,
struct cosms_core_server tcp_server;
CosmsCoreServerError error = cosms_core_server_create(&tcp_server, AF_INET, SOCK_STREAM, 0);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_listen(&tcp_server, 10);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_destroy(&tcp_server);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
return NULL;
)
COSMS_CORE_TEST_TEST(server_listen_on_udp_server,
struct cosms_core_server udp_server;
CosmsCoreServerError error = cosms_core_server_create(&udp_server, AF_INET, SOCK_DGRAM, 0);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_listen(&udp_server, 10);
if (error != COSMS_CORE_SERVER_COULD_NOT_SET_MODE) {
return cosms_core_server_error_string(error);
}
error = cosms_core_server_destroy(&udp_server);
if (error != COSMS_CORE_SERVER_OK) {
return cosms_core_server_error_string(error);
}
return NULL;
)

View file

@ -0,0 +1,26 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_CORE_SERVER_TEST
#define COSMS_CORE_SERVER_TEST
#include "test.h"
COSMS_CORE_TEST_DEFINE(server_create_tcp_server);
COSMS_CORE_TEST_DEFINE(server_create_udp_server);
COSMS_CORE_TEST_DEFINE(server_listen_on_tcp_server);
COSMS_CORE_TEST_DEFINE(server_listen_on_udp_server);
COSMS_CORE_TEST_EXPORT(server,
COSMS_CORE_TEST_EXPORT_TEST(server_create_tcp_server),
COSMS_CORE_TEST_EXPORT_TEST(server_create_udp_server),
COSMS_CORE_TEST_EXPORT_TEST(server_listen_on_tcp_server),
COSMS_CORE_TEST_EXPORT_TEST(server_listen_on_udp_server)
);
#endif /* COSMS_CORE_SERVER_TEST */

View file

@ -11,9 +11,11 @@
#include "test.h"
#include "file.h"
#include "networking/server.h"
COSMS_CORE_TEST_START
COSMS_CORE_TEST_IMPORT(file)
COSMS_CORE_TEST_IMPORT(file),
COSMS_CORE_TEST_IMPORT(server)
COSMS_CORE_TEST_END
#endif