- socket-linux: get source IP address in UdpSocket_receiveFrom

pull/291/head
Michael Zillgith 5 years ago
parent 474a482766
commit 8bf76601ea

@ -151,8 +151,19 @@ UdpSocket_bind(UdpSocket self, const char* address, int port);
PAL_API bool PAL_API bool
UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, int msgSize); UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, int msgSize);
/**
* \brief Receive data from UDP socket (store data and (optionally) the IP address of the sender
*
* \param self UDP socket instance
* \param address (optional) buffer to store the IP address as string
* \param maxAddrSize (optional) size of the provided buffer to store the IP address
* \param msg buffer to store the UDP message data
* \param msgSize the maximum size of the message to receive
*
* \return number of received bytes or -1 in case of an error
*/
PAL_API int PAL_API int
UdpSocket_receiveFrom(UdpSocket self, char** address, int maxAddrSize, uint8_t* msg, int msgSize); UdpSocket_receiveFrom(UdpSocket self, char* address, int maxAddrSize, uint8_t* msg, int msgSize);
PAL_API void PAL_API void

@ -755,14 +755,40 @@ UdpSocket_sendTo(UdpSocket self, const char* address, int port, uint8_t* msg, in
int int
UdpSocket_receiveFrom(UdpSocket self, char** address, int maxAddrSize, uint8_t* msg, int msgSize) 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 (result == -1) {
if (DEBUG_SOCKET) if (DEBUG_SOCKET)
printf("SOCKET: failed to receive UDP message (errno=%i)\n", errno); 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; return result;
} }

@ -1,7 +1,7 @@
/* /*
* time.c * time.c
* *
* Copyright 2013, 2014 Michael Zillgith * Copyright 2013-2021 Michael Zillgith
* *
* This file is part of libIEC61850. * This file is part of libIEC61850.
* *

Loading…
Cancel
Save