net: samples: Convert echo-server to use network app API

This commit will convert echo-server to use the net app API
when creating the listening service. Most of the network
setup code will be removed from echo-server by this commit.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2017-05-10 22:25:37 +03:00 committed by Anas Nashif
commit 2e7c3519a4
18 changed files with 826 additions and 512 deletions

View file

@ -30,3 +30,5 @@ CONFIG_SPI=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV4=y

View file

@ -29,6 +29,8 @@ CONFIG_NET_MAX_CONTEXTS=10
CONFIG_NET_SHELL=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"

View file

@ -42,5 +42,7 @@ CONFIG_IEEE802154_CC2520=y
CONFIG_IEEE802154_CC2520_AUTO_ACK=y
CONFIG_SYS_LOG_IEEE802154_DRIVER_LEVEL=1
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::2"

View file

@ -25,6 +25,9 @@ CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_SHELL=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_NEED_IPV4=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"

View file

@ -49,5 +49,7 @@ CONFIG_IEEE802154_CC2520_SPI_DRV_NAME="SPI_0"
CONFIG_IEEE802154_CC2520_SPI_FREQ=4000000
CONFIG_IEEE802154_CC2520_SPI_SLAVE=0
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::2"

View file

@ -42,5 +42,7 @@ CONFIG_SPI_0=y
CONFIG_IEEE802154_MCR20A=y
CONFIG_SYS_LOG_IEEE802154_DRIVER_LEVEL=1
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::2"

View file

@ -49,6 +49,8 @@ CONFIG_IEEE802154_NRF5_DRV_NAME="IEEE802154_nrf5"
CONFIG_IEEE802154_NRF5_INIT_PRIO=80
CONFIG_SYS_LOG_IEEE802154_DRIVER_LEVEL=1
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::2"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::1"

View file

@ -43,5 +43,7 @@ CONFIG_NET_DEBUG_L2_IEEE802154_FRAGMENT=y
CONFIG_IEEE802154_UPIPE=y
CONFIG_SYS_LOG_IEEE802154_DRIVER_LEVEL=1
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::2"

View file

@ -21,6 +21,9 @@ CONFIG_NET_MAX_CONTEXTS=16
CONFIG_NET_SHELL=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_NEED_IPV4=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"

View file

@ -1,30 +1,43 @@
CONFIG_NETWORKING=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
#CONFIG_NET_DHCPV4=y
CONFIG_RANDOM_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NET_BUF_LOG=y
CONFIG_SYS_LOG_NET_BUF_LEVEL=2
CONFIG_NET_LOG=y
CONFIG_SYS_LOG_NET_LEVEL=2
CONFIG_NET_SLIP_TAP=y
CONFIG_SYS_LOG_SHOW_COLOR=y
CONFIG_INIT_STACKS=y
CONFIG_PRINTK=y
CONFIG_NET_LOG=y
CONFIG_SYS_LOG_NET_LEVEL=4
CONFIG_SYS_LOG_SHOW_COLOR=y
CONFIG_NET_STATISTICS=y
CONFIG_PRINTK=y
CONFIG_NET_SLIP_TAP=y
CONFIG_NET_PKT_RX_COUNT=10
CONFIG_NET_PKT_TX_COUNT=10
CONFIG_NET_BUF_RX_COUNT=20
CONFIG_NET_BUF_TX_COUNT=20
CONFIG_NET_CONTEXT_NET_PKT_POOL=y
CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=4
CONFIG_NET_MAX_CONTEXTS=10
CONFIG_NET_SHELL=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_NEED_IPV4=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_DEBUG_APP=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
CONFIG_NET_DEBUG_CONTEXT=n

View file

@ -0,0 +1,45 @@
CONFIG_NETWORKING=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=n
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NET_BUF_LOG=y
CONFIG_SYS_LOG_NET_BUF_LEVEL=2
CONFIG_NET_LOG=y
CONFIG_SYS_LOG_NET_LEVEL=2
CONFIG_NET_DEBUG_NET_PKT=y
CONFIG_NET_SLIP_TAP=y
CONFIG_SYS_LOG_SHOW_COLOR=y
CONFIG_INIT_STACKS=y
CONFIG_PRINTK=y
CONFIG_NET_STATISTICS=y
CONFIG_NET_PKT_RX_COUNT=100
CONFIG_NET_PKT_TX_COUNT=100
CONFIG_NET_BUF_RX_COUNT=100
CONFIG_NET_BUF_TX_COUNT=100
CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5
CONFIG_NET_MAX_CONTEXTS=10
CONFIG_NET_SHELL=y
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_CFG_FILE="config-mini-tls1_2.h"
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=30000
CONFIG_NET_APP=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_NEED_IPV4=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_TLS=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_PEER_IPV6_ADDR="2001:db8::2"
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
CONFIG_RAM_SIZE=320

View file

@ -24,6 +24,9 @@ CONFIG_PRINTK=y
CONFIG_ETH_SAM_GMAC=y
CONFIG_NET_APP_SERVER=y
CONFIG_NET_APP_NEED_IPV6=y
CONFIG_NET_APP_NEED_IPV4=y
CONFIG_NET_APP_SETTINGS=y
CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"

View file

