Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Sending a UDP-message

Altera_Forum
Honored Contributor II
1,649 Views

Hi all, 

 

this is my code. I am trying to send messages through UDP. But all it is sending are ARP's. 

 

Could anyone tell me what's wrong? 

 

I am going crazy overhere.... http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/unsure.gif  

 

Cheers, 

 

Danny 

 

******************************************************************** 

/* 

* This file is for testing stand-alone (NO_SYS) LWIP 

* using a RAM-based simple file system 

*/ 

# include "system.h" 

# include "arch/init.h"# include "lwip/ip_addr.h"# include "lwip/tcpip.h"# include "lwip/netif.h"# include "netif/lan91c111if.h" 

# include "httpd.h"# include "echo.h" 

# include "sys/alt_alarm.h" 

//#define get_milliseconds() alt_nticks() 

 

/* ---------- IP oriented addresses for ethernet adapter ---------- */# define IPADDR0 169# define IPADDR1 254# define IPADDR2 0# define IPADDR3 2 

# define NETMASK0 255# define NETMASK1 255# define NETMASK2 0# define NETMASK3 0 

# define GWADDR0 0# define GWADDR1 0# define GWADDR2 0# define GWADDR3 0 

 

 

int main(void) 

//0.6.4 struct netif *netif; 

struct netif netif; 

struct ip_addr ipaddr, netmask, gw; 

struct ip_addr udpDestIpAddr; //IP-address to send UDP packet to 

struct pbuf* packetBuffer; //Pointer to a packet buffer 

struct udp_pcb* udpSocket; //Create UDP-socket 

 

char buffer[100]; 

// char *buffPtr; 

 

alt_avalon_lan91c111_if* dev_list_ptr = (alt_avalon_lan91c111_if*)alt_ethernet_device_list.next; 

 

printf("UDP-server using Light-weight IP (LWIP)\n\n"); 

// sprintf(buffer, "PWNED \n"); //MSG to send to vics0urce 

 

// buffPtr = mem_malloc(sizeof(buffer)); 

// memcpy(buffPtr, buffer, sizeof(buffer)); //copy&#39;s from buffer to buffer 

 

packetBuffer = pbuf_alloc(PBUF_TRANSPORT,666,PBUF_RAM); 

pbuf_free(packetBuffer); 

// packetBuffer -> payload = (void *)buffPtr; 

//packetBuffer -> next = NULL;  

// packetBuffer -> tot_len=1450;  

// packetBuffer -> len=1450;  

/* 

* Initialize lwip 

*/ 

lwip_init(); 

 

printf ("Setting IP address to: %d.%d.%d.%d\n", IPADDR0, IPADDR1, IPADDR2, IPADDR3); 

printf ("Setting netmask to: %d.%d.%d.%d\n", NETMASK0, NETMASK1, NETMASK2, NETMASK3); 

printf ("Setting gateway address to: %d.%d.%d.%d\n\n\n", GWADDR0, GWADDR1, GWADDR2, GWADDR3); 

IP4_ADDR(&ipaddr, IPADDR0, IPADDR1, IPADDR2, IPADDR3); 

IP4_ADDR(&netmask, NETMASK0, NETMASK1, NETMASK2, NETMASK3); 

IP4_ADDR(&gw, GWADDR0, GWADDR1, GWADDR2, GWADDR3); 

 

//0.6.4 netif = netif_add(&ipaddr, &netmask, &gw, 

netif_add(&netif, &ipaddr, &netmask, &gw, 

(void*)dev_list_ptr, 

lan91c111if_init,  

ip_input); 

//0.6.4 netif_set_default(&netif); 

netif_set_default(&netif); 

 

IP4_ADDR(&udpDestIpAddr, 169, 254, 0, 1); 

 

udpSocket = udp_new();  

udp_bind(udpSocket, IP_ADDR_ANY, 1024); //Bind socket to port 

udp_connect(udpSocket, &udpDestIpAddr, 666); //Bind socket to any incoming IP-address 

 

 

printf("Ready to send....\n"); 

 

while(1) 

 

//0.6.4 lan91c111if_service(netif); 

 

