1 /** @file
2  * @brief Network context definitions
3  *
4  * An API for applications to define a network connection.
5  */
6 
7 /*
8  * Copyright (c) 2016 Intel Corporation
9  * Copyright (c) 2021 Nordic Semiconductor
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 
14 #ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
15 #define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
16 
17 /**
18  * @brief Application network context
19  * @defgroup net_context Application network context
20  * @since 1.0
21  * @version 0.8.0
22  * @ingroup networking
23  * @{
24  */
25 
26 #include <zephyr/kernel.h>
27 #include <zephyr/sys/atomic.h>
28 
29 #include <zephyr/net/net_ip.h>
30 #include <zephyr/net/net_if.h>
31 #include <zephyr/net/net_stats.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /** Is this context used or not */
38 #define NET_CONTEXT_IN_USE BIT(0)
39 
40 /** @cond INTERNAL_HIDDEN */
41 
42 /** State of the context (bits 1 & 2 in the flags) */
43 enum net_context_state {
44 	NET_CONTEXT_IDLE = 0,
45 	NET_CONTEXT_UNCONNECTED = 0,
46 	NET_CONTEXT_CONFIGURING = 1,
47 	NET_CONTEXT_CONNECTING = 1,
48 	NET_CONTEXT_READY = 2,
49 	NET_CONTEXT_CONNECTED = 2,
50 	NET_CONTEXT_LISTENING = 3,
51 };
52 
53 /** @endcond */
54 
55 /**
56  * The address family, connection type and IP protocol are
57  * stored into a bit field to save space.
58  */
59 /** Protocol family of this connection */
60 #define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
61 
62 /** Type of the connection (datagram / stream / raw) */
63 #define NET_CONTEXT_TYPE   (BIT(6) | BIT(7))
64 
65 /** Remote address set */
66 #define NET_CONTEXT_REMOTE_ADDR_SET  BIT(8)
67 
68 /** Is the socket accepting connections */
69 #define NET_CONTEXT_ACCEPTING_SOCK  BIT(9)
70 
71 /** Is the socket closing / closed */
72 #define NET_CONTEXT_CLOSING_SOCK  BIT(10)
73 
74 /** Context is bound to a specific interface */
75 #define NET_CONTEXT_BOUND_TO_IFACE BIT(11)
76 
77 struct net_context;
78 
79 /**
80  * @typedef net_context_recv_cb_t
81  * @brief Network data receive callback.
82  *
83  * @details The recv callback is called after a network data packet is
84  * received. This callback is called by RX thread so its stack and execution
85  * context is used here. Keep processing in the callback minimal to reduce the
86  * time spent blocked while handling packets.
87  *
88  * @param context The context to use.
89  * @param pkt Network buffer that is received. If the pkt is not NULL,
90  * then the callback will own the buffer and it needs to unref the pkt
91  * as soon as it has finished working with it.  On EOF, pkt will be NULL.
92  * @param ip_hdr a pointer to relevant IP (v4 or v6) header.
93  * @param proto_hdr a pointer to relevant protocol (udp or tcp) header.
94  * @param status Value is set to 0 if some data or the connection is
95  * at EOF, <0 if there was an error receiving data, in this case the
96  * pkt parameter is set to NULL.
97  * @param user_data The user data given in net_recv() call.
98  */
99 typedef void (*net_context_recv_cb_t)(struct net_context *context,
100 				      struct net_pkt *pkt,
101 				      union net_ip_header *ip_hdr,
102 				      union net_proto_header *proto_hdr,
103 				      int status,
104 				      void *user_data);
105 
106 /**
107  * @typedef net_context_send_cb_t
108  * @brief Network data send callback.
109  *
110  * @details The send callback is called after a network data packet is sent.
111  * This callback is called by TX thread so its stack and execution context is
112  * used here. Keep processing in the callback minimal to reduce the time spent
113  * blocked while handling packets.
114  *
115  * @param context The context to use.
116  * @param status Value is set to >= 0: amount of data that was sent,
117  * < 0 there was an error sending data.
118  * @param user_data The user data given in net_send() call.
119  */
120 typedef void (*net_context_send_cb_t)(struct net_context *context,
121 				      int status,
122 				      void *user_data);
123 
124 /**
125  * @typedef net_tcp_accept_cb_t
126  * @brief Accept callback
127  *
128  * @details The accept callback is called after a successful connection was
129  * established or if there was an error while we were waiting for a connection
130  * attempt. This callback is called by RX thread so its stack and execution
131  * context is used here. Keep processing in the callback minimal to reduce the
132  * time spent blocked while handling packets.
133  *
134  * @param new_context The context to use.
135  * @param addr The peer address.
136  * @param addrlen Length of the peer address.
137  * @param status The status code, 0 on success, < 0 otherwise
138  * @param user_data The user data given in net_context_accept() call.
139  */
140 typedef void (*net_tcp_accept_cb_t)(struct net_context *new_context,
141 				    struct sockaddr *addr,
142 				    socklen_t addrlen,
143 				    int status,
144 				    void *user_data);
145 
146 /**
147  * @typedef net_context_connect_cb_t
148  * @brief Connection callback.
149  *
150  * @details The connect callback is called after a connection is being
151  * established.
152  * For TCP connections, this callback is called by RX thread so its stack and
153  * execution context is used here. The callback is called after the TCP
154  * connection was established or if the connection failed. Keep processing in
155  * the callback minimal to reduce the time spent blocked while handling
156  * packets.
157  * For UDP connections, this callback is called immediately by
158  * net_context_connect() function. UDP is a connectionless protocol so the
159  * connection can be thought of being established immediately.
160  *
161  * @param context The context to use.
162  * @param status Status of the connection establishment. This is 0
163  * if the connection was established successfully, <0 if there was an
164  * error.
165  * @param user_data The user data given in net_context_connect() call.
166  */
167 typedef void (*net_context_connect_cb_t)(struct net_context *context,
168 					 int status,
169 					 void *user_data);
170 
171 /* The net_pkt_get_slab_func_t is here in order to avoid circular
172  * dependency between net_pkt.h and net_context.h
173  */
174 /**
175  * @typedef net_pkt_get_slab_func_t
176  *
177  * @brief Function that is called to get the slab that is used
178  * for net_pkt allocations.
179  *
180  * @return Pointer to valid struct k_mem_slab instance.
181  */
182 typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
183 
184 /* The net_pkt_get_pool_func_t is here in order to avoid circular
185  * dependency between net_pkt.h and net_context.h
186  */
187 /**
188  * @typedef net_pkt_get_pool_func_t
189  *
190  * @brief Function that is called to get the pool that is used
191  * for net_buf allocations.
192  *
193  * @return Pointer to valid struct net_buf_pool instance.
194  */
195 typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
196 
197 struct net_tcp;
198 
199 struct net_conn_handle;
200 
201 /**
202  * Note that we do not store the actual source IP address in the context
203  * because the address is already set in the network interface struct.
204  * If there is no such source address there, the packet cannot be sent
205  * anyway. This saves 12 bytes / context in IPv6.
206  */
207 __net_socket struct net_context {
208 	/** First member of the structure to allow to put contexts into a FIFO.
209 	 */
210 	void *fifo_reserved;
211 
212 	/** User data associated with a context.
213 	 */
214 	void *user_data;
215 
216 	/** Reference count
217 	 */
218 	atomic_t refcount;
219 
220 	/** Internal lock for protecting this context from multiple access.
221 	 */
222 	struct k_mutex lock;
223 
224 	/** Local endpoint address. Note that the values are in network byte
225 	 * order.
226 	 */
227 	struct sockaddr_ptr local;
228 
229 	/** Remote endpoint address. Note that the values are in network byte
230 	 * order.
231 	 */
232 	struct sockaddr remote;
233 
234 	/** Connection handle */
235 	struct net_conn_handle *conn_handler;
236 
237 	/** Receive callback to be called when desired packet
238 	 * has been received.
239 	 */
240 	net_context_recv_cb_t recv_cb;
241 
242 	/** Send callback to be called when the packet has been sent
243 	 * successfully.
244 	 */
245 	net_context_send_cb_t send_cb;
246 
247 	/** Connect callback to be called when a connection has been
248 	 *  established.
249 	 */
250 	net_context_connect_cb_t connect_cb;
251 
252 #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
253 	/** Get TX net_buf pool for this context.
254 	 */
255 	net_pkt_get_slab_func_t tx_slab;
256 
257 	/** Get DATA net_buf pool for this context.
258 	 */
259 	net_pkt_get_pool_func_t data_pool;
260 #endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
261 
262 #if defined(CONFIG_NET_TCP)
263 	/** TCP connection information */
264 	void *tcp;
265 #endif /* CONFIG_NET_TCP */
266 
267 #if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
268 	/**
269 	 * Semaphore to signal synchronous recv call completion.
270 	 */
271 	struct k_sem recv_data_wait;
272 #endif /* CONFIG_NET_CONTEXT_SYNC_RECV */
273 
274 #if defined(CONFIG_NET_SOCKETS)
275 	/** BSD socket private data */
276 	void *socket_data;
277 
278 	/** Per-socket packet or connection queues */
279 	union {
280 		struct k_fifo recv_q;
281 		struct k_fifo accept_q;
282 	};
283 
284 	struct {
285 		/** Condition variable used when receiving data */
286 		struct k_condvar recv;
287 
288 		/** Mutex used by condition variable */
289 		struct k_mutex *lock;
290 	} cond;
291 #endif /* CONFIG_NET_SOCKETS */
292 
293 #if defined(CONFIG_NET_OFFLOAD)
294 	/** context for use by offload drivers */
295 	void *offload_context;
296 #endif /* CONFIG_NET_OFFLOAD */
297 
298 #if defined(CONFIG_NET_SOCKETS_CAN)
299 	int can_filter_id;
300 #endif /* CONFIG_NET_SOCKETS_CAN */
301 
302 	/** Option values */
303 	struct {
304 #if defined(CONFIG_NET_CONTEXT_PRIORITY)
305 		/** Priority of the network data sent via this net_context */
306 		uint8_t priority;
307 #endif
308 #if defined(CONFIG_NET_CONTEXT_TXTIME)
309 		/** When to send the packet out */
310 		bool txtime;
311 #endif
312 #if defined(CONFIG_SOCKS)
313 		/** Socks proxy address */
314 		struct {
315 			struct sockaddr addr;
316 			socklen_t addrlen;
317 		} proxy;
318 #endif
319 #if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
320 		/** Restrict local port range between these values.
321 		 * The option takes an uint32_t value with the high 16 bits
322 		 * set to the upper range bound, and the low 16 bits set to
323 		 * the lower range bound.  Range bounds are inclusive. The
324 		 * 16-bit values should be in host byte order.
325 		 * The lower bound has to be less than the upper bound when
326 		 * both bounds are not zero. Otherwise, setting the option
327 		 * fails with EINVAL.
328 		 * If either bound is outside of the global local port range,
329 		 * or is zero, then that bound has no effect.
330 		 */
331 		uint32_t port_range;
332 #endif
333 #if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
334 		/** Receive timeout */
335 		k_timeout_t rcvtimeo;
336 #endif
337 #if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
338 		/** Send timeout */
339 		k_timeout_t sndtimeo;
340 #endif
341 #if defined(CONFIG_NET_CONTEXT_RCVBUF)
342 		/** Receive buffer maximum size */
343 		uint16_t rcvbuf;
344 #endif
345 #if defined(CONFIG_NET_CONTEXT_SNDBUF)
346 		/** Send buffer maximum size */
347 		uint16_t sndbuf;
348 #endif
349 #if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
350 		/**
351 		 * DSCP (Differentiated Services Code point) and
352 		 * ECN (Explicit Congestion Notification) values.
353 		 */
354 		uint8_t dscp_ecn;
355 #endif
356 #if defined(CONFIG_NET_CONTEXT_REUSEADDR)
357 		/** Re-use address (SO_REUSEADDR) flag on a socket. */
358 		bool reuseaddr;
359 #endif
360 #if defined(CONFIG_NET_CONTEXT_REUSEPORT)
361 		/** Re-use port (SO_REUSEPORT) flag on a socket. */
362 		bool reuseport;
363 #endif
364 #if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
365 		/** Support v4-mapped-on-v6 addresses */
366 		bool ipv6_v6only;
367 #endif
368 #if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
369 		/** Receive network packet information in recvmsg() call */
370 		bool recv_pktinfo;
371 #endif
372 #if defined(CONFIG_NET_IPV6)
373 		/**
374 		 * Source address selection preferences. Currently used only for IPv6,
375 		 * see RFC 5014 for details.
376 		 */
377 		uint16_t addr_preferences;
378 #endif
379 #if defined(CONFIG_NET_IPV6) || defined(CONFIG_NET_IPV4)
380 		union {
381 			/**
382 			 * IPv6 multicast output network interface for this context/socket.
383 			 * Only allowed for SOCK_DGRAM or SOCK_RAW type sockets.
384 			 */
385 			uint8_t ipv6_mcast_ifindex;
386 
387 			/**
388 			 * IPv4 multicast output network interface for this context/socket.
389 			 * Only allowed for SOCK_DGRAM type sockets.
390 			 */
391 			uint8_t ipv4_mcast_ifindex;
392 		};
393 #endif /* CONFIG_NET_IPV6 || CONFIG_NET_IPV4 */
394 
395 #if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
396 		/** Enable RX, TX or both timestamps of packets send through sockets. */
397 		uint8_t timestamping;
398 #endif
399 	} options;
400 
401 	/** Protocol (UDP, TCP or IEEE 802.3 protocol value) */
402 	uint16_t proto;
403 
404 	/** Flags for the context */
405 	uint16_t flags;
406 
407 	/** Network interface assigned to this context */
408 	int8_t iface;
409 
410 	/** IPv6 hop limit or IPv4 ttl for packets sent via this context. */
411 	union {
412 		struct {
413 			uint8_t ipv6_hop_limit;       /**< IPv6 hop limit */
414 			uint8_t ipv6_mcast_hop_limit; /**< IPv6 multicast hop limit */
415 		};
416 		struct {
417 			uint8_t ipv4_ttl;       /**< IPv4 TTL */
418 			uint8_t ipv4_mcast_ttl; /**< IPv4 multicast TTL */
419 		};
420 	};
421 
422 #if defined(CONFIG_SOCKS)
423 	/** Is socks proxy enabled */
424 	bool proxy_enabled;
425 #endif
426 
427 };
428 
429 /**
430  * @brief Is this context used or not.
431  *
432  * @param context Network context.
433  *
434  * @return True if the context is currently in use, False otherwise.
435  */
net_context_is_used(struct net_context * context)436 static inline bool net_context_is_used(struct net_context *context)
437 {
438 	NET_ASSERT(context);
439 
440 	return context->flags & NET_CONTEXT_IN_USE;
441 }
442 
443 /**
444  * @brief Is this context bound to a network interface.
445  *
446  * @param context Network context.
447  *
448  * @return True if the context is bound to network interface, False otherwise.
449  */
net_context_is_bound_to_iface(struct net_context * context)450 static inline bool net_context_is_bound_to_iface(struct net_context *context)
451 {
452 	NET_ASSERT(context);
453 
454 	return context->flags & NET_CONTEXT_BOUND_TO_IFACE;
455 }
456 
457 /**
458  * @brief Is this context is accepting data now.
459  *
460  * @param context Network context.
461  *
462  * @return True if the context is accepting connections, False otherwise.
463  */
net_context_is_accepting(struct net_context * context)464 static inline bool net_context_is_accepting(struct net_context *context)
465 {
466 	NET_ASSERT(context);
467 
468 	return context->flags & NET_CONTEXT_ACCEPTING_SOCK;
469 }
470 
471 /**
472  * @brief Set this context to accept data now.
473  *
474  * @param context Network context.
475  * @param accepting True if accepting, False if not
476  */
net_context_set_accepting(struct net_context * context,bool accepting)477 static inline void net_context_set_accepting(struct net_context *context,
478 					     bool accepting)
479 {
480 	NET_ASSERT(context);
481 
482 	if (accepting) {
483 		context->flags |= NET_CONTEXT_ACCEPTING_SOCK;
484 	} else {
485 		context->flags &= (uint16_t)~NET_CONTEXT_ACCEPTING_SOCK;
486 	}
487 }
488 
489 /**
490  * @brief Is this context closing.
491  *
492  * @param context Network context.
493  *
494  * @return True if the context is closing, False otherwise.
495  */
net_context_is_closing(struct net_context * context)496 static inline bool net_context_is_closing(struct net_context *context)
497 {
498 	NET_ASSERT(context);
499 
500 	return context->flags & NET_CONTEXT_CLOSING_SOCK;
501 }
502 
503 /**
504  * @brief Set this context to closing.
505  *
506  * @param context Network context.
507  * @param closing True if closing, False if not
508  */
net_context_set_closing(struct net_context * context,bool closing)509 static inline void net_context_set_closing(struct net_context *context,
510 					   bool closing)
511 {
512 	NET_ASSERT(context);
513 
514 	if (closing) {
515 		context->flags |= NET_CONTEXT_CLOSING_SOCK;
516 	} else {
517 		context->flags &= (uint16_t)~NET_CONTEXT_CLOSING_SOCK;
518 	}
519 }
520 
521 /** @cond INTERNAL_HIDDEN */
522 
523 #define NET_CONTEXT_STATE_SHIFT 1
524 #define NET_CONTEXT_STATE_MASK 0x03
525 
526 /** @endcond */
527 
528 /**
529  * @brief Get state for this network context.
530  *
531  * @details This function returns the state of the context.
532  *
533  * @param context Network context.
534  *
535  * @return Network state.
536  */
537 static inline
net_context_get_state(struct net_context * context)538 enum net_context_state net_context_get_state(struct net_context *context)
539 {
540 	NET_ASSERT(context);
541 
542 	return (enum net_context_state)
543 		((context->flags >> NET_CONTEXT_STATE_SHIFT) &
544 		NET_CONTEXT_STATE_MASK);
545 }
546 
547 /**
548  * @brief Set state for this network context.
549  *
550  * @details This function sets the state of the context.
551  *
552  * @param context Network context.
553  * @param state New network context state.
554  */
net_context_set_state(struct net_context * context,enum net_context_state state)555 static inline void net_context_set_state(struct net_context *context,
556 					 enum net_context_state state)
557 {
558 	NET_ASSERT(context);
559 
560 	context->flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
561 	context->flags |= ((state & NET_CONTEXT_STATE_MASK) <<
562 			   NET_CONTEXT_STATE_SHIFT);
563 }
564 
565 /**
566  * @brief Get address family for this network context.
567  *
568  * @details This function returns the address family (IPv4 or IPv6)
569  * of the context.
570  *
571  * @param context Network context.
572  *
573  * @return Network state.
574  */
net_context_get_family(struct net_context * context)575 static inline sa_family_t net_context_get_family(struct net_context *context)
576 {
577 	NET_ASSERT(context);
578 
579 	return ((context->flags & NET_CONTEXT_FAMILY) >> 3);
580 }
581 
582 /**
583  * @brief Set address family for this network context.
584  *
585  * @details This function sets the address family (IPv4, IPv6 or AF_PACKET)
586  * of the context.
587  *
588  * @param context Network context.
589  * @param family Address family (AF_INET, AF_INET6, AF_PACKET, AF_CAN)
590  */
net_context_set_family(struct net_context * context,sa_family_t family)591 static inline void net_context_set_family(struct net_context *context,
592 					  sa_family_t family)
593 {
594 	uint8_t flag = 0U;
595 
596 	NET_ASSERT(context);
597 
598 	if (family == AF_UNSPEC || family == AF_INET || family == AF_INET6 ||
599 	    family == AF_PACKET || family == AF_CAN) {
600 		/* Family is in BIT(4), BIT(5) and BIT(6) */
601 		flag = (uint8_t)(family << 3);
602 	}
603 
604 	context->flags |= flag;
605 }
606 
607 /**
608  * @brief Get context type for this network context.
609  *
610  * @details This function returns the context type (stream, datagram or raw)
611  * of the context.
612  *
613  * @param context Network context.
614  *
615  * @return Network context type.
616  */
617 static inline
net_context_get_type(struct net_context * context)618 enum net_sock_type net_context_get_type(struct net_context *context)
619 {
620 	NET_ASSERT(context);
621 
622 	return (enum net_sock_type)((context->flags & NET_CONTEXT_TYPE) >> 6);
623 }
624 
625 /**
626  * @brief Set context type for this network context.
627  *
628  * @details This function sets the context type (stream or datagram)
629  * of the context.
630  *
631  * @param context Network context.
632  * @param type Context type (SOCK_STREAM or SOCK_DGRAM)
633  */
net_context_set_type(struct net_context * context,enum net_sock_type type)634 static inline void net_context_set_type(struct net_context *context,
635 					enum net_sock_type type)
636 {
637 	uint16_t flag = 0U;
638 
639 	NET_ASSERT(context);
640 
641 	if (type == SOCK_DGRAM || type == SOCK_STREAM || type == SOCK_RAW) {
642 		/* Type is in BIT(6) and BIT(7)*/
643 		flag = (uint16_t)(type << 6);
644 	}
645 
646 	context->flags |= flag;
647 }
648 
649 /**
650  * @brief Set CAN filter id for this network context.
651  *
652  * @details This function sets the CAN filter id of the context.
653  *
654  * @param context Network context.
655  * @param filter_id CAN filter id
656  */
657 #if defined(CONFIG_NET_SOCKETS_CAN)
net_context_set_can_filter_id(struct net_context * context,int filter_id)658 static inline void net_context_set_can_filter_id(struct net_context *context,
659 					     int filter_id)
660 {
661 	NET_ASSERT(context);
662 
663 	context->can_filter_id = filter_id;
664 }
665 #else
net_context_set_can_filter_id(struct net_context * context,int filter_id)666 static inline void net_context_set_can_filter_id(struct net_context *context,
667 					     int filter_id)
668 {
669 	ARG_UNUSED(context);
670 	ARG_UNUSED(filter_id);
671 }
672 #endif
673 
674 /**
675  * @brief Get CAN filter id for this network context.
676  *
677  * @details This function gets the CAN filter id of the context.
678  *
679  * @param context Network context.
680  *
681  * @return Filter id of this network context
682  */
683 #if defined(CONFIG_NET_SOCKETS_CAN)
net_context_get_can_filter_id(struct net_context * context)684 static inline int net_context_get_can_filter_id(struct net_context *context)
685 {
686 	NET_ASSERT(context);
687 
688 	return context->can_filter_id;
689 }
690 #else
net_context_get_can_filter_id(struct net_context * context)691 static inline int net_context_get_can_filter_id(struct net_context *context)
692 {
693 	ARG_UNUSED(context);
694 
695 	return -1;
696 }
697 #endif
698 
699 /**
700  * @brief Get context IP protocol for this network context.
701  *
702  * @details This function returns the IP protocol (UDP / TCP /
703  * IEEE 802.3 protocol value) of the context.
704  *
705  * @param context Network context.
706  *
707  * @return Network context IP protocol.
708  */
net_context_get_proto(struct net_context * context)709 static inline uint16_t net_context_get_proto(struct net_context *context)
710 {
711 	return context->proto;
712 }
713 
714 /**
715  * @brief Set context IP protocol for this network context.
716  *
717  * @details This function sets the context IP protocol (UDP / TCP)
718  * of the context.
719  *
720  * @param context Network context.
721  * @param proto Context IP protocol (IPPROTO_UDP, IPPROTO_TCP or IEEE 802.3
722  * protocol value)
723  */
net_context_set_proto(struct net_context * context,uint16_t proto)724 static inline void net_context_set_proto(struct net_context *context,
725 					 uint16_t proto)
726 {
727 	context->proto = proto;
728 }
729 
730 /**
731  * @brief Get network interface for this context.
732  *
733  * @details This function returns the used network interface.
734  *
735  * @param context Network context.
736  *
737  * @return Context network interface if context is bind to interface,
738  * NULL otherwise.
739  */
740 static inline
net_context_get_iface(struct net_context * context)741 struct net_if *net_context_get_iface(struct net_context *context)
742 {
743 	NET_ASSERT(context);
744 
745 	return net_if_get_by_index(context->iface);
746 }
747 
748 /**
749  * @brief Set network interface for this context.
750  *
751  * @details This function binds network interface to this context.
752  *
753  * @param context Network context.
754  * @param iface Network interface.
755  */
net_context_set_iface(struct net_context * context,struct net_if * iface)756 static inline void net_context_set_iface(struct net_context *context,
757 					 struct net_if *iface)
758 {
759 	NET_ASSERT(iface);
760 
761 	context->iface = (uint8_t)net_if_get_by_iface(iface);
762 }
763 
764 /**
765  * @brief Bind network interface to this context.
766  *
767  * @details This function binds network interface to this context.
768  *
769  * @param context Network context.
770  * @param iface Network interface.
771  */
net_context_bind_iface(struct net_context * context,struct net_if * iface)772 static inline void net_context_bind_iface(struct net_context *context,
773 					  struct net_if *iface)
774 {
775 	NET_ASSERT(iface);
776 
777 	context->flags |= NET_CONTEXT_BOUND_TO_IFACE;
778 	net_context_set_iface(context, iface);
779 }
780 
781 /**
782  * @brief Get IPv4 TTL (time-to-live) value for this context.
783  *
784  * @details This function returns the IPv4 TTL (time-to-live) value that is
785  *          set to this context.
786  *
787  * @param context Network context.
788  *
789  * @return IPv4 TTL value
790  */
net_context_get_ipv4_ttl(struct net_context * context)791 static inline uint8_t net_context_get_ipv4_ttl(struct net_context *context)
792 {
793 	return context->ipv4_ttl;
794 }
795 
796 /**
797  * @brief Set IPv4 TTL (time-to-live) value for this context.
798  *
799  * @details This function sets the IPv4 TTL (time-to-live) value for
800  *          this context.
801  *
802  * @param context Network context.
803  * @param ttl IPv4 time-to-live value.
804  */
net_context_set_ipv4_ttl(struct net_context * context,uint8_t ttl)805 static inline void net_context_set_ipv4_ttl(struct net_context *context,
806 					    uint8_t ttl)
807 {
808 	context->ipv4_ttl = ttl;
809 }
810 
811 /**
812  * @brief Get IPv4 multicast TTL (time-to-live) value for this context.
813  *
814  * @details This function returns the IPv4 multicast TTL (time-to-live) value
815  *          that is set to this context.
816  *
817  * @param context Network context.
818  *
819  * @return IPv4 multicast TTL value
820  */
net_context_get_ipv4_mcast_ttl(struct net_context * context)821 static inline uint8_t net_context_get_ipv4_mcast_ttl(struct net_context *context)
822 {
823 	return context->ipv4_mcast_ttl;
824 }
825 
826 /**
827  * @brief Set IPv4 multicast TTL (time-to-live) value for this context.
828  *
829  * @details This function sets the IPv4 multicast TTL (time-to-live) value for
830  *          this context.
831  *
832  * @param context Network context.
833  * @param ttl IPv4 multicast time-to-live value.
834  */
net_context_set_ipv4_mcast_ttl(struct net_context * context,uint8_t ttl)835 static inline void net_context_set_ipv4_mcast_ttl(struct net_context *context,
836 						  uint8_t ttl)
837 {
838 	context->ipv4_mcast_ttl = ttl;
839 }
840 
841 /**
842  * @brief Get IPv6 hop limit value for this context.
843  *
844  * @details This function returns the IPv6 hop limit value that is set to this
845  *          context.
846  *
847  * @param context Network context.
848  *
849  * @return IPv6 hop limit value
850  */
net_context_get_ipv6_hop_limit(struct net_context * context)851 static inline uint8_t net_context_get_ipv6_hop_limit(struct net_context *context)
852 {
853 	return context->ipv6_hop_limit;
854 }
855 
856 /**
857  * @brief Set IPv6 hop limit value for this context.
858  *
859  * @details This function sets the IPv6 hop limit value for this context.
860  *
861  * @param context Network context.
862  * @param hop_limit IPv6 hop limit value.
863  */
net_context_set_ipv6_hop_limit(struct net_context * context,uint8_t hop_limit)864 static inline void net_context_set_ipv6_hop_limit(struct net_context *context,
865 						  uint8_t hop_limit)
866 {
867 	context->ipv6_hop_limit = hop_limit;
868 }
869 
870 /**
871  * @brief Get IPv6 multicast hop limit value for this context.
872  *
873  * @details This function returns the IPv6 multicast hop limit value
874  *          that is set to this context.
875  *
876  * @param context Network context.
877  *
878  * @return IPv6 multicast hop limit value
879  */
net_context_get_ipv6_mcast_hop_limit(struct net_context * context)880 static inline uint8_t net_context_get_ipv6_mcast_hop_limit(struct net_context *context)
881 {
882 	return context->ipv6_mcast_hop_limit;
883 }
884 
885 /**
886  * @brief Set IPv6 multicast hop limit value for this context.
887  *
888  * @details This function sets the IPv6 multicast hop limit value for
889  *          this context.
890  *
891  * @param context Network context.
892  * @param hop_limit IPv6 multicast hop limit value.
893  */
net_context_set_ipv6_mcast_hop_limit(struct net_context * context,uint8_t hop_limit)894 static inline void net_context_set_ipv6_mcast_hop_limit(struct net_context *context,
895 							uint8_t hop_limit)
896 {
897 	context->ipv6_mcast_hop_limit = hop_limit;
898 }
899 
900 /**
901  * @brief Enable or disable socks proxy support for this context.
902  *
903  * @details This function either enables or disables socks proxy support for
904  *          this context.
905  *
906  * @param context Network context.
907  * @param enable Enable socks proxy or disable it.
908  */
909 #if defined(CONFIG_SOCKS)
net_context_set_proxy_enabled(struct net_context * context,bool enable)910 static inline void net_context_set_proxy_enabled(struct net_context *context,
911 						 bool enable)
912 {
913 	context->proxy_enabled = enable;
914 }
915 #else
net_context_set_proxy_enabled(struct net_context * context,bool enable)916 static inline void net_context_set_proxy_enabled(struct net_context *context,
917 						 bool enable)
918 {
919 	ARG_UNUSED(context);
920 	ARG_UNUSED(enable);
921 }
922 #endif
923 
924 /**
925  * @brief Is socks proxy support enabled or disabled for this context.
926  *
927  * @details This function returns current socks proxy status for
928  *          this context.
929  *
930  * @param context Network context.
931  *
932  * @return True if socks proxy is enabled for this context, False otherwise
933  */
934 #if defined(CONFIG_SOCKS)
net_context_is_proxy_enabled(struct net_context * context)935 static inline bool net_context_is_proxy_enabled(struct net_context *context)
936 {
937 	return context->proxy_enabled;
938 }
939 #else
net_context_is_proxy_enabled(struct net_context * context)940 static inline bool net_context_is_proxy_enabled(struct net_context *context)
941 {
942 	ARG_UNUSED(context);
943 	return false;
944 }
945 #endif
946 
947 /**
948  * @brief Get network context.
949  *
950  * @details Network context is used to define the connection 5-tuple
951  * (protocol, remote address, remote port, source address and source
952  * port). Random free port number will be assigned to source port when
953  * context is created. This is similar as BSD socket() function.
954  * The context will be created with a reference count of 1.
955  *
956  * @param family IP address family (AF_INET or AF_INET6)
957  * @param type Type of the socket, SOCK_STREAM or SOCK_DGRAM
958  * @param ip_proto IP protocol, IPPROTO_UDP or IPPROTO_TCP. For raw socket
959  * access, the value is the L2 protocol value from IEEE 802.3 (see ethernet.h)
960  * @param context The allocated context is returned to the caller.
961  *
962  * @return 0 if ok, < 0 if error
963  */
964 int net_context_get(sa_family_t family,
965 		    enum net_sock_type type,
966 		    uint16_t ip_proto,
967 		    struct net_context **context);
968 
969 /**
970  * @brief Close and unref a network context.
971  *
972  * @details This releases the context. It is not possible to send or
973  * receive data via this context after this call.  This is similar as
974  * BSD shutdown() function.  For legacy compatibility, this function
975  * will implicitly decrement the reference count and possibly destroy
976  * the context either now or when it reaches a final state.
977  *
978  * @param context The context to be closed.
979  *
980  * @return 0 if ok, < 0 if error
981  */
982 int net_context_put(struct net_context *context);
983 
984 /**
985  * @brief Take a reference count to a net_context, preventing destruction
986  *
987  * @details Network contexts are not recycled until their reference
988  * count reaches zero.  Note that this does not prevent any "close"
989  * behavior that results from errors or net_context_put.  It simply
990  * prevents the context from being recycled for further use.
991  *
992  * @param context The context on which to increment the reference count
993  *
994  * @return The new reference count
995  */
996 int net_context_ref(struct net_context *context);
997 
998 /**
999  * @brief Decrement the reference count to a network context
1000  *
1001  * @details Decrements the refcount.  If it reaches zero, the context
1002  * will be recycled.  Note that this does not cause any
1003  * network-visible "close" behavior (i.e. future packets to this
1004  * connection may see TCP RST or ICMP port unreachable responses).  See
1005  * net_context_put() for that.
1006  *
1007  * @param context The context on which to decrement the reference count
1008  *
1009  * @return The new reference count, zero if the context was destroyed
1010  */
1011 int net_context_unref(struct net_context *context);
1012 
1013 /**
1014  * @brief Create IPv4 packet in provided net_pkt from context
1015  *
1016  * @param context Network context for a connection
1017  * @param pkt Network packet
1018  * @param src Source address, or NULL to choose a default
1019  * @param dst Destination IPv4 address
1020  *
1021  * @return Return 0 on success, negative errno otherwise.
1022  */
1023 #if defined(CONFIG_NET_IPV4)
1024 int net_context_create_ipv4_new(struct net_context *context,
1025 				struct net_pkt *pkt,
1026 				const struct in_addr *src,
1027 				const struct in_addr *dst);
1028 #else
net_context_create_ipv4_new(struct net_context * context,struct net_pkt * pkt,const struct in_addr * src,const struct in_addr * dst)1029 static inline int net_context_create_ipv4_new(struct net_context *context,
1030 					      struct net_pkt *pkt,
1031 					      const struct in_addr *src,
1032 					      const struct in_addr *dst)
1033 {
1034 	return -1;
1035 }
1036 #endif /* CONFIG_NET_IPV4 */
1037 
1038 /**
1039  * @brief Create IPv6 packet in provided net_pkt from context
1040  *
1041  * @param context Network context for a connection
1042  * @param pkt Network packet
1043  * @param src Source address, or NULL to choose a default from context
1044  * @param dst Destination IPv6 address
1045  *
1046  * @return Return 0 on success, negative errno otherwise.
1047  */
1048 #if defined(CONFIG_NET_IPV6)
1049 int net_context_create_ipv6_new(struct net_context *context,
1050 				struct net_pkt *pkt,
1051 				const struct in6_addr *src,
1052 				const struct in6_addr *dst);
1053 #else
net_context_create_ipv6_new(struct net_context * context,struct net_pkt * pkt,const struct in6_addr * src,const struct in6_addr * dst)1054 static inline int net_context_create_ipv6_new(struct net_context *context,
1055 					      struct net_pkt *pkt,
1056 					      const struct in6_addr *src,
1057 					      const struct in6_addr *dst)
1058 {
1059 	ARG_UNUSED(context);
1060 	ARG_UNUSED(pkt);
1061 	ARG_UNUSED(src);
1062 	ARG_UNUSED(dst);
1063 	return -1;
1064 }
1065 #endif /* CONFIG_NET_IPV6 */
1066 
1067 /**
1068  * @brief Assign a socket a local address.
1069  *
1070  * @details This is similar as BSD bind() function.
1071  *
1072  * @param context The context to be assigned.
1073  * @param addr Address to assigned.
1074  * @param addrlen Length of the address.
1075  *
1076  * @return 0 if ok, < 0 if error
1077  */
1078 int net_context_bind(struct net_context *context,
1079 		     const struct sockaddr *addr,
1080 		     socklen_t addrlen);
1081 
1082 /**
1083  * @brief Mark the context as a listening one.
1084  *
1085  * @details This is similar as BSD listen() function.
1086  *
1087  * @param context The context to use.
1088  * @param backlog The size of the pending connections backlog.
1089  *
1090  * @return 0 if ok, < 0 if error
1091  */
1092 int net_context_listen(struct net_context *context,
1093 		       int backlog);
1094 
1095 /**
1096  * @brief            Create a network connection.
1097  *
1098  * @details          The net_context_connect function creates a network
1099  *                   connection to the host specified by addr. After the
1100  *                   connection is established, the user-supplied callback (cb)
1101  *                   is executed. cb is called even if the timeout was set to
1102  *                   K_FOREVER. cb is not called if the timeout expires.
1103  *                   For datagram sockets (SOCK_DGRAM), this function only sets
1104  *                   the peer address.
1105  *                   This function is similar to the BSD connect() function.
1106  *
1107  * @param context    The network context.
1108  * @param addr       The peer address to connect to.
1109  * @param addrlen    Peer address length.
1110  * @param cb         Callback function. Set to NULL if not required.
1111  * @param timeout    The timeout value for the connection. Possible values:
1112  *                   * K_NO_WAIT: this function will return immediately,
1113  *                   * K_FOREVER: this function will block until the
1114  *                                      connection is established,
1115  *                   * >0: this function will wait the specified ms.
1116  * @param user_data  Data passed to the callback function.
1117  *
1118  * @return           0 on success.
1119  * @return           -EINVAL if an invalid parameter is passed as an argument.
1120  * @return           -ENOTSUP if the operation is not supported or implemented.
1121  * @return           -ETIMEDOUT if the connect operation times out.
1122  */
1123 int net_context_connect(struct net_context *context,
1124 			const struct sockaddr *addr,
1125 			socklen_t addrlen,
1126 			net_context_connect_cb_t cb,
1127 			k_timeout_t timeout,
1128 			void *user_data);
1129 
1130 /**
1131  * @brief Accept a network connection attempt.
1132  *
1133  * @details Accept a connection being established. This function
1134  * will return immediately if the timeout is set to K_NO_WAIT.
1135  * In this case the context will call the supplied callback when ever
1136  * there is a connection established to this context. This is "a register
1137  * handler and forget" type of call (async).
1138  * If the timeout is set to K_FOREVER, the function will wait
1139  * until the connection is established. Timeout value > 0, will wait as
1140  * many ms.
1141  * After the connection is established a caller-supplied callback is called.
1142  * The callback is called even if timeout was set to K_FOREVER, the
1143  * callback is called before this function will return in this case.
1144  * The callback is not called if the timeout expires.
1145  * This is similar as BSD accept() function.
1146  *
1147  * @param context The context to use.
1148  * @param cb Caller-supplied callback function.
1149  * @param timeout Timeout for the connection. Possible values
1150  * are K_FOREVER, K_NO_WAIT, >0.
1151  * @param user_data Caller-supplied user data.
1152  *
1153  * @return 0 if ok, < 0 if error
1154  */
1155 int net_context_accept(struct net_context *context,
1156 		       net_tcp_accept_cb_t cb,
1157 		       k_timeout_t timeout,
1158 		       void *user_data);
1159 
1160 /**
1161  * @brief Send data to a peer.
1162  *
1163  * @details This function can be used to send network data to a peer
1164  * connection. After the network buffer is sent, a caller-supplied
1165  * callback is called. Note that the callback might be called after this
1166  * function has returned. For context of type SOCK_DGRAM, the destination
1167  * address must have been set by the call to net_context_connect().
1168  * This is similar as BSD send() function.
1169  *
1170  * @param context The network context to use.
1171  * @param buf The data buffer to send
1172  * @param len Length of the buffer
1173  * @param cb Caller-supplied callback function.
1174  * @param timeout Currently this value is not used.
1175  * @param user_data Caller-supplied user data.
1176  *
1177  * @return 0 if ok, < 0 if error
1178  */
1179 int net_context_send(struct net_context *context,
1180 		     const void *buf,
1181 		     size_t len,
1182 		     net_context_send_cb_t cb,
1183 		     k_timeout_t timeout,
1184 		     void *user_data);
1185 
1186 /**
1187  * @brief Send data to a peer specified by address.
1188  *
1189  * @details This function can be used to send network data to a peer
1190  * specified by address. This variant can only be used for datagram
1191  * connections of type SOCK_DGRAM. After the network buffer is sent,
1192  * a caller-supplied callback is called. Note that the callback might be
1193  * called after this function has returned.
1194  * This is similar as BSD sendto() function.
1195  *
1196  * @param context The network context to use.
1197  * @param buf The data buffer to send
1198  * @param len Length of the buffer
1199  * @param dst_addr Destination address.
1200  * @param addrlen Length of the address.
1201  * @param cb Caller-supplied callback function.
1202  * @param timeout Currently this value is not used.
1203  * @param user_data Caller-supplied user data.
1204  *
1205  * @return numbers of bytes sent on success, a negative errno otherwise
1206  */
1207 int net_context_sendto(struct net_context *context,
1208 		       const void *buf,
1209 		       size_t len,
1210 		       const struct sockaddr *dst_addr,
1211 		       socklen_t addrlen,
1212 		       net_context_send_cb_t cb,
1213 		       k_timeout_t timeout,
1214 		       void *user_data);
1215 
1216 /**
1217  * @brief Send data in iovec to a peer specified in msghdr struct.
1218  *
1219  * @details This function has similar semantics as Posix sendmsg() call.
1220  * For unconnected socket, the msg_name field in msghdr must be set. For
1221  * connected socket the msg_name should be set to NULL, and msg_namelen to 0.
1222  * After the network buffer is sent, a caller-supplied callback is called.
1223  * Note that the callback might be called after this function has returned.
1224  *
1225  * @param context The network context to use.
1226  * @param msghdr The data to send
1227  * @param flags Flags for the sending.
1228  * @param cb Caller-supplied callback function.
1229  * @param timeout Currently this value is not used.
1230  * @param user_data Caller-supplied user data.
1231  *
1232  * @return numbers of bytes sent on success, a negative errno otherwise
1233  */
1234 int net_context_sendmsg(struct net_context *context,
1235 			const struct msghdr *msghdr,
1236 			int flags,
1237 			net_context_send_cb_t cb,
1238 			k_timeout_t timeout,
1239 			void *user_data);
1240 
1241 /**
1242  * @brief Receive network data from a peer specified by context.
1243  *
1244  * @details This function can be used to register a callback function
1245  * that is called by the network stack when network data has been received
1246  * for this context. As this function registers a callback, then there
1247  * is no need to call this function multiple times if timeout is set to
1248  * K_NO_WAIT.
1249  * If callback function or user data changes, then the function can be called
1250  * multiple times to register new values.
1251  * This function will return immediately if the timeout is set to K_NO_WAIT.
1252  * If the timeout is set to K_FOREVER, the function will wait until the
1253  * network buffer is received. Timeout value > 0 will wait as many ms.
1254  * After the network buffer is received, a caller-supplied callback is
1255  * called. The callback is called even if timeout was set to K_FOREVER,
1256  * the callback is called before this function will return in this case.
1257  * The callback is not called if the timeout expires. The timeout functionality
1258  * can be compiled out if synchronous behavior is not needed. The sync call
1259  * logic requires some memory that can be saved if only async way of call is
1260  * used. If CONFIG_NET_CONTEXT_SYNC_RECV is not set, then the timeout parameter
1261  * value is ignored.
1262  * This is similar as BSD recv() function.
1263  * Note that net_context_bind() should be called before net_context_recv().
1264  * Default random port number is assigned to local port. Only bind() will
1265  * update connection information from context. If recv() is called before
1266  * bind() call, it may refuse to bind to a context which already has
1267  * a connection associated.
1268  *
1269  * @param context The network context to use.
1270  * @param cb Caller-supplied callback function.
1271  * @param timeout Caller-supplied timeout. Possible values
1272  * are K_FOREVER, K_NO_WAIT, >0.
1273  * @param user_data Caller-supplied user data.
1274  *
1275  * @return 0 if ok, < 0 if error
1276  */
1277 int net_context_recv(struct net_context *context,
1278 		     net_context_recv_cb_t cb,
1279 		     k_timeout_t timeout,
1280 		     void *user_data);
1281 
1282 /**
1283  * @brief Update TCP receive window for context.
1284  *
1285  * @details This function should be used by an application which
1286  * doesn't fully process incoming data in its receive callback,
1287  * but for example, queues it. In this case, receive callback
1288  * should decrease the window (call this function with a negative
1289  * value) by the size of queued data, and function(s) which dequeue
1290  * data - with positive value corresponding to the dequeued size.
1291  * For example, if receive callback gets a packet with the data
1292  * size of 256 and queues it, it should call this function with
1293  * delta of -256. If a function extracts 10 bytes of the queued
1294  * data, it should call it with delta of 10.
1295  *
1296  * @param context The TCP network context to use.
1297  * @param delta Size, in bytes, by which to increase TCP receive
1298  * window (negative value to decrease).
1299  *
1300  * @return 0 if ok, < 0 if error
1301  */
1302 int net_context_update_recv_wnd(struct net_context *context,
1303 				int32_t delta);
1304 
1305 /** @brief Network context options. These map to BSD socket option values. */
1306 enum net_context_option {
1307 	NET_OPT_PRIORITY          = 1,  /**< Context priority */
1308 	NET_OPT_TXTIME            = 2,  /**< TX time */
1309 	NET_OPT_SOCKS5            = 3,  /**< SOCKS5 */
1310 	NET_OPT_RCVTIMEO          = 4,  /**< Receive timeout */
1311 	NET_OPT_SNDTIMEO          = 5,  /**< Send timeout */
1312 	NET_OPT_RCVBUF            = 6,  /**< Receive buffer */
1313 	NET_OPT_SNDBUF            = 7,  /**< Send buffer */
1314 	NET_OPT_DSCP_ECN          = 8,  /**< DSCP ECN */
1315 	NET_OPT_REUSEADDR         = 9,  /**< Re-use address */
1316 	NET_OPT_REUSEPORT         = 10, /**< Re-use port */
1317 	NET_OPT_IPV6_V6ONLY       = 11, /**< Share IPv4 and IPv6 port space */
1318 	NET_OPT_RECV_PKTINFO      = 12, /**< Receive packet information */
1319 	NET_OPT_MCAST_TTL         = 13, /**< IPv4 multicast TTL */
1320 	NET_OPT_MCAST_HOP_LIMIT   = 14, /**< IPv6 multicast hop limit */
1321 	NET_OPT_UNICAST_HOP_LIMIT = 15, /**< IPv6 unicast hop limit */
1322 	NET_OPT_TTL               = 16, /**< IPv4 unicast TTL */
1323 	NET_OPT_ADDR_PREFERENCES  = 17, /**< IPv6 address preference */
1324 	NET_OPT_TIMESTAMPING      = 18, /**< Packet timestamping */
1325 	NET_OPT_MCAST_IFINDEX     = 19, /**< IPv6 multicast output network interface index */
1326 	NET_OPT_MTU               = 20, /**< IPv4 socket path MTU */
1327 	NET_OPT_LOCAL_PORT_RANGE  = 21, /**< Clamp local port range */
1328 };
1329 
1330 /**
1331  * @brief Set an connection option for this context.
1332  *
1333  * @param context The network context to use.
1334  * @param option Option to set
1335  * @param value Option value
1336  * @param len Option length
1337  *
1338  * @return 0 if ok, <0 if error
1339  */
1340 int net_context_set_option(struct net_context *context,
1341 			   enum net_context_option option,
1342 			   const void *value, size_t len);
1343 
1344 /**
1345  * @brief Get connection option value for this context.
1346  *
1347  * @param context The network context to use.
1348  * @param option Option to set
1349  * @param value Option value
1350  * @param len Option length (returned to caller)
1351  *
1352  * @return 0 if ok, <0 if error
1353  */
1354 int net_context_get_option(struct net_context *context,
1355 			   enum net_context_option option,
1356 			   void *value, size_t *len);
1357 
1358 /**
1359  * @typedef net_context_cb_t
1360  * @brief Callback used while iterating over network contexts
1361  *
1362  * @param context A valid pointer on current network context
1363  * @param user_data A valid pointer on some user data or NULL
1364  */
1365 typedef void (*net_context_cb_t)(struct net_context *context, void *user_data);
1366 
1367 /**
1368  * @brief Go through all the network connections and call callback
1369  * for each network context.
1370  *
1371  * @param cb User-supplied callback function to call.
1372  * @param user_data User specified data.
1373  */
1374 void net_context_foreach(net_context_cb_t cb, void *user_data);
1375 
1376 /**
1377  * @brief Set custom network buffer pools for context send operations
1378  *
1379  * Set custom network buffer pools used by the IP stack to allocate
1380  * network buffers used by the context when sending data to the
1381  * network. Using dedicated buffers may help make send operations on
1382  * a given context more reliable, e.g. not be subject to buffer
1383  * starvation due to operations on other network contexts. Buffer pools
1384  * are set per context, but several contexts may share the same buffers.
1385  * Note that there's no support for per-context custom receive packet
1386  * pools.
1387  *
1388  * @param context Context that will use the given net_buf pools.
1389  * @param tx_pool Pointer to the function that will return TX pool
1390  * to the caller. The TX pool is used when sending data to network.
1391  * There is one TX net_pkt for each network packet that is sent.
1392  * @param data_pool Pointer to the function that will return DATA pool
1393  * to the caller. The DATA pool is used to store data that is sent to
1394  * the network.
1395  */
1396 #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
net_context_setup_pools(struct net_context * context,net_pkt_get_slab_func_t tx_slab,net_pkt_get_pool_func_t data_pool)1397 static inline void net_context_setup_pools(struct net_context *context,
1398 					   net_pkt_get_slab_func_t tx_slab,
1399 					   net_pkt_get_pool_func_t data_pool)
1400 {
1401 	NET_ASSERT(context);
1402 
1403 	context->tx_slab = tx_slab;
1404 	context->data_pool = data_pool;
1405 }
1406 #else
1407 #define net_context_setup_pools(context, tx_pool, data_pool)
1408 #endif
1409 
1410 /**
1411  * @brief Check if a port is in use (bound)
1412  *
1413  * This function checks if a port is bound with respect to the specified
1414  * @p ip_proto and @p local_addr.
1415  *
1416  * @param ip_proto the IP protocol
1417  * @param local_port the port to check
1418  * @param local_addr the network address
1419  *
1420  * @return true if the port is bound
1421  * @return false if the port is not bound
1422  */
1423 bool net_context_port_in_use(enum net_ip_protocol ip_proto,
1424 	uint16_t local_port, const struct sockaddr *local_addr);
1425 
1426 #ifdef __cplusplus
1427 }
1428 #endif
1429 
1430 /**
1431  * @}
1432  */
1433 
1434 #endif /* ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_ */
1435