From e8310b680cfbfe830c63ccca5eaf6f89111f9393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Tue, 7 May 2024 21:40:35 +0200 Subject: [PATCH] network: Add utility function to convert IP address with port to a socket address --- src/network/utils/address-utils.cc | 22 ++++++++++++++++++++++ src/network/utils/address-utils.h | 11 +++++++++++ 2 files changed, 33 insertions(+) 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