blob: 2c12d72740760b03fd773bd61994a4bf3c80dd0f [file] [log] [blame]
Jukka Rissanena1be6a82017-05-10 22:23:29 +03001/** @file
2 * @brief Common routines needed in various network applications.
3 */
4
5/*
6 * Copyright (c) 2017 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#ifndef __NET_APP_H
12#define __NET_APP_H
13
Jukka Rissanen098483d2017-08-26 01:04:02 +030014#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
Jukka Rissanena1be6a82017-05-10 22:23:29 +030015#if defined(CONFIG_MBEDTLS)
16#if !defined(CONFIG_MBEDTLS_CFG_FILE)
17#include "mbedtls/config.h"
18#else
19#include CONFIG_MBEDTLS_CFG_FILE
20#endif /* CONFIG_MBEDTLS_CFG_FILE */
21
22#if defined(MBEDTLS_PLATFORM_C)
23#include "mbedtls/platform.h"
24#else
25#include <stdlib.h>
26#define mbedtls_time_t time_t
27#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
28#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
29#endif /* MBEDTLS_PLATFORM_C */
30
31#include <mbedtls/ssl_cookie.h>
32#include <mbedtls/entropy.h>
33#include <mbedtls/ctr_drbg.h>
34#include <mbedtls/net_sockets.h>
35#include <mbedtls/x509.h>
36#include <mbedtls/x509_crt.h>
37#include <mbedtls/ssl.h>
38#include <mbedtls/error.h>
39#include <mbedtls/debug.h>
40#endif /* CONFIG_MBEDTLS */
Jukka Rissanen098483d2017-08-26 01:04:02 +030041#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
Jukka Rissanena1be6a82017-05-10 22:23:29 +030042
43#include <net/net_ip.h>
44#include <net/net_pkt.h>
45#include <net/net_context.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
Jukka Rissanen539b0c42017-07-20 16:29:45 +030051/**
52 * @brief Network application library
53 * @defgroup net_app Network Application Library
Anas Nashifca9285d2017-11-17 11:43:53 -050054 * @ingroup networking
Jukka Rissanen539b0c42017-07-20 16:29:45 +030055 * @{
56 */
57
Jukka Rissanena1be6a82017-05-10 22:23:29 +030058/** Flags that tell what kind of functionality is needed by the application. */
59#define NET_APP_NEED_ROUTER 0x00000001
60#define NET_APP_NEED_IPV6 0x00000002
61#define NET_APP_NEED_IPV4 0x00000004
62
63enum net_app_type {
64 NET_APP_UNSPEC = 0,
65 NET_APP_SERVER,
66 NET_APP_CLIENT,
67};
68
69struct net_app_ctx;
70
71/**
72 * @typedef net_app_recv_cb_t
73 * @brief Network data receive callback.
74 *
75 * @details The recv callback is called after a network data is
76 * received.
77 *
78 * @param ctx The context to use.
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.
82 * @param status Value is set to 0 if some data or the connection is
83 * at EOF, <0 if there was an error receiving data, in this case the
84 * pkt parameter is set to NULL.
85 * @param user_data The user data given in init call.
86 */
87typedef void (*net_app_recv_cb_t)(struct net_app_ctx *ctx,
88 struct net_pkt *pkt,
89 int status,
90 void *user_data);
91
92/**
93 * @typedef net_app_connect_cb_t
94 * @brief Connection callback.
95 *
96 * @details The connect callback is called after a connection is being
97 * established.
98 *
99 * @param ctx The context to use.
100 * @param status Status of the connection establishment. This is 0
101 * if the connection was established successfully, <0 if there was an
102 * error.
103 * @param user_data The user data given in init call.
104 */
105typedef void (*net_app_connect_cb_t)(struct net_app_ctx *ctx,
106 int status,
107 void *user_data);
108
109/**
110 * @typedef net_app_send_cb_t
111 * @brief Network data send callback.
112 *
113 * @details The send callback is called after a network data is
114 * sent.
115 *
116 * @param ctx The context to use.
117 * @param status Value is set to 0 if all data was sent ok, <0 if
118 * there was an error sending data. >0 amount of data that was
119 * sent when not all data was sent ok.
120 * @param user_data_send The user data given in net_app_send() call.
121 * @param user_data The user data given in init call.
122 */
123typedef void (*net_app_send_cb_t)(struct net_app_ctx *ctx,
124 int status,
125 void *user_data_send,
126 void *user_data);
127
128/**
129 * @typedef net_app_close_cb_t
130 * @brief Close callback.
131 *
132 * @details The close callback is called after a connection is being
133 * shutdown.
134 *
135 * @param ctx The context to use.
136 * @param status Error code for the closing.
137 * @param user_data The user data given in init call.
138 */
139typedef void (*net_app_close_cb_t)(struct net_app_ctx *ctx,
140 int status,
141 void *user_data);
142
143/** Network application callbacks */
144struct net_app_cb {
145 /** Function that is called when a connection is established.
146 */
147 net_app_connect_cb_t connect;
148
149 /** Function that is called when data is received from network.
150 */
151 net_app_recv_cb_t recv;
152
153 /** Function that is called when net_pkt is sent.
154 */
155 net_app_send_cb_t send;
156
157 /** Function that is called when connection is shutdown.
158 */
159 net_app_close_cb_t close;
160};
161
162/* This is the same prototype as what net_context_sendto() has
163 * so that we can override the sending of the data for TLS traffic.
164 */
165typedef int (*net_app_send_data_t)(struct net_pkt *pkt,
166 const struct sockaddr *dst_addr,
167 socklen_t addrlen,
168 net_context_send_cb_t cb,
169 s32_t timeout,
170 void *token,
171 void *user_data);
172
Jukka Rissanen098483d2017-08-26 01:04:02 +0300173#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300174/* Internal information for managing TLS data */
175struct tls_context {
176 struct net_pkt *rx_pkt;
Jukka Rissanen4fea5b12017-08-25 12:51:03 +0300177 struct net_buf *hdr; /* IP + UDP/TCP header */
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300178 struct net_buf *frag;
179 struct k_sem tx_sem;
180 struct k_fifo tx_rx_fifo;
181 int remaining;
Jukka Rissanenf3f32e82017-07-18 14:02:32 +0300182#if defined(CONFIG_NET_APP_DTLS) && defined(CONFIG_NET_APP_SERVER)
183 char client_id;
184#endif
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300185};
186
187/* This struct is used to pass data to TLS thread when reading or sending
188 * data.
189 */
190struct net_app_fifo_block {
191 struct k_mem_block block;
192 struct net_pkt *pkt;
193 void *token; /* Used when sending data */
194 net_context_send_cb_t cb;
195 u8_t dir;
196};
197
198#define NET_APP_TLS_POOL_DEFINE(name, count) \
199 K_MEM_POOL_DEFINE(name, sizeof(struct net_app_fifo_block), \
200 sizeof(struct net_app_fifo_block), count, sizeof(int))
201
202#if defined(CONFIG_NET_APP_SERVER)
203/**
204 * @typedef net_app_cert_cb_t
205 * @brief Callback used when the API user needs to setup the certs.
206 *
207 * @param ctx Net app context.
208 * @param cert MBEDTLS certificate
209 * @param pkey MBEDTLS private key
210 *
211 * @return 0 if ok, <0 if there is an error
212 */
213typedef int (*net_app_cert_cb_t)(struct net_app_ctx *ctx,
214 mbedtls_x509_crt *cert,
215 mbedtls_pk_context *pkey);
216#endif /* CONFIG_NET_APP_SERVER */
217
218#if defined(CONFIG_NET_APP_CLIENT)
219/**
220 * @typedef net_app_ca_cert_cb_t
221 * @brief Callback used when the API user needs to setup certs.
222 *
223 * @param ctx Net app client context.
224 * @param ca_cert MBEDTLS certificate. This is of type mbedtls_x509_crt
225 * if MBEDTLS_X509_CRT_PARSE_C is defined.
226 *
227 * @return 0 if ok, <0 if there is an error
228 */
229typedef int (*net_app_ca_cert_cb_t)(struct net_app_ctx *ctx,
230 void *ca_cert);
231#endif /* CONFIG_NET_APP_CLIENT */
232
233/**
234 * @typedef net_app_entropy_src_cb_t
235 * @brief Callback used when the API user needs to setup the entropy source.
236 * @details This is the same as mbedtls_entropy_f_source_ptr callback.
237 *
238 * @param data Callback-specific data pointer
239 * @param output Data to fill
240 * @param len Maximum size to provide
241 * @param olen The actual amount of bytes put into the buffer (Can be 0)
242 */
243typedef int (*net_app_entropy_src_cb_t)(void *data, unsigned char *output,
244 size_t len, size_t *olen);
Jukka Rissanen098483d2017-08-26 01:04:02 +0300245#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300246
Jukka Rissanenf3f32e82017-07-18 14:02:32 +0300247#if defined(CONFIG_NET_APP_DTLS)
248struct dtls_timing_context {
249 u32_t snapshot;
250 u32_t int_ms;
251 u32_t fin_ms;
252};
253#endif /* CONFIG_NET_APP_DTLS */
254
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300255/* Information for the context and local/remote addresses used. */
256struct net_app_endpoint {
257 /** Network context. */
258 struct net_context *ctx;
259
260 /** Local address */
261 struct sockaddr local;
262
263 /** Remote address */
264 struct sockaddr remote;
265};
266
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300267/** Network application context. */
268struct net_app_ctx {
269#if defined(CONFIG_NET_IPV6)
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300270 struct net_app_endpoint ipv6;
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300271#endif
272#if defined(CONFIG_NET_IPV4)
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300273 struct net_app_endpoint ipv4;
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300274#endif
275
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300276 /** What is the default endpoint for this context. */
277 struct net_app_endpoint *default_ctx;
278
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300279 /** Internal function that is called when user data is sent to
280 * network. By default this is set to net_context_sendto() but
281 * is overridden for TLS as it requires special handling.
282 */
283 net_app_send_data_t send_data;
284
285 /** Connection callbacks */
286 struct net_app_cb cb;
287
288 /** Internal function that is called when data is received from
289 * network. This will do what ever needed and then pass data to
290 * application.
291 */
292 net_context_recv_cb_t recv_cb;
293
Jukka Rissanenf3f32e82017-07-18 14:02:32 +0300294#if defined(CONFIG_NET_APP_DTLS)
295 struct {
296 /** Currently active network context. This will contain the
297 * new context that is created after connection is established
298 * when UDP and DTLS is used.
299 */
300 struct net_context *ctx;
301
302 /** DTLS final timer. Connection is terminated if this expires.
303 */
304 struct k_delayed_work fin_timer;
Jukka Rissanenf3f32e82017-07-18 14:02:32 +0300305 } dtls;
306#endif
307
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300308#if defined(CONFIG_NET_APP_SERVER)
309 struct {
310#if defined(CONFIG_NET_TCP)
Jukka Rissanen8c5f25d2017-09-07 16:16:30 +0300311 /** Currently active network contexts. This will contain the
312 * new contexts that are created after connection is accepted
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300313 * when TCP is enabled.
314 */
Jukka Rissanen8c5f25d2017-09-07 16:16:30 +0300315 struct net_context *net_ctxs[CONFIG_NET_APP_SERVER_NUM_CONN];
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300316#endif
317 } server;
318#endif /* CONFIG_NET_APP_SERVER */
319
320#if defined(CONFIG_NET_APP_CLIENT)
321 struct {
322 /** Connect waiter */
323 struct k_sem connect_wait;
324
325#if defined(CONFIG_DNS_RESOLVER)
326 /** DNS resolver waiter */
327 struct k_sem dns_wait;
328
329 /** DNS query id. This is needed if the query needs to be
330 * cancelled.
331 */
332 u16_t dns_id;
333#endif
334 } client;
335#endif /* CONFIG_NET_APP_CLIENT */
336
Jukka Rissanen098483d2017-08-26 01:04:02 +0300337#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300338 struct {
339 /** TLS stack for mbedtls library. */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700340 k_thread_stack_t *stack;
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300341
342 /** TLS stack size. */
343 int stack_size;
344
345 /** TLS thread id */
346 k_tid_t tid;
347
348 /** TLS thread */
349 struct k_thread thread;
350
351 /** Memory pool for RX data */
352 struct k_mem_pool *pool;
353
354 /** Where the encrypted request is stored, this is to be
355 * provided by the user.
356 */
357 u8_t *request_buf;
358
359 /** Hostname to be used in the certificate verification */
360 const char *cert_host;
361
362 /** Request buffer maximum length */
363 size_t request_buf_len;
364
365 /** mbedtls related configuration. */
366 struct {
367#if defined(CONFIG_NET_APP_SERVER)
368 net_app_cert_cb_t cert_cb;
369 mbedtls_x509_crt srvcert;
370 mbedtls_pk_context pkey;
371#endif
372#if defined(CONFIG_NET_APP_CLIENT)
373 net_app_ca_cert_cb_t ca_cert_cb;
374 mbedtls_x509_crt ca_cert;
375#endif
376 struct tls_context ssl_ctx;
377 net_app_entropy_src_cb_t entropy_src_cb;
378 mbedtls_entropy_context entropy;
379 mbedtls_ctr_drbg_context ctr_drbg;
380 mbedtls_ssl_context ssl;
381 mbedtls_ssl_config conf;
Jukka Rissanenf3f32e82017-07-18 14:02:32 +0300382#if defined(CONFIG_NET_APP_DTLS)
383 mbedtls_ssl_cookie_ctx cookie_ctx;
384 struct dtls_timing_context timing_ctx;
385#endif
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300386 u8_t *personalization_data;
387 size_t personalization_data_len;
388 } mbedtls;
389
390 /** Have we called connect cb yet? */
Jukka Rissanen5cc4ef72017-10-26 10:03:33 +0300391 u8_t connect_cb_called : 1;
Jukka Rissanen9090bdf2017-10-06 13:28:36 +0300392
393 /** User wants to close the connection */
Jukka Rissanen5cc4ef72017-10-26 10:03:33 +0300394 u8_t close_requested : 1;
Jukka Rissanen9090bdf2017-10-06 13:28:36 +0300395
396 /** Is there TX pending? If there is then the close operation
397 * will be postponed after we have sent the data.
398 */
Jukka Rissanen5cc4ef72017-10-26 10:03:33 +0300399 u8_t tx_pending : 1;
Jukka Rissanen882f4762017-10-26 09:57:16 +0300400
401 /** Is the TLS/DTLS handshake fully done */
402 u8_t handshake_done : 1;
Jukka Rissanen5cc4ef72017-10-26 10:03:33 +0300403
404 /** Is the connection closing */
405 u8_t connection_closing : 1;
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300406 } tls;
Jukka Rissanen098483d2017-08-26 01:04:02 +0300407#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300408
409#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
410 /** Network packet (net_pkt) memory pool for network contexts attached
411 * to this network app context.
412 */
413 net_pkt_get_slab_func_t tx_slab;
414
415 /** Network data net_buf pool for network contexts attached to this
416 * network app context.
417 */
418 net_pkt_get_pool_func_t data_pool;
419#endif
420
421 /** User data pointer */
422 void *user_data;
423
Jukka Rissanen95d30432017-07-12 14:42:59 +0300424#if defined(CONFIG_NET_DEBUG_APP)
425 /** Used when debugging with net-shell */
426 sys_snode_t node;
427#endif
428
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300429 /** Type of the connection (stream or datagram) */
430 enum net_sock_type sock_type;
431
432 /** IP protocol type (UDP or TCP) */
433 enum net_ip_protocol proto;
434
435 /** Application type (client or server) of this instance */
436 enum net_app_type app_type;
437
438 /** Is this context setup or not */
439 u8_t is_init : 1;
440
441 /** Is this instance supporting TLS or not.
442 */
443 u8_t is_tls : 1;
444
445 /** Running status of the server. If true, then the server is enabled.
446 * If false then it is disabled and will not serve clients.
Jukka Rissanenc200de12017-08-22 11:16:16 +0300447 * The server is disabled by default after initialization and needs to
448 * be manually enabled in order to serve any requests.
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300449 */
450 u8_t is_enabled : 1;
451
452 /** Unused bits */
453 u8_t _padding : 5;
454};
455
456/**
457 * @brief Initialize this network application.
458 *
459 * @param app_info String describing this application.
460 * @param flags Flags related to this application startup.
461 * @param timeout How long to wait the network setup before continuing
462 * the startup.
463 *
464 * @return 0 if ok, <0 if error.
465 */
466int net_app_init(const char *app_info, u32_t flags, s32_t timeout);
467
468#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
469/**
470 * @brief Configure the net_pkt pool for this context.
471 *
472 * @details Use of this function is optional and if the pools are not set,
473 * then the default TX and DATA pools are used.
474 *
475 * @param tx_slab Function which is used when allocating TX network packet.
476 * This can be NULL in which case default TX memory pool is used.
477 * @param data_pool Function which is used when allocating data network buffer.
478 * This can be NULL in which case default DATA net_buf pool is used.
479 */
480int net_app_set_net_pkt_pool(struct net_app_ctx *ctx,
481 net_pkt_get_slab_func_t tx_slab,
482 net_pkt_get_pool_func_t data_pool);
483#else
484#define net_app_set_net_pkt_pool(...)
485#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
486
487#if defined(CONFIG_NET_APP_SERVER)
488/**
489 * @brief Create a network server application.
490 *
491 * @details Note that caller must create the context and initialize it to 0
492 * before calling this function. The context must be valid for the whole
493 * duration of the application life cycle. This usually means that it
494 * cannot be allocated from stack.
495 *
496 * @param ctx Network application context.
497 * @param sock_type Connection type (stream or datagram).
498 * @param proto IP protocol (UDP or TCP)
499 * @param server_addr Local address of the server. If set to NULL, then the
500 * API will figure out a proper address where to bind the context.
501 * @param port UDP or TCP port number where the service is located. This is
502 * only used if server_addr parameter is NULL.
503 * @param timeout Timeout for this function call. This timeout tells how
504 * long to wait while accepting the data from network.
505 * @param user_data User specific data.
506 *
507 * @return 0 if ok, <0 if error.
508 */
509int net_app_init_server(struct net_app_ctx *ctx,
510 enum net_sock_type sock_type,
511 enum net_ip_protocol proto,
512 struct sockaddr *server_addr,
513 u16_t port,
514 void *user_data);
515
516/**
517 * @brief Create a TCP server application.
518 *
519 * @details Note that caller must create the context and initialize it to 0
520 * before calling this function. The context must be valid for the whole
521 * duration of the application life cycle. This usually means that it
522 * cannot be allocated from stack.
523 *
524 * @param ctx Network application context.
525 * @param server_addr Local address of the server. If set to NULL, then the
526 * API will figure out a proper address where to bind the context.
527 * @param port UDP or TCP port number where the service is located. This is
528 * only used if server_addr parameter is NULL.
529 * @param timeout Timeout for this function call. This timeout tells how
530 * long to wait while accepting the data from network.
531 * @param user_data User specific data.
532 *
533 * @return 0 if ok, <0 if error.
534 */
535static inline int net_app_init_tcp_server(struct net_app_ctx *ctx,
536 struct sockaddr *server_addr,
537 u16_t port,
538 void *user_data)
539{
540 return net_app_init_server(ctx,
541 SOCK_STREAM,
542 IPPROTO_TCP,
543 server_addr,
544 port,
545 user_data);
546}
547
548/**
549 * @brief Create a UDP server application.
550 *
551 * @details Note that caller must create the context and initialize it to 0
552 * before calling this function. The context must be valid for the whole
553 * duration of the application life cycle. This usually means that it
554 * cannot be allocated from stack.
555 *
556 * @param ctx Network application context.
557 * @param server_addr Local address of the server. If set to NULL, then the
558 * API will figure out a proper address where to bind the context.
559 * @param port UDP or TCP port number where the service is located. This is
560 * only used if server_addr parameter is NULL.
561 * @param timeout Timeout for this function call. This timeout tells how
562 * long to wait while accepting the data from network.
563 * @param user_data User specific data.
564 *
565 * @return 0 if ok, <0 if error.
566 */
567static inline int net_app_init_udp_server(struct net_app_ctx *ctx,
568 struct sockaddr *server_addr,
569 u16_t port,
570 void *user_data)
571{
572 return net_app_init_server(ctx,
573 SOCK_DGRAM,
574 IPPROTO_UDP,
575 server_addr,
576 port,
577 user_data);
578}
579
580/**
581 * @brief Wait for an incoming connection.
582 *
583 * @details Note that caller must have called net_app_init_server() before
584 * calling this function. This functionality is separated from init function
585 * so that user can setup the callbacks after calling init. Only after calling
586 * this function the server starts to listen connection attempts. This function
587 * will not block but will initialize the local end point address so that
588 * receive callback will be called for incoming connection.
589 *
590 * @param ctx Network application context.
591 *
592 * @return 0 if ok, <0 if error.
593 */
594int net_app_listen(struct net_app_ctx *ctx);
595
Jukka Rissanenc200de12017-08-22 11:16:16 +0300596/**
597 * @brief Enable server to serve connections.
598 *
599 * @details By default the server status is disabled.
600 *
601 * @param ctx Network application context.
602 *
603 * @return 0 if ok, <0 if error.
604 */
605bool net_app_server_enable(struct net_app_ctx *ctx);
606
607/**
608 * @brief Disable server so that it will not serve any clients.
609 *
610 * @param ctx Network application context.
611 *
612 * @return 0 if ok, <0 if error.
613 */
614bool net_app_server_disable(struct net_app_ctx *ctx);
615
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300616#endif /* CONFIG_NET_APP_SERVER */
617
618#if defined(CONFIG_NET_APP_CLIENT)
619/**
620 * @brief Create a network client application.
621 *
622 * @details Note that caller must create the context and initialize it to 0
623 * before calling this function. The context must be valid for the whole
624 * duration of the application life cycle. This usually means that it
625 * cannot be allocated from stack.
626 *
627 * @param ctx Network application context.
628 * @param sock_type Connection type (stream or datagram).
629 * @param proto IP protocol (UDP or TCP)
630 * @param client_addr Local address of the client. If set to NULL, then the
631 * API will figure out a proper address where to bind the context.
632 * @param peer_addr Peer (target) address. If this is NULL, then the
633 * peer_add_str string and peer_port are used when connecting to peer.
634 * @param peer_addr_str Peer (target) address as a string. If this is NULL,
635 * then the peer_addr sockaddr is used to set the peer address. If DNS is
636 * configured in the system, then the hostname is automatically resolved if
637 * given here. Note that the port number is optional in the string. If the
638 * port number is not given in the string, then peer_port variable is used
639 * instead.
David B. Kinder2c850d72017-08-10 08:21:28 -0700640 * Following syntax is supported for the address:
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300641 * 192.0.2.1
642 * 192.0.2.1:5353
643 * 2001:db8::1
644 * [2001:db8::1]:5353
645 * peer.example.com
646 * peer.example.com:1234
647 * @param peer_port Port number where to connect to. Ignored if port number is
648 * found in the peer_addr_str.
649 * @param timeout Timeout for this function call. This is used if the API
650 * needs to do some time consuming operation, like resolving DNS address.
651 * @param user_data User specific data.
652 *
653 * @return 0 if ok, <0 if error.
654 */
655int net_app_init_client(struct net_app_ctx *ctx,
656 enum net_sock_type sock_type,
657 enum net_ip_protocol proto,
658 struct sockaddr *client_addr,
659 struct sockaddr *peer_addr,
660 const char *peer_addr_str,
661 u16_t peer_port,
662 s32_t timeout,
663 void *user_data);
664
665/**
666 * @brief Create a TCP client application.
667 *
668 * @details Note that caller must create the context and initialize it to 0
669 * before calling this function. The context must be valid for the whole
670 * duration of the application life cycle. This usually means that it
671 * cannot be allocated from stack.
672 *
673 * @param ctx Network application context.
674 * @param client_addr Local address of the client. If set to NULL, then the
675 * API will figure out a proper address where to bind the context.
676 * @param peer_addr Peer (target) address. If this is NULL, then the
677 * peer_add_str string and peer_port are used when connecting to peer.
678 * @param peer_addr_str Peer (target) address as a string. If this is NULL,
679 * then the peer_addr sockaddr is used to set the peer address. If DNS is
680 * configured in the system, then the hostname is automatically resolved if
681 * given here. Note that the port number is optional in the string. If the
682 * port number is not given in the string, then peer_port variable is used
683 * instead.
David B. Kinder2c850d72017-08-10 08:21:28 -0700684 * Following syntax is supported for the address:
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300685 * 192.0.2.1
686 * 192.0.2.1:5353
687 * 2001:db8::1
688 * [2001:db8::1]:5353
689 * peer.example.com
690 * peer.example.com:1234
691 * @param peer_port Port number where to connect to. Ignored if port number is
692 * found in the peer_addr_str.
693 * @param timeout Timeout for this function call. This is used if the API
694 * needs to do some time consuming operation, like resolving DNS address.
695 * @param user_data User specific data.
696 *
697 * @return 0 if ok, <0 if error.
698 */
699static inline int net_app_init_tcp_client(struct net_app_ctx *ctx,
700 struct sockaddr *client_addr,
701 struct sockaddr *peer_addr,
702 const char *peer_addr_str,
703 u16_t peer_port,
704 s32_t timeout,
705 void *user_data)
706{
707 return net_app_init_client(ctx,
708 SOCK_STREAM,
709 IPPROTO_TCP,
710 client_addr,
711 peer_addr,
712 peer_addr_str,
713 peer_port,
714 timeout,
715 user_data);
716}
717
718/**
719 * @brief Create a UDP client application.
720 *
721 * @details Note that caller must create the context and initialize it to 0
722 * before calling this function. The context must be valid for the whole
723 * duration of the application life cycle. This usually means that it
724 * cannot be allocated from stack.
725 *
726 * @param ctx Network application context.
727 * @param client_addr Local address of the client. If set to NULL, then the
728 * API will figure out a proper address where to bind the context.
729 * @param peer_addr Peer (target) address. If this is NULL, then the
730 * peer_add_str string and peer_port are used when connecting to peer.
731 * @param peer_addr_str Peer (target) address as a string. If this is NULL,
732 * then the peer_addr sockaddr is used to set the peer address. If DNS is
733 * configured in the system, then the hostname is automatically resolved if
734 * given here. Note that the port number is optional in the string. If the
735 * port number is not given in the string, then peer_port variable is used
736 * instead.
David B. Kinder2c850d72017-08-10 08:21:28 -0700737 * Following syntax is supported for the address:
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300738 * 192.0.2.1
739 * 192.0.2.1:5353
740 * 2001:db8::1
741 * [2001:db8::1]:5353
742 * peer.example.com
743 * peer.example.com:1234
744 * @param peer_port Port number where to connect to. Ignored if port number is
745 * found in the peer_addr_str.
746 * @param timeout Timeout for this function call. This is used if the API
747 * needs to do some time consuming operation, like resolving DNS address.
748 * @param user_data User specific data.
749 *
750 * @return 0 if ok, <0 if error.
751 */
752static inline int net_app_init_udp_client(struct net_app_ctx *ctx,
753 struct sockaddr *client_addr,
754 struct sockaddr *peer_addr,
755 const char *peer_addr_str,
756 u16_t peer_port,
757 s32_t timeout,
758 void *user_data)
759{
760 return net_app_init_client(ctx,
761 SOCK_DGRAM,
762 IPPROTO_UDP,
763 client_addr,
764 peer_addr,
765 peer_addr_str,
766 peer_port,
767 timeout,
768 user_data);
769}
770
771/**
772 * @brief Establish a network connection to peer.
773 *
774 * @param ctx Network application context.
775 * @param timeout How long to wait the network connection before giving up.
776 *
777 * @return 0 if ok, <0 if error.
778 */
779int net_app_connect(struct net_app_ctx *ctx, s32_t timeout);
780#endif /* CONFIG_NET_APP_CLIENT */
781
782/**
783 * @brief Set various network application callbacks.
784 *
785 * @param ctx Network app context.
786 * @param connect_cb Connect callback.
787 * @param recv_cb Data receive callback.
788 * @param send_cb Data sent callback.
789 * @param close_cb Close callback.
790 *
791 * @return 0 if ok, <0 if error.
792 */
793int net_app_set_cb(struct net_app_ctx *ctx,
794 net_app_connect_cb_t connect_cb,
795 net_app_recv_cb_t recv_cb,
796 net_app_send_cb_t send_cb,
797 net_app_close_cb_t close_cb);
798
799/**
800 * @brief Send data that is found in net_pkt to peer.
801 *
802 * @details If the function return < 0, then it is caller responsibility
803 * to unref the pkt. If the packet was sent successfully, then the lower
804 * IP stack will release the network pkt.
805 *
806 * @param ctx Network application context.
807 * @param pkt Network packet to send.
808 * @param dst Destination address where to send packet. This is
809 * ignored for TCP data.
810 * @param dst_len Destination address structure size
811 * @param timeout How long to wait the send before giving up.
812 * @param user_data_send User data specific to this send call.
813 *
814 * @return 0 if ok, <0 if error.
815 */
816int net_app_send_pkt(struct net_app_ctx *ctx,
817 struct net_pkt *pkt,
818 const struct sockaddr *dst,
819 socklen_t dst_len,
820 s32_t timeout,
821 void *user_data_send);
822
823/**
824 * @brief Send data that is found in user specified buffer to peer.
825 *
826 * @param ctx Network application context.
827 * @param buf Buffer to send.
828 * @param buf_len Amount of data to send.
829 * @param dst Destination address where to send packet. This is
830 * ignored for TCP data.
831 * @param dst_len Destination address structure size
832 * @param timeout How long to wait the send before giving up.
833 * @param user_data_send User data specific to this send call.
834 *
835 * @return 0 if ok, <0 if error.
836 */
837int net_app_send_buf(struct net_app_ctx *ctx,
838 u8_t *buf,
839 size_t buf_len,
840 const struct sockaddr *dst,
841 socklen_t dst_len,
842 s32_t timeout,
843 void *user_data_send);
844
845/**
846 * @brief Create network packet.
847 *
848 * @param ctx Network application context.
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300849 * @param family What kind of network packet to get (AF_INET or AF_INET6)
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300850 * @param timeout How long to wait the send before giving up.
851 *
852 * @return valid net_pkt if ok, NULL if error.
853 */
854struct net_pkt *net_app_get_net_pkt(struct net_app_ctx *ctx,
Jukka Rissanen7a31d4b2017-07-01 00:39:16 +0300855 sa_family_t family,
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300856 s32_t timeout);
857
858/**
Ravi kumar Veeramallyb22ab262018-02-08 14:35:23 +0200859 * @brief Create network packet based on dst address.
860 *
861 * @param ctx Network application context.
862 * @param dst Destination address to select net_context
863 * @param timeout How long to wait the send before giving up.
864 *
865 * @return valid net_pkt if ok, NULL if error.
866 */
867struct net_pkt *net_app_get_net_pkt_with_dst(struct net_app_ctx *ctx,
868 const struct sockaddr *dst,
869 s32_t timeout);
870
871/**
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300872 * @brief Create network buffer that will hold network data.
873 *
874 * @details The returned net_buf is automatically appended to the
875 * end of network packet fragment chain.
876 *
877 * @param ctx Network application context.
878 * @param pkt Network packet to where the data buf is appended.
879 * @param timeout How long to wait the send before giving up.
880 *
881 * @return valid net_pkt if ok, NULL if error.
882 */
883struct net_buf *net_app_get_net_buf(struct net_app_ctx *ctx,
884 struct net_pkt *pkt,
885 s32_t timeout);
886
887/**
888 * @brief Close a network connection to peer.
889 *
890 * @param ctx Network application context.
891 *
892 * @return 0 if ok, <0 if error.
893 */
894int net_app_close(struct net_app_ctx *ctx);
895
896/**
Jukka Rissanen1b1a3a62017-09-18 12:10:39 +0300897 * @brief Close a specific network connection.
898 *
899 * @param ctx Network application context.
900 * @param net_ctx Network context.
901 *
902 * @return 0 if ok, <0 if error.
903 */
904int net_app_close2(struct net_app_ctx *ctx,
905 struct net_context *net_ctx);
906
907/**
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300908 * @brief Release this network application context.
909 *
910 * @details No network data will be received via this context after this
911 * call.
912 *
913 * @param ctx Network application context.
914 *
915 * @return 0 if ok, <0 if error.
916 */
917int net_app_release(struct net_app_ctx *ctx);
918
Jukka Rissanen098483d2017-08-26 01:04:02 +0300919#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300920#if defined(CONFIG_NET_APP_CLIENT)
921/**
922 * @brief Initialize TLS support for this net_app client context.
923 *
924 * @param ctx net_app context.
David B. Kinder2c850d72017-08-10 08:21:28 -0700925 * @param request_buf Caller-supplied buffer where the TLS request will be
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300926 * stored
David B. Kinder2c850d72017-08-10 08:21:28 -0700927 * @param request_buf_len Length of the caller-supplied buffer.
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300928 * @param personalization_data Personalization data (Device specific
929 * identifiers) for random number generator. (Can be NULL).
930 * @param personalization_data_len Length of the personalization data.
David B. Kinder2c850d72017-08-10 08:21:28 -0700931 * @param cert_cb User-supplied callback that setups the certificates.
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300932 * @param cert_host Hostname that is used to verify the server certificate.
933 * This value is used when net_api API calls mbedtls_ssl_set_hostname()
934 * which sets the hostname to check against the received server certificate.
935 * See https://tls.mbed.org/kb/how-to/use-sni for more details.
936 * This can be left NULL in which case mbedtls will silently skip certificate
937 * verification entirely. This option is only used if MBEDTLS_X509_CRT_PARSE_C
938 * is enabled in mbedtls config file.
David B. Kinder2c850d72017-08-10 08:21:28 -0700939 * @param entropy_src_cb User-supplied callback that setup the entropy. This
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300940 * can be set to NULL, in which case default entropy source is used.
941 * @param pool Memory pool for RX data reads.
942 * @param stack TLS thread stack.
943 * @param stack_len TLS thread stack size.
944 *
945 * @return Return 0 if ok, <0 if error.
946 */
947int net_app_client_tls(struct net_app_ctx *ctx,
948 u8_t *request_buf,
949 size_t request_buf_len,
950 u8_t *personalization_data,
951 size_t personalization_data_len,
952 net_app_ca_cert_cb_t cert_cb,
953 const char *cert_host,
954 net_app_entropy_src_cb_t entropy_src_cb,
955 struct k_mem_pool *pool,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700956 k_thread_stack_t *stack,
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300957 size_t stack_size);
958#endif /* CONFIG_NET_APP_CLIENT */
959
960#if defined(CONFIG_NET_APP_SERVER)
961/**
962 * @brief Initialize TLS support for this net_app server context.
963 *
964 * @param ctx net_app context.
David B. Kinder2c850d72017-08-10 08:21:28 -0700965 * @param request_buf Caller-supplied buffer where the TLS request will be
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300966 * stored
David B. Kinder2c850d72017-08-10 08:21:28 -0700967 * @param request_buf_len Length of the caller-supplied buffer.
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300968 * @param server_banner Print information about started service. This is only
969 * printed if net_app debugging is activated. The parameter can be set to NULL
970 * if no extra prints are needed.
971 * @param personalization_data Personalization data (Device specific
972 * identifiers) for random number generator. (Can be NULL).
973 * @param personalization_data_len Length of the personalization data.
David B. Kinder2c850d72017-08-10 08:21:28 -0700974 * @param cert_cb User-supplied callback that setups the certificates.
975 * @param entropy_src_cb User-supplied callback that setup the entropy. This
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300976 * can be set to NULL, in which case default entropy source is used.
977 * @param pool Memory pool for RX data reads.
978 * @param stack TLS thread stack.
979 * @param stack_len TLS thread stack size.
980 *
981 * @return Return 0 if ok, <0 if error.
982 */
983int net_app_server_tls(struct net_app_ctx *ctx,
984 u8_t *request_buf,
985 size_t request_buf_len,
986 const char *server_banner,
987 u8_t *personalization_data,
988 size_t personalization_data_len,
989 net_app_cert_cb_t cert_cb,
990 net_app_entropy_src_cb_t entropy_src_cb,
991 struct k_mem_pool *pool,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700992 k_thread_stack_t *stack,
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300993 size_t stack_len);
994
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300995#endif /* CONFIG_NET_APP_SERVER */
996
Jukka Rissanen098483d2017-08-26 01:04:02 +0300997#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
Jukka Rissanena1be6a82017-05-10 22:23:29 +0300998
Jukka Rissanen539b0c42017-07-20 16:29:45 +0300999/**
1000 * @}
1001 */
1002
Jukka Rissanen95d30432017-07-12 14:42:59 +03001003#if defined(CONFIG_NET_DEBUG_APP)
1004typedef void (*net_app_ctx_cb_t)(struct net_app_ctx *ctx, void *user_data);
1005void net_app_server_foreach(net_app_ctx_cb_t cb, void *user_data);
1006void net_app_client_foreach(net_app_ctx_cb_t cb, void *user_data);
1007#endif /* CONFIG_NET_DEBUG_APP */
1008
Jukka Rissanena1be6a82017-05-10 22:23:29 +03001009#ifdef __cplusplus
1010}
1011#endif
1012
1013#endif /* __NET_APP_H */