lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] Re: lwip-users Digest, Vol 43, Issue 28


From: Jian Rong @ Makoto
Subject: [lwip-users] Re: lwip-users Digest, Vol 43, Issue 28
Date: Sun, 18 Mar 2007 04:50:55 -0700 (PDT)

Dear everyone,
 
               Has anyone run the UDP server code on Xilinx Spartan Board?  I wonder if anyone got examples of the source code. My source code below doesn't seem to run:
 
#include "udp_config.h"
#include "xbasic_types.h"
#include "mfs_config.h"    // Required for the Xilinx Memory File System
#include "xstatus.h"        //Required for system checks on peripheral functions
#include "xmk.h"    //Required for xilkernel_main()
#include "xintc.h"
/* C library Includes */
#include "stdio.h"
#include "string.h"        /* memset() */

/* lwIP Library Includes */
#include "netif/xemacif.h"
#include "lwip/tcpip.h"
//#include "lwip/udp.h"
#include "lwip/memp.h"
#include "netif/etharp.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
//#include "lwip/inet.h"

/* Defined in EDK generated xemacif_g.c file */
extern XEmacIf_Config XEmacIf_ConfigTable[];
void* socket_app_thread(void *arg)
{
   struct ip_addr ipaddr, netmask, gw;
   struct netif *server_netif;
   char fullmac[6] = {0x00, 0x18, 0x3E, 0x00, 0x24, 0xA3};
 char ip[4] =      {192, 168, 0, 2};      
   char subnet[4] =  {255, 255, 255, 0};
   char gateway[4] = {192, 168, 0, 1};
 char msg[MAX_MSG];
 int sock, rc, n;
   int cliAddrLen;
   struct sockaddr_in servaddr,cliaddr;
 
   XEmacIf_Config *xemacif_ptr = &XEmacIf_ConfigTable[0];
 
   /*-----------------------------------------------------------------------*/
   /* Initial Header and Menus.  Do this before the netif_init() so we can  */
   /* change the MAC Address and IP addresses if needed                     */
   /*-----------------------------------------------------------------------*/
   // Clear the screen and print the legal header for the reference design
   //clearScreen();
   //printLegalHeader();
   //fullmac[0] = MAC_OUI0;
   //fullmac[1] = MAC_OUI1;
   //fullmac[2] = MAC_OUI2;
 //fullmac[3] = MAC_OUI3;
   //fullmac[4] = MAC_OUI4;
   //fullmac[5] = MAC_OUI5;
       
   /*-----------------------------------------------------------------------*/
   /* Set host addresses                                                    */
   /*-----------------------------------------------------------------------*/
   xemacif_setmac(0, (u8_t *) fullmac);                        //Set MAC
   IP4_ADDR(&gw, gateway[0],gateway[1],gateway[2],gateway[3]); //Set gateway
   IP4_ADDR(&ipaddr, ip[0],ip[1],ip[2],ip[3]);                 //Set ip
   IP4_ADDR(&netmask,subnet[0],subnet[1],subnet[2],subnet[3]); //Set subnet msk
  
   /*-----------------------------------------------------------------------*/
   /* Show some host boot stuff and parameters                              */
   /*-----------------------------------------------------------------------*/
    clearScreen();
    displayConfiguration(fullmac, ip, subnet, gateway);
    
   /*-----------------------------------------------------------------------*/
   /* Set up the lwIP network interface...                                  */
   /*-----------------------------------------------------------------------*/
   /* allocate netif structure */ 
   server_netif = mem_malloc(sizeof(struct netif));
   if (server_netif == NULL) {
        xil_printf("ERROR : netif_add(): out of memory for default_netif\n");
        return NULL;
   }
   server_netif = netif_add(server_netif,
                              &ipaddr,
                              &netmask,
                              &gw,
                              &XEmacIf_ConfigTable[0],
                              xemacif_init,
                              tcpip_input);
  
 netif_set_default(server_netif);
   /* Register XEmacHandler with interrupt controller and enable interrupts
   * with XMK
   */
   register_int_handler(XPAR_OPB_INTC_0_ETHERNET_MAC_IP2INTC_IRPT_INTR,
                         (XInterruptHandler)XEmac_IntrHandlerFifo,
                         xemacif_ptr->instance_ptr);
  
   /* Enable EMAC interrupts in XMK */
   enable_interrupt(XPAR_OPB_INTC_0_ETHERNET_MAC_IP2INTC_IRPT_INTR);
  
   /*-----------------------------------------------------------------------*/
   /* create socket and bind                                                */
   /*-----------------------------------------------------------------------*/
   xil_printf("SOCKET: Creating socket..");
  
   sock = socket(AF_INET,SOCK_DGRAM,0);
 xil_printf("done.\r\n");
   xil_printf("SOCKET: Doing bind..");
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servaddr.sin_port = htons(LOCAL_SERVER_PORT);
 
 cliaddr.sin_family = AF_INET;
 cliaddr.sin_port = htons(0);
 cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
   rc = bind(sock,(struct sockaddr *) &servaddr,sizeof(servaddr));
 if(rc<0) {
    xil_printf("Cannot bind port number %d \n", LOCAL_SERVER_PORT);
    exit(1);
   }
   xil_printf("Waiting for data on port UDP %u\n", LOCAL_SERVER_PORT);
   xil_printf("done.\r\n");
 
 
   /* server infinite loop */
   while(1){
   
     /* init buffer */
     memset(msg,0x0,MAX_MSG);
     /* receive message */
     cliAddrLen = sizeof(cliaddr);
   xil_printf("Handling client.........................\r\n");
     n = recvfrom(sock, msg, MAX_MSG, 0, (struct sockaddr *) &cliaddr, &cliAddrLen);
     if(n<0) {
       xil_printf("Cannot receive data \n");
       continue;
      }
     
     /* print received message */
     xil_printf("From %s:UDP%u : %s \n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port),msg);
   
  }/* end of server infinite loop */
return 0;
}
void* socket_thread(void* arg)
{
    /*-----------------------------------------------------------------------*/
    /* Do LWIP System Inits                                                  */
    /*-----------------------------------------------------------------------*/
   xil_printf("Initializing lwIP .");
   lwip_init();
   xil_printf(".");
   xil_printf(" done.\r\n");
 sys_thread_new ((void (*)(void*))socket_app_thread, 0, 0);
 return 0;
  
}

int main() {
 
    // Clear the screen to get rid of anything that may initially have
    // been displayed on the user's terminal
    clearScreen();
 
    xil_printf("Xilinx Web Server Demonstration\r\n");
    xil_printf("(c) Xilinx, Inc. 2005\r\n\r\n");
    xil_printf("System Initialization in progress:\r\n\r\n");
       
 xilkernel_main();
   return 0;
 
}
 
 
 
Send lwip-users mailing list submissions to
address@hidden

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.nongnu.org/mailman/listinfo/lwip-users
or, via email, send a message with subject or body 'help' to
address@hidden

You can reach the person managing the list at
address@hidden

When replying, please edit your Subject line so it is more specific
than "Re: Contents of lwip-users digest..."


Today's Topics:

1. Re: RE: Why the tcp_pcb haven't been free when I
calltcp_close() (geckook Xu)
2. TAP/TUN Device (Dinesh Chachan)
3. Re: RE: Why the tcp_pcb haven't been free when I
calltcp_close() (Kieran Mansley)
4. RE: Why the tcp_pcb haven't been free when I calltcp_close()
(geckook Xu)
5. Re: RE: Why the tcp_pcb haven't been free when I
calltcp_close() (Kieran Mansley)


----------------------------------------------------------------------

Message: 1
Date: Fri, 16 Mar 2007 07:44:31 +0800
From: "geckook Xu"
Subject: Re: [lwip-users] RE: Why the tcp_pcb haven't been free when I
calltcp_close()
To: address@hidden
Message-ID:
<address@hidden>
Content-Type: text/plain; charset="iso-8859-1"

yes ,when call tcp_close(),they go to FIN_WAIT1,
I reduce the TCP_FIN_WAIT_TIMEOUT and the TCP_MSL values, but my 100
tcp_pcb have been exhausted.
I find it from the mem states.
Can you look into my code and give me some advice.


> > When I call tcp_close(),the program will send FIN,
>
> At which point the connection will (or at least should) move from
> ESTABLISHED to FIN_WAIT1.
>
> > and then the
> > receiver will reply FIN
>
> At which point we'll go through FIN_WAIT2 to TIME_WAIT.
>
> > But the tcp_pcb haven't been free after this operation.
>
> It will have to wait to timeout from TIME_WAIT before it is freed.
>
> However, you said in an earlier post that the connection was still in
> the ESTABLISHED TCP state. If that is the case I don't know what is
> going on.
>
> Kieran
>
-------------- next part --------------
/*------------------------------------------------------------*/
/* Sample task of http */
/*------------------------------------------------------------*/
#include
#include
#include
#include
#include "lwip/sys.h"
#include "lwip/api.h"
#include "lwip/httpd.h"

//by ming 2005-01 add counter & runtimer
u32_t runtime=0;
u32_t counter=0;
void httpd_init(void);
/*-----------------------------------------------------------------------------------*/
static void
conn_err(void *arg, err_t err)
{
struct http_state *hs;

hs = arg;
mem_free(hs);
}
/*-----------------------------------------------------------------------------------*/
static void
close_conn(struct tcp_pcb *pcb, struct http_state *hs)
{
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
mem_free(hs);
tcp_close(pcb);
}
/*-----------------------------------------------------------------------------------*/
static void
send_data(struct tcp_pcb *pcb, struct http_state *hs)
{
err_t err;
u16_t len;

/* We cannot send more data than space available in the send
buffer. */
if(tcp_sndbuf(pcb) < hs->left) {
len = tcp_sndbuf(pcb);
} else {
len = hs->left;
}

do {
err = tcp_write(pcb, hs->file, len, 0);
if(err == ERR_MEM) {
len /= 2;
}
} while(err == ERR_MEM && len > 1);

if(err == ERR_OK) {
hs->file += len;
hs->left -= len;
}
}
/*-----------------------------------------------------------------------------------*/
static err_t
http_poll(void *arg, struct tcp_pcb *pcb)
{
struct http_state *hs;

hs = arg;

/* printf("Polll\n");*/
if(hs == NULL) {
/* printf("Null, close\n");*/
tcp_abort(pcb);
return ERR_ABRT;
} else {
++hs->retries;
if(hs->retries == 4) {
tcp_abort(pcb);
return ERR_ABRT;
}
send_data(pcb, hs);
}

return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
static err_t
http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct http_state *hs;

hs = arg;

hs->retries = 0;

if(hs->left > 0) {
send_data(pcb, hs);
} else {
close_conn(pcb, hs);
}

return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
static err_t
http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
int i;
char *data;
struct http_state *hs;
//by ming 2005.01
u8_t hour,min,sec;
u16_t day;


hs = arg;

if(err == ERR_OK && p != NULL) {

/* Inform TCP that we have taken the data. */
tcp_recved(pcb, p->tot_len);

if(hs->file == NULL) {
data = "">
if(*data ="" {
for(i = 0; i < 40; i++) {
if( ((char *)data + 4)[i] == ' ' || ((char *)data + 4)[i] == '\r' || ((char *)data + 4)[i] == '\n') {
((char *)data + 4)[i] = 0;
}// if
}//for

// if it was GET dsp.jpg
if ((*(data+5) =='d') && (*(data+6) =='s') && (*(data+7) =='p')) {
//hs->file = (char *)&jpeg;
//hs->left = sizeof(jpeg);
}
// if it was GET dsk.jpg (large one)
else if ((*(data+5) =='d') && (*(data+6) =='s') && (*(data+7) =='k')) {
//hs->file = (char *)&large;
//hs->left = sizeof(large);
}
// GET other files
else {
hs->file = (char *)&demo;
hs->left = sizeof(demo);
/*
//-----------------------------------------------------------------
//counter
counter ++;
sprintf(&demo[517],"%.6d",counter);
//runtime
runtime = OSTimeGet() / 80; ///100; //tmp by millin 2005.01.23
sec = runtime % 60;
min = (runtime / 60) % 60;
hour = (runtime / 3600) % 24;
day = (runtime / 3600) / 24;
sprintf(&demo[573],"%.3d:%.2d:%.2d:%.2d",day,hour,min,sec);
//-----------------------------------------------------------------*/
}

pbuf_free(p);
send_data(pcb, hs);

/* Tell TCP that we wish be to informed of data that has been
successfully sent by a call to the http_sent() function. */
tcp_sent(pcb, http_sent);

} //if(*data ="">
else {
pbuf_free(p);
close_conn(pcb, hs);
} //else if(*data ="">
} //if(hs->file == NULL)

else {
pbuf_free(p);
}
}

if(err == ERR_OK && p == NULL) {
close_conn(pcb, hs);
}
return ERR_OK;
}
/*-----------------------------------------------------------------------------------*/
static err_t
http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct http_state *hs;

tcp_setprio(pcb, TCP_PRIO_MIN);

/* Allocate memory for the structure that holds the state of the
connection. */
hs = mem_malloc(sizeof(struct http_state));

if(hs == NULL) {
return ERR_MEM;
}

/* Initialize the structure. */
hs->file = NULL;
hs->left = 0;
hs->retries = 0;

/* Tell TCP that this is the structure we wish to be passed for our
callbacks. */
tcp_arg(pcb, hs);

/* Tell TCP that we wish to be informed of incoming data by a call
to the http_recv() function. */
tcp_recv(pcb, http_recv);

tcp_err(pcb, conn_err);

tcp_poll(pcb, http_poll, 4);
return ERR_OK;
}

/*-----------------------------------------------------------------------------------*/
void
httpd_init(void)
{
struct tcp_pcb *pcb;

//by ming 2005.1
OSTimeSet((u32_t) 0x00000000);
counter = (u32_t) 0x00000000;

pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 80);
pcb = tcp_listen(pcb);
tcp_accept(pcb, http_accept);

}
char sendBuf[129]={0};
static err_t
http_recv2(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
int i;
char *data;
struct http_state *hs;
//by ming 2005.01
u8_t hour,min,sec;
u16_t day;
struct pbuf *q=NULL;

int len=0;
hs = arg;

if(err == ERR_OK && p != NULL) {

/* Inform TCP that we have taken the data. */
tcp_recved(pcb, p->tot_len);
q=p;
while(q)
{
data = "">if(data) {
len = q->len;
while(len--)
{
sendBuf[len-1]=data[len-1];
}
sendBuf[len]=0;
hs->file = (char *)&sendBuf;
hs->left = q->len;
send_data(pcb, hs);
// break;
/* Tell TCP that we wish be to informed of data that has been
successfully sent by a call to the http_sent() function. */
// tcp_sent(pcb, http_sent);
}
q=q->next;
}
pbuf_free(p);
}

if(err == ERR_OK && p == NULL) {
// close_conn(pcb, hs);
}
return ERR_OK;
}
static err_t
http_accept2(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct http_state *hs;

tcp_setprio(pcb, TCP_PRIO_MIN);

/* Allocate memory for the structure that holds the state of the
connection. */
hs = mem_malloc(sizeof(struct http_state));

if(hs == NULL) {
return ERR_MEM;
}

/* Initialize the structure. */
hs->file = NULL;
hs->left = 0;
hs->retries = 0;

/* Tell TCP that this is the structure we wish to be passed for our
callbacks. */
tcp_arg(pcb, hs);

/* Tell TCP that we wish to be informed of incoming data by a call
to the http_recv() function. */
tcp_recv(pcb, http_recv2);

tcp_err(pcb, conn_err);

tcp_poll(pcb, http_poll, 4);
return ERR_OK;
}

