lwip socket udp

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

lwip socket udp

Post by gvandenbosch »

Hello,

I have lwip running on my board, I can successfully ping the board and also tested the supplied webserver (web.c) and this is working perfect.

Currently I am trying to open a UDP socket the following way using "lwip/sockets.h":

Code: Select all

int iBytesWritten = 0;
struct sockaddr_in sa;

memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_len = sizeof(sa);
sa.sin_addr.s_addr = inet_addr("192.168.82.20");
sa.sin_port = htons(5060);

int sockfd = lwip_socket(AF_INET, SOCK_DGRAM, 0);
iBytesWritten = lwip_sendto(sockfd, (char*)"hallo", (int)5, 0, (struct sockaddr*)&sa, sizeof(sa));


The ip for the board is "192.168.82.160" and target host is "192.168.82.20" .

The 'iBytesWritten' variable nicely holds 5 bytes written when it returns, but no connection to my socket test program on the target host, also with wireshark no visible traffic.

Does anyone have an idea why this wouldn't work properly?

Cheers,
Gerard
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: lwip socket udp

Post by Giovanni »

Never used UDP with lwIP.

Moving to general support where this belongs.

Giovanni
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip socket udp

Post by gvandenbosch »

Hello,

Sorry for posting in the wrong subforum, I overlooked this subforum.

I have tried a different approach with the netconn api of lwip.

Code: Select all

#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "lwip/ip_addr.h"

struct netconn *conn;
struct netbuf *buf;
struct ip_addr addr;
char* data;

char msg[5] = { 'H','E','L','L','O' };

conn = netconn_new(NETCONN_UDP);

addr.addr = htonl(0xC0A85214); //192.168.82.20
netconn_connect(conn, &addr ,5060);

buf     =  netbuf_new();
data    =  netbuf_alloc(buf, sizeof(msg));
memcpy (data, msg, sizeof (msg));

netconn_send(conn, buf);
netbuf_delete(buf);
netconn_disconnect(conn);


This doesn't work as well, however if I change NETCONN_UDP to NETCONN_TCP and setup test TCP server on same port, it does connect.
Also before the TCP traffic can see the ARP request in order to resolve the IP address, but with UDP it is completely silent.

UDP is enabled in my configuration file and I am a bit clueless at the moment.

Cheers,
Gerard
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip socket udp

Post by gvandenbosch »

Hello,

I located the problem and have a workaround at the moment.

It seems to need some time to initialize properly. When I initialize LWIP thread, I wait for a Gratuitous ARP request in wireshark.
This seems to indicate that everything is up.

After that I set a boolean to true with my debugger which then executes the code to create the UDP sockets.
Then the code I hard in first post nicely works. First see the ARP handshake and then the UDP message.

Weird thing is that TCP worked out of the box and this doesn't.

Cheers,
Gerard
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: lwip socket udp

Post by Giovanni »

Do a search in the forum, just in case, may be somebody already did this.

BTW, better use Google than the forum search function.

Giovanni
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip socket udp

Post by gvandenbosch »

I continued on this and it seems that the problem is deeper and inside the MAC layer of the LPC43xx driver.

At the point the total size of an UDP packet gets bigger than 244 bytes which is an actual 202 byte packet + 42 byte added UDP header by LWIP.
It won't send anymore, it still comes in the "mac_lld_write_transmit_descriptor" function and I can see the actual packet it wants to send no matter the size.

But when the total size is greater than the 244 bytes it won't come out of the ethernet port.

Testing with quite a simple snippet:

Code: Select all

int iBytesWritten = 0;
struct sockaddr_in sa;

memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_len = sizeof(sa);
sa.sin_addr.s_addr = inet_addr("192.168.82.20");
sa.sin_port = htons(5060);

char acTest[250];
int i = 0;
for(i = 0; i < 250; i++)
{
   if(i%2)
      acTest[i] = 0x06;
   else
      acTest[i] = 0x09;
}

int sockfd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

iBytesWritten = lwip_sendto(sockfd, (char*)acTest, (int)202, 0, (struct sockaddr*)&sa, sizeof(sa));
lwip_close(sockfd);


If I make my test greater than 202 bytes (42 byte UDP header added by lwip) it fails.
Searched around in these forums and didn't find similar problem yet.
Still trying to find the cause of this...
Post Reply