diff --git a/src/network/utils/address-utils.cc b/src/network/utils/address-utils.cc index a111dedd9..220fd22c1 100644 --- a/src/network/utils/address-utils.cc +++ b/src/network/utils/address-utils.cc @@ -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 diff --git a/src/network/utils/address-utils.h b/src/network/utils/address-utils.h index 81cc6e165..8401d4a05 100644 --- a/src/network/utils/address-utils.h +++ b/src/network/utils/address-utils.h @@ -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