From 2912d799d870d6c87292c6fc2c1ba148214195ce Mon Sep 17 00:00:00 2001 From: Marty Moradian Date: Mon, 6 Jan 2020 23:04:26 -0600 Subject: [PATCH 1/4] Add htons, ntohl, and ntohs to socket.rs --- vm/src/stdlib/socket.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index c6cbfc8a8..8a4120f69 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -449,6 +449,28 @@ fn socket_htonl(host: u32, vm: &VirtualMachine) -> PyResult { Ok(vm.new_int(host.to_be())) } +fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult { + Ok(vm.new_int(host.to_be())) +} + +fn socket_ntohl(network: u32, vm: &VirtualMachine) -> PyResult { + if cfg!(target_endian = "big") { + Ok(vm.new_int(network)) + } + else { + Ok(vm.new_int(network.to_le())) + } +} + +fn socket_ntohs(network: u16, vm: &VirtualMachine) -> PyResult { + if cfg!(target_endian = "big") { + Ok(vm.new_int(network)) + } + else { + Ok(vm.new_int(network.to_le())) + } +} + #[derive(FromArgs)] struct GAIOptions { #[pyarg(positional_only)] @@ -636,6 +658,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "inet_ntoa" => ctx.new_rustfunc(socket_inet_ntoa), "gethostname" => ctx.new_rustfunc(socket_gethostname), "htonl" => ctx.new_rustfunc(socket_htonl), + "htons" => ctx.new_rustfunc(socket_htons), + "ntohl" => ctx.new_rustfunc(socket_ntohl), + "ntohs" => ctx.new_rustfunc(socket_ntohs), "getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()), "getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo), "gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr), From 93e2eec2da9d925cd6bbd677d947749aa043b196 Mon Sep 17 00:00:00 2001 From: Marty Moradian Date: Tue, 7 Jan 2020 15:42:49 -0600 Subject: [PATCH 2/4] Made requested changes. (Combined short and long versions of hton and ntoh with generics, simplified their return values, replaced a chunk of code with from_be.) --- vm/src/stdlib/socket.rs | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 8a4120f69..224ed4c37 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -445,30 +445,12 @@ fn socket_inet_ntoa(packed_ip: PyBytesRef, vm: &VirtualMachine) -> PyResult { Ok(vm.new_str(Ipv4Addr::from(ip_num).to_string())) } -fn socket_htonl(host: u32, vm: &VirtualMachine) -> PyResult { - Ok(vm.new_int(host.to_be())) +fn socket_hton(host: U, _vm: &VirtualMachine) -> U { + host.to_be() } -fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult { - Ok(vm.new_int(host.to_be())) -} - -fn socket_ntohl(network: u32, vm: &VirtualMachine) -> PyResult { - if cfg!(target_endian = "big") { - Ok(vm.new_int(network)) - } - else { - Ok(vm.new_int(network.to_le())) - } -} - -fn socket_ntohs(network: u16, vm: &VirtualMachine) -> PyResult { - if cfg!(target_endian = "big") { - Ok(vm.new_int(network)) - } - else { - Ok(vm.new_int(network.to_le())) - } +fn socket_ntoh(network: U, _vm: &VirtualMachine) -> U { + network.from_be() } #[derive(FromArgs)] @@ -657,10 +639,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "inet_aton" => ctx.new_rustfunc(socket_inet_aton), "inet_ntoa" => ctx.new_rustfunc(socket_inet_ntoa), "gethostname" => ctx.new_rustfunc(socket_gethostname), - "htonl" => ctx.new_rustfunc(socket_htonl), - "htons" => ctx.new_rustfunc(socket_htons), - "ntohl" => ctx.new_rustfunc(socket_ntohl), - "ntohs" => ctx.new_rustfunc(socket_ntohs), + "htonl" => ctx.new_rustfunc(socket_hton::), + "htons" => ctx.new_rustfunc(socket_hton::), + "ntohl" => ctx.new_rustfunc(socket_ntoh::), + "ntohs" => ctx.new_rustfunc(socket_ntoh::), "getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()), "getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo), "gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr), From 43d135594ab51465465c969863adafd2634712ef Mon Sep 17 00:00:00 2001 From: Marty Moradian Date: Tue, 7 Jan 2020 15:54:05 -0600 Subject: [PATCH 3/4] Disambiguated method call for ntoh --- vm/src/stdlib/socket.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 224ed4c37..b1b32571e 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -445,12 +445,12 @@ fn socket_inet_ntoa(packed_ip: PyBytesRef, vm: &VirtualMachine) -> PyResult { Ok(vm.new_str(Ipv4Addr::from(ip_num).to_string())) } -fn socket_hton(host: U, _vm: &VirtualMachine) -> U { - host.to_be() +fn socket_hton(host: U, _vm: &VirtualMachine) -> U { + U::to_be(host) } -fn socket_ntoh(network: U, _vm: &VirtualMachine) -> U { - network.from_be() +fn socket_ntoh(network: U, _vm: &VirtualMachine) -> U { + U::from_be(network) } #[derive(FromArgs)] From a72109cec001edea31a86061c972d47e86134352 Mon Sep 17 00:00:00 2001 From: Marty Moradian Date: Tue, 7 Jan 2020 16:58:44 -0600 Subject: [PATCH 4/4] Formatting changes. --- vm/src/stdlib/socket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index b1b32571e..638dd2d80 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -450,7 +450,7 @@ fn socket_hton(host: U, _vm: &VirtualMachine) -> U } fn socket_ntoh(network: U, _vm: &VirtualMachine) -> U { - U::from_be(network) + U::from_be(network) } #[derive(FromArgs)]