MCPcopy Index your code
hub / github.com/RustPython/RustPython / if_nameindex

Function if_nameindex

crates/stdlib/src/socket.rs:3123–3196  ·  view source on GitHub ↗
(vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

3121 ))]
3122 #[pyfunction]
3123 fn if_nameindex(vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
3124 #[cfg(not(windows))]
3125 {
3126 let list = nix::net::if_::if_nameindex()
3127 .map_err(|err| err.into_pyexception(vm))?
3128 .to_slice()
3129 .iter()
3130 .map(|iface| {
3131 let tup: (u32, String) =
3132 (iface.index(), iface.name().to_string_lossy().into_owned());
3133 tup.to_pyobject(vm)
3134 })
3135 .collect();
3136
3137 Ok(list)
3138 }
3139 #[cfg(windows)]
3140 {
3141 use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH;
3142
3143 let table = MibTable::get_raw().map_err(|err| err.into_pyexception(vm))?;
3144 let list = table.as_slice().iter().map(|entry| {
3145 let name =
3146 get_name(&entry.InterfaceLuid).map_err(|err| err.into_pyexception(vm))?;
3147 let tup = (entry.InterfaceIndex, name.to_string_lossy());
3148 Ok(tup.to_pyobject(vm))
3149 });
3150 let list = list.collect::<PyResult<_>>()?;
3151 return Ok(list);
3152
3153 fn get_name(luid: &NET_LUID_LH) -> io::Result<widestring::WideCString> {
3154 let mut buf = [0; c::IF_NAMESIZE + 1];
3155 let ret = unsafe {
3156 IpHelper::ConvertInterfaceLuidToNameW(luid, buf.as_mut_ptr(), buf.len())
3157 };
3158 if ret == 0 {
3159 Ok(widestring::WideCString::from_ustr_truncate(
3160 widestring::WideStr::from_slice(&buf[..]),
3161 ))
3162 } else {
3163 Err(io::Error::from_raw_os_error(ret as i32))
3164 }
3165 }
3166 struct MibTable {
3167 ptr: core::ptr::NonNull<IpHelper::MIB_IF_TABLE2>,
3168 }
3169 impl MibTable {
3170 fn get_raw() -> io::Result<Self> {
3171 let mut ptr = core::ptr::null_mut();
3172 let ret = unsafe { IpHelper::GetIfTable2Ex(IpHelper::MibIfTableRaw, &mut ptr) };
3173 if ret == 0 {
3174 let ptr = unsafe { core::ptr::NonNull::new_unchecked(ptr) };
3175 Ok(Self { ptr })
3176 } else {
3177 Err(io::Error::from_raw_os_error(ret as i32))
3178 }
3179 }
3180 }

Callers

nothing calls this directly

Calls 11

collectMethod · 0.80
into_ownedMethod · 0.80
get_nameFunction · 0.70
mapMethod · 0.45
iterMethod · 0.45
into_pyexceptionMethod · 0.45
indexMethod · 0.45
to_string_lossyMethod · 0.45
nameMethod · 0.45
to_pyobjectMethod · 0.45
as_sliceMethod · 0.45

Tested by

no test coverage detected