KronkNet 1.0
Loading...
Searching...
No Matches
socket.h
1/*
2** FREE PROJECT, 2026
3** KRONKNET
4** File description:
5** Sockets, the same way on every platform.
6*/
7#ifndef KRONKNET_PLATFORM_SOCKET_H
8 #define KRONKNET_PLATFORM_SOCKET_H
9 #include "kronknet/macros/types.h"
10 #include <stddef.h>
11 #include <stdint.h>
12
13 // NOTE: The one place that knows where each platform keeps its socket API.
14 // What works differently is behind the knSocket_* functions below,
15 // written once per platform in src/platform/<platform>/ (CMake builds
16 // the one of the target)
17 #ifdef _WIN32
18 #include <winsock2.h>
19 #include <ws2tcpip.h>
20 #else
21 #include <arpa/inet.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #endif /* _WIN32 */
26
27 #define KN_POLLIN 0x01
28 #define KN_POLLOUT 0x02
29 #define KN_POLLERR 0x04
30 #define KN_POLLHUP 0x08
31
33
41typedef struct kronknet_addr_s {
42
43 union {
44 struct sockaddr any;
45 struct sockaddr_in v4;
46 struct sockaddr_in6 v6;
47 };
48 socklen_t len;
49
50} knAddr;
51
53
61int knSocket_startup(void);
62
64
68void knSocket_cleanup(void);
69
71
80knSocket knSocket_open(int family, int type);
81
83
92knSocket knSocket_accept(knSocket listener, knAddr *addr);
93
95
99void knSocket_close(knSocket fd);
100
102
109int knSocket_setReuseAddr(knSocket fd);
110
112
119int knSocket_setDualStack(knSocket fd);
120
122
132int knSocket_ignorePortUnreachable(knSocket fd);
133
135
144ssize_t knSocket_send(knSocket fd, const void *data, size_t size);
145ssize_t knSocket_recv(knSocket fd, void *buff, size_t size);
146ssize_t knSocket_sendTo(knSocket fd, const void *data, size_t size, const knAddr *addr);
147ssize_t knSocket_recvFrom(knSocket fd, void *buff, size_t size, knAddr *addr);
148
150
154knBool knSocket_wouldBlock(void);
155
157
161knBool knSocket_inProgress(void);
162
164
175int knSocket_poll(knSocket fd, uint32_t events, int timeoutMs, uint32_t *revents);
176
177#endif /* KRONKNET_PLATFORM_SOCKET_H */
An IPv4 or an IPv6 address, with its port.
Definition socket.h:41
socklen_t len
The size of the address in use.
Definition socket.h:48
struct sockaddr_in v4
When any.sa_family is AF_INET.
Definition socket.h:45
struct sockaddr any
What the socket API takes.
Definition socket.h:44
struct sockaddr_in6 v6
When any.sa_family is AF_INET6.
Definition socket.h:46