@ -1,33 +1,11 @@
obj-y = echo-server.o
#
# Copyright (c) 2017 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
ifeq ($(CONFIG_IEEE802154_CC2520),y)
include $(ZEPHYR_BASE)/samples/net/common/Makefile.common
ifeq ($(CONFIG_BOARD_ARDUINO_101),y)
ccflags-y +=-I${ZEPHYR_BASE}/include/drivers/
obj-y += ../../common/cc2520_a101.o
endif
ifeq ($(CONFIG_BOARD_FRDM_K64F),y)
ccflags-y +=-I${ZEPHYR_BASE}/drivers/
ccflags-y +=-I${ZEPHYR_BASE}/include/drivers/
obj-y += ../../common/cc2520_frdm_k64f.o
endif
endif
ifeq ($(CONFIG_NET_TESTING), y)
ccflags-y +=-I${ZEPHYR_BASE}/samples/net/common/
ccflags-y +=-DNET_TESTING_SERVER=1
endif
ifeq ($(CONFIG_NET_L2_BLUETOOTH), y)
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth/
obj-y += ../../../bluetooth/gatt/ipss.o
endif
ifeq ($(CONFIG_NET_L2_IEEE802154), y)
ccflags-y +=-I${ZEPHYR_BASE}/samples/net/common/
ifeq ($(CONFIG_NET_APP_SETTINGS), y)
obj-y += ../../common/ieee802154_settings.o
endif
endif
obj-y += echo-server.o
obj-$(CONFIG_NET_UDP) += udp.o
obj-$(CONFIG_NET_TCP) += tcp.o

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define MY_PORT 4242
#define MAX_DBG_PRINT 64
void start_udp(void);
void stop_udp(void);
void start_tcp(void);
void stop_tcp(void);
struct net_pkt *build_reply_pkt(const char *name,
struct net_app_ctx *ctx,
struct net_pkt *pkt);
void pkt_sent(struct net_app_ctx *ctx, int status,
void *token, void *user_data);
void panic(const char *msg);
void quit(void);

View file

