Skip to content

Commit c30fabd

Browse files
committed
add gethostbyname function
1 parent 6e796d7 commit c30fabd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

vm/src/stdlib/socket.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,23 @@ fn socket_gethostbyaddr(
579579
))
580580
}
581581

582+
#[cfg(not(target_os = "redox"))]
583+
fn socket_gethostbyname(name: PyStringRef, vm: &VirtualMachine) -> PyResult<String> {
584+
match socket_gethostbyaddr(name, vm) {
585+
Ok((_, _, hosts)) => {
586+
let lst = vm.extract_elements::<PyStringRef>(&hosts)?;
587+
Ok(lst.get(0).unwrap().to_string())
588+
}
589+
Err(_) => {
590+
let error_type = vm.class("_socket", "gaierror");
591+
Err(vm.new_exception_msg(
592+
error_type,
593+
"nodename nor servname provided, or not known".to_owned(),
594+
))
595+
}
596+
}
597+
}
598+
582599
fn get_addr<T, I>(vm: &VirtualMachine, addr: T) -> PyResult<socket2::SockAddr>
583600
where
584601
T: ToSocketAddrs<Iter = I>,
@@ -707,6 +724,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
707724
extend_module!(vm, module, {
708725
"getaddrinfo" => ctx.new_function(socket_getaddrinfo),
709726
"gethostbyaddr" => ctx.new_function(socket_gethostbyaddr),
727+
"gethostbyname" => ctx.new_function(socket_gethostbyname),
710728
});
711729

712730
extend_module_platform_specific(vm, &module);

0 commit comments

Comments
 (0)