sync the upstream branch
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2021 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_config_demo.c
|
||||
* @brief Demo for ping function
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.12.15
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
#include "sys_arch.h"
|
||||
#include <shell.h>
|
||||
#include <sys.h>
|
||||
#include <string.h>
|
||||
|
||||
/******************************************************************************/
|
||||
uint8_t enet_id = 0;
|
||||
static void LwipSetIPTask(void *param)
|
||||
{
|
||||
uint8_t enet_port = *(uint8_t *)param; ///< test enet port
|
||||
printf("lw: [%s] config netport id[%d]\n", __func__, enet_port);
|
||||
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
}
|
||||
|
||||
void LwipSetIPTest(int argc, char *argv[])
|
||||
{
|
||||
if(argc >= 4)
|
||||
{
|
||||
printf("lw: [%s] ip %s mask %s gw %s netport %s\n", __func__, argv[1], argv[2], argv[3], argv[4]);
|
||||
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], &lwip_netmask[2], &lwip_netmask[3]);
|
||||
sscanf(argv[3], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]);
|
||||
sscanf(argv[4], "%d", &enet_id);
|
||||
|
||||
if(0 == enet_id)
|
||||
{
|
||||
printf("save eth0 info\n");
|
||||
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, 20);
|
||||
memcpy(lwip_eth0_netmask, lwip_netmask, 20);
|
||||
memcpy(lwip_eth0_gwaddr, lwip_gwaddr, 20);
|
||||
}
|
||||
else if(1 == enet_id)
|
||||
{
|
||||
printf("save eth1 info\n");
|
||||
memcpy(lwip_eth1_ipaddr, lwip_ipaddr, 20);
|
||||
memcpy(lwip_eth1_netmask, lwip_netmask, 20);
|
||||
memcpy(lwip_eth1_gwaddr, lwip_gwaddr, 20);
|
||||
}
|
||||
}
|
||||
else if(argc == 2)
|
||||
{
|
||||
printf("lw: [%s] set eth0 ipaddr %s \n", __func__, argv[1]);
|
||||
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
|
||||
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, strlen(lwip_ipaddr));
|
||||
}
|
||||
sys_thread_new("SET ip address", LwipSetIPTask, &enet_id, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5),
|
||||
setip, LwipSetIPTest, setip [IP] [Netmask] [Gateway] [port]);
|
||||
|
||||
|
||||
void LwipShowIPTask(int argc, char *argv[])
|
||||
{
|
||||
char mac_addr0[] = configMAC_ADDR;
|
||||
|
||||
lw_notice("\r\n************************************************\r\n");
|
||||
lw_notice(" Network Configuration\r\n");
|
||||
lw_notice("************************************************\r\n");
|
||||
lw_notice(" ETH0 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth0_ipaddr)[0], ((u8_t *)&lwip_eth0_ipaddr)[1],
|
||||
((u8_t *)&lwip_eth0_ipaddr)[2], ((u8_t *)&lwip_eth0_ipaddr)[3]);
|
||||
lw_notice(" ETH0 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth0_netmask)[0], ((u8_t *)&lwip_eth0_netmask)[1],
|
||||
((u8_t *)&lwip_eth0_netmask)[2], ((u8_t *)&lwip_eth0_netmask)[3]);
|
||||
lw_notice(" ETH0 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_gwaddr)[0], ((u8_t *)&lwip_eth0_gwaddr)[1],
|
||||
((u8_t *)&lwip_eth0_gwaddr)[2], ((u8_t *)&lwip_eth0_gwaddr)[3]);
|
||||
lw_notice(" ETH0 MAC Address : %x:%x:%x:%x:%x:%x\r\n", mac_addr0[0], mac_addr0[1], mac_addr0[2],
|
||||
mac_addr0[3], mac_addr0[4], mac_addr0[5]);
|
||||
#ifdef BOARD_NET_COUNT
|
||||
if(BOARD_NET_COUNT > 1)
|
||||
{
|
||||
char mac_addr1[] = configMAC_ADDR_ETH1;
|
||||
lw_notice("\r\n");
|
||||
lw_notice(" ETH1 IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_ipaddr)[0], ((u8_t *)&lwip_eth1_ipaddr)[1],
|
||||
((u8_t *)&lwip_eth1_ipaddr)[2], ((u8_t *)&lwip_eth1_ipaddr)[3]);
|
||||
lw_notice(" ETH1 IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_netmask)[0], ((u8_t *)&lwip_eth1_netmask)[1],
|
||||
((u8_t *)&lwip_eth1_netmask)[2], ((u8_t *)&lwip_eth1_netmask)[3]);
|
||||
lw_notice(" ETH1 IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&lwip_eth1_gwaddr)[0], ((u8_t *)&lwip_eth1_gwaddr)[1],
|
||||
((u8_t *)&lwip_eth1_gwaddr)[2], ((u8_t *)&lwip_eth1_gwaddr)[3]);
|
||||
lw_notice(" ETH1 MAC Address : %x:%x:%x:%x:%x:%x\r\n", mac_addr1[0], mac_addr1[1], mac_addr1[2],
|
||||
mac_addr1[3], mac_addr1[4], mac_addr1[5]);
|
||||
}
|
||||
#endif
|
||||
lw_notice("************************************************\r\n");
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
showip, LwipShowIPTask, GetIp [IP] [Netmask] [Gateway]);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_demo.h
|
||||
* @brief lwip demo relative struct and definition
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-03-21
|
||||
*/
|
||||
|
||||
#ifndef __LWIP_DEMO_H__
|
||||
#define __LWIP_DEMO_H__
|
||||
|
||||
typedef struct LwipTcpSocketStruct
|
||||
{
|
||||
char ip[6];
|
||||
uint16_t port;
|
||||
char *buf;
|
||||
}LwipTcpSocketParamType;
|
||||
|
||||
#define LWIP_TEST_MSG_SIZE 128
|
||||
|
||||
#define LWIP_TEST_STACK_SIZE 4096
|
||||
#define LWIP_TEST_TASK_PRIO 20
|
||||
|
||||
#endif /* __LWIP_DEMO_H__ */
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016-2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_dhcp_demo.c
|
||||
* @brief Demo for DHCP function
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.12.15
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Includes
|
||||
******************************************************************************/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if LWIP_IPV4 && LWIP_DHCP
|
||||
|
||||
#include "lwip/timeouts.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/init.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "lwip/prot/dhcp.h"
|
||||
#include "netif/ethernet.h"
|
||||
|
||||
#include <shell.h>
|
||||
#include <assert.h>
|
||||
#include <sys_arch.h>
|
||||
#include "board.h"
|
||||
#include "connect_ethernet.h"
|
||||
|
||||
#define LWIP_DHCP_TIME 10000 // 10s
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Prints DHCP status of the interface when it has changed from last status.
|
||||
*
|
||||
* @param netif network interface structure
|
||||
*/
|
||||
int LwipPrintDHCP(struct netif *netif)
|
||||
{
|
||||
static u8_t dhcp_last_state = DHCP_STATE_OFF;
|
||||
struct dhcp *dhcp = netif_dhcp_data(netif);
|
||||
|
||||
if (dhcp == NULL)
|
||||
{
|
||||
dhcp_last_state = DHCP_STATE_OFF;
|
||||
}
|
||||
else if (dhcp_last_state != dhcp->state)
|
||||
{
|
||||
dhcp_last_state = dhcp->state;
|
||||
|
||||
lw_notice(" DHCP state : ");
|
||||
switch (dhcp_last_state)
|
||||
{
|
||||
case DHCP_STATE_OFF:
|
||||
lw_notice("OFF");
|
||||
break;
|
||||
case DHCP_STATE_REQUESTING:
|
||||
lw_notice("REQUESTING");
|
||||
break;
|
||||
case DHCP_STATE_INIT:
|
||||
lw_notice("INIT");
|
||||
break;
|
||||
case DHCP_STATE_REBOOTING:
|
||||
lw_notice("REBOOTING");
|
||||
break;
|
||||
case DHCP_STATE_REBINDING:
|
||||
lw_notice("REBINDING");
|
||||
break;
|
||||
case DHCP_STATE_RENEWING:
|
||||
lw_notice("RENEWING");
|
||||
break;
|
||||
case DHCP_STATE_SELECTING:
|
||||
lw_notice("SELECTING");
|
||||
break;
|
||||
case DHCP_STATE_INFORMING:
|
||||
lw_notice("INFORMING");
|
||||
break;
|
||||
case DHCP_STATE_CHECKING:
|
||||
lw_notice("CHECKING");
|
||||
break;
|
||||
case DHCP_STATE_BOUND:
|
||||
lw_notice("BOUND");
|
||||
break;
|
||||
case DHCP_STATE_BACKING_OFF:
|
||||
lw_notice("BACKING_OFF");
|
||||
break;
|
||||
default:
|
||||
lw_notice("%u", dhcp_last_state);
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
lw_notice("\r\n");
|
||||
|
||||
if (dhcp_last_state == DHCP_STATE_BOUND)
|
||||
{
|
||||
lw_notice("\r\n IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr));
|
||||
lw_notice(" IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask));
|
||||
lw_notice(" IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Main function.
|
||||
*/
|
||||
void LwipDHCPTest(void)
|
||||
{
|
||||
u32_t dhcp_time;
|
||||
static int flag = 0;
|
||||
uint8_t enet_port = 0; ///< test enet port 0
|
||||
char ip_addr[4] = {0, 0, 0, 0};
|
||||
|
||||
lwip_config_net(enet_port, ip_addr, ip_addr, ip_addr);
|
||||
set_lwip_bit(LWIP_PRINT_FLAG);
|
||||
|
||||
dhcp_start(&gnetif);
|
||||
|
||||
lw_print("\r\n************************************************\r\n");
|
||||
lw_print(" DHCP example\r\n");
|
||||
lw_print("************************************************\r\n");
|
||||
|
||||
dhcp_time = sys_now();
|
||||
|
||||
while ((sys_now() - dhcp_time) < LWIP_DHCP_TIME)
|
||||
{
|
||||
/* Poll the driver, get any outstanding frames */
|
||||
ethernetif_input(&gnetif);
|
||||
|
||||
/* Handle all system timeouts for all core protocols */
|
||||
sys_check_timeouts();
|
||||
|
||||
/* Print DHCP progress */
|
||||
if(LwipPrintDHCP(&gnetif))
|
||||
{
|
||||
sscanf(ipaddr_ntoa(&gnetif.ip_addr), "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1],
|
||||
&lwip_ipaddr[2], &lwip_ipaddr[3]);
|
||||
|
||||
sscanf(ipaddr_ntoa(&gnetif.netmask), "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1],
|
||||
&lwip_netmask[2], &lwip_netmask[3]);
|
||||
|
||||
sscanf(ipaddr_ntoa(&gnetif.gw), "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1],
|
||||
&lwip_gwaddr[2], &lwip_gwaddr[3]);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
clr_lwip_bit(LWIP_PRINT_FLAG);
|
||||
}
|
||||
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
getdynip, LwipDHCPTest, DHCP_Test);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2021 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_ping_demo.c
|
||||
* @brief Demo for ping function
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.12.15
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if LWIP_IPV4 && LWIP_RAW
|
||||
|
||||
#include "ping.h"
|
||||
|
||||
#include "lwip/timeouts.h"
|
||||
#include "lwip/init.h"
|
||||
#include "netif/ethernet.h"
|
||||
#include <shell.h>
|
||||
#include "board.h"
|
||||
|
||||
#include <sys_arch.h>
|
||||
#include "connect_ethernet.h"
|
||||
#include <string.h>
|
||||
|
||||
ip4_addr_t ping_addr;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void LwipPingTest(int argc, char *argv[])
|
||||
{
|
||||
int result = 0;
|
||||
uint8_t enet_port = 0; ///< test enet port 0
|
||||
|
||||
if(argc >= 4)
|
||||
{
|
||||
printf("lw: [%s] ip %s mask %s gw %s for netport %s\n", __func__, argv[1], argv[2], argv[3], argv[4]);
|
||||
sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]);
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], &lwip_netmask[2], &lwip_netmask[3]);
|
||||
sscanf(argv[3], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]);
|
||||
sscanf(argv[4], "%d", &enet_port);
|
||||
|
||||
if(0 == enet_port)
|
||||
{
|
||||
memcpy(lwip_eth0_ipaddr, lwip_ipaddr, strlen(lwip_ipaddr));
|
||||
memcpy(lwip_eth0_netmask, lwip_netmask, strlen(lwip_netmask));
|
||||
memcpy(lwip_eth0_gwaddr, lwip_gwaddr, strlen(lwip_gwaddr));
|
||||
}
|
||||
if(1 == enet_port)
|
||||
{
|
||||
memcpy(lwip_eth1_ipaddr, lwip_ipaddr, strlen(lwip_ipaddr));
|
||||
memcpy(lwip_eth1_netmask, lwip_netmask, strlen(lwip_netmask));
|
||||
memcpy(lwip_eth1_gwaddr, lwip_gwaddr, strlen(lwip_gwaddr));
|
||||
}
|
||||
}
|
||||
else if(argc == 3)
|
||||
{
|
||||
sscanf(argv[2], "%d", &enet_port);
|
||||
printf("lw: [%s] gw %s netport %d\n", __func__, argv[1], enet_port);
|
||||
if(isdigit(argv[1][0]))
|
||||
{
|
||||
if(sscanf(argv[1], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]) == EOF)
|
||||
{
|
||||
lw_notice("input wrong ip\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#if (LWIP_DHCP) && (PING_USE_SOCKETS)
|
||||
else
|
||||
{
|
||||
get_url_ip(argv[1]);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else if(argc == 2)
|
||||
{
|
||||
printf("lw: [%s] gw %s\n", __func__, argv[1]);
|
||||
if(isdigit(argv[1][0]))
|
||||
{
|
||||
if(sscanf(argv[1], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]) == EOF)
|
||||
{
|
||||
lw_notice("input wrong ip\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#if (LWIP_DHCP) && (PING_USE_SOCKETS)
|
||||
else
|
||||
{
|
||||
get_url_ip(argv[1]);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
printf("lw: [%s] argc %d\n", __func__, argc);
|
||||
|
||||
IP4_ADDR(&ping_addr, lwip_gwaddr[0], lwip_gwaddr[1], lwip_gwaddr[2], lwip_gwaddr[3]);
|
||||
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
ping_init(&ping_addr);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5),
|
||||
ping, LwipPingTest, ping [IP] 10 times);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_tcp_demo.c
|
||||
* @brief TCP demo based on LwIP
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-03-21
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
#include "lwip_demo.h"
|
||||
#include "sys_arch.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "tcpecho_raw.h"
|
||||
#include <shell.h>
|
||||
#include <sys.h>
|
||||
char tcp_demo_msg[LWIP_TEST_MSG_SIZE] = { 0 };
|
||||
char tcp_demo_ip[] = {192, 168, 250, 252};
|
||||
u16_t tcp_demo_port = LWIP_TARGET_PORT;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static void LwipTcpSendTask(void *arg)
|
||||
{
|
||||
int fd = -1;
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
lw_error("Socket error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
LwipTcpSocketParamType *param = (LwipTcpSocketParamType *)arg;
|
||||
|
||||
struct sockaddr_in tcp_sock;
|
||||
tcp_sock.sin_family = AF_INET;
|
||||
tcp_sock.sin_port = htons(param->port);
|
||||
tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(param->ip[0], param->ip[1], param->ip[2], param->ip[3]));
|
||||
memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_error("Unable to connect\n");
|
||||
closesocket(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
lw_notice("tcp connect success, start to send.\n");
|
||||
lw_notice("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
|
||||
|
||||
sendto(fd, tcp_demo_msg, strlen(tcp_demo_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr));
|
||||
|
||||
lw_notice("Send tcp msg: %s ", tcp_demo_msg);
|
||||
|
||||
closesocket(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
void LwipTcpSendTest(int argc, char *argv[])
|
||||
{
|
||||
LwipTcpSocketParamType param;
|
||||
uint8_t enet_port = 0;
|
||||
memset(tcp_demo_msg, 0, LWIP_TEST_MSG_SIZE);
|
||||
if(argc >= 2)
|
||||
{
|
||||
strncpy(tcp_demo_msg, argv[1], strlen(argv[1]));
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(tcp_demo_msg, "hello world", strlen("hello world"));
|
||||
}
|
||||
strcat(tcp_demo_msg, "\r\n");
|
||||
|
||||
if(argc >= 3)
|
||||
{
|
||||
if(sscanf(argv[2], "%d.%d.%d.%d:%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3], &tcp_demo_port) == EOK)
|
||||
{
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3]);
|
||||
}
|
||||
}
|
||||
lw_notice("get ipaddr %d.%d.%d.%d:%d\n", tcp_demo_ip[0], tcp_demo_ip[1], tcp_demo_ip[2], tcp_demo_ip[3], tcp_demo_port);
|
||||
lwip_config_tcp(enet_port, lwip_ipaddr, lwip_netmask, tcp_demo_ip);
|
||||
|
||||
memcpy(param.ip, tcp_demo_ip, 4);
|
||||
param.port = tcp_demo_port;
|
||||
param.buf = malloc(LWIP_TEST_MSG_SIZE);
|
||||
memcpy(param.buf, tcp_demo_msg, LWIP_TEST_MSG_SIZE);
|
||||
|
||||
sys_thread_new("tcp send", LwipTcpSendTask, ¶m, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
TCPSend, LwipTcpSendTest, TCP Send message);
|
||||
|
||||
void LwipTcpRecvTest(void)
|
||||
{
|
||||
uint8_t enet_port = 0; ///< test enet port 0
|
||||
|
||||
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
tcpecho_raw_init();
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
TCPRecv, LwipTcpRecvTest, TCP Recv message);
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lwip_udp_demo.c
|
||||
* @brief One UDP demo based on LwIP
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-03-21
|
||||
*/
|
||||
#include "board.h"
|
||||
#include "sys_arch.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include <shell.h>
|
||||
#include <sys.h>
|
||||
|
||||
|
||||
#define PBUF_SIZE 27
|
||||
|
||||
static struct udp_pcb *udpecho_raw_pcb;
|
||||
|
||||
char udp_demo_ip[] = {192, 168, 250, 252};
|
||||
u16_t udp_demo_port = LWIP_TARGET_PORT;
|
||||
|
||||
char hello_str[] = {"hello world\r\n"};
|
||||
char udp_demo_msg[] = "\nThis one is UDP package!!!\n";
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static void LwipUDPSendTask(void *arg)
|
||||
{
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
lw_print("udp_send_demo start.\n");
|
||||
|
||||
int socket_fd = -1;
|
||||
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (socket_fd < 0)
|
||||
{
|
||||
lw_error("Socket error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in udp_sock;
|
||||
udp_sock.sin_family = AF_INET;
|
||||
udp_sock.sin_port = htons(udp_demo_port);
|
||||
udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3]));
|
||||
memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero));
|
||||
|
||||
if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_error("Unable to connect\n");
|
||||
closesocket(socket_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
lw_notice("UDP connect success, start to send.\n");
|
||||
lw_notice("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
|
||||
|
||||
sendto(socket_fd, udp_demo_msg, strlen(udp_demo_msg), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr));
|
||||
lw_notice("Send UDP msg: %s ", udp_demo_msg);
|
||||
closesocket(socket_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
void *LwipUdpSendTest(int argc, char *argv[])
|
||||
{
|
||||
uint8_t enet_port = 0; ///< test enet port 0
|
||||
memset(udp_demo_msg, 0, sizeof(udp_demo_msg));
|
||||
|
||||
if(argc == 1)
|
||||
{
|
||||
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3]);
|
||||
strncpy(udp_demo_msg, hello_str, strlen(hello_str));
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(udp_demo_msg, argv[1], strlen(argv[1]));
|
||||
strncat(udp_demo_msg, "\r\n", 2);
|
||||
if(argc == 3)
|
||||
{
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &udp_demo_ip[0], &udp_demo_ip[1], &udp_demo_ip[2], &udp_demo_ip[3]);
|
||||
}
|
||||
}
|
||||
|
||||
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3]);
|
||||
|
||||
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, udp_demo_ip);
|
||||
sys_thread_new("udp send", LwipUDPSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
UDPSend, LwipUdpSendTest, UDP send echo);
|
||||
|
||||
static void LwipUdpRecvTask(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
const ip_addr_t *addr, u16_t port)
|
||||
{
|
||||
int udp_len;
|
||||
err_t err;
|
||||
struct pbuf* udp_buf;
|
||||
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
udp_len = p->tot_len;
|
||||
lw_notice("Receive data :%dB\r\n", udp_len);
|
||||
|
||||
if(udp_len <= 80)
|
||||
{
|
||||
lw_notice("%.*s\r\n", udp_len, (char *)(p->payload));
|
||||
}
|
||||
|
||||
udp_buf = pbuf_alloc(PBUF_TRANSPORT, PBUF_SIZE, PBUF_RAM);
|
||||
|
||||
memset(udp_buf->payload, 0, PBUF_SIZE);
|
||||
|
||||
err = pbuf_take(udp_buf, "Client receive success!\r\n", 27);
|
||||
|
||||
/* send received packet back to sender */
|
||||
udp_sendto(upcb, udp_buf, addr, port);
|
||||
|
||||
/* free the pbuf */
|
||||
pbuf_free(p);
|
||||
pbuf_free(udp_buf);
|
||||
}
|
||||
|
||||
void LwipUdpRecvTest(void)
|
||||
{
|
||||
err_t err;
|
||||
uint8_t enet_port = 0; ///< test enet port 0
|
||||
|
||||
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
|
||||
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (udpecho_raw_pcb == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LWIP_LOCAL_PORT);
|
||||
if (err == ERR_OK)
|
||||
{
|
||||
udp_recv(udpecho_raw_pcb, LwipUdpRecvTask, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
UDPRecv, LwipUdpRecvTest, UDP Receive echo);
|
||||
|
||||
+541
@@ -0,0 +1,541 @@
|
||||
/**
|
||||
* @file
|
||||
* Ping sender module
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is an example of a "ping" sender (with raw API and socket API).
|
||||
* It can be used as a start point to maintain opened a network connection, or
|
||||
* like a network "watchdog" for your device.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <xs_ktask.h>
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
|
||||
|
||||
#include "ping.h"
|
||||
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/raw.h"
|
||||
#include "lwip/icmp.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/timeouts.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/prot/ip4.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/prot/ip.h"
|
||||
|
||||
#if PING_USE_SOCKETS
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/inet.h"
|
||||
#include <string.h>
|
||||
#endif /* PING_USE_SOCKETS */
|
||||
|
||||
|
||||
/**
|
||||
* PING_DEBUG: Enable debugging for PING.
|
||||
*/
|
||||
#ifndef PING_DEBUG
|
||||
#define PING_DEBUG LWIP_DBG_ON
|
||||
#endif
|
||||
|
||||
/** ping receive timeout - in milliseconds */
|
||||
#ifndef PING_RCV_TIMEO
|
||||
#define PING_RCV_TIMEO 2000
|
||||
#endif
|
||||
|
||||
/** ping delay - in milliseconds */
|
||||
#ifndef PING_DELAY
|
||||
#define PING_DELAY 1000
|
||||
#endif
|
||||
|
||||
/** ping identifier - must fit on a u16_t */
|
||||
#ifndef PING_ID
|
||||
#define PING_ID 0xAFAF
|
||||
#endif
|
||||
|
||||
/** ping additional data size to include in the packet */
|
||||
#ifndef PING_DATA_SIZE
|
||||
#define PING_DATA_SIZE 32
|
||||
#endif
|
||||
|
||||
/** ping result action - no default action */
|
||||
#ifndef PING_RESULT
|
||||
#define PING_RESULT(ping_ok)
|
||||
#endif
|
||||
|
||||
/* ping variables */
|
||||
static const ip_addr_t* ping_target;
|
||||
static u16_t ping_seq_num;
|
||||
#ifdef LWIP_DEBUG
|
||||
static u32_t ping_time;
|
||||
#endif /* LWIP_DEBUG */
|
||||
#if !PING_USE_SOCKETS
|
||||
static struct raw_pcb *ping_pcb;
|
||||
#endif /* PING_USE_SOCKETS */
|
||||
|
||||
/** Prepare a echo ICMP request */
|
||||
static void
|
||||
ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t data_len = len - sizeof(struct icmp_echo_hdr);
|
||||
|
||||
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
|
||||
ICMPH_CODE_SET(iecho, 0);
|
||||
iecho->chksum = 0;
|
||||
iecho->id = PING_ID;
|
||||
iecho->seqno = lwip_htons(++ping_seq_num);
|
||||
|
||||
/* fill the additional data buffer with some data */
|
||||
for(i = 0; i < data_len; i++) {
|
||||
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
|
||||
}
|
||||
|
||||
iecho->chksum = inet_chksum(iecho, len);
|
||||
}
|
||||
|
||||
#if PING_USE_SOCKETS
|
||||
|
||||
/* Ping using the socket ip */
|
||||
static err_t
|
||||
ping_send(int s, const ip_addr_t *addr)
|
||||
{
|
||||
int err;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
struct sockaddr_storage to;
|
||||
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
||||
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) {
|
||||
/* todo: support ICMP6 echo */
|
||||
return ERR_VAL;
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
|
||||
if (!iecho) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
ping_prepare_echo(iecho, (u16_t)ping_size);
|
||||
|
||||
#if LWIP_IPV4
|
||||
if(IP_IS_V4(addr)) {
|
||||
struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
|
||||
to4->sin_len = sizeof(to4);
|
||||
to4->sin_family = AF_INET;
|
||||
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(IP_IS_V6(addr)) {
|
||||
struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
|
||||
to6->sin6_len = sizeof(to6);
|
||||
to6->sin6_family = AF_INET6;
|
||||
inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
|
||||
|
||||
mem_free(iecho);
|
||||
|
||||
return (err ? ERR_OK : ERR_VAL);
|
||||
}
|
||||
|
||||
static void
|
||||
ping_recv(int s)
|
||||
{
|
||||
char buf[64];
|
||||
int len;
|
||||
struct sockaddr_storage from;
|
||||
int fromlen = sizeof(from);
|
||||
|
||||
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
|
||||
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
|
||||
ip_addr_t fromaddr;
|
||||
memset(&fromaddr, 0, sizeof(fromaddr));
|
||||
|
||||
#if LWIP_IPV4
|
||||
if(from.ss_family == AF_INET) {
|
||||
struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
|
||||
inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
|
||||
IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4);
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(from.ss_family == AF_INET6) {
|
||||
struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
|
||||
inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
|
||||
IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6);
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
|
||||
ip_addr_debug_print_val(PING_DEBUG, fromaddr);
|
||||
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
|
||||
|
||||
/* todo: support ICMP6 echo */
|
||||
#if LWIP_IPV4
|
||||
if (IP_IS_V4_VAL(fromaddr)) {
|
||||
struct ip_hdr *iphdr;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
|
||||
iphdr = (struct ip_hdr *)buf;
|
||||
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
|
||||
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
|
||||
/* do some ping result processing */
|
||||
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
|
||||
return;
|
||||
} else {
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
|
||||
}
|
||||
}
|
||||
#endif /* LWIP_IPV4 */
|
||||
}
|
||||
fromlen = sizeof(from);
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
|
||||
}
|
||||
|
||||
/* do some ping result processing */
|
||||
PING_RESULT(0);
|
||||
}
|
||||
|
||||
static void
|
||||
ping_thread(void *arg)
|
||||
{
|
||||
int s;
|
||||
int ret;
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
||||
int timeout = PING_RCV_TIMEO;
|
||||
#else
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = PING_RCV_TIMEO/1000;
|
||||
timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
|
||||
#endif
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
|
||||
lw_print("lw: [%s] start...\n", __func__);
|
||||
|
||||
#if LWIP_IPV6
|
||||
if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) {
|
||||
s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
|
||||
} else {
|
||||
s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
|
||||
}
|
||||
#else
|
||||
s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
|
||||
#endif
|
||||
if (s < 0) {
|
||||
lw_print("lw: [%s] ping failed %d!\n", __func__, s);
|
||||
return;
|
||||
}
|
||||
|
||||
lw_print("lw: [%s] ping start!\n", __func__);
|
||||
|
||||
ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
LWIP_ASSERT("setting receive timeout failed", ret != 0);
|
||||
LWIP_UNUSED_ARG(ret);
|
||||
|
||||
while (cnt --) {
|
||||
if (ping_send(s, ping_target) == ERR_OK) {
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
||||
ip_addr_debug_print(PING_DEBUG, ping_target);
|
||||
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
||||
|
||||
#ifdef LWIP_DEBUG
|
||||
ping_time = sys_now();
|
||||
#endif /* LWIP_DEBUG */
|
||||
ping_recv(s);
|
||||
} else {
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
||||
ip_addr_debug_print(PING_DEBUG, ping_target);
|
||||
LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
|
||||
}
|
||||
sys_msleep(PING_DELAY);
|
||||
}
|
||||
lwip_close(s);
|
||||
}
|
||||
|
||||
#else /* PING_USE_SOCKETS */
|
||||
|
||||
/* Ping using the raw ip */
|
||||
static u8_t
|
||||
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
|
||||
{
|
||||
struct icmp_echo_hdr *iecho;
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
LWIP_UNUSED_ARG(pcb);
|
||||
LWIP_UNUSED_ARG(addr);
|
||||
LWIP_ASSERT("p != NULL", p != NULL);
|
||||
|
||||
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
|
||||
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
|
||||
iecho = (struct icmp_echo_hdr *)p->payload;
|
||||
|
||||
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
|
||||
ip_addr_debug_print(PING_DEBUG, addr);
|
||||
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
|
||||
|
||||
/* do some ping result processing */
|
||||
PING_RESULT(1);
|
||||
pbuf_free(p);
|
||||
return 1; /* eat the packet */
|
||||
}
|
||||
/* not eaten, restore original packet */
|
||||
/* Changed to the "_force" version because of LPC zerocopy pbufs */
|
||||
pbuf_add_header_force(p, PBUF_IP_HLEN);
|
||||
}
|
||||
|
||||
return 0; /* don't eat the packet */
|
||||
}
|
||||
|
||||
static void
|
||||
ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
|
||||
{
|
||||
struct pbuf *p;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
||||
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
||||
ip_addr_debug_print(PING_DEBUG, addr);
|
||||
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
||||
LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
|
||||
|
||||
p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
if ((p->len == p->tot_len) && (p->next == NULL)) {
|
||||
iecho = (struct icmp_echo_hdr *)p->payload;
|
||||
|
||||
ping_prepare_echo(iecho, (u16_t)ping_size);
|
||||
|
||||
raw_sendto(raw, p, addr);
|
||||
#ifdef LWIP_DEBUG
|
||||
ping_time = sys_now();
|
||||
#endif /* LWIP_DEBUG */
|
||||
}
|
||||
pbuf_free(p);
|
||||
}
|
||||
|
||||
static void
|
||||
ping_timeout(void *arg)
|
||||
{
|
||||
static int cnt = LWIP_DEMO_TIMES;
|
||||
struct raw_pcb *pcb = (struct raw_pcb*)arg;
|
||||
|
||||
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
|
||||
|
||||
ping_send(pcb, ping_target);
|
||||
|
||||
if(cnt -- > 0)
|
||||
{
|
||||
sys_timeout(PING_DELAY, ping_timeout, pcb);
|
||||
}
|
||||
else
|
||||
{
|
||||
cnt = LWIP_DEMO_TIMES;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ping_raw_init(void)
|
||||
{
|
||||
ping_pcb = raw_new(IP_PROTO_ICMP);
|
||||
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
|
||||
|
||||
raw_recv(ping_pcb, ping_recv, NULL);
|
||||
raw_bind(ping_pcb, IP_ADDR_ANY);
|
||||
sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
|
||||
}
|
||||
|
||||
void
|
||||
ping_send_now(void)
|
||||
{
|
||||
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
|
||||
ping_send(ping_pcb, ping_target);
|
||||
}
|
||||
|
||||
#endif /* PING_USE_SOCKETS */
|
||||
|
||||
void
|
||||
ping_init(const ip_addr_t* ping_addr)
|
||||
{
|
||||
sys_thread_t th;
|
||||
|
||||
ping_target = ping_addr;
|
||||
|
||||
#if PING_USE_SOCKETS
|
||||
th = sys_thread_new("ping_thread", ping_thread, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
lw_print("lw: [%s] new thread %d addr %#x\n", __func__, th, (*ping_addr).addr);
|
||||
#else /* PING_USE_SOCKETS */
|
||||
ping_raw_init();
|
||||
#endif /* PING_USE_SOCKETS */
|
||||
}
|
||||
|
||||
int lwip_ping_send(int s, ip_addr_t *addr, int size)
|
||||
{
|
||||
int err;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
struct sockaddr_in to;
|
||||
int ping_size = sizeof(struct icmp_echo_hdr) + size;
|
||||
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
|
||||
|
||||
iecho = malloc(ping_size);
|
||||
if (iecho == NULL)
|
||||
{
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
ping_prepare_echo(iecho, (u16_t) ping_size);
|
||||
|
||||
to.sin_len = sizeof(to);
|
||||
to.sin_family = AF_INET;
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
to.sin_addr.s_addr = addr->u_addr.ip4.addr;
|
||||
#elif LWIP_IPV4
|
||||
to.sin_addr.s_addr = addr->addr;
|
||||
#elif LWIP_IPV6
|
||||
#error Not supported IPv6.
|
||||
#endif
|
||||
|
||||
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
|
||||
free(iecho);
|
||||
|
||||
return (err == ping_size ? ERR_OK : ERR_VAL);
|
||||
}
|
||||
|
||||
int lwip_ping_recv(int s, int *ttl)
|
||||
{
|
||||
char buf[64];
|
||||
int fromlen = sizeof(struct sockaddr_in), len;
|
||||
struct sockaddr_in from;
|
||||
struct ip_hdr *iphdr;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
|
||||
while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
|
||||
{
|
||||
if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
|
||||
{
|
||||
iphdr = (struct ip_hdr *) buf;
|
||||
iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
|
||||
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
|
||||
{
|
||||
*ttl = iphdr->_ttl;
|
||||
return len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
#if (LWIP_DHCP) && (PING_USE_SOCKETS)
|
||||
int get_url_ip(char* url)
|
||||
{
|
||||
#if LWIP_VERSION_MAJOR >= 2U
|
||||
struct timeval timeout = { PING_RCV_TIMEO / TICK_PER_SECOND, PING_RCV_TIMEO % TICK_PER_SECOND };
|
||||
#else
|
||||
int timeout = PING_RCV_TIMEO * 1000UL / TICK_PER_SECOND;
|
||||
#endif
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
int s, ttl, recv_len;
|
||||
ip_addr_t target_addr;
|
||||
struct addrinfo hint, *res = NULL;
|
||||
struct sockaddr_in *h = NULL;
|
||||
struct in_addr ina;
|
||||
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
/* convert URL to IP */
|
||||
if (lwip_getaddrinfo(url, NULL, &hint, &res) != 0)
|
||||
{
|
||||
lw_notice("ping: unknown host %s\n", url);
|
||||
return -1;
|
||||
}
|
||||
memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
|
||||
memcpy(&ina, &h->sin_addr, sizeof(ina));
|
||||
lwip_freeaddrinfo(res);
|
||||
if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
|
||||
{
|
||||
lw_notice("ping: unknown host %s\n", url);
|
||||
return -2;
|
||||
}
|
||||
/* new a socket */
|
||||
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
|
||||
{
|
||||
lw_notice("ping: create socket failed\n");
|
||||
return -3;
|
||||
}
|
||||
|
||||
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
|
||||
while (cnt --)
|
||||
{
|
||||
if (ping_send(s, &target_addr) == ERR_OK)
|
||||
{
|
||||
#ifdef LWIP_DEBUG
|
||||
ping_time = sys_now();
|
||||
#endif /* LWIP_DEBUG */
|
||||
if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
|
||||
{
|
||||
lw_notice("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), cnt,
|
||||
ttl, sys_now() - ping_time);
|
||||
}
|
||||
else
|
||||
{
|
||||
lw_notice("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), cnt);
|
||||
}
|
||||
}
|
||||
|
||||
sys_msleep(PING_DELAY);
|
||||
}
|
||||
|
||||
lwip_close(s);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LWIP_RAW */
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#ifndef LWIP_PING_H
|
||||
#define LWIP_PING_H
|
||||
|
||||
#include "lwip/ip_addr.h"
|
||||
|
||||
/**
|
||||
* PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used
|
||||
*/
|
||||
#ifndef PING_USE_SOCKETS
|
||||
#define PING_USE_SOCKETS LWIP_SOCKET
|
||||
#endif
|
||||
|
||||
void ping_init(const ip_addr_t* ping_addr);
|
||||
|
||||
#if !PING_USE_SOCKETS
|
||||
void ping_send_now(void);
|
||||
#endif /* !PING_USE_SOCKETS */
|
||||
|
||||
int get_url_ip(char* url);
|
||||
|
||||
#endif /* LWIP_PING_H */
|
||||
@@ -0,0 +1,334 @@
|
||||
/*
|
||||
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of and a contribution to the lwIP TCP/IP stack.
|
||||
*
|
||||
* Credits go to Adam Dunkels (and the current maintainers) of this software.
|
||||
*
|
||||
* Christiaan Simons rewrote this file to get a more stable echo example.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* TCP echo server example using raw API.
|
||||
*
|
||||
* Echos all bytes sent by connecting client,
|
||||
* and passively closes when client is done.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/debug.h"
|
||||
#include "lwip/stats.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "tcpecho_raw.h"
|
||||
#include <string.h>
|
||||
|
||||
#if LWIP_TCP && LWIP_CALLBACK_API
|
||||
|
||||
#define MAX_TCP_RECV_SIZE (56000)
|
||||
#define MAX_TCP_SHOW_SIZE 80
|
||||
#define TCP_ACK_MSG_SIZE 20
|
||||
#define TCP_EOF_CH '\n'
|
||||
|
||||
static struct tcp_pcb *tcpecho_raw_pcb;
|
||||
|
||||
enum tcpecho_raw_states
|
||||
{
|
||||
ES_NONE = 0,
|
||||
ES_ACCEPTED,
|
||||
ES_RECEIVED,
|
||||
ES_CLOSING
|
||||
};
|
||||
|
||||
struct tcpecho_raw_state
|
||||
{
|
||||
u8_t state;
|
||||
u8_t retries;
|
||||
struct tcp_pcb *pcb;
|
||||
/* pbuf (chain) to recycle */
|
||||
struct pbuf *p;
|
||||
};
|
||||
|
||||
static void
|
||||
tcpecho_raw_free(struct tcpecho_raw_state *es)
|
||||
{
|
||||
if (es != NULL) {
|
||||
if (es->p) {
|
||||
/* free the buffer chain if present */
|
||||
pbuf_free(es->p);
|
||||
}
|
||||
|
||||
mem_free(es);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
|
||||
{
|
||||
tcp_arg(tpcb, NULL);
|
||||
tcp_sent(tpcb, NULL);
|
||||
tcp_recv(tpcb, NULL);
|
||||
tcp_err(tpcb, NULL);
|
||||
tcp_poll(tpcb, NULL, 0);
|
||||
|
||||
tcpecho_raw_free(es);
|
||||
|
||||
tcp_close(tpcb);
|
||||
}
|
||||
|
||||
static void
|
||||
tcpecho_raw_error(void *arg, err_t err)
|
||||
{
|
||||
struct tcpecho_raw_state *es;
|
||||
|
||||
LWIP_UNUSED_ARG(err);
|
||||
|
||||
es = (struct tcpecho_raw_state *)arg;
|
||||
|
||||
tcpecho_raw_free(es);
|
||||
}
|
||||
|
||||
static void
|
||||
tcpecho_raw_ack_size(struct tcp_pcb *tpcb, int ack_len)
|
||||
{
|
||||
struct pbuf *ack_buf = NULL;
|
||||
|
||||
// ack message
|
||||
ack_buf = pbuf_alloc(PBUF_TRANSPORT, TCP_ACK_MSG_SIZE, PBUF_RAM);
|
||||
snprintf(ack_buf->payload, TCP_ACK_MSG_SIZE, "%d\n\0", ack_len);
|
||||
ack_buf->len = strlen(ack_buf->payload);
|
||||
ack_buf->tot_len = strlen(ack_buf->payload);
|
||||
ack_buf->next = NULL;
|
||||
|
||||
tcp_write(tpcb, ack_buf->payload, ack_buf->len, 1);
|
||||
pbuf_free(ack_buf);
|
||||
}
|
||||
|
||||
// compute received message length until '\n'
|
||||
static void
|
||||
tcpecho_raw_ack(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
||||
struct pbuf *ptr = es->p;
|
||||
char *recv_buf = ptr->payload;
|
||||
int recv_len = ptr->len;
|
||||
static char *g_buf = NULL; //global received buffer
|
||||
static int g_buf_size = 0;
|
||||
|
||||
lw_print("lw: [%s] recv %d tot %d next %p ref %d tye %d id %d %s\n", __func__, ptr->len, ptr->tot_len,
|
||||
ptr->next, ptr->ref, ptr->type_internal,
|
||||
ptr->if_idx, ptr->payload);
|
||||
|
||||
if(g_buf == NULL)
|
||||
{
|
||||
g_buf = (char *)malloc(MAX_TCP_RECV_SIZE);
|
||||
memset(g_buf, 0, MAX_TCP_RECV_SIZE);
|
||||
memcpy(g_buf, recv_buf, recv_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(g_buf_size + recv_len <= MAX_TCP_RECV_SIZE)
|
||||
{
|
||||
memcpy(g_buf + g_buf_size, recv_buf, recv_len);
|
||||
}
|
||||
}
|
||||
g_buf_size += recv_len;
|
||||
|
||||
if((recv_len != TCP_MSS) || (recv_buf[recv_len - 1] == TCP_EOF_CH))
|
||||
{
|
||||
if(g_buf_size < MAX_TCP_SHOW_SIZE){
|
||||
lw_notice("Received: %s\n", g_buf);
|
||||
}else{
|
||||
lw_notice("Received a string of length %d\n", g_buf_size);
|
||||
}
|
||||
|
||||
tcpecho_raw_ack_size(tpcb, g_buf_size);
|
||||
|
||||
free(g_buf);
|
||||
g_buf = NULL;
|
||||
g_buf_size = 0;
|
||||
}
|
||||
|
||||
es->p = ptr->next;
|
||||
if(es->p != NULL) {
|
||||
/* new reference! */
|
||||
pbuf_ref(es->p);
|
||||
}
|
||||
pbuf_free(ptr);
|
||||
tcp_recved(tpcb, recv_len);
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
{
|
||||
err_t ret_err;
|
||||
struct tcpecho_raw_state *es;
|
||||
|
||||
es = (struct tcpecho_raw_state *)arg;
|
||||
if (es != NULL) {
|
||||
if (es->p != NULL) {
|
||||
/* there is a remaining pbuf (chain) */
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
} else {
|
||||
/* no remaining pbuf (chain) */
|
||||
if(es->state == ES_CLOSING) {
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
}
|
||||
}
|
||||
ret_err = ERR_OK;
|
||||
} else {
|
||||
/* nothing to be done */
|
||||
tcp_abort(tpcb);
|
||||
ret_err = ERR_ABRT;
|
||||
}
|
||||
return ret_err;
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
||||
{
|
||||
struct tcpecho_raw_state *es;
|
||||
|
||||
LWIP_UNUSED_ARG(len);
|
||||
|
||||
es = (struct tcpecho_raw_state *)arg;
|
||||
es->retries = 0;
|
||||
es->p = NULL;
|
||||
// the sent reply from server never have successors
|
||||
if(es->state == ES_CLOSING) {
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
{
|
||||
struct tcpecho_raw_state *es;
|
||||
err_t ret_err;
|
||||
|
||||
LWIP_ASSERT("arg != NULL",arg != NULL);
|
||||
es = (struct tcpecho_raw_state *)arg;
|
||||
if (p == NULL) {
|
||||
/* remote host closed connection */
|
||||
es->state = ES_CLOSING;
|
||||
if(es->p == NULL) {
|
||||
/* we're done sending, close it */
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
} else {
|
||||
/* we're not done yet */
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
}
|
||||
ret_err = ERR_OK;
|
||||
} else if(err != ERR_OK) {
|
||||
/* cleanup, for unknown reason */
|
||||
if (p != NULL) {
|
||||
pbuf_free(p);
|
||||
}
|
||||
ret_err = err;
|
||||
}
|
||||
else if(es->state == ES_ACCEPTED) {
|
||||
/* first data chunk in p->payload */
|
||||
es->state = ES_RECEIVED;
|
||||
/* store reference to incoming pbuf (chain) */
|
||||
es->p = p;
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
ret_err = ERR_OK;
|
||||
} else if (es->state == ES_RECEIVED) {
|
||||
/* read some more data */
|
||||
if(es->p == NULL) {
|
||||
es->p = p;
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
} else {
|
||||
struct pbuf *ptr;
|
||||
|
||||
/* chain pbufs to the end of what we recv'ed previously */
|
||||
ptr = es->p;
|
||||
pbuf_cat(ptr,p);
|
||||
}
|
||||
ret_err = ERR_OK;
|
||||
} else {
|
||||
/* unkown es->state, trash data */
|
||||
tcp_recved(tpcb, p->tot_len);
|
||||
pbuf_free(p);
|
||||
ret_err = ERR_OK;
|
||||
}
|
||||
return ret_err;
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
||||
{
|
||||
err_t ret_err;
|
||||
struct tcpecho_raw_state *es;
|
||||
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
if ((err != ERR_OK) || (newpcb == NULL)) {
|
||||
return ERR_VAL;
|
||||
}
|
||||
|
||||
/* Unless this pcb should have NORMAL priority, set its priority now.
|
||||
When running out of pcbs, low priority pcbs can be aborted to create
|
||||
new pcbs of higher priority. */
|
||||
tcp_setprio(newpcb, TCP_PRIO_MIN);
|
||||
|
||||
es = (struct tcpecho_raw_state *)mem_malloc(sizeof(struct tcpecho_raw_state));
|
||||
if (es != NULL) {
|
||||
es->state = ES_ACCEPTED;
|
||||
es->pcb = newpcb;
|
||||
es->retries = 0;
|
||||
es->p = NULL;
|
||||
/* pass newly allocated es to our callbacks */
|
||||
tcp_arg(newpcb, es);
|
||||
tcp_recv(newpcb, tcpecho_raw_recv);
|
||||
tcp_err(newpcb, tcpecho_raw_error);
|
||||
tcp_poll(newpcb, tcpecho_raw_poll, 0);
|
||||
tcp_sent(newpcb, tcpecho_raw_sent);
|
||||
ret_err = ERR_OK;
|
||||
} else {
|
||||
ret_err = ERR_MEM;
|
||||
}
|
||||
return ret_err;
|
||||
}
|
||||
|
||||
void
|
||||
tcpecho_raw_init(void)
|
||||
{
|
||||
tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (tcpecho_raw_pcb != NULL) {
|
||||
err_t err;
|
||||
err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, LWIP_TEST_TCP_PORT);
|
||||
if (err == ERR_OK) {
|
||||
tcpecho_raw_pcb = tcp_listen(tcpecho_raw_pcb);
|
||||
tcp_accept(tcpecho_raw_pcb, tcpecho_raw_accept);
|
||||
} else {
|
||||
/* abort? output diagnostic? */
|
||||
}
|
||||
} else {
|
||||
/* abort? output diagnostic? */
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* LWIP_TCP && LWIP_CALLBACK_API */
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
*/
|
||||
#ifndef LWIP_TCPECHO_RAW_H
|
||||
#define LWIP_TCPECHO_RAW_H
|
||||
|
||||
#define LWIP_TEST_TCP_PORT 4840
|
||||
|
||||
void tcpecho_raw_init(void);
|
||||
|
||||
#endif /* LWIP_TCPECHO_RAW_H */
|
||||
Reference in New Issue
Block a user