Looking for a similar function to getpeername() for win32 simulator

kat123 wrote on Thursday, February 01, 2018:

I’ve got some C++ code and I’m trying to replicate the functionality with FreeRTOS + TCP on the Windows simulator before porting it to an STM32F4.

My original code broadcasts a UDP message and then uses getpeername() to find the IP address of devices on a local network that respond fore creating a TCP socket and connecting to the devices.

I can’t find anything that performs the same function or a combination of things that perfom the same function in the FreeRTOS API.

FreeRTOS_getremoteaddress() seems to do something similar but is only for TCP connections.

Is there a way to do this with FreeRTOS + TCP?

heinbali01 wrote on Thursday, February 01, 2018:

Hi Kathryn, when this is the BSD function:

int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

…this is the +TCP variant:

/* Return the remote address and IP port. */
BaseType_t FreeRTOS_GetRemoteAddress( Socket_t xSocket, struct freertos_sockaddr *pxAddress );

And yes, we should have named it FreeRTOS_getpeername()

FreeRTOS_getremoteaddress() seems to do something similar
but is only for TCP connections.

That is true, because a UDP socket in FreeRTOS+TCP can not connect() to a remote socket. A UDP socket can send data to any address at any time, using sendto().

kat123 wrote on Thursday, February 01, 2018:

Is there anyway to get an IP address from a UDP packet?

So if I receive a UDP packet from a device, can I get the IP of the device that sent it?

Currently I receive a UDP packet
ByteReceived[i] = recvfrom(s, recvBuffer[i], 1024, 0, (SOCKADDR *)&SenderAddr[i], &SenderAddrSize);

And then
getpeername(s, (SOCKADDR *)&SenderAddr[i], &SenderAddrSize);

Then I can use the IP from the sender address and create a TCP socket

heinbali01 wrote on Thursday, February 01, 2018:

The BSD function is :

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);

FreeRTOS+TCP has a similar definition:

int32_t FreeRTOS_recvfrom( Socket_t xSocket, void *pvBuffer, size_t xBufferLength, BaseType_t xFlags, struct freertos_sockaddr *pxSourceAddress, socklen_t *pxSourceAddressLength )

In this case, you don’t need to call getpeername(), because you already have that address in src_addr or pxSourceAddress.

When replying to the sender, you can safely use that address. No need to call getpeername().

UDP is a connectionless protocol, there is no fixed peer. There has been some attempt to do as if a UDP socket can be connected. Here some comments on that attempt:

https://stackoverflow.com/questions/9741392/can-you-bind-and-connect-both-ends-of-a-udp-connection

kenchang1 wrote on Thursday, February 01, 2018:

I think Kathryn is looking for a macro or function to extract the IP-address from the struct member freertos_sockaddr.sin_addr in pxSourceAddress.
You can modify FreeRTOS_GetRemoteAddress() for UDP, right?
Edit: never mind, it makes no sense to extract the IP address, you can just reuse struct freertos_sockaddr *pxSourceAddress