blob: 3163e20c5c6d0fcc31c127b4c4cc069d6dec4ceb [file] [log] [blame]
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001/** @file
2 * @brief Network context definitions
3 *
4 * An API for applications to define a network connection.
5 */
6
Jukka Rissanenc43212d2016-05-09 15:49:48 +03007/*
Tomasz Bursztykab70a4f52016-06-20 18:34:51 +02008 * Copyright (c) 2016 Intel Corporation
Jukka Rissanenc43212d2016-05-09 15:49:48 +03009 *
David B. Kinderac74d8b2017-01-18 17:01:01 -080010 * SPDX-License-Identifier: Apache-2.0
Jukka Rissanenc43212d2016-05-09 15:49:48 +030011 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
14#define ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_
Jukka Rissanen68ea9372016-11-12 00:13:24 +020015
Tomasz Bursztykad8323e12017-01-06 14:10:06 +010016/**
17 * @brief Application network context
18 * @defgroup net_context Application network context
Anas Nashifca9285d2017-11-17 11:43:53 -050019 * @ingroup networking
Tomasz Bursztykad8323e12017-01-06 14:10:06 +010020 * @{
21 */
22
Jukka Rissanen68ea9372016-11-12 00:13:24 +020023#include <kernel.h>
Anas Nashife1e05a22019-06-25 12:25:32 -040024#include <sys/atomic.h>
Jukka Rissanen68ea9372016-11-12 00:13:24 +020025
26#include <net/net_ip.h>
27#include <net/net_if.h>
28#include <net/net_stats.h>
29
30#ifdef __cplusplus
31extern "C" {
Jukka Rissanen13733f72016-06-22 16:44:55 +030032#endif
Jukka Rissanen68ea9372016-11-12 00:13:24 +020033
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) */
38enum 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 Rissanen66621792019-01-24 14:13:42 +020053#define NET_CONTEXT_FAMILY (BIT(3) | BIT(4) | BIT(5))
Jukka Rissanen68ea9372016-11-12 00:13:24 +020054
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +020055/** Type of the connection (datagram / stream / raw) */
56#define NET_CONTEXT_TYPE (BIT(6) | BIT(7))
Jukka Rissanen68ea9372016-11-12 00:13:24 +020057
58/** Remote address set */
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +020059#define NET_CONTEXT_REMOTE_ADDR_SET BIT(8)
Jukka Rissanen68ea9372016-11-12 00:13:24 +020060
Jukka Rissanend88f25b2019-10-17 16:42:22 +030061/** 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 Rissanen68ea9372016-11-12 00:13:24 +020067struct net_context;
68
69/**
Tomasz Bursztykad8323e12017-01-06 14:10:06 +010070 * @typedef net_context_recv_cb_t
Jukka Rissanen68ea9372016-11-12 00:13:24 +020071 * @brief Network data receive callback.
72 *
Jukka Rissanen31b5b142017-07-10 13:47:05 +030073 * @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 Rissanen68ea9372016-11-12 00:13:24 +020077 *
78 * @param context The context to use.
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +020079 * @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 Bursztyka4b78a252019-01-30 10:25:03 +010082 * @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 Ross96294b72017-01-10 10:37:58 -080084 * @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 Bursztykadb11fcd2017-04-05 08:37:44 +020086 * pkt parameter is set to NULL.
Jukka Rissanen68ea9372016-11-12 00:13:24 +020087 * @param user_data The user data given in net_recv() call.
88 */
89typedef void (*net_context_recv_cb_t)(struct net_context *context,
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +020090 struct net_pkt *pkt,
Tomasz Bursztyka4b78a252019-01-30 10:25:03 +010091 union net_ip_header *ip_hdr,
92 union net_proto_header *proto_hdr,
Jukka Rissanen68ea9372016-11-12 00:13:24 +020093 int status,
94 void *user_data);
95
96/**
Tomasz Bursztykad8323e12017-01-06 14:10:06 +010097 * @typedef net_context_send_cb_t
Jukka Rissanen68ea9372016-11-12 00:13:24 +020098 * @brief Network data send callback.
99 *
Jukka Rissanen31b5b142017-07-10 13:47:05 +0300100 * @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 Rissanen68ea9372016-11-12 00:13:24 +0200104 *
105 * @param context The context to use.
Tomasz Bursztyka874b1642019-03-14 11:50:31 +0100106 * @param status Value is set to >= 0: amount of data that was sent,
107 * < 0 there was an error sending data.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200108 * @param user_data The user data given in net_send() call.
109 */
110typedef void (*net_context_send_cb_t)(struct net_context *context,
111 int status,
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200112 void *user_data);
113
114/**
Tomasz Bursztykad8323e12017-01-06 14:10:06 +0100115 * @typedef net_tcp_accept_cb_t
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200116 * @brief Accept callback
117 *
Jukka Rissanen31b5b142017-07-10 13:47:05 +0300118 * @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 Rissanen68ea9372016-11-12 00:13:24 +0200123 *
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 Scotteb9055c2017-01-19 14:53:41 -0800130typedef 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 Rissanen68ea9372016-11-12 00:13:24 +0200135
Andy Ross7b27d4b2017-01-13 12:29:04 -0800136/**
Tomasz Bursztykad8323e12017-01-06 14:10:06 +0100137 * @typedef net_context_connect_cb_t
Andy Ross7b27d4b2017-01-13 12:29:04 -0800138 * @brief Connection callback.
139 *
140 * @details The connect callback is called after a connection is being
141 * established.
Jukka Rissanen31b5b142017-07-10 13:47:05 +0300142 * 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 Ross7b27d4b2017-01-13 12:29:04 -0800150 *
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 */
157typedef void (*net_context_connect_cb_t)(struct net_context *context,
158 int status,
159 void *user_data);
160
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +0200161/* 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 */
172typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
173
Tomasz Bursztykabf964cd2017-04-03 17:14:35 +0200174/* 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 Rissanen7719cee2017-02-15 21:41:44 +0200176 */
177/**
Tomasz Bursztykabf964cd2017-04-03 17:14:35 +0200178 * @typedef net_pkt_get_pool_func_t
Jukka Rissanen7719cee2017-02-15 21:41:44 +0200179 *
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 Bursztykabf964cd2017-04-03 17:14:35 +0200185typedef struct net_buf_pool *(*net_pkt_get_pool_func_t)(void);
Jukka Rissanen7719cee2017-02-15 21:41:44 +0200186
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200187struct net_tcp;
188
189struct 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 Boiefed960b2020-05-29 13:33:12 -0700197__net_socket struct net_context {
Paul Sokolovsky4877d652017-06-03 20:38:57 +0300198 /** 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 Ross5d6e0d42017-01-25 15:14:59 -0800205 /** Reference count
206 */
207 atomic_t refcount;
208
Jukka Rissanen93e51812018-06-28 16:40:10 +0300209 /** Internal lock for protecting this context from multiple access.
210 */
211 struct k_mutex lock;
212
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200213 /** Local endpoint address. Note that the values are in network byte
214 * order.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200215 */
216 struct sockaddr_ptr local;
217
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200218 /** Remote endpoint address. Note that the values are in network byte
219 * order.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200220 */
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 Ross7b27d4b2017-01-13 12:29:04 -0800236 /** Connect callback to be called when a connection has been
237 * established.
238 */
239 net_context_connect_cb_t connect_cb;
240
Tomasz Bursztykabf964cd2017-04-03 17:14:35 +0200241#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
Jukka Rissanen7719cee2017-02-15 21:41:44 +0200242 /** Get TX net_buf pool for this context.
243 */
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +0200244 net_pkt_get_slab_func_t tx_slab;
Jukka Rissanen7719cee2017-02-15 21:41:44 +0200245
246 /** Get DATA net_buf pool for this context.
247 */
Tomasz Bursztykabf964cd2017-04-03 17:14:35 +0200248 net_pkt_get_pool_func_t data_pool;
249#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
Jukka Rissanen7719cee2017-02-15 21:41:44 +0200250
Oleg Zhurakivskyy9666f222019-10-03 19:16:15 +0300251#if defined(CONFIG_NET_TCP1)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200252 /** TCP connection information */
253 struct net_tcp *tcp;
Oleg Zhurakivskyy9666f222019-10-03 19:16:15 +0300254#endif /* CONFIG_NET_TCP1 */
255
256#if defined(CONFIG_NET_TCP2)
257 /** TCP connection information */
258 void *tcp;
259#endif /* CONFIG_NET_TCP2 */
Paul Sokolovsky041d3872017-05-11 13:49:16 +0300260
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300261#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 Sokolovsky041d3872017-05-11 13:49:16 +0300268#if defined(CONFIG_NET_SOCKETS)
Jukka Rissanene98f5d32019-09-17 09:51:26 +0300269 /** BSD socket private data */
270 void *socket_data;
271
Paul Sokolovsky041d3872017-05-11 13:49:16 +0300272 /** Per-socket packet or connection queues */
273 union {
274 struct k_fifo recv_q;
275 struct k_fifo accept_q;
276 };
Robert Lubosa7c698d2018-07-02 16:14:27 +0200277
Paul Sokolovsky041d3872017-05-11 13:49:16 +0300278#endif /* CONFIG_NET_SOCKETS */
Gil Pitney42f51ef2018-07-10 11:07:05 -0700279
280#if defined(CONFIG_NET_OFFLOAD)
281 /** context for use by offload drivers */
282 void *offload_context;
283#endif /* CONFIG_NET_OFFLOAD */
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300284
Jukka Rissanen06b500b2019-06-18 15:35:45 +0300285#if defined(CONFIG_NET_SOCKETS_CAN)
286 int can_filter_id;
287#endif /* CONFIG_NET_SOCKETS_CAN */
288
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300289 /** Option values */
290 struct {
291#if defined(CONFIG_NET_CONTEXT_PRIORITY)
292 /** Priority of the network data sent via this net_context */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500293 uint8_t priority;
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300294#endif
Tomasz Bursztykadd01e992019-03-07 11:20:43 +0100295#if defined(CONFIG_NET_CONTEXT_TIMESTAMP)
296 bool timestamp;
297#endif
Jukka Rissanen0435dce2019-06-27 17:57:01 +0300298#if defined(CONFIG_NET_CONTEXT_TXTIME)
299 bool txtime;
300#endif
Ravi kumar Veeramallyc8fa1692019-08-01 16:00:54 +0300301#if defined(CONFIG_SOCKS)
302 struct {
303 struct sockaddr addr;
304 socklen_t addrlen;
305 } proxy;
306#endif
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300307 } options;
308
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200309 /** Protocol (UDP, TCP or IEEE 802.3 protocol value) */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500310 uint16_t proto;
Jukka Rissanen9b8c83f2018-07-25 13:22:49 +0300311
312 /** Flags for the context */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500313 uint16_t flags;
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200314
315 /** Network interface assigned to this context */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500316 int8_t iface;
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100317
318 /** IPv6 hop limit or IPv4 ttl for packets sent via this context. */
319 union {
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500320 uint8_t ipv6_hop_limit;
321 uint8_t ipv4_ttl;
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100322 };
Ravi kumar Veeramallyc8fa1692019-08-01 16:00:54 +0300323
324#if defined(CONFIG_SOCKS)
325 bool proxy_enabled;
326#endif
327
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200328};
329
330static 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 Rissanend88f25b2019-10-17 16:42:22 +0300337/**
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 */
344static 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 */
357static 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 */
376static 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 */
389static 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 Rissanen68ea9372016-11-12 00:13:24 +0200401#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 */
413static inline
414enum net_context_state net_context_get_state(struct net_context *context)
415{
416 NET_ASSERT(context);
417
Alexander Polletifb4cb3a2018-11-14 14:43:10 +0100418 return (enum net_context_state)
419 ((context->flags >> NET_CONTEXT_STATE_SHIFT) &
420 NET_CONTEXT_STATE_MASK);
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200421}
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 */
431static inline void net_context_set_state(struct net_context *context,
432 enum net_context_state state)
433{
434 NET_ASSERT(context);
435
Michael Scott22bd26c2017-01-10 08:28:23 -0800436 context->flags &= ~(NET_CONTEXT_STATE_MASK << NET_CONTEXT_STATE_SHIFT);
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200437 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 */
451static inline sa_family_t net_context_get_family(struct net_context *context)
452{
453 NET_ASSERT(context);
454
Jukka Rissanen66621792019-01-24 14:13:42 +0200455 return ((context->flags & NET_CONTEXT_FAMILY) >> 3);
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200456}
457
458/**
459 * @brief Set address family for this network context.
460 *
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200461 * @details This function sets the address family (IPv4, IPv6 or AF_PACKET)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200462 * of the context.
463 *
464 * @param context Network context.
Jukka Rissanen66621792019-01-24 14:13:42 +0200465 * @param family Address family (AF_INET, AF_INET6, AF_PACKET, AF_CAN)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200466 */
467static inline void net_context_set_family(struct net_context *context,
468 sa_family_t family)
469{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500470 uint8_t flag = 0U;
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200471
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200472 NET_ASSERT(context);
473
Jukka Rissanen66621792019-01-24 14:13:42 +0200474 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 Rissanen68ea9372016-11-12 00:13:24 +0200478 }
479
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200480 context->flags |= flag;
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200481}
482
483/**
484 * @brief Get context type for this network context.
485 *
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200486 * @details This function returns the context type (stream, datagram or raw)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200487 * of the context.
488 *
489 * @param context Network context.
490 *
491 * @return Network context type.
492 */
493static inline
494enum net_sock_type net_context_get_type(struct net_context *context)
495{
496 NET_ASSERT(context);
497
Loic Poulain10bdc2a2019-03-01 15:41:49 +0100498 return (enum net_sock_type)((context->flags & NET_CONTEXT_TYPE) >> 6);
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200499}
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 */
510static inline void net_context_set_type(struct net_context *context,
511 enum net_sock_type type)
512{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500513 uint16_t flag = 0U;
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200514
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200515 NET_ASSERT(context);
516
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200517 if (type == SOCK_DGRAM || type == SOCK_STREAM || type == SOCK_RAW) {
518 /* Type is in BIT(6) and BIT(7)*/
519 flag = type << 6;
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200520 }
521
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200522 context->flags |= flag;
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200523}
524
525/**
Jukka Rissanen06b500b2019-06-18 15:35:45 +0300526 * @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)
534static 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
542static 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)
560static 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
567static 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 Rissanen68ea9372016-11-12 00:13:24 +0200576 * @brief Get context IP protocol for this network context.
577 *
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200578 * @details This function returns the IP protocol (UDP / TCP /
579 * IEEE 802.3 protocol value) of the context.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200580 *
581 * @param context Network context.
582 *
583 * @return Network context IP protocol.
584 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500585static inline uint16_t net_context_get_ip_proto(struct net_context *context)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200586{
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200587 return context->proto;
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200588}
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 Veeramally337b6f92019-01-28 15:45:28 +0200597 * @param proto Context IP protocol (IPPROTO_UDP, IPPROTO_TCP or IEEE 802.3
598 * protocol value)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200599 */
600static inline void net_context_set_ip_proto(struct net_context *context,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500601 uint16_t proto)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200602{
Ravi kumar Veeramally337b6f92019-01-28 15:45:28 +0200603 context->proto = proto;
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200604}
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 */
616static inline
617struct 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 */
632static 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 Galaa1b77fd2020-05-27 11:26:57 -0500640static inline uint8_t net_context_get_ipv4_ttl(struct net_context *context)
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100641{
642 return context->ipv4_ttl;
643}
644
645static inline void net_context_set_ipv4_ttl(struct net_context *context,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500646 uint8_t ttl)
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100647{
648 context->ipv4_ttl = ttl;
649}
650
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500651static inline uint8_t net_context_get_ipv6_hop_limit(struct net_context *context)
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100652{
653 return context->ipv6_hop_limit;
654}
655
656static inline void net_context_set_ipv6_hop_limit(struct net_context *context,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500657 uint8_t hop_limit)
Tomasz Bursztyka64355532019-02-15 09:45:09 +0100658{
659 context->ipv6_hop_limit = hop_limit;
660}
661
Ravi kumar Veeramallyc8fa1692019-08-01 16:00:54 +0300662#if defined(CONFIG_SOCKS)
663static inline void net_context_set_proxy_enabled(struct net_context *context,
664 bool enable)
665{
666 context->proxy_enabled = enable;
667}
668
669static inline bool net_context_is_proxy_enabled(struct net_context *context)
670{
671 return context->proxy_enabled;
672}
673#else
674static 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
681static inline bool net_context_is_proxy_enabled(struct net_context *context)
682{
683 return false;
684}
685#endif
686
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200687/**
688 * @brief Get network context.
689 *
Andy Ross5d6e0d42017-01-25 15:14:59 -0800690 * @details Network context is used to define the connection 5-tuple
691 * (protocol, remote address, remote port, source address and source
Ravi kumar Veeramallydf201a02017-02-03 13:49:00 +0200692 * 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 Rissanen68ea9372016-11-12 00:13:24 +0200695 *
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 Veeramally337b6f92019-01-28 15:45:28 +0200698 * @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 Rissanen68ea9372016-11-12 00:13:24 +0200700 * @param context The allocated context is returned to the caller.
701 *
702 * @return 0 if ok, < 0 if error
703 */
704int net_context_get(sa_family_t family,
705 enum net_sock_type type,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500706 uint16_t ip_proto,
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200707 struct net_context **context);
708
709/**
Andy Ross5d6e0d42017-01-25 15:14:59 -0800710 * @brief Close and unref a network context.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200711 *
Andy Ross5d6e0d42017-01-25 15:14:59 -0800712 * @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 Rissanen68ea9372016-11-12 00:13:24 +0200717 *
718 * @param context The context to be closed.
719 *
720 * @return 0 if ok, < 0 if error
721 */
722int net_context_put(struct net_context *context);
723
724/**
Andy Ross5d6e0d42017-01-25 15:14:59 -0800725 * @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 */
736int 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 */
751int net_context_unref(struct net_context *context);
752
753/**
Tomasz Bursztyka68f7e962018-06-29 10:33:56 +0200754 * @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 Bursztyka0b173e32019-02-19 10:08:37 +0100761 * @return Return 0 on success, negative errno otherwise.
Tomasz Bursztyka68f7e962018-06-29 10:33:56 +0200762 */
763#if defined(CONFIG_NET_IPV4)
Tomasz Bursztyka40ad4b72019-01-14 10:57:42 +0100764int 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
769static 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 Bursztyka68f7e962018-06-29 10:33:56 +0200777
778/**
Tomasz Bursztykad309c872018-06-29 10:56:15 +0200779 * @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 Bursztyka0b173e32019-02-19 10:08:37 +0100786 * @return Return 0 on success, negative errno otherwise.
Tomasz Bursztykad309c872018-06-29 10:56:15 +0200787 */
788#if defined(CONFIG_NET_IPV6)
Tomasz Bursztyka40ad4b72019-01-14 10:57:42 +0100789int 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
794static 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 Bursztykad309c872018-06-29 10:56:15 +0200802
803/**
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200804 * @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 */
814int 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 */
828int net_context_listen(struct net_context *context,
829 int backlog);
830
831/**
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200832 * @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. Kinder2c850d72017-08-10 08:21:28 -0700836 * connection is established, the user-supplied callback (cb)
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200837 * 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 Scott72e013a2017-01-17 21:49:33 -0800857 * @return -ETIMEDOUT if the connect operation times out.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200858 */
859int net_context_connect(struct net_context *context,
860 const struct sockaddr *addr,
861 socklen_t addrlen,
862 net_context_connect_cb_t cb,
Jukka Rissanene56fa752020-04-03 10:48:00 +0300863 k_timeout_t timeout,
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200864 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. Kinder2c850d72017-08-10 08:21:28 -0700877 * After the connection is established a caller-supplied callback is called.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200878 * 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. Kinder2c850d72017-08-10 08:21:28 -0700884 * @param cb Caller-supplied callback function.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200885 * @param timeout Timeout for the connection. Possible values
886 * are K_FOREVER, K_NO_WAIT, >0.
David B. Kinder2c850d72017-08-10 08:21:28 -0700887 * @param user_data Caller-supplied user data.
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200888 *
889 * @return 0 if ok, < 0 if error
890 */
891int net_context_accept(struct net_context *context,
Michael Scotteb9055c2017-01-19 14:53:41 -0800892 net_tcp_accept_cb_t cb,
Jukka Rissanene56fa752020-04-03 10:48:00 +0300893 k_timeout_t timeout,
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200894 void *user_data);
895
896/**
Paul Sokolovsky97449852019-02-19 18:47:27 +0300897 * @brief Send data to a peer.
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100898 *
899 * @details This function can be used to send network data to a peer
Jukka Rissanencaa99b72019-08-05 13:20:40 +0300900 * 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 Bursztyka0d519f72018-12-13 14:05:21 +0100904 * 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 Rissanencaa99b72019-08-05 13:20:40 +0300910 * @param timeout Currently this value is not used.
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100911 * @param user_data Caller-supplied user data.
912 *
913 * @return 0 if ok, < 0 if error
914 */
Tomasz Bursztyka172fe5a2019-02-20 08:48:36 +0100915int net_context_send(struct net_context *context,
916 const void *buf,
917 size_t len,
918 net_context_send_cb_t cb,
Jukka Rissanene56fa752020-04-03 10:48:00 +0300919 k_timeout_t timeout,
Tomasz Bursztyka172fe5a2019-02-20 08:48:36 +0100920 void *user_data);
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100921
922/**
Paul Sokolovsky97449852019-02-19 18:47:27 +0300923 * @brief Send data to a peer specified by address.
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100924 *
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 Rissanencaa99b72019-08-05 13:20:40 +0300927 * 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 Bursztyka0d519f72018-12-13 14:05:21 +0100930 * 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 Sokolovsky97449852019-02-19 18:47:27 +0300935 * @param dst_addr Destination address.
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100936 * @param addrlen Length of the address.
937 * @param cb Caller-supplied callback function.
Jukka Rissanencaa99b72019-08-05 13:20:40 +0300938 * @param timeout Currently this value is not used.
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100939 * @param user_data Caller-supplied user data.
940 *
941 * @return numbers of bytes sent on success, a negative errno otherwise
942 */
Tomasz Bursztyka172fe5a2019-02-20 08:48:36 +0100943int 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 Rissanene56fa752020-04-03 10:48:00 +0300949 k_timeout_t timeout,
Tomasz Bursztyka172fe5a2019-02-20 08:48:36 +0100950 void *user_data);
Tomasz Bursztyka0d519f72018-12-13 14:05:21 +0100951
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200952/**
Jukka Rissanen390a6cf2019-06-27 17:48:10 +0300953 * @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 Rissanencaa99b72019-08-05 13:20:40 +0300958 * 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 Rissanen390a6cf2019-06-27 17:48:10 +0300960 *
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 Rissanencaa99b72019-08-05 13:20:40 +0300965 * @param timeout Currently this value is not used.
Jukka Rissanen390a6cf2019-06-27 17:48:10 +0300966 * @param user_data Caller-supplied user data.
967 *
968 * @return numbers of bytes sent on success, a negative errno otherwise
969 */
970int net_context_sendmsg(struct net_context *context,
971 const struct msghdr *msghdr,
972 int flags,
973 net_context_send_cb_t cb,
Jukka Rissanene56fa752020-04-03 10:48:00 +0300974 k_timeout_t timeout,
Jukka Rissanen390a6cf2019-06-27 17:48:10 +0300975 void *user_data);
976
977/**
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200978 * @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. Kinder2c850d72017-08-10 08:21:28 -0700990 * After the network buffer is received, a caller-supplied callback is
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200991 * 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. Kinderfc5f2b32017-05-02 17:21:56 -0700994 * can be compiled out if synchronous behavior is not needed. The sync call
Jukka Rissanen68ea9372016-11-12 00:13:24 +0200995 * 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 Veeramallydf201a02017-02-03 13:49:00 +0200999 * 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 Sokolovskye25df542017-12-28 15:40:21 +02001001 * update connection information from context. If recv() is called before
Ravi kumar Veeramallydf201a02017-02-03 13:49:00 +02001002 * bind() call, it may refuse to bind to a context which already has
1003 * a connection associated.
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001004 *
1005 * @param context The network context to use.
David B. Kinder2c850d72017-08-10 08:21:28 -07001006 * @param cb Caller-supplied callback function.
1007 * @param timeout Caller-supplied timeout. Possible values
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001008 * are K_FOREVER, K_NO_WAIT, >0.
David B. Kinder2c850d72017-08-10 08:21:28 -07001009 * @param user_data Caller-supplied user data.
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001010 *
1011 * @return 0 if ok, < 0 if error
1012 */
1013int net_context_recv(struct net_context *context,
1014 net_context_recv_cb_t cb,
Jukka Rissanene56fa752020-04-03 10:48:00 +03001015 k_timeout_t timeout,
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001016 void *user_data);
1017
Tomasz Bursztykad8323e12017-01-06 14:10:06 +01001018/**
Paul Sokolovsky19ff9632017-04-28 22:05:29 +03001019 * @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 */
1038int net_context_update_recv_wnd(struct net_context *context,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001039 int32_t delta);
Paul Sokolovsky19ff9632017-04-28 22:05:29 +03001040
Jukka Rissanen81d8d522018-02-07 14:44:04 +02001041enum net_context_option {
Tomasz Bursztykadd01e992019-03-07 11:20:43 +01001042 NET_OPT_PRIORITY = 1,
1043 NET_OPT_TIMESTAMP = 2,
Jukka Rissanen0435dce2019-06-27 17:57:01 +03001044 NET_OPT_TXTIME = 3,
Ravi kumar Veeramallyc8fa1692019-08-01 16:00:54 +03001045 NET_OPT_SOCKS5 = 4,
Jukka Rissanen81d8d522018-02-07 14:44:04 +02001046};
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 */
1058int 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 */
1072int net_context_get_option(struct net_context *context,
1073 enum net_context_option option,
1074 void *value, size_t *len);
1075
Paul Sokolovsky19ff9632017-04-28 22:05:29 +03001076/**
Tomasz Bursztykad8323e12017-01-06 14:10:06 +01001077 * @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 Rissanen68ea9372016-11-12 00:13:24 +02001083typedef 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. Kinder2c850d72017-08-10 08:21:28 -07001089 * @param cb User-supplied callback function to call.
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001090 * @param user_data User specified data.
1091 */
1092void net_context_foreach(net_context_cb_t cb, void *user_data);
1093
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001094/**
Paul Sokolovsky3e2444d2017-10-23 19:19:22 +03001095 * @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 Rissanen7719cee2017-02-15 21:41:44 +02001105 *
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 Bursztykadb11fcd2017-04-05 08:37:44 +02001109 * There is one TX net_pkt for each network packet that is sent.
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001110 * @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 Bursztykabf964cd2017-04-03 17:14:35 +02001114#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001115static inline void net_context_setup_pools(struct net_context *context,
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +02001116 net_pkt_get_slab_func_t tx_slab,
Tomasz Bursztykabf964cd2017-04-03 17:14:35 +02001117 net_pkt_get_pool_func_t data_pool)
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001118{
1119 NET_ASSERT(context);
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001120
Tomasz Bursztykadb11fcd2017-04-05 08:37:44 +02001121 context->tx_slab = tx_slab;
Jukka Rissanen7719cee2017-02-15 21:41:44 +02001122 context->data_pool = data_pool;
1123}
1124#else
1125#define net_context_setup_pools(context, tx_pool, data_pool)
1126#endif
1127
Christopher Friedt5c691492020-10-29 14:52:51 -04001128/**
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 */
1141bool net_context_port_in_use(enum net_ip_protocol ip_proto,
1142 uint16_t local_port, const struct sockaddr *local_addr);
1143
Jukka Rissanen68ea9372016-11-12 00:13:24 +02001144#ifdef __cplusplus
1145}
1146#endif
1147
Tomasz Bursztykad8323e12017-01-06 14:10:06 +01001148/**
1149 * @}
1150 */
1151
Flavio Ceolin67ca1762018-09-14 10:43:44 -07001152#endif /* ZEPHYR_INCLUDE_NET_NET_CONTEXT_H_ */