void
httpdecho_init(void)
{
struct tcp_pcb *pcb;

//by ming 2005.1
OSTimeSet((u32_t) 0x00000000);
counter = (u32_t) 0x00000000;

pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 80);
pcb = tcp_listen(pcb);
tcp_accept(pcb, http_accept2);

}

------------------------------

Message: 2
Date: 16 Mar 2007 05:26:36 -0000
From: "Dinesh Chachan"
Subject: [lwip-users] TAP/TUN Device
To: address@hidden
Message-ID: <address@hidden>
Content-Type: text/plain; charset="iso-8859-1"


As Kieran had said to enable IP fowarding. It has already been enabled in my system. I even ried to run the simhost application. But the simhost application doesnot respond to any ping messages.

The configuration of my tun devices is as follows

tun0 Link encap:Point-to-Point Protocol
inet addr:192.168.1.2 P-t-P:192.168.1.3 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

tun1 Link encap:Point-to-Point Protocol
inet addr:192.168.1.3 P-t-P:192.168.1.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:1 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:44 (44.0 b) TX bytes:0 (0.0 b)

On tun0 my server program is listening while on tun1 my client program is writing data.

When I do tcpdump -i tun0 nothing is shown

When I do tcpdump -i tun1 the following is displayed

tcpdump: listening on tun1
14:04:26.454185 192.168.1.3.4097 > 192.168.1.2.1500: S 6509:6509(0) win 1024 s 128> (DF)
14:04:29.477613 192.168.1.3.4097 > 192.168.1.2.1500: S 6509:6509(0) win 1024 s 128> (DF)
14:04:32.501040 192.168.1.3.4097 > 192.168.1.2.1500: S 6509:6509(0) win 1024 s 128> (DF)
14:04:35.524490 192.168.1.3.4097 > 192.168.1.2.1500: S 6509:6509(0) win 1024 s 128> (DF)
tcpdump: pcap_loop: recvfrom: Network is down

