Anjay Lite
Loading...
Searching...
No Matches
anj_net_api.h
Go to the documentation of this file.
1/*
2 * Copyright 2023-2025 AVSystem <avsystem@avsystem.com>
3 * AVSystem Anjay Lite LwM2M SDK
4 * All rights reserved.
5 *
6 * Licensed under AVSystem Anjay Lite LwM2M Client SDK - Non-Commercial License.
7 * See the attached LICENSE file for details.
8 */
9
10#include <anj/init.h>
11
12#ifndef ANJ_NET_API_H
13# define ANJ_NET_API_H
14
15# include <stdbool.h>
16# include <stddef.h>
17# include <stdint.h>
18
19# ifdef __cplusplus
20extern "C" {
21# endif
22
23# ifdef ANJ_WITH_SECURE_BINDINGS
24// TODO: Remove references to avs_commons and include necessary
25// structures in a header file inside Anjay Lite
26# include <avsystem/commons/avs_crypto_pki.h>
27# include <avsystem/commons/avs_crypto_psk.h>
28# include <avsystem/commons/avs_prng.h>
29# endif // ANJ_WITH_SECURE_BINDINGS
30
39# define ANJ_NET_OK (0)
40
45# define ANJ_NET_EAGAIN (1)
46
50# define ANJ_NET_EMSGSIZE (2)
51
56# define ANJ_NET_ENOTSUP (3)
57
65
92
93# ifdef ANJ_WITH_SECURE_BINDINGS
94typedef struct {
95 uint64_t min;
96 uint64_t max;
97} anj_net_dtls_handshake_timeouts_t;
98
99typedef enum {
100 ANJ_NET_SOCKET_DANE_CA_CONSTRAINT = 0,
101 ANJ_NET_SOCKET_DANE_SERVICE_CERTIFICATE_CONSTRAINT = 1,
102 ANJ_NET_SOCKET_DANE_TRUST_ANCHOR_ASSERTION = 2,
103 ANJ_NET_SOCKET_DANE_DOMAIN_ISSUED_CERTIFICATE = 3
104} anj_net_socket_dane_certificate_usage_t;
105
106typedef enum {
107 ANJ_NET_SOCKET_DANE_CERTIFICATE = 0,
108 ANJ_NET_SOCKET_DANE_PUBLIC_KEY = 1
109} anj_net_socket_dane_selector_t;
110
111typedef enum {
112 ANJ_NET_SOCKET_DANE_MATCH_FULL = 0,
113 ANJ_NET_SOCKET_DANE_MATCH_SHA256 = 1,
114 ANJ_NET_SOCKET_DANE_MATCH_SHA512 = 2
115} anj_net_socket_dane_matching_type_t;
116
117typedef struct {
118 anj_net_socket_dane_certificate_usage_t certificate_usage;
119 anj_net_socket_dane_selector_t selector;
120 anj_net_socket_dane_matching_type_t matching_type;
121 const void *association_data;
122 size_t association_data_size;
123} anj_net_socket_dane_tlsa_record_t;
124
125typedef struct {
126 const anj_net_socket_dane_tlsa_record_t *array_ptr;
127 size_t array_element_count;
128} anj_net_socket_dane_tlsa_array_t;
129# endif // ANJ_WITH_SECURE_BINDINGS
130
138
156
157# ifdef ANJ_WITH_SECURE_BINDINGS
161typedef enum {
162 ANJ_NET_SSL_VERSION_DEFAULT = 0,
163 ANJ_NET_SSL_VERSION_TLSv1,
164 ANJ_NET_SSL_VERSION_TLSv1_1,
165 ANJ_NET_SSL_VERSION_TLSv1_2,
166 ANJ_NET_SSL_VERSION_TLSv1_3
167} anj_net_ssl_version_t;
168
169typedef enum {
170 ANJ_NET_SECURITY_DEFAULT = 0,
171 ANJ_NET_SECURITY_PSK,
172 ANJ_NET_SECURITY_CERTIFICATE =
173 ANJ_NET_SECURITY_DEFAULT
174} anj_net_security_mode_t;
175
179typedef struct {
180 avs_crypto_psk_key_info_t key;
181 avs_crypto_psk_identity_info_t identity;
182} anj_net_psk_info_t;
183
187typedef struct {
192 bool server_cert_validation;
193
201 bool ignore_system_trust_store; // TODO: consider changing the default value
202
210 bool dane;
211
217 avs_crypto_certificate_chain_info_t trusted_certs;
218
224 avs_crypto_cert_revocation_list_info_t cert_revocation_lists;
225
232 avs_crypto_certificate_chain_info_t client_cert;
233
240 avs_crypto_private_key_info_t client_key;
241
251 bool rebuild_client_cert_chain;
252} anj_net_certificate_info_t;
253
254typedef struct {
255 anj_net_security_mode_t mode;
256 union {
257 anj_net_psk_info_t psk;
258 anj_net_certificate_info_t cert;
259 } data;
260} anj_net_security_info_t;
261
262typedef struct {
264 uint32_t *ids;
266 size_t num_ids;
267} anj_net_socket_tls_ciphersuites_t;
268
269typedef struct {
273 anj_net_ssl_version_t version;
274
279 anj_net_security_info_t security;
280
284 const anj_net_dtls_handshake_timeouts_t *dtls_handshake_timeouts;
285
308 void *session_resumption_buffer;
309
322 size_t session_resumption_buffer_size;
323
338 anj_net_socket_tls_ciphersuites_t ciphersuites;
339
347 const char *
348 server_name_indication; // TODO: consider chaning the name of this
349 // parameter, see CN/SAN:
350 // https://confluence.avsystem.com/pages/viewpage.action?pageId=151233033
351
357 bool use_connection_id;
358
363 avs_crypto_prng_ctx_t *prng_ctx;
364} anj_net_ssl_configuration_t;
365# endif // ANJ_WITH_SECURE_BINDINGS
366
378typedef struct {
380# ifdef ANJ_WITH_SECURE_BINDINGS
381 anj_net_ssl_configuration_t secure_socket_config;
382# endif // ANJ_WITH_SECURE_BINDINGS
384
385struct anj_net_ctx_struct;
386typedef struct anj_net_ctx_struct anj_net_ctx_t;
387
388static inline bool anj_net_is_ok(int res) {
389 return ANJ_NET_OK == res;
390}
391
392static inline bool anj_net_is_again(int res) {
393 return ANJ_NET_EAGAIN == res;
394}
395
422
437 const anj_net_config_t *config);
438
455
479typedef int
480anj_net_connect_t(anj_net_ctx_t *ctx, const char *hostname, const char *port);
481
510 size_t *bytes_sent,
511 const uint8_t *buf,
512 size_t length);
513
543 size_t *bytes_received,
544 uint8_t *buf,
545 size_t length);
546
565
587
607
617 anj_net_socket_state_t *out_value);
618
628typedef int anj_net_get_inner_mtu_t(anj_net_ctx_t *ctx, int32_t *out_value);
629
638typedef int anj_net_get_bytes_sent_t(anj_net_ctx_t *ctx, uint64_t *out_value);
639
650 uint64_t *out_value);
651
652# ifdef ANJ_WITH_SECURE_BINDINGS
653// TODO To decide later if the following functions should return ANJ_NET_EAGAIN.
673typedef int anj_net_get_session_resumed_t(anj_net_ctx_t *ctx, bool *out_value);
674
689typedef int
690anj_net_set_dane_tlsa_array_t(anj_net_ctx_t *ctx,
691 anj_net_socket_dane_tlsa_array_t *value);
692
701typedef int
702anj_net_set_dtls_handshake_timeouts_t(anj_net_ctx_t *ctx,
703 anj_net_dtls_handshake_timeouts_t *value);
704
718typedef int anj_net_get_connection_id_resumed_t(anj_net_ctx_t *ctx,
719 bool *out_value);
720
721# endif // ANJ_WITH_SECURE_BINDINGS
722
723# ifdef __cplusplus
724}
725# endif
726
727#endif // ANJ_NET_API_H
int anj_net_get_state_t(anj_net_ctx_t *ctx, anj_net_socket_state_t *out_value)
Definition anj_net_api.h:616
#define ANJ_NET_EAGAIN
Definition anj_net_api.h:45
int anj_net_close_t(anj_net_ctx_t *ctx)
Definition anj_net_api.h:606
int anj_net_shutdown_t(anj_net_ctx_t *ctx)
Definition anj_net_api.h:586
const void * anj_net_get_system_socket_t(anj_net_ctx_t *ctx)
Definition anj_net_api.h:421
static bool anj_net_is_ok(int res)
Definition anj_net_api.h:388
anj_net_binding_type_t
Definition anj_net_api.h:58
@ ANJ_NET_BINDING_TCP
Definition anj_net_api.h:60
@ ANJ_NET_BINDING_TLS
Definition anj_net_api.h:62
@ ANJ_NET_BINDING_NON_IP
Definition anj_net_api.h:63
@ ANJ_NET_BINDING_DTLS
Definition anj_net_api.h:61
@ ANJ_NET_BINDING_UDP
Definition anj_net_api.h:59
struct anj_net_ctx_struct anj_net_ctx_t
Definition anj_net_api.h:386
int anj_net_get_bytes_received_t(anj_net_ctx_t *ctx, uint64_t *out_value)
Definition anj_net_api.h:649
int anj_net_reuse_last_port_t(anj_net_ctx_t *ctx)
Definition anj_net_api.h:564
int anj_net_get_bytes_sent_t(anj_net_ctx_t *ctx, uint64_t *out_value)
Definition anj_net_api.h:638
int anj_net_create_ctx_t(anj_net_ctx_t **ctx, const anj_net_config_t *config)
Definition anj_net_api.h:436
int anj_net_get_inner_mtu_t(anj_net_ctx_t *ctx, int32_t *out_value)
Definition anj_net_api.h:628
#define ANJ_NET_OK
Definition anj_net_api.h:39
int anj_net_recv_t(anj_net_ctx_t *ctx, size_t *bytes_received, uint8_t *buf, size_t length)
Definition anj_net_api.h:542
anj_net_socket_state_t
Definition anj_net_api.h:66
@ ANJ_NET_SOCKET_STATE_CONNECTED
Definition anj_net_api.h:90
@ ANJ_NET_SOCKET_STATE_CLOSED
Definition anj_net_api.h:71
@ ANJ_NET_SOCKET_STATE_SHUTDOWN
Definition anj_net_api.h:77
@ ANJ_NET_SOCKET_STATE_BOUND
Definition anj_net_api.h:83
int anj_net_cleanup_ctx_t(anj_net_ctx_t **ctx)
Definition anj_net_api.h:454
int anj_net_connect_t(anj_net_ctx_t *ctx, const char *hostname, const char *port)
Definition anj_net_api.h:480
anj_net_address_family_setting_t
Definition anj_net_api.h:131
@ ANJ_NET_AF_SETTING_PREFERRED_INET4
Definition anj_net_api.h:135
@ ANJ_NET_AF_SETTING_FORCE_INET4
Definition anj_net_api.h:133
@ ANJ_NET_AF_SETTING_FORCE_INET6
Definition anj_net_api.h:134
@ ANJ_NET_AF_SETTING_PREFERRED_INET6
Definition anj_net_api.h:136
@ ANJ_NET_AF_SETTING_UNSPEC
Definition anj_net_api.h:132
static bool anj_net_is_again(int res)
Definition anj_net_api.h:392
int anj_net_send_t(anj_net_ctx_t *ctx, size_t *bytes_sent, const uint8_t *buf, size_t length)
Definition anj_net_api.h:509
Definition anj_net_api.h:378
anj_net_socket_configuration_t raw_socket_config
Definition anj_net_api.h:379
Definition anj_net_api.h:143
anj_net_address_family_setting_t af_setting
Definition anj_net_api.h:154