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 be 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_RCVTIMEO)
320 		/** Receive timeout */
321 		k_timeout_t rcvtimeo;
322 #endif
323 #if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
324 		/** Send timeout */
325 		k_timeout_t sndtimeo;
326 #endif
327 #if defined(CONFIG_NET_CONTEXT_RCVBUF)
328 		/** Receive buffer maximum size */
329 		uint16_t rcvbuf;
330 #endif
331 #if defined(CONFIG_NET_CONTEXT_SNDBUF)
332 		/** Send buffer maximum size */
333 		uint16_t sndbuf;
334 #endif
335 #if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
336 		/**
337 		 * DSCP (Differentiated Services Code point) and
338 		 * ECN (Explicit Congestion Notification) values.
339 		 */
340 		uint8_t dscp_ecn;
341 #endif
342 #if defined(CONFIG_NET_CONTEXT_REUSEADDR)
343 		/** Re-use address (SO_REUSEADDR) flag on a socket. */
344 		bool reuseaddr;
345 #endif
346 #if defined(CONFIG_NET_CONTEXT_REUSEPORT)
347 		/** Re-use port (SO_REUSEPORT) flag on a socket. */
348 		bool reuseport;
349 #endif
350 #if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
351 		/** Support v4-mapped-on-v6 addresses */
352 		bool ipv6_v6only;
353 #endif
354 #if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
355 		/** Receive network packet information in recvmsg() call */
356 		bool recv_pktinfo;
357 #endif
358 #if defined(CONFIG_NET_IPV6)
359 		/**
360 		 * Source address selection preferences. Currently used only for IPv6,
361 		 * see RFC 5014 for details.
362 		 */
363 		uint16_t addr_preferences;
364 #endif
365 #if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
366 		/** Enable RX, TX or both timestamps of packets send through sockets. */
367 		uint8_t timestamping;
368 #endif
369 	} options;
370 
371 	/** Protocol (UDP, TCP or IEEE 802.3 protocol value) */
372 	uint16_t proto;
373 
374 	/** Flags for the context */
375 	uint16_t flags;
376 
377 	/** Network interface assigned to this context */
378 	int8_t iface;
379 
380 	/** IPv6 hop limit or IPv4 ttl for packets sent via this context. */
381 	union {
382 		struct {
383 			uint8_t ipv6_hop_limit;       /**< IPv6 hop limit */
384 			uint8_t ipv6_mcast_hop_limit; /**< IPv6 multicast hop limit */
385 		};
386 		struct {
387 			uint8_t ipv4_ttl;       /**< IPv4 TTL */
388 			uint8_t ipv4_mcast_ttl; /**< IPv4 multicast TTL */
389 		};
390 	};
391 
392 #if defined(CONFIG_SOCKS)
393 	/** Is socks proxy enabled */
394 	bool proxy_enabled;
395 #endif
396 
397 };
398 
399 /**
400  * @brief Is this context used or not.
401  *
402  * @param context Network context.
403  *
404  * @return True if the context is currently in use, False otherwise.
405  */
net_context_is_used(struct net_context * context)406 static inline bool net_context_is_used(struct net_context *context)
407 {
408 	NET_ASSERT(context);
409 
410 	return context->flags & NET_CONTEXT_IN_USE;
411 }
412 
413 /**
414  * @brief Is this context bound to a network interface.
415  *
416  * @param context Network context.
417  *
418  * @return True if the context is bound to network interface, False otherwise.
419  */
net_context_is_bound_to_iface(struct net_context * context)420 static inline bool net_context_is_bound_to_iface(struct net_context *context)
421 {
422 	NET_ASSERT(context);
423 
424 	return context->flags & NET_CONTEXT_BOUND_TO_IFACE;
425 }
426 
427 /**
428  * @brief Is this context is accepting data now.
429  *
430  * @param context Network context.
431  *
432  * @return True if the context is accepting connections, False otherwise.
433  */
net_context_is_accepting(struct net_context * context)434 static inline bool net_context_is_accepting(struct net_context *context)
435 {
436 	NET_ASSERT(context);
437 
438 	return context->flags & NET_CONTEXT_ACCEPTING_SOCK;
439 }
440 
441 /**
442  * @brief Set this context to accept data now.
443  *
444  * @param context Network context.
445  * @param accepting True if accepting, False if not
446  */
net_context_set_accepting(struct net_context * context,bool accepting)447 static inline void net_context_set_accepting(struct net_context *context,
448 					     bool accepting)
449 {
450 	NET_ASSERT(context);
451 
452 	if (accepting) {
453 		context->flags |= NET_CONTEXT_ACCEPTING_SOCK;
454 	} else {
455 		context->flags &= (uint16_t)~NET_CONTEXT_ACCEPTING_SOCK;
456 	}
457 }
458 
459 /**
460  * @brief Is this context closing.
461  *
462  * @param context Network context.
463  *
464  * @return True if the context is closing, False otherwise.
465  */
net_context_is_closing(struct net_context * context)466 static inline bool net_context_is_closing(struct net_context *context)
467 {
468 	NET_ASSERT(context);
469 
470 	return context->flags & NET_CONTEXT_CLOSING_SOCK;
471 }
472 
473 /**
474  * @brief Set this context to closing.
475  *
476  * @param context Network context.
477  * @param closing True if closing, False if not
478  */
net_context_set_closing(struct net_context * context,bool closing)479 static inline void net_context_set_closing(struct net_context *context,
480 					   bool closing)
481 {
482 	NET_ASSERT(context);
483 
484 	if (closing) {
485 		context->flags |= NET_CONTEXT_CLOSING_SOCK;
486 	} else {
487 		context->flags &= (uint16_t)~NET_CONTEXT_CLOSING_SOCK;
488 	}
489 }
490 
491 /** @cond INTERNAL_HIDDEN */
492 
493 #define NET_CONTEXT_STATE_SHIFT 1
494 #define NET_CONTEXT_STATE_MASK 0x03
495 
496 /** @endcond */
497 
498 /**
499  * @brief Get state for this network context.
500  *
501  * @details This function returns the state of the context.
502  *
503  * @param context Network context.
504  *
505  * @return Network state.
506  */
507 static inline
net_context_get_state(struct net_context * context)508 enum net_context_state net_context_get_state(struct net_context *context)
509 {
510 	NET_ASSERT(context);
511 
512 	return (enum net_context_state)
513 		((context->flags >> NET_CONTEXT_STATE_SHIFT) &
514 		NET_CONTEXT_STATE_MASK);
515 }
516 
517 /**
518  * @brief Set state for this network context.
519  *
520  * @details This function sets the state of the context.
521  *
522  * @param context Network context.
523  * @param state New network context state.
524  */
net_context_set_state(struct net_context * context,enum net_context_state state)525 static inline void net_context_set_state(struct net_context *context,
526 					 enum net_context_state state)
527 {
528 	NET_ASSERT(context);
529 
530 	context->flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
531 	context->flags |= ((state & NET_CONTEXT_STATE_MASK) <<
532 			   NET_CONTEXT_STATE_SHIFT);
533 }
534 
535 /**
536  * @brief Get address family for this network context.
537  *
538  * @details This function returns the address family (IPv4 or IPv6)
539  * of the context.
540  *
541  * @param context Network context.
542  *
543  * @return Network state.
544  */
net_context_get_family(struct net_context * context)545 static inline sa_family_t net_context_get_family(struct net_context *context)
546 {
547 	NET_ASSERT(context);
548 
549 	return ((context->flags & NET_CONTEXT_FAMILY) >> 3);
550 }
551 
552 /**
553  * @brief Set address family for this network context.
554  *
555  * @details This function sets the address family (IPv4, IPv6 or AF_PACKET)
556  * of the context.
557  *
558  * @param context Network context.
559  * @param family Address family (AF_INET, AF_INET6, AF_PACKET, AF_CAN)
560  */
net_context_set_family(struct net_context * context,sa_family_t family)561 static inline void net_context_set_family(struct net_context *context,
562 					  sa_family_t family)
563 {
564 	uint8_t flag = 0U;
565 
566 	NET_ASSERT(context);
567 
568 	if (family == AF_UNSPEC || family == AF_INET || family == AF_INET6 ||
569 	    family == AF_PACKET || family == AF_CAN) {
570 		/* Family is in BIT(4), BIT(5) and BIT(6) */
571 		flag = (uint8_t)(family << 3);
572 	}
573 
574 	context->flags |= flag;
575 }
576 
577 /**
578  * @brief Get context type for this network context.
579  *
580  * @details This function returns the context type (stream, datagram or raw)
581  * of the context.
582  *
583  * @param context Network context.
584  *
585  * @return Network context type.
586  */
587 static inline
net_context_get_type(struct net_context * context)588 enum net_sock_type net_context_get_type(struct net_context *context)
589 {
590 	NET_ASSERT(context);
591 
592 	return (enum net_sock_type)((context->flags & NET_CONTEXT_TYPE) >> 6);
593 }
594 
595 /**
596  * @brief Set context type for this network context.
597  *
598  * @details This function sets the context type (stream or datagram)
599  * of the context.
600  *
601  * @param context Network context.
602  * @param type Context type (SOCK_STREAM or SOCK_DGRAM)
603  */
net_context_set_type(struct net_context * context,enum net_sock_type type)604 static inline void net_context_set_type(struct net_context *context,
605 					enum net_sock_type type)
606 {
607 	uint16_t flag = 0U;
608 
609 	NET_ASSERT(context);
610 
611 	if (type == SOCK_DGRAM || type == SOCK_STREAM || type == SOCK_RAW) {
612 		/* Type is in BIT(6) and BIT(7)*/
613 		flag = (uint16_t)(type << 6);
614 	}
615 
616 	context->flags |= flag;
617 }
618 
619 /**
620  * @brief Set CAN filter id for this network context.
621  *
622  * @details This function sets the CAN filter id of the context.
623  *
624  * @param context Network context.
625  * @param filter_id CAN filter id
626  */
627 #if defined(CONFIG_NET_SOCKETS_CAN)
net_context_set_can_filter_id(struct net_context * context,int filter_id)628 static inline void net_context_set_can_filter_id(struct net_context *context,
629 					     int filter_id)
630 {
631 	NET_ASSERT(context);
632 
633 	context->can_filter_id = filter_id;
634 }
635 #else
net_context_set_can_filter_id(struct net_context * context,int filter_id)636 static inline void net_context_set_can_filter_id(struct net_context *context,
637 					     int filter_id)
638 {
639 	ARG_UNUSED(context);
640 	ARG_UNUSED(filter_id);
641 }
642 #endif
643 
644 /**
645  * @brief Get CAN filter id for this network context.
646  *
647  * @details This function gets the CAN filter id of the context.
648  *
649  * @param context Network context.
650  *
651  * @return Filter id of this network context
652  */
653 #if defined(CONFIG_NET_SOCKETS_CAN)
net_context_get_can_filter_id(struct net_context * context)654 static inline int net_context_get_can_filter_id(struct net_context *context)
655 {
656 	NET_ASSERT(context);
657 
658 	return context->can_filter_id;
659 }
660 #else
net_context_get_can_filter_id(struct net_context * context)661 static inline int net_context_get_can_filter_id(struct net_context *context)
662 {
663 	ARG_UNUSED(context);
664 
665 	return -1;
666 }
667 #endif
668 
669 /**
670  * @brief Get context IP protocol for this network context.
671  *
672  * @details This function returns the IP protocol (UDP / TCP /
673  * IEEE 802.3 protocol value) of the context.
674  *
675  * @param context Network context.
676  *
677  * @return Network context IP protocol.
678  */
net_context_get_proto(struct net_context * context)679 static inline uint16_t net_context_get_proto(struct net_context *context)
680 {
681 	return context->proto;
682 }
683 
684 /**
685  * @brief Set context IP protocol for this network context.
686  *
687  * @details This function sets the context IP protocol (UDP / TCP)
688  * of the context.
689  *
690  * @param context Network context.
691  * @param proto Context IP protocol (IPPROTO_UDP, IPPROTO_TCP or IEEE 802.3
692  * protocol value)
693  */
net_context_set_proto(struct net_context * context,uint16_t proto)694 static inline void net_context_set_proto(struct net_context *context,
695 					 uint16_t proto)
696 {
697 	context->proto = proto;
698 }
699 
700 /**
701  * @brief Get network interface for this context.
702  *
703  * @details This function returns the used network interface.
704  *
705  * @param context Network context.
706  *
707  * @return Context network interface if context is bind to interface,
708  * NULL otherwise.
709  */
710 static inline
net_context_get_iface(struct net_context * context)711 struct net_if *net_context_get_iface(struct net_context *context)
712 {
713 	NET_ASSERT(context);
714 
715 	return net_if_get_by_index(context->iface);
716 }
717 
718 /**
719  * @brief Set network interface for this context.
720  *
721  * @details This function binds network interface to this context.
722  *
723  * @param context Network context.
724  * @param iface Network interface.
725  */
net_context_set_iface(struct net_context * context,struct net_if * iface)726 static inline void net_context_set_iface(struct net_context *context,
727 					 struct net_if *iface)
728 {
729 	NET_ASSERT(iface);
730 
731 	context->iface = (uint8_t)net_if_get_by_iface(iface);
732 }
733 
734 /**
735  * @brief Bind network interface to this context.
736  *
737  * @details This function binds network interface to this context.
738  *
739  * @param context Network context.
740  * @param iface Network interface.
741  */
net_context_bind_iface(struct net_context * context,struct net_if * iface)742 static inline void net_context_bind_iface(struct net_context *context,
743 					  struct net_if *iface)
744 {
745 	NET_ASSERT(iface);
746 
747 	context->flags |= NET_CONTEXT_BOUND_TO_IFACE;
748 	net_context_set_iface(context, iface);
749 }
750 
751 /**
752  * @brief Get IPv4 TTL (time-to-live) value for this context.
753  *
754  * @details This function returns the IPv4 TTL (time-to-live) value that is
755  *          set to this context.
756  *
757  * @param context Network context.
758  *
759  * @return IPv4 TTL value
760  */
net_context_get_ipv4_ttl(struct net_context * context)761 static inline uint8_t net_context_get_ipv4_ttl(struct net_context *context)
762 {
763 	return context->ipv4_ttl;
764 }
765 
766 /**
767  * @brief Set IPv4 TTL (time-to-live) value for this context.
768  *
769  * @details This function sets the IPv4 TTL (time-to-live) value for
770  *          this context.
771  *
772  * @param context Network context.
773  * @param ttl IPv4 time-to-live value.
774  */
net_context_set_ipv4_ttl(struct net_context * context,uint8_t ttl)775 static inline void net_context_set_ipv4_ttl(struct net_context *context,
776 					    uint8_t ttl)
777 {
778 	context->ipv4_ttl = ttl;
779 }
780 
781 /**
782  * @brief Get IPv4 multicast TTL (time-to-live) value for this context.
783  *
784  * @details This function returns the IPv4 multicast TTL (time-to-live) value
785  *          that is set to this context.
786  *
787  * @param context Network context.
788  *
789  * @return IPv4 multicast TTL value
790  */
net_context_get_ipv4_mcast_ttl(struct net_context * context)791 static inline uint8_t net_context_get_ipv4_mcast_ttl(struct net_context *context)
792 {
793 	return context->ipv4_mcast_ttl;
794 }
795 
796 /**
797  * @brief Set IPv4 multicast TTL (time-to-live) value for this context.
798  *
799  * @details This function sets the IPv4 multicast TTL (time-to-live) value for
800  *          this context.
801  *
802  * @param context Network context.
803  * @param ttl IPv4 multicast time-to-live value.
804  */
net_context_set_ipv4_mcast_ttl(struct net_context * context,uint8_t ttl)805 static inline void net_context_set_ipv4_mcast_ttl(struct net_context *context,
806 						  uint8_t ttl)
807 {
808 	context->ipv4_mcast_ttl = ttl;
809 }
810 
811 /**
812  * @brief Get IPv6 hop limit value for this context.
813  *
814  * @details This function returns the IPv6 hop limit value that is set to this
815  *          context.
816  *
817  * @param context Network context.
818  *
819  * @return IPv6 hop limit value
820  */
net_context_get_ipv6_hop_limit(struct net_context * context)821 static inline uint8_t net_context_get_ipv6_hop_limit(struct net_context *context)
822 {
823 	return context->ipv6_hop_limit;
824 }
825 
826 /**
827  * @brief Set IPv6 hop limit value for this context.
828  *
829  * @details This function sets the IPv6 hop limit value for this context.
830  *
831  * @param context Network context.
832  * @param hop_limit IPv6 hop limit value.
833  */
net_context_set_ipv6_hop_limit(struct net_context * context,uint8_t hop_limit)834 static inline void net_context_set_ipv6_hop_limit(struct net_context *context,
835 						  uint8_t hop_limit)
836 {
837 	context->ipv6_hop_limit = hop_limit;
838 }
839 
840 /**
841  * @brief Get IPv6 multicast hop limit value for this context.
842  *
843  * @details This function returns the IPv6 multicast hop limit value
844  *          that is set to this context.
845  *
846  * @param context Network context.
847  *
848  * @return IPv6 multicast hop limit value
849  */
net_context_get_ipv6_mcast_hop_limit(struct net_context * context)850 static inline uint8_t net_context_get_ipv6_mcast_hop_limit(struct net_context *context)
851 {
852 	return context->ipv6_mcast_hop_limit;
853 }
854 
855 /**
856  * @brief Set IPv6 multicast hop limit value for this context.
857  *
858  * @details This function sets the IPv6 multicast hop limit value for
859  *          this context.
860  *
861  * @param context Network context.
862  * @param hop_limit IPv6 multicast hop limit value.
863  */
net_context_set_ipv6_mcast_hop_limit(struct net_context * context,uint8_t hop_limit)864 static inline void net_context_set_ipv6_mcast_hop_limit(struct net_context *context,
865 							uint8_t hop_limit)
866 {
867 	context->ipv6_mcast_hop_limit = hop_limit;
868 }
869 
870 /**
871  * @brief Enable or disable socks proxy support for this context.
872  *
873  * @details This function either enables or disables socks proxy support for
874  *          this context.
875  *
876  * @param context Network context.
877  * @param enable Enable socks proxy or disable it.
878  */
879 #if defined(CONFIG_SOCKS)
net_context_set_proxy_enabled(struct net_context * context,bool enable)880 static inline void net_context_set_proxy_enabled(struct net_context *context,
881 						 bool enable)
882 {
883 	context->proxy_enabled = enable;
884 }
885 #else
net_context_set_proxy_enabled(struct net_context * context,bool enable)886 static inline void net_context_set_proxy_enabled(struct net_context *context,
887 						 bool enable)
888 {
889 	ARG_UNUSED(context);
890 	ARG_UNUSED(enable);
891 }
892 #endif
893 
894 /**
895  * @brief Is socks proxy support enabled or disabled for this context.
896  *
897  * @details This function returns current socks proxy status for
898  *          this context.
899  *
900  * @param context Network context.
901  *
902  * @return True if socks proxy is enabled for this context, False otherwise
903  */
904 #if defined(CONFIG_SOCKS)
net_context_is_proxy_enabled(struct net_context * context)905 static inline bool net_context_is_proxy_enabled(struct net_context *context)
906 {
907 	return context->proxy_enabled;
908 }
909 #else
net_context_is_proxy_enabled(struct net_context * context)910 static inline bool net_context_is_proxy_enabled(struct net_context *context)
911 {
912 	ARG_UNUSED(context);
913 	return false;
914 }
915 #endif
916 
917 /**
918  * @brief Get network context.
919  *
920  * @details Network context is used to define the connection 5-tuple
921  * (protocol, remote address, remote port, source address and source
922  * port). Random free port number will be assigned to source port when
923  * context is created. This is similar as BSD socket() function.
924  * The context will be created with a reference count of 1.
925  *
926  * @param family IP address family (AF_INET or AF_INET6)
927  * @param type Type of the socket, SOCK_STREAM or SOCK_DGRAM
928  * @param ip_proto IP protocol, IPPROTO_UDP or IPPROTO_TCP. For raw socket
929  * access, the value is the L2 protocol value from IEEE 802.3 (see ethernet.h)
930  * @param context The allocated context is returned to the caller.
931  *
932  * @return 0 if ok, < 0 if error
933  */
934 int net_context_get(sa_family_t family,
935 		    enum net_sock_type type,
936 		    uint16_t ip_proto,
937 		    struct net_context **context);
938 
939 /**
940  * @brief Close and unref a network context.
941  *
942  * @details This releases the context. It is not possible to send or
943  * receive data via this context after this call.  This is similar as
944  * BSD shutdown() function.  For legacy compatibility, this function
945  * will implicitly decrement the reference count and possibly destroy
946  * the context either now or when it reaches a final state.
947  *
948  * @param context The context to be closed.
949  *
950  * @return 0 if ok, < 0 if error
951  */
952 int net_context_put(struct net_context *context);
953 
954 /**
955  * @brief Take a reference count to a net_context, preventing destruction
956  *
957  * @details Network contexts are not recycled until their reference
958  * count reaches zero.  Note that this does not prevent any "close"
959  * behavior that results from errors or net_context_put.  It simply
960  * prevents the context from being recycled for further use.
961  *
962  * @param context The context on which to increment the reference count
963  *
964  * @return The new reference count
965  */
966 int net_context_ref(struct net_context *context);
967 
968 /**
969  * @brief Decrement the reference count to a network context
970  *
971  * @details Decrements the refcount.  If it reaches zero, the context
972  * will be recycled.  Note that this does not cause any
973  * network-visible "close" behavior (i.e. future packets to this
974  * connection may see TCP RST or ICMP port unreachable responses).  See
975  * net_context_put() for that.
976  *
977  * @param context The context on which to decrement the reference count
978  *
979  * @return The new reference count, zero if the context was destroyed
980  */
981 int net_context_unref(struct net_context *context);
982 
983 /**
984  * @brief Create IPv4 packet in provided net_pkt from context
985  *
986  * @param context Network context for a connection
987  * @param pkt Network packet
988  * @param src Source address, or NULL to choose a default
989  * @param dst Destination IPv4 address
990  *
991  * @return Return 0 on success, negative errno otherwise.
992  */
993 #if defined(CONFIG_NET_IPV4)
994 int net_context_create_ipv4_new(struct net_context *context,
995 				struct net_pkt *pkt,
996 				const struct in_addr *src,
997 				const struct in_addr *dst);
998 #else
net_context_create_ipv4_new(struct net_context * context,struct net_pkt * pkt,const struct in_addr * src,const struct in_addr * dst)999 static inline int net_context_create_ipv4_new(struct net_context *context,
1000 					      struct net_pkt *pkt,
1001 					      const struct in_addr *src,
1002 					      const struct in_addr *dst)
1003 {
1004 	return -1;
1005 }
1006 #endif /* CONFIG_NET_IPV4 */
1007 
1008 /**
1009  * @brief Create IPv6 packet in provided net_pkt from context
1010  *
1011  * @param context Network context for a connection
1012  * @param pkt Network packet
1013  * @param src Source address, or NULL to choose a default from context
1014  * @param dst Destination IPv6 address
1015  *
1016  * @return Return 0 on success, negative errno otherwise.
1017  */
1018 #if defined(CONFIG_NET_IPV6)
1019 int net_context_create_ipv6_new(struct net_context *context,
1020 				struct net_pkt *pkt,
1021 				const struct in6_addr *src,
1022 				const struct in6_addr *dst);
1023 #else
net_context_create_ipv6_new(struct net_context * context,struct net_pkt * pkt,const struct in6_addr * src,const struct in6_addr * dst)1024 static inline int net_context_create_ipv6_new(struct net_context *context,
1025 					      struct net_pkt *pkt,
1026 					      const struct in6_addr *src,
1027 					      const struct in6_addr *dst)
1028 {
1029 	ARG_UNUSED(context);
1030 	ARG_UNUSED(pkt);
1031 	ARG_UNUSED(src);
1032 	ARG_UNUSED(dst);
1033 	return -1;
1034 }
1035 #endif /* CONFIG_NET_IPV6 */
1036 
1037 /**
1038  * @brief Assign a socket a local address.
1039  *
1040  * @details This is similar as BSD bind() function.
1041  *
1042  * @param context The context to be assigned.
1043  * @param addr Address to assigned.
1044  * @param addrlen Length of the address.
1045  *
1046  * @return 0 if ok, < 0 if error
1047  */
1048 int net_context_bind(struct net_context *context,
1049 		     const struct sockaddr *addr,
1050 		     socklen_t addrlen);
1051 
1052 /**
1053  * @brief Mark the context as a listening one.
1054  *
1055  * @details This is similar as BSD listen() function.
1056  *
1057  * @param context The context to use.
1058  * @param backlog The size of the pending connections backlog.
1059  *
1060  * @return 0 if ok, < 0 if error
1061  */
1062 int net_context_listen(struct net_context *context,
1063 		       int backlog);
1064 
1065 /**
1066  * @brief            Create a network connection.
1067  *
1068  * @details          The net_context_connect function creates a network
1069  *                   connection to the host specified by addr. After the
1070  *                   connection is established, the user-supplied callback (cb)
1071  *                   is executed. cb is called even if the timeout was set to
1072  *                   K_FOREVER. cb is not called if the timeout expires.
1073  *                   For datagram sockets (SOCK_DGRAM), this function only sets
1074  *                   the peer address.
1075  *                   This function is similar to the BSD connect() function.
1076  *
1077  * @param context    The network context.
1078  * @param addr       The peer address to connect to.
1079  * @param addrlen    Peer address length.
1080  * @param cb         Callback function. Set to NULL if not required.
1081  * @param timeout    The timeout value for the connection. Possible values:
1082  *                   * K_NO_WAIT: this function will return immediately,
1083  *                   * K_FOREVER: this function will block until the
1084  *                                      connection is established,
1085  *                   * >0: this function will wait the specified ms.
1086  * @param user_data  Data passed to the callback function.
1087  *
1088  * @return           0 on success.
1089  * @return           -EINVAL if an invalid parameter is passed as an argument.
1090  * @return           -ENOTSUP if the operation is not supported or implemented.
1091  * @return           -ETIMEDOUT if the connect operation times out.
1092  */
1093 int net_context_connect(struct net_context *context,
1094 			const struct sockaddr *addr,
1095 			socklen_t addrlen,
1096 			net_context_connect_cb_t cb,
1097 			k_timeout_t timeout,
1098 			void *user_data);
1099 
1100 /**
1101  * @brief Accept a network connection attempt.
1102  *
1103  * @details Accept a connection being established. This function
1104  * will return immediately if the timeout is set to K_NO_WAIT.
1105  * In this case the context will call the supplied callback when ever
1106  * there is a connection established to this context. This is "a register
1107  * handler and forget" type of call (async).
1108  * If the timeout is set to K_FOREVER, the function will wait
1109  * until the connection is established. Timeout value > 0, will wait as
1110  * many ms.
1111  * After the connection is established a caller-supplied callback is called.
1112  * The callback is called even if timeout was set to K_FOREVER, the
1113  * callback is called before this function will return in this case.
1114  * The callback is not called if the timeout expires.
1115  * This is similar as BSD accept() function.
1116  *
1117  * @param context The context to use.
1118  * @param cb Caller-supplied callback function.
1119  * @param timeout Timeout for the connection. Possible values
1120  * are K_FOREVER, K_NO_WAIT, >0.
1121  * @param user_data Caller-supplied user data.
1122  *
1123  * @return 0 if ok, < 0 if error
1124  */
1125 int net_context_accept(struct net_context *context,
1126 		       net_tcp_accept_cb_t cb,
1127 		       k_timeout_t timeout,
1128 		       void *user_data);
1129 
1130 /**
1131  * @brief Send data to a peer.
1132  *
1133  * @details This function can be used to send network data to a peer
1134  * connection. After the network buffer is sent, a caller-supplied
1135  * callback is called. Note that the callback might be called after this
1136  * function has returned. For context of type SOCK_DGRAM, the destination
1137  * address must have been set by the call to net_context_connect().
1138  * This is similar as BSD send() function.
1139  *
1140  * @param context The network context to use.
1141  * @param buf The data buffer to send
1142  * @param len Length of the buffer
1143  * @param cb Caller-supplied callback function.
1144  * @param timeout Currently this value is not used.
1145  * @param user_data Caller-supplied user data.
1146  *
1147  * @return 0 if ok, < 0 if error
1148  */
1149 int net_context_send(struct net_context *context,
1150 		     const void *buf,
1151 		     size_t len,
1152 		     net_context_send_cb_t cb,
1153 		     k_timeout_t timeout,
1154 		     void *user_data);
1155 
1156 /**
1157  * @brief Send data to a peer specified by address.
1158  *
1159  * @details This function can be used to send network data to a peer
1160  * specified by address. This variant can only be used for datagram
1161  * connections of type SOCK_DGRAM. After the network buffer is sent,
1162  * a caller-supplied callback is called. Note that the callback might be
1163  * called after this function has returned.
1164  * This is similar as BSD sendto() function.
1165  *
1166  * @param context The network context to use.
1167  * @param buf The data buffer to send
1168  * @param len Length of the buffer
1169  * @param dst_addr Destination address.
1170  * @param addrlen Length of the address.
1171  * @param cb Caller-supplied callback function.
1172  * @param timeout Currently this value is not used.
1173  * @param user_data Caller-supplied user data.
1174  *
1175  * @return numbers of bytes sent on success, a negative errno otherwise
1176  */
1177 int net_context_sendto(struct net_context *context,
1178 		       const void *buf,
1179 		       size_t len,
1180 		       const struct sockaddr *dst_addr,
1181 		       socklen_t addrlen,
1182 		       net_context_send_cb_t cb,
1183 		       k_timeout_t timeout,
1184 		       void *user_data);
1185 
1186 /**
1187  * @brief Send data in iovec to a peer specified in msghdr struct.
1188  *
1189  * @details This function has similar semantics as Posix sendmsg() call.
1190  * For unconnected socket, the msg_name field in msghdr must be set. For
1191  * connected socket the msg_name should be set to NULL, and msg_namelen to 0.
1192  * After the network buffer is sent, a caller-supplied callback is called.
1193  * Note that the callback might be called after this function has returned.
1194  *
1195  * @param context The network context to use.
1196  * @param msghdr The data to send
1197  * @param flags Flags for the sending.
1198  * @param cb Caller-supplied callback function.
1199  * @param timeout Currently this value is not used.
1200  * @param user_data Caller-supplied user data.
1201  *
1202  * @return numbers of bytes sent on success, a negative errno otherwise
1203  */
1204 int net_context_sendmsg(struct net_context *context,
1205 			const struct msghdr *msghdr,
1206 			int flags,
1207 			net_context_send_cb_t cb,
1208 			k_timeout_t timeout,
1209 			void *user_data);
1210 
1211 /**
1212  * @brief Receive network data from a peer specified by context.
1213  *
1214  * @details This function can be used to register a callback function
1215  * that is called by the network stack when network data has been received
1216  * for this context. As this function registers a callback, then there
1217  * is no need to call this function multiple times if timeout is set to
1218  * K_NO_WAIT.
1219  * If callback function or user data changes, then the function can be called
1220  * multiple times to register new values.
1221  * This function will return immediately if the timeout is set to K_NO_WAIT.
1222  * If the timeout is set to K_FOREVER, the function will wait until the
1223  * network buffer is received. Timeout value > 0 will wait as many ms.
1224  * After the network buffer is received, a caller-supplied callback is
1225  * called. The callback is called even if timeout was set to K_FOREVER,
1226  * the callback is called before this function will return in this case.
1227  * The callback is not called if the timeout expires. The timeout functionality
1228  * can be compiled out if synchronous behavior is not needed. The sync call
1229  * logic requires some memory that can be saved if only async way of call is
1230  * used. If CONFIG_NET_CONTEXT_SYNC_RECV is not set, then the timeout parameter
1231  * value is ignored.
1232  * This is similar as BSD recv() function.
1233  * Note that net_context_bind() should be called before net_context_recv().
1234  * Default random port number is assigned to local port. Only bind() will
1235  * update connection information from context. If recv() is called before
1236  * bind() call, it may refuse to bind to a context which already has
1237  * a connection associated.
1238  *
1239  * @param context The network context to use.
1240  * @param cb Caller-supplied callback function.
1241  * @param timeout Caller-supplied timeout. Possible values
1242  * are K_FOREVER, K_NO_WAIT, >0.
1243  * @param user_data Caller-supplied user data.
1244  *
1245  * @return 0 if ok, < 0 if error
1246  */
1247 int net_context_recv(struct net_context *context,
1248 		     net_context_recv_cb_t cb,
1249 		     k_timeout_t timeout,
1250 		     void *user_data);
1251 
1252 /**
1253  * @brief Update TCP receive window for context.
1254  *
1255  * @details This function should be used by an application which
1256  * doesn't fully process incoming data in its receive callback,
1257  * but for example, queues it. In this case, receive callback
1258  * should decrease the window (call this function with a negative
1259  * value) by the size of queued data, and function(s) which dequeue
1260  * data - with positive value corresponding to the dequeued size.
1261  * For example, if receive callback gets a packet with the data
1262  * size of 256 and queues it, it should call this function with
1263  * delta of -256. If a function extracts 10 bytes of the queued
1264  * data, it should call it with delta of 10.
1265  *
1266  * @param context The TCP network context to use.
1267  * @param delta Size, in bytes, by which to increase TCP receive
1268  * window (negative value to decrease).
1269  *
1270  * @return 0 if ok, < 0 if error
1271  */
1272 int net_context_update_recv_wnd(struct net_context *context,
1273 				int32_t delta);
1274 
1275 /** @brief Network context options. These map to BSD socket option values. */
1276 enum net_context_option {
1277 	NET_OPT_PRIORITY          = 1,  /**< Context priority */
1278 	NET_OPT_TXTIME            = 2,  /**< TX time */
1279 	NET_OPT_SOCKS5            = 3,  /**< SOCKS5 */
1280 	NET_OPT_RCVTIMEO          = 4,  /**< Receive timeout */
1281 	NET_OPT_SNDTIMEO          = 5,  /**< Send timeout */
1282 	NET_OPT_RCVBUF            = 6,  /**< Receive buffer */
1283 	NET_OPT_SNDBUF            = 7,  /**< Send buffer */
1284 	NET_OPT_DSCP_ECN          = 8,  /**< DSCP ECN */
1285 	NET_OPT_REUSEADDR         = 9,  /**< Re-use address */
1286 	NET_OPT_REUSEPORT         = 10, /**< Re-use port */
1287 	NET_OPT_IPV6_V6ONLY       = 11, /**< Share IPv4 and IPv6 port space */
1288 	NET_OPT_RECV_PKTINFO      = 12, /**< Receive packet information */
1289 	NET_OPT_MCAST_TTL         = 13, /**< IPv4 multicast TTL */
1290 	NET_OPT_MCAST_HOP_LIMIT   = 14, /**< IPv6 multicast hop limit */
1291 	NET_OPT_UNICAST_HOP_LIMIT = 15, /**< IPv6 unicast hop limit */
1292 	NET_OPT_TTL               = 16, /**< IPv4 unicast TTL */
1293 	NET_OPT_ADDR_PREFERENCES  = 17, /**< IPv6 address preference */
1294 	NET_OPT_TIMESTAMPING      = 18, /**< Packet timestamping */
1295 };
1296 
1297 /**
1298  * @brief Set an connection option for this context.
1299  *
1300  * @param context The network context to use.
1301  * @param option Option to set
1302  * @param value Option value
1303  * @param len Option length
1304  *
1305  * @return 0 if ok, <0 if error
1306  */
1307 int net_context_set_option(struct net_context *context,
1308 			   enum net_context_option option,
1309 			   const void *value, size_t len);
1310 
1311 /**
1312  * @brief Get connection option value for this context.
1313  *
1314  * @param context The network context to use.
1315  * @param option Option to set
1316  * @param value Option value
1317  * @param len Option length (returned to caller)
1318  *
1319  * @return 0 if ok, <0 if error
1320  */
1321 int net_context_get_option(struct net_context *context,
1322 			   enum net_context_option option,
1323 			   void *value, size_t *len);
1324 
1325 /**
1326  * @typedef net_context_cb_t
1327  * @brief Callback used while iterating over network contexts
1328  *
1329  * @param context A valid pointer on current network context
1330  * @param user_data A valid pointer on some user data or NULL
1331  */
1332 typedef void (*net_context_cb_t)(struct net_context *context, void *user_data);
1333 
1334 /**
1335  * @brief Go through all the network connections and call callback
1336  * for each network context.
1337  *
1338  * @param cb User-supplied callback function to call.
1339  * @param user_data User specified data.
1340  */
1341 void net_context_foreach(net_context_cb_t cb, void *user_data);
1342 
1343 /**
1344  * @brief Set custom network buffer pools for context send operations
1345  *
1346  * Set custom network buffer pools used by the IP stack to allocate
1347  * network buffers used by the context when sending data to the
1348  * network. Using dedicated buffers may help make send operations on
1349  * a given context more reliable, e.g. not be subject to buffer
1350  * starvation due to operations on other network contexts. Buffer pools
1351  * are set per context, but several contexts may share the same buffers.
1352  * Note that there's no support for per-context custom receive packet
1353  * pools.
1354  *
1355  * @param context Context that will use the given net_buf pools.
1356  * @param tx_pool Pointer to the function that will return TX pool
1357  * to the caller. The TX pool is used when sending data to network.
1358  * There is one TX net_pkt for each network packet that is sent.
1359  * @param data_pool Pointer to the function that will return DATA pool
1360  * to the caller. The DATA pool is used to store data that is sent to
1361  * the network.
1362  */
1363 #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)1364 static inline void net_context_setup_pools(struct net_context *context,
1365 					   net_pkt_get_slab_func_t tx_slab,
1366 					   net_pkt_get_pool_func_t data_pool)
1367 {
1368 	NET_ASSERT(context);
1369 
1370 	context->tx_slab = tx_slab;
1371 	context->data_pool = data_pool;
1372 }
1373 #else
1374 #define net_context_setup_pools(context, tx_pool, data_pool)
1375 #endif
1376 
1377 /**
1378  * @brief Check if a port is in use (bound)
1379  *
1380  * This function checks if a port is bound with respect to the specified
1381  * @p ip_proto and @p local_addr.
1382  *
1383  * @param ip_proto the IP protocol
1384  * @param local_port the port to check
1385  * @param local_addr the network address
1386  *
1387  * @return true if the port is bound
1388  * @return false if the port is not bound
1389  */
1390 bool net_context_port_in_use(enum net_ip_protocol ip_proto,
1391 	uint16_t local_port, const struct sockaddr *local_addr);
1392 
1393 #ifdef __cplusplus
1394 }
1395 #endif
1396 
1397 /**
1398  * @}
1399  */
1400 
1401 #endif /* ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_ */
1402