[libc-commits] [libc] [libc] Implement if_nametoindex and if_indextoname (PR #206082)

via libc-commits libc-commits at lists.llvm.org
Fri Jun 26 08:13:58 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Pavel Labath (labath)

<details>
<summary>Changes</summary>

This patch implements if_nametoindex and if_indextoname for Linux.

Both functions work by creating a temporary AF_UNIX socket and issuing ioctl calls (SIOCGIFINDEX and SIOCGIFNAME, respectively).

The main implementation details:
- for if_nametoindex, I check that the interface name fits within IF_NAMESIZE before issuing the ioctl
- for if_indextoname, if the kernel returns ENODEV for an unknown index, I map it to ENXIO to comply with POSIX requirements
- added the definition of struct ifreq to support these operations. Similar to the other net structure definitions, I'm using an anonymous union to avoid #defining members.

Assisted by Gemini.

---

Patch is 24.45 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/206082.diff


27 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+4) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+4) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+4) 
- (modified) libc/hdr/CMakeLists.txt (+9) 
- (added) libc/hdr/net_if_macros.h (+22) 
- (modified) libc/hdr/types/CMakeLists.txt (+9) 
- (added) libc/hdr/types/struct_ifreq.h (+26) 
- (modified) libc/include/CMakeLists.txt (+12) 
- (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6) 
- (modified) libc/include/llvm-libc-macros/linux/CMakeLists.txt (+6) 
- (added) libc/include/llvm-libc-macros/linux/net-if-macros.h (+19) 
- (added) libc/include/llvm-libc-macros/net-if-macros.h (+21) 
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1) 
- (added) libc/include/llvm-libc-types/struct_ifreq.h (+40) 
- (added) libc/include/net/if.yaml (+24) 
- (modified) libc/src/CMakeLists.txt (+1) 
- (added) libc/src/net/CMakeLists.txt (+17) 
- (added) libc/src/net/if_indextoname.h (+25) 
- (added) libc/src/net/if_nametoindex.h (+25) 
- (added) libc/src/net/linux/CMakeLists.txt (+44) 
- (added) libc/src/net/linux/if_indextoname.cpp (+68) 
- (added) libc/src/net/linux/if_nametoindex.cpp (+62) 
- (modified) libc/test/src/CMakeLists.txt (+1) 
- (added) libc/test/src/net/CMakeLists.txt (+4) 
- (added) libc/test/src/net/linux/CMakeLists.txt (+27) 
- (added) libc/test/src/net/linux/if_indextoname_test.cpp (+51) 
- (added) libc/test/src/net/linux/if_nametoindex_test.cpp (+42) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 570f7666ba67b..8f0fb01128a1f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1027,6 +1027,10 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.arpa.inet.ntohl
     libc.src.arpa.inet.ntohs
 
+    # net/if.h entrypoints
+    libc.src.net.if_indextoname
+    libc.src.net.if_nametoindex
+
     # netinet/in.h entrypoints
     libc.src.netinet.in6addr_any
     libc.src.netinet.in6addr_loopback
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2508de5cfe5a4..1d12c763259df 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1160,6 +1160,10 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.arpa.inet.ntohl
     libc.src.arpa.inet.ntohs
 
+    # net/if.h entrypoints
+    libc.src.net.if_indextoname
+    libc.src.net.if_nametoindex
+
     # netinet/in.h entrypoints
     libc.src.netinet.in6addr_any
     libc.src.netinet.in6addr_loopback
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5ace968a739c8..ccf21f9085f8b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -41,6 +41,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
 
+    # net/if.h entrypoints
+    libc.src.net.if_indextoname
+    libc.src.net.if_nametoindex
+
     # netinet/in.h entrypoints
     libc.src.netinet.in6addr_any
     libc.src.netinet.in6addr_loopback
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index c24b826a6ef14..16e7f6d8afd4c 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -77,6 +77,15 @@ add_proxy_header_library(
     libc.include.fenv
 )
 
