Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Application network context

Application network context. More...

Data Structures

struct  net_context
 Note that we do not store the actual source IP address in the context because the address is already be set in the network interface struct. More...
 

Macros

#define NET_CONTEXT_IN_USE   BIT(0)
 Is this context used or not.
 
#define NET_CONTEXT_FAMILY   (BIT(3) | BIT(4) | BIT(5))
 The address family, connection type and IP protocol are stored into a bit field to save space.
 
#define NET_CONTEXT_TYPE   (BIT(6) | BIT(7))
 Type of the connection (datagram / stream / raw)
 
#define NET_CONTEXT_REMOTE_ADDR_SET   BIT(8)
 Remote address set.
 
#define NET_CONTEXT_ACCEPTING_SOCK   BIT(9)
 Is the socket accepting connections.
 
#define NET_CONTEXT_CLOSING_SOCK   BIT(10)
 Is the socket closing / closed.
 
#define NET_CONTEXT_BOUND_TO_IFACE   BIT(11)
 Context is bound to a specific interface.
 
#define net_context_setup_pools(context, tx_pool, data_pool)
 Set custom network buffer pools for context send operations.
 

Typedefs

typedef 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.
 
typedef void(* net_context_send_cb_t) (struct net_context *context, int status, void *user_data)
 Network data send callback.
 
typedef void(* net_tcp_accept_cb_t) (struct net_context *new_context, struct sockaddr *addr, socklen_t addrlen, int status, void *user_data)
 Accept callback.
 
typedef void(* net_context_connect_cb_t) (struct net_context *context, int status, void *user_data)
 Connection callback.
 
typedef 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.
 
typedef 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.
 
typedef void(* net_context_cb_t) (struct net_context *context, void *user_data)
 Callback used while iterating over network contexts.
 

Enumerations

enum  net_context_option {
  NET_OPT_PRIORITY = 1 , NET_OPT_TXTIME = 2 , NET_OPT_SOCKS5 = 3 , NET_OPT_RCVTIMEO = 4 ,
  NET_OPT_SNDTIMEO = 5 , NET_OPT_RCVBUF = 6 , NET_OPT_SNDBUF = 7 , NET_OPT_DSCP_ECN = 8 ,
  NET_OPT_REUSEADDR = 9 , NET_OPT_REUSEPORT = 10 , NET_OPT_IPV6_V6ONLY = 11 , NET_OPT_RECV_PKTINFO = 12 ,
  NET_OPT_MCAST_TTL = 13 , NET_OPT_MCAST_HOP_LIMIT = 14 , NET_OPT_UNICAST_HOP_LIMIT = 15 , NET_OPT_TTL = 16 ,
  NET_OPT_ADDR_PREFERENCES = 17 , NET_OPT_TIMESTAMPING = 18
}
 Network context options. More...
 

Functions

static bool net_context_is_used (struct net_context *context)
 Is this context used or not.
 
static bool net_context_is_bound_to_iface (struct net_context *context)
 Is this context bound to a network interface.
 
static bool net_context_is_accepting (struct net_context *context)
 Is this context is accepting data now.
 
static void net_context_set_accepting (struct net_context *context, bool accepting)
 Set this context to accept data now.
 
static bool net_context_is_closing (struct net_context *context)
 Is this context closing.
 
static void net_context_set_closing (struct net_context *context, bool closing)
 Set this context to closing.
 
static enum net_context_state net_context_get_state (struct net_context *context)
 Get state for this network context.
 
static void net_context_set_state (struct net_context *context, enum net_context_state state)
 Set state for this network context.
 
static sa_family_t net_context_get_family (struct net_context *context)
 Get address family for this network context.
 
static void net_context_set_family (struct net_context *context, sa_family_t family)
 Set address family for this network context.
 
static enum net_sock_type net_context_get_type (struct net_context *context)
 Get context type for this network context.
 
static void net_context_set_type (struct net_context *context, enum net_sock_type type)
 Set context type for this network context.
 
static void net_context_set_can_filter_id (struct net_context *context, int filter_id)
 Set CAN filter id for this network context.
 
static int net_context_get_can_filter_id (struct net_context *context)
 Get CAN filter id for this network context.
 
static uint16_t net_context_get_proto (struct net_context *context)
 Get context IP protocol for this network context.
 
static void net_context_set_proto (struct net_context *context, uint16_t proto)
 Set context IP protocol for this network context.
 
static struct net_ifnet_context_get_iface (struct net_context *context)
 Get network interface for this context.
 
static void net_context_set_iface (struct net_context *context, struct net_if *iface)
 Set network interface for this context.
 
static void net_context_bind_iface (struct net_context *context, struct net_if *iface)
 Bind network interface to this context.
 
static uint8_t net_context_get_ipv4_ttl (struct net_context *context)
 Get IPv4 TTL (time-to-live) value for this context.
 
static void net_context_set_ipv4_ttl (struct net_context *context, uint8_t ttl)
 Set IPv4 TTL (time-to-live) value for this context.
 
static uint8_t net_context_get_ipv4_mcast_ttl (struct net_context *context)
 Get IPv4 multicast TTL (time-to-live) value for this context.
 
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.
 
static uint8_t net_context_get_ipv6_hop_limit (struct net_context *context)
 Get IPv6 hop limit value for this context.
 
static void net_context_set_ipv6_hop_limit (struct net_context *context, uint8_t hop_limit)
 Set IPv6 hop limit value for this context.
 
static uint8_t net_context_get_ipv6_mcast_hop_limit (struct net_context *context)
 Get IPv6 multicast hop limit 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.
 
static void net_context_set_proxy_enabled (struct net_context *context, bool enable)
 Enable or disable socks proxy support for this context.
 
static bool net_context_is_proxy_enabled (struct net_context *context)
 Is socks proxy support enabled or disabled for this context.
 
int net_context_get (sa_family_t family, enum net_sock_type type, uint16_t ip_proto, struct net_context **context)
 Get network context.
 
int net_context_put (struct net_context *context)
 Close and unref a network context.
 
int net_context_ref (struct net_context *context)
 Take a reference count to a net_context, preventing destruction.
 
int net_context_unref (struct net_context *context)
 Decrement the reference count to a network context.
 
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.
 
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.
 
int net_context_bind (struct net_context *context, const struct sockaddr *addr, socklen_t addrlen)
 Assign a socket a local address.
 
int net_context_listen (struct net_context *context, int backlog)
 Mark the context as a listening one.
 
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.
 
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_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.
 
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.
 
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_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.
 
int net_context_update_recv_wnd (struct net_context *context, int32_t delta)
 Update TCP receive window for 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.
 
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.
 
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.
 
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)
 

Detailed Description

Application network context.

Macro Definition Documentation

◆ NET_CONTEXT_ACCEPTING_SOCK

#define NET_CONTEXT_ACCEPTING_SOCK   BIT(9)

#include <include/zephyr/net/net_context.h>

Is the socket accepting connections.

◆ NET_CONTEXT_BOUND_TO_IFACE

#define NET_CONTEXT_BOUND_TO_IFACE   BIT(11)

#include <include/zephyr/net/net_context.h>

Context is bound to a specific interface.

◆ NET_CONTEXT_CLOSING_SOCK

#define NET_CONTEXT_CLOSING_SOCK   BIT(10)

#include <include/zephyr/net/net_context.h>

Is the socket closing / closed.

◆ NET_CONTEXT_FAMILY

#define NET_CONTEXT_FAMILY   (BIT(3) | BIT(4) | BIT(5))

#include <include/zephyr/net/net_context.h>

The address family, connection type and IP protocol are stored into a bit field to save space.

Protocol family of this connection

◆ NET_CONTEXT_IN_USE

#define NET_CONTEXT_IN_USE   BIT(0)

#include <include/zephyr/net/net_context.h>

Is this context used or not.

◆ NET_CONTEXT_REMOTE_ADDR_SET

#define NET_CONTEXT_REMOTE_ADDR_SET   BIT(8)

#include <include/zephyr/net/net_context.h>

Remote address set.

◆ net_context_setup_pools

#define net_context_setup_pools (   context,
  tx_pool,
  data_pool 
)

#include <include/zephyr/net/net_context.h>

Set custom network buffer pools for context send operations.

Set custom network buffer pools used by the IP stack to allocate network buffers used by the context when sending data to the network. Using dedicated buffers may help make send operations on a given context more reliable, e.g. not be subject to buffer starvation due to operations on other network contexts. Buffer pools are set per context, but several contexts may share the same buffers. Note that there's no support for per-context custom receive packet pools.

Parameters
contextContext that will use the given net_buf pools.
tx_poolPointer to the function that will return TX pool to the caller. The TX pool is used when sending data to network. There is one TX net_pkt for each network packet that is sent.
data_poolPointer to the function that will return DATA pool to the caller. The DATA pool is used to store data that is sent to the network.

◆ NET_CONTEXT_TYPE

#define NET_CONTEXT_TYPE   (BIT(6) | BIT(7))

#include <include/zephyr/net/net_context.h>

Type of the connection (datagram / stream / raw)

Typedef Documentation

◆ net_context_cb_t

net_context_cb_t

#include <include/zephyr/net/net_context.h>

Callback used while iterating over network contexts.

Parameters
contextA valid pointer on current network context
user_dataA valid pointer on some user data or NULL

◆ net_context_connect_cb_t

net_context_connect_cb_t

#include <include/zephyr/net/net_context.h>

Connection callback.

The connect callback is called after a connection is being established. For TCP connections, this callback is called by RX thread so its stack and execution context is used here. The callback is called after the TCP connection was established or if the connection failed. Keep processing in the callback minimal to reduce the time spent blocked while handling packets. For UDP connections, this callback is called immediately by net_context_connect() function. UDP is a connectionless protocol so the connection can be thought of being established immediately.

Parameters
contextThe context to use.
statusStatus of the connection establishment. This is 0 if the connection was established successfully, <0 if there was an error.
user_dataThe user data given in net_context_connect() call.

◆ net_context_recv_cb_t

net_context_recv_cb_t

#include <include/zephyr/net/net_context.h>

Network data receive callback.

The recv callback is called after a network data packet is received. This callback is called by RX thread so its stack and execution context is used here. Keep processing in the callback minimal to reduce the time spent blocked while handling packets.

Parameters
contextThe context to use.
pktNetwork buffer that is received. If the pkt is not NULL, then the callback will own the buffer and it needs to unref the pkt as soon as it has finished working with it. On EOF, pkt will be NULL.
ip_hdra pointer to relevant IP (v4 or v6) header.
proto_hdra pointer to relevant protocol (udp or tcp) header.
statusValue is set to 0 if some data or the connection is at EOF, <0 if there was an error receiving data, in this case the pkt parameter is set to NULL.
user_dataThe user data given in net_recv() call.

◆ net_context_send_cb_t

net_context_send_cb_t

#include <include/zephyr/net/net_context.h>

Network data send callback.

The send callback is called after a network data packet is sent. This callback is called by TX thread so its stack and execution context is used here. Keep processing in the callback minimal to reduce the time spent blocked while handling packets.

Parameters
contextThe context to use.
statusValue is set to >= 0: amount of data that was sent, < 0 there was an error sending data.
user_dataThe user data given in net_send() call.

◆ net_pkt_get_pool_func_t

net_pkt_get_pool_func_t

#include <include/zephyr/net/net_context.h>

Function that is called to get the pool that is used for net_buf allocations.

Returns
Pointer to valid struct net_buf_pool instance.

◆ net_pkt_get_slab_func_t

net_pkt_get_slab_func_t

#include <include/zephyr/net/net_context.h>

Function that is called to get the slab that is used for net_pkt allocations.

Returns
Pointer to valid struct k_mem_slab instance.

◆ net_tcp_accept_cb_t

net_tcp_accept_cb_t

#include <include/zephyr/net/net_context.h>

Accept callback.

The accept callback is called after a successful connection was established or if there was an error while we were waiting for a connection attempt. This callback is called by RX thread so its stack and execution context is used here. Keep processing in the callback minimal to reduce the time spent blocked while handling packets.

