]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2817] use unsigned int for ifindex throughout
authorAndrei Pavel <andrei@isc.org>
Sat, 13 May 2023 12:06:35 +0000 (15:06 +0300)
committerAndrei Pavel <andrei@isc.org>
Sat, 13 May 2023 12:06:35 +0000 (15:06 +0300)
which is what if_nametoindex returns

13 files changed:
src/bin/dhcp4/tests/direct_client_unittest.cc
src/bin/dhcp6/tests/dhcp6_test_utils.h
src/bin/perfdhcp/perf_socket.h
src/lib/dhcp/iface_mgr.cc
src/lib/dhcp/iface_mgr.h
src/lib/dhcp/pkt.cc
src/lib/dhcp/pkt.h
src/lib/dhcp/pkt_filter_inet6.cc
src/lib/dhcp/tests/iface_mgr_test_config.cc
src/lib/dhcp/tests/iface_mgr_test_config.h
src/lib/dhcp/tests/iface_mgr_unittest.cc
src/lib/dhcp/tests/pkt_filter6_test_utils.h
src/lib/dhcp/tests/pkt_filter_test_utils.h

index 3d98e4ec7e68a160f8f643878da3f3fadef48d5e..b6145974a1119ec9273aec89b9ca45422710a239 100644 (file)
@@ -83,7 +83,7 @@ public:
     /// @return Generated message.
     Pkt4Ptr createClientMessage(const uint16_t msg_type,
                                 const std::string& iface,
-                                const uint32_t ifindex);
+                                const unsigned int ifindex);
 
     /// @brief Creates simple message from a client.
     ///
@@ -102,7 +102,7 @@ public:
     /// @return Configured and parsed message.
     Pkt4Ptr createClientMessage(const Pkt4Ptr &msg,
                                 const std::string& iface,
-                                const uint32_t ifindex);
+                                const unsigned int ifindex);
 
     /// @brief This test checks that the message from directly connected client
     /// is processed and that client is offered IPv4 address from the subnet
