Zephyr API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
net_context.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2016 Intel Corporation
9 * Copyright (c) 2021 Nordic Semiconductor
10 * Copyright (c) 2025 Aerlync Labs Inc.
11 * Copyright 2025 NXP
12 *
13 * SPDX-License-Identifier: Apache-2.0
14 */
15
16#ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
17#define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
18
27
28#include <zephyr/kernel.h>
29#include <zephyr/sys/atomic.h>
30
31#include <zephyr/net/net_ip.h>
32#include <zephyr/net/net_if.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
40#define NET_CONTEXT_IN_USE BIT(0)
41
43
45enum net_context_state {
46 NET_CONTEXT_IDLE = 0,
47 NET_CONTEXT_UNCONNECTED = 0,
48 NET_CONTEXT_CONFIGURING = 1,
49 NET_CONTEXT_CONNECTING = 1,
50 NET_CONTEXT_READY = 2,
51 NET_CONTEXT_CONNECTED = 2,
52 NET_CONTEXT_LISTENING = 3,
53};
54
56
62#define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
63
65#define NET_CONTEXT_TYPE (BIT(6) | BIT(7))
66
68#define NET_CONTEXT_REMOTE_ADDR_SET BIT(8)
69
71#define NET_CONTEXT_BOUND_TO_IFACE BIT(9)
72
73struct net_context;
74
95typedef void (*net_context_recv_cb_t)(struct net_context *context,
96 struct net_pkt *pkt,
97 union net_ip_header *ip_hdr,
98 union net_proto_header *proto_hdr,
99 int status,
100 void *user_data);
101
116typedef void (*net_context_send_cb_t)(struct net_context *context,
117 int status,
118 void *user_data);
119
136typedef void (*net_tcp_accept_cb_t)(struct net_context *new_context,
137 struct net_sockaddr *addr,
138 net_socklen_t addrlen,
139 int status,
140 void *user_data);
141
163typedef void (*net_context_connect_cb_t)(struct net_context *context,
164 int status,
165 void *user_data);
166
167/* The net_pkt_get_slab_func_t is here in order to avoid circular
168 * dependency between net_pkt.h and net_context.h
169 */
178typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
179
180/* The net_pkt_get_pool_func_t is here in order to avoid circular
181 * dependency between net_pkt.h and net_context.h
182 */
191typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
192
193struct net_tcp;
194
195struct net_conn_handle;
196
203__net_socket struct net_context {
207
211
215
218 struct k_mutex lock;
219
223 struct net_sockaddr_ptr local;
224
229
231 struct net_conn_handle *conn_handler;
232
237
242
247
248#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
252
255 net_pkt_get_pool_func_t data_pool;
256#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
257
258#if defined(CONFIG_NET_TCP)
260 void *tcp;
261#endif /* CONFIG_NET_TCP */
262
263#if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
267 struct k_sem recv_data_wait;
268#endif /* CONFIG_NET_CONTEXT_SYNC_RECV */
269
270#if defined(CONFIG_NET_CONTEXT_LINGER)
275 struct k_sem linger_sem;
276#endif /* CONFIG_NET_CONTEXT_LINGER */
277
278#if defined(CONFIG_NET_SOCKETS)
280 void *socket_data;
281
283 union {
284 struct k_fifo recv_q;
285 struct k_fifo accept_q;
286 };
287
288 struct {
290 struct k_condvar recv;
291
293 struct k_mutex *lock;
294 } cond;
295#endif /* CONFIG_NET_SOCKETS */
296
297#if defined(CONFIG_NET_OFFLOAD)
299 void *offload_context;
300#endif /* CONFIG_NET_OFFLOAD */
301
302#if defined(CONFIG_NET_SOCKETS_CAN)
303 int can_filter_id;
304#endif /* CONFIG_NET_SOCKETS_CAN */
305
307 struct {
308#if defined(CONFIG_NET_CONTEXT_PRIORITY)
310 uint8_t priority;
311#endif
312#if defined(CONFIG_NET_CONTEXT_TXTIME)
314 bool txtime;
315#endif
316#if defined(CONFIG_SOCKS)
318 struct {
319 struct net_sockaddr addr;
320 net_socklen_t addrlen;
321 } proxy;
322#endif
323#if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
335 uint32_t port_range;
336#endif
337#if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
339 k_timeout_t rcvtimeo;
340#endif
341#if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
343 k_timeout_t sndtimeo;
344#endif
345#if defined(CONFIG_NET_CONTEXT_RCVBUF)
347 uint16_t rcvbuf;
348#endif
349#if defined(CONFIG_NET_CONTEXT_SNDBUF)
351 uint16_t sndbuf;
352#endif
353#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
358 uint8_t dscp_ecn;
359#endif
360#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
362 bool reuseaddr;
363#endif
364#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
366 bool reuseport;
367#endif
368#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
370 bool ipv6_v6only;
371#endif
372#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
374 bool recv_pktinfo;
375#endif
376#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
378 bool recv_hoplimit;
379#endif
380#if defined(CONFIG_NET_IPV6)
385 uint16_t addr_preferences;
386#endif
387#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
388 union {
393 uint8_t ipv6_mcast_ifindex;
394
399 uint8_t ipv4_mcast_ifindex;
400 };
402 union {
403 bool ipv6_mcast_loop;
404 bool ipv4_mcast_loop;
405 };
406
408 bool dont_fragment;
409#endif /* CONFIG_NET_IPV6 || CONFIG_NET_IPV4 */
410
411#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
413 uint8_t timestamping;
414#endif
415
416#if defined(CONFIG_NET_CONTEXT_LINGER)
421 struct net_linger linger;
422#endif /* CONFIG_NET_CONTEXT_LINGER */
423
424#if defined(CONFIG_NET_UDP_OPTIONS)
425 struct {
427 uint32_t enabled;
429 uint32_t required;
431 bool drop_all_opts;
433 uint16_t mds;
435 struct net_udp_opt_mrds mrds;
436 } udp_opt;
437#endif
439
442
445
448
450 union {
451 struct {
454 };
455 struct {
458 };
459 };
460
461#if defined(CONFIG_SOCKS)
463 bool proxy_enabled;
464#endif
465
466};
467
475static inline bool net_context_is_used(struct net_context *context)
476{
477 NET_ASSERT(context);
478
479 return context->flags & NET_CONTEXT_IN_USE;
480}
481
489static inline bool net_context_is_bound_to_iface(struct net_context *context)
490{
491 NET_ASSERT(context);
492
493 return context->flags & NET_CONTEXT_BOUND_TO_IFACE;
494}
495
497
498#define NET_CONTEXT_STATE_SHIFT 1
499#define NET_CONTEXT_STATE_MASK 0x03
500
502
512static inline
513enum net_context_state net_context_get_state(struct net_context *context)
514{
515 NET_ASSERT(context);
516
517 return (enum net_context_state)
518 ((context->flags >> NET_CONTEXT_STATE_SHIFT) &
519 NET_CONTEXT_STATE_MASK);
520}
521
530static inline void net_context_set_state(struct net_context *context,
531 enum net_context_state state)
532{
533 NET_ASSERT(context);
534
535 context->flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
536 context->flags |= ((state & NET_CONTEXT_STATE_MASK) <<
537 NET_CONTEXT_STATE_SHIFT);
538}
539
551{
552 NET_ASSERT(context);
553
554 return ((context->flags & NET_CONTEXT_FAMILY) >> 3);
555}
556
566static inline void net_context_set_family(struct net_context *context,
567 net_sa_family_t family)
568{
569 uint8_t flag = 0U;
570
571 NET_ASSERT(context);
572
573 if (family == NET_AF_UNSPEC || family == NET_AF_INET || family == NET_AF_INET6 ||
574 family == NET_AF_PACKET || family == NET_AF_CAN) {
575 /* Family is in BIT(4), BIT(5) and BIT(6) */
576 flag = (uint8_t)(family << 3);
577 }
578
579 context->flags |= flag;
580}
581
592static inline
594{
595 NET_ASSERT(context);
596
597 return (enum net_sock_type)((context->flags & NET_CONTEXT_TYPE) >> 6);
598}
599
609static inline void net_context_set_type(struct net_context *context,
610 enum net_sock_type type)
611{
612 uint16_t flag = 0U;
613
614 NET_ASSERT(context);
615
616 if (type == NET_SOCK_DGRAM || type == NET_SOCK_STREAM || type == NET_SOCK_RAW) {
617 /* Type is in BIT(6) and BIT(7)*/
618 flag = (uint16_t)(type << 6);
619 }
620
621 context->flags |= flag;
622}
623
632#if defined(CONFIG_NET_SOCKETS_CAN)
633static inline void net_context_set_can_filter_id(struct net_context *context,
634 int filter_id)
635{
636 NET_ASSERT(context);
637
638 context->can_filter_id = filter_id;
639}
640#else
641static inline void net_context_set_can_filter_id(struct net_context *context,
642 int filter_id)
643{
644 ARG_UNUSED(context);
645 ARG_UNUSED(filter_id);
646}
647#endif
648
658#if defined(CONFIG_NET_SOCKETS_CAN)
659static inline int net_context_get_can_filter_id(struct net_context *context)
660{
661 NET_ASSERT(context);
662
663 return context->can_filter_id;
664}
665#else
666static inline int net_context_get_can_filter_id(struct net_context *context)
667{
668 ARG_UNUSED(context);
669
670 return -1;
671}
672#endif
673
684static inline uint16_t net_context_get_proto(struct net_context *context)
685{
686 return context->proto;
687}
688
699static inline void net_context_set_proto(struct net_context *context,
700 uint16_t proto)
701{
702 context->proto = proto;
703}
704
715static inline
717{
718 NET_ASSERT(context);
719
720 return net_if_get_by_index(context->iface);
721}
722
731static inline void net_context_set_iface(struct net_context *context,
732 struct net_if *iface)
733{
734 NET_ASSERT(iface);
735
736 context->iface = (uint8_t)net_if_get_by_iface(iface);
737}
738
747static inline void net_context_bind_iface(struct net_context *context,
748 struct net_if *iface)
749{
750 NET_ASSERT(iface);
751
753 net_context_set_iface(context, iface);
754}
755
766static inline uint8_t net_context_get_ipv4_ttl(struct net_context *context)
767{
768 return context->ipv4_ttl;
769}
770
780static inline void net_context_set_ipv4_ttl(struct net_context *context,
781 uint8_t ttl)
782{
783 context->ipv4_ttl = ttl;
784}
785
797{
798 return context->ipv4_mcast_ttl;
799}
800
810static inline void net_context_set_ipv4_mcast_ttl(struct net_context *context,
811 uint8_t ttl)
812{
813 context->ipv4_mcast_ttl = ttl;
814}
815
816#if defined(CONFIG_NET_IPV4)
827static inline bool net_context_get_ipv4_mcast_loop(struct net_context *context)
828{
829 return context->options.ipv4_mcast_loop;
830}
831
841static inline void net_context_set_ipv4_mcast_loop(struct net_context *context,
842 bool ipv4_mcast_loop)
843{
844 context->options.ipv4_mcast_loop = ipv4_mcast_loop;
845}
846#endif
847
859{
860 return context->ipv6_hop_limit;
861}
862
871static inline void net_context_set_ipv6_hop_limit(struct net_context *context,
872 uint8_t hop_limit)
873{
874 context->ipv6_hop_limit = hop_limit;
875}
876
888{
889 return context->ipv6_mcast_hop_limit;
890}
891
901static inline void net_context_set_ipv6_mcast_hop_limit(struct net_context *context,
902 uint8_t hop_limit)
903{
904 context->ipv6_mcast_hop_limit = hop_limit;
905}
906
907#if defined(CONFIG_NET_IPV6)
908
919static inline bool net_context_get_ipv6_mcast_loop(struct net_context *context)
920{
921 return context->options.ipv6_mcast_loop;
922}
923
933static inline void net_context_set_ipv6_mcast_loop(struct net_context *context,
934 bool ipv6_mcast_loop)
935{
936 context->options.ipv6_mcast_loop = ipv6_mcast_loop;
937}
938
939#endif
940
950#if defined(CONFIG_SOCKS)
951static inline void net_context_set_proxy_enabled(struct net_context *context,
952 bool enable)
953{
954 context->proxy_enabled = enable;
955}
956#else
957static inline void net_context_set_proxy_enabled(struct net_context *context,
958 bool enable)
959{
960 ARG_UNUSED(context);
961 ARG_UNUSED(enable);
962}
963#endif
964
975#if defined(CONFIG_SOCKS)
976static inline bool net_context_is_proxy_enabled(struct net_context *context)
977{
978 return context->proxy_enabled;
979}
980#else
981static inline bool net_context_is_proxy_enabled(struct net_context *context)
982{
983 ARG_UNUSED(context);
984 return false;
985}
986#endif
987
1006 enum net_sock_type type,
1007 uint16_t ip_proto,
1008 struct net_context **context);
1009
1023int net_context_put(struct net_context *context);
1024
1037int net_context_ref(struct net_context *context);
1038
1052int net_context_unref(struct net_context *context);
1053
1062static inline void net_context_signal_linger(struct net_context *context)
1063{
1064#if defined(CONFIG_NET_CONTEXT_LINGER)
1065 k_sem_give(&context->linger_sem);
1066#else
1067 ARG_UNUSED(context);
1068#endif /* CONFIG_NET_CONTEXT_LINGER */
1069}
1070
1081#if defined(CONFIG_NET_IPV4)
1082int net_context_create_ipv4_new(struct net_context *context,
1083 struct net_pkt *pkt,
1084 const struct net_in_addr *src,
1085 const struct net_in_addr *dst);
1086#else
1087static inline int net_context_create_ipv4_new(struct net_context *context,
1088 struct net_pkt *pkt,
1089 const struct net_in_addr *src,
1090 const struct net_in_addr *dst)
1091{
1092 return -1;
1093}
1094#endif /* CONFIG_NET_IPV4 */
1095
1106#if defined(CONFIG_NET_IPV6)
1107int net_context_create_ipv6_new(struct net_context *context,
1108 struct net_pkt *pkt,
1109 const struct net_in6_addr *src,
1110 const struct net_in6_addr *dst);
1111#else
1112static inline int net_context_create_ipv6_new(struct net_context *context,
1113 struct net_pkt *pkt,
1114 const struct net_in6_addr *src,
1115 const struct net_in6_addr *dst)
1116{
1117 ARG_UNUSED(context);
1118 ARG_UNUSED(pkt);
1119 ARG_UNUSED(src);
1120 ARG_UNUSED(dst);
1121 return -1;
1122}
1123#endif /* CONFIG_NET_IPV6 */
1124
1136int net_context_bind(struct net_context *context,
1137 const struct net_sockaddr *addr,
1138 net_socklen_t addrlen);
1139
1151 int backlog);
1152
1182 const struct net_sockaddr *addr,
1183 net_socklen_t addrlen,
1185 k_timeout_t timeout,
1186 void *user_data);
1187
1215 k_timeout_t timeout,
1216 void *user_data);
1217
1237int net_context_send(struct net_context *context,
1238 const void *buf,
1239 size_t len,
1241 k_timeout_t timeout,
1242 void *user_data);
1243
1266 const void *buf,
1267 size_t len,
1268 const struct net_sockaddr *dst_addr,
1269 net_socklen_t addrlen,
1271 k_timeout_t timeout,
1272 void *user_data);
1273
1295 const struct net_msghdr *msghdr,
1296 int flags,
1298 k_timeout_t timeout,
1299 void *user_data);
1300
1337int net_context_recv(struct net_context *context,
1339 k_timeout_t timeout,
1340 void *user_data);
1341
1363 int32_t delta);
1364
1408
1420 enum net_context_option option,
1421 const void *value, uint32_t len);
1422
1434 enum net_context_option option,
1435 void *value, uint32_t *len);
1436
1444typedef void (*net_context_cb_t)(struct net_context *context, void *user_data);
1445
1454
1475#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
1476static inline void net_context_setup_pools(struct net_context *context,
1478 net_pkt_get_pool_func_t data_pool)
1479{
1480 NET_ASSERT(context);
1481
1482 context->tx_slab = tx_slab;
1483 context->data_pool = data_pool;
1484}
1485#else
1486#define net_context_setup_pools(context, tx_pool, data_pool)
1487#endif
1488
1503 uint16_t local_port, const struct net_sockaddr *local_addr);
1504
1505#ifdef __cplusplus
1506}
1507#endif
1508
1512
1513#endif /* ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_ */
Header file for the Atomic operations API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
#define NET_AF_PACKET
Packet family.
Definition net_ip.h:57
#define NET_AF_CAN
Controller Area Network.
Definition net_ip.h:58
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:172
#define NET_AF_INET6
IP protocol family version 6.
Definition net_ip.h:56
#define NET_AF_UNSPEC
Unspecified address family.
Definition net_ip.h:54
unsigned short int net_sa_family_t
Socket address family type.
Definition net_ip.h:169
net_sock_type
Socket type.
Definition net_ip.h:89
#define NET_AF_INET
IP protocol family version 4.
Definition net_ip.h:55
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:64
@ NET_SOCK_STREAM
Stream socket type.
Definition net_ip.h:90
@ NET_SOCK_RAW
RAW socket type.
Definition net_ip.h:92
@ NET_SOCK_DGRAM
Datagram socket type.
Definition net_ip.h:91
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:609
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:871
int net_context_unref(struct net_context *context)
Decrement the reference count to a network context.
static void net_context_set_iface(struct net_context *context, struct net_if *iface)
Set network interface for this context.
Definition net_context.h:731
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:780
static void net_context_signal_linger(struct net_context *context)
Signal that the connection backing a context has been closed by the transport, so a close() blocked o...
Definition net_context.h:1062
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:163
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.
int net_context_bind(struct net_context *context, const struct net_sockaddr *addr, net_socklen_t addrlen)
Assign a socket a local address.
static enum net_sock_type net_context_get_type(struct net_context *context)
Get context type for this network context.
Definition net_context.h:593
static bool net_context_is_bound_to_iface(struct net_context *context)
Is this context bound to a network interface.
Definition net_context.h:489
static void net_context_bind_iface(struct net_context *context, struct net_if *iface)
Bind network interface to this context.
Definition net_context.h:747
static void net_context_set_family(struct net_context *context, net_sa_family_t family)
Set address family for this network context.
Definition net_context.h:566
int net_context_get(net_sa_family_t family, enum net_sock_type type, uint16_t ip_proto, struct net_context **context)
Get network context.
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:178
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:796
int net_context_set_option(struct net_context *context, enum net_context_option option, const void *value, uint32_t len)
Set an connection option for this context.
static bool net_context_is_used(struct net_context *context)
Is this context used or not.
Definition net_context.h:475
int net_context_ref(struct net_context *context)
Take a reference count to a net_context, preventing destruction.
static net_sa_family_t net_context_get_family(struct net_context *context)
Get address family for this network context.
Definition net_context.h:550
static enum net_context_state net_context_get_state(struct net_context *context)
Get state for this network context.
Definition net_context.h:513
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:641
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:981
#define NET_CONTEXT_IN_USE
Is this context used or not.
Definition net_context.h:40
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:666
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:858
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:887
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:957
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:766
#define NET_CONTEXT_BOUND_TO_IFACE
Context is bound to a specific interface.
Definition net_context.h:71
void(* net_context_cb_t)(struct net_context *context, void *user_data)
Callback used while iterating over network contexts.
Definition net_context.h:1444
static int net_context_create_ipv6_new(struct net_context *context, struct net_pkt *pkt, const struct net_in6_addr *src, const struct net_in6_addr *dst)
Create IPv6 packet in provided net_pkt from context.
Definition net_context.h:1112
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:530
net_context_option
Network context options.
Definition net_context.h:1366
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:810
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:116
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_get_option(struct net_context *context, enum net_context_option option, void *value, uint32_t *len)
Get connection option value for this context.
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.
#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:62
#define NET_CONTEXT_TYPE
Type of the connection (datagram / stream / raw).
Definition net_context.h:65
bool net_context_port_in_use(enum net_ip_protocol ip_proto, uint16_t local_port, const struct net_sockaddr *local_addr)
Check if a port is in use (bound).
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:699
#define net_context_setup_pools(context, tx_pool, data_pool)
Set custom network buffer pools for context send operations.
Definition net_context.h:1486
static uint16_t net_context_get_proto(struct net_context *context)
Get context IP protocol for this network context.
Definition net_context.h:684
int net_context_connect(struct net_context *context, const struct net_sockaddr *addr, net_socklen_t addrlen, net_context_connect_cb_t cb, k_timeout_t timeout, void *user_data)
Create a network connection.
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:191
static int net_context_create_ipv4_new(struct net_context *context, struct net_pkt *pkt, const struct net_in_addr *src, const struct net_in_addr *dst)
Create IPv4 packet in provided net_pkt from context.
Definition net_context.h:1087
void(* net_tcp_accept_cb_t)(struct net_context *new_context, struct net_sockaddr *addr, net_socklen_t addrlen, int status, void *user_data)
Accept callback.
Definition net_context.h:136
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:901
int net_context_sendmsg(struct net_context *context, const struct net_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 net_msghdr struct.
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:95
int net_context_sendto(struct net_context *context, const void *buf, size_t len, const struct net_sockaddr *dst_addr, net_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:716
@ NET_OPT_UDP_OPT_MDS
UDP options: Maximum Datagram Size.
Definition net_context.h:1397
@ NET_OPT_UDP_OPT_MRDS
UDP options: Maximum Reassembled Datagram Size.
Definition net_context.h:1398
@ NET_OPT_ADDR_PREFERENCES
IPv6 address preference.
Definition net_context.h:1383
@ NET_OPT_MCAST_TTL
IPv4 multicast TTL.
Definition net_context.h:1379
@ NET_OPT_REUSEADDR
Re-use address.
Definition net_context.h:1375
@ NET_OPT_SNDBUF
Send buffer.
Definition net_context.h:1373
@ NET_OPT_IPV4_MCAST_LOOP
IPV4 multicast loop.
Definition net_context.h:1389
@ NET_OPT_RECV_PKTINFO
Receive packet information.
Definition net_context.h:1378
@ NET_OPT_RECV_HOPLIMIT
Receive hop limit information.
Definition net_context.h:1390
@ NET_OPT_LOCAL_PORT_RANGE
Clamp local port range.
Definition net_context.h:1387
@ NET_OPT_PRIORITY
Context priority.
Definition net_context.h:1367
@ NET_OPT_REUSEPORT
Re-use port.
Definition net_context.h:1376
@ NET_OPT_MCAST_HOP_LIMIT
IPv6 multicast hop limit.
Definition net_context.h:1380
@ NET_OPT_UDP_OPT_TIME
UDP options: Timestamp.
Definition net_context.h:1401
@ NET_OPT_RCVTIMEO
Receive timeout.
Definition net_context.h:1370
@ NET_OPT_UDP_OPT_REQ
UDP options: Echo Request.
Definition net_context.h:1399
@ NET_OPT_UDP_OPT_APC
UDP options: Additional Payload Checksum.
Definition net_context.h:1395
@ NET_OPT_TXTIME
TX time.
Definition net_context.h:1368
@ NET_OPT_SNDTIMEO
Send timeout.
Definition net_context.h:1371
@ NET_OPT_UDP_OPT_UEXP
UDP options: UNSAFE Experimental.
Definition net_context.h:1406
@ NET_OPT_MCAST_IFINDEX
IPv6 multicast output network interface index.
Definition net_context.h:1385
@ NET_OPT_UDP_OPT_UCMP
UDP options: UNSAFE Compression.
Definition net_context.h:1404
@ NET_OPT_UDP_OPT_FRAG
UDP options: Fragmentation.
Definition net_context.h:1396
@ NET_OPT_UDP_OPT_OCS
UDP options: Option Checksum.
Definition net_context.h:1394
@ NET_OPT_SOCKS5
SOCKS5.
Definition net_context.h:1369
@ NET_OPT_UDP_OPT_EXP
UDP options: Experimental.
Definition net_context.h:1403
@ NET_OPT_IPV6_MCAST_LOOP
IPV6 multicast loop.
Definition net_context.h:1388
@ NET_OPT_UDP_OPT_AUTH
UDP options: Authentication.
Definition net_context.h:1402
@ NET_OPT_TTL
IPv4 unicast TTL.
Definition net_context.h:1382
@ NET_OPT_RCVBUF
Receive buffer.
Definition net_context.h:1372
@ NET_OPT_UDP_OPT
UDP options master enable (RFC 9868).
Definition net_context.h:1393
@ NET_OPT_DONT_FRAGMENT
Disable local IP fragmentation.
Definition net_context.h:1391
@ NET_OPT_MTU
IPv4 socket path MTU.
Definition net_context.h:1386
@ NET_OPT_UNICAST_HOP_LIMIT
IPv6 unicast hop limit.
Definition net_context.h:1381
@ NET_OPT_LINGER
Socket linger (SO_LINGER).
Definition net_context.h:1392
@ NET_OPT_DSCP_ECN
DSCP ECN.
Definition net_context.h:1374
@ NET_OPT_UDP_OPT_RES
UDP options: Echo Response.
Definition net_context.h:1400
@ NET_OPT_UDP_OPT_UENC
UDP options: UNSAFE Encryption.
Definition net_context.h:1405
@ NET_OPT_TIMESTAMPING
Packet timestamping.
Definition net_context.h:1384
@ NET_OPT_IPV6_V6ONLY
Share IPv4 and IPv6 port space.
Definition net_context.h:1377
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.
void k_sem_give(struct k_sem *sem)
Give a semaphore.
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
Kernel condition variable structure.
Definition kernel.h:3618
Kernel FIFO structure.
Definition kernel.h:2948
Kernel mutex structure.
Definition kernel.h:3497
Semaphore structure.
Definition kernel.h:3723
Kernel timeout type.
Definition clock.h:65
Network buffer pool representation.
Definition net_buf.h:1151
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:203
atomic_t refcount
Reference count.
Definition net_context.h:214
struct net_sockaddr remote
Remote endpoint address.
Definition net_context.h:228
void * user_data
User data associated with a context.
Definition net_context.h:210
void * fifo_reserved
First member of the structure to allow to put contexts into a FIFO.
Definition net_context.h:206
uint16_t flags
Flags for the context.
Definition net_context.h:444
uint8_t ipv4_mcast_ttl
IPv4 multicast TTL.
Definition net_context.h:457
net_context_send_cb_t send_cb
Send callback to be called when the packet has been sent successfully.
Definition net_context.h:241
struct k_mutex lock
Internal lock for protecting this context from multiple access.
Definition net_context.h:218
struct net_sockaddr_ptr local
Local endpoint address.
Definition net_context.h:223
uint8_t ipv4_ttl
IPv4 TTL.
Definition net_context.h:456
uint8_t ipv6_mcast_hop_limit
IPv6 multicast hop limit.
Definition net_context.h:453
net_context_connect_cb_t connect_cb
Connect callback to be called when a connection has been established.
Definition net_context.h:246
struct net_conn_handle * conn_handler
Connection handle.
Definition net_context.h:231
uint16_t proto
Protocol (UDP, TCP or IEEE 802.3 protocol value).
Definition net_context.h:441
int8_t iface
Network interface assigned to this context.
Definition net_context.h:447
void * tcp
TCP connection information.
Definition net_context.h:260
struct net_context::@301222043044136136127251054270033130365030331345 options
Option values.
net_context_recv_cb_t recv_cb
Receive callback to be called when desired packet has been received.
Definition net_context.h:236
uint8_t ipv6_hop_limit
IPv6 hop limit.
Definition net_context.h:452
Network Interface structure.
Definition net_if.h:746
IPv6 address struct.
Definition net_ip.h:144
IPv4 address struct.
Definition net_ip.h:156
Message struct.
Definition net_ip.h:295
Network packet.
Definition net_pkt.h:119
Generic sockaddr struct.
Definition net_ip.h:462