Parameters
new_contextThe context to use.
addrThe peer address.
addrlenLength of the peer address.
statusThe status code, 0 on success, < 0 otherwise
user_dataThe user data given in net_context_accept() call.

Enumeration Type Documentation

◆ net_context_option

#include <include/zephyr/net/net_context.h>

Network context options.

These map to BSD socket option values.

Enumerator
NET_OPT_PRIORITY 

Context priority.

NET_OPT_TXTIME 

TX time.

NET_OPT_SOCKS5 

SOCKS5.

NET_OPT_RCVTIMEO 

Receive timeout.

NET_OPT_SNDTIMEO 

Send timeout.

NET_OPT_RCVBUF 

Receive buffer.

NET_OPT_SNDBUF 

Send buffer.

NET_OPT_DSCP_ECN 

DSCP ECN.

NET_OPT_REUSEADDR 

Re-use address.

NET_OPT_REUSEPORT 

Re-use port.

NET_OPT_IPV6_V6ONLY 

Share IPv4 and IPv6 port space.

NET_OPT_RECV_PKTINFO 

Receive packet information.

NET_OPT_MCAST_TTL 

IPv4 multicast TTL.

NET_OPT_MCAST_HOP_LIMIT 

IPv6 multicast hop limit.

NET_OPT_UNICAST_HOP_LIMIT 

IPv6 unicast hop limit.

NET_OPT_TTL 

IPv4 unicast TTL.

NET_OPT_ADDR_PREFERENCES 

IPv6 address preference.

NET_OPT_TIMESTAMPING 

Packet timestamping.

Function Documentation

◆ net_context_accept()

int net_context_accept ( struct net_context context,
net_tcp_accept_cb_t  cb,
k_timeout_t  timeout,
void *  user_data 
)

#include <include/zephyr/net/net_context.h>

Accept a network connection attempt.

Accept a connection being established. This function will return immediately if the timeout is set to K_NO_WAIT. In this case the context will call the supplied callback when ever there is a connection established to this context. This is "a register handler and forget" type of call (async). If the timeout is set to K_FOREVER, the function will wait until the connection is established. Timeout value > 0, will wait as many ms. After the connection is established a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return in this case. The callback is not called if the timeout expires. This is similar as BSD accept() function.

Parameters
contextThe context to use.
cbCaller-supplied callback function.
timeoutTimeout for the connection. Possible values are K_FOREVER, K_NO_WAIT, >0.
user_dataCaller-supplied user data.
Returns
0 if ok, < 0 if error

◆ net_context_bind()

int net_context_bind ( struct net_context context,
const struct sockaddr addr,
socklen_t  addrlen 
)

#include <include/zephyr/net/net_context.h>

Assign a socket a local address.

This is similar as BSD bind() function.

Parameters
contextThe context to be assigned.
addrAddress to assigned.
addrlenLength of the address.
Returns
0 if ok, < 0 if error

◆ net_context_bind_iface()

static void net_context_bind_iface ( struct net_context context,
struct net_if iface 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Bind network interface to this context.

This function binds network interface to this context.

Parameters
contextNetwork context.
ifaceNetwork interface.

◆ net_context_connect()

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 
)

#include <include/zephyr/net/net_context.h>

Create a network connection.

The net_context_connect function creates a network connection to the host specified by addr. After the connection is established, the user-supplied callback (cb) is executed. cb is called even if the timeout was set to K_FOREVER. cb is not called if the timeout expires. For datagram sockets (SOCK_DGRAM), this function only sets the peer address. This function is similar to the BSD connect() function.

Parameters
contextThe network context.
addrThe peer address to connect to.
addrlenPeer address length.
cbCallback function. Set to NULL if not required.
timeoutThe timeout value for the connection. Possible values:
  • K_NO_WAIT: this function will return immediately,
  • K_FOREVER: this function will block until the connection is established,
  • >0: this function will wait the specified ms.
user_dataData passed to the callback function.
Returns
0 on success.
-EINVAL if an invalid parameter is passed as an argument.
-ENOTSUP if the operation is not supported or implemented.
-ETIMEDOUT if the connect operation times out.

◆ net_context_create_ipv4_new()

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 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Create IPv4 packet in provided net_pkt from context.

Parameters
contextNetwork context for a connection
pktNetwork packet
srcSource address, or NULL to choose a default
dstDestination IPv4 address
Returns
Return 0 on success, negative errno otherwise.

◆ net_context_create_ipv6_new()

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 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Create IPv6 packet in provided net_pkt from context.

Parameters
contextNetwork context for a connection
pktNetwork packet
srcSource address, or NULL to choose a default from context
dstDestination IPv6 address
Returns
Return 0 on success, negative errno otherwise.

◆ net_context_foreach()

void net_context_foreach ( net_context_cb_t  cb,
void *  user_data 
)

#include <include/zephyr/net/net_context.h>

Go through all the network connections and call callback for each network context.

Parameters
cbUser-supplied callback function to call.
user_dataUser specified data.

◆ net_context_get()

int net_context_get ( sa_family_t  family,
enum net_sock_type  type,
uint16_t  ip_proto,
struct net_context **  context 
)

#include <include/zephyr/net/net_context.h>

Get network context.

Network context is used to define the connection 5-tuple (protocol, remote address, remote port, source address and source port). Random free port number will be assigned to source port when context is created. This is similar as BSD socket() function. The context will be created with a reference count of 1.

Parameters
familyIP address family (AF_INET or AF_INET6)
typeType of the socket, SOCK_STREAM or SOCK_DGRAM
ip_protoIP protocol, IPPROTO_UDP or IPPROTO_TCP. For raw socket access, the value is the L2 protocol value from IEEE 802.3 (see ethernet.h)
contextThe allocated context is returned to the caller.
Returns
0 if ok, < 0 if error

◆ net_context_get_can_filter_id()

static int net_context_get_can_filter_id ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get CAN filter id for this network context.

This function gets the CAN filter id of the context.

Parameters
contextNetwork context.
Returns
Filter id of this network context

◆ net_context_get_family()

static sa_family_t net_context_get_family ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get address family for this network context.

This function returns the address family (IPv4 or IPv6) of the context.

Parameters
contextNetwork context.
Returns
Network state.

◆ net_context_get_iface()

static struct net_if * net_context_get_iface ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get network interface for this context.

This function returns the used network interface.

Parameters
contextNetwork context.
Returns
Context network interface if context is bind to interface, NULL otherwise.

◆ net_context_get_ipv4_mcast_ttl()

static uint8_t net_context_get_ipv4_mcast_ttl ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get IPv4 multicast TTL (time-to-live) value for this context.

This function returns the IPv4 multicast TTL (time-to-live) value that is set to this context.

Parameters
contextNetwork context.
Returns
IPv4 multicast TTL value

◆ net_context_get_ipv4_ttl()

static uint8_t net_context_get_ipv4_ttl ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get IPv4 TTL (time-to-live) value for this context.

This function returns the IPv4 TTL (time-to-live) value that is set to this context.

Parameters
contextNetwork context.
Returns
IPv4 TTL value

◆ net_context_get_ipv6_hop_limit()

static uint8_t net_context_get_ipv6_hop_limit ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get IPv6 hop limit value for this context.

This function returns the IPv6 hop limit value that is set to this context.

Parameters
contextNetwork context.
Returns
IPv6 hop limit value

◆ net_context_get_ipv6_mcast_hop_limit()

static uint8_t net_context_get_ipv6_mcast_hop_limit ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get IPv6 multicast hop limit value for this context.

This function returns the IPv6 multicast hop limit value that is set to this context.

Parameters
contextNetwork context.
Returns
IPv6 multicast hop limit value

◆ net_context_get_option()

int net_context_get_option ( struct net_context context,
enum net_context_option  option,
void *  value,
size_t len 
)

#include <include/zephyr/net/net_context.h>

Get connection option value for this context.

Parameters
contextThe network context to use.
optionOption to set
valueOption value
lenOption length (returned to caller)
Returns
0 if ok, <0 if error

◆ net_context_get_proto()

static uint16_t net_context_get_proto ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get context IP protocol for this network context.

