network: Add utility function to convert IP address with port to a socket address
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user