﻿
LwIP Ethernet Sample Application
--------------------------------

  LwIP is an open source TCP/IP stack designed for embedded applcations.  This
example shows how LwIP can be used with our Hardware Libraries to implement
an Ethernet example on an Intel SoC FPGA product.  The LwIP code provides the 
network software stack and the Hardware Libraries provide the low-level
interface to the SoC hardware.

  This example demonstrates how to use the alt_ethernet.c libraries with
a network stack.  This example is not designed to be secure and it should not
be considered production code.  Software vulnerabilities, both discovered and
undiscovered, may exist in the code.

  The LwIP stack was provided by the LwIP team; more information and software
updates can be found at this URL:

  https://savannah.nongnu.org/projects/lwip/

  For our example, we have enabled two applications, a simple web sever used
to demonstrate basic Ethernet traffic, and an iperf application to assist in
performance measurement.

How to use this program:
-----------------------

After starting the example program...

The default address of 10.122.105.14 is a static address and 
is only valid if LWIP_DHCP is set to 0 in lwipopts.h.
This static address can be changed in lwipopts.h.

If LWIP_DHCP is set to 1 (the default), then you must substitute
the address in the lines below with the acquired DHCP address.
This address will be printed at startup time.

In a web browser type the ip address to see a web page,  

Also you can run iperf -c 10.122.105.14 -p 32    
(you must have the iperf app)

Also, udp packets and tcp packets will be reflected back.  So you can use 
a simple program to send packets and then receive them back to check udp and tcp
operation.

Also, you can ping 10.122.105.14

The ip address and port assignments can be changed in lwipopts.h

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

Here is an example of a simple program to send and recieve udp packets.   

#include<stdio.h>	 
#include<string.h>	 
#include<sys/socket.h>	 
#include<arpa/inet.h>	 
#include<fcntl.h>	 

int main(int argc , unsigned char *argv[])
{
  struct sockaddr_in serverAddr;
  int s, size, i=0,j,k,port,errs=0,pass=0;
  char buf[512],buf2[512];
  socklen_t addr_size=sizeof(serverAddr);;
  int tx=0,rx=0;
  int count;

  if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1){
		printf("Could not create socket");
  }
  puts("Socket created");

  memset((char *) &serverAddr, 0, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  sscanf(argv[2],"%d",&port);
  serverAddr.sin_port = htons(port);
  serverAddr.sin_addr.s_addr = inet_addr(argv[1]);

  //make socket non blocking
  fcntl(s, F_SETFL, O_NONBLOCK);

  for (k=0;k<40000;k++) {
    sprintf(buf, "This is packet %d                              This is packet %d+1 \n",i,i+1);

    if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr))==-1) {
       printf("Send Error\n");
    }
    else tx++; 
        
    size=0;   
    count=0;    
    while (size<=0) {    
       size = recvfrom(s,buf2,1640,0,(struct sockaddr *)&serverAddr, &addr_size);
       buf2[size]=0;
       if (size>0) rx++;
       if ((count++) > 1000000) { printf("skip\n"); break; }
       if (count > 500000) usleep(10);
    }
    
    if (strcmp(buf,buf2)!=0) errs++;
    else pass++;
    
    if (rx != 0)
    if (((rx) & 0xff) == 0) printf("tx=%d rx=%d errs=%d pass=%d\n",tx,rx,errs,pass);

    i++;
  }

  close(s);
  return 0;
}

