lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Running echoserver in socket mode - RST is sent as the conn


From: Alekhya Perugupalli
Subject: [lwip-users] Running echoserver in socket mode - RST is sent as the connection is established
Date: Fri, 28 Sep 2012 15:05:24 -0500

Hi all,

I'm running a lwip echoserver on my board in socket mode. It works fine till a 
connection is established with the client. But lwip sends a RST as soon as it 
receives an ACK from the client. I'm not sure what the problem is. Please help. 

Here is the debug information : 

Board IP: 192.168.1.100
Netmask : 255.255.254.0
Gateway : 128.104.28.1
netif_set_ipaddr: netif address being changed
netif: IP address of interface set to 192.168.1.100
netif: netmask of interface set to 255.255.254.0
netif: GW address of interface set to 128.104.28.1
XLlTemac detect_phy: No PHY detected. Assuming a PHY at address 0
link speed: 1000
netif: added interface te IP addr 192.168.1.100 netmask 255.255.254.0 gw 
128.104.28.1
netif: setting default interface te
tcpip_thread: API message 0x9011b468
tcpip_thread: API message 0x9011b464
tcp_bind: bind to port 23
tcpip_thread: API message 0x9011b46c

Echo Server is ready at port 23
tcpip: ip_reass_tmr()
tcpip_thread: PACKET 0x9004fbc0
ip_input: iphdr->dest 0xc0a80164 netif->ip_addr 0xc0a80164 (0xc0a80000, 
0xc0a80000, 0x164)
ip_input: packet accepted on interface te
ip_input:
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 60 | (v, hl, tos, len)
+-------------------------------+
| 53753 |010| 0 | (id, flags, offset)
+-------------------------------+
| 64 | 6 | 0xe50b | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 2 | (src)
+-------------------------------+
| 192 | 168 | 1 | 100 | (dest)
+-------------------------------+
ip_input: p->len 60 p->tot_len 60
TCP header:
+-------------------------------+
| 41587 | 23 | (src port, dest port)
+-------------------------------+
| 00cf7d3e5e | (seq no)
+-------------------------------+
| 0000000000 | (ack no)
+-------------------------------+
| 10 | |000010| 5840 | (hdrlen, flags (SYN ), win)
+-------------------------------+
| 0x0000 | 0 | (chksum, urgp)
+-------------------------------+
tcp_input: packed for LISTENing connection.
TCP connection request 41587 -> 23.
tcp_enqueue(pcb=0x9004ae00, arg=0x0, len=0, flags=12, apiflags=0)
tcp_enqueue: queueing 196e:196f (0x12)
tcp_output_segment: 196e:196e
ip_output_if: te0
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 44 | (v, hl, tos, len)
+-------------------------------+
| 0 |000| 0 | (id, flags, offset)
+-------------------------------+
| 255 | 6 | 0x3815 | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 100 | (src)
+-------------------------------+
| 192 | 168 | 1 | 2 | (dest)
+-------------------------------+
netif->output()tcpip_thread: PACKET 0x9004fb80
tcpip_thread: PACKET 0x9004fbc0
ip_input: iphdr->dest 0xc0a80164 netif->ip_addr 0xc0a80164 (0xc0a80000, 
0xc0a80000, 0x164)
ip_input: packet accepted on interface te
ip_input:
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 40 | (v, hl, tos, len)
+-------------------------------+
| 53754 |010| 0 | (id, flags, offset)
+-------------------------------+
| 64 | 6 | 0xe51e | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 2 | (src)
+-------------------------------+
| 192 | 168 | 1 | 100 | (dest)
+-------------------------------+
ip_input: p->len 40 p->tot_len 40
TCP header:
+-------------------------------+
| 41587 | 23 | (src port, dest port)
+-------------------------------+
| 00cf7d3e5f | (seq no)
+-------------------------------+
| 000000196f | (ack no)
+-------------------------------+
| 5 | |010000| 5840 | (hdrlen, flags (ACK ), win)
+-------------------------------+
| 0x0000 | 0 | (chksum, urgp)
+-------------------------------+
+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags ACK -+-+-+-+-+-+-+-+-+-+-+-+-+-+
State: SYN_RCVD
TCP connection established 41587 -> 23.
tcp_pcb_purge
tcp_pcb_purge: data left on ->unacked
ip_output_if: te0
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 40 | (v, hl, tos, len)
+-------------------------------+
| 1 |000| 0 | (id, flags, offset)
+-------------------------------+
| 255 | 6 | 0x3818 | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 100 | (src)
+-------------------------------+
| 192 | 168 | 1 | 2 | (dest)
+-------------------------------+
netif->output()State: CLOSED
tcpip_thread: PACKET 0x9004fb40
ip_input: iphdr->dest 0xc0a80164 netif->ip_addr 0xc0a80164 (0xc0a80000, 
0xc0a80000, 0x164)
ip_input: packet accepted on interface te
ip_input:
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 46 | (v, hl, tos, len)
+-------------------------------+
| 53755 |010| 0 | (id, flags, offset)
+-------------------------------+
| 64 | 6 | 0xe517 | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 2 | (src)
+-------------------------------+
| 192 | 168 | 1 | 100 | (dest)
+-------------------------------+
ip_input: p->len 46 p->tot_len 46
TCP header:
+-------------------------------+
| 41587 | 23 | (src port, dest port)
+-------------------------------+
| 00cf7d3e5f | (seq no)
+-------------------------------+
| 000000196f | (ack no)
+-------------------------------+
| 5 | |011000| 5840 | (hdrlen, flags (PSH ACK ), win)
+-------------------------------+
| 0x0000 | 0 | (chksum, urgp)
+-------------------------------+
tcp_input: packed for LISTENing connection.
ip_output_if: te0
IP header:
+-------------------------------+
| 4 | 5 | 0x00 | 40 | (v, hl, tos, len)
+-------------------------------+
| 2 |000| 0 | (id, flags, offset)
+-------------------------------+
| 255 | 6 | 0x3817 | (ttl, proto, chksum)
+-------------------------------+
| 192 | 168 | 1 | 100 | (src)
+-------------------------------+
| 192 | 168 | 1 | 2 | (dest)
+-------------------------------+
netif->output()tcpip_thread: PACKET 0x9004fb80
tcp_slowtmr: no active pcbs
tcpip: ip_reass_tmr()
tcpip: ip_reass_tmr()
tcpip: ip_reass_tmr()


