Looking for a similar function to getpeername() for win32 simulator
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?
Looking for a similar function to getpeername() for win32 simulator
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()
.
Looking for a similar function to getpeername() for win32 simulator
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
Looking for a similar function to getpeername() for win32 simulator
The BSD function is :
~~~
ssizet recvfrom(int sockfd, void *buf, sizet len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
~~~
FreeRTOS+TCP has a similar definition:
~~~
int32t FreeRTOSrecvfrom( Sockett xSocket, void *pvBuffer, sizet xBufferLength, BaseTypet xFlags, struct freertossockaddr *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
Looking for a similar function to getpeername() for win32 simulator
I think Kathryn is looking for a macro or function to extract the IP-address from the struct member freertossockaddr.sinaddr in pxSourceAddress.
You can modify FreeRTOSGetRemoteAddress() for UDP, right?
Edit: never mind, it makes no sense to extract the IP address, you can just reuse struct freertossockaddr *pxSourceAddress