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