And my client program ends with netconn_connect returning an error value -3.

Am I doing something wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.gnu.org/pipermail/lwip-users/attachments/20070316/3e9a6203/attachment.html

------------------------------

Message: 3
Date: Fri, 16 Mar 2007 08:49:51 +0000
From: Kieran Mansley
Subject: Re: [lwip-users] RE: Why the tcp_pcb haven't been free when I
calltcp_close()
To: Mailing list for lwIP users
Message-ID: <address@hidden>
Content-Type: text/plain

On Fri, 2007-03-16 at 07:44 +0800, geckook Xu wrote:
> yes ,when call tcp_close(),they go to FIN_WAIT1,
> I reduce the TCP_FIN_WAIT_TIMEOUT and the TCP_MSL values, but my 100
> tcp_pcb have been exhausted.
> I find it from the mem states.

Reducing the FIN_WAIT and TIME_WAIT timeouts is not something I would
recommend unless you know for sure that it will not cause you problems.
These are mechanisms that TCP uses to ensure reliable connections and
reliable transmission of data. I'm afraid that if you have a lot of
connections in TIME_WAIT you either have to wait until they time out and
become available for reuse, reduce your connection rate so that you
don't run out in the first place, or configure more pcbs. This isn't
due to the way lwIP is implemented - it is due to the way TCP is
designed, and would be true for all RFC compliant TCP stacks.