This function returns the IP protocol (UDP / TCP / IEEE 802.3 protocol value) of the context.

Parameters
contextNetwork context.
Returns
Network context IP protocol.

◆ net_context_get_state()

static enum net_context_state net_context_get_state ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get state for this network context.

This function returns the state of the context.

Parameters
contextNetwork context.
Returns
Network state.

◆ net_context_get_type()

static enum net_sock_type net_context_get_type ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Get context type for this network context.

This function returns the context type (stream, datagram or raw) of the context.

Parameters
contextNetwork context.
Returns
Network context type.

◆ net_context_is_accepting()

static bool net_context_is_accepting ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Is this context is accepting data now.

Parameters
contextNetwork context.
Returns
True if the context is accepting connections, False otherwise.

◆ net_context_is_bound_to_iface()

static bool net_context_is_bound_to_iface ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Is this context bound to a network interface.

Parameters
contextNetwork context.
Returns
True if the context is bound to network interface, False otherwise.

◆ net_context_is_closing()

static bool net_context_is_closing ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Is this context closing.

Parameters
contextNetwork context.
Returns
True if the context is closing, False otherwise.

◆ net_context_is_proxy_enabled()

static bool net_context_is_proxy_enabled ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Is socks proxy support enabled or disabled for this context.

This function returns current socks proxy status for this context.

Parameters
contextNetwork context.
Returns
True if socks proxy is enabled for this context, False otherwise

◆ net_context_is_used()

static bool net_context_is_used ( struct net_context context)
inlinestatic

#include <include/zephyr/net/net_context.h>

Is this context used or not.

Parameters
contextNetwork context.
Returns
True if the context is currently in use, False otherwise.

◆ net_context_listen()

int net_context_listen ( struct net_context context,
int  backlog 
)

#include <include/zephyr/net/net_context.h>

Mark the context as a listening one.

This is similar as BSD listen() function.

Parameters
contextThe context to use.
backlogThe size of the pending connections backlog.
Returns
0 if ok, < 0 if error

◆ net_context_port_in_use()

bool net_context_port_in_use ( enum net_ip_protocol  ip_proto,
uint16_t  local_port,
const struct sockaddr local_addr 
)

#include <include/zephyr/net/net_context.h>

Check if a port is in use (bound)

This function checks if a port is bound with respect to the specified ip_proto and local_addr.

Parameters
ip_protothe IP protocol
local_portthe port to check
local_addrthe network address
Returns
true if the port is bound
false if the port is not bound

◆ net_context_put()

int net_context_put ( struct net_context context)

#include <include/zephyr/net/net_context.h>

Close and unref a network context.

This releases the context. It is not possible to send or receive data via this context after this call. This is similar as BSD shutdown() function. For legacy compatibility, this function will implicitly decrement the reference count and possibly destroy the context either now or when it reaches a final state.

Parameters
contextThe context to be closed.
Returns
0 if ok, < 0 if error

◆ net_context_recv()

int net_context_recv ( struct net_context context,
net_context_recv_cb_t  cb,
k_timeout_t  timeout,
void *  user_data 
)

#include <include/zephyr/net/net_context.h>

Receive network data from a peer specified by context.

This function can be used to register a callback function that is called by the network stack when network data has been received for this context. As this function registers a callback, then there is no need to call this function multiple times if timeout is set to K_NO_WAIT. If callback function or user data changes, then the function can be called multiple times to register new values. This function will return immediately if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function will wait until the network buffer is received. Timeout value > 0 will wait as many ms. After the network buffer is received, a caller-supplied callback is called. The callback is called even if timeout was set to K_FOREVER, the callback is called before this function will return in this case. The callback is not called if the timeout expires. The timeout functionality can be compiled out if synchronous behavior is not needed. The sync call logic requires some memory that can be saved if only async way of call is used. If CONFIG_NET_CONTEXT_SYNC_RECV is not set, then the timeout parameter value is ignored. This is similar as BSD recv() function. Note that net_context_bind() should be called before net_context_recv(). Default random port number is assigned to local port. Only bind() will update connection information from context. If recv() is called before bind() call, it may refuse to bind to a context which already has a connection associated.