@ -17,10 +17,11 @@
#include <errno.h>
#include <net/net_pkt.h>
#include <net/net_if.h>
#include <net/net_core.h>
#include <net/net_context.h>
#include <net/net_app.h>
#if defined(CONFIG_NET_L2_BLUETOOTH)
#include <bluetooth/bluetooth.h>
#include <gatt/ipss.h>
@ -30,267 +31,48 @@
#include <ieee802154_settings.h>
#endif
/* Allow binding to ANY IP address. */
#define NET_BIND_ANY_ADDR 1
#include "common.h"
#if defined(CONFIG_NET_IPV6)
/* Define my IP address where to expect messages */
#define MY_IP6ADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0x1 } } }
#define MY_PREFIX_LEN 64
static struct in6_addr in6addr_my = MY_IP6ADDR;
#endif /* IPv6 */
#if defined(CONFIG_NET_IPV4)
/* The 192.0.2.0/24 is the private address space for documentation RFC 5737 */
#define MY_IP4ADDR { { { 192, 0, 2, 1 } } }
#if !defined(CONFIG_NET_DHCPV4) || !NET_BIND_ANY_ADDR
static struct in_addr in4addr_my = MY_IP4ADDR;
#endif /* CONFIG_NET_DHCPV4 || !NET_BIND_ANY_ADDR */
#endif /* IPv4 */
/* Note that both tcp and udp can share the same pool but in this
* example the UDP context and TCP context have separate pools.
/* The startup time needs to be longish if DHCP is enabled as setting
* DHCP up takes some time.
*/
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
#if defined(CONFIG_NET_TCP)
NET_PKT_TX_SLAB_DEFINE(echo_tx_tcp, 15);
NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30);
#define APP_STARTUP_TIME K_SECONDS(20)
static struct k_mem_slab *tx_tcp_slab(void)
{
return &echo_tx_tcp;
}
static struct net_buf_pool *data_tcp_pool(void)
{
return &echo_data_tcp;
}
#endif
#if defined(CONFIG_NET_UDP)
NET_PKT_TX_SLAB_DEFINE(echo_tx_udp, 5);
NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20);
static struct k_mem_slab *tx_udp_slab(void)
{
return &echo_tx_udp;
}
static struct net_buf_pool *data_udp_pool(void)
{
return &echo_data_udp;
}
#endif
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
#define MY_PORT 4242
#define STACKSIZE 2000
K_THREAD_STACK_DEFINE(thread_stack, STACKSIZE);
static struct k_thread thread_data;
#define MAX_DBG_PRINT 64
#define APP_BANNER "Run echo server"
static struct k_sem quit_lock;
static inline void quit(void)
void panic(const char *msg)
{
if (msg) {
NET_ERR("%s", msg);
}
for (;;) {
k_sleep(K_FOREVER);
}
}
void quit(void)
{
k_sem_give(&quit_lock);
}
static inline void init_app(void)
{
NET_INFO("Run echo server");
k_sem_init(&quit_lock, 0, UINT_MAX);
#if defined(CONFIG_NET_IPV6)
#if defined(CONFIG_NET_APP_MY_IPV6_ADDR)
if (net_addr_pton(AF_INET6,
CONFIG_NET_APP_MY_IPV6_ADDR,
&in6addr_my) < 0) {
NET_ERR("Invalid IPv6 address %s",
CONFIG_NET_APP_MY_IPV6_ADDR);
}
#endif
do {
struct net_if_addr *ifaddr;
ifaddr = net_if_ipv6_addr_add(net_if_get_default(),
&in6addr_my, NET_ADDR_MANUAL, 0);
} while (0);
#endif
#if defined(CONFIG_NET_IPV4)
#if defined(CONFIG_NET_DHCPV4)
net_dhcpv4_start(net_if_get_default());
#else
#if defined(CONFIG_NET_APP_MY_IPV4_ADDR)
if (net_addr_pton(AF_INET,
CONFIG_NET_APP_MY_IPV4_ADDR,
&in4addr_my) < 0) {
NET_ERR("Invalid IPv4 address %s",
CONFIG_NET_APP_MY_IPV4_ADDR);
}
net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my,
NET_ADDR_MANUAL, 0);
#endif
#endif /* CONFIG_NET_DHCPV4 */
#endif /* CONFIG_NET_IPV4 */
}
static inline bool get_context(struct net_context **udp_recv4,
struct net_context **udp_recv6,
struct net_context **tcp_recv4,
struct net_context **tcp_recv6)
{
int ret;
#if defined(CONFIG_NET_IPV6)
struct sockaddr_in6 my_addr6 = { 0 };
#endif
#if defined(CONFIG_NET_IPV4)
struct sockaddr_in my_addr4 = { 0 };
#endif
#if defined(CONFIG_NET_IPV6)
#if !NET_BIND_ANY_ADDR
net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my);
#endif
my_addr6.sin6_family = AF_INET6;
my_addr6.sin6_port = htons(MY_PORT);
#endif
#if defined(CONFIG_NET_IPV4)
#if !NET_BIND_ANY_ADDR
net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my);
#endif
my_addr4.sin_family = AF_INET;
my_addr4.sin_port = htons(MY_PORT);
#endif
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_UDP)
ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, udp_recv6);
if (ret < 0) {
NET_ERR("Cannot get network context for IPv6 UDP (%d)",
ret);
return false;
}
net_context_setup_pools(*udp_recv6, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6,
sizeof(struct sockaddr_in6));
if (ret < 0) {
NET_ERR("Cannot bind IPv6 UDP port %d (%d)",
ntohs(my_addr6.sin6_port), ret);
return false;
}
#endif
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_UDP)
ret = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP, udp_recv4);
if (ret < 0) {
NET_ERR("Cannot get network context for IPv4 UDP (%d)",
ret);
return false;
}
net_context_setup_pools(*udp_recv4, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4,
sizeof(struct sockaddr_in));
if (ret < 0) {
NET_ERR("Cannot bind IPv4 UDP port %d (%d)",
ntohs(my_addr4.sin_port), ret);
return false;
}
#endif
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_TCP)
if (tcp_recv6) {
ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP,
tcp_recv6);
if (ret < 0) {
NET_ERR("Cannot get network context "
"for IPv6 TCP (%d)", ret);
return false;
}
net_context_setup_pools(*tcp_recv6, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv6,
(struct sockaddr *)&my_addr6,
sizeof(struct sockaddr_in6));
if (ret < 0) {
NET_ERR("Cannot bind IPv6 TCP port %d (%d)",
ntohs(my_addr6.sin6_port), ret);
return false;
}
ret = net_context_listen(*tcp_recv6, 0);
if (ret < 0) {
NET_ERR("Cannot listen IPv6 TCP (%d)", ret);
return false;
}
}
#endif
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_TCP)
if (tcp_recv4) {
ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP,
tcp_recv4);
if (ret < 0) {
NET_ERR("Cannot get network context for IPv4 TCP");
return false;
}
net_context_setup_pools(*tcp_recv4, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv4,
(struct sockaddr *)&my_addr4,
sizeof(struct sockaddr_in));
if (ret < 0) {
NET_ERR("Cannot bind IPv4 TCP port %d",
ntohs(my_addr4.sin_port));
return false;
}
ret = net_context_listen(*tcp_recv4, 0);
if (ret < 0) {
NET_ERR("Cannot listen IPv4 TCP");
return false;
}
}
#endif
return true;
}
static struct net_pkt *build_reply_pkt(const char *name,
struct net_context *context,
struct net_pkt *pkt)
struct net_pkt *build_reply_pkt(const char *name,
struct net_app_ctx *ctx,
struct net_pkt *pkt)
{
struct net_pkt *reply_pkt;
struct net_buf *frag, *tmp;
int header_len, recv_len, reply_len;
int header_len = 0, recv_len, reply_len;
NET_INFO("%s received %d bytes", name,
net_pkt_appdatalen(pkt));
NET_INFO("%s received %d bytes", name, net_pkt_appdatalen(pkt));
if (net_pkt_appdatalen(pkt) == 0) {
return NULL;
}
reply_pkt = net_pkt_get_tx(context, K_FOREVER);
reply_pkt = net_app_get_net_pkt(ctx, K_FOREVER);
NET_ASSERT(reply_pkt);
@ -298,22 +80,27 @@ static struct net_pkt *build_reply_pkt(const char *name,
tmp = pkt->frags;
/* First fragment will contain IP header so move the data
* down in order to get rid of it.
*/
header_len = net_pkt_appdata(pkt) - tmp->data;
/* If we have link layer headers, then get rid of them here. */
if (recv_len != net_pkt_appdatalen(pkt)) {
/* First fragment will contain IP header so move the data
* down in order to get rid of it.
*/
header_len = net_pkt_appdata(pkt) - tmp->data;
NET_ASSERT(header_len < CONFIG_NET_BUF_DATA_SIZE);
NET_ASSERT(header_len < CONFIG_NET_BUF_DATA_SIZE);
/* After this pull, the tmp->data points directly to application
* data.
*/
net_buf_pull(tmp, header_len);
/* After this pull, the tmp->data points directly to application
* data.
*/
net_buf_pull(tmp, header_len);
}
net_pkt_set_appdatalen(reply_pkt, net_pkt_appdatalen(pkt));
while (tmp) {
frag = net_pkt_get_data(context, K_FOREVER);
frag = net_app_get_net_buf(ctx, reply_pkt, K_FOREVER);
if (!net_buf_headroom(tmp)) {
if (net_buf_headroom(tmp) == 0) {
/* If there is no link layer headers in the
* received fragment, then get rid of that also
* in the sending fragment. We end up here
@ -331,7 +118,9 @@ static struct net_pkt *build_reply_pkt(const char *name,
net_pkt_set_ll_reserve(reply_pkt, 0);
}
NET_ASSERT(net_buf_tailroom(frag) >= tmp->len);
NET_ASSERT_INFO(net_buf_tailroom(frag) >= tmp->len,
"tail %zd longer than len %d",
net_buf_tailroom(frag), tmp->len);
memcpy(net_buf_add(frag, tmp->len), tmp->data, tmp->len);
@ -349,228 +138,24 @@ static struct net_pkt *build_reply_pkt(const char *name,
return reply_pkt;
}
static inline void pkt_sent(struct net_context *context,
int status,
void *token,
void *user_data)
void pkt_sent(struct net_app_ctx *ctx,
int status,
void *user_data_send,
void *user_data)
{
if (!status) {
NET_INFO("Sent %d bytes", POINTER_TO_UINT(token));
NET_INFO("Sent %d bytes", POINTER_TO_UINT(user_data_send));
}
}
#if defined(CONFIG_NET_UDP)
static inline void set_dst_addr(sa_family_t family,
struct net_pkt *pkt,
struct sockaddr *dst_addr)
static inline int init_app(void)
{
#if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) {
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
&NET_IPV6_HDR(pkt)->src);
net_sin6(dst_addr)->sin6_family = AF_INET6;
net_sin6(dst_addr)->sin6_port = NET_UDP_HDR(pkt)->src_port;
}
#endif /* CONFIG_NET_IPV6) */
#if defined(CONFIG_NET_IPV4)
if (family == AF_INET) {
net_ipaddr_copy(&net_sin(dst_addr)->sin_addr,
&NET_IPV4_HDR(pkt)->src);
net_sin(dst_addr)->sin_family = AF_INET;
net_sin(dst_addr)->sin_port = NET_UDP_HDR(pkt)->src_port;
}
#endif /* CONFIG_NET_IPV6) */
}
static void udp_received(struct net_context *context,
struct net_pkt *pkt,
int status,
void *user_data)
{
struct net_pkt *reply_pkt;
struct sockaddr dst_addr;
sa_family_t family = net_pkt_family(pkt);
static char dbg[MAX_DBG_PRINT + 1];
int ret;
snprintk(dbg, MAX_DBG_PRINT, "UDP IPv%c",
family == AF_INET6 ? '6' : '4');
set_dst_addr(family, pkt, &dst_addr);
reply_pkt = build_reply_pkt(dbg, context, pkt);
net_pkt_unref(pkt);
if (!reply_pkt) {
return;
}
ret = net_context_sendto(reply_pkt, &dst_addr,
family == AF_INET6 ?
sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in),
pkt_sent, 0,
UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
user_data);
if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_pkt);
}
}
static void setup_udp_recv(struct net_context *udp_recv4,
struct net_context *udp_recv6)
{
int ret;
#if defined(CONFIG_NET_IPV6)
ret = net_context_recv(udp_recv6, udp_received, 0, NULL);
if (ret < 0) {
NET_ERR("Cannot receive IPv6 UDP packets");
}
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4)
ret = net_context_recv(udp_recv4, udp_received, 0, NULL);
if (ret < 0) {
NET_ERR("Cannot receive IPv4 UDP packets");
}
#endif /* CONFIG_NET_IPV4 */
}
#endif /* CONFIG_NET_UDP */
#if defined(CONFIG_NET_TCP)
static void tcp_received(struct net_context *context,
struct net_pkt *pkt,
int status,
void *user_data)
{
static char dbg[MAX_DBG_PRINT + 1];
struct net_pkt *reply_pkt;
sa_family_t family;
int ret;
if (!pkt) {
/* EOF condition */
return;
}
family = net_pkt_family(pkt);
snprintk(dbg, MAX_DBG_PRINT, "TCP IPv%c",
family == AF_INET6 ? '6' : '4');
reply_pkt = build_reply_pkt(dbg, context, pkt);
net_pkt_unref(pkt);
if (!reply_pkt) {
return;
}
ret = net_context_send(reply_pkt, pkt_sent, K_NO_WAIT,
UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
NULL);
if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_pkt);
quit();
}
}
static void tcp_accepted(struct net_context *context,
struct sockaddr *addr,
socklen_t addrlen,
int error,
void *user_data)
{
int ret;
NET_DBG("Accept called, context %p error %d", context, error);
ret = net_context_recv(context, tcp_received, 0, NULL);
if (ret < 0) {
NET_ERR("Cannot receive TCP packet (family %d)",
net_context_get_family(context));
}
}
static void setup_tcp_accept(struct net_context *tcp_recv4,
struct net_context *tcp_recv6)
{
int ret;
#if defined(CONFIG_NET_IPV6)
ret = net_context_accept(tcp_recv6, tcp_accepted, 0, NULL);
if (ret < 0) {
NET_ERR("Cannot receive IPv6 TCP packets (%d)", ret);
}
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4)
ret = net_context_accept(tcp_recv4, tcp_accepted, 0, NULL);
if (ret < 0) {
NET_ERR("Cannot receive IPv4 TCP packets (%d)", ret);
}
#endif /* CONFIG_NET_IPV4 */
}
#endif /* CONFIG_NET_TCP */
void receive(void)
{
struct net_context *udp_recv4 = { 0 };
struct net_context *udp_recv6 = { 0 };
struct net_context *tcp_recv4 = { 0 };
struct net_context *tcp_recv6 = { 0 };
if (!get_context(&udp_recv4, &udp_recv6,
&tcp_recv4, &tcp_recv6)) {
NET_ERR("Cannot get network contexts");
return;
}
NET_INFO("Starting to wait");
#if defined(CONFIG_NET_TCP)
setup_tcp_accept(tcp_recv4, tcp_recv6);
#endif
#if defined(CONFIG_NET_UDP)
setup_udp_recv(udp_recv4, udp_recv6);
#endif
k_sem_take(&quit_lock, K_FOREVER);
NET_INFO("Stopping...");
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_UDP)
net_context_put(udp_recv6);
#endif
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_UDP)
net_context_put(udp_recv4);
#endif
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_TCP)
net_context_put(tcp_recv6);
#endif
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_TCP)
net_context_put(tcp_recv4);
#endif
}
void main(void)
{
init_app();
k_sem_init(&quit_lock, 0, UINT_MAX);
#if defined(CONFIG_NET_L2_BLUETOOTH)
if (bt_enable(NULL)) {
NET_ERR("Bluetooth init failed");
return;
return -EFAULT;
}
ipss_init();
ipss_advertise();
@ -579,11 +164,34 @@ void main(void)
#if defined(CONFIG_NET_L2_IEEE802154)
if (ieee802154_sample_setup()) {
NET_ERR("IEEE 802.15.4 setup failed");
return;
return -EFAULT;
}
#endif
k_thread_create(&thread_data, thread_stack, STACKSIZE,
(k_thread_entry_t)receive,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0);
return 0;
}
void main(void)
{
init_app();
if (IS_ENABLED(CONFIG_NET_TCP)) {
start_tcp();
}
if (IS_ENABLED(CONFIG_NET_UDP)) {
start_udp();
}
k_sem_take(&quit_lock, K_FOREVER);
NET_INFO("Stopping...");
if (IS_ENABLED(CONFIG_NET_TCP)) {
stop_tcp();
}
if (IS_ENABLED(CONFIG_NET_UDP)) {
stop_udp();
}
}

