|
|
|
@ -755,14 +755,40 @@ UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, in
|
|
|
|
|
int
|
|
|
|
|
UdpSocket_receiveFrom(UdpSocket self, char** address, int maxAddrSize, uint8_t* msg, int msgSize)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in remoteAddress;
|
|
|
|
|
struct sockaddr_storage remoteAddress;
|
|
|
|
|
|
|
|
|
|
int result = recvfrom(self->fd, msg, msgSize, MSG_DONTWAIT, NULL, NULL);
|
|
|
|
|
int result = recvfrom(self->fd, msg, msgSize, MSG_DONTWAIT, (struct sockaddr*)&remoteAddress, sizeof(struct sockaddr_storage));
|
|
|
|
|
|
|
|
|
|
if (result == -1) {
|
|
|
|
|
if (DEBUG_SOCKET)
|
|
|
|
|
printf("SOCKET: failed to receive UDP message (errno=%i)\n", errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (address) {
|
|
|
|
|
bool isIPv6;
|
|
|
|
|
char addrString[INET6_ADDRSTRLEN + 7];
|
|
|
|
|
int port;
|
|
|
|
|
|
|
|
|
|
if (remoteAddress.ss_family == AF_INET) {
|
|
|
|
|
struct sockaddr_in* ipv4Addr = (struct sockaddr_in*) &remoteAddress;
|
|
|
|
|
port = ntohs(ipv4Addr->sin_port);
|
|
|
|
|
inet_ntop(AF_INET, &(ipv4Addr->sin_addr), addrString, INET_ADDRSTRLEN);
|
|
|
|
|
isIPv6 = false;
|
|
|
|
|
}
|
|
|
|
|
else if (remoteAddress.ss_family == AF_INET6) {
|
|
|
|
|
struct sockaddr_in6* ipv6Addr = (struct sockaddr_in6*) &remoteAddress;
|
|
|
|
|
port = ntohs(ipv6Addr->sin6_port);
|
|
|
|
|
inet_ntop(AF_INET6, &(ipv6Addr->sin6_addr), addrString, INET6_ADDRSTRLEN);
|
|
|
|
|
isIPv6 = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return NULL ;
|
|
|
|
|
|
|
|
|
|
if (isIPv6)
|
|
|
|
|
snprintf(*address, maxAddrSize, "[%s]:%i", addrString, port);
|
|
|
|
|
else
|
|
|
|
|
snprintf(*address, maxAddrSize, "%s:%i", addrString, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|