diff --git a/.gitignore b/.gitignore index e93af23..d21fdf2 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,5 @@ Module.symvers Mkfile.old dkms.conf +build +.cache \ No newline at end of file diff --git a/CONVENTION.md b/CONVENTION.md index 5cb231e..d225834 100644 --- a/CONVENTION.md +++ b/CONVENTION.md @@ -53,7 +53,7 @@ struct qnet_server { unsigned short port; int socket; -} +}; ``` ### Enums diff --git a/include/qnet/network.h b/include/qnet/network.h new file mode 100644 index 0000000..e807c49 --- /dev/null +++ b/include/qnet/network.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * 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 + +#define QNET_INVALID_SOCKET -1 + +typedef int qnet_socket_type; +#elif defined(_WIN32) +#include +#include + +#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; + +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 */ \ No newline at end of file diff --git a/src/network.c b/src/network.c new file mode 100644 index 0000000..d673d33 --- /dev/null +++ b/src/network.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * 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); + if (inet_pton(address->sin_family, ip, &address->sin_addr.s_addr) != 0) { + 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); + if (inet_pton(address->sin6_family, ip, &address->sin6_addr.s6_addr) != 0) { + return QNET_NETWORK_ERROR_INVALID_IP; + } + + return QNET_NETWORK_ERROR_OK; +} \ No newline at end of file