bio
 
Loading...
Searching...
No Matches
net.h
1#ifndef BIO_NET_H
2#define BIO_NET_H
3
4#include "bio.h"
5
15typedef struct {
16 bio_handle_t handle;
18
31
37
39typedef struct {
41 union {
43 char ipv4[4];
45 char ipv6[16];
64 struct {
65 size_t len;
66 char name[256];
67 } named;
68 };
70
72typedef uint16_t bio_port_t;
73
79static const bio_port_t BIO_PORT_ANY = 0;
80
88 .ipv4 = { 0 },
89};
90
94 .ipv4 = { 127, 0, 0, 1 },
95};
96
104 .ipv6 = { 0 },
105};
106
110 .ipv6 = { [15] = 1 },
111};
112
130bool
132 bio_socket_t* sock,
133 uintptr_t handle,
134 bio_addr_type_t addr_type,
135 bio_error_t* error
136);
137
154uintptr_t
156
171bool
173 bio_socket_type_t socket_type,
174 const bio_addr_t* addr,
175 bio_port_t port,
176 bio_socket_t* sock,
177 bio_error_t* error
178);
179
188bool
190 bio_socket_t socket,
191 bio_socket_t* client,
192 bio_error_t* error
193);
194
207bool
209 bio_socket_type_t socket_type,
210 const bio_addr_t* addr,
211 bio_port_t port,
212 bio_socket_t* socket,
213 bio_error_t* error
214);
215
217bool
219
227size_t
229 bio_socket_t socket,
230 const void* buf,
231 size_t size,
232 bio_error_t* error
233);
234
242size_t
244 bio_socket_t socket,
245 void* buf,
246 size_t size,
247 bio_error_t* error
248);
249
251size_t
253 bio_socket_t socket,
254 const bio_addr_t* addr,
255 const void* buf,
256 size_t size,
257 bio_error_t* error
258);
259
261size_t
263 bio_socket_t socket,
264 bio_addr_t* addr,
265 void* buf,
266 size_t size,
267 bio_error_t* error
268);
269
271int
273
275static inline size_t
276bio_net_send_exactly(bio_socket_t socket, const void* buf, size_t size, bio_error_t* error) {
277 size_t total_bytes_sent = 0;
278 while (total_bytes_sent < size) {
279 size_t bytes_sent = bio_net_send(
280 socket,
281 (char*)buf + total_bytes_sent,
282 size - total_bytes_sent,
283 error
284 );
285 if (bytes_sent == 0) { break; }
286 total_bytes_sent += bytes_sent;
287 }
288
289 return total_bytes_sent;
290}
291
293static inline size_t
294bio_net_recv_exactly(bio_socket_t socket, void* buf, size_t size, bio_error_t* error) {
295 size_t total_bytes_received = 0;
296 while (total_bytes_received < size) {
297 size_t bytes_received = bio_net_recv(
298 socket,
299 (char*)buf + total_bytes_received,
300 size - total_bytes_received,
301 error
302 );
303 if (bytes_received == 0) { break; }
304 total_bytes_received += bytes_received;
305 }
306
307 return total_bytes_received;
308}
309
312#endif
static const bio_port_t BIO_PORT_ANY
Any port.
Definition net.h:79
size_t bio_net_sendto(bio_socket_t socket, const bio_addr_t *addr, const void *buf, size_t size, bio_error_t *error)
Not implemented.
size_t bio_net_recv(bio_socket_t socket, void *buf, size_t size, bio_error_t *error)
Receive from a socket.
uintptr_t bio_net_unwrap(bio_socket_t socket)
Retrieve an OS handle from a bio_socket_t.
static const bio_addr_t BIO_ADDR_IPV6_ANY
Any IPv6 address.
Definition net.h:102
static const bio_addr_t BIO_ADDR_IPV4_LOOPBACK
Local loopback IPv4 address.
Definition net.h:92
static const bio_addr_t BIO_ADDR_IPV4_ANY
Any IPv4 address.
Definition net.h:86
size_t bio_net_recvfrom(bio_socket_t socket, bio_addr_t *addr, void *buf, size_t size, bio_error_t *error)
Not implemented.
int bio_net_address_compare(const bio_addr_t *lhs, const bio_addr_t *rhs)
Compare two addresses.
static const bio_addr_t BIO_ADDR_IPV6_LOOPBACK
Local loopback IPv6 address.
Definition net.h:108
static size_t bio_net_recv_exactly(bio_socket_t socket, void *buf, size_t size, bio_error_t *error)
Helper to deal with short read.
Definition net.h:294
bool bio_net_connect(bio_socket_type_t socket_type, const bio_addr_t *addr, bio_port_t port, bio_socket_t *socket, bio_error_t *error)
Make an outgoing connection.
size_t bio_net_send(bio_socket_t socket, const void *buf, size_t size, bio_error_t *error)
Send to a socket.
bool bio_net_listen(bio_socket_type_t socket_type, const bio_addr_t *addr, bio_port_t port, bio_socket_t *sock, bio_error_t *error)
Create a listening socket.
bio_addr_type_t
Address type.
Definition net.h:20
bool bio_net_close(bio_socket_t socket, bio_error_t *error)
Close a socket.
bool bio_net_wrap(bio_socket_t *sock, uintptr_t handle, bio_addr_type_t addr_type, bio_error_t *error)
Wrap a socket handle from the OS into a bio_socket_t.
static size_t bio_net_send_exactly(bio_socket_t socket, const void *buf, size_t size, bio_error_t *error)
Helper to deal with short write.
Definition net.h:276
bool bio_net_accept(bio_socket_t socket, bio_socket_t *client, bio_error_t *error)
Accept a new connection.
uint16_t bio_port_t
Port number in host byte order.
Definition net.h:72
bio_socket_type_t
Socket type.
Definition net.h:33
@ BIO_ADDR_IPV4
IPv4.
Definition net.h:22
@ BIO_ADDR_NAMED
On Unix platforms, this will be a Unix domain socket.
Definition net.h:29
@ BIO_ADDR_IPV6
IPv6.
Definition net.h:24
@ BIO_SOCKET_STREAM
Stream socket.
Definition net.h:34
@ BIO_SOCKET_DATAGRAM
Datagram socket.
Definition net.h:35
Net address.
Definition net.h:39
bio_addr_type_t type
Address type.
Definition net.h:40
An error returned from a function.
Definition bio.h:468
Definition bio.h:126
Handle to a socket.
Definition net.h:15