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

Function unparse_address

crates/stdlib/src/overlapped.rs:387–413  ·  view source on GitHub ↗

Parse a SOCKADDR_IN6 (which can also hold IPv4 addresses) to a Python address tuple

(addr: &SOCKADDR_IN6, _addr_len: i32, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

385
386 /// Parse a SOCKADDR_IN6 (which can also hold IPv4 addresses) to a Python address tuple
387 fn unparse_address(addr: &SOCKADDR_IN6, _addr_len: i32, vm: &VirtualMachine) -> PyResult {
388 use core::net::{Ipv4Addr, Ipv6Addr};
389
390 unsafe {
391 let family = addr.sin6_family;
392 if family == AF_INET {
393 // IPv4 address stored in SOCKADDR_IN6 structure
394 let addr_in = &*(addr as *const SOCKADDR_IN6 as *const SOCKADDR_IN);
395 let ip_bytes = addr_in.sin_addr.S_un.S_un_b;
396 let ip_str =
397 Ipv4Addr::new(ip_bytes.s_b1, ip_bytes.s_b2, ip_bytes.s_b3, ip_bytes.s_b4)
398 .to_string();
399 let port = u16::from_be(addr_in.sin_port);
400 Ok((ip_str, port).to_pyobject(vm))
401 } else if family == AF_INET6 {
402 // IPv6 address
403 let ip_bytes = addr.sin6_addr.u.Byte;
404 let ip_str = Ipv6Addr::from(ip_bytes).to_string();
405 let port = u16::from_be(addr.sin6_port);
406 let flowinfo = u32::from_be(addr.sin6_flowinfo);
407 let scope_id = addr.Anonymous.sin6_scope_id;
408 Ok((ip_str, port, flowinfo, scope_id).to_pyobject(vm))
409 } else {
410 Err(vm.new_value_error("recvfrom returned unsupported address family"))
411 }
412 }
413 }
414
415 #[pyclass(with(Constructor, Destructor))]
416 impl Overlapped {

Callers 1

getresultMethod · 0.85

Calls 4

newFunction · 0.85
to_stringMethod · 0.80
ErrClass · 0.50
to_pyobjectMethod · 0.45

Tested by

no test coverage detected