View file

@ -0,0 +1,203 @@
/* tcp.c - TCP specific code for echo server */
/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#if 1
#define SYS_LOG_DOMAIN "echo-server"
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif
#include <zephyr.h>
#include <errno.h>
#include <stdio.h>
#include <net/net_pkt.h>
#include <net/net_core.h>
#include <net/net_context.h>
#include <net/net_app.h>
#include "common.h"
static struct net_app_ctx tcp;
/* Note that both tcp and udp can share the same pool but in this
* example the UDP context and TCP context have separate pools.
*/
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
NET_PKT_TX_SLAB_DEFINE(echo_tx_tcp, 15);
NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30);
static struct k_mem_slab *tx_tcp_slab(void)
{
return &echo_tx_tcp;
}
static struct net_buf_pool *data_tcp_pool(void)
{
return &echo_data_tcp;
}
#else
#define tx_tcp_slab NULL
#define data_tcp_pool NULL
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
#if defined(CONFIG_NET_APP_TLS)
/* The result buf size is set to large enough so that we can receive max size
* buf back. Note that mbedtls needs also be configured to have equal size
* value for its buffer size. See MBEDTLS_SSL_MAX_CONTENT_LEN option in TLS
* config file.
*/
#define RESULT_BUF_SIZE 1500
static u8_t tls_result[RESULT_BUF_SIZE];
#if !defined(CONFIG_NET_APP_TLS_STACK_SIZE)
#define CONFIG_NET_APP_TLS_STACK_SIZE 8192
#endif /* CONFIG_NET_APP_TLS_STACK_SIZE */
#define APP_BANNER "Run TLS echo-server"
#define INSTANCE_INFO "Zephyr TLS echo-server #1"
/* Note that each net_app context needs its own stack as there will be
* a separate thread needed.
*/
NET_STACK_DEFINE(NET_APP_TLS, net_app_tls_stack,
CONFIG_NET_APP_TLS_STACK_SIZE, CONFIG_NET_APP_TLS_STACK_SIZE);
#define RX_FIFO_DEPTH 4
K_MEM_POOL_DEFINE(ssl_pool, 4, 64, RX_FIFO_DEPTH, 4);
#endif /* CONFIG_NET_APP_TLS */
#if defined(CONFIG_NET_APP_TLS)
/* Load the certificates and private RSA key. */
#include "test_certs.h"
static int setup_cert(struct net_app_ctx *ctx,
mbedtls_x509_crt *cert,
mbedtls_pk_context *pkey)
{
int ret;
ret = mbedtls_x509_crt_parse(cert, rsa_example_cert_der,
rsa_example_cert_der_len);
if (ret != 0) {
NET_ERR("mbedtls_x509_crt_parse returned %d", ret);
return ret;
}
ret = mbedtls_pk_parse_key(pkey, rsa_example_keypair_der,
rsa_example_keypair_der_len, NULL, 0);
if (ret != 0) {
NET_ERR("mbedtls_pk_parse_key returned %d", ret);
return ret;
}
return 0;
}
#endif /* CONFIG_NET_APP_TLS */
static void tcp_received(struct net_app_ctx *ctx,
struct net_pkt *pkt,
int status,
void *user_data)
{
static char dbg[MAX_DBG_PRINT + 1];
struct net_pkt *reply_pkt;
sa_family_t family;
int ret;
if (!pkt) {
/* EOF condition */
return;
}
family = net_pkt_family(pkt);
snprintk(dbg, MAX_DBG_PRINT, "TCP IPv%c",
family == AF_INET6 ? '6' : '4');
reply_pkt = build_reply_pkt(dbg, ctx, pkt);
net_pkt_unref(pkt);
if (!reply_pkt) {
return;
}
ret = net_app_send_pkt(ctx, reply_pkt, NULL, 0, K_NO_WAIT,
UINT_TO_POINTER(net_pkt_get_len(reply_pkt)));
if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_pkt);
quit();
}
}
void start_tcp(void)
{
int ret;
ret = net_app_init_tcp_server(&tcp, NULL, MY_PORT, NULL);
if (ret < 0) {
NET_ERR("Cannot init TCP service at port %d", MY_PORT);
return;
}
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
net_app_set_net_pkt_pool(&tcp, tx_tcp_slab, data_tcp_pool);
#endif
ret = net_app_set_cb(&tcp, NULL, tcp_received, NULL, NULL);
if (ret < 0) {
NET_ERR("Cannot set callbacks (%d)", ret);
net_app_release(&tcp);
return;
}
#if defined(CONFIG_NET_APP_TLS)
ret = net_app_server_tls(&tcp,
tls_result,
sizeof(tls_result),
APP_BANNER,
INSTANCE_INFO,
strlen(INSTANCE_INFO),
setup_cert,
NULL,
&ssl_pool,
net_app_tls_stack,
K_THREAD_STACK_SIZEOF(net_app_tls_stack));
if (ret < 0) {
NET_ERR("Cannot init TLS");
} else {
ret = net_app_server_tls_enable(&tcp);
if (!ret) {
NET_ERR("Cannot enable TLS support");
}
}
#endif
ret = net_app_listen(&tcp);
if (ret < 0) {
NET_ERR("Cannot wait connection (%d)", ret);
net_app_release(&tcp);
return;
}
}
void stop_tcp(void)
{
#if defined(CONFIG_NET_APP_TLS)
net_app_server_tls_disable(&tcp);
#endif
net_app_close(&tcp);
net_app_release(&tcp);
}