Parameters
contextThe network context to use.
cbCaller-supplied callback function.
timeoutCaller-supplied timeout. Possible values are K_FOREVER, K_NO_WAIT, >0.
user_dataCaller-supplied user data.
Returns
0 if ok, < 0 if error

◆ net_context_ref()

int net_context_ref ( struct net_context context)

#include <include/zephyr/net/net_context.h>

Take a reference count to a net_context, preventing destruction.

Network contexts are not recycled until their reference count reaches zero. Note that this does not prevent any "close" behavior that results from errors or net_context_put. It simply prevents the context from being recycled for further use.

Parameters
contextThe context on which to increment the reference count
Returns
The new reference count

◆ net_context_send()

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 
)

#include <include/zephyr/net/net_context.h>

Send data to a peer.

This function can be used to send network data to a peer connection. After the network buffer is sent, a caller-supplied callback is called. Note that the callback might be called after this function has returned. For context of type SOCK_DGRAM, the destination address must have been set by the call to net_context_connect(). This is similar as BSD send() function.

Parameters
contextThe network context to use.
bufThe data buffer to send
lenLength of the buffer
cbCaller-supplied callback function.
timeoutCurrently this value is not used.
user_dataCaller-supplied user data.
Returns
0 if ok, < 0 if error

◆ net_context_sendmsg()

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 
)

#include <include/zephyr/net/net_context.h>

Send data in iovec to a peer specified in msghdr struct.

This function has similar semantics as Posix sendmsg() call. For unconnected socket, the msg_name field in msghdr must be set. For connected socket the msg_name should be set to NULL, and msg_namelen to 0. After the network buffer is sent, a caller-supplied callback is called. Note that the callback might be called after this function has returned.

Parameters
contextThe network context to use.
msghdrThe data to send
flagsFlags for the sending.
cbCaller-supplied callback function.
timeoutCurrently this value is not used.
user_dataCaller-supplied user data.
Returns
numbers of bytes sent on success, a negative errno otherwise

◆ net_context_sendto()

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 
)

#include <include/zephyr/net/net_context.h>

Send data to a peer specified by address.

This function can be used to send network data to a peer specified by address. This variant can only be used for datagram connections of type SOCK_DGRAM. After the network buffer is sent, a caller-supplied callback is called. Note that the callback might be called after this function has returned. This is similar as BSD sendto() function.

Parameters
contextThe network context to use.
bufThe data buffer to send
lenLength of the buffer
dst_addrDestination address.
addrlenLength of the address.
cbCaller-supplied callback function.
timeoutCurrently this value is not used.
user_dataCaller-supplied user data.
Returns
numbers of bytes sent on success, a negative errno otherwise

◆ net_context_set_accepting()

