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

Function get_addr_tuple

crates/stdlib/src/socket.rs:2571–2633  ·  view source on GitHub ↗
(addr: &socket2::SockAddr, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

2569 }
2570
2571 fn get_addr_tuple(addr: &socket2::SockAddr, vm: &VirtualMachine) -> PyObjectRef {
2572 if let Some(addr) = addr.as_socket() {
2573 return get_ip_addr_tuple(&addr, vm);
2574 }
2575 #[cfg(unix)]
2576 if addr.is_unix() {
2577 use std::os::unix::ffi::OsStrExt;
2578 if let Some(abstractpath) = addr.as_abstract_namespace() {
2579 return vm.ctx.new_bytes([b"\0", abstractpath].concat()).into();
2580 }
2581 // necessary on macos
2582 let path = ffi::OsStr::as_bytes(addr.as_pathname().unwrap_or("".as_ref()).as_ref());
2583 let nul_pos = memchr::memchr(b'\0', path).unwrap_or(path.len());
2584 let path = ffi::OsStr::from_bytes(&path[..nul_pos]);
2585 return vm.fsdecode(path).into();
2586 }
2587 #[cfg(target_os = "linux")]
2588 {
2589 let family = addr.family();
2590 if family == libc::AF_CAN as libc::sa_family_t {
2591 // AF_CAN address: (interface_name,) or (interface_name, can_id)
2592 let can_addr = unsafe { &*(addr.as_ptr() as *const libc::sockaddr_can) };
2593 let ifindex = can_addr.can_ifindex;
2594 let ifname = if ifindex == 0 {
2595 String::new()
2596 } else {
2597 let mut buf = [0u8; libc::IF_NAMESIZE];
2598 let ret = unsafe {
2599 libc::if_indextoname(
2600 ifindex as libc::c_uint,
2601 buf.as_mut_ptr() as *mut libc::c_char,
2602 )
2603 };
2604 if ret.is_null() {
2605 String::new()
2606 } else {
2607 let nul_pos = memchr::memchr(b'\0', &buf).unwrap_or(buf.len());
2608 String::from_utf8_lossy(&buf[..nul_pos]).into_owned()
2609 }
2610 };
2611 return vm.ctx.new_tuple(vec![vm.ctx.new_str(ifname).into()]).into();
2612 }
2613 if family == libc::AF_ALG as libc::sa_family_t {
2614 // AF_ALG address: (type, name)
2615 let alg_addr = unsafe { &*(addr.as_ptr() as *const libc::sockaddr_alg) };
2616 let type_bytes = &alg_addr.salg_type;
2617 let name_bytes = &alg_addr.salg_name;
2618 let type_nul = memchr::memchr(b'\0', type_bytes).unwrap_or(type_bytes.len());
2619 let name_nul = memchr::memchr(b'\0', name_bytes).unwrap_or(name_bytes.len());
2620 let type_str = String::from_utf8_lossy(&type_bytes[..type_nul]).into_owned();
2621 let name_str = String::from_utf8_lossy(&name_bytes[..name_nul]).into_owned();
2622 return vm
2623 .ctx
2624 .new_tuple(vec![
2625 vm.ctx.new_str(type_str).into(),
2626 vm.ctx.new_str(name_str).into(),
2627 ])
2628 .into();

Callers 7

delMethod · 0.85
_acceptMethod · 0.85
recvfromMethod · 0.85
recvfrom_intoMethod · 0.85
recvmsgMethod · 0.85
getsocknameMethod · 0.85
getpeernameMethod · 0.85

Calls 14

get_ip_addr_tupleFunction · 0.85
newFunction · 0.85
if_indextonameFunction · 0.85
fsdecodeMethod · 0.80
as_mut_ptrMethod · 0.80
into_ownedMethod · 0.80
new_bytesMethod · 0.45
concatMethod · 0.45
as_refMethod · 0.45
lenMethod · 0.45
familyMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected