oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
net.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
9#ifndef OVE_NET_H
10#define OVE_NET_H
11
27#include "ove/types.h"
28#include "ove_config.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* ── Types (always visible) ──────────────────────────────────────── */
35
37#ifdef __ZIG_CIMPORT__ /* @cond ZIG_ABI */
38/* Zig @cImport uses clang which does not default to -fshort-enums on ARM.
39 * Use fixed-width types so the struct layout matches the ARM EABI ABI. */
40typedef uint8_t ove_sock_type_t;
41#define OVE_SOCK_STREAM ((ove_sock_type_t)1)
42#define OVE_SOCK_DGRAM ((ove_sock_type_t)2)
43typedef uint8_t ove_af_t;
44#define OVE_AF_INET ((ove_af_t)2)
45#define OVE_AF_INET6 ((ove_af_t)10)
46#else /* @endcond */
51
53typedef enum {
56} ove_af_t;
57#endif
58
62typedef struct {
64 uint16_t port;
65 uint8_t addr[16];
67
78
79#include "ove/storage.h"
80
81#ifdef CONFIG_OVE_NET
82
83/* ── Network interface ───────────────────────────────────────────── */
84
92int ove_netif_init(ove_netif_t *netif, ove_netif_storage_t *storage);
93
100
109
116
127 ove_sockaddr_t *netmask);
128
129#ifdef OVE_HEAP_NET
137
144#endif /* OVE_HEAP_NET */
145
146/* ── Sockets ─────────────────────────────────────────────────────── */
147
157int ove_socket_open(ove_socket_t *sock, ove_socket_storage_t *storage, ove_af_t af,
158 ove_sock_type_t type);
159
166
175int ove_socket_connect(ove_socket_t sock, const ove_sockaddr_t *addr, uint64_t timeout_ns);
176
185
193int ove_socket_listen(ove_socket_t sock, int backlog);
194
204int ove_socket_accept(ove_socket_t sock, ove_socket_t *client, ove_socket_storage_t *client_storage,
205 uint64_t timeout_ns);
206
216int ove_socket_send(ove_socket_t sock, const void *data, size_t len, size_t *sent);
217
228int ove_socket_recv(ove_socket_t sock, void *buf, size_t len, size_t *received,
229 uint64_t timeout_ns);
230
241int ove_socket_sendto(ove_socket_t sock, const void *data, size_t len, size_t *sent,
242 const ove_sockaddr_t *dest);
243
255int ove_socket_recvfrom(ove_socket_t sock, void *buf, size_t len, size_t *received,
256 ove_sockaddr_t *src, uint64_t timeout_ns);
257
258#ifdef OVE_HEAP_NET
268
275#endif /* OVE_HEAP_NET */
276
277/* ── DNS ─────────────────────────────────────────────────────────── */
278
287int ove_dns_resolve(const char *hostname, ove_sockaddr_t *addr, uint64_t timeout_ns);
288
289/* ── Helpers ─────────────────────────────────────────────────────── */
290
301void ove_sockaddr_ipv4(ove_sockaddr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d,
302 uint16_t port);
303
304#else /* !CONFIG_OVE_NET */
305
307/* Provide dummy storage types so the disabled inline stubs compile. */
308#ifndef CONFIG_OVE_NET
309typedef struct {
310 uint8_t _unused;
311} ove_socket_storage_t;
312typedef struct {
313 uint8_t _unused;
314} ove_netif_storage_t;
315#endif
316
317static inline int ove_netif_init(ove_netif_t *netif, ove_netif_storage_t *storage)
318{
319 (void)netif;
320 (void)storage;
322}
323static inline void ove_netif_deinit(ove_netif_t netif)
324{
325 (void)netif;
326}
327static inline int ove_netif_up(ove_netif_t netif, const ove_netif_config_t *cfg)
328{
329 (void)netif;
330 (void)cfg;
332}
333static inline void ove_netif_down(ove_netif_t netif)
334{
335 (void)netif;
336}
337static inline int ove_netif_get_addr(ove_netif_t netif, ove_sockaddr_t *ip, ove_sockaddr_t *gw,
338 ove_sockaddr_t *nm)
339{
340 (void)netif;
341 (void)ip;
342 (void)gw;
343 (void)nm;
345}
346static inline int ove_socket_open(ove_socket_t *sock, ove_socket_storage_t *storage, ove_af_t af,
347 ove_sock_type_t type)
348{
349 (void)sock;
350 (void)storage;
351 (void)af;
352 (void)type;
354}
355static inline void ove_socket_close(ove_socket_t sock)
356{
357 (void)sock;
358}
359static inline int ove_socket_connect(ove_socket_t sock, const ove_sockaddr_t *addr,
360 uint64_t timeout_ns)
361{
362 (void)sock;
363 (void)addr;
364 (void)timeout_ns;
366}
367static inline int ove_socket_bind(ove_socket_t sock, const ove_sockaddr_t *addr)
368{
369 (void)sock;
370 (void)addr;
372}
373static inline int ove_socket_listen(ove_socket_t sock, int backlog)
374{
375 (void)sock;
376 (void)backlog;
378}
379static inline int ove_socket_accept(ove_socket_t sock, ove_socket_t *client,
380 ove_socket_storage_t *client_storage, uint64_t timeout_ns)
381{
382 (void)sock;
383 (void)client;
384 (void)client_storage;
385 (void)timeout_ns;
387}
388static inline int ove_socket_send(ove_socket_t sock, const void *data, size_t len, size_t *sent)
389{
390 (void)sock;
391 (void)data;
392 (void)len;
393 (void)sent;
395}
396static inline int ove_socket_recv(ove_socket_t sock, void *buf, size_t len, size_t *received,
397 uint64_t timeout_ns)
398{
399 (void)sock;
400 (void)buf;
401 (void)len;
402 (void)received;
403 (void)timeout_ns;
405}
406static inline int ove_socket_sendto(ove_socket_t sock, const void *data, size_t len, size_t *sent,
407 const ove_sockaddr_t *dest)
408{
409 (void)sock;
410 (void)data;
411 (void)len;
412 (void)sent;
413 (void)dest;
415}
416static inline int ove_socket_recvfrom(ove_socket_t sock, void *buf, size_t len, size_t *received,
417 ove_sockaddr_t *src, uint64_t timeout_ns)
418{
419 (void)sock;
420 (void)buf;
421 (void)len;
422 (void)received;
423 (void)src;
424 (void)timeout_ns;
426}
427static inline int ove_dns_resolve(const char *hostname, ove_sockaddr_t *addr, uint64_t timeout_ns)
428{
429 (void)hostname;
430 (void)addr;
431 (void)timeout_ns;
433}
434static inline void ove_sockaddr_ipv4(ove_sockaddr_t *addr, uint8_t a, uint8_t b, uint8_t c,
435 uint8_t d, uint16_t port)
436{
437 (void)addr;
438 (void)a;
439 (void)b;
440 (void)c;
441 (void)d;
442 (void)port;
443}
446#endif /* CONFIG_OVE_NET */
447
448#ifdef __cplusplus
449}
450#endif
451
454#endif /* OVE_NET_H */
int ove_socket_listen(ove_socket_t sock, int backlog)
Mark a bound socket as listening for incoming connections.
int ove_socket_recv(ove_socket_t sock, void *buf, size_t len, size_t *received, uint64_t timeout_ns)
Receive data from a connected socket.
void ove_netif_deinit(ove_netif_t netif)
De-initialise a network interface.
int ove_socket_sendto(ove_socket_t sock, const void *data, size_t len, size_t *sent, const ove_sockaddr_t *dest)
Send a datagram to a specific destination.
void ove_socket_close(ove_socket_t sock)
Close a socket.
int ove_netif_create(ove_netif_t *netif)
Heap-allocate and initialise a network interface.
int ove_netif_up(ove_netif_t netif, const ove_netif_config_t *cfg)
Bring the network interface up.
int ove_netif_get_addr(ove_netif_t netif, ove_sockaddr_t *ip, ove_sockaddr_t *gateway, ove_sockaddr_t *netmask)
Query the current addresses of a network interface.
ove_sock_type_t
Socket type.
Definition net.h:47
int ove_dns_resolve(const char *hostname, ove_sockaddr_t *addr, uint64_t timeout_ns)
Resolve a hostname to an address.
void ove_socket_destroy(ove_socket_t sock)
Destroy a heap-allocated socket.
ove_af_t
Address family.
Definition net.h:53
int ove_socket_open(ove_socket_t *sock, ove_socket_storage_t *storage, ove_af_t af, ove_sock_type_t type)
Open a socket from caller-supplied storage.
void ove_sockaddr_ipv4(ove_sockaddr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port)
Fill a sockaddr from IPv4 address components.
int ove_socket_bind(ove_socket_t sock, const ove_sockaddr_t *addr)
Bind a socket to a local address.
int ove_socket_accept(ove_socket_t sock, ove_socket_t *client, ove_socket_storage_t *client_storage, uint64_t timeout_ns)
Accept an incoming connection on a listening socket.
int ove_socket_recvfrom(ove_socket_t sock, void *buf, size_t len, size_t *received, ove_sockaddr_t *src, uint64_t timeout_ns)
Receive a datagram and the sender's address.
int ove_socket_connect(ove_socket_t sock, const ove_sockaddr_t *addr, uint64_t timeout_ns)
Connect a socket to a remote address.
void ove_netif_down(ove_netif_t netif)
Tear down the network interface.
void ove_netif_destroy(ove_netif_t netif)
Destroy a heap-allocated network interface.
int ove_socket_send(ove_socket_t sock, const void *data, size_t len, size_t *sent)
Send data on a connected socket.
int ove_socket_create(ove_socket_t *sock, ove_af_t af, ove_sock_type_t type)
Heap-allocate and open a socket.
int ove_netif_init(ove_netif_t *netif, ove_netif_storage_t *storage)
Initialise a network interface from caller-supplied storage.
@ OVE_SOCK_DGRAM
Definition net.h:49
@ OVE_SOCK_STREAM
Definition net.h:48
@ OVE_AF_INET
Definition net.h:54
@ OVE_AF_INET6
Definition net.h:55
struct ove_netif * ove_netif_t
Opaque handle for a network interface.
Definition types.h:250
struct ove_socket * ove_socket_t
Opaque handle for a network socket.
Definition types.h:247
@ OVE_ERR_NOT_SUPPORTED
Definition types.h:98
Network interface configuration.
Definition net.h:71
ove_sockaddr_t static_ip
Definition net.h:73
ove_sockaddr_t gateway
Definition net.h:74
ove_sockaddr_t dns
Definition net.h:76
int use_dhcp
Definition net.h:72
ove_sockaddr_t netmask
Definition net.h:75
Generic socket address (large enough for IPv4 or IPv6).
Definition net.h:62
ove_af_t family
Definition net.h:63
uint16_t port
Definition net.h:64