//udp_recv(udpSocket, &packetBuffer, (int *)sizeof(packetBuffer)); 

 

// pbuf2->payload = (void*)((UDP_CHECKSUM_BASE+4*11)|0x80000000);  

 

udp_send(udpSocket, packetBuffer); 

 

 

if(buffer != NULL) 

 

{  

printf("Pakket is verzonden\n"); 

// sprintf(udpSocket, "socket is \n"); 

 

 

}  

 

lan91c111if_service(&netif); 

 

******************************************************************
0 Kudos
10 Replies
Altera_Forum
Honored Contributor II
788 Views

Hello Danny, 

 

Does your target (perhaps the PC) send an answer for the ARP requests? If not lwip can’t send any UDP packets because the stack doesn’t know which MAC address belongs to the IP address (in your code 169.254.0.1). But it must be possible to send broadcast messages. Just change IP4_ADDR(&udpDestIpAddr, 169, 254, 0, 1); to IP4_ADDR(&udpDestIpAddr, 255, 255, 255, 255);. 

 

Regards, 

niosIIuser
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

 

--- Quote Start ---  

originally posted by niosiiuser@Nov 8 2005, 06:53 AM 

hello danny, 

 

does your target (perhaps the pc) send an answer for the arp requests? if not lwip can’t send any udp packets because the stack doesn’t know which mac address belongs to the ip address (in your code 169.254.0.1). but it must be possible to send broadcast messages. just change ip4_addr(&udpdestipaddr, 169, 254, 0, 1); to ip4_addr(&udpdestipaddr, 255, 255, 255, 255);. 

 

regards, 

niosiiuser 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=10846) 

--- quote end ---  

 

--- Quote End ---  

 

 

Hello niosIIuser, 

 

the pc does send a request.... so I don&#39;t know what is wrong.... but I&#39;ve got an idea... let&#39;s see if it works. 

 

Cheers, 

 

Danny
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

Hello Danny, 

 

Did you test to send a broadcast? That must work because for sending a broadcast message there is no need for an actual ARP list. 

 

Regards, 

niosIIuser
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

 

--- Quote Start ---  

originally posted by niosiiuser@Nov 8 2005, 07:45 AM 

hello danny, 

 

did you test to send a broadcast? that must work because for sending a broadcast message there is no need for an actual arp list. 

 

regards, 

niosiiuser 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=10848) 

--- quote end ---  

 

--- Quote End ---  

 

 

Hi niosIIuser, 

 

I am able to send to broadcast. Source port and destination port are both &#39;0&#39; according to ethereal. But when I try to send to 169.254.0.1 I only get ARP&#39;s. What could be the problem?! 

 

Cheers, 

 

Danny http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

okok, I am stupid http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/tongue.gif  

 

I copied lan91c111if_service(&netif); outside the while loop http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/dry.gif  

 

so I can send again http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/laugh.gif the only problem now is that I can send only to port 0 http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/sad.gif so if anyone has got any suggestions please let me know.... 

 

 

cheers, 

 

Danny 

 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/ph34r.gif
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

Danny, 

 

At least, initially, it makes sense to model your code after the example shipped with Standalone LWIP. In other words, get the example working, understand what it&#39;s doing, and then add something like the following to it (in the ./apps subdirectory): 

 

#include "echo_udp.h"# include "lwip/debug.h"# include "lwip/stats.h"# include "lwip/udp.h"# include "system.h"# include "alt_types.h"# include "altera_avalon_pio_regs.h"# include "stdlib.h"# include "stdio.h"# include "ctype.h"# include <unistd.h> static void echo_udp_recv( void* arg, struct udp_pcb *pcb,                          struct pbuf *p,                          struct ip_addr *addr,                          u16_t port ) {  err_t err;  err = udp_connect( pcb, addr, port );  if (err != ERR_OK)  {    printf( "ERROR:  udp_connect() error:  %d\n", err );  }  err = udp_send( pcb, p );  if (err != ERR_OK)  {    printf( "ERROR:  udp_send() error:  %d\n", err );  }  pbuf_free( p );  udp_remove( pcb );  echo_udp_init();  return; } void echo_udp_init(void) {  struct udp_pcb *pcb;  pcb = udp_new();  udp_bind( pcb, IP_ADDR_ANY, 8 );  udp_recv( pcb, &echo_udp_recv, NULL ); } 

 