Kieran





------------------------------

Message: 4
Date: Fri, 16 Mar 2007 23:48:27 +0800
From: "geckook Xu"
Subject: [lwip-users] RE: Why the tcp_pcb haven't been free when I
calltcp_close()
To: address@hidden
Message-ID:
<address@hidden>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

But the tcp_pcb haven't been free for a long time,and my stack can not work.

When I add a new thread to do with the received data, then the tcp_pcb
would be free. In this cast, I use the message box to communicate
between the stack and the other thread. But when the tcp_pcb have been
exhausted, message box will block for a long time (about 2 seconds).
What is the metter?
but when I do with the received data in the tcp_rcvd callback
function, In this case, the tcp_pcb couldn't be free.
Are there anything should be take attention to?


> yes ,when call tcp_close(),they go to FIN_WAIT1,
> I reduce the TCP_FIN_WAIT_TIMEOUT and the TCP_MSL values, but my 100
> tcp_pcb have been exhausted.
> I find it from the mem states.

Reducing the FIN_WAIT and TIME_WAIT timeouts is not something I would
recommend unless you know for sure that it will not cause you problems.
These are mechanisms that TCP uses to ensure reliable connections and
reliable transmission of data. I'm afraid that if you have a lot of
connections in TIME_WAIT you either have to wait until they time out and
become available for reuse, reduce your connection rate so that you
don't run out in the first place, or configure more pcbs. This isn't
due to the way lwIP is implemented - it is due to the way TCP is
designed, and would be true for all RFC compliant TCP stacks.




