Skip to content

Commit 97f9b7a

Browse files
committed
implement socket.getnameinfo and add constants
1 parent e4cdb14 commit 97f9b7a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

vm/src/stdlib/socket.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,24 @@ fn socket_getprotobyname(name: PyStringRef, vm: &VirtualMachine) -> PyResult {
658658
Ok(vm.ctx.new_int(num))
659659
}
660660

661+
fn socket_getnameinfo(
662+
address: Address,
663+
flags: i32,
664+
vm: &VirtualMachine,
665+
) -> PyResult<(String, String)> {
666+
let addr = get_addr(vm, address)?;
667+
let nameinfo = addr
668+
.as_std()
669+
.and_then(|addr| dns_lookup::getnameinfo(&addr, flags).ok());
670+
nameinfo.ok_or_else(|| {
671+
let error_type = vm.class("_socket", "gaierror");
672+
vm.new_exception_msg(
673+
error_type,
674+
"nodename nor servname provided, or not known".to_owned(),
675+
)
676+
})
677+
}
678+
661679
fn get_addr<T, I>(vm: &VirtualMachine, addr: T) -> PyResult<socket2::SockAddr>
662680
where
663681
T: ToSocketAddrs<Iter = I>,
@@ -773,6 +791,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
773791
"inet_pton" => ctx.new_function(socket_inet_pton),
774792
"inet_ntop" => ctx.new_function(socket_inet_ntop),
775793
"getprotobyname" => ctx.new_function(socket_getprotobyname),
794+
"getnameinfo" => ctx.new_function(socket_getnameinfo),
776795
// constants
777796
"AF_UNSPEC" => ctx.new_int(0),
778797
"AF_INET" => ctx.new_int(c::AF_INET),
@@ -797,6 +816,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
797816
"TCP_NODELAY" => ctx.new_int(c::TCP_NODELAY),
798817
"AI_ALL" => ctx.new_int(c::AI_ALL),
799818
"AI_PASSIVE" => ctx.new_int(c::AI_PASSIVE),
819+
"NI_NAMEREQD" => ctx.new_int(c::NI_NAMEREQD),
820+
"NI_NOFQDN" => ctx.new_int(c::NI_NOFQDN),
821+
"NI_NUMERICHOST" => ctx.new_int(c::NI_NUMERICHOST),
822+
"NI_NUMERICSERV" => ctx.new_int(c::NI_NUMERICSERV),
800823
});
801824

802825
#[cfg(not(target_os = "redox"))]

0 commit comments

Comments
 (0)