+add_proxy_header_library(
+  net_if_macros
+  HDRS
+    net_if_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.net_if
+    libc.include.llvm-libc-macros.net_if_macros
+)
+
 add_proxy_header_library(
   netinet_in_macros
   HDRS
diff --git a/libc/hdr/net_if_macros.h b/libc/hdr/net_if_macros.h
new file mode 100644
index 0000000000000..e87f486088dc4
--- /dev/null
+++ b/libc/hdr/net_if_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from net/if.h --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_NET_IF_MACROS_H
+#define LLVM_LIBC_HDR_NET_IF_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/net-if-macros.h"
+
+#else // Overlay mode
+
+#include <net/if.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_NET_IF_MACROS_H
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index a130f7ee0000a..49d8583db1204 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -509,6 +509,15 @@ add_proxy_header_library(
     libc.include.arpa_inet
 )
 
+add_proxy_header_library(
+  struct_ifreq
+  HDRS
+    struct_ifreq.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_ifreq
+    libc.include.net_if
+)
+
 add_proxy_header_library(
   struct_sockaddr
   HDRS
diff --git a/libc/hdr/types/struct_ifreq.h b/libc/hdr/types/struct_ifreq.h
new file mode 100644
index 0000000000000..3f34ffa4a9d7c
--- /dev/null
+++ b/libc/hdr/types/struct_ifreq.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy for struct ifreq.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_IFREQ_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_IFREQ_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_ifreq.h"
+
+#else
+
+#include <net/if.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_IFREQ_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index bb670b614742a..22b53a50850fa 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -219,6 +219,18 @@ add_header_macro(
     .llvm-libc-macros.inet_address_macros
 )
 
+file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/net)
+
+add_header_macro(
+  net_if
+  ../libc/include/net/if.yaml
+  net/if.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.net_if_macros
+    .llvm-libc-types.struct_ifreq
+)
+
 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/netinet)
 
 add_header_macro(
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 892f2a8b5b198..8f21d9811a160 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -314,6 +314,12 @@ add_macro_header(
     sys-socket-macros.h
 )
 
+add_macro_header(
+  net_if_macros
+  HDR
+    net-if-macros.h
+)
+
 add_macro_header(
   sys_time_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index c97270413197e..374e7e6159a5d 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -64,6 +64,12 @@ add_header(
     sys-socket-macros.h
 )
 
+add_header(
+  net_if_macros
+  HDR
+    net-if-macros.h
+)
+
 add_header(
   sys_wait_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/linux/net-if-macros.h b/libc/include/llvm-libc-macros/linux/net-if-macros.h
new file mode 100644
index 0000000000000..b5cd2c2ef144d
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/net-if-macros.h
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros defined in net/if.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_NET_IF_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_NET_IF_MACROS_H
+
+#define IF_NAMESIZE 16
+
+#endif // LLVM_LIBC_MACROS_LINUX_NET_IF_MACROS_H
diff --git a/libc/include/llvm-libc-macros/net-if-macros.h b/libc/include/llvm-libc-macros/net-if-macros.h
new file mode 100644
index 0000000000000..4af2b0bf401d5
--- /dev/null
+++ b/libc/include/llvm-libc-macros/net-if-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros defined in net/if.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_NET_IF_MACROS_H
+#define LLVM_LIBC_MACROS_NET_IF_MACROS_H
+
+#ifdef __linux__
+#include "linux/net-if-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_NET_IF_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 0512d3f0e642a..308523d5de55b 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -225,6 +225,7 @@ add_header(
 )
 add_header(struct_sockaddr_storage HDR struct_sockaddr_storage.h DEPENDS .sa_family_t)
 add_header(struct_sockaddr_un HDR struct_sockaddr_un.h DEPENDS .sa_family_t)
+add_header(struct_ifreq HDR struct_ifreq.h DEPENDS libc.include.llvm-libc-macros.net_if_macros .struct_sockaddr)
 add_header(struct_iovec HDR struct_iovec.h DEPENDS .size_t)
 add_header(struct_linger HDR struct_linger.h)
 add_header(struct_cmsghdr HDR struct_cmsghdr.h DEPENDS .size_t)
diff --git a/libc/include/llvm-libc-types/struct_ifreq.h b/libc/include/llvm-libc-types/struct_ifreq.h
new file mode 100644
index 0000000000000..b9ad182f97802
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_ifreq.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct ifreq.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_IFREQ_H
+#define LLVM_LIBC_TYPES_STRUCT_IFREQ_H
+
+#include "../llvm-libc-macros/net-if-macros.h"
+#include "struct_sockaddr.h"
+
+struct ifreq {
+  char ifr_name[IF_NAMESIZE];
+  __extension__ union {
+    struct sockaddr ifr_hwaddr;
+    struct sockaddr ifr_addr;
+    struct sockaddr ifr_dstaddr;
+    struct sockaddr ifr_broadaddr;
+    struct sockaddr ifr_netmask;
+    short int ifr_flags;
+    int ifr_metric;
+    int ifr_mtu;
+    int ifr_ifindex;
+    int ifr_bandwidth;
+    int ifr_qlen;
+    char ifr_newname[IF_NAMESIZE];
+    char ifr_slave[IF_NAMESIZE];
+    char *ifr_data;
+  };
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_IFREQ_H
diff --git a/libc/include/net/if.yaml b/libc/include/net/if.yaml
new file mode 100644
index 0000000000000..51034d84e4428
--- /dev/null
+++ b/libc/include/net/if.yaml
@@ -0,0 +1,24 @@
+header: net/if.h
+standards:
+  - posix
+macros:
+  - macro_name: IF_NAMESIZE
+    macro_header: net-if-macros.h
+types:
+  - type_name: struct_ifreq
+enums: []
+objects: []
+functions:
+  - name: if_indextoname
+    standards:
+      - posix
+    return_type: char *
+    arguments:
+      - type: unsigned int
+      - type: char *
+  - name: if_nametoindex
+    standards:
+      - posix
+    return_type: unsigned int
+    arguments:
+      - type: const char *
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 62aa164b35ee7..6ada1bef26a88 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -10,6 +10,7 @@ add_subdirectory(inttypes)
 add_subdirectory(libgen)
 add_subdirectory(link)
 add_subdirectory(math)
+add_subdirectory(net)
 add_subdirectory(netinet)
 add_subdirectory(stdbit)
 add_subdirectory(stdfix)
diff --git a/libc/src/net/CMakeLists.txt b/libc/src/net/CMakeLists.txt
new file mode 100644
index 0000000000000..2173a111cff4f
--- /dev/null
+++ b/libc/src/net/CMakeLists.txt
@@ -0,0 +1,17 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  if_indextoname
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.if_indextoname
+)
+
+add_entrypoint_object(
+  if_nametoindex
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.if_nametoindex
+)
diff --git a/libc/src/net/if_indextoname.h b/libc/src/net/if_indextoname.h
new file mode 100644
index 0000000000000..215535328fbee
--- /dev/null
+++ b/libc/src/net/if_indextoname.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for if_indextoname.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_NET_IF_INDEXTONAME_H
+#define LLVM_LIBC_SRC_NET_IF_INDEXTONAME_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+char *if_indextoname(unsigned int ifindex, char *ifname);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_NET_IF_INDEXTONAME_H
diff --git a/libc/src/net/if_nametoindex.h b/libc/src/net/if_nametoindex.h
new file mode 100644
index 0000000000000..c3b27986cc5e7
--- /dev/null
+++ b/libc/src/net/if_nametoindex.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for if_nametoindex.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_NET_IF_NAMETOINDEX_H
+#define LLVM_LIBC_SRC_NET_IF_NAMETOINDEX_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned int if_nametoindex(const char *ifname);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_NET_IF_NAMETOINDEX_H
diff --git a/libc/src/net/linux/CMakeLists.txt b/libc/src/net/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..9c82636d1c54c
--- /dev/null
+++ b/libc/src/net/linux/CMakeLists.txt
@@ -0,0 +1,44 @@
+add_entrypoint_object(
+  if_indextoname
+  SRCS
+    if_indextoname.cpp
+  HDRS
+    ../if_indextoname.h
+  DEPENDS
+    libc.hdr.net_if_macros
+    libc.hdr.errno_macros
+    libc.hdr.sys_ioctl_macros
+    libc.hdr.sys_socket_macros
+    libc.hdr.types.struct_ifreq
+    libc.src.__support.CPP.string_view
+    libc.src.__support.common
+    libc.src.__support.macros.null_check
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socket
+    libc.src.__support.OSUtil.linux.syscall_wrappers.ioctl
+    libc.src.__support.OSUtil.linux.syscall_wrappers.close
+    libc.src.errno.errno
+    libc.src.string.memory_utils.inline_memcpy
+    libc.src.string.string_utils
+)
+
+add_entrypoint_object(
+  if_nametoindex
+  SRCS
+    if_nametoindex.cpp
+  HDRS
+    ../if_nametoindex.h
+  DEPENDS
+    libc.hdr.net_if_macros
+    libc.hdr.errno_macros
+    libc.hdr.sys_ioctl_macros
+    libc.hdr.sys_socket_macros
+    libc.hdr.types.struct_ifreq
+    libc.src.__support.CPP.string_view
+    libc.src.__support.common
+    libc.src.__support.macros.null_check
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socket
+    libc.src.__support.OSUtil.linux.syscall_wrappers.ioctl
+    libc.src.__support.OSUtil.linux.syscall_wrappers.close
+    libc.src.errno.errno
+    libc.src.string.memory_utils.inline_memcpy
+)
diff --git a/libc/src/net/linux/if_indextoname.cpp b/libc/src/net/linux/if_indextoname.cpp
new file mode 100644
index 0000000000000..f046adc1ad220
--- /dev/null
+++ b/libc/src/net/linux/if_indextoname.cpp
@@ -0,0 +1,68 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of if_indextoname.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/net/if_indextoname.h"
+#include "hdr/errno_macros.h"
+#include "hdr/net_if_macros.h"
+#include "hdr/sys_ioctl_macros.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_ifreq.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/ioctl.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/socket.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/null_check.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/string/string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(char *, if_indextoname,
+                   (unsigned int ifindex, char *ifname)) {
+  LIBC_CRASH_ON_NULLPTR(ifname);
+
+  ErrorOr<int> fd =
+      linux_syscalls::socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+  if (!fd.has_value()) {
+    libc_errno = fd.error();
+    return nullptr;
+  }
+
+  struct ifreq ifr;
+  ifr.ifr_ifindex = static_cast<int>(ifindex);
+
+  ErrorOr<int> ioctl_res = linux_syscalls::ioctl(*fd, SIOCGIFNAME, &ifr);
+
+  linux_syscalls::close(*fd);
+
+  if (!ioctl_res.has_value()) {
+    // Map kernel ENODEV to POSIX-mandated ENXIO.
+    libc_errno = (ioctl_res.error() == ENODEV) ? ENXIO : ioctl_res.error();
+    return nullptr;
+  }
+
+  cpp::string_view k_name(
+      ifr.ifr_name, internal::strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)));
+  inline_memcpy(ifname, k_name.data(), k_name.size());
+  // Linux kernel does allow network devices exactly IF_NAMESIZE long (it leaves
+  // room for the terminating \0). But if that happens, let's not overrun the
+  // user provided buffer.
+  if (k_name.size() < IF_NAMESIZE)
+    ifname[k_name.size()] = '\0';
+
+  return ifname;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/net/linux/if_nametoindex.cpp b/libc/src/net/linux/if_nametoindex.cpp
new file mode 100644
index 0000000000000..507e7f59f1423
--- /dev/null
+++ b/libc/src/net/linux/if_nametoindex.cpp
@@ -0,0 +1,62 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of if_nametoindex.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/net/if_nametoindex.h"
+#include "hdr/errno_macros.h"
+#include "hdr/net_if_macros.h"
+#include "hdr/sys_ioctl_macros.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_ifreq.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/ioctl.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/socket.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/null_check.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(unsigned int, if_nametoindex, (const char *ifname)) {
+  LIBC_CRASH_ON_NULLPTR(ifname);
+
+  cpp::string_view name(ifname);
+  if (name.empty() || name.size() >= IF_NAMESIZE) {
+    libc_errno = ENODEV;
+    return 0;
+  }
+
+  ErrorOr<int> fd =
+      linux_syscalls::socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+  if (!fd.has_value()) {
+    libc_errno = fd.error();
+    return 0;
+  }
+
+  struct ifreq ifr;
+  inline_memcpy(ifr.ifr_name, name.data(), name.size() + 1);
+
+  ErrorOr<int> ioctl_res = linux_syscalls::ioctl(*fd, SIOCGIFINDEX, &ifr);
+
+  linux_syscalls::close(*fd);
+
+  if (!ioctl_res.has_value()) {
+    libc_errno = ioctl_res.error();
+    return 0;
+  }
+
+  return static_cast<unsigned int>(ifr.ifr_ifindex);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 45815c9bba8ca..077327d987dac 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -66,6 +66,7 @@ add_subdirectory(fenv)
 add_subdirectory(libgen)
 add_subdirectory(link)
 add_subdirectory(math)
+add_subdirectory(net)
 add_subdirectory(netinet)
 add_subdirectory(search)
 add_subdirectory(setjmp)
diff --git a/libc/test/src/net/CMakeLists.txt b/libc/test/src/net/CMakeLists.txt
new file mode 100644
index 0000000000000..8e285dd5b94c5
--- /dev/null
+++ b/libc/t...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/206082


More information about the libc-commits mailing list