The echo server code is as follows: 


#include "xmk.h"
#include "xenv_standalone.h"
#include "xparameters.h"
#include "platform.h"
#include "platform_config.h"
#include "lwip/inet.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwipopts.h"
#include "netif/xadapter.h"
#include "lwip/ip_addr.h"
#include <stdio.h>
#include <unistd.h> /* misc. UNIX functions */
#include "helper.h" /* Our own helper functions */
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <sys/time.h> /* for struct timeval {} */
#include <fcntl.h> /* for fcntl() */
#include "mfs_config.h"

/* Global constants */

#define MAX_LINE (1000)
#define THREAD_STACKSIZE (4096)
#define ASCII_CARRIAGE_RETURN (13)
#define RECV_BUF_SIZE (2048)
#define ECHOSERV_PORT (7)
#define ECHOSERV_TIMEOUT (200) //in seconds


/* Function declarations */

int ParseCmdLine(int argc, char *argv[], char **szAddress, char **szPort);
int echoclnt();
void print_ip(char *msg, struct ip_addr *ip);
void print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr 
*gw);
void network_thread(void *p);
int echoserv();
int process_echo_request(int new_sd);


/* Structures */
struct netif server_netif;

/* echoclnt */

int echoserv()
{
 u16_t echo_port = 23;
 int sock, new_sd;
 struct sockaddr_in address, remote;
 int size;
 char recv_buf[RECV_BUF_SIZE];
 int n, nwrote;


 if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 xil_printf("\n\rlwip_socket Problem\n\r");
 return;
 }

 address.sin_family = AF_INET;
 address.sin_port = htons(echo_port);
 address.sin_addr.s_addr = INADDR_ANY;

 if (lwip_bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0) {
 xil_printf("\n\rlwip_bind Problem\n\r");
 return;
 }

 lwip_listen(sock, 5);

 xil_printf("\n\rEcho Server is ready at port %d\n\r", echo_port);

 size = sizeof(remote);

 if ((new_sd = lwip_accept(sock, (struct sockaddr *)&remote, (socklen_t 
*)&size)) < 0) {
 xil_printf("\n\rlwip_accept Problem\n\r");
 return;
 }

 while (1) {

 // read a max of RECV_BUF_SIZE bytes from socket
 if ((n = read(new_sd, recv_buf, RECV_BUF_SIZE)) < 0) {
 xil_printf("%s: error reading from socket %d, closing socket\n\r", 
__FUNCTION__, new_sd);
 close(new_sd);
 return;
 } else {
 recv_buf[n] = 0;
 xil_printf("incoming msg: %s\n\r", recv_buf);
 }

 //break if the recved message = "quit"
 if (!strncmp(recv_buf, "quit", 4))
 break;

 //break if client closed connection
 if (n <= 0)
 break;

 //handle request
 if ((nwrote = write(new_sd, recv_buf, n)) < 0) {
 xil_printf("%s: ERROR responding to client echo request. received = %d, 
written = %d\n\r",
 __FUNCTION__, n, nwrote);
 xil_printf("Closing socket %d\n\r", new_sd);
 close(new_sd);
 return;
 }
 }