View file

@ -0,0 +1,276 @@
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __TEST_CERTS_H__
#define __TEST_CERTS_H__
/* Read the README file in net-tools repo how to generate
* the key and the certificate.
*/
/* This is the private key in pkcs#8 format. */
static const unsigned char rsa_example_keypair_der[] = {
0x30, 0x82, 0x04, 0xbe, 0x02, 0x01, 0x00, 0x30,
0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
0x04, 0xa8, 0x30, 0x82, 0x04, 0xa4, 0x02, 0x01,
0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc6, 0x00,
0x7b, 0x0d, 0xd1, 0x17, 0x43, 0x6b, 0xa9, 0xa7,
0x79, 0x9c, 0x0f, 0x8d, 0x77, 0x91, 0xd2, 0xf7,
0x47, 0x35, 0xb3, 0x17, 0xe2, 0xdd, 0xed, 0x6d,
0x01, 0xf9, 0xb1, 0x92, 0xc9, 0x48, 0x80, 0xe0,
0x1f, 0xcf, 0xb7, 0xa4, 0x5f, 0xf0, 0x36, 0xea,
0xbf, 0xe1, 0x33, 0xf8, 0xa9, 0xc5, 0xe6, 0xd4,
0x19, 0x8b, 0x82, 0x25, 0xd9, 0x19, 0x74, 0x70,
0x79, 0xec, 0xc6, 0x68, 0xc9, 0xef, 0xce, 0x1a,
0xa9, 0xf0, 0xb7, 0x01, 0x35, 0x91, 0xff, 0xd3,
0x75, 0x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0x31, 0xfc, 0x97,
0x96, 0x06, 0x63, 0x36, 0x86, 0x84, 0x42, 0x7d,
0x86, 0xa2, 0x92, 0x5c, 0xfa, 0xbc, 0x4a, 0x7c,
0x29, 0xb0, 0xc2, 0x4d, 0xb4, 0x2d, 0x55, 0x7a,
0xca, 0x06, 0x96, 0xac, 0xa9, 0x8c, 0x7a, 0x9a,
0x84, 0x38, 0x11, 0x62, 0x2d, 0x23, 0xb4, 0xdf,
0x1c, 0xf1, 0x39, 0xa1, 0xa5, 0x97, 0x61, 0x02,
0x81, 0x80, 0x6c, 0xfb, 0x23, 0x03, 0xa6, 0xa4,
0x29, 0x03, 0x36, 0x84, 0x86, 0x95, 0x6d, 0x86,
0x32, 0x98, 0xbe, 0x0a, 0xa2, 0xba, 0x47, 0xd0,
0x29, 0x3f, 0x8c, 0xa6, 0x28, 0x93, 0x6f, 0x25,
0x69, 0x38, 0x38, 0x03, 0xe5, 0x9b, 0xc8, 0x48,
0x35, 0xc7, 0x26, 0xc0, 0x75, 0xd2, 0x95, 0x5b,
0x51, 0x6d, 0x28, 0x88, 0xb1, 0x4b, 0xf0, 0x59,
0x18, 0x15, 0x9d, 0x9e, 0x48, 0xb5, 0x67, 0x0c,
0xb0, 0x83, 0xfa, 0x45, 0x48, 0x4d, 0xaa, 0xba,
0xe3, 0xdf, 0x76, 0x60, 0x06, 0x09, 0x0d, 0x72,
0x8c, 0x0c, 0xd2, 0x70, 0x39, 0xed, 0x19, 0xd8,
0x1b, 0x64, 0x33, 0xce, 0x12, 0xa8, 0xb3, 0xa0,
0xf5, 0x5f, 0xcc, 0xf7, 0x1a, 0x63, 0x5d, 0x44,
0x61, 0xc6, 0xa0, 0x54, 0xe9, 0x01, 0xa6, 0x54,
0x16, 0xa7, 0xa0, 0x9e, 0xc9, 0xd8, 0x9c, 0x19,
0x04, 0xe1, 0x5e, 0xd5, 0x37, 0xbb, 0xfd, 0x76,
0x77, 0x75,
};
static const unsigned int rsa_example_keypair_der_len =
sizeof(rsa_example_keypair_der);
static const unsigned char rsa_example_cert_der[] = {
0x30, 0x82, 0x02, 0xfb, 0x30, 0x82, 0x01, 0xe3,
0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
0xee, 0x10, 0x1f, 0xc1, 0xf2, 0x30, 0xe9, 0x11,
0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61,
0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17,
0x0d, 0x31, 0x37, 0x30, 0x36, 0x32, 0x36, 0x31,
0x30, 0x35, 0x36, 0x31, 0x30, 0x5a, 0x17, 0x0d,
0x34, 0x34, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30,
0x35, 0x36, 0x31, 0x30, 0x5a, 0x30, 0x14, 0x31,
0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03,
0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68,
0x6f, 0x73, 0x74, 0x30, 0x82, 0x01, 0x22, 0x30,
0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82,
0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02,
0x82, 0x01, 0x01, 0x00, 0xc6, 0x00, 0x7b, 0x0d,
0xd1, 0x17, 0x43, 0x6b, 0xa9, 0xa7, 0x79, 0x9c,
0x0f, 0x8d, 0x77, 0x91, 0xd2, 0xf7, 0x47, 0x35,
0xb3, 0x17, 0xe2, 0xdd, 0xed, 0x6d, 0x01, 0xf9,
0xb1, 0x92, 0xc9, 0x48, 0x80, 0xe0, 0x1f, 0xcf,
0xb7, 0xa4, 0x5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0x54, 0xda, 0xf1, 0xdd,
0x48, 0xf9, 0xd7, 0xf0, 0x35, 0x7c, 0x8e, 0x24,
0x7e, 0x44, 0x2f, 0xf3, 0xbf, 0x39, 0x0e, 0x96,
0xab, 0xe1, 0x45, 0x03, 0x8b, 0x54, 0xdc, 0xe1,
0xb6, 0x11, 0x81, 0x21, 0x02, 0x03, 0x01, 0x00,
0x01, 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06,
0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
0xa4, 0xef, 0x6d, 0xdc, 0x9b, 0x23, 0xc5, 0x3a,
0xdd, 0x34, 0xd9, 0x01, 0x1c, 0x68, 0x03, 0x53,
0xae, 0x92, 0xc2, 0xc9, 0x30, 0x1f, 0x06, 0x03,
0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
0x14, 0xa4, 0xef, 0x6d, 0xdc, 0x9b, 0x23, 0xc5,
0x3a, 0xdd, 0x34, 0xd9, 0x01, 0x1c, 0x68, 0x03,
0x53, 0xae, 0x92, 0xc2, 0xc9, 0x30, 0x0c, 0x06,
0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03,
0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a,
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x67,
0x65, 0xbf, 0x93, 0x89, 0xde, 0x4f, 0x71, 0xff,
0x1c, 0x93, 0x68, 0xa0, 0x64, 0x09, 0x5e, 0x95,
0x94, 0xf5, 0xd5, 0xf4, 0x6b, 0x20, 0x32, 0xd8,
0x04, 0x80, 0xac, 0xf8, 0x52, 0x36, 0x7a, 0x38,
0x83, 0xae, 0xab, 0x29, 0x22, 0x42, 0x71, 0x7e,
0xea, 0xe5, 0x4f, 0x71, 0xac, 0x44, 0x3f, 0x9e,
0x5e, 0x49, 0x22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x0d, 0xf3, 0xba, 0x87,
0x46, 0xf0, 0x04, 0xfc, 0xc3, 0xbe, 0x84, 0x85,
0x3c, 0x01, 0xef, 0xe0, 0x68, 0x65, 0xee,
};
static const unsigned int rsa_example_cert_der_len =
sizeof(rsa_example_cert_der);
#endif

