forked from xuos/xiuos
fixed the bug that updating ip address and TCP recv fault
This commit is contained in:
parent
a49eeaf548
commit
b6bec0c2b6
|
@ -48,7 +48,7 @@ static void tcp_recv_demo(void *arg)
|
||||||
{
|
{
|
||||||
lw_print("tcp_recv_demo start.\n");
|
lw_print("tcp_recv_demo start.\n");
|
||||||
|
|
||||||
int socket_fd = -1;
|
int fd = -1;
|
||||||
char *recv_buf;
|
char *recv_buf;
|
||||||
struct sockaddr_in tcp_addr, server_addr;
|
struct sockaddr_in tcp_addr, server_addr;
|
||||||
int recv_len;
|
int recv_len;
|
||||||
|
@ -63,8 +63,8 @@ static void tcp_recv_demo(void *arg)
|
||||||
goto __exit;
|
goto __exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (socket_fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
lw_print("Socket error\n");
|
lw_print("Socket error\n");
|
||||||
goto __exit;
|
goto __exit;
|
||||||
|
@ -75,7 +75,7 @@ static void tcp_recv_demo(void *arg)
|
||||||
tcp_addr.sin_port = htons(LOCAL_PORT_SERVER);
|
tcp_addr.sin_port = htons(LOCAL_PORT_SERVER);
|
||||||
memset(&(tcp_addr.sin_zero), 0, sizeof(tcp_addr.sin_zero));
|
memset(&(tcp_addr.sin_zero), 0, sizeof(tcp_addr.sin_zero));
|
||||||
|
|
||||||
if (bind(socket_fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1)
|
if (bind(fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1)
|
||||||
{
|
{
|
||||||
lw_print("Unable to bind\n");
|
lw_print("Unable to bind\n");
|
||||||
goto __exit;
|
goto __exit;
|
||||||
|
@ -87,15 +87,15 @@ static void tcp_recv_demo(void *arg)
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
memset(recv_buf, 0, TCP_BUF_SIZE);
|
memset(recv_buf, 0, TCP_BUF_SIZE);
|
||||||
recv_len = recvfrom(socket_fd, recv_buf, TCP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
|
recv_len = recvfrom(fd, recv_buf, TCP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
|
||||||
lw_print("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
|
lw_pr_info("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
|
||||||
lw_print("Receive data : %s\n\n", recv_buf);
|
lw_pr_info("Receive data : %s\n\n", recv_buf);
|
||||||
sendto(socket_fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
|
sendto(fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
__exit:
|
__exit:
|
||||||
if (socket_fd >= 0)
|
if (fd >= 0)
|
||||||
closesocket(socket_fd);
|
closesocket(fd);
|
||||||
|
|
||||||
if (recv_buf)
|
if (recv_buf)
|
||||||
free(recv_buf);
|
free(recv_buf);
|
||||||
|
@ -116,11 +116,11 @@ void tcp_socket_recv_run(int argc, char *argv[])
|
||||||
|
|
||||||
ETH_BSP_Config();
|
ETH_BSP_Config();
|
||||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||||
sys_thread_new("tcp_recv_demo", tcp_recv_demo, NULL, 4096, 25);
|
sys_thread_new("tcp_recv_demo", tcp_recv_demo, NULL, 4096, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||||
TcpSocketRecv, tcp_socket_recv_run, TCP recv echo);
|
TCPSocketRecv, tcp_socket_recv_run, TCP recv echo);
|
||||||
|
|
||||||
static void tcp_send_demo(void *arg)
|
static void tcp_send_demo(void *arg)
|
||||||
{
|
{
|
||||||
|
|
|
@ -500,6 +500,16 @@ void lwip_config_net(char *ip, char *mask, char *gw)
|
||||||
if(lwip_init_flag)
|
if(lwip_init_flag)
|
||||||
{
|
{
|
||||||
lw_print("lw: [%s] already ...\n", __func__);
|
lw_print("lw: [%s] already ...\n", __func__);
|
||||||
|
|
||||||
|
IP4_ADDR(&net_ipaddr, ip[0], ip[1], ip[2], ip[3]);
|
||||||
|
IP4_ADDR(&net_netmask, mask[0], mask[1], mask[2], mask[3]);
|
||||||
|
IP4_ADDR(&net_gw, gw[0], gw[1], gw[2], gw[3]);
|
||||||
|
|
||||||
|
netif_set_down(&gnetif);
|
||||||
|
netif_set_gw(&gnetif, &net_gw);
|
||||||
|
netif_set_netmask(&gnetif, &net_netmask);
|
||||||
|
netif_set_ipaddr(&gnetif, &net_ipaddr);
|
||||||
|
netif_set_up(&gnetif);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lwip_init_flag = 1;
|
lwip_init_flag = 1;
|
||||||
|
|
|
@ -49,6 +49,8 @@
|
||||||
|
|
||||||
#if LWIP_TCP && LWIP_CALLBACK_API
|
#if LWIP_TCP && LWIP_CALLBACK_API
|
||||||
|
|
||||||
|
#define MAX_TESTED_TCP_SEND_SIZE (56000)
|
||||||
|
|
||||||
static struct tcp_pcb *tcpecho_raw_pcb;
|
static struct tcp_pcb *tcpecho_raw_pcb;
|
||||||
|
|
||||||
enum tcpecho_raw_states
|
enum tcpecho_raw_states
|
||||||
|
@ -107,50 +109,70 @@ tcpecho_raw_error(void *arg, err_t err)
|
||||||
tcpecho_raw_free(es);
|
tcpecho_raw_free(es);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define SHELL_TCP_LENGTH 80
|
||||||
char* recved_msg;
|
char* recved_msg;
|
||||||
|
int recved_msg_length;
|
||||||
|
// compute received message length until '\n'
|
||||||
static void record_msg(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
static void record_msg(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
||||||
if(es==NULL||es->p==NULL||es->p->len==0){
|
struct pbuf* ptr = es->p;
|
||||||
recved_msg = NULL;
|
int plen = ptr->len;
|
||||||
|
if(es==NULL||ptr==NULL||plen==0){
|
||||||
|
if(recved_msg){
|
||||||
|
free(recved_msg);
|
||||||
|
recved_msg = NULL;
|
||||||
|
}
|
||||||
|
recved_msg_length = 0;
|
||||||
}
|
}
|
||||||
int new_size = es->p->len+1;
|
int new_size = plen+1;
|
||||||
char* temp = recved_msg;
|
char* temp = recved_msg;
|
||||||
if(temp!=NULL){
|
if(temp!=NULL){
|
||||||
new_size += strlen(temp);
|
new_size += strlen(temp);
|
||||||
lw_print("temp: %s\r\n", recved_msg);
|
|
||||||
}
|
}
|
||||||
recved_msg = malloc(new_size);
|
recved_msg = malloc(new_size);
|
||||||
memset(recved_msg, 0, new_size);
|
memset(recved_msg, 0, new_size);
|
||||||
if(temp!=NULL){
|
if(temp!=NULL){
|
||||||
memcpy(recved_msg,temp,strlen(temp));
|
memcpy(recved_msg,temp,strlen(temp));
|
||||||
}
|
}
|
||||||
memcpy(recved_msg+new_size-es->p->len-1, es->p->payload, es->p->len);
|
memcpy(recved_msg+new_size-plen-1, ptr->payload, plen);
|
||||||
free(temp);
|
free(temp);
|
||||||
if(((char*)es->p->payload)[es->p->len-1]=='\n'){
|
recved_msg_length += plen;
|
||||||
if(strlen(recved_msg)<80){
|
if(recved_msg_length>=SHELL_TCP_LENGTH){
|
||||||
lw_pr_info("Received: %s\n", recved_msg);
|
if(recved_msg){
|
||||||
|
free(recved_msg);
|
||||||
|
recved_msg = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(((char*)ptr->payload)[plen-1]=='\n'){
|
||||||
|
if(recved_msg_length>MAX_TESTED_TCP_SEND_SIZE){
|
||||||
|
KPrintf("Recved_msg_length is larger than %d, which may lead to unexpected exceptions.\n",MAX_TESTED_TCP_SEND_SIZE);
|
||||||
|
}
|
||||||
|
if(recved_msg_length<SHELL_TCP_LENGTH){
|
||||||
|
KPrintf("Received: %s\n", recved_msg);
|
||||||
|
KPrintf("Received: %s\n", recved_msg);
|
||||||
}else{
|
}else{
|
||||||
lw_pr_info("Received a string of length %d\n", strlen(recved_msg));
|
KPrintf("Received a string of length %d\n", recved_msg_length);
|
||||||
}
|
}
|
||||||
struct pbuf* reply_pbuf = pbuf_alloc(PBUF_TRANSPORT, 20, PBUF_RAM); // only reply received message length
|
struct pbuf* reply_pbuf = pbuf_alloc(PBUF_TRANSPORT, 20, PBUF_RAM); // only reply received message length
|
||||||
sprintf(reply_pbuf->payload,"%d\n\0",strlen(recved_msg));
|
sprintf(reply_pbuf->payload,"%d\n\0",recved_msg_length);
|
||||||
reply_pbuf->len = strlen(reply_pbuf->payload);
|
reply_pbuf->len = strlen(reply_pbuf->payload);
|
||||||
reply_pbuf->tot_len = strlen(reply_pbuf->payload);
|
reply_pbuf->tot_len = strlen(reply_pbuf->payload);
|
||||||
reply_pbuf->next = NULL;
|
reply_pbuf->next = NULL;
|
||||||
|
|
||||||
tcp_write(tpcb, reply_pbuf->payload, reply_pbuf->len, 1);
|
tcp_write(tpcb, reply_pbuf->payload, reply_pbuf->len, 1);
|
||||||
tcp_recved(tpcb, reply_pbuf->len);
|
|
||||||
|
|
||||||
pbuf_free(reply_pbuf);
|
pbuf_free(reply_pbuf);
|
||||||
free(recved_msg);
|
free(recved_msg);
|
||||||
recved_msg = NULL;
|
recved_msg = NULL;
|
||||||
|
recved_msg_length = 0;
|
||||||
}
|
}
|
||||||
es->p = es->p->next;
|
es->p = ptr->next;
|
||||||
if(es->p != NULL) {
|
if(es->p != NULL) {
|
||||||
/* new reference! */
|
/* new reference! */
|
||||||
pbuf_ref(es->p);
|
pbuf_ref(es->p);
|
||||||
}
|
}
|
||||||
}
|
pbuf_free(ptr);
|
||||||
|
tcp_recved(tpcb, plen);
|
||||||
|
}
|
||||||
|
|
||||||
static err_t
|
static err_t
|
||||||
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||||
|
@ -256,6 +278,7 @@ tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
||||||
err_t ret_err;
|
err_t ret_err;
|
||||||
struct tcpecho_raw_state *es;
|
struct tcpecho_raw_state *es;
|
||||||
recved_msg = NULL;
|
recved_msg = NULL;
|
||||||
|
recved_msg_length = 0;
|
||||||
|
|
||||||
LWIP_UNUSED_ARG(arg);
|
LWIP_UNUSED_ARG(arg);
|
||||||
if ((err != ERR_OK) || (newpcb == NULL)) {
|
if ((err != ERR_OK) || (newpcb == NULL)) {
|
||||||
|
|
Loading…
Reference in New Issue