I have a UDP server that should serve clients on both IPV4 and IPV6 addresses. I created the only IPV6 socket to serve IPV4 and IPV6 clients.
The server stores the IP address of the client in the very first message. If it is an IPV4 client, it is stored as an IPV4 address, and if it is an IPV6 client, the server saves it as an IPV6 address. For all future communications, he checks whether this client is already known (saved), and then acts accordingly. To compare the client address with the saved address, I do memcmp based on the family type (AF_INET and AF_INET6).
When communicating with an IPV6 client, the system works fine, but when communicating with an IPV4 client, the system behaves as if it never knew the client. During debugging, I discovered that due to IPV6, the IPv4 client's Socket IPAddresss type is accepted as IPV6, mapped to the IPV4 address with the family installed on IPV6. To solve this problem, I need to compare between the stored IPV4 address and the IPV6 address. For this, I use the sin_addr.s_addr structure of the IPV4 and sin6_addr.in6_u.u6_addr32 structures of the IPV6. Below is a snippet of code.
ipv6_clientdata = (const struct sockaddr_in6 *)&sockStor;
ipv4_storeddata = (const struct sockaddr_in *)&(_stData[index].clientaddr);
if( (ipv6_clientdata->sin6_port == ipv4_storeddata->sin_port) &&
(ipv6_clientdata->sin6_addr.in6_u.u6_addr32[3] == ipv4_storeddata->sin_addr.s_addr)
)
{
addrfound = true;
}
I would like to know if this method is a suitable solution for comparing an IPV6 IPV4 address with an IPV4 address or another better way.
source
share