8000 Add htons, ntohl, and ntohs to socket.rs · RustPython/RustPython@bc6ae61 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /react-partial>

Commit bc6ae61

Browse files
committed
Add htons, ntohl, and ntohs to socket.rs
1 parent a3293a7 commit bc6ae61

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

vm/src/stdlib/socket.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,32 @@ fn socket_htonl(host: u32, vm: &VirtualMachine) -> PyResult {
449449
Ok(vm.new_int(host.to_be()))
450450
}
451451

452+
fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult {
453+
Ok(vm.new_int(host.to_be()))
454+
}
455+
456+
fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult {
457+
Ok(vm.new_int(host.to_be()))
458+
}
459+
460+
fn socket_ntohl(network: u32, vm: &VirtualMachine) -> PyResult {
461+
if cfg!(target_endian = "big") {
462+
Ok(vm.new_int(network))
463+
}
464+
else {
465+
Ok(vm.new_int(network.to_le()))
466+
}
467+
}
468+
469+
fn socket_ntohs(network: u16, vm: &VirtualMachine) -> PyResult {
470+
if cfg!(target_endian = "big") {
471+
Ok(vm.new_int(network))
472+
}
473+
else {
474+
Ok(vm.new_int(network.to_le()))
475+
}
476+
}
477+
452478
#[derive(FromArgs)]
453479
struct GAIOptions {
454480
#[pyarg(positional_only)]
@@ -636,6 +662,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
636662
"inet_ntoa" => ctx.new_rustfunc(socket_inet_ntoa),
637663
"gethostname" => ctx.new_rustfunc(socket_gethostname),
638664
"htonl" => ctx.new_rustfunc(socket_htonl),
665+
"htons" => ctx.new_rustfunc(socket_htons),
666+
"ntohl" => ctx.new_rustfunc(socket_ntohl),
667+
"ntohs" => ctx.new_rustfunc(socket_ntohs),
639668
"getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()),
640669
"getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo),
641670
"gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr),

0 commit comments

Comments
 (0)
0