View file

@ -0,0 +1,145 @@
/* udp.c - UDP specific code for echo server */
/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#if 1
#define SYS_LOG_DOMAIN "echo-server"
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif
#include <zephyr.h>
#include <errno.h>
#include <stdio.h>
#include <net/net_pkt.h>
#include <net/net_core.h>
#include <net/net_context.h>
#include <net/net_app.h>
#include "common.h"
static struct net_app_ctx udp;
/* Note that both tcp and udp can share the same pool but in this
* example the UDP context and TCP context have separate pools.
*/
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
NET_PKT_TX_SLAB_DEFINE(echo_tx_udp, 5);
NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20);
static struct k_mem_slab *tx_udp_slab(void)
{
return &echo_tx_udp;
}
static struct net_buf_pool *data_udp_pool(void)
{
return &echo_data_udp;
}
#else
#define tx_udp_slab NULL
#define data_udp_pool NULL
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */
static inline void set_dst_addr(sa_family_t family,
struct net_pkt *pkt,
struct sockaddr *dst_addr)
{
#if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) {
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
&NET_IPV6_HDR(pkt)->src);
net_sin6(dst_addr)->sin6_family = AF_INET6;
net_sin6(dst_addr)->sin6_port = NET_UDP_HDR(pkt)->src_port;
}
#endif /* CONFIG_NET_IPV6) */
#if defined(CONFIG_NET_IPV4)
if (family == AF_INET) {
net_ipaddr_copy(&net_sin(dst_addr)->sin_addr,
&NET_IPV4_HDR(pkt)->src);
net_sin(dst_addr)->sin_family = AF_INET;
net_sin(dst_addr)->sin_port = NET_UDP_HDR(pkt)->src_port;
}
#endif /* CONFIG_NET_IPV6) */
}
static void udp_received(struct net_app_ctx *ctx,
struct net_pkt *pkt,
int status,
void *user_data)
{
static char dbg[MAX_DBG_PRINT + 1];
struct net_pkt *reply_pkt;
struct sockaddr dst_addr;
sa_family_t family = net_pkt_family(pkt);
socklen_t dst_len;
int ret;
snprintk(dbg, MAX_DBG_PRINT, "UDP IPv%c",
family == AF_INET6 ? '6' : '4');
if (family == AF_INET6) {
dst_len = sizeof(struct sockaddr_in6);
} else {
dst_len = sizeof(struct sockaddr_in);
}
set_dst_addr(family, pkt, &dst_addr);
reply_pkt = build_reply_pkt(dbg, ctx, pkt);
net_pkt_unref(pkt);
if (!reply_pkt) {
return;
}
ret = net_app_send_pkt(ctx, reply_pkt, &dst_addr, dst_len, K_NO_WAIT,
UINT_TO_POINTER(net_pkt_get_len(reply_pkt)));
if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_pkt);
}
}
void start_udp(void)
{
int ret;
ret = net_app_init_udp_server(&udp, NULL, MY_PORT, NULL);
if (ret < 0) {
NET_ERR("Cannot init UDP service at port %d", MY_PORT);
return;
}
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
net_app_set_net_pkt_pool(&udp, tx_udp_slab, data_udp_pool);
#endif
ret = net_app_set_cb(&udp, NULL, udp_received, pkt_sent, NULL);
if (ret < 0) {
NET_ERR("Cannot set callbacks (%d)", ret);
net_app_release(&udp);
return;
}
ret = net_app_listen(&udp);
if (ret < 0) {
NET_ERR("Cannot wait connection (%d)", ret);
net_app_release(&udp);
return;
}
}
void stop_udp(void)
{
net_app_close(&udp);
net_app_release(&udp);
}