echo_udp_init() initializes a new pcb, binds to local port 8, and then registers echo_udp_recv() as the callback when anything is received on port 8. 

 

Keep in mind that UDP is connectionless and we&#39;re not talking about sockets, at all, with stand alone LWIP. It&#39;s a totally separate distribution, available from this website.  

 

In any case, the above code works, if you can get the example shipped with the stand alone distribution working... 

 

Cheers, 

 

- slacker
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

 

--- Quote Start ---  

originally posted by slacker@Nov 8 2005, 12:49 PM 

danny, 

 

at least, initially, it makes sense to model your code after the example shipped with standalone lwip.  in other words, get the example working, understand what it&#39;s doing, and then add something like the following to it (in the ./apps subdirectory): 

 

#include "echo_udp.h"# include "lwip/debug.h"# include "lwip/stats.h"# include "lwip/udp.h"# include "system.h"# include "alt_types.h"# include "altera_avalon_pio_regs.h"# include "stdlib.h"# include "stdio.h"# include "ctype.h"# include <unistd.h> static void echo_udp_recv( void* arg, struct udp_pcb *pcb,                          struct pbuf *p,                          struct ip_addr *addr,                          u16_t port ) {  err_t err;  err = udp_connect( pcb, addr, port );  if (err != err_ok)  {    printf( "error:  udp_connect() error:  %d\n", err );  }  err = udp_send( pcb, p );  if (err != err_ok)  {    printf( "error:  udp_send() error:  %d\n", err );  }  pbuf_free( p );  udp_remove( pcb );  echo_udp_init();  return; } void echo_udp_init(void) {  struct udp_pcb *pcb;  pcb = udp_new();  udp_bind( pcb, ip_addr_any, 8 );  udp_recv( pcb, &echo_udp_recv, null ); } 

 

echo_udp_init() initializes a new pcb, binds to local port 8, and then registers echo_udp_recv() as the callback when anything is received on port 8. 

 

keep in mind that udp is connectionless and we&#39;re not talking about sockets, at all, with stand alone lwip.  it&#39;s a totally separate distribution, available from this website.  

 

in any case, the above code works, if you can get the example shipped with the stand alone distribution working... 

 

cheers, 

 

- slacker 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=10856)</div> 

--- Quote End ---  

 

 

Hi all, 

 

I already got the example code working. I can also send messages thanks to Revolt.... The problem now is that when I try to send a message, the only problem that I&#39;ve got now is that when I send a message to a PC, it will send from source port 0 to destination port 0. Probably something I forgot to set. I am going to look at the LWIP code. Perhaps I can find a solution for my problem. The problem is that I&#39;m an interm and at the end of january they want me to fix a couple of things. Butt as a n00b, that is not going to succeed, so I am trying to fix as much as possible, so thanx for your help all... 

 

Cheers, 

 

Danny
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

Hi Slacker, 

 

so what you are suggesting is make a c-file with you&#39;re code and try to get it working? 

 

Cheers, 

 

Danny
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

Well it seems to be working now http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif I can send messages http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif  

 

took Slackers advice. I began to read the manual of LWIP and I realised that I forgot to close the pcb. But now it works http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif  

 

Thanks Slacker and niosIIuser... I owe you one!!! 

 

cheers, 

 

Danny
0 Kudos
Altera_Forum
Honored Contributor II
788 Views

Dear Friend, 

 

i am new in the filed of nios based socket programming though i am comfortable to write socket programming based on linux environment. 

 

as i write a code for receving and sending (UDP) over ethernet using lwip. but it generate lot's of error.....and i feel that it all belong to initialization of lwip. 

 

your problem reflect that you have a good experiance in writing lwip and UDP based socket programming....so please give me a sample code based on nios lwip. which send and receive data over ethernet using UDP 

 

kaushal
0 Kudos
Reply