summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-06-24 17:06:37 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-06-24 17:06:37 +0200
commitb9bf5f0b675a2b1cae0d2fc4e68477d2ec2518ea (patch)
tree4cf35e62745a3ea37a599dc26c61a01bc8c50548
parent621479eec199277aa834eeeb20e249a3570fe669 (diff)
downloadkit-b9bf5f0b675a2b1cae0d2fc4e68477d2ec2518ea.zip
Sockets wrapper
-rw-r--r--README37
-rw-r--r--TODO6
-rw-r--r--source/kit/sockets.h122
-rw-r--r--source/kit/status.h4
4 files changed, 150 insertions, 19 deletions
diff --git a/README b/README
index f8f40c1..417477b 100644
--- a/README
+++ b/README
@@ -1,24 +1,29 @@
kit
-A collection of C libraries.
+A collection of cross-platform utility libraries.
Features
-- Unit-testing
-- Microbenchmarks
-- Lower bound
-- Move back
-- Dynamic array
-- Input buffer
-- Random number generation
-- Big integer math
-- SHA-256
-- Async function
-- Atomic
-- Condition variable
-- Mutual exclusion
-- Thread
+- Testing
+ - Unit-tests
+ - Microbenchmarks
+- Algorithms and data types
+ - Lower bound
+ - Move back
+ - Dynamic array
+ - Big integer math
+ - SHA-256
+- System
+ - Atomics
+ - Condition variables
+ - Mutual exclusion
+ - Threads
+ - Sockets wrapper
+- Misc
+ - Input buffer
+ - Random number generation
+ - Async functions (coroutines)
-Condition variables, mutual exclusions and threads implementation was forked from Mesa source code.
+Condition variables, mutual exclusion and threads implementation was forked from Mesa source code.
* https://gitlab.freedesktop.org/mesa/mesa
Folder "/include" contains header-only version of this library.
diff --git a/TODO b/TODO
index f8545f4..027d794 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,5 @@
-# kit To-Do
+To-Do
+
- Build system
- Remove CMake scripts
- Allocators
@@ -9,5 +10,6 @@
- Terminal
- File and memory mapping
- Inter-process transfer
- - Sockets wrapper
- SDL boilerplate
+- Add sockets tests
+
diff --git a/source/kit/sockets.h b/source/kit/sockets.h
new file mode 100644
index 0000000..8afcee6
--- /dev/null
+++ b/source/kit/sockets.h
@@ -0,0 +1,122 @@
+#ifndef KIT_SOCKETS_H
+#define KIT_SOCKETS_H
+
+#include "status.h"
+
+#ifndef KIT_DISABLE_SYSTEM_SOCKETS
+# ifdef __GNUC__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+# endif
+
+# if defined(_WIN32) && !defined(__CYGWIN__)
+
+# define WIN32_LEAN_AND_MEAN
+# include <winsock2.h>
+# include <ws2tcpip.h>
+
+# define socket_t SOCKET
+# define socklen_t int
+
+//# define EINPROGRESS WSAEINPROGRESS
+//# define EWOULDBLOCK WSAEWOULDBLOCK
+//# define EMSGSIZE WSAEMSGSIZE
+//# define EISCONN WSAEISCONN
+//# define ECONNRESET WSAECONNRESET
+//# define EADDRINUSE WSAEADDRINUSE
+
+//# define errno ((int) WSAGetLastError())
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+static kit_status_t kit_sockets_init(void) {
+ WSADATA data;
+ memset(&data, 0, sizeof data);
+ WORD version = MAKEWORD(2, 2);
+ return WSAStartup(version, &data) == ERROR_SUCCESS
+ ? KIT_OK
+ : KIT_ERROR_SOCKETS_STARTUP_FAILED;
+}
+
+static kit_status_t kit_sockets_cleanup(void) {
+ WSACleanup();
+ return KIT_OK;
+}
+
+static int kit_socket_set_blocking(socket_t s) {
+ u_long flag = 0;
+ return ioctlsocket(s, FIONBIO, &flag) == 0
+ ? KIT_OK
+ : KIT_ERROR_SOCKET_CONTROL_FAILED;
+}
+
+static int kit_socket_set_nonblocking(socket_t s) {
+ u_long flag = 1;
+ return ioctlsocket(s, FIONBIO, &flag) == 0
+ ? KIT_OK
+ : KIT_ERROR_SOCKET_CONTROL_FAILED;
+}
+
+# ifdef __cplusplus
+}
+# endif
+
+# else
+
+# include <arpa/inet.h>
+# include <errno.h>
+# include <fcntl.h>
+# include <netinet/in.h>
+# include <signal.h>
+# include <sys/ioctl.h>
+# include <sys/select.h>
+# include <sys/socket.h>
+# include <sys/types.h>
+# include <unistd.h>
+
+# define socket_t int
+# define closesocket close
+# define INVALID_SOCKET -1
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+static kit_status_t kit_sockets_init(void) {
+ signal(SIGPIPE, SIG_IGN);
+ return KIT_OK;
+}
+
+static kit_status_t kit_sockets_cleanup(void) {
+ return KIT_OK;
+}
+
+static int kit_socket_set_blocking(socket_t s) {
+ int const flags = fcntl(s, F_GETFL, 0);
+ return fcntl(s, F_SETFL, flags & ~O_NONBLOCK) == 0
+ ? KIT_OK
+ : KIT_ERROR_SOCKET_CONTROL_FAILED;
+}
+
+static int kit_socket_set_nonblocking(socket_t s) {
+ int const flags = fcntl(s, F_GETFL, 0);
+ return fcntl(s, F_SETFL, flags | O_NONBLOCK) == 0
+ ? KIT_OK
+ : KIT_ERROR_SOCKET_CONTROL_FAILED;
+}
+
+# ifdef __cplusplus
+}
+# endif
+
+# endif
+
+# ifdef __GNUC__
+# pragma GCC diagnostic pop
+# endif
+#endif
+
+#endif
diff --git a/source/kit/status.h b/source/kit/status.h
index 3b27a91..0e23151 100644
--- a/source/kit/status.h
+++ b/source/kit/status.h
@@ -13,7 +13,9 @@ enum {
KIT_ERROR_UNLINK_FAILED,
KIT_ERROR_FILE_ALREADY_EXISTS,
KIT_ERROR_FILE_DO_NOT_EXIST,
- KIT_ERROR_PATH_TOO_LONG
+ KIT_ERROR_PATH_TOO_LONG,
+ KIT_ERROR_SOCKETS_STARTUP_FAILED,
+ KIT_ERROR_SOCKET_CONTROL_FAILED
};
typedef int kit_status_t;