------------------------------

Message: 5
Date: Fri, 16 Mar 2007 16:09:46 +0000
From: Kieran Mansley
Subject: Re: [lwip-users] RE: Why the tcp_pcb haven't been free when I
calltcp_close()
To: Mailing list for lwIP users
Message-ID: <address@hidden>
Content-Type: text/plain

On Fri, 2007-03-16 at 23:48 +0800, geckook Xu wrote:
> But the tcp_pcb haven't been free for a long time,and my stack can not work.

How long is a long time? It should take two minutes to timeout from the
TIMEWAIT state if you haven't changed the default configuration for
TCP_MSL.

> When I add a new thread to do with the received data, then the tcp_pcb
> would be free. In this cast, I use the message box to communicate
> between the stack and the other thread. But when the tcp_pcb have been
> exhausted, message box will block for a long time (about 2 seconds).
> What is the metter?
> but when I do with the received data in the tcp_rcvd callback
> function, In this case, the tcp_pcb couldn't be free.
> Are there anything should be take attention to?

Sounds like it's possible your the timeouts aren't happening properly.
Are you calling tcp_tmr() (or alternatively tcp_slowtmr() and
tcp_fasttmr() directly) as described in the doc/rawapi.txt?

"When the system is running, the two timer functions tcp_fasttmr() and
tcp_slowtmr() must be called with regular intervals. The tcp_fasttmr()
should be called every TCP_FAST_INTERVAL milliseconds (defined in tcp.h)
and tcp_slowtmr() should be called every TCP_SLOW_INTERVAL milliseconds"

The tcp_tmr() function simplifies things slightly. If you call this
every TCP_FAST_INTERVAL it will call the other two at appropriate times.

Kieran





------------------------------

_______________________________________________
lwip-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/lwip-users

End of lwip-users Digest, Vol 43, Issue 28
******************************************



Yours sincerely,
Jian Rong @ Makoto
http://www.amtfpureland.com.cn/
http://buddha.goodweb.cn/music/music1.htm
 
 


It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
reply via email to

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