Compare commits

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

3 commits
main ... QN-5

9 changed files with 232 additions and 1 deletions

2
.gitignore vendored
View file

@ -66,3 +66,5 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
build
.cache

View file

@ -53,7 +53,7 @@ struct qnet_server {
unsigned short port; unsigned short port;
int socket; int socket;
} };
``` ```
### Enums ### Enums

37
include/qnet/network.h Normal file
View file

@ -0,0 +1,37 @@
/*
* 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 QNET_NETWORK
#define QNET_NETWORK
#if defined(__linux__)
#include <arpa/inet.h>
#define QNET_INVALID_SOCKET -1
typedef int qnet_socket_type;
#elif defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#define QNET_INVALID_SOCKET INVALID_SOCKET
typedef SOCKET qnet_socket_type;
#endif
typedef enum {
QNET_NETWORK_ERROR_OK = 0,
QNET_NETWORK_ERROR_INVALID_IP = -1
} QNetNetworkError;
inline void qnet_socket_init(qnet_socket_type *socket) { *socket = 0; }
QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct sockaddr_in *address);
QNetNetworkError qnet_ip6_address(const char *ip, unsigned short port, struct sockaddr_in6 *address);
#endif /* QNET_NETWORK */

24
include/qnet/server.h Normal file
View file

@ -0,0 +1,24 @@
/*
* 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 QNET_SERVER
#define QNET_SERVER
#include "qnet/network.h"
typedef enum {
QNET_SERVER_ERROR_OK = 0,
QNET_SERVER_ERROR_FAILED_TO_CREATE_SOCKET = -1,
QNET_SERVER_ERROR_INVALID_ADDRESS = -2,
QNET_SERVER_ERROR_FAILED_TO_BIND_ADDRESS = -3
} QNetServerError;
QNetServerError qnet_server_tcp_bind(qnet_socket_type *server_socket, struct sockaddr *address);
QNetServerError qnet_server_udp_bind(qnet_socket_type *server_socket, struct sockaddr *address);
#endif

33
src/network.c Normal file
View file

@ -0,0 +1,33 @@
/*
* 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 "qnet/network.h"
QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct sockaddr_in *address) {
address->sin_family = AF_INET;
address->sin_port = htons(port);
int result = inet_pton(address->sin_family, ip, &address->sin_addr.s_addr);
if (result != 1) {
return QNET_NETWORK_ERROR_INVALID_IP;
}
return QNET_NETWORK_ERROR_OK;
}
QNetNetworkError qnet_ip6_address(const char *ip, unsigned short port, struct sockaddr_in6 *address) {
address->sin6_family = AF_INET6;
address->sin6_port = htons(port);
int result = inet_pton(address->sin6_family, ip, &address->sin6_addr.s6_addr);
if (result != 1) {
return QNET_NETWORK_ERROR_INVALID_IP;
}
return QNET_NETWORK_ERROR_OK;
}

57
src/server.c Normal file
View file

@ -0,0 +1,57 @@
/*
* 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 "qnet/server.h"
QNetServerError qnet_tcp_bind(qnet_socket_type *server_socket, struct sockaddr *address) {
qnet_socket_type tcp_socket = socket(address->sa_family, SOCK_STREAM, 0);
if (tcp_socket == QNET_INVALID_SOCKET) {
return QNET_SERVER_ERROR_FAILED_TO_CREATE_SOCKET;
}
unsigned int address_length;
if (address->sa_family == AF_INET) {
address_length = sizeof(struct sockaddr_in);
} else if (address->sa_family == AF_INET6) {
address_length = sizeof(struct sockaddr_in6);
} else {
return QNET_SERVER_ERROR_INVALID_ADDRESS;
}
int result = bind(tcp_socket, address, address_length);
if (result == -1) {
return QNET_SERVER_ERROR_FAILED_TO_BIND_ADDRESS;
}
*server_socket = tcp_socket;
return QNET_SERVER_ERROR_OK;
}
QNetServerError qnet_udp_bind(qnet_socket_type *server_socket, struct sockaddr *address) {
qnet_socket_type udp_socket = socket(address->sa_family, SOCK_DGRAM, 0);
if (udp_socket == QNET_INVALID_SOCKET) {
return QNET_SERVER_ERROR_FAILED_TO_CREATE_SOCKET;
}
unsigned int address_length;
if (address->sa_family == AF_INET) {
address_length = sizeof(struct sockaddr_in);
} else if (address->sa_family == AF_INET6) {
address_length = sizeof(struct sockaddr_in6);
} else {
return QNET_SERVER_ERROR_INVALID_ADDRESS;
}
int result = bind(udp_socket, address, address_length);
if (result == -1) {
return QNET_SERVER_ERROR_FAILED_TO_BIND_ADDRESS;
}
*server_socket = udp_socket;
return QNET_SERVER_ERROR_OK;
}

50
tests/unit/network.c Normal file
View file

@ -0,0 +1,50 @@
/*
* 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/network.h"
#include "qnet/network.h"
QNET_TEST_IMPLEMENT(network_create_ip4_address,
struct sockaddr_in address;
QNetNetworkError error = qnet_ip4_address("127.0.0.1", 8080, &address);
if (error != QNET_NETWORK_ERROR_OK) {
return "Failed to create ip4 address, address invalid";
}
return NULL;
)
QNET_TEST_IMPLEMENT(network_create_invalid_ip4_address,
struct sockaddr_in address;
QNetNetworkError error = qnet_ip4_address("3012.23.54546.2.1", 8080, &address);
if (error != QNET_NETWORK_ERROR_INVALID_IP) {
return "Was able to create ip4 address with invalid ip";
}
return NULL;
)
QNET_TEST_IMPLEMENT(network_create_ip6_address,
struct sockaddr_in6 address;
QNetNetworkError error = qnet_ip6_address("0:0:0:0:0:0:0:1", 8080, &address);
if (error != QNET_NETWORK_ERROR_OK) {
return "Failed to create ip6 address, address invalid";
}
return NULL;
)
QNET_TEST_IMPLEMENT(network_create_invalid_ip6_address,
struct sockaddr_in6 address;
QNetNetworkError error = qnet_ip6_address("0:0:0:0:0:0:0:1:3:2:1", 8080, &address);
if (error != QNET_NETWORK_ERROR_INVALID_IP) {
return "Was able to create ip6 address with invalid ip";
}
return NULL;
)

26
tests/unit/network.h Normal file
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 QNET_NETWORK_TEST
#define QNET_NETWORK_TEST
#include "test.h"
QNET_TEST_DEFINE(network_create_ip4_address);
QNET_TEST_DEFINE(network_create_invalid_ip4_address);
QNET_TEST_DEFINE(network_create_ip6_address);
QNET_TEST_DEFINE(network_create_invalid_ip6_address);
QNET_TEST_EXPORT(network,
QNET_TEST_EXPORT_TEST(network_create_ip4_address),
QNET_TEST_EXPORT_TEST(network_create_invalid_ip4_address),
QNET_TEST_EXPORT_TEST(network_create_ip6_address),
QNET_TEST_EXPORT_TEST(network_create_invalid_ip6_address)
);
#endif

View file

@ -10,8 +10,10 @@
#define QNET_UNIT #define QNET_UNIT
#include "test.h" #include "test.h"
#include "network.h"
QNET_TEST_START QNET_TEST_START
QNET_TEST_IMPORT(network)
QNET_TEST_END QNET_TEST_END
#endif /* QNET_UNIT */ #endif /* QNET_UNIT */