Lost in Sockets

oneguyindc wrote on Thursday, January 10, 2019:

My application sucessfully can send data via UDP sockets all day long
Ocassionally when there is a back to back attempt at sending more than one UDP packet - The attempt to create a FreeRTOS_socket fails -
/* Create the socket. */
xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_DGRAM,
FREERTOS_IPPROTO_UDP );
configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );

I am assuming that the sockets are dynamically create on the heap and more than one socket can be available at at time. As far as I can tell I have plenty of heap so I am at a loss what could cause the call to FreeRTOS_socket to fail.

Is there a config parameter that specifies how many UDP sockets can be active at one time

Lost in Sockets

rtel wrote on Thursday, January 10, 2019:

Looking at the FreeRTOS_socket() function (which you have the source
code for, and can step through in the debugger), it seems like the only
reasons FREERTOS_INVALID_SOCKET would get returned are:

  1. prvDetermineSocketSize() fails. You can look through this function
    to see the reasons this could fail - basically network is not ready or
    an input parameter is wrong.

  2. The socket structure cannot be created (is pvPortMallocSocket() just
    mapped to pvPortMalloc(), which is the default? If so, which memory
    allocater are you using? Heap_1, heap_2, etc.)

  3. xEventGroupCreate() fails, which again is just a memory allocation,
    so same as #2.

heinbali01 wrote on Thursday, January 10, 2019:

Hi Pedro, I am not sure what to answer here.

Remember that with configASSERT() defined, your application will halt if any of the calls to prvPortMalloc() fails, provided that you use one module out of portable/MemMang/heap_[1..5].c.

The number of UDP sockets that you create is under your control. So you have to put a limit to it.
I am not sure why you open more than one UDP socket? Do you advertise so many UDP ports?

Normally, when you create a UDP-server, you’d open just one UDP socket and use that for every request. You can handle each request and send a reply to the sender.

If you want, attach some code that illustrates what you are doing.

There is special UDP protection tough, which becomes active if you define e.g. in your FreeRTOSIPConfig.h:

#define ipconfigUDP_MAX_RX_PACKETS	4

With this example, each UDP socket will never store more that 4 received packets. For individual sockets, you can changes this limit with FreeRTOS_setsockopt() (FREERTOS_SO_UDP_MAX_RX_PACKETS);

This option can protect your device against malicious attacks from outside.