Browse Source
2. changed lwip test command 3. merge dns and ping command 4. change test to cmd_lwippull/125/head
19 changed files with 556 additions and 546 deletions
@ -1,3 +1,3 @@ |
|||
SRC_DIR += LwIP test |
|||
SRC_DIR += LwIP cmd_lwip |
|||
|
|||
include $(KERNEL_ROOT)/compiler.mk |
|||
|
@ -1,172 +1,177 @@ |
|||
/*
|
|||
* Copyright (c) 2016, Freescale Semiconductor, Inc. |
|||
* Copyright 2016-2019 NXP |
|||
* All rights reserved. |
|||
* |
|||
* |
|||
* SPDX-License-Identifier: BSD-3-Clause |
|||
*/ |
|||
|
|||
/*******************************************************************************
|
|||
* 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 "enet_ethernetif.h" |
|||
|
|||
#include "board.h" |
|||
|
|||
#include "pin_mux.h" |
|||
#include "clock_config.h" |
|||
#include "fsl_gpio.h" |
|||
#include "fsl_iomuxc.h" |
|||
#include "sys_arch.h" |
|||
|
|||
/*******************************************************************************
|
|||
* Definitions |
|||
******************************************************************************/ |
|||
|
|||
/*******************************************************************************
|
|||
* Prototypes |
|||
******************************************************************************/ |
|||
|
|||
/*******************************************************************************
|
|||
* Variables |
|||
******************************************************************************/ |
|||
|
|||
/*******************************************************************************
|
|||
* Code |
|||
******************************************************************************/ |
|||
|
|||
/*!
|
|||
* @brief Prints DHCP status of the interface when it has changed from last status. |
|||
* |
|||
* @param netif network interface structure |
|||
*/ |
|||
int print_dhcp_state(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_pr_info(" DHCP state : "); |
|||
switch (dhcp_last_state) |
|||
{ |
|||
case DHCP_STATE_OFF: |
|||
lw_pr_info("OFF"); |
|||
break; |
|||
case DHCP_STATE_REQUESTING: |
|||
lw_pr_info("REQUESTING"); |
|||
break; |
|||
case DHCP_STATE_INIT: |
|||
lw_pr_info("INIT"); |
|||
break; |
|||
case DHCP_STATE_REBOOTING: |
|||
lw_pr_info("REBOOTING"); |
|||
break; |
|||
case DHCP_STATE_REBINDING: |
|||
lw_pr_info("REBINDING"); |
|||
break; |
|||
case DHCP_STATE_RENEWING: |
|||
lw_pr_info("RENEWING"); |
|||
break; |
|||
case DHCP_STATE_SELECTING: |
|||
lw_pr_info("SELECTING"); |
|||
break; |
|||
case DHCP_STATE_INFORMING: |
|||
lw_pr_info("INFORMING"); |
|||
break; |
|||
case DHCP_STATE_CHECKING: |
|||
lw_pr_info("CHECKING"); |
|||
break; |
|||
case DHCP_STATE_BOUND: |
|||
lw_pr_info("BOUND"); |
|||
break; |
|||
case DHCP_STATE_BACKING_OFF: |
|||
lw_pr_info("BACKING_OFF"); |
|||
break; |
|||
default: |
|||
lw_pr_info("%u", dhcp_last_state); |
|||
assert(0); |
|||
break; |
|||
} |
|||
lw_pr_info("\r\n"); |
|||
|
|||
if (dhcp_last_state == DHCP_STATE_BOUND) |
|||
{ |
|||
lw_pr_info("\r\n IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr)); |
|||
lw_pr_info(" IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask)); |
|||
lw_pr_info(" IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw)); |
|||
return 1; |
|||
} |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
/*!
|
|||
* @brief Main function. |
|||
*/ |
|||
void lwip_dhcp_test(void) |
|||
{ |
|||
static int flag = 0; |
|||
char ip_addr[4] = {0, 0, 0, 0}; |
|||
|
|||
ETH_BSP_Config(); |
|||
|
|||
lwip_config_net(ip_addr, ip_addr, ip_addr); |
|||
is_lwip_test = 1; |
|||
|
|||
dhcp_start(&gnetif); |
|||
|
|||
lw_pr_info("\r\n************************************************\r\n"); |
|||
lw_pr_info(" DHCP example\r\n"); |
|||
lw_pr_info("************************************************\r\n"); |
|||
|
|||
while (1) |
|||
{ |
|||
/* 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(print_dhcp_state(&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; |
|||
} |
|||
} |
|||
|
|||
is_lwip_test = 0; |
|||
} |
|||
|
|||
|
|||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), |
|||
getip, lwip_dhcp_test, DHCP_Test); |
|||
|
|||
#endif |
|||
/*
|
|||
* Copyright (c) 2016, Freescale Semiconductor, Inc. |
|||
* Copyright 2016-2019 NXP |
|||
* All rights reserved. |
|||
* |
|||
* |
|||
* SPDX-License-Identifier: BSD-3-Clause |
|||
*/ |
|||
|
|||
/*******************************************************************************
|
|||
* 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 "enet_ethernetif.h" |
|||
|
|||
#include "board.h" |
|||
|
|||
#include "pin_mux.h" |
|||
#include "clock_config.h" |
|||
#include "fsl_gpio.h" |
|||
#include "fsl_iomuxc.h" |
|||
#include "sys_arch.h" |
|||
|
|||
/*******************************************************************************
|
|||
* Definitions |
|||
******************************************************************************/ |
|||
|
|||
#define LWIP_DHCP_TIME 10000 // 10s
|
|||
|
|||
/*******************************************************************************
|
|||
* Prototypes |
|||
******************************************************************************/ |
|||
|
|||
/*******************************************************************************
|
|||
* Variables |
|||
******************************************************************************/ |
|||
|
|||
/*******************************************************************************
|
|||
* Code |
|||
******************************************************************************/ |
|||
|
|||
/*!
|
|||
* @brief Prints DHCP status of the interface when it has changed from last status. |
|||
* |
|||
* @param netif network interface structure |
|||
*/ |
|||
int print_dhcp_state(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_pr_info(" DHCP state : "); |
|||
switch (dhcp_last_state) |
|||
{ |
|||
case DHCP_STATE_OFF: |
|||
lw_pr_info("OFF"); |
|||
break; |
|||
case DHCP_STATE_REQUESTING: |
|||
lw_pr_info("REQUESTING"); |
|||
break; |
|||
case DHCP_STATE_INIT: |
|||
lw_pr_info("INIT"); |
|||
break; |
|||
case DHCP_STATE_REBOOTING: |
|||
lw_pr_info("REBOOTING"); |
|||
break; |
|||
case DHCP_STATE_REBINDING: |
|||
lw_pr_info("REBINDING"); |
|||
break; |
|||
case DHCP_STATE_RENEWING: |
|||
lw_pr_info("RENEWING"); |
|||
break; |
|||
case DHCP_STATE_SELECTING: |
|||
lw_pr_info("SELECTING"); |
|||
break; |
|||
case DHCP_STATE_INFORMING: |
|||
lw_pr_info("INFORMING"); |
|||
break; |
|||
case DHCP_STATE_CHECKING: |
|||
lw_pr_info("CHECKING"); |
|||
break; |
|||
case DHCP_STATE_BOUND: |
|||
lw_pr_info("BOUND"); |
|||
break; |
|||
case DHCP_STATE_BACKING_OFF: |
|||
lw_pr_info("BACKING_OFF"); |
|||
break; |
|||
default: |
|||
lw_pr_info("%u", dhcp_last_state); |
|||
assert(0); |
|||
break; |
|||
} |
|||
lw_pr_info("\r\n"); |
|||
|
|||
if (dhcp_last_state == DHCP_STATE_BOUND) |
|||
{ |
|||
lw_pr_info("\r\n IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr)); |
|||
lw_pr_info(" IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask)); |
|||
lw_pr_info(" IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw)); |
|||
return 1; |
|||
} |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
/*!
|
|||
* @brief Main function. |
|||
*/ |
|||
void lwip_dhcp_test(void) |
|||
{ |
|||
u32_t dhcp_time; |
|||
static int flag = 0; |
|||
char ip_addr[4] = {0, 0, 0, 0}; |
|||
|
|||
ETH_BSP_Config(); |
|||
|
|||
lwip_config_net(ip_addr, ip_addr, ip_addr); |
|||
is_lwip_test = 1; |
|||
|
|||
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(print_dhcp_state(&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; |
|||
} |
|||
} |
|||
|
|||
is_lwip_test = 0; |
|||
} |
|||
|
|||
|
|||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), |
|||
getdynip, lwip_dhcp_test, DHCP_Test); |
|||
|
|||
#endif |
@ -1,302 +1,302 @@ |
|||
/*
|
|||
* 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" |
|||
|
|||
#if LWIP_TCP && LWIP_CALLBACK_API |
|||
|
|||
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_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es) |
|||
{ |
|||
struct pbuf *ptr; |
|||
err_t wr_err = ERR_OK; |
|||
|
|||
while ((wr_err == ERR_OK) && |
|||
(es->p != NULL) && |
|||
(es->p->len <= tcp_sndbuf(tpcb))) { |
|||
ptr = es->p; |
|||
|
|||
/* enqueue data for transmission */ |
|||
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1); |
|||
if (wr_err == ERR_OK) { |
|||
u16_t plen; |
|||
|
|||
plen = ptr->len; |
|||
/* continue with next pbuf in chain (if any) */ |
|||
es->p = ptr->next; |
|||
if(es->p != NULL) { |
|||
/* new reference! */ |
|||
pbuf_ref(es->p); |
|||
} |
|||
/* chop first pbuf from chain */ |
|||
pbuf_free(ptr); |
|||
/* we can read more data now */ |
|||
tcp_recved(tpcb, plen); |
|||
} else if(wr_err == ERR_MEM) { |
|||
/* we are low on memory, try later / harder, defer to poll */ |
|||
es->p = ptr; |
|||
} else { |
|||
/* other problem ?? */ |
|||
} |
|||
} |
|||
} |
|||
|
|||
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 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_send(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; |
|||
|
|||
if(es->p != NULL) { |
|||
/* still got pbufs to send */ |
|||
tcp_sent(tpcb, tcpecho_raw_sent); |
|||
tcpecho_raw_send(tpcb, es); |
|||
} else { |
|||
/* no more pbufs to send */ |
|||
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_send(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_send(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_send(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 */ |
|||
/*
|
|||
* 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" |
|||
|
|||
#if LWIP_TCP && LWIP_CALLBACK_API |
|||
|
|||
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_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es) |
|||
{ |
|||
struct pbuf *ptr; |
|||
err_t wr_err = ERR_OK; |
|||
|
|||
while ((wr_err == ERR_OK) && |
|||
(es->p != NULL) && |
|||
(es->p->len <= tcp_sndbuf(tpcb))) { |
|||
ptr = es->p; |
|||
|
|||
/* enqueue data for transmission */ |
|||
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1); |
|||
if (wr_err == ERR_OK) { |
|||
u16_t plen; |
|||
|
|||
plen = ptr->len; |
|||
/* continue with next pbuf in chain (if any) */ |
|||
es->p = ptr->next; |
|||
if(es->p != NULL) { |
|||
/* new reference! */ |
|||
pbuf_ref(es->p); |
|||
} |
|||
/* chop first pbuf from chain */ |
|||
pbuf_free(ptr); |
|||
/* we can read more data now */ |
|||
tcp_recved(tpcb, plen); |
|||
} else if(wr_err == ERR_MEM) { |
|||
/* we are low on memory, try later / harder, defer to poll */ |
|||
es->p = ptr; |
|||
} else { |
|||
/* other problem ?? */ |
|||
} |
|||
} |
|||
} |
|||
|
|||
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 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_send(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; |
|||
|
|||
if(es->p != NULL) { |
|||
/* still got pbufs to send */ |
|||
tcp_sent(tpcb, tcpecho_raw_sent); |
|||
tcpecho_raw_send(tpcb, es); |
|||
} else { |
|||
/* no more pbufs to send */ |
|||
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_send(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_send(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_send(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 */ |
@ -1,37 +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 */ |
|||
/*
|
|||
* 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 */ |
Loading…
Reference in new issue