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