Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
net_context.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2016 Intel Corporation
9 * Copyright (c) 2021 Nordic Semiconductor
10 * Copyright (c) 2025 Aerlync Labs Inc.
11 *
12 * SPDX-License-Identifier: Apache-2.0
13 */
14
15#ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
16#define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
17
27#include <zephyr/kernel.h>
28#include <zephyr/sys/atomic.h>
29
30#include <zephyr/net/net_ip.h>
31#include <zephyr/net/net_if.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
39#define NET_CONTEXT_IN_USE BIT(0)
40
44enum net_context_state {
45 NET_CONTEXT_IDLE = 0,
46 NET_CONTEXT_UNCONNECTED = 0,
47 NET_CONTEXT_CONFIGURING = 1,
48 NET_CONTEXT_CONNECTING = 1,
49 NET_CONTEXT_READY = 2,
50 NET_CONTEXT_CONNECTED = 2,
51 NET_CONTEXT_LISTENING = 3,
52};
53
61#define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
62
64#define NET_CONTEXT_TYPE (BIT(6) | BIT(7))
65
67#define NET_CONTEXT_REMOTE_ADDR_SET BIT(8)
68
70#define NET_CONTEXT_ACCEPTING_SOCK BIT(9)
71
73#define NET_CONTEXT_CLOSING_SOCK BIT(10)
74
76#define NET_CONTEXT_BOUND_TO_IFACE BIT(11)
77
78struct net_context;
79
100typedef void (*net_context_recv_cb_t)(struct net_context *context,
101 struct net_pkt *pkt,
102 union net_ip_header *ip_hdr,
103 union net_proto_header *proto_hdr,
104 int status,
105 void *user_data);
106
121typedef void (*net_context_send_cb_t)(struct net_context *context,
122 int status,
123 void *user_data);
124
141typedef void (*net_tcp_accept_cb_t)(struct net_context *new_context,
142 struct sockaddr *addr,
143 socklen_t addrlen,
144 int status,
145 void *user_data);
146
168typedef void (*net_context_connect_cb_t)(struct net_context *context,
169 int status,
170 void *user_data);
171
172/* The net_pkt_get_slab_func_t is here in order to avoid circular
173 * dependency between net_pkt.h and net_context.h
174 */
183typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
184
185/* The net_pkt_get_pool_func_t is here in order to avoid circular
186 * dependency between net_pkt.h and net_context.h
187 */
196typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
197
198struct net_tcp;
199
200struct net_conn_handle;
201
208__net_socket struct net_context {
212
216
220
223 struct k_mutex lock;
224
228 struct sockaddr_ptr local;
229
234
236 struct net_conn_handle *conn_handler;
237
242
247
252
253#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
257
260 net_pkt_get_pool_func_t data_pool;
261#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
262
263#if defined(CONFIG_NET_TCP)
265 void *tcp;
266#endif /* CONFIG_NET_TCP */
267
268#if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
272 struct k_sem recv_data_wait;
273#endif /* CONFIG_NET_CONTEXT_SYNC_RECV */
274
275#if defined(CONFIG_NET_SOCKETS)
277 void *socket_data;
278
280 union {
281 struct k_fifo recv_q;
282 struct k_fifo accept_q;
283 };
284
285 struct {
287 struct k_condvar recv;
288
290 struct k_mutex *lock;
291 } cond;
292#endif /* CONFIG_NET_SOCKETS */
293
294#if defined(CONFIG_NET_OFFLOAD)
296 void *offload_context;
297#endif /* CONFIG_NET_OFFLOAD */
298
299#if defined(CONFIG_NET_SOCKETS_CAN)
300 int can_filter_id;
301#endif /* CONFIG_NET_SOCKETS_CAN */
302
304 struct {
305#if defined(CONFIG_NET_CONTEXT_PRIORITY)
307 uint8_t priority;
308#endif
309#if defined(CONFIG_NET_CONTEXT_TXTIME)
311 bool txtime;
312#endif
313#if defined(CONFIG_SOCKS)
315 struct {
316 struct sockaddr addr;
317 socklen_t addrlen;
318 } proxy;
319#endif
320#if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
332 uint32_t port_range;
333#endif
334#if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
336 k_timeout_t rcvtimeo;
337#endif
338#if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
340 k_timeout_t sndtimeo;
341#endif
342#if defined(CONFIG_NET_CONTEXT_RCVBUF)
344 uint16_t rcvbuf;
345#endif
346#if defined(CONFIG_NET_CONTEXT_SNDBUF)
348 uint16_t sndbuf;
349#endif
350#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
355 uint8_t dscp_ecn;
356#endif
357#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
359 bool reuseaddr;
360#endif
361#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
363 bool reuseport;
364#endif
365#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
367 bool ipv6_v6only;
368#endif
369#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
371 bool recv_pktinfo;
372#endif
373#if defined(CONFIG_NET_IPV6)
378 uint16_t addr_preferences;
379#endif
380#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
381 union {
386 uint8_t ipv6_mcast_ifindex;
387
392 uint8_t ipv4_mcast_ifindex;
393 };
395 union {
396 bool ipv6_mcast_loop;
397 bool ipv4_mcast_loop;
398 };
399#endif /* CONFIG_NET_IPV6 || CONFIG_NET_IPV4 */
400
401#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
403 uint8_t timestamping;
404#endif
406
409
412
415
417 union {
418 struct {
421 };
422 struct {
425 };
426 };
427
428#if defined(CONFIG_SOCKS)
430 bool proxy_enabled;
431#endif
432
433};
434
442static inline bool net_context_is_used(struct net_context *context)
443{
444 NET_ASSERT(context);
445
446 return context->flags & NET_CONTEXT_IN_USE;
447}
448
456static inline bool net_context_is_bound_to_iface(struct net_context *context)
457{
458 NET_ASSERT(context);
459
460 return context->flags & NET_CONTEXT_BOUND_TO_IFACE;
461}
462
470static inline bool net_context_is_accepting(struct net_context *context)
471{
472 NET_ASSERT(context);
473
474 return context->flags & NET_CONTEXT_ACCEPTING_SOCK;
475}
476
483static inline void net_context_set_accepting(struct net_context *context,
484 bool accepting)
485{
486 NET_ASSERT(context);
487
488 if (accepting) {
490 } else {
492 }
493}
494
502static inline bool net_context_is_closing(struct net_context *context)
503{
504 NET_ASSERT(context);
505
506 return context->flags & NET_CONTEXT_CLOSING_SOCK;
507}
508
515static inline void net_context_set_closing(struct net_context *context,
516 bool closing)
517{
518 NET_ASSERT(context);
519
520 if (closing) {
522 } else {
524 }
525}
526
529#define NET_CONTEXT_STATE_SHIFT 1
530#define NET_CONTEXT_STATE_MASK 0x03
531
543static inline
544enum net_context_state net_context_get_state(struct net_context *context)
545{
546 NET_ASSERT(context);
547
548 return (enum net_context_state)
549 ((context->flags >> NET_CONTEXT_STATE_SHIFT) &
550 NET_CONTEXT_STATE_MASK);
551}
552
561static inline void net_context_set_state(struct net_context *context,
562 enum net_context_state state)
563{
564 NET_ASSERT(context);
565
566 context->flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
567 context->flags |= ((state & NET_CONTEXT_STATE_MASK) <<
568 NET_CONTEXT_STATE_SHIFT);
569}
570
581static inline sa_family_t net_context_get_family(struct net_context *context)
582{
583 NET_ASSERT(context);
584
585 return ((context->flags & NET_CONTEXT_FAMILY) >> 3);
586}
587
597static inline void net_context_set_family(struct net_context *context,
598 sa_family_t family)
599{
600 uint8_t flag = 0U;
601
602 NET_ASSERT(context);
603
604 if (family == AF_UNSPEC || family == AF_INET || family == AF_INET6 ||
605 family == AF_PACKET || family == AF_CAN) {
606 /* Family is in BIT(4), BIT(5) and BIT(6) */
607 flag = (uint8_t)(family << 3);
608 }
609
610 context->flags |= flag;
611}
612
623static inline
625{
626 NET_ASSERT(context);
627
628 return (enum net_sock_type)((context->flags & NET_CONTEXT_TYPE) >> 6);
629}
630
640static inline void net_context_set_type(struct net_context *context,
641 enum net_sock_type type)
642{
643 uint16_t flag = 0U;
644
645 NET_ASSERT(context);
646
647 if (type == SOCK_DGRAM || type == SOCK_STREAM || type == SOCK_RAW) {
648 /* Type is in BIT(6) and BIT(7)*/
649 flag = (uint16_t)(type << 6);
650 }
651
652 context->flags |= flag;
653}
654
663#if defined(CONFIG_NET_SOCKETS_CAN)
664static inline void net_context_set_can_filter_id(struct net_context *context,
665 int filter_id)
666{
667 NET_ASSERT(context);
668
669 context->can_filter_id = filter_id;
670}
671#else
672static inline void net_context_set_can_filter_id(struct net_context *context,
673 int filter_id)
674{
675 ARG_UNUSED(context);
676 ARG_UNUSED(filter_id);
677}
678#endif
679
689#if defined(CONFIG_NET_SOCKETS_CAN)
690static inline int net_context_get_can_filter_id(struct net_context *context)
691{
692 NET_ASSERT(context);
693
694 return context->can_filter_id;
695}
696#else
697static inline int net_context_get_can_filter_id(struct net_context *context)
698{
699 ARG_UNUSED(context);
700
701 return -1;
702}
703#endif
704
715static inline uint16_t net_context_get_proto(struct net_context *context)
716{
717 return context->proto;
718}
719
730static inline void net_context_set_proto(struct net_context *context,
731 uint16_t proto)
732{
733 context->proto = proto;
734}
735
746static inline
748{
749 NET_ASSERT(context);
750
751 return net_if_get_by_index(context->iface);
752}
753
762static inline void net_context_set_iface(struct net_context *context,
763 struct net_if *iface)
764{
765 NET_ASSERT(iface);
766
767 context->iface = (uint8_t)net_if_get_by_iface(iface);
768}
769
778static inline void net_context_bind_iface(struct net_context *context,
779 struct net_if *iface)
780{
781 NET_ASSERT(iface);
782
784 net_context_set_iface(context, iface);
785}
786
797static inline uint8_t net_context_get_ipv4_ttl(struct net_context *context)
798{
799 return context->ipv4_ttl;
800}
801
811static inline void net_context_set_ipv4_ttl(struct net_context *context,
812 uint8_t ttl)
813{
814 context->ipv4_ttl = ttl;
815}
816
828{
829 return context->ipv4_mcast_ttl;
830}
831
841static inline void net_context_set_ipv4_mcast_ttl(struct net_context *context,
842 uint8_t ttl)
843{
844 context->ipv4_mcast_ttl = ttl;
845}
846
847#if defined(CONFIG_NET_IPV4)
858static inline bool net_context_get_ipv4_mcast_loop(struct net_context *context)
859{
860 return context->options.ipv4_mcast_loop;
861}
862
872static inline void net_context_set_ipv4_mcast_loop(struct net_context *context,
873 bool ipv4_mcast_loop)
874{
875 context->options.ipv4_mcast_loop = ipv4_mcast_loop;
876}
877#endif
878
890{
891 return context->ipv6_hop_limit;
892}
893
902static inline void net_context_set_ipv6_hop_limit(struct net_context *context,
903 uint8_t hop_limit)
904{
905 context->ipv6_hop_limit = hop_limit;
906}
907
919{
920 return context->ipv6_mcast_hop_limit;
921}
922
932static inline void net_context_set_ipv6_mcast_hop_limit(struct net_context *context,
933 uint8_t hop_limit)
934{
935 context->ipv6_mcast_hop_limit = hop_limit;
936}
937
938#if defined(CONFIG_NET_IPV6)
939
950static inline bool net_context_get_ipv6_mcast_loop(struct net_context *context)
951{
952 return context->options.ipv6_mcast_loop;
953}
954
964static inline void net_context_set_ipv6_mcast_loop(struct net_context *context,
965 bool ipv6_mcast_loop)
966{
967 context->options.ipv6_mcast_loop = ipv6_mcast_loop;
968}
969
970#endif
971
981#if defined(CONFIG_SOCKS)
982static inline void net_context_set_proxy_enabled(struct net_context *context,
983 bool enable)
984{
985 context->proxy_enabled = enable;
986}
987#else
988static inline void net_context_set_proxy_enabled(struct net_context *context,
989 bool enable)
990{
991 ARG_UNUSED(context);
992 ARG_UNUSED(enable);
993}
994#endif
995
1006#if defined(CONFIG_SOCKS)
1007static inline bool net_context_is_proxy_enabled(struct net_context *context)
1008{
1009 return context->proxy_enabled;
1010}
1011#else
1012static inline bool net_context_is_proxy_enabled(struct net_context *context)
1013{
1014 ARG_UNUSED(context);
1015 return false;
1016}
1017#endif
1018
1037 enum net_sock_type type,
1038 uint16_t ip_proto,
1039 struct net_context **context);
1040
1054int net_context_put(struct net_context *context);
1055
1068int net_context_ref(struct net_context *context);
1069
1083int net_context_unref(struct net_context *context);
1084
1095#if defined(CONFIG_NET_IPV4)
1096int net_context_create_ipv4_new(struct net_context *context,
1097 struct net_pkt *pkt,
1098 const struct in_addr *src,
1099 const struct in_addr *dst);
1100#else
1101static inline int net_context_create_ipv4_new(struct net_context *context,
1102 struct net_pkt *pkt,
1103 const struct in_addr *src,
1104 const struct in_addr *dst)
1105{
1106 return -1;
1107}
1108#endif /* CONFIG_NET_IPV4 */
1109
1120#if defined(CONFIG_NET_IPV6)
1121int net_context_create_ipv6_new(struct net_context *context,
1122 struct net_pkt *pkt,
1123 const struct in6_addr *src,
1124 const struct in6_addr *dst);
1125#else
1126static inline int net_context_create_ipv6_new(struct net_context *context,
1127 struct net_pkt *pkt,
1128 const struct in6_addr *src,
1129 const struct in6_addr *dst)
1130{
1131 ARG_UNUSED(context);
1132 ARG_UNUSED(pkt);
1133 ARG_UNUSED(src);
1134 ARG_UNUSED(dst);
1135 return -1;
1136}
1137#endif /* CONFIG_NET_IPV6 */
1138
1150int net_context_bind(struct net_context *context,
1151 const struct sockaddr *addr,
1152 socklen_t addrlen);
1153
1165 int backlog);
1166
1196 const struct sockaddr *addr,
1197 socklen_t addrlen,
1199 k_timeout_t timeout,
1200 void *user_data);
1201
1229 k_timeout_t timeout,
1230 void *user_data);
1231
1251int net_context_send(struct net_context *context,
1252 const void *buf,
1253 size_t len,
1255 k_timeout_t timeout,
1256 void *user_data);
1257
1280 const void *buf,
1281 size_t len,
1282 const struct sockaddr *dst_addr,
1283 socklen_t addrlen,
1285 k_timeout_t timeout,
1286 void *user_data);
1287
1307 const struct msghdr *msghdr,
1308 int flags,
1310 k_timeout_t timeout,
1311 void *user_data);
1312
1349int net_context_recv(struct net_context *context,
1351 k_timeout_t timeout,
1352 void *user_data);
1353
1375 int32_t delta);
1376
1403
1415 enum net_context_option option,
1416 const void *value, size_t len);
1417
1429 enum net_context_option option,
1430 void *value, size_t *len);
1431
1439typedef void (*net_context_cb_t)(struct net_context *context, void *user_data);
1440
1449
1470#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
1471static inline void net_context_setup_pools(struct net_context *context,
1473 net_pkt_get_pool_func_t data_pool)
1474{
1475 NET_ASSERT(context);
1476
1477 context->tx_slab = tx_slab;
1478 context->data_pool = data_pool;
1479}
1480#else
1481#define net_context_setup_pools(context, tx_pool, data_pool)
1482#endif
1483
1498 uint16_t local_port, const struct sockaddr *local_addr);
1499
1500#ifdef __cplusplus
1501}
1502#endif
1503
1508#endif /* ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_ */
long atomic_t
Definition atomic_types.h:15
unsigned short int sa_family_t
Socket address family type.
Definition net_ip.h:168
#define AF_CAN
Controller Area Network.
Definition net_ip.h:58
#define AF_INET
IP protocol family version 4.
Definition net_ip.h:55
#define AF_INET6
IP protocol family version 6.
Definition net_ip.h:56
#define AF_PACKET
Packet family.
Definition net_ip.h:57
net_sock_type
Socket type.
Definition net_ip.h:88
size_t socklen_t
Length of a socket address.
Definition net_ip.h:172
#define AF_UNSPEC
Unspecified address family.
Definition net_ip.h:54
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:64
@ SOCK_DGRAM
Datagram socket type.
Definition net_ip.h:90
@ SOCK_RAW
RAW socket type
Definition net_ip.h:91
@ SOCK_STREAM
Stream socket type
Definition net_ip.h:89
static void net_context_set_type(struct net_context *context, enum net_sock_type type)
Set context type for this network context.
Definition net_context.h:640
static void net_context_set_ipv6_hop_limit(struct net_context *context, uint8_t hop_limit)
Set IPv6 hop limit value for this context.
Definition net_context.h:902
int net_context_unref(struct net_context *context)
Decrement the reference count to a network context.
int net_context_bind(struct net_context *context, const struct sockaddr *addr, socklen_t addrlen)
Assign a socket a local address.
static void net_context_set_iface(struct net_context *context, struct net_if *iface)
Set network interface for this context.
Definition net_context.h:762
static void net_context_set_ipv4_ttl(struct net_context *context, uint8_t ttl)
Set IPv4 TTL (time-to-live) value for this context.
Definition net_context.h:811
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:168
int net_context_accept(struct net_context *context, net_tcp_accept_cb_t cb, k_timeout_t timeout, void *user_data)
Accept a network connection attempt.
int net_context_put(struct net_context *context)
Close and unref a network context.
static enum net_sock_type net_context_get_type(struct net_context *context)
Get context type for this network context.
Definition net_context.h:624
static bool net_context_is_accepting(struct net_context *context)
Is this context is accepting data now.
Definition net_context.h:470
static sa_family_t net_context_get_family(struct net_context *context)
Get address family for this network context.
Definition net_context.h:581
static bool net_context_is_bound_to_iface(struct net_context *context)
Is this context bound to a network interface.
Definition net_context.h:456
static void net_context_bind_iface(struct net_context *context, struct net_if *iface)
Bind network interface to this context.
Definition net_context.h:778
int net_context_listen(struct net_context *context, int backlog)
Mark the context as a listening one.
struct k_mem_slab *(* net_pkt_get_slab_func_t)(void)
Function that is called to get the slab that is used for net_pkt allocations.
Definition net_context.h:183
static uint8_t net_context_get_ipv4_mcast_ttl(struct net_context *context)
Get IPv4 multicast TTL (time-to-live) value for this context.
Definition net_context.h:827
static bool net_context_is_used(struct net_context *context)
Is this context used or not.
Definition net_context.h:442
int net_context_sendmsg(struct net_context *context, const struct msghdr *msghdr, int flags, net_context_send_cb_t cb, k_timeout_t timeout, void *user_data)
Send data in iovec to a peer specified in msghdr struct.
int net_context_ref(struct net_context *context)
Take a reference count to a net_context, preventing destruction.
static enum net_context_state net_context_get_state(struct net_context *context)
Get state for this network context.
Definition net_context.h:544
int net_context_connect(struct net_context *context, const struct sockaddr *addr, socklen_t addrlen, net_context_connect_cb_t cb, k_timeout_t timeout, void *user_data)
Create a network connection.
static void net_context_set_can_filter_id(struct net_context *context, int filter_id)
Set CAN filter id for this network context.
Definition net_context.h:672
static bool net_context_is_proxy_enabled(struct net_context *context)
Is socks proxy support enabled or disabled for this context.
Definition net_context.h:1012
#define NET_CONTEXT_IN_USE
Is this context used or not.
Definition net_context.h:39
static void net_context_set_accepting(struct net_context *context, bool accepting)
Set this context to accept data now.
Definition net_context.h:483
bool net_context_port_in_use(enum net_ip_protocol ip_proto, uint16_t local_port, const struct sockaddr *local_addr)
Check if a port is in use (bound)
static void net_context_set_family(struct net_context *context, sa_family_t family)
Set address family for this network context.
Definition net_context.h:597
static int net_context_create_ipv6_new(struct net_context *context, struct net_pkt *pkt, const struct in6_addr *src, const struct in6_addr *dst)
Create IPv6 packet in provided net_pkt from context.
Definition net_context.h:1126
static int net_context_create_ipv4_new(struct net_context *context, struct net_pkt *pkt, const struct in_addr *src, const struct in_addr *dst)
Create IPv4 packet in provided net_pkt from context.
Definition net_context.h:1101
int net_context_recv(struct net_context *context, net_context_recv_cb_t cb, k_timeout_t timeout, void *user_data)
Receive network data from a peer specified by context.
static int net_context_get_can_filter_id(struct net_context *context)
Get CAN filter id for this network context.
Definition net_context.h:697
#define NET_CONTEXT_CLOSING_SOCK
Is the socket closing / closed.
Definition net_context.h:73
static uint8_t net_context_get_ipv6_hop_limit(struct net_context *context)
Get IPv6 hop limit value for this context.
Definition net_context.h:889
static void net_context_set_closing(struct net_context *context, bool closing)
Set this context to closing.
Definition net_context.h:515
static uint8_t net_context_get_ipv6_mcast_hop_limit(struct net_context *context)
Get IPv6 multicast hop limit value for this context.
Definition net_context.h:918
static void net_context_set_proxy_enabled(struct net_context *context, bool enable)
Enable or disable socks proxy support for this context.
Definition net_context.h:988
static uint8_t net_context_get_ipv4_ttl(struct net_context *context)
Get IPv4 TTL (time-to-live) value for this context.
Definition net_context.h:797
#define NET_CONTEXT_BOUND_TO_IFACE
Context is bound to a specific interface.
Definition net_context.h:76
void(* net_context_cb_t)(struct net_context *context, void *user_data)
Callback used while iterating over network contexts.
Definition net_context.h:1439
static void net_context_set_state(struct net_context *context, enum net_context_state state)
Set state for this network context.
Definition net_context.h:561
net_context_option
Network context options.
Definition net_context.h:1378
static void net_context_set_ipv4_mcast_ttl(struct net_context *context, uint8_t ttl)
Set IPv4 multicast TTL (time-to-live) value for this context.
Definition net_context.h:841
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:121
int net_context_update_recv_wnd(struct net_context *context, int32_t delta)
Update TCP receive window for context.
void net_context_foreach(net_context_cb_t cb, void *user_data)
Go through all the network connections and call callback for each network context.
int net_context_set_option(struct net_context *context, enum net_context_option option, const void *value, size_t len)
Set an connection option for this context.
void(* net_tcp_accept_cb_t)(struct net_context *new_context, struct sockaddr *addr, socklen_t addrlen, int status, void *user_data)
Accept callback.
Definition net_context.h:141
int net_context_send(struct net_context *context, const void *buf, size_t len, net_context_send_cb_t cb, k_timeout_t timeout, void *user_data)
Send data to a peer.
static bool net_context_is_closing(struct net_context *context)
Is this context closing.
Definition net_context.h:502
#define NET_CONTEXT_FAMILY
The address family, connection type and IP protocol are stored into a bit field to save space.
Definition net_context.h:61
#define NET_CONTEXT_TYPE
Type of the connection (datagram / stream / raw)
Definition net_context.h:64
static void net_context_set_proto(struct net_context *context, uint16_t proto)
Set context IP protocol for this network context.
Definition net_context.h:730
#define net_context_setup_pools(context, tx_pool, data_pool)
Set custom network buffer pools for context send operations.
Definition net_context.h:1481
static uint16_t net_context_get_proto(struct net_context *context)
Get context IP protocol for this network context.
Definition net_context.h:715
int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t ip_proto, struct net_context **context)
Get network context.
struct net_buf_pool *(* net_pkt_get_pool_func_t)(void)
Function that is called to get the pool that is used for net_buf allocations.
Definition net_context.h:196
#define NET_CONTEXT_ACCEPTING_SOCK
Is the socket accepting connections.
Definition net_context.h:70
int net_context_get_option(struct net_context *context, enum net_context_option option, void *value, size_t *len)
Get connection option value for this context.
static void net_context_set_ipv6_mcast_hop_limit(struct net_context *context, uint8_t hop_limit)
Set IPv6 multicast hop limit value for this context.
Definition net_context.h:932
void(* net_context_recv_cb_t)(struct net_context *context, struct net_pkt *pkt, union net_ip_header *ip_hdr, union net_proto_header *proto_hdr, int status, void *user_data)
Network data receive callback.
Definition net_context.h:100
int net_context_sendto(struct net_context *context, const void *buf, size_t len, const struct sockaddr *dst_addr, socklen_t addrlen, net_context_send_cb_t cb, k_timeout_t timeout, void *user_data)
Send data to a peer specified by address.
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition net_context.h:747
@ NET_OPT_ADDR_PREFERENCES
IPv6 address preference.
Definition net_context.h:1395
@ NET_OPT_MCAST_TTL
IPv4 multicast TTL.
Definition net_context.h:1391
@ NET_OPT_REUSEADDR
Re-use address.
Definition net_context.h:1387
@ NET_OPT_SNDBUF
Send buffer.
Definition net_context.h:1385
@ NET_OPT_IPV4_MCAST_LOOP
IPV4 multicast loop.
Definition net_context.h:1401
@ NET_OPT_RECV_PKTINFO
Receive packet information.
Definition net_context.h:1390
@ NET_OPT_LOCAL_PORT_RANGE
Clamp local port range.
Definition net_context.h:1399
@ NET_OPT_PRIORITY
Context priority.
Definition net_context.h:1379
@ NET_OPT_REUSEPORT
Re-use port.
Definition net_context.h:1388
@ NET_OPT_MCAST_HOP_LIMIT
IPv6 multicast hop limit.
Definition net_context.h:1392
@ NET_OPT_RCVTIMEO
Receive timeout.
Definition net_context.h:1382
@ NET_OPT_TXTIME
TX time.
Definition net_context.h:1380
@ NET_OPT_SNDTIMEO
Send timeout.
Definition net_context.h:1383
@ NET_OPT_MCAST_IFINDEX
IPv6 multicast output network interface index.
Definition net_context.h:1397
@ NET_OPT_SOCKS5
SOCKS5.
Definition net_context.h:1381
@ NET_OPT_IPV6_MCAST_LOOP
IPV6 multicast loop.
Definition net_context.h:1400
@ NET_OPT_TTL
IPv4 unicast TTL.
Definition net_context.h:1394
@ NET_OPT_RCVBUF
Receive buffer.
Definition net_context.h:1384
@ NET_OPT_MTU
IPv4 socket path MTU.
Definition net_context.h:1398
@ NET_OPT_UNICAST_HOP_LIMIT
IPv6 unicast hop limit.
Definition net_context.h:1393
@ NET_OPT_DSCP_ECN
DSCP ECN.
Definition net_context.h:1386
@ NET_OPT_TIMESTAMPING
Packet timestamping.
Definition net_context.h:1396
@ NET_OPT_IPV6_V6ONLY
Share IPv4 and IPv6 port space.
Definition net_context.h:1389
int net_if_get_by_iface(struct net_if *iface)
Get interface index according to pointer.
struct net_if * net_if_get_by_index(int index)
Get interface according to index.
Public kernel APIs.
Public API for network interface.
IPv6 and IPv4 definitions.
Network statistics.
flags
Definition parser.h:97
state
Definition parser_state.h:29
ssize_t recv(int sock, void *buf, size_t max_len, int flags)
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT8_TYPE__ int8_t
Definition stdint.h:72
IPv6 address struct.
Definition net_ip.h:143
IPv4 address struct.
Definition net_ip.h:155
Definition kernel.h:3182
Definition kernel.h:2540
Mutex Structure.
Definition kernel.h:3070
Kernel timeout type.
Definition sys_clock.h:65
Message struct.
Definition net_ip.h:257
Network buffer pool representation.
Definition net_buf.h:1078
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:208
atomic_t refcount
Reference count.
Definition net_context.h:219
void * user_data
User data associated with a context.
Definition net_context.h:215
void * fifo_reserved
First member of the structure to allow to put contexts into a FIFO.
Definition net_context.h:211
uint16_t flags
Flags for the context.
Definition net_context.h:411
uint8_t ipv4_mcast_ttl
IPv4 multicast TTL.
Definition net_context.h:424
net_context_send_cb_t send_cb
Send callback to be called when the packet has been sent successfully.
Definition net_context.h:246
struct sockaddr remote
Remote endpoint address.
Definition net_context.h:233
struct net_context::@409 options
Option values.
struct k_mutex lock
Internal lock for protecting this context from multiple access.
Definition net_context.h:223
struct sockaddr_ptr local
Local endpoint address.
Definition net_context.h:228
uint8_t ipv4_ttl
IPv4 TTL.
Definition net_context.h:423
uint8_t ipv6_mcast_hop_limit
IPv6 multicast hop limit.
Definition net_context.h:420
net_context_connect_cb_t connect_cb
Connect callback to be called when a connection has been established.
Definition net_context.h:251
struct net_conn_handle * conn_handler
Connection handle.
Definition net_context.h:236
uint16_t proto
Protocol (UDP, TCP or IEEE 802.3 protocol value)
Definition net_context.h:408
int8_t iface
Network interface assigned to this context.
Definition net_context.h:414
void * tcp
TCP connection information.
Definition net_context.h:265
net_context_recv_cb_t recv_cb
Receive callback to be called when desired packet has been received.
Definition net_context.h:241
uint8_t ipv6_hop_limit
IPv6 hop limit.
Definition net_context.h:419
Network Interface structure.
Definition net_if.h:714
Network packet.
Definition net_pkt.h:91
Generic sockaddr struct.
Definition net_ip.h:408