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.
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().
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:
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