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