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