From 42c19497b6360cc97bb6ee797bccb52e2a950c33 Mon Sep 17 00:00:00 2001 From: Ravi kumar Veeramally Date: Tue, 4 Sep 2018 11:49:12 +0300 Subject: [PATCH] samples: net: coap: Remove apps using raw mbedtls APIs This removes these network sample applications samples/net/coaps_client samples/net/coaps_server as they are using low level mbedtls APIs. You should use preferably socket based or net-app based applications. Signed-off-by: Ravi kumar Veeramally --- doc/subsystems/networking/overview.rst | 4 +- samples/net/coaps_client/CMakeLists.txt | 6 - samples/net/coaps_client/README.rst | 51 -- samples/net/coaps_client/prj.conf | 28 - samples/net/coaps_client/sample.yaml | 7 - samples/net/coaps_client/src/coaps_client.c | 443 ------------- samples/net/coaps_client/src/udp.c | 207 ------ samples/net/coaps_client/src/udp.h | 23 - samples/net/coaps_client/src/udp_cfg.h | 21 - samples/net/coaps_server/CMakeLists.txt | 6 - samples/net/coaps_server/README.rst | 77 --- samples/net/coaps_server/prj.conf | 28 - samples/net/coaps_server/sample.yaml | 7 - samples/net/coaps_server/src/coaps_server.c | 689 -------------------- samples/net/coaps_server/src/udp.c | 196 ------ samples/net/coaps_server/src/udp.h | 24 - samples/net/coaps_server/src/udp_cfg.h | 20 - 17 files changed, 1 insertion(+), 1836 deletions(-) delete mode 100644 samples/net/coaps_client/CMakeLists.txt delete mode 100644 samples/net/coaps_client/README.rst delete mode 100644 samples/net/coaps_client/prj.conf delete mode 100644 samples/net/coaps_client/sample.yaml delete mode 100644 samples/net/coaps_client/src/coaps_client.c delete mode 100644 samples/net/coaps_client/src/udp.c delete mode 100644 samples/net/coaps_client/src/udp.h delete mode 100644 samples/net/coaps_client/src/udp_cfg.h delete mode 100644 samples/net/coaps_server/CMakeLists.txt delete mode 100644 samples/net/coaps_server/README.rst delete mode 100644 samples/net/coaps_server/prj.conf delete mode 100644 samples/net/coaps_server/sample.yaml delete mode 100644 samples/net/coaps_server/src/coaps_server.c delete mode 100644 samples/net/coaps_server/src/udp.c delete mode 100644 samples/net/coaps_server/src/udp.h delete mode 100644 samples/net/coaps_server/src/udp_cfg.h diff --git a/doc/subsystems/networking/overview.rst b/doc/subsystems/networking/overview.rst index 2ab0b94217b..9165757d2dc 100644 --- a/doc/subsystems/networking/overview.rst +++ b/doc/subsystems/networking/overview.rst @@ -65,9 +65,7 @@ can be disabled if not needed. * **CoAP** Constrained Application Protocol (RFC 7252) is supported. Both :ref:`coap-client-sample` and :ref:`coap-server-sample` sample - applications are implemented. A :ref:`coaps-client-sample` and - :ref:`coaps-server-sample` using DTLS (Datagram Transport Layer Security) - (RFC 6347) are also implemented. + applications are implemented. * **LWM2M** OMA Lightweight Machine-to-Machine Protocol (V1.0 Feb 2017) is supported via the "Register Device" API (Register, De-Register and Update) diff --git a/samples/net/coaps_client/CMakeLists.txt b/samples/net/coaps_client/CMakeLists.txt deleted file mode 100644 index 800ba5841af..00000000000 --- a/samples/net/coaps_client/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.8.2) -include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) -project(NONE) - -FILE(GLOB app_sources src/*.c) -target_sources(app PRIVATE ${app_sources}) diff --git a/samples/net/coaps_client/README.rst b/samples/net/coaps_client/README.rst deleted file mode 100644 index a4c1e9ad923..00000000000 --- a/samples/net/coaps_client/README.rst +++ /dev/null @@ -1,51 +0,0 @@ -.. _coaps-client-sample: - -CoAP over DTLS sample client -############################ - -Overview -******** -This sample code shows a CoAP over DTLS client using mbedTLS on top of Zephyr. - -Building and Running -******************** - -Follow the steps for testing :ref:`networking_with_qemu`. - -Run the server application at samples/net/coaps_server, with the following -command: - -.. code-block:: console - - $ make server - -In other terminal window, run this client application at samples/net/coaps_client: - -.. code-block:: console - - $ make client - -You will get the following output: - -.. code-block:: console - - reply: 60 45 00 01 ff 54 79 70 65 3a 20 30 0a 43 6f 64 65 3a 20 31 0a 4d - 49 44 3a 20 31 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 (123 bytes) - -From the server application directory the screen should display - -.. code-block:: console - - ******* - type: 0 code 1 id 1 - ******* - connection was closed gracefully - done - -If the server does not receive the messages, restart the app and try to connect -the client again. - diff --git a/samples/net/coaps_client/prj.conf b/samples/net/coaps_client/prj.conf deleted file mode 100644 index 6cfcb87f591..00000000000 --- a/samples/net/coaps_client/prj.conf +++ /dev/null @@ -1,28 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_IPV6=y -CONFIG_NET_UDP=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_NET_BUF_LOG=y -CONFIG_NET_LOG=y -CONFIG_SYS_LOG_SHOW_COLOR=y -CONFIG_INIT_STACKS=y -CONFIG_PRINTK=y -CONFIG_NET_STATISTICS=y -CONFIG_NET_PKT_RX_COUNT=14 -CONFIG_NET_PKT_TX_COUNT=14 -CONFIG_NET_BUF_RX_COUNT=30 -CONFIG_NET_BUF_TX_COUNT=30 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 -CONFIG_NET_MAX_CONTEXTS=10 - -CONFIG_MBEDTLS=y -CONFIG_MBEDTLS_BUILTIN=y -CONFIG_MBEDTLS_CFG_FILE="config-coap.h" - -CONFIG_COAP=y -CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=256 - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::3" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::1" diff --git a/samples/net/coaps_client/sample.yaml b/samples/net/coaps_client/sample.yaml deleted file mode 100644 index 21966771132..00000000000 --- a/samples/net/coaps_client/sample.yaml +++ /dev/null @@ -1,7 +0,0 @@ -sample: - name: COAP Client with DTLS -tests: - test: - harness: net - depends_on: netif - tags: net coap dtls diff --git a/samples/net/coaps_client/src/coaps_client.c b/samples/net/coaps_client/src/coaps_client.c deleted file mode 100644 index 1811c4f6296..00000000000 --- a/samples/net/coaps_client/src/coaps_client.c +++ /dev/null @@ -1,443 +0,0 @@ -/* Sample CoAP over DTLS client using mbedTLS. - * (Meant to be used with config-coap.h) - * - * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved - * - * SPDX-License-Identifier: Apache-2.0 - * - * This file is part of mbed TLS (https://tls.mbed.org) - */ - -#include -#include -#include -#include - -#if !defined(CONFIG_MBEDTLS_CFG_FILE) -#include "mbedtls/config.h" -#else -#include CONFIG_MBEDTLS_CFG_FILE -#endif - -#if defined(MBEDTLS_PLATFORM_C) -#include "mbedtls/platform.h" -#else -#include -#define mbedtls_time_t time_t -#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS -#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE -#endif - -#include -#include -#include -#include -#include -#include "udp.h" -#include "udp_cfg.h" - -#include "mbedtls/net.h" -#include "mbedtls/ssl.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - -#include - -#if defined(MBEDTLS_DEBUG_C) -#include "mbedtls/debug.h" -#define DEBUG_THRESHOLD 0 -#endif - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) -#include "mbedtls/memory_buffer_alloc.h" -static unsigned char heap[8192]; -#endif - -/* - * Hardcoded values for server host and port - */ - -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) -const unsigned char psk[] = "passwd\0"; -const char psk_id[] = "Client_identity\0"; -#endif - -const char *pers = "mini_client"; -static unsigned char payload[128]; - -#define NUM_REPLIES 3 -struct coap_reply replies[NUM_REPLIES]; - -#define COAP_BUF_SIZE 128 - -NET_PKT_TX_SLAB_DEFINE(coap_pkt_slab, 4); -NET_BUF_POOL_DEFINE(coap_data_pool, 4, COAP_BUF_SIZE, 0, NULL); - -static const char *const test_path[] = { "test", NULL }; - -static struct in6_addr mcast_addr = MCAST_IP_ADDR; - -struct dtls_timing_context { - u32_t snapshot; - u32_t int_ms; - u32_t fin_ms; -}; - -static void msg_dump(const char *s, u8_t *data, unsigned int len) -{ - unsigned int i; - - printk("%s: ", s); - for (i = 0; i < len; i++) { - printk("%02x ", data[i]); - } - - printk("(%u bytes)\n", len); -} - -static int resource_reply_cb(const struct coap_packet *response, - struct coap_reply *reply, - const struct sockaddr *from) -{ - - struct net_buf *frag = response->pkt->frags; - - while (frag) { - msg_dump("reply", frag->data, frag->len); - frag = frag->frags; - } - - return 0; -} - -static void my_debug(void *ctx, int level, - const char *file, int line, const char *str) -{ - const char *p, *basename; - - /* Extract basename from file */ - for (p = basename = file; *p != '\0'; p++) { - if (*p == '/' || *p == '\\') { - basename = p + 1; - } - } - - mbedtls_printf("%s:%04d: |%d| %s", basename, line, level, str); -} - -void dtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) -{ - struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; - - ctx->int_ms = int_ms; - ctx->fin_ms = fin_ms; - - if (fin_ms != 0) { - ctx->snapshot = k_uptime_get_32(); - } -} - -int dtls_timing_get_delay(void *data) -{ - struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; - unsigned long elapsed_ms; - - if (ctx->fin_ms == 0) { - return -1; - } - - elapsed_ms = k_uptime_get_32() - ctx->snapshot; - - if (elapsed_ms >= ctx->fin_ms) { - return 2; - } - - if (elapsed_ms >= ctx->int_ms) { - return 1; - } - - return 0; -} - -static int entropy_source(void *data, unsigned char *output, size_t len, - size_t *olen) -{ - u32_t seed; - - ARG_UNUSED(data); - - seed = sys_rand32_get(); - - if (len > sizeof(seed)) { - len = sizeof(seed); - } - - memcpy(output, &seed, len); - - *olen = len; - - return 0; -} - -void dtls_client(void) -{ - int ret; - struct udp_context ctx; - struct dtls_timing_context timer; - struct coap_packet request, cpkt; - struct coap_reply *reply; - struct net_pkt *pkt; - struct net_buf *frag; - u8_t observe = 0; - const char *const *p; - u16_t len; - - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_ssl_context ssl; - mbedtls_ssl_config conf; - - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_platform_set_printf(printk); - mbedtls_ssl_init(&ssl); - mbedtls_ssl_config_init(&conf); - mbedtls_entropy_init(&entropy); - mbedtls_entropy_add_source(&entropy, entropy_source, NULL, - MBEDTLS_ENTROPY_MAX_GATHER, - MBEDTLS_ENTROPY_SOURCE_STRONG); - - ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - (const unsigned char *)pers, strlen(pers)); - if (ret != 0) { - mbedtls_printf("mbedtls_ctr_drbg_seed failed returned -0x%x\n", - -ret); - goto exit; - } - - ret = mbedtls_ssl_config_defaults(&conf, - MBEDTLS_SSL_IS_CLIENT, - MBEDTLS_SSL_TRANSPORT_DATAGRAM, - MBEDTLS_SSL_PRESET_DEFAULT); - if (ret != 0) { - mbedtls_printf("mbedtls_ssl_config_defaults" - " failed! returned -0x%x\n", -ret); - goto exit; - } - -/* Modify this to change the default timeouts for the DTLS handshake */ -/* mbedtls_ssl_conf_handshake_timeout( &conf, min, max ); */ - -#if defined(MBEDTLS_DEBUG_C) - mbedtls_debug_set_threshold(DEBUG_THRESHOLD); -#endif - - mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); - mbedtls_ssl_conf_dbg(&conf, my_debug, NULL); - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) - mbedtls_memory_buffer_alloc_init(heap, sizeof(heap)); -#endif - - ret = mbedtls_ssl_setup(&ssl, &conf); - - if (ret != 0) { - mbedtls_printf("mbedtls_ssl_setup failed returned -0x%x\n", - -ret); - goto exit; - } - - ret = udp_init(&ctx); - if (ret != 0) { - mbedtls_printf("udp_init failed returned 0x%x\n", ret); - goto exit; - } - - udp_tx(&ctx, payload, 32); - -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) - ret = mbedtls_ssl_conf_psk(&conf, psk, strlen((char *)psk), - (unsigned char *)psk_id, - strlen(psk_id)); - if (ret != 0) { - mbedtls_printf(" failed\n mbedtls_ssl_conf_psk" - " returned -0x%x\n", -ret); - goto exit; - } -#endif - - mbedtls_ssl_set_timer_cb(&ssl, &timer, dtls_timing_set_delay, - dtls_timing_get_delay); - - mbedtls_ssl_set_bio(&ssl, &ctx, udp_tx, udp_rx, NULL); - - do { - ret = mbedtls_ssl_handshake(&ssl); - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (ret != 0) { - mbedtls_printf("mbedtls_ssl_handshake failed returned -0x%x\n", - -ret); - goto exit; - } - - /* Write to server */ -retry: - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - goto exit; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - goto exit; - } - - net_pkt_frag_add(pkt, frag); - - ret = coap_packet_init(&request, pkt, 1, COAP_TYPE_CON, - 0, NULL, COAP_METHOD_GET, coap_next_id()); - if (ret < 0) { - goto exit; - } - - /* Enable observing the resource. */ - ret = coap_packet_append_option(&request, COAP_OPTION_OBSERVE, - &observe, sizeof(observe)); - if (ret < 0) { - mbedtls_printf("Unable add option to request.\n"); - goto exit; - } - - for (p = test_path; p && *p; p++) { - ret = coap_packet_append_option(&request, COAP_OPTION_URI_PATH, - *p, strlen(*p)); - if (ret < 0) { - mbedtls_printf("Unable add option/path to request.\n"); - goto exit; - } - } - - reply = coap_reply_next_unused(replies, NUM_REPLIES); - if (!reply) { - mbedtls_printf("No resources for waiting for replies.\n"); - goto exit; - } - - coap_reply_init(reply, &request); - reply->reply = resource_reply_cb; - len = frag->len; - - do { - ret = mbedtls_ssl_write(&ssl, frag->data, len); - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE); - - net_pkt_unref(pkt); - - if (ret <= 0) { - mbedtls_printf("mbedtls_ssl_write failed returned 0x%x\n", - -ret); - goto exit; - } - - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - mbedtls_printf("Could not get packet from pool\n"); - goto exit; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - mbedtls_printf("Could not get frag from pool\n"); - goto exit; - } - - net_pkt_frag_add(pkt, frag); - len = COAP_BUF_SIZE - 1; - memset(frag->data, 0, COAP_BUF_SIZE); - - do { - ret = mbedtls_ssl_read(&ssl, frag->data, COAP_BUF_SIZE - 1); - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (ret <= 0) { - net_pkt_unref(pkt); - - switch (ret) { - case MBEDTLS_ERR_SSL_TIMEOUT: - mbedtls_printf(" timeout\n"); - goto retry; - - case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: - mbedtls_printf(" connection was closed" - " gracefully\n"); - goto exit; - - default: - mbedtls_printf(" mbedtls_ssl_read" - " returned -0x%x\n", -ret); - goto exit; - } - } - - len = ret; - frag->len = len; - - ret = coap_packet_parse(&cpkt, pkt, NULL, 0); - if (ret) { - mbedtls_printf("Could not parse packet\n"); - goto exit; - } - - reply = coap_response_received(&cpkt, NULL, replies, NUM_REPLIES); - if (!reply) { - mbedtls_printf("No handler for response (%d)\n", ret); - } - - net_pkt_unref(pkt); - mbedtls_ssl_close_notify(&ssl); -exit: - - mbedtls_ssl_free(&ssl); - mbedtls_ssl_config_free(&conf); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); -} - -#define STACK_SIZE 4096 -K_THREAD_STACK_DEFINE(stack, STACK_SIZE); -static struct k_thread thread_data; - -static inline int init_app(void) -{ -#if defined(CONFIG_NET_CONFIG_MY_IPV6_ADDR) - if (net_addr_pton(AF_INET6, - CONFIG_NET_CONFIG_MY_IPV6_ADDR, - (struct sockaddr *)&client_addr) < 0) { - mbedtls_printf("Invalid IPv6 address %s", - CONFIG_NET_CONFIG_MY_IPV6_ADDR); - } -#endif - if (!net_if_ipv6_addr_add(net_if_get_default(), &client_addr, - NET_ADDR_MANUAL, 0)) { - return -EIO; - } - - net_if_ipv6_maddr_add(net_if_get_default(), &mcast_addr); - - return 0; -} - -void main(void) -{ - if (init_app() != 0) { - printk("Cannot initialize network\n"); - return; - } - - k_thread_create(&thread_data, stack, STACK_SIZE, - (k_thread_entry_t) dtls_client, - NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); -} diff --git a/samples/net/coaps_client/src/udp.c b/samples/net/coaps_client/src/udp.c deleted file mode 100644 index 9acf454c942..00000000000 --- a/samples/net/coaps_client/src/udp.c +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#if !defined(CONFIG_MBEDTLS_CFG_FILE) -#include "mbedtls/config.h" -#else -#include CONFIG_MBEDTLS_CFG_FILE -#endif -#include "mbedtls/ssl.h" - -#include "udp_cfg.h" -#include "udp.h" - -static struct in6_addr server_addr; -static struct in6_addr mcast_addr = MCAST_IP_ADDR; -static const socklen_t addrlen = sizeof(struct sockaddr_in6); - -static void set_destination(struct sockaddr *addr) -{ - struct sockaddr_in6 *dst_addr = (struct sockaddr_in6 *)addr; - - net_ipaddr_copy(&dst_addr->sin6_addr, &server_addr); - dst_addr->sin6_family = AF_INET6; - dst_addr->sin6_port = htons(SERVER_PORT); -} - -static void udp_received(struct net_context *context, - struct net_pkt *pkt, int status, void *user_data) -{ - struct udp_context *ctx = user_data; - - ARG_UNUSED(context); - ARG_UNUSED(status); - - ctx->rx_pkt = pkt; - k_sem_give(&ctx->rx_sem); -} - -int udp_tx(void *context, const unsigned char *buf, size_t size) -{ - struct udp_context *ctx = context; - struct net_context *udp_ctx; - struct net_pkt *send_pkt; - struct sockaddr dst_addr; - int rc, len; - - udp_ctx = ctx->net_ctx; - - send_pkt = net_pkt_get_tx(udp_ctx, K_FOREVER); - if (!send_pkt) { - return MBEDTLS_ERR_SSL_ALLOC_FAILED; - } - - rc = net_pkt_append_all(send_pkt, size, (u8_t *) buf, K_FOREVER); - if (!rc) { - return MBEDTLS_ERR_SSL_INTERNAL_ERROR; - } - - set_destination(&dst_addr); - len = net_pkt_get_len(send_pkt); - k_sleep(UDP_TX_TIMEOUT); - - rc = net_context_sendto(send_pkt, &dst_addr, - addrlen, NULL, K_FOREVER, NULL, NULL); - if (rc < 0) { - net_pkt_unref(send_pkt); - return MBEDTLS_ERR_SSL_INTERNAL_ERROR; - } else { - return len; - } -} - -int udp_rx(void *context, unsigned char *buf, size_t size) -{ - struct udp_context *ctx = context; - struct net_buf *rx_buf = NULL; - u16_t read_bytes; - u8_t *ptr; - int pos; - int len; - int rc; - - k_sem_take(&ctx->rx_sem, K_FOREVER); - - read_bytes = net_pkt_appdatalen(ctx->rx_pkt); - if (read_bytes > size) { - return MBEDTLS_ERR_SSL_ALLOC_FAILED; - } - - ptr = net_pkt_appdata(ctx->rx_pkt); - rx_buf = ctx->rx_pkt->frags; - len = rx_buf->len - (ptr - rx_buf->data); - pos = 0; - - while (rx_buf) { - memcpy(buf + pos, ptr, len); - pos += len; - - rx_buf = rx_buf->frags; - if (!rx_buf) { - break; - } - - ptr = rx_buf->data; - len = rx_buf->len; - } - - net_pkt_unref(ctx->rx_pkt); - ctx->rx_pkt = NULL; - - if (read_bytes != pos) { - return MBEDTLS_ERR_SSL_INTERNAL_ERROR; - } - - rc = read_bytes; - ctx->remaining = 0; - - return rc; -} - -int udp_init(struct udp_context *ctx) -{ - struct net_context *udp_ctx = { 0 }; - struct net_context *mcast_ctx = { 0 }; - struct sockaddr_in6 my_addr = { 0 }; - struct sockaddr_in6 my_mcast_addr = { 0 }; - struct net_if_mcast_addr *mcast; - int rc; - - k_sem_init(&ctx->rx_sem, 0, UINT_MAX); - - net_ipaddr_copy(&my_mcast_addr.sin6_addr, &mcast_addr); - my_mcast_addr.sin6_family = AF_INET6; - - net_ipaddr_copy(&my_addr.sin6_addr, &client_addr); - my_addr.sin6_family = AF_INET6; - my_addr.sin6_port = htons(CLIENT_PORT); - - rc = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &udp_ctx); - if (rc < 0) { - printk("Cannot get network context for IPv6 UDP (%d)", rc); - return -EIO; - } - - rc = net_context_bind(udp_ctx, (struct sockaddr *)&my_addr, - sizeof(struct sockaddr_in6)); - if (rc < 0) { - printk("Cannot bind IPv6 UDP port %d (%d)", CLIENT_PORT, rc); - goto error; - } - - mcast = net_if_ipv6_maddr_add(net_if_get_default(), - &my_mcast_addr.sin6_addr); - if (!mcast) { - printk("Cannot add mcast addr\n"); - goto error; - } - - rc = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &mcast_ctx); - if (rc < 0) { - printk("Cannot get receiving IPv6 mcast (%d)", rc); - goto error; - } - - rc = net_context_bind(mcast_ctx, (struct sockaddr *)&my_mcast_addr, - sizeof(struct sockaddr_in6)); - if (rc < 0) { - printk("Cannot get bind IPv6 mcast (%d)", rc); - goto error; - } - - ctx->rx_pkt = NULL; - ctx->remaining = 0; - ctx->net_ctx = udp_ctx; - -#if defined(CONFIG_NET_CONFIG_PEER_IPV6_ADDR) - if (net_addr_pton(AF_INET6, - CONFIG_NET_CONFIG_PEER_IPV6_ADDR, - &server_addr) < 0) { - printk("Invalid peer IPv6 address %s", - CONFIG_NET_CONFIG_PEER_IPV6_ADDR); - } -#endif - - rc = net_context_recv(ctx->net_ctx, udp_received, K_NO_WAIT, ctx); - if (rc != 0) { - return -EIO; - } - - return 0; - -error: - net_context_put(udp_ctx); - return -EINVAL; -} diff --git a/samples/net/coaps_client/src/udp.h b/samples/net/coaps_client/src/udp.h deleted file mode 100644 index 609e4bbcfcd..00000000000 --- a/samples/net/coaps_client/src/udp.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef _UDP_H_ -#define _UDP_H_ - -#include - -struct udp_context { - struct net_context *net_ctx; - struct net_pkt *rx_pkt; - struct k_sem rx_sem; - int remaining; -}; - -int udp_init(struct udp_context *ctx); -int udp_tx(void *ctx, const unsigned char *buf, size_t size); -int udp_rx(void *ctx, unsigned char *buf, size_t size); - -#endif diff --git a/samples/net/coaps_client/src/udp_cfg.h b/samples/net/coaps_client/src/udp_cfg.h deleted file mode 100644 index d6cc6b6c6ef..00000000000 --- a/samples/net/coaps_client/src/udp_cfg.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef UDP_CONFIG_H_ -#define UDP_CONFIG_H_ - -/* admin-local, dynamically allocated multicast address */ -#define MCAST_IP_ADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, \ - 0, 0, 0, 0, 0, 0, 0, 0x2 } } } - -static struct in6_addr client_addr; - -#define SERVER_PORT 5684 -#define CLIENT_PORT 8484 - -#define UDP_TX_TIMEOUT 100 /* Timeout in milliseconds */ - -#endif diff --git a/samples/net/coaps_server/CMakeLists.txt b/samples/net/coaps_server/CMakeLists.txt deleted file mode 100644 index 800ba5841af..00000000000 --- a/samples/net/coaps_server/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.8.2) -include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) -project(NONE) - -FILE(GLOB app_sources src/*.c) -target_sources(app PRIVATE ${app_sources}) diff --git a/samples/net/coaps_server/README.rst b/samples/net/coaps_server/README.rst deleted file mode 100644 index bd997d1b63e..00000000000 --- a/samples/net/coaps_server/README.rst +++ /dev/null @@ -1,77 +0,0 @@ -.. _coaps-server-sample: - -CoAP over DTLS sample server -############################ - -Overview -******** -This sample code shows a CoAP over DTLS server using mbedTLS on top of Zephyr. - -Building and Running -******************** - -Follow the steps for testing :ref:`networking_with_qemu`. - -In the application directory type: - -.. code-block:: console - - $ make run - -In other terminal window, obtain the libcoap code from: - -.. code-block:: console - - $ git clone --recursive -b dtls https://github.com/obgm/libcoap.git - -and put it in a well known directory, in your Linux machine, this will be your -client. In order to compile libcoap you may need to install the following -libraries, for Ubuntu - -.. code-block:: console - - $ sudo apt-get install libtool asciidoc - -Move to that directory and compile the libcoap on your host machine - -.. code-block:: console - - $ ./autogen.sh - $ ./configure --disable-shared - $ make all - -Now you can run the client like - -.. code-block:: console - - $ cd examples - $ ./coap-client -m get coaps://[2001:db8::1]/test -u Client_identity -k passwd - -You will get the following output: - -.. code-block:: console - - v:1 t:CON c:GET i:7154 {} [ ] - decrypt_verify(): found 24 bytes cleartext - decrypt_verify(): found 123 bytes cleartext - Type: 0 - Code: 1 - MID: 29012 - -From the app directory type the screen should display - -.. code-block:: console - - ******* - type: 0 code 1 id 29012 - ******* - mbedtls_ssl_read returned -0x7780 - -If the server does not receive the messages, restart the app and try to connect -the client again. - -References -********** - -* https://tls.mbed.org/ -* https://libcoap.net/ diff --git a/samples/net/coaps_server/prj.conf b/samples/net/coaps_server/prj.conf deleted file mode 100644 index 0de7d666e2f..00000000000 --- a/samples/net/coaps_server/prj.conf +++ /dev/null @@ -1,28 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_IPV6=y -CONFIG_NET_IPV4=n -CONFIG_NET_UDP=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_NET_LOG=y -CONFIG_SYS_LOG_SHOW_COLOR=y -CONFIG_INIT_STACKS=y -CONFIG_PRINTK=y -CONFIG_NET_STATISTICS=y -CONFIG_NET_PKT_RX_COUNT=14 -CONFIG_NET_PKT_TX_COUNT=14 -CONFIG_NET_BUF_RX_COUNT=30 -CONFIG_NET_BUF_TX_COUNT=30 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 -CONFIG_NET_MAX_CONTEXTS=10 - -CONFIG_COAP=y -CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=256 - -CONFIG_MBEDTLS=y -CONFIG_MBEDTLS_BUILTIN=y -CONFIG_MBEDTLS_CFG_FILE="config-coap.h" - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" diff --git a/samples/net/coaps_server/sample.yaml b/samples/net/coaps_server/sample.yaml deleted file mode 100644 index f93b6153282..00000000000 --- a/samples/net/coaps_server/sample.yaml +++ /dev/null @@ -1,7 +0,0 @@ -sample: - name: COAP Server with DTLS -tests: - test: - harness: net - depends_on: netif - tags: net coap diff --git a/samples/net/coaps_server/src/coaps_server.c b/samples/net/coaps_server/src/coaps_server.c deleted file mode 100644 index eb8ee2f5045..00000000000 --- a/samples/net/coaps_server/src/coaps_server.c +++ /dev/null @@ -1,689 +0,0 @@ -/* CoAP over DTLS server implemented with mbedTLS. - * (Meant to be used with config-coap.h) - * - * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved - * - * SPDX-License-Identifier: Apache-2.0 - * - * This file is part of mbed TLS (https://tls.mbed.org) - */ - -#include -#include - -#include -#include -#include - -#if !defined(CONFIG_MBEDTLS_CFG_FILE) -#include "mbedtls/config.h" -#else -#include CONFIG_MBEDTLS_CFG_FILE -#endif - -#if defined(MBEDTLS_PLATFORM_C) -#include "mbedtls/platform.h" -#else -#include -#define mbedtls_time_t time_t -#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS -#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE -#endif - -#include -#include -#include -#include -#include -#include - -#include - -#include "udp.h" -#include "udp_cfg.h" - -#include "mbedtls/net.h" -#include "mbedtls/ssl.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" -#include "mbedtls/ssl_cookie.h" - -#if defined(MBEDTLS_DEBUG_C) -#include "mbedtls/debug.h" -#define DEBUG_THRESHOLD 0 -#endif - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) -#include "mbedtls/memory_buffer_alloc.h" -static unsigned char heap[8192]; -#endif - -#define COAP_BUF_SIZE 128 - -NET_PKT_TX_SLAB_DEFINE(coap_pkt_slab, 4); -NET_BUF_POOL_DEFINE(coap_data_pool, 4, COAP_BUF_SIZE, 0, NULL); - -/* - * Hardcoded values for server host and port - */ - -const char *pers = "dtsl_server"; - -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) -const unsigned char psk[] = "passwd\0"; -const char psk_id[] = "Client_identity\0"; -#endif - -static mbedtls_ssl_context *curr_ctx; - -static int send_response(struct coap_packet *request, u8_t response_code) -{ - struct net_pkt *pkt; - struct net_buf *frag; - struct coap_packet response; - u8_t code, type; - u16_t id; - int r; - - code = coap_header_get_code(request); - type = coap_header_get_type(request); - id = coap_header_get_id(request); - - printk("*******\n"); - printk("type: %u code %u id %u\n", type, code, id); - printk("*******\n"); - - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - return -ENOMEM; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - net_pkt_unref(pkt); - return -ENOMEM; - } - - net_pkt_frag_add(pkt, frag); - - r = coap_packet_init(&response, pkt, 1, COAP_TYPE_ACK, - 0, NULL, response_code, id); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - do { - r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); - } while (r == MBEDTLS_ERR_SSL_WANT_READ - || r == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (r >= 0) { - r = 0; - } - - net_pkt_unref(pkt); - - return r; -} - -static int test_del(struct coap_resource *resource, - struct coap_packet *request) -{ - return send_response(request, COAP_RESPONSE_CODE_DELETED); -} - -static int test_put(struct coap_resource *resource, - struct coap_packet *request) -{ - return send_response(request, COAP_RESPONSE_CODE_CHANGED); -} - -static int test_post(struct coap_resource *resource, - struct coap_packet *request) -{ - return send_response(request, COAP_RESPONSE_CODE_CREATED); -} - -static int piggyback_get(struct coap_resource *resource, - struct coap_packet *request) -{ - struct net_pkt *pkt; - struct net_buf *frag; - struct coap_packet response; - u8_t payload[40], code, type; - u16_t id; - int r; - - code = coap_header_get_code(request); - type = coap_header_get_type(request); - id = coap_header_get_id(request); - - printk("*******\n"); - printk("type: %u code %u id %u\n", type, code, id); - printk("*******\n"); - - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - return -ENOMEM; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - net_pkt_unref(pkt); - return -ENOMEM; - } - - net_pkt_frag_add(pkt, frag); - - r = coap_packet_init(&response, pkt, 1, COAP_TYPE_ACK, - 0, NULL, COAP_RESPONSE_CODE_CONTENT, id); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - r = coap_packet_append_payload_marker(&response); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - /* The response that coap-client expects */ - r = snprintk((char *)payload, sizeof(payload), - "Type: %u\nCode: %u\nMID: %u\n", type, code, id); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - r = coap_packet_append_payload(&response, (u8_t *)payload, - strlen(payload)); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - do { - r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); - } while (r == MBEDTLS_ERR_SSL_WANT_READ - || r == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (r >= 0) { - r = 0; - } - - net_pkt_unref(pkt); - - return r; -} - -static int query_get(struct coap_resource *resource, - struct coap_packet *request) -{ - struct coap_option options[4]; - struct net_pkt *pkt; - struct net_buf *frag; - struct coap_packet response; - u8_t payload[40], code, type; - u16_t id; - int i, r; - - code = coap_header_get_code(request); - type = coap_header_get_type(request); - id = coap_header_get_id(request); - - r = coap_find_options(request, COAP_OPTION_URI_QUERY, options, 4); - if (r <= 0) { - return -EINVAL; - } - - printk("*******\n"); - printk("type: %u code %u id %u\n", type, code, id); - printk("num queries: %d\n", r); - - for (i = 0; i < r; i++) { - char str[16]; - - if (options[i].len + 1 > sizeof(str)) { - printk("Unexpected length of query: " - "%d (expected %zu)\n", - options[i].len, sizeof(str)); - break; - } - - memcpy(str, options[i].value, options[i].len); - str[options[i].len] = '\0'; - - printk("query[%d]: %s\n", i + 1, str); - } - - printk("*******\n"); - - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - return -ENOMEM; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - net_pkt_unref(pkt); - return -ENOMEM; - } - - net_pkt_frag_add(pkt, frag); - - r = coap_packet_init(&response, pkt, 1, COAP_TYPE_ACK, - 0, NULL, COAP_RESPONSE_CODE_CONTENT, id); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - r = coap_packet_append_payload_marker(&response); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - /* The response that coap-client expects */ - r = snprintk((char *)payload, sizeof(payload), - "Type: %u\nCode: %u\nMID: %u\n", type, code, id); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - r = coap_packet_append_payload(&response, (u8_t *)payload, - strlen(payload)); - if (r < 0) { - net_pkt_unref(pkt); - return -EINVAL; - } - - do { - r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); - } while (r == MBEDTLS_ERR_SSL_WANT_READ - || r == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (r >= 0) { - r = 0; - } - - net_pkt_unref(pkt); - - return r; -} - -static const char *const test_path[] = { "test", NULL }; - -static const char *const segments_path[] = { "seg1", "seg2", "seg3", NULL }; - -static const char *const query_path[] = { "query", NULL }; - -static struct coap_resource resources[] = { - {.get = piggyback_get, - .post = test_post, - .del = test_del, - .put = test_put, - .path = test_path}, - {.get = piggyback_get, - .path = segments_path, - }, - {.get = query_get, - .path = query_path, - }, - {}, -}; - -struct dtls_timing_context { - u32_t snapshot; - u32_t int_ms; - u32_t fin_ms; -}; - -static void my_debug(void *ctx, int level, - const char *file, int line, const char *str) -{ - const char *p, *basename; - - ARG_UNUSED(ctx); - - /* Extract basename from file */ - for (p = basename = file; *p != '\0'; p++) { - if (*p == '/' || *p == '\\') { - basename = p + 1; - } - - } - - mbedtls_printf("%s:%04d: |%d| %s", basename, line, level, str); -} - -void dtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) -{ - struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; - - ctx->int_ms = int_ms; - ctx->fin_ms = fin_ms; - - if (fin_ms != 0) { - ctx->snapshot = k_uptime_get_32(); - } -} - -int dtls_timing_get_delay(void *data) -{ - struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; - unsigned long elapsed_ms; - - if (ctx->fin_ms == 0) { - return -1; - } - - elapsed_ms = k_uptime_get_32() - ctx->snapshot; - - if (elapsed_ms >= ctx->fin_ms) { - return 2; - } - - if (elapsed_ms >= ctx->int_ms) { - return 1; - } - - return 0; -} - -static int entropy_source(void *data, unsigned char *output, size_t len, - size_t *olen) -{ - u32_t seed; - - ARG_UNUSED(data); - - seed = sys_rand32_get(); - - if (len > sizeof(seed)) { - len = sizeof(seed); - } - - memcpy(output, &seed, len); - - *olen = len; - - return 0; -} - -void dtls_server(void) -{ - int len, ret = 0; - struct udp_context ctx; - struct dtls_timing_context timer; - struct coap_packet cpkt; - struct net_pkt *pkt; - struct net_buf *frag; - struct coap_option options[16]; - u8_t opt_num = 16; - - mbedtls_ssl_cookie_ctx cookie_ctx; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_ssl_context ssl; - mbedtls_ssl_config conf; - - mbedtls_ctr_drbg_init(&ctr_drbg); - - mbedtls_platform_set_printf(printk); - -#if defined(MBEDTLS_DEBUG_C) - mbedtls_debug_set_threshold(DEBUG_THRESHOLD); -#endif - - /* - * Initialize and setup - */ - mbedtls_ssl_init(&ssl); - mbedtls_ssl_config_init(&conf); - - mbedtls_entropy_init(&entropy); - mbedtls_entropy_add_source(&entropy, entropy_source, NULL, - MBEDTLS_ENTROPY_MAX_GATHER, - MBEDTLS_ENTROPY_SOURCE_STRONG); - - ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - (const unsigned char *)pers, strlen(pers)); - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ctr_drbg_seed returned -0x%x\n", -ret); - goto exit; - } - - ret = mbedtls_ssl_config_defaults(&conf, - MBEDTLS_SSL_IS_SERVER, - MBEDTLS_SSL_TRANSPORT_DATAGRAM, - MBEDTLS_SSL_PRESET_DEFAULT); - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ssl_config_defaults returned -0x%x\n", - -ret); - goto exit; - } - -/* Modify this to change the default timeouts for the DTLS handshake */ -/* mbedtls_ssl_conf_handshake_timeout( &conf, min, max ); */ - - mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); - mbedtls_ssl_conf_dbg(&conf, my_debug, NULL); - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) - mbedtls_memory_buffer_alloc_init(heap, sizeof(heap)); -#endif - ret = mbedtls_ssl_cookie_setup(&cookie_ctx, mbedtls_ctr_drbg_random, - &ctr_drbg); - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ssl_cookie_setup returned -0x%x\n", - -ret); - goto exit; - } - - mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, - mbedtls_ssl_cookie_check, &cookie_ctx); - - ret = mbedtls_ssl_setup(&ssl, &conf); - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ssl_setup returned -0x%x\n", -ret); - goto exit; - } - - ret = udp_init(&ctx); - if (ret != 0) { - mbedtls_printf(" failed!\n udp_init returned 0x%x\n", ret); - goto exit; - } - -reset: - mbedtls_ssl_session_reset(&ssl); - -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) - ret = mbedtls_ssl_conf_psk(&conf, psk, strlen((char *)psk), - (unsigned char *)psk_id, - strlen(psk_id)); - if (ret != 0) { - mbedtls_printf(" failed!\n mbedtls_ssl_conf_psk" - " returned -0x%04X\n", -ret); - goto exit; - } -#endif - - mbedtls_ssl_set_timer_cb(&ssl, &timer, dtls_timing_set_delay, - dtls_timing_get_delay); - - mbedtls_ssl_set_bio(&ssl, &ctx, udp_tx, udp_rx, NULL); - - /* For HelloVerifyRequest cookies */ - ctx.client_id = (char)ctx.remaining; - - ret = mbedtls_ssl_set_client_transport_id( - &ssl, (unsigned char *)&ctx.client_id, sizeof(char)); - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ssl_set_client_transport_id()" - " returned -0x%x\n", -ret); - goto exit; - } - - curr_ctx = &ssl; - - do { - ret = mbedtls_ssl_handshake(&ssl); - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { - ret = 0; - goto reset; - } - - if (ret != 0) { - mbedtls_printf(" failed!\n" - " mbedtls_ssl_handshake returned -0x%x\n", - -ret); - goto reset; - } - - do { - struct net_buf *ip; - - /* Read the request */ - pkt = net_pkt_get_reserve(&coap_pkt_slab, 0, K_NO_WAIT); - if (!pkt) { - mbedtls_printf("Could not get packet from slab\n"); - goto exit; - } - - frag = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!frag) { - mbedtls_printf("Could not get frag from pool\n"); - goto exit; - } - - net_pkt_frag_add(pkt, frag); - len = COAP_BUF_SIZE - 1; - memset(frag->data, 0, COAP_BUF_SIZE); - - ret = mbedtls_ssl_read(&ssl, frag->data, len); - if (ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE) { - continue; - } - - if (ret <= 0) { - net_pkt_unref(pkt); - - switch (ret) { - case MBEDTLS_ERR_SSL_TIMEOUT: - mbedtls_printf(" timeout\n"); - goto reset; - - case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: - mbedtls_printf(" connection was closed" - " gracefully\n"); - goto close_notify; - - default: - mbedtls_printf(" mbedtls_ssl_read" - " returned -0x%x\n", -ret); - goto reset; - } - } - - len = ret; - frag->len = len; - - /* The COAP packet does not have IP + UDP header and the coap - * packet starts immediately from first fragment. The net_pkt - * contains information from which byte in net_buf the coap - * packet actually starts and coap API will use that info when - * parsing the packet. Because of this add a dummy IP + UDP - * header before the actual coap data so that parsing succeeds. - */ - ip = net_buf_alloc(&coap_data_pool, K_NO_WAIT); - if (!ip) { - mbedtls_printf("Could not get frag from pool\n"); - goto exit; - } - - net_buf_add(ip, net_pkt_ip_hdr_len(pkt) + - net_pkt_ipv6_ext_len(pkt) + NET_UDPH_LEN); - ip->frags = pkt->frags; - pkt->frags = ip; - - ret = coap_packet_parse(&cpkt, pkt, options, opt_num); - if (ret) { - mbedtls_printf("Could not parse packet\n"); - goto exit; - } - - ret = coap_handle_request(&cpkt, resources, options, opt_num); - if (ret < 0) { - mbedtls_printf("No handler for such request (%d)\n", - ret); - } - - net_pkt_unref(pkt); - - } while (1); - -close_notify: - /* No error checking, the connection might be closed already */ - do { - ret = mbedtls_ssl_close_notify(&ssl); - } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE); - ret = 0; - mbedtls_printf(" done\n"); - goto reset; - -exit: - mbedtls_ssl_free(&ssl); - mbedtls_ssl_config_free(&conf); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); -} - -#define STACK_SIZE 4096 -K_THREAD_STACK_DEFINE(stack, STACK_SIZE); -static struct k_thread thread_data; - -static inline int init_app(void) -{ -#if defined(CONFIG_NET_CONFIG_MY_IPV6_ADDR) - if (net_addr_pton(AF_INET6, CONFIG_NET_CONFIG_MY_IPV6_ADDR, - &server_addr) < 0) { - mbedtls_printf("Invalid IPv6 address %s", - CONFIG_NET_CONFIG_MY_IPV6_ADDR); - } -#endif - if (!net_if_ipv6_addr_add(net_if_get_default(), &server_addr, - NET_ADDR_MANUAL, 0)) { - return -EIO; - } - - net_if_ipv6_maddr_add(net_if_get_default(), &mcast_addr); - - return 0; -} - -void main(void) -{ - if (init_app() != 0) { - printk("Cannot initialize network\n"); - return; - } - - k_thread_create(&thread_data, stack, STACK_SIZE, - (k_thread_entry_t) dtls_server, - NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); - -} diff --git a/samples/net/coaps_server/src/udp.c b/samples/net/coaps_server/src/udp.c deleted file mode 100644 index 85ea49b08c7..00000000000 --- a/samples/net/coaps_server/src/udp.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (c) 2017 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "udp_cfg.h" -#include "udp.h" - -static const socklen_t addrlen = sizeof(struct sockaddr_in6); - -static void set_client_address(struct sockaddr *addr, struct net_pkt *rx_pkt) -{ - struct net_udp_hdr hdr, *udp_hdr; - - udp_hdr = net_udp_get_hdr(rx_pkt, &hdr); - if (!udp_hdr) { - printk("Invalid UDP data\n"); - return; - } - - net_ipaddr_copy(&net_sin6(addr)->sin6_addr, &NET_IPV6_HDR(rx_pkt)->src); - net_sin6(addr)->sin6_family = AF_INET6; - net_sin6(addr)->sin6_port = udp_hdr->src_port; -} - -static void udp_received(struct net_context *context, - struct net_pkt *pkt, int status, void *user_data) -{ - struct udp_context *ctx = user_data; - - ARG_UNUSED(context); - ARG_UNUSED(status); - - ctx->rx_pkt = pkt; - k_sem_give(&ctx->rx_sem); -} - -int udp_tx(void *context, const unsigned char *buf, size_t size) -{ - struct udp_context *ctx = context; - struct net_context *net_ctx; - struct net_pkt *send_pkt; - - int rc, len; - - net_ctx = ctx->net_ctx; - - send_pkt = net_pkt_get_tx(net_ctx, K_FOREVER); - if (!send_pkt) { - printk("cannot create pkt\n"); - return -EIO; - } - - rc = net_pkt_append_all(send_pkt, size, (u8_t *) buf, K_FOREVER); - if (!rc) { - printk("cannot write buf\n"); - return -EIO; - } - - len = net_pkt_get_len(send_pkt); - - rc = net_context_sendto(send_pkt, &net_ctx->remote, - addrlen, NULL, K_FOREVER, NULL, NULL); - - if (rc < 0) { - printk("Cannot send data to peer (%d)\n", rc); - net_pkt_unref(send_pkt); - return -EIO; - } else { - return len; - } -} - -int udp_rx(void *context, unsigned char *buf, size_t size) -{ - struct udp_context *ctx = context; - struct net_context *net_ctx = ctx->net_ctx; - struct net_pkt *rx_pkt = NULL; - struct net_buf *rx_buf; - u16_t read_bytes; - u8_t *ptr; - int pos; - int len; - int rc; - - k_sem_take(&ctx->rx_sem, K_FOREVER); - - read_bytes = net_pkt_appdatalen(ctx->rx_pkt); - if (read_bytes > size) { - return -ENOMEM; - } - - rx_pkt = ctx->rx_pkt; - - set_client_address(&net_ctx->remote, rx_pkt); - - ptr = net_pkt_appdata(rx_pkt); - rx_buf = rx_pkt->frags; - len = rx_buf->len - (ptr - rx_buf->data); - pos = 0; - - while (rx_buf) { - memcpy(buf + pos, ptr, len); - pos += len; - - rx_buf = rx_buf->frags; - if (!rx_buf) { - break; - } - - ptr = rx_buf->data; - len = rx_buf->len; - } - - net_pkt_unref(ctx->rx_pkt); - ctx->rx_pkt = NULL; - - if (read_bytes != pos) { - return -EIO; - } - - rc = read_bytes; - ctx->remaining = 0; - - return rc; -} - -int udp_init(struct udp_context *ctx) -{ - struct net_context *udp_ctx = { 0 }; - struct net_context *mcast_ctx = { 0 }; - struct sockaddr_in6 my_addr = { 0 }; - struct sockaddr_in6 my_mcast_addr = { 0 }; - int rc; - - k_sem_init(&ctx->rx_sem, 0, UINT_MAX); - - net_ipaddr_copy(&my_mcast_addr.sin6_addr, &mcast_addr); - my_mcast_addr.sin6_family = AF_INET6; - - net_ipaddr_copy(&my_addr.sin6_addr, &server_addr); - my_addr.sin6_family = AF_INET6; - my_addr.sin6_port = htons(SERVER_PORT); - - rc = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &udp_ctx); - if (rc < 0) { - printk("Cannot get network context for IPv6 UDP (%d)", rc); - return -EIO; - } - - rc = net_context_bind(udp_ctx, (struct sockaddr *)&my_addr, - sizeof(struct sockaddr_in6)); - if (rc < 0) { - printk("Cannot bind IPv6 UDP port %d (%d)", SERVER_PORT, rc); - goto error; - } - - rc = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &mcast_ctx); - if (rc < 0) { - printk("Cannot get receiving IPv6 mcast (%d)", rc); - goto error; - } - - rc = net_context_bind(mcast_ctx, (struct sockaddr *)&my_mcast_addr, - sizeof(struct sockaddr_in6)); - if (rc < 0) { - printk("Cannot get bind IPv6 mcast (%d)", rc); - goto error; - } - - ctx->rx_pkt = NULL; - ctx->remaining = 0; - ctx->net_ctx = udp_ctx; - - rc = net_context_recv(ctx->net_ctx, udp_received, K_NO_WAIT, ctx); - if (rc != 0) { - return -EIO; - } - - return 0; - -error: - net_context_put(udp_ctx); - return -EINVAL; -} diff --git a/samples/net/coaps_server/src/udp.h b/samples/net/coaps_server/src/udp.h deleted file mode 100644 index 02814b8ea08..00000000000 --- a/samples/net/coaps_server/src/udp.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2017 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef _UDP_H_ -#define _UDP_H_ - -#include - -struct udp_context { - struct net_context *net_ctx; - struct net_pkt *rx_pkt; - struct k_sem rx_sem; - int remaining; - char client_id; -}; - -int udp_init(struct udp_context *ctx); -int udp_tx(void *ctx, const unsigned char *buf, size_t size); -int udp_rx(void *ctx, unsigned char *buf, size_t size); - -#endif diff --git a/samples/net/coaps_server/src/udp_cfg.h b/samples/net/coaps_server/src/udp_cfg.h deleted file mode 100644 index ef6e39b1dfc..00000000000 --- a/samples/net/coaps_server/src/udp_cfg.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2017 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef UDP_CONFIG_H_ -#define UDP_CONFIG_H_ - -#define MCAST_IP_ADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, \ - 0, 0, 0, 0, 0, 0, 0, 0x2 } } } - -#define SERVER_PREFIX_LEN 64 - -static struct in6_addr server_addr; -static struct in6_addr mcast_addr = MCAST_IP_ADDR; - -#define SERVER_PORT 5684 - -#endif