Add htons, ntohl, and ntohs to 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),
