16#ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
17#define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
40#define NET_CONTEXT_IN_USE BIT(0)
45enum net_context_state {
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,
62#define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
65#define NET_CONTEXT_TYPE (BIT(6) | BIT(7))
68#define NET_CONTEXT_REMOTE_ADDR_SET BIT(8)
71#define NET_CONTEXT_ACCEPTING_SOCK BIT(9)
74#define NET_CONTEXT_CLOSING_SOCK BIT(10)
77#define NET_CONTEXT_BOUND_TO_IFACE BIT(11)
103 union net_ip_header *ip_hdr,
104 union net_proto_header *proto_hdr,
184typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
197typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
201struct net_conn_handle;
254#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
264#if defined(CONFIG_NET_TCP)
269#if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
273 struct k_sem recv_data_wait;
276#if defined(CONFIG_NET_SOCKETS)
295#if defined(CONFIG_NET_OFFLOAD)
297 void *offload_context;
300#if defined(CONFIG_NET_SOCKETS_CAN)
306#if defined(CONFIG_NET_CONTEXT_PRIORITY)
310#if defined(CONFIG_NET_CONTEXT_TXTIME)
314#if defined(CONFIG_SOCKS)
317 struct sockaddr addr;
321#if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
335#if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
337 k_timeout_t rcvtimeo;
339#if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
341 k_timeout_t sndtimeo;
343#if defined(CONFIG_NET_CONTEXT_RCVBUF)
347#if defined(CONFIG_NET_CONTEXT_SNDBUF)
351#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
358#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
362#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
366#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
370#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
374#if defined(CONFIG_NET_CONTEXT_RECV_HOPLIMIT)
378#if defined(CONFIG_NET_IPV6)
385#if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
401 bool ipv6_mcast_loop;
402 bool ipv4_mcast_loop;
406#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
433#if defined(CONFIG_SOCKS)
534#define NET_CONTEXT_STATE_SHIFT 1
535#define NET_CONTEXT_STATE_MASK 0x03
553 return (
enum net_context_state)
554 ((context->
flags >> NET_CONTEXT_STATE_SHIFT) &
555 NET_CONTEXT_STATE_MASK);
567 enum net_context_state
state)
571 context->
flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
572 context->
flags |= ((
state & NET_CONTEXT_STATE_MASK) <<
573 NET_CONTEXT_STATE_SHIFT);
615 context->
flags |= flag;
657 context->
flags |= flag;
668#if defined(CONFIG_NET_SOCKETS_CAN)
674 context->can_filter_id = filter_id;
681 ARG_UNUSED(filter_id);
694#if defined(CONFIG_NET_SOCKETS_CAN)
699 return context->can_filter_id;
722 return context->
proto;
738 context->
proto = proto;
852#if defined(CONFIG_NET_IPV4)
863static inline bool net_context_get_ipv4_mcast_loop(
struct net_context *context)
865 return context->
options.ipv4_mcast_loop;
877static inline void net_context_set_ipv4_mcast_loop(
struct net_context *context,
878 bool ipv4_mcast_loop)
880 context->
options.ipv4_mcast_loop = ipv4_mcast_loop;
943#if defined(CONFIG_NET_IPV6)
955static inline bool net_context_get_ipv6_mcast_loop(
struct net_context *context)
957 return context->
options.ipv6_mcast_loop;
969static inline void net_context_set_ipv6_mcast_loop(
struct net_context *context,
970 bool ipv6_mcast_loop)
972 context->
options.ipv6_mcast_loop = ipv6_mcast_loop;
986#if defined(CONFIG_SOCKS)
990 context->proxy_enabled = enable;
1011#if defined(CONFIG_SOCKS)
1014 return context->proxy_enabled;
1019 ARG_UNUSED(context);
1100#if defined(CONFIG_NET_IPV4)
1125#if defined(CONFIG_NET_IPV6)
1136 ARG_UNUSED(context);
1422 const void *value,
size_t len);
1436 void *value,
size_t *len);
1476#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
1481 NET_ASSERT(context);
1483 context->tx_slab = tx_slab;
1484 context->data_pool = data_pool;
1487#define net_context_setup_pools(context, tx_pool, data_pool)
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:645
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:907
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:767
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:816
void(* net_context_connect_cb_t)(struct net_context *context, int status, void *user_data)
Connection callback.
Definition net_context.h:169
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:629
static bool net_context_is_accepting(struct net_context *context)
Is this context is accepting data now.
Definition net_context.h:475
static sa_family_t net_context_get_family(struct net_context *context)
Get address family for this network context.
Definition net_context.h:586
static bool net_context_is_bound_to_iface(struct net_context *context)
Is this context bound to a network interface.
Definition net_context.h:461
static void net_context_bind_iface(struct net_context *context, struct net_if *iface)
Bind network interface to this context.
Definition net_context.h:783
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:184
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:832
static bool net_context_is_used(struct net_context *context)
Is this context used or not.
Definition net_context.h:447
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:549
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:677
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:1017
#define NET_CONTEXT_IN_USE
Is this context used or not.
Definition net_context.h:40
static void net_context_set_accepting(struct net_context *context, bool accepting)
Set this context to accept data now.
Definition net_context.h:488
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:602
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:1131
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:1106
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:702
#define NET_CONTEXT_CLOSING_SOCK
Is the socket closing / closed.
Definition net_context.h:74
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:894
static void net_context_set_closing(struct net_context *context, bool closing)
Set this context to closing.
Definition net_context.h:520
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:923
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:993
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:802
#define NET_CONTEXT_BOUND_TO_IFACE
Context is bound to a specific interface.
Definition net_context.h:77
void(* net_context_cb_t)(struct net_context *context, void *user_data)
Callback used while iterating over network contexts.
Definition net_context.h:1445
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:566
net_context_option
Network context options.
Definition net_context.h:1383
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:846
void(* net_context_send_cb_t)(struct net_context *context, int status, void *user_data)
Network data send callback.
Definition net_context.h:122
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:142
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:507
#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
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:735
#define net_context_setup_pools(context, tx_pool, data_pool)
Set custom network buffer pools for context send operations.
Definition net_context.h:1487
static uint16_t net_context_get_proto(struct net_context *context)
Get context IP protocol for this network context.
Definition net_context.h:720
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:197
#define NET_CONTEXT_ACCEPTING_SOCK
Is the socket accepting connections.
Definition net_context.h:71
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:937
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:101
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:752
@ NET_OPT_ADDR_PREFERENCES
IPv6 address preference.
Definition net_context.h:1400
@ NET_OPT_MCAST_TTL
IPv4 multicast TTL.
Definition net_context.h:1396
@ NET_OPT_REUSEADDR
Re-use address.
Definition net_context.h:1392
@ NET_OPT_SNDBUF
Send buffer.
Definition net_context.h:1390
@ NET_OPT_IPV4_MCAST_LOOP
IPV4 multicast loop.
Definition net_context.h:1406
@ NET_OPT_RECV_PKTINFO
Receive packet information.
Definition net_context.h:1395
@ NET_OPT_RECV_HOPLIMIT
Receive hop limit information.
Definition net_context.h:1407
@ NET_OPT_LOCAL_PORT_RANGE
Clamp local port range.
Definition net_context.h:1404
@ NET_OPT_PRIORITY
Context priority.
Definition net_context.h:1384
@ NET_OPT_REUSEPORT
Re-use port.
Definition net_context.h:1393
@ NET_OPT_MCAST_HOP_LIMIT
IPv6 multicast hop limit.
Definition net_context.h:1397
@ NET_OPT_RCVTIMEO
Receive timeout.
Definition net_context.h:1387
@ NET_OPT_TXTIME
TX time.
Definition net_context.h:1385
@ NET_OPT_SNDTIMEO
Send timeout.
Definition net_context.h:1388
@ NET_OPT_MCAST_IFINDEX
IPv6 multicast output network interface index.
Definition net_context.h:1402
@ NET_OPT_SOCKS5
SOCKS5.
Definition net_context.h:1386
@ NET_OPT_IPV6_MCAST_LOOP
IPV6 multicast loop.
Definition net_context.h:1405
@ NET_OPT_TTL
IPv4 unicast TTL.
Definition net_context.h:1399
@ NET_OPT_RCVBUF
Receive buffer.
Definition net_context.h:1389
@ NET_OPT_MTU
IPv4 socket path MTU.
Definition net_context.h:1403
@ NET_OPT_UNICAST_HOP_LIMIT
IPv6 unicast hop limit.
Definition net_context.h:1398
@ NET_OPT_DSCP_ECN
DSCP ECN.
Definition net_context.h:1391
@ NET_OPT_TIMESTAMPING
Packet timestamping.
Definition net_context.h:1401
@ NET_OPT_IPV6_V6ONLY
Share IPv4 and IPv6 port space.
Definition net_context.h:1394
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 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
IPv6 address struct.
Definition net_ip.h:143
IPv4 address struct.
Definition net_ip.h:155
Mutex Structure.
Definition kernel.h:3070
Semaphore structure.
Definition kernel.h:3275
Kernel timeout type.
Definition clock.h:65
Message struct.
Definition net_ip.h:257
Network buffer pool representation.
Definition net_buf.h:1079
Note that we do not store the actual source IP address in the context because the address is already ...
Definition net_context.h:209
atomic_t refcount
Reference count.
Definition net_context.h:220
void * user_data
User data associated with a context.
Definition net_context.h:216
void * fifo_reserved
First member of the structure to allow to put contexts into a FIFO.
Definition net_context.h:212
uint16_t flags
Flags for the context.
Definition net_context.h:416
uint8_t ipv4_mcast_ttl
IPv4 multicast TTL.
Definition net_context.h:429
net_context_send_cb_t send_cb
Send callback to be called when the packet has been sent successfully.
Definition net_context.h:247
struct sockaddr remote
Remote endpoint address.
Definition net_context.h:234
struct k_mutex lock
Internal lock for protecting this context from multiple access.
Definition net_context.h:224
struct sockaddr_ptr local
Local endpoint address.
Definition net_context.h:229
uint8_t ipv4_ttl
IPv4 TTL.
Definition net_context.h:428
uint8_t ipv6_mcast_hop_limit
IPv6 multicast hop limit.
Definition net_context.h:425
net_context_connect_cb_t connect_cb
Connect callback to be called when a connection has been established.
Definition net_context.h:252
struct net_conn_handle * conn_handler
Connection handle.
Definition net_context.h:237
uint16_t proto
Protocol (UDP, TCP or IEEE 802.3 protocol value)
Definition net_context.h:413
int8_t iface
Network interface assigned to this context.
Definition net_context.h:419
void * tcp
TCP connection information.
Definition net_context.h:266
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:242
uint8_t ipv6_hop_limit
IPv6 hop limit.
Definition net_context.h:424
Network Interface structure.
Definition net_if.h:726
Network packet.
Definition net_pkt.h:91
Generic sockaddr struct.
Definition net_ip.h:410