static void net_context_set_accepting ( struct net_context context,
bool  accepting 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set this context to accept data now.

Parameters
contextNetwork context.
acceptingTrue if accepting, False if not

◆ net_context_set_can_filter_id()

static void net_context_set_can_filter_id ( struct net_context context,
int  filter_id 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set CAN filter id for this network context.

This function sets the CAN filter id of the context.

Parameters
contextNetwork context.
filter_idCAN filter id

◆ net_context_set_closing()

static void net_context_set_closing ( struct net_context context,
bool  closing 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set this context to closing.

Parameters
contextNetwork context.
closingTrue if closing, False if not

◆ net_context_set_family()

static void net_context_set_family ( struct net_context context,
sa_family_t  family 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set address family for this network context.

This function sets the address family (IPv4, IPv6 or AF_PACKET) of the context.

Parameters
contextNetwork context.
familyAddress family (AF_INET, AF_INET6, AF_PACKET, AF_CAN)

◆ net_context_set_iface()

static void net_context_set_iface ( struct net_context context,
struct net_if iface 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set network interface for this context.

This function binds network interface to this context.

Parameters
contextNetwork context.
ifaceNetwork interface.

◆ net_context_set_ipv4_mcast_ttl()

static void net_context_set_ipv4_mcast_ttl ( struct net_context context,
uint8_t  ttl 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set IPv4 multicast TTL (time-to-live) value for this context.

This function sets the IPv4 multicast TTL (time-to-live) value for this context.

Parameters
contextNetwork context.
ttlIPv4 multicast time-to-live value.

◆ net_context_set_ipv4_ttl()

static void net_context_set_ipv4_ttl ( struct net_context context,
uint8_t  ttl 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set IPv4 TTL (time-to-live) value for this context.

This function sets the IPv4 TTL (time-to-live) value for this context.

Parameters
contextNetwork context.
ttlIPv4 time-to-live value.

◆ net_context_set_ipv6_hop_limit()

static void net_context_set_ipv6_hop_limit ( struct net_context context,
uint8_t  hop_limit 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set IPv6 hop limit value for this context.

This function sets the IPv6 hop limit value for this context.

Parameters
contextNetwork context.
hop_limitIPv6 hop limit value.

◆ net_context_set_ipv6_mcast_hop_limit()

static void net_context_set_ipv6_mcast_hop_limit ( struct net_context context,
uint8_t  hop_limit 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set IPv6 multicast hop limit value for this context.

This function sets the IPv6 multicast hop limit value for this context.

Parameters
contextNetwork context.
hop_limitIPv6 multicast hop limit value.

◆ net_context_set_option()

int net_context_set_option ( struct net_context context,
enum net_context_option  option,
const void *  value,
size_t  len 
)

#include <include/zephyr/net/net_context.h>

Set an connection option for this context.

Parameters
contextThe network context to use.
optionOption to set
valueOption value
lenOption length
Returns
0 if ok, <0 if error

◆ net_context_set_proto()

static void net_context_set_proto ( struct net_context context,
uint16_t  proto 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set context IP protocol for this network context.

This function sets the context IP protocol (UDP / TCP) of the context.

Parameters
contextNetwork context.
protoContext IP protocol (IPPROTO_UDP, IPPROTO_TCP or IEEE 802.3 protocol value)

◆ net_context_set_proxy_enabled()

static void net_context_set_proxy_enabled ( struct net_context context,
bool  enable 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Enable or disable socks proxy support for this context.

This function either enables or disables socks proxy support for this context.

Parameters
contextNetwork context.
enableEnable socks proxy or disable it.

◆ net_context_set_state()

static void net_context_set_state ( struct net_context context,
enum net_context_state  state 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set state for this network context.

This function sets the state of the context.

Parameters
contextNetwork context.
stateNew network context state.

◆ net_context_set_type()

static void net_context_set_type ( struct net_context context,
enum net_sock_type  type 
)
inlinestatic

#include <include/zephyr/net/net_context.h>

Set context type for this network context.

This function sets the context type (stream or datagram) of the context.

Parameters
contextNetwork context.
typeContext type (SOCK_STREAM or SOCK_DGRAM)

◆ net_context_unref()

int net_context_unref ( struct net_context context)

#include <include/zephyr/net/net_context.h>

Decrement the reference count to a network context.

Decrements the refcount. If it reaches zero, the context will be recycled. Note that this does not cause any network-visible "close" behavior (i.e. future packets to this connection may see TCP RST or ICMP port unreachable responses). See net_context_put() for that.

Parameters
contextThe context on which to decrement the reference count
Returns
The new reference count, zero if the context was destroyed

◆ net_context_update_recv_wnd()

int net_context_update_recv_wnd ( struct net_context context,
int32_t  delta 
)

#include <include/zephyr/net/net_context.h>

Update TCP receive window for context.

This function should be used by an application which doesn't fully process incoming data in its receive callback, but for example, queues it. In this case, receive callback should decrease the window (call this function with a negative value) by the size of queued data, and function(s) which dequeue data - with positive value corresponding to the dequeued size. For example, if receive callback gets a packet with the data size of 256 and queues it, it should call this function with delta of -256. If a function extracts 10 bytes of the queued data, it should call it with delta of 10.

Parameters
contextThe TCP network context to use.
deltaSize, in bytes, by which to increase TCP receive window (negative value to decrease).
Returns
0 if ok, < 0 if error