]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-common: Use if_nametoindex(3) to check if interface exists
authorMartin Schwenke <mschwenke@ddn.com>
Mon, 12 Jan 2026 07:05:23 +0000 (18:05 +1100)
committerVolker Lendecke <vl@samba.org>
Mon, 9 Feb 2026 11:17:41 +0000 (11:17 +0000)
This is much simpler than looping through the array.  It will also
find an interface by altname, which could be very useful.

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Volker Lendecke <vl@samba.org>
ctdb/common/system.c

index 05a95647233c4c5461ccf0f9feb1926605997e6c..170268c41fcc39514720fd28a19cd79b1b5841eb 100644 (file)
@@ -148,36 +148,18 @@ void ctdb_wait_for_process_to_exit(pid_t pid)
        }
 }
 
-#ifdef HAVE_IF_NAMEINDEX
+#ifdef HAVE_IF_NAMETOINDEX
 
 bool ctdb_sys_check_iface_exists(const char *iface)
 {
-       struct if_nameindex *ifnis, *ifni;
-       bool found = false;
+       unsigned int index = 0;
 
-       ifnis = if_nameindex();
-       if (ifnis == NULL) {
-               DBG_ERR("Failed to retrieve interface list\n");
-               return false;
-       }
-
-       for (ifni = ifnis;
-            ifni->if_index != 0 || ifni->if_name != NULL;
-            ifni++) {
-               int cmp = strcmp(iface, ifni->if_name);
-               if (cmp == 0) {
-                       found = true;
-                       goto done;
-               }
-       }
-
-done:
-       if_freenameindex(ifnis);
+       index = if_nametoindex(iface);
 
-       return found;
+       return index != 0;
 }
 
-#else /* HAVE_IF_NAMEINDEX */
+#else /* HAVE_IF_NAMETOINDEX */
 
 bool ctdb_sys_check_iface_exists(const char *iface)
 {
@@ -185,7 +167,7 @@ bool ctdb_sys_check_iface_exists(const char *iface)
        return true;
 }
 
-#endif /* HAVE_IF_NAMEINDEX */
+#endif /* HAVE_IF_NAMETOINDEX */
 
 #ifdef HAVE_PEERCRED