Skip to content

Commit 435d364

Browse files
authored
Merge pull request RustPython#2188 from hyperbora/extend-the-socket-module
implement socket.getprotobyname
2 parents a1a8d03 + 3ff0647 commit 435d364

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

vm/src/stdlib/socket.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,18 @@ fn socket_inet_ntop(af_inet: i32, packed_ip: PyBytesRef, vm: &VirtualMachine) ->
646646
}
647647
}
648648

649+
fn socket_getprotobyname(name: PyStringRef, vm: &VirtualMachine) -> PyResult {
650+
use std::ffi::CString;
651+
let cstr = CString::new(name.borrow_value())
652+
.map_err(|_| vm.new_value_error("embedded null character".to_owned()))?;
653+
let proto = unsafe { c::getprotobyname(cstr.as_ptr()) };
654+
if proto.is_null() {
655+
return Err(vm.new_os_error("protocol not found".to_owned()));
656+
}
657+
let num = unsafe { (*proto).p_proto };
658+
Ok(vm.ctx.new_int(num))
659+
}
660+
649661
fn get_addr<T, I>(vm: &VirtualMachine, addr: T) -> PyResult<socket2::SockAddr>
650662
where
651663
T: ToSocketAddrs<Iter = I>,
@@ -760,6 +772,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
760772
"has_ipv6" => ctx.new_bool(false),
761773
"inet_pton" => ctx.new_function(socket_inet_pton),
762774
"inet_ntop" => ctx.new_function(socket_inet_ntop),
775+
"getprotobyname" => ctx.new_function(socket_getprotobyname),
763776
// constants
764777
"AF_UNSPEC" => ctx.new_int(0),
765778
"AF_INET" => ctx.new_int(c::AF_INET),

0 commit comments

Comments
 (0)