nrfxlib API 3.4.99
Loading...
Searching...
No Matches
nrf_socket.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
14
15#ifndef NRF_SOCKET_H__
16#define NRF_SOCKET_H__
17
18#include <stdint.h>
19#include <stddef.h>
20#include <stdbool.h>
21
22#ifdef __cplusplus
23 #ifdef __GNUC__
24 /* GNU C++ compiler supports restrict as __restrict__ */
25 #define restrict __restrict__
26 #else
27 /* Other C++ compilers may not support restrict */
28 #define restrict
29 #endif
30#endif
31
32/* ssize_t is not part of the C library, but is defined by POSIX in <sys/types.h> */
33#ifdef __has_include
34 #if __has_include(<sys/types.h>)
35 #include <sys/types.h>
36 #else
37 /* <sys/types.h> missing, define ssize_t here */
38 typedef int32_t ssize_t;
39 #endif
40#else
41 /* __has_include() not supported, rely on build-system macro */
42 #ifdef NRF_MODEM_HAS_SYS_TYPES
43 #include <sys/types.h>
44 #else
45 typedef int32_t ssize_t;
46 #endif
47#endif
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
57#define NRF_MODEM_MAX_SOCKET_COUNT 8
58
63
65#define NRF_HTONS(x) ((uint16_t) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8)))
66
68#define NRF_HTONL(x) ((uint32_t) ((((x) & 0xff000000) >> 24) | \
69 (((x) & 0x00ff0000) >> 8) | \
70 (((x) & 0x0000ff00) << 8) | \
71 (((x) & 0x000000ff) << 24)))
72
74#define NRF_NTOHS(x) NRF_HTONS(x)
75
77#define NRF_NTOHL(x) NRF_HTONL(x)
78
80#define nrf_htons(x) NRF_HTONS(x)
81
83#define nrf_htonl(x) NRF_HTONL(x)
84
86#define nrf_ntohs(x) NRF_NTOHS(x)
87
89#define nrf_ntohl(x) NRF_NTOHL(x)
90
92#define NRF_INET_ADDRSTRLEN 16
93
95#define NRF_INET6_ADDRSTRLEN 46
96
98
104
110#define NRF_AF_UNSPEC 0
112#define NRF_AF_INET 1
114#define NRF_AF_INET6 2
116#define NRF_AF_PACKET 3
118
124#define NRF_SOCK_STREAM 1
126#define NRF_SOCK_DGRAM 2
128#define NRF_SOCK_RAW 3
130
136
138#define NRF_IPPROTO_IP 0
140#define NRF_IPPROTO_TCP 6
142#define NRF_IPPROTO_UDP 17
144#define NRF_IPPROTO_IPV6 41
146#define NRF_IPPROTO_RAW 255
148#define NRF_IPPROTO_ALL 512
149
151#define NRF_SPROTO_TLS1v2 258
153#define NRF_SPROTO_DTLS1v2 273
154
156 /* nrf_socket_api_enumerators */
158
164
168#define NRF_SOCKET_TLS_MAX_MESSAGE_SIZE 2048
169
173#define NRF_SOCKET_TLS_MAX_SEC_TAG_LIST_SIZE 7
174
187#define NRF_SO_SEC_TAG_LIST 1
188
200#define NRF_SO_SEC_HOSTNAME 2
201
214#define NRF_SO_SEC_CIPHERSUITE_LIST 3
215
223#define NRF_SO_SEC_CIPHERSUITE_USED 4
224
236#define NRF_SO_SEC_PEER_VERIFY 5
237
245#define NRF_SO_SEC_ROLE 6
246
256#define NRF_SO_SEC_SESSION_CACHE 12
257
263#define NRF_SO_SEC_SESSION_CACHE_PURGE 13
264
272#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEO 14
273
289#define NRF_SO_SEC_DTLS_CID 15
290
304#define NRF_SO_SEC_DTLS_CID_STATUS 16
305
344#define NRF_SO_SEC_DTLS_CONN_SAVE 17
345
364#define NRF_SO_SEC_DTLS_CONN_LOAD 18
365
377#define NRF_SO_SEC_HANDSHAKE_STATUS 19
378
390#define NRF_SO_SEC_DTLS_FRAG_EXT 20
391
401#define NRF_SO_SEC_TLS_SUPPORTED_VERSION 21
403
410
420#define NRF_SO_REUSEADDR 2
421
434#define NRF_SO_ERROR 4
435
454#define NRF_SO_RCVTIMEO 20
455
471#define NRF_SO_SNDTIMEO 21
472
480#define NRF_SO_SILENCE_ALL 30
481
489#define NRF_SO_IP_ECHO_REPLY 31
490
498#define NRF_SO_IPV6_ECHO_REPLY 32
515#define NRF_SO_EXCEPTIONAL_DATA 33
553#define NRF_SO_KEEPOPEN 34
554
562#define NRF_SO_BINDTOPDN 40
563
575#define NRF_SO_TCP_SRV_SESSTIMEO 55
576
595#define NRF_SO_POLLCB 60
596
602#define NRF_SO_RAI 61
603
622#define NRF_SO_IPV6_DELAYED_ADDR_REFRESH 62
623
641#define NRF_SO_SENDCB 63
643
666
668typedef void (*nrf_modem_sendcb_t)(const struct nrf_modem_sendcb_params *params);
669
675
690
692
698#define NRF_SOL_SOCKET 1
699#define NRF_SOL_SECURE 282
701
719#define NRF_MSG_PEEK 0x02
720
730#define NRF_MSG_DONTWAIT 0x40
731
749#define NRF_MSG_WAITALL 0x100
750
777#define NRF_MSG_WAITACK 0x200
779
787#define NRF_F_GETFL 3
789#define NRF_F_SETFL 4
791
799#define NRF_O_NONBLOCK 0x01
801
817#define NRF_RAI_NO_DATA 1
825#define NRF_RAI_LAST 2
834#define NRF_RAI_ONE_RESP 3
841#define NRF_RAI_ONGOING 4
848#define NRF_RAI_WAIT_MORE 5
850
859#define NRF_SO_SEC_PEER_VERIFY_NONE 0
861#define NRF_SO_SEC_PEER_VERIFY_OPTIONAL 1
863#define NRF_SO_SEC_PEER_VERIFY_REQUIRED 2
865
877#define NRF_SO_SEC_ROLE_CLIENT 0
879#define NRF_SO_SEC_ROLE_SERVER 1
881
890#define NRF_SO_SEC_SESSION_CACHE_DISABLED 0
892#define NRF_SO_SEC_SESSION_CACHE_ENABLED 1
894
905#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_NONE 0
907#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_1S 1
909#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_3S 3
911#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_7S 7
913#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_15S 15
915#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_31S 31
917#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_63S 63
919#define NRF_SO_SEC_DTLS_HANDSHAKE_TIMEOUT_123S 123
921
928#define NRF_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024
929#define NRF_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A
930#define NRF_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023
931#define NRF_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009
932#define NRF_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014
933#define NRF_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027
934#define NRF_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013
935#define NRF_TLS_PSK_WITH_AES_256_CBC_SHA 0x008D
936#define NRF_TLS_PSK_WITH_AES_128_CBC_SHA256 0x00AE
937#define NRF_TLS_PSK_WITH_AES_128_CBC_SHA 0x008C
938#define NRF_TLS_PSK_WITH_AES_128_CCM_8 0xC0A8
939#define NRF_TLS_EMPTY_RENEGOTIATIONINFO_SCSV 0x00FF
940#define NRF_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B
941#define NRF_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030
942#define NRF_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F
944
952
957#define NRF_SO_SEC_DTLS_CID_DISABLED 0
962#define NRF_SO_SEC_DTLS_CID_SUPPORTED 1
967#define NRF_SO_SEC_DTLS_CID_ENABLED 2
969
977
982#define NRF_SO_SEC_DTLS_CID_STATUS_DISABLED 0
986#define NRF_SO_SEC_DTLS_CID_STATUS_DOWNLINK 1
990#define NRF_SO_SEC_DTLS_CID_STATUS_UPLINK 2
995#define NRF_SO_SEC_DTLS_CID_STATUS_BIDIRECTIONAL 3
997
1012#define NRF_SO_SEC_HANDSHAKE_STATUS_FULL 0
1018#define NRF_SO_SEC_HANDSHAKE_STATUS_CACHED 1
1020
1032#define NRF_SO_SEC_DTLS_FRAG_EXT_DISABLED 0
1033
1040#define NRF_SO_SEC_DTLS_FRAG_EXT_512_ENABLED 1
1041
1048#define NRF_SO_SEC_DTLS_FRAG_EXT_1024_ENABLED 2
1050
1059#define NRF_SO_SEC_TLS_VERSION_1_2 0
1061#define NRF_SO_SEC_TLS_VERSION_1_3 1
1063
1079#define NRF_SEC_TAG_TLS_DECRYPT_BASE 2147483648
1080#define NRF_SEC_TAG_TLS_DECRYPT_0 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 0)
1081#define NRF_SEC_TAG_TLS_DECRYPT_1 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 1)
1082#define NRF_SEC_TAG_TLS_DECRYPT_2 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 2)
1083#define NRF_SEC_TAG_TLS_DECRYPT_3 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 3)
1084#define NRF_SEC_TAG_TLS_DECRYPT_4 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 4)
1085#define NRF_SEC_TAG_TLS_DECRYPT_5 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 5)
1086#define NRF_SEC_TAG_TLS_DECRYPT_6 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 6)
1087#define NRF_SEC_TAG_TLS_DECRYPT_7 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 7)
1088#define NRF_SEC_TAG_TLS_DECRYPT_8 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 8)
1089#define NRF_SEC_TAG_TLS_DECRYPT_9 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 9)
1090#define NRF_SEC_TAG_TLS_DECRYPT_10 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 10)
1091#define NRF_SEC_TAG_TLS_DECRYPT_11 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 11)
1092#define NRF_SEC_TAG_TLS_DECRYPT_12 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 12)
1093#define NRF_SEC_TAG_TLS_DECRYPT_13 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 13)
1094#define NRF_SEC_TAG_TLS_DECRYPT_14 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 14)
1095#define NRF_SEC_TAG_TLS_DECRYPT_15 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 15)
1096#define NRF_SEC_TAG_TLS_DECRYPT_16 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 16)
1097#define NRF_SEC_TAG_TLS_DECRYPT_17 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 17)
1098#define NRF_SEC_TAG_TLS_DECRYPT_18 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 18)
1099#define NRF_SEC_TAG_TLS_DECRYPT_19 (NRF_SEC_TAG_TLS_DECRYPT_BASE + 19)
1101
1108#define NRF_SEC_TAG_TLS_INVALID 0xFFFFFFFF
1110
1115
1121 uint32_t tv_sec;
1123 uint32_t tv_usec;
1124};
1125
1126
1131
1135typedef uint16_t nrf_in_port_t;
1136
1142typedef unsigned short int nrf_sa_family_t;
1143
1148 uint8_t s6_addr[16];
1149};
1150
1154typedef uint32_t nrf_in_addr_t;
1155
1162
1166extern const struct nrf_in6_addr nrf_in6addr_any;
1167
1171extern const struct nrf_in_addr nrf_inaddr_any;
1172
1188
1202
1203typedef uint32_t nrf_socklen_t;
1204typedef uint32_t nrf_nfds_t;
1205
1217
1218/* Flags for getaddrinfo() hints. */
1219
1221#define NRF_AI_CANONNAME 0x2
1223#define NRF_AI_NUMERICSERV 0x400
1231#define NRF_AI_PDNSERV 0x1000
1232
1254
1259 /* Pointer to next struct */
1264 uint32_t ifa_flags;
1275};
1276
1278
1283
1292typedef uint32_t nrf_sec_tag_t;
1293
1295
1300
1310int nrf_socket(int family, int type, int protocol);
1311
1321int nrf_close(int fildes);
1322
1339int nrf_fcntl(int fd, int cmd, int flags);
1340
1353int nrf_connect(int socket, const struct nrf_sockaddr *address, nrf_socklen_t address_len);
1354
1371ssize_t nrf_send(int socket, const void *buffer, size_t length, int flags);
1372
1389ssize_t nrf_sendto(int socket, const void *message, size_t length, int flags,
1390 const struct nrf_sockaddr *dest_addr, nrf_socklen_t dest_len);
1391
1402ssize_t nrf_recv(int socket, void *buffer, size_t length, int flags);
1403
1414ssize_t nrf_recvfrom(int socket, void *restrict buffer, size_t length, int flags,
1415 struct nrf_sockaddr *restrict address, nrf_socklen_t *restrict address_len);
1416
1418
1424
1430 int fd;
1432 short events;
1434 short revents;
1435};
1436
1438#define NRF_POLLIN 0x1
1440#define NRF_POLLOUT 0x4
1442#define NRF_POLLERR 0x8
1444#define NRF_POLLHUP 0x10
1446#define NRF_POLLNVAL 0x20
1447
1449typedef void (*nrf_modem_pollcb_t)(struct nrf_pollfd *pollfd);
1450
1462
1464
1469
1479int nrf_poll(struct nrf_pollfd fds[], nrf_nfds_t nfds, int timeout);
1480
1497int nrf_setsockopt(int socket, int level, int option_name,
1498 const void *option_value, nrf_socklen_t option_len);
1499
1509int nrf_getsockopt(int socket, int level, int option_name,
1510 void *restrict option_value, nrf_socklen_t *restrict option_len);
1511
1521int nrf_bind(int socket, const struct nrf_sockaddr *address, nrf_socklen_t address_len);
1522
1532int nrf_listen(int sock, int backlog);
1533
1544int nrf_accept(int socket, struct nrf_sockaddr *restrict address,
1545 nrf_socklen_t *restrict address_len);
1546
1548
1555
1562int nrf_inet_pton(int af, const char *restrict src, void *restrict dst);
1563
1570const char *nrf_inet_ntop(int af, const void *restrict src, char *restrict dst, nrf_socklen_t size);
1571
1587int nrf_getaddrinfo(const char *restrict nodename,
1588 const char *restrict servname,
1589 const struct nrf_addrinfo *restrict hints,
1590 struct nrf_addrinfo **restrict res);
1591
1600
1615int nrf_getifaddrs(struct nrf_ifaddrs **ifa);
1616
1625
1648int nrf_setdnsaddr(int family, const void *in_addr, nrf_socklen_t in_size);
1649
1665
1675
1677
1678#ifdef __cplusplus
1679}
1680#endif
1681
1682#endif /* NRF_SOCKET_H__ */
uint32_t nrf_in_addr_t
IPv4 address.
Definition nrf_socket.h:1154
void nrf_freeifaddrs(struct nrf_ifaddrs *ifa)
Free address information returned by nrf_getifaddrs().
uint16_t nrf_in_port_t
Socket port type.
Definition nrf_socket.h:1135
const char * nrf_inet_ntop(int af, const void *restrict src, char *restrict dst, nrf_socklen_t size)
Convert IPv4 and IPv6 addresses between binary and text form.
const struct nrf_in6_addr nrf_in6addr_any
Global IPv6 any-address.
int nrf_socket_data_enabled_set(bool enabled)
Enable or disable data traffic through the socket interface.
void nrf_freeaddrinfo(struct nrf_addrinfo *ai)
Free address information returned by nrf_getaddrinfo().
uint32_t nrf_nfds_t
Definition nrf_socket.h:1204
unsigned short int nrf_sa_family_t
Socket families.
Definition nrf_socket.h:1142
int nrf_setdnsaddr(int family, const void *in_addr, nrf_socklen_t in_size)
Set or unset a fallback DNS address.
bool nrf_socket_data_enabled_get(void)
Get the current data enabled state.
int nrf_getifaddrs(struct nrf_ifaddrs **ifa)
Get interface address information.
int nrf_inet_pton(int af, const char *restrict src, void *restrict dst)
Convert IPv4 and IPv6 addresses between binary and text form.
uint32_t nrf_socklen_t
Definition nrf_socket.h:1203
const struct nrf_in_addr nrf_inaddr_any
Global IPv4 any-address.
int nrf_getaddrinfo(const char *restrict nodename, const char *restrict servname, const struct nrf_addrinfo *restrict hints, struct nrf_addrinfo **restrict res)
Get address information.
void(* nrf_modem_pollcb_t)(struct nrf_pollfd *pollfd)
Definition nrf_socket.h:1449
int nrf_getsockopt(int socket, int level, int option_name, void *restrict option_value, nrf_socklen_t *restrict option_len)
Get the socket options.
int nrf_close(int fildes)
Close a network socket.
int nrf_poll(struct nrf_pollfd fds[], nrf_nfds_t nfds, int timeout)
Poll multiple sockets for events.
ssize_t nrf_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct nrf_sockaddr *restrict address, nrf_socklen_t *restrict address_len)
Receive a message from a socket.
int nrf_accept(int socket, struct nrf_sockaddr *restrict address, nrf_socklen_t *restrict address_len)
Accept a new connection a socket.
int nrf_setsockopt(int socket, int level, int option_name, const void *option_value, nrf_socklen_t option_len)
Set the socket options.
int nrf_connect(int socket, const struct nrf_sockaddr *address, nrf_socklen_t address_len)
Connect a socket.
ssize_t nrf_recv(int socket, void *buffer, size_t length, int flags)
Receive a message from a connected socket.
int nrf_socket(int family, int type, int protocol)
Create a network socket.
ssize_t nrf_send(int socket, const void *buffer, size_t length, int flags)
Send a message on a connected socket.
int nrf_listen(int sock, int backlog)
Listen for socket connections and limit the queue of incoming connections.
int nrf_fcntl(int fd, int cmd, int flags)
Function for controlling file descriptor options.
ssize_t nrf_sendto(int socket, const void *message, size_t length, int flags, const struct nrf_sockaddr *dest_addr, nrf_socklen_t dest_len)
Send a message on a socket.
int nrf_bind(int socket, const struct nrf_sockaddr *address, nrf_socklen_t address_len)
Bind a name to a socket.
void(* nrf_modem_sendcb_t)(const struct nrf_modem_sendcb_params *params)
Definition nrf_socket.h:668
uint32_t nrf_sec_tag_t
Security tags used on the TLS socket.
Definition nrf_socket.h:1292
int32_t ssize_t
Definition nrf_socket.h:45
struct nrf_sockaddr * ai_addr
Definition nrf_socket.h:1248
int ai_flags
Definition nrf_socket.h:1238
int ai_socktype
Definition nrf_socket.h:1242
int ai_family
Definition nrf_socket.h:1240
struct nrf_addrinfo * ai_next
Definition nrf_socket.h:1252
char * ai_canonname
Definition nrf_socket.h:1250
nrf_socklen_t ai_addrlen
Definition nrf_socket.h:1246
int ai_protocol
Definition nrf_socket.h:1244
Address information.
Definition nrf_socket.h:1236
struct nrf_sockaddr * ifa_addr
Definition nrf_socket.h:1266
char * ifa_name
Definition nrf_socket.h:1262
void * ifa_data
Definition nrf_socket.h:1274
struct nrf_sockaddr * ifa_netmask
Definition nrf_socket.h:1268
struct nrf_sockaddr * ifa_dstaddr
Definition nrf_socket.h:1272
struct nrf_sockaddr * ifa_broadaddr
Definition nrf_socket.h:1270
uint32_t ifa_flags
Definition nrf_socket.h:1264
struct nrf_ifaddrs * ifa_next
Definition nrf_socket.h:1260
Interface address information.
Definition nrf_socket.h:1258
uint8_t s6_addr[16]
Definition nrf_socket.h:1148
IPv6 address.
Definition nrf_socket.h:1147
nrf_in_addr_t s_addr
Definition nrf_socket.h:1160
IPv4 address structure.
Definition nrf_socket.h:1159
short events
Definition nrf_socket.h:1456
bool oneshot
Definition nrf_socket.h:1460
nrf_modem_pollcb_t callback
Definition nrf_socket.h:1454
Definition nrf_socket.h:1452
int status
Definition nrf_socket.h:662
size_t bytes_sent
Definition nrf_socket.h:664
int fd
Definition nrf_socket.h:654
This structure is used to describe the parameters to a send callback function.
Definition nrf_socket.h:652
nrf_modem_sendcb_t callback
Definition nrf_socket.h:673
Definition nrf_socket.h:671
uint8_t min_tls_version
Definition nrf_socket.h:686
uint8_t max_tls_version
Definition nrf_socket.h:688
TLS version configuration structure for NRF_SO_SEC_TLS_SUPPORTED_VERSION.
Definition nrf_socket.h:684
short revents
Definition nrf_socket.h:1434
short events
Definition nrf_socket.h:1432
int fd
Definition nrf_socket.h:1430
This structure is used to describe which events to poll for a given socket.
Definition nrf_socket.h:1428
uint8_t sin6_scope_id
Definition nrf_socket.h:1186
struct nrf_in6_addr sin6_addr
Definition nrf_socket.h:1184
nrf_sa_family_t sin6_family
Definition nrf_socket.h:1180
nrf_in_port_t sin6_port
Definition nrf_socket.h:1182
Address record for IPv6 addresses.
Definition nrf_socket.h:1178
nrf_sa_family_t sin_family
Definition nrf_socket.h:1196
struct nrf_in_addr sin_addr
Definition nrf_socket.h:1200
nrf_in_port_t sin_port
Definition nrf_socket.h:1198
Address record for IPv4 addresses.
Definition nrf_socket.h:1194
char sa_data[]
Definition nrf_socket.h:1215
nrf_sa_family_t sa_family
Definition nrf_socket.h:1213
Generic socket address.
Definition nrf_socket.h:1211
uint32_t tv_sec
Definition nrf_socket.h:1121
uint32_t tv_usec
Definition nrf_socket.h:1123
Structure specifying time interval.
Definition nrf_socket.h:1119