16#ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
17#define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
41#define NET_CONTEXT_IN_USE BIT(0)
46enum net_context_state {
48 NET_CONTEXT_UNCONNECTED = 0,
49 NET_CONTEXT_CONFIGURING = 1,
50 NET_CONTEXT_CONNECTING = 1,
51 NET_CONTEXT_READY = 2,
52 NET_CONTEXT_CONNECTED = 2,
53 NET_CONTEXT_LISTENING = 3,
63#define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
66#define NET_CONTEXT_TYPE (BIT(6) | BIT(7))
69#define NET_CONTEXT_REMOTE_ADDR_SET BIT(8)
72#define NET_CONTEXT_BOUND_TO_IFACE BIT(9)
75#define NET_CONTEXT_LOCAL_ADDR_SET BIT(10)
101 union net_ip_header *ip_hdr,
102 union net_proto_header *proto_hdr,
182typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
195typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
199struct net_conn_handle;
262#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
272#if defined(CONFIG_NET_TCP)
277#if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
281 struct k_sem recv_data_wait;
284#if defined(CONFIG_NET_CONTEXT_LINGER)
289 struct k_sem linger_sem;
292#if defined(CONFIG_NET_SOCKETS)
317#if defined(CONFIG_NET_OFFLOAD)
319 void *offload_context;
322#if defined(CONFIG_NET_SOCKETS_CAN)
328#if defined(CONFIG_NET_CONTEXT_PRIORITY)
332#if defined(CONFIG_NET_CONTEXT_TXTIME)
336#if defined(CONFIG_SOCKS)
339 struct net_sockaddr addr;
343#if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
357#if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
359 k_timeout_t rcvtimeo;
361#if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
363 k_timeout_t sndtimeo;
365#if defined(CONFIG_NET_CONTEXT_RCVBUF)
369#if defined(CONFIG_NET_CONTEXT_SNDBUF)
373#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
380#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
384#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
388#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
392#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
396#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
400#if defined(CONFIG_NET_IPV6)
407#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
423 bool ipv6_mcast_loop;
424 bool ipv4_mcast_loop;
431#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
436#if defined(CONFIG_NET_CONTEXT_LINGER)
441 struct net_linger linger;
444#if defined(CONFIG_NET_UDP_OPTIONS)
455 struct net_udp_opt_mrds mrds;
456#if defined(CONFIG_NET_UDP_OPTIONS_DPLPMTUD)
460 struct net_dplpmtud_path path;
462 struct k_work_delayable timer;
474 struct k_work echo_work;
476 struct net_sockaddr_storage echo_peer;
514#if defined(CONFIG_SOCKS)
566#define NET_CONTEXT_STATE_SHIFT 1
567#define NET_CONTEXT_STATE_MASK 0x03
585 return (
enum net_context_state)
586 ((context->
flags >> NET_CONTEXT_STATE_SHIFT) &
587 NET_CONTEXT_STATE_MASK);
599 enum net_context_state
state)
603 context->
flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
604 context->
flags |= ((
state & NET_CONTEXT_STATE_MASK) <<
605 NET_CONTEXT_STATE_SHIFT);
647 context->
flags |= flag;
689 context->
flags |= flag;
700#if defined(CONFIG_NET_SOCKETS_CAN)
706 context->can_filter_id = filter_id;
713 ARG_UNUSED(filter_id);
726#if defined(CONFIG_NET_SOCKETS_CAN)
731 return context->can_filter_id;
754 return context->
proto;
770 context->
proto = proto;
884#if defined(CONFIG_NET_IPV4)
895static inline bool net_context_get_ipv4_mcast_loop(
struct net_context *context)
897 return context->
options.ipv4_mcast_loop;
909static inline void net_context_set_ipv4_mcast_loop(
struct net_context *context,
910 bool ipv4_mcast_loop)
912 context->
options.ipv4_mcast_loop = ipv4_mcast_loop;
975#if defined(CONFIG_NET_IPV6)
987static inline bool net_context_get_ipv6_mcast_loop(
struct net_context *context)
989 return context->
options.ipv6_mcast_loop;
1001static inline void net_context_set_ipv6_mcast_loop(
struct net_context *context,
1002 bool ipv6_mcast_loop)
1004 context->
options.ipv6_mcast_loop = ipv6_mcast_loop;
1018#if defined(CONFIG_SOCKS)
1022 context->proxy_enabled = enable;
1028 ARG_UNUSED(context);
1043#if defined(CONFIG_SOCKS)
1046 return context->proxy_enabled;
1051 ARG_UNUSED(context);
1132#if defined(CONFIG_NET_CONTEXT_LINGER)
1135 ARG_UNUSED(context);
1149#if defined(CONFIG_NET_IPV4)
1174#if defined(CONFIG_NET_IPV6)
1185 ARG_UNUSED(context);
1518#if defined(CONFIG_NET_UDP_OPTIONS_DPLPMTUD) || defined(__DOXYGEN__)
1524 ARG_UNUSED(context);
1569#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
1574 NET_ASSERT(context);
1576 context->tx_slab = tx_slab;
1577 context->data_pool = data_pool;
1580#define net_context_setup_pools(context, tx_slab, data_pool)
Datagram PLPMTUD transport-facing 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:677
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:939
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:799
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:848
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:1130
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:167
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:661
static bool net_context_is_bound_to_iface(struct net_context *context)
Is this context bound to a network interface.
Definition net_context.h:542
static void net_context_bind_iface(struct net_context *context, struct net_if *iface)
Bind network interface to this context.
Definition net_context.h:815
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:634
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:182
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:864
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.
#define net_context_setup_pools(context, tx_slab, data_pool)
Set custom network buffer pools for context send operations.
Definition net_context.h:1580
static bool net_context_is_used(struct net_context *context)
Is this context used or not.
Definition net_context.h:528
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:618
static enum net_context_state net_context_get_state(struct net_context *context)
Get state for this network context.
Definition net_context.h:581
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:709
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:1049
#define NET_CONTEXT_IN_USE
Is this context used or not.
Definition net_context.h:41
#define NET_CONTEXT_LOCAL_ADDR_SET
A local address has been bound to this context.
Definition net_context.h:75
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:734
static bool net_context_is_local_addr_set(struct net_context *context)
Has a local address been bound to this context.
Definition net_context.h:557
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:926
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:955
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:1025
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:834
#define NET_CONTEXT_BOUND_TO_IFACE
Context is bound to a specific interface.
Definition net_context.h:72
void(* net_context_cb_t)(struct net_context *context, void *user_data)
Callback used while iterating over network contexts.
Definition net_context.h:1538
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:1180
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:598
net_context_option
Network context options.
Definition net_context.h:1434
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:878
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:120
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:63
#define NET_CONTEXT_TYPE
Type of the connection (datagram / stream / raw).
Definition net_context.h:66
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:767
static uint16_t net_context_get_proto(struct net_context *context)
Get context IP protocol for this network context.
Definition net_context.h:752
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:195
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:1155
int net_context_set_udp_dplpmtud(struct net_context *context, bool enable)
Enable or disable DPLPMTUD (RFC 9869) on a UDP context.
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:140
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:969
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:99
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:784
@ NET_OPT_UDP_OPT_MDS
UDP options: Maximum Datagram Size.
Definition net_context.h:1465
@ NET_OPT_UDP_OPT_MRDS
UDP options: Maximum Reassembled Datagram Size.
Definition net_context.h:1466
@ NET_OPT_ADDR_PREFERENCES
IPv6 address preference.
Definition net_context.h:1451
@ NET_OPT_MCAST_TTL
IPv4 multicast TTL.
Definition net_context.h:1447
@ NET_OPT_REUSEADDR
Re-use address.
Definition net_context.h:1443
@ NET_OPT_SNDBUF
Send buffer.
Definition net_context.h:1441
@ NET_OPT_IPV4_MCAST_LOOP
IPV4 multicast loop.
Definition net_context.h:1457
@ NET_OPT_RECV_PKTINFO
Receive packet information.
Definition net_context.h:1446
@ NET_OPT_RECV_HOPLIMIT
Receive hop limit information.
Definition net_context.h:1458
@ NET_OPT_LOCAL_PORT_RANGE
Clamp local port range.
Definition net_context.h:1455
@ NET_OPT_UDP_OPT_DPLPMTUD
UDP options: DPLPMTUD enable (RFC 9869).
Definition net_context.h:1475
@ NET_OPT_PRIORITY
Context priority.
Definition net_context.h:1435
@ NET_OPT_REUSEPORT
Re-use port.
Definition net_context.h:1444
@ NET_OPT_MCAST_HOP_LIMIT
IPv6 multicast hop limit.
Definition net_context.h:1448
@ NET_OPT_UDP_OPT_TIME
UDP options: Timestamp.
Definition net_context.h:1469
@ NET_OPT_RCVTIMEO
Receive timeout.
Definition net_context.h:1438
@ NET_OPT_UDP_OPT_REQ
UDP options: Echo Request.
Definition net_context.h:1467
@ NET_OPT_UDP_OPT_APC
UDP options: Additional Payload Checksum.
Definition net_context.h:1463
@ NET_OPT_TXTIME
TX time.
Definition net_context.h:1436
@ NET_OPT_SNDTIMEO
Send timeout.
Definition net_context.h:1439
@ NET_OPT_UDP_OPT_UEXP
UDP options: UNSAFE Experimental.
Definition net_context.h:1474
@ NET_OPT_MCAST_IFINDEX
IPv6 multicast output network interface index.
Definition net_context.h:1453
@ NET_OPT_UDP_OPT_UCMP
UDP options: UNSAFE Compression.
Definition net_context.h:1472
@ NET_OPT_UDP_OPT_FRAG
UDP options: Fragmentation.
Definition net_context.h:1464
@ NET_OPT_UDP_OPT_OCS
UDP options: Option Checksum.
Definition net_context.h:1462
@ NET_OPT_SOCKS5
SOCKS5.
Definition net_context.h:1437
@ NET_OPT_UDP_OPT_EXP
UDP options: Experimental.
Definition net_context.h:1471
@ NET_OPT_IPV6_MCAST_LOOP
IPV6 multicast loop.
Definition net_context.h:1456
@ NET_OPT_UDP_OPT_AUTH
UDP options: Authentication.
Definition net_context.h:1470
@ NET_OPT_TTL
IPv4 unicast TTL.
Definition net_context.h:1450
@ NET_OPT_RCVBUF
Receive buffer.
Definition net_context.h:1440
@ NET_OPT_UDP_OPT
UDP options master enable (RFC 9868).
Definition net_context.h:1461
@ NET_OPT_DONT_FRAGMENT
Disable local IP fragmentation.
Definition net_context.h:1459
@ NET_OPT_MTU
IPv4 socket path MTU.
Definition net_context.h:1454
@ NET_OPT_UNICAST_HOP_LIMIT
IPv6 unicast hop limit.
Definition net_context.h:1449
@ NET_OPT_LINGER
Socket linger (SO_LINGER).
Definition net_context.h:1460
@ NET_OPT_DSCP_ECN
DSCP ECN.
Definition net_context.h:1442
@ NET_OPT_UDP_OPT_RES
UDP options: Echo Response.
Definition net_context.h:1468
@ NET_OPT_UDP_OPT_UENC
UDP options: UNSAFE Encryption.
Definition net_context.h:1473
@ NET_OPT_TIMESTAMPING
Packet timestamping.
Definition net_context.h:1452
@ NET_OPT_IPV6_V6ONLY
Share IPv4 and IPv6 port space.
Definition net_context.h:1445
@ NET_OPT_UDP_OPT_DPLPMTUD_APP_RESPOND
UDP options: app echoes RES itself.
Definition net_context.h:1476
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.
#define ENOTSUP
Unsupported value.
Definition errno.h:115
Public API for network interface.
IPv6 and IPv4 definitions.
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:3693
Kernel FIFO structure.
Definition kernel.h:3023
Kernel mutex structure.
Definition kernel.h:3572
Semaphore structure.
Definition kernel.h:3798
Kernel timeout type.
Definition clock.h:65
Network buffer pool representation.
Definition net_buf.h:1184
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:207
atomic_t refcount
Reference count.
Definition net_context.h:218
struct net_sockaddr remote
Remote endpoint address.
Definition net_context.h:239
void * user_data
User data associated with a context.
Definition net_context.h:214
void * fifo_reserved
First member of the structure to allow to put contexts into a FIFO.
Definition net_context.h:210
uint16_t flags
Flags for the context.
Definition net_context.h:497
uint8_t ipv4_mcast_ttl
IPv4 multicast TTL.
Definition net_context.h:510
net_context_send_cb_t send_cb
Send callback to be called when the packet has been sent successfully.
Definition net_context.h:255
struct net_context::@372151270134046060361377361306332237353327362170 options
Option values.
struct net_sockaddr_storage local_storage
Storage-typed alias of the local endpoint address.
Definition net_context.h:231
struct k_mutex lock
Internal lock for protecting this context from multiple access.
Definition net_context.h:222
struct net_sockaddr local
Local endpoint address.
Definition net_context.h:229
struct net_sockaddr_storage remote_storage
Storage-typed alias of the remote endpoint address.
Definition net_context.h:241
uint8_t ipv4_ttl
IPv4 TTL.
Definition net_context.h:509
uint8_t ipv6_mcast_hop_limit
IPv6 multicast hop limit.
Definition net_context.h:506
net_context_connect_cb_t connect_cb
Connect callback to be called when a connection has been established.
Definition net_context.h:260
struct net_conn_handle * conn_handler
Connection handle.
Definition net_context.h:245
uint16_t proto
Protocol (UDP, TCP or IEEE 802.3 protocol value).
Definition net_context.h:494
int8_t iface
Network interface assigned to this context.
Definition net_context.h:500
void * tcp
TCP connection information.
Definition net_context.h:274
net_context_recv_cb_t recv_cb
Receive callback to be called when desired packet has been received.
Definition net_context.h:250
uint8_t ipv6_hop_limit
IPv6 hop limit.
Definition net_context.h:505
Network Interface structure.
Definition net_if.h:764
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:455
Header file for the Atomic operations API.