From a3ec4ecfbeeb88267f6a69d07959e004e2b0888e Mon Sep 17 00:00:00 2001 From: Tommaso Pecorella Date: Fri, 14 Mar 2025 23:06:41 +0100 Subject: [PATCH] network: add a function to detect IPv4 APIPA addresses --- CHANGES.md | 2 ++ src/network/utils/ipv4-address.cc | 8 ++++++++ src/network/utils/ipv4-address.h | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 47010f5ff..ed41c5257 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,8 @@ This file is a best-effort approach to solving this issue; we will do our best b ### New API +* (network) Added a function to detect IPv4 APIPA addresses (169.254.0.0/16). + ### Changes to existing API * (internet-apps) Added a parameter to the RADVD helper to announce a prefix without the autoconfiguration flag. diff --git a/src/network/utils/ipv4-address.cc b/src/network/utils/ipv4-address.cc index 858a5eb78..5928c05c5 100644 --- a/src/network/utils/ipv4-address.cc +++ b/src/network/utils/ipv4-address.cc @@ -281,6 +281,14 @@ Ipv4Address::IsLocalMulticast() const return (m_address & 0xffffff00) == 0xe0000000; } +bool +Ipv4Address::IsLinkLocal() const +{ + NS_LOG_FUNCTION(this); + // Link-Local address is 169.254.0.0/16 + return (m_address & 0xffff0000) == 0xa9fe0000; +} + void Ipv4Address::Serialize(uint8_t buf[4]) const { diff --git a/src/network/utils/ipv4-address.h b/src/network/utils/ipv4-address.h index 273004e07..7bff02d93 100644 --- a/src/network/utils/ipv4-address.h +++ b/src/network/utils/ipv4-address.h @@ -112,6 +112,14 @@ class Ipv4Address * @return true only if address is in local multicast address scope, 224.0.0.0/24 */ bool IsLocalMulticast() const; + /** + * @brief If the IPv4 address is an APIPA address (169.254/16). + * + * The Automatic Private IP Address is described in \RFC{3927} + * + * @return true if the address is link-local, false otherwise + */ + bool IsLinkLocal() const; /** * @brief Combine this address with a network mask *