/* close connection */
 close(new_sd);
}



/* MAIN */
int main() {

 init_platform();

 /* start the kernel - does not return */
 xilkernel_main();

 return 0;
}


/* network_thread */
void network_thread(void *p) {

 struct netif *netif;
 struct ip_addr ipaddr, netmask, gw;
 char ch;

 /* the mac address of the board. this should be unique per board */
 unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x02, 0x02, 0x96 };

 netif = &server_netif;


 /* initliaze IP addresses to be used for this board*/
 IP4_ADDR(&ipaddr, 192, 168, 1, 100);
 IP4_ADDR(&netmask, 255, 255, 254, 0);
 IP4_ADDR(&gw, 128, 104, 28, 1);


 /* print out IP settings of the board */
 print("\n\r\n\r");
 print("-----Echo Client & Server Socket Mode Demo Application ------\n\r");
 print_ip_settings(&ipaddr, &netmask, &gw);

 /* print all application headers */
 //print_headers();

 /* Add network interface to the netif_list, and set it as default */
 if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, 
XPAR_ETH_CHAN_0_BASEADDR)) {
 xil_printf("Error adding N/W interface\n\r");
 return;
 }
 netif_set_default(netif);

 /* specify that the network if is up */
 netif_set_up(netif);

 /* start packet receive thread - required for lwIP operation */
 sys_thread_new("xemacif_input_thread", (void(*)(void*))xemacif_input_thread, 
netif,
 THREAD_STACKSIZE,
 DEFAULT_THREAD_PRIO);

 /* now we can start echo server threads */
 sys_thread_new("echoserv", echoserv, 0, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);

 return;
}

/* main_thread() */
int main_thread() {
 /* initialize lwIP before calling sys_thread_new */
 lwip_init();


 /* The API for function sys_thread_new has changed from lwIP 1.2.0 to lwIP
 * 1.3.0. Use the new API as follows:
 * sys_thread_t sys_thread_new(char *name, void (*thread)(void *arg), void
 * *arg, int stacksize, int prio);
 */
 
 sys_thread_new("NW_THREAD", network_thread, NULL,
 THREAD_STACKSIZE,
 DEFAULT_THREAD_PRIO);

 return 0;
}

int ParseCmdLine(int argc, char *argv[], char **szAddress, char **szPort) {

 int n = 1;

 while ( n < argc ) {
 if ( !strncmp(argv[n], "-a", 2) || !strncmp(argv[n], "-A", 2) ) {
 *szAddress = argv[++n];
 }
 else if ( !strncmp(argv[n], "-p", 2) || !strncmp(argv[n], "-P", 2) ) {
 *szPort = argv[++n];
 }
 else if ( !strncmp(argv[n], "-h", 2) || !strncmp(argv[n], "-H", 2) ) {
 printf("Usage:\n\n");
 printf(" timeclnt -a (remote IP) -p (remote port)\n\n");
 exit(EXIT_SUCCESS);
 }
 ++n;
 }

 return 0;
}

/* print_ip */
void print_ip(char *msg, struct ip_addr *ip) {
 print(msg);
 xil_printf("%d.%d.%d.%d\n\r", ip4_addr1(ip), ip4_addr2(ip),
 ip4_addr3(ip), ip4_addr4(ip));
}


/* print_ip_settings */
void print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr 
*gw) {

 print_ip("Board IP: ", ip);
 print_ip("Netmask : ", mask);
 print_ip("Gateway : ", gw);
}


Thanks,
Alek



reply via email to

[Prev in Thread] Current Thread [Next in Thread]