network: Add utility function to convert IP address with port to a socket address

This commit is contained in:
Sébastien Deronne
2024-05-07 21:40:35 +02:00
parent fe6304eecf
commit e8310b680c
2 changed files with 33 additions and 0 deletions

View File

@@ -156,6 +156,28 @@ IsMulticast(const Address& ad)
return false;
}
Address
ConvertToSocketAddress(const Address& address, uint16_t port)
{
NS_LOG_FUNCTION(address << port);
Address convertedAddress;
if (Ipv4Address::IsMatchingType(address))
{
convertedAddress = InetSocketAddress(Ipv4Address::ConvertFrom(address), port);
NS_LOG_DEBUG("Address converted to " << convertedAddress);
}
else if (Ipv6Address::IsMatchingType(address))
{
convertedAddress = Inet6SocketAddress(Ipv6Address::ConvertFrom(address), port);
NS_LOG_DEBUG("Address converted to " << convertedAddress);
}
else
{
NS_FATAL_ERROR("This function should be called for an IPv4 or an IPv6 address");
}
return convertedAddress;
}
} // namespace addressUtils
} // namespace ns3

View File

@@ -110,8 +110,19 @@ namespace addressUtils
/**
* \brief Address family-independent test for a multicast address
* \param ad the address to test
* \return true if the address is a multicast address, false otherwise
*/
bool IsMulticast(const Address& ad);
/**
* \brief Convert IPv4/IPv6 address with port to a socket address
* \param address the address to convert
* \param port the port
* \return the corresponding socket address
*/
Address ConvertToSocketAddress(const Address& address, uint16_t port);
}; // namespace addressUtils
}; // namespace ns3