@@ -196,7 +196,7 @@ DirectClientTest::configureTwoSubnets(const std::string& prefix1,
 Pkt4Ptr
 DirectClientTest::createClientMessage(const uint16_t msg_type,
                                       const std::string& iface,
-                                      const uint32_t ifindex) {
+                                      const unsigned int ifindex) {
     // Create a source packet.
     Pkt4Ptr msg = Pkt4Ptr(new Pkt4(msg_type, 1234));
     return (createClientMessage(msg, iface, ifindex));
@@ -206,7 +206,7 @@ DirectClientTest::createClientMessage(const uint16_t msg_type,
 Pkt4Ptr
 DirectClientTest::createClientMessage(const Pkt4Ptr& msg,
                                       const std::string& iface,
-                                      const uint32_t ifindex) {
+                                      const unsigned int ifindex) {
     msg->setRemoteAddr(IOAddress("255.255.255.255"));
     msg->addOption(generateClientId());
     msg->setIface(iface);
index 93aadb968d711eb790779ab55c797b38ae7a4bff..35a11280bd5b627ace0bd8daaf513430dccb5c8b 100644 (file)
@@ -4,7 +4,7 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
-/// @file   dhcp6_test_utils.h
+/// @file dhcp6_test_utils.h
 ///
 /// @brief This file contains utility classes used for DHCPv6 server testing
 
@@ -519,7 +519,7 @@ public:
     std::string valid_iface_;
 
     // Index of a valid network interface
-    uint32_t valid_ifindex_;
+    unsigned int valid_ifindex_;
 };
 
 // We need to pass one reference to the Dhcp6Client, which is defined in
index 56a8551a273bc0bcb4a8b65e7835b7b99342bbda..e0c45734b8958fe1960f05a5273f7c26c145e1a2 100644 (file)
@@ -26,7 +26,7 @@ namespace perfdhcp {
 class BasePerfSocket : public dhcp::SocketInfo {
 public:
     /// Interface index.
-    uint16_t ifindex_;
+    unsigned int ifindex_;
 
     /// \brief Default constructor of BasePerfSocket.
     BasePerfSocket() :
index ebb5bf0a9be1686a3057be4b0cadadc3fc4a91a8..6bf1a0853f5c9438aafd9cffcda0bfb6e5bc3a3e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -832,7 +832,7 @@ IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
 }
 
 IfacePtr
-IfaceCollection::getIface(uint32_t ifindex) {
+IfaceCollection::getIface(const unsigned int ifindex) {
     return (getIfaceInternal(ifindex, MultiThreadingMgr::instance().getMode()));
 }
 
@@ -843,7 +843,10 @@ IfaceCollection::getIface(const std::string& ifname) {
 }
 
 IfacePtr
-IfaceCollection::getIfaceInternal(uint32_t ifindex, bool need_lock) {
+IfaceCollection::getIfaceInternal(const unsigned int ifindex, const bool need_lock) {
+    if (ifindex == UNSET_IFINDEX) {
+        isc_throw(BadValue, "interface index was not set");
+    }
     if (need_lock) {
         lock_guard<mutex> lock(mutex_);
         if (cache_ && (cache_->getIndex() == ifindex)) {
@@ -864,14 +867,13 @@ IfaceCollection::getIfaceInternal(uint32_t ifindex, bool need_lock) {
         cache_ = *it;
         return (cache_);
     } else {
-        lock_guard<mutex> lock(mutex_);
         cache_ = *it;
         return (cache_);
     }
 }
 
 IfacePtr
-IfaceCollection::getIfaceInternal(const std::string& ifname, bool need_lock) {
+IfaceCollection::getIfaceInternal(const std::string& ifname, const bool need_lock) {
     if (need_lock) {
         lock_guard<mutex> lock(mutex_);
         if (cache_ && (cache_->getName() == ifname)) {
@@ -892,17 +894,13 @@ IfaceCollection::getIfaceInternal(const std::string& ifname, bool need_lock) {
         cache_ = *it;
         return (cache_);
     } else {
-        lock_guard<mutex> lock(mutex_);
         cache_ = *it;
         return (cache_);
     }
 }
 
 IfacePtr
-IfaceMgr::getIface(int ifindex) {
-    if ((ifindex < 0) || (ifindex > std::numeric_limits<int32_t>::max())) {
-        return (IfacePtr()); // out of range
-    }
+IfaceMgr::getIface(const unsigned int ifindex) {
     return (ifaces_.getIface(ifindex));
 }
 
@@ -1210,7 +1208,7 @@ Pkt4Ptr IfaceMgr::receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec /
         return (Pkt4Ptr());
     } else if (result < 0) {
         // In most cases we would like to know whether select() returned
-        // an error because of a signal being received  or for some other
+        // an error because of a signal being received or for some other
         // reason. This is because DHCP servers use signals to trigger
         // certain actions, like reconfiguration or graceful shutdown.
         // By catching a dedicated exception the caller will know if the
@@ -1329,7 +1327,7 @@ Pkt4Ptr IfaceMgr::receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec /*
 
     } else if (result < 0) {
         // In most cases we would like to know whether select() returned
-        // an error because of a signal being received  or for some other
+        // an error because of a signal being received or for some other
         // reason. This is because DHCP servers use signals to trigger
         // certain actions, like reconfiguration or graceful shutdown.
         // By catching a dedicated exception the caller will know if the
@@ -1476,7 +1474,7 @@ IfaceMgr::receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ )
 
     } else if (result < 0) {
         // In most cases we would like to know whether select() returned
-        // an error because of a signal being received  or for some other
+        // an error because of a signal being received or for some other
         // reason. This is because DHCP servers use signals to trigger
         // certain actions, like reconfiguration or graceful shutdown.
         // By catching a dedicated exception the caller will know if the
@@ -1600,7 +1598,7 @@ IfaceMgr::receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */
         return (Pkt6Ptr());
     } else if (result < 0) {
         // In most cases we would like to know whether select() returned
-        // an error because of a signal being received  or for some other
+        // an error because of a signal being received or for some other
         // reason. This is because DHCP servers use signals to trigger
         // certain actions, like reconfiguration or graceful shutdown.
         // By catching a dedicated exception the caller will know if the
index c0ee4f030810a2d725e88a6d5f63d45c6d4da999..bccfeee01cb36d8d5d96a18a22bb67f10b51dd29 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -216,7 +216,7 @@ public:
     /// @brief Returns interface index.
     ///
     /// @return interface index
-    int getIndex() const { return ifindex_; }
+    unsigned int getIndex() const { return ifindex_; }
 
     /// @brief Returns interface name.
     ///
@@ -416,7 +416,7 @@ protected:
     std::string name_;
 
     /// Interface index (a value that uniquely identifies an interface).
-    int ifindex_;
+    unsigned int ifindex_;
 
     /// List of assigned addresses.
     AddressCollection addrs_;
@@ -511,7 +511,7 @@ public:
             boost::multi_index::hashed_unique<
                 // Use the interface index as the key.
                 boost::multi_index::const_mem_fun<
-                    Iface, int, &Iface::getIndex
+                    Iface, unsigned int, &Iface::getIndex
                 >
             >,
             // Start definition of index #2.
@@ -571,7 +571,7 @@ public:
     ///
     /// @param ifindex The index of the interface to find.
     /// @return The interface with the index or null.
-    IfacePtr getIface(uint32_t ifindex);
+    IfacePtr getIface(const unsigned int ifindex);
 
     /// @brief Lookup by interface name.
     ///
@@ -585,7 +585,7 @@ private:
     /// @param ifindex The index of the interface to find.
     /// @param need_lock True when the cache operation needs to hold the mutex.
     /// @return The interface with the index or null.
-    IfacePtr getIfaceInternal(uint32_t ifindex, bool need_lock);
+    IfacePtr getIfaceInternal(const unsigned int ifindex, const bool need_lock);
 
     /// @brief Lookup by interface name.
     ///
@@ -594,7 +594,7 @@ private:
     /// @param ifname The name of the interface to find.
     /// @param need_lock True when the cache operation needs to hold the mutex.
     /// @return The interface with the name or null.
-    IfacePtr getIfaceInternal(const std::string& ifname, bool need_lock);
+    IfacePtr getIfaceInternal(const std::string& ifname, const bool need_lock);
 
     /// @brief The mutex for protecting the cache from concurrent
     /// access from packet processing threads.
@@ -749,7 +749,7 @@ public:
     /// @return interface with requested index (or null if no such
     ///         interface is present)
     ///
-    IfacePtr getIface(int ifindex);
+    IfacePtr getIface(const unsigned int ifindex);
 
     /// @brief Returns interface with specified interface name
     ///
index fdd1e8977e076dd66e6a2931c34b7a64353582cc..6f2555f352d6f1897691072beb62f5694e6a0a69 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -17,7 +17,7 @@ namespace dhcp {
 Pkt::Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
          const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
          uint16_t remote_port)
-    : transid_(transid), iface_(""), ifindex_(-1), local_addr_(local_addr),
+    : transid_(transid), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
       remote_addr_(remote_addr), local_port_(local_port),
       remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
 }
@@ -25,7 +25,7 @@ Pkt::Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
 Pkt::Pkt(const uint8_t* buf, uint32_t len, const isc::asiolink::IOAddress& local_addr,
          const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
          uint16_t remote_port)
-    : transid_(0), iface_(""), ifindex_(-1), local_addr_(local_addr),
+    : transid_(0), iface_(""), ifindex_(UNSET_IFINDEX), local_addr_(local_addr),
       remote_addr_(remote_addr), local_port_(local_port),
       remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
     if (len != 0) {
index 441fd395988ef40806c4c1f24a37f480b01f423f..3a2a35a30195341c29ee442b42759b2365463fc1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/shared_ptr.hpp>
 
+#include <limits>
 #include <utility>
 
 namespace isc {
 
 namespace dhcp {
 
+/// @brief A value used to signal that the interface index was not set.
+/// That means that more than UNSET_IFINDEX interfaces are not supported.
+/// That's fine, since it would have overflowed with UNSET_IFINDEX + 1 anyway.
+constexpr unsigned int UNSET_IFINDEX = std::numeric_limits<unsigned int>::max();
+
 /// @brief RAII object enabling copying options retrieved from the
 /// packet.
 ///
@@ -524,13 +530,13 @@ public:
     /// @brief Sets interface index.
     ///
     /// @param ifindex specifies interface index.
-    void setIndex(int ifindex) {
+    void setIndex(const unsigned int ifindex) {
         ifindex_ = ifindex;
     }
 
     /// @brief Resets interface index to negative value.
     void resetIndex() {
-        ifindex_ = -1;
+        ifindex_ = UNSET_IFINDEX;
     }
 
     /// @brief Returns interface index.
@@ -544,7 +550,7 @@ public:
     ///
     /// @return true if interface index set, false otherwise.
     bool indexSet() const {
-        return (ifindex_ >= 0);
+        return (ifindex_ != UNSET_IFINDEX);
     }
 
     /// @brief Returns interface name.
@@ -789,7 +795,7 @@ protected:
     /// Each network interface has assigned an unique ifindex.
     /// It is a functional equivalent of a name, but sometimes more useful, e.g.
     /// when using odd systems that allow spaces in interface names.
-    int64_t ifindex_;
+    unsigned int ifindex_;
 
     /// @brief Local IP (v4 or v6) address.
     ///
index 20d682cfbefbe0f26edb41354c539ebb70a114c3..e8e7ed09505cf92a4f977774c8b9e8a986e37ada 100644 (file)
@@ -182,7 +182,7 @@ PktFilterInet6::receive(const SocketInfo& socket_info) {
     struct in6_addr to_addr;
     memset(&to_addr, 0, sizeof(to_addr));
 
-    int ifindex = -1;
+    unsigned int ifindex = UNSET_IFINDEX;
     if (result >= 0) {
         struct in6_pktinfo* pktinfo = NULL;
 
index bd5d20778f1908d36a884ceeb6bebc73d141545f..a36786734c7250f9babdcc6cb1ee27c1860c3471 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -67,12 +67,12 @@ IfaceMgrTestConfig::addIface(const IfacePtr& iface) {
 }
 
 void
-IfaceMgrTestConfig::addIface(const std::string& name, const int ifindex) {
+IfaceMgrTestConfig::addIface(const std::string& name, const unsigned int ifindex) {
     IfaceMgr::instance().addInterface(createIface(name, ifindex));
 }
 
 IfacePtr
-IfaceMgrTestConfig::createIface(const std::string &name, const int ifindex) {
+IfaceMgrTestConfig::createIface(const std::string &name, const unsigned int ifindex) {
     IfacePtr iface(new Iface(name, ifindex));
     if (name == "lo") {
         iface->flag_loopback_ = true;
index 26167e8f2d3de13a25d74282aaef6a6877809436..2b13a996e8957d0e2323aeac0b46553fc6bc287b 100644 (file)
@@ -177,7 +177,7 @@ public:
     ///
     /// @param name Name of the new interface.
     /// @param ifindex Index for a new interface.
-    void addIface(const std::string& name, const int ifindex);
+    void addIface(const std::string& name, const unsigned int ifindex);
 
     /// @brief Create an object representing interface.
     ///
@@ -198,7 +198,7 @@ public:
     /// @param ifindex An index of the interface to be created.
     ///
     /// @return An object representing interface.
-    static IfacePtr createIface(const std::string& name, const int ifindex);
+    static IfacePtr createIface(const std::string& name, const unsigned int ifindex);
 
     /// @brief Creates a default (example) set of fake interfaces.
     void createIfaces();
index 41611fa9b1e4272e3c00acba587ae42f64ed4f13..2761319b30f1b9a7e91f2cf3c9f08a19a2f5693b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -271,7 +271,7 @@ public:
     /// @param ifindex An index of the interface to be created.
     ///
     /// @return An object representing interface.
-    static IfacePtr createIface(const std::string& name, const int ifindex) {
+    static IfacePtr createIface(const std::string& name, const unsigned int ifindex) {
         IfacePtr iface(new Iface(name, ifindex));
         if (name == "lo") {
             iface->flag_loopback_ = true;
@@ -941,6 +941,46 @@ TEST_F(IfaceMgrTest, ifaceClass) {
     EXPECT_EQ(66666, iface->getIndex());
 }
 
+// This test checks the getIface by index method.
+TEST_F(IfaceMgrTest, getIfaceByIndex) {
+    NakedIfaceMgr ifacemgr;
+
+    // Create a set of fake interfaces. At the same time, remove the actual
+    // interfaces that have been detected by the IfaceMgr.
+    ifacemgr.createIfaces();
+
+    // Getting an unset index should throw.
+    EXPECT_THROW_MSG(ifacemgr.getIface(UNSET_IFINDEX), BadValue, "interface index was not set");
+
+    // Historically -1 was used as an unset value. Let's also check that it throws in case we didn't
+    // migrate all code to UNSET_IFINDEX and in case the values diverge.
+    EXPECT_THROW_MSG(ifacemgr.getIface(-1), BadValue, "interface index was not set");
+
+    // Get the first interface defined.
+    IfacePtr iface(ifacemgr.getIface(0));
+    ASSERT_TRUE(iface);
+    EXPECT_EQ("lo", iface->getName());
+
+    // Attemt to get an undefined interface.
+    iface = ifacemgr.getIface(3);
+    EXPECT_FALSE(iface);
+
+    // Check that we can go past INT_MAX.
+    unsigned int int_max(numeric_limits<int>::max());
+    iface = ifacemgr.getIface(int_max);
+    EXPECT_FALSE(iface);
+    iface = ifacemgr.createIface("wlan0", int_max);
+    ifacemgr.addInterface(iface);
+    iface = ifacemgr.getIface(int_max);
+    EXPECT_TRUE(iface);
+    iface = ifacemgr.getIface(int_max + 1);
+    EXPECT_FALSE(iface);
+    iface = ifacemgr.createIface("wlan1", int_max + 1);
+    ifacemgr.addInterface(iface);
+    iface = ifacemgr.getIface(int_max + 1);
+    EXPECT_TRUE(iface);
+}
+
 // This test checks the getIface by packet method.
 TEST_F(IfaceMgrTest, getIfaceByPkt) {
     NakedIfaceMgr ifacemgr;
@@ -991,9 +1031,9 @@ TEST_F(IfaceMgrTest, getIfaceByPkt) {
     EXPECT_FALSE(pkt6->indexSet());
 
     // Test that you can also reset the index via setIndex().
-    pkt4->setIndex(-1);
+    pkt4->setIndex(UNSET_IFINDEX);
     EXPECT_FALSE(pkt4->indexSet());
-    pkt6->setIndex(-1);
+    pkt6->setIndex(UNSET_IFINDEX);
     EXPECT_FALSE(pkt6->indexSet());
 }
 
index 398b3a1ac11048d4c8b19ff96e74334715d7e8d6..dc683cf6ce808df3e30c082a1645da5c04304b4d 100644 (file)
@@ -74,7 +74,7 @@ public:
     void testRcvdMessage(const Pkt6Ptr& rcvd_msg) const;
 
     std::string ifname_;   ///< Loopback interface name.
-    uint16_t ifindex_;     ///< Loopback interface index.
+    unsigned int ifindex_; ///< Loopback interface index.
     uint16_t port_;        ///< A port number used for the test.
     isc::dhcp::SocketInfo sock_info_; ///< A structure holding socket info.
     int send_msg_sock_;    ///< Holds a descriptor of the socket used by
index 253df184d245181cdbc3aca5c396232b2e1f287e..c7ebb263fc983a5ca5b60f4d3ec7f9a432a71d90 100644 (file)
@@ -83,7 +83,7 @@ public:
     void testRcvdMessageAddressPort(const Pkt4Ptr& rcvd_msg) const;
 
     std::string ifname_;   ///< Loopback interface name
-    uint16_t ifindex_;     ///< Loopback interface index.
+    unsigned int ifindex_; ///< Loopback interface index.
     uint16_t port_;        ///< A port number used for the test.
     isc::dhcp::SocketInfo sock_info_; ///< A structure holding socket info.
     int send_msg_sock_;    ///< Holds a descriptor of the socket used by