From 55e618d8c08c2a8e7f873dbe558e5d1570bccf42 Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Wed, 11 Jul 2018 12:49:50 +0200
Subject: [PATCH] Add the last socket functions on linux; leave unimplemented
 on redox

---
 src/platform/src/linux/mod.rs | 41 +++++++++++++++++++++++++++++--
 src/platform/src/redox/mod.rs | 46 +++++++++++++++++++++++++++++++++++
 src/sys_socket/src/lib.rs     | 14 +++++------
 3 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs
index 6121d939..ad20aca4 100644
--- a/src/platform/src/linux/mod.rs
+++ b/src/platform/src/linux/mod.rs
@@ -124,7 +124,7 @@ pub fn getgid() -> gid_t {
 }
 
 pub unsafe fn getpeername(socket: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int {
-    e(unsafe { syscall!(GETPEERNAME, socket, address, address_len) }) as c_int
+    e(syscall!(GETPEERNAME, socket, address, address_len)) as c_int
 }
 
 pub fn getpgid(pid: pid_t) -> pid_t {
@@ -140,7 +140,17 @@ pub fn getppid() -> pid_t {
 }
 
 pub unsafe fn getsockname(socket: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int {
-    e(unsafe { syscall!(GETSOCKNAME, socket, address, address_len) }) as c_int
+    e(syscall!(GETSOCKNAME, socket, address, address_len)) as c_int
+}
+
+pub fn getsockopt(
+    socket: c_int,
+    level: c_int,
+    option_name: c_int,
+    option_value: *mut c_void,
+    option_len: *mut socklen_t,
+) -> c_int {
+    e(unsafe { syscall!(GETSOCKOPT, socket, level, option_name, option_value, option_len) }) as c_int
 }
 
 pub fn getuid() -> uid_t {
@@ -159,6 +169,10 @@ pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
     e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, AT_FDCWD, path2, 0) }) as c_int
 }
 
+pub fn listen(socket: c_int, backlog: c_int) -> c_int {
+    e(unsafe { syscall!(LISTEN, socket, backlog) }) as c_int
+}
+
 pub fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
     e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
 }
@@ -243,6 +257,20 @@ pub fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
     e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
 }
 
+pub fn setsockopt(
+    socket: c_int,
+    level: c_int,
+    option_name: c_int,
+    option_value: *const c_void,
+    option_len: socklen_t,
+) -> c_int {
+    e(unsafe { syscall!(SETSOCKOPT, socket, level, option_name, option_value, option_len) }) as c_int
+}
+
+pub fn shutdown(socket: c_int, how: c_int) -> c_int {
+    e(unsafe { syscall!(SHUTDOWN, socket, how) }) as c_int
+}
+
 pub fn stat(file: *const c_char, buf: *mut stat) -> c_int {
     e(unsafe { syscall!(NEWFSTATAT, AT_FDCWD, file, buf, 0) }) as c_int
 }
@@ -251,6 +279,15 @@ pub fn socket(domain: c_int, kind: c_int, protocol: c_int) -> c_int {
     e(unsafe { syscall!(SOCKET, domain, kind, protocol) }) as c_int
 }
 
+pub fn socketpair(
+    domain: c_int,
+    kind: c_int,
+    protocol: c_int,
+    socket_vector: *mut c_int,
+) -> c_int {
+    e(unsafe { syscall!(SOCKETPAIR, domain, kind, protocol, socket_vector) }) as c_int
+}
+
 pub fn uname(utsname: usize) -> c_int {
     e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
 }
diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs
index 0ba0697b..ee04bff2 100644
--- a/src/platform/src/redox/mod.rs
+++ b/src/platform/src/redox/mod.rs
@@ -1,5 +1,6 @@
 //! sys/socket implementation, following http://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/socket.h.html
 
+use core::fmt::Write;
 use core::mem;
 use core::ptr;
 use core::slice;
@@ -331,6 +332,18 @@ pub unsafe fn getsockname(socket: c_int, address: *mut sockaddr, address_len: *m
     e(inner_get_name(true, socket, address, address_len)) as c_int
 }
 
+pub fn getsockopt(
+    socket: c_int,
+    level: c_int,
+    option_name: c_int,
+    option_value: *mut c_void,
+    option_len: *mut socklen_t,
+) -> c_int {
+    let _ = write!(::FileWriter(2), "unimplemented: getsockopt({}, {}, {}, {:p}, {:p})",
+        socket, level, option_name, option_value, option_len);
+    -1
+}
+
 pub fn getuid() -> uid_t {
     e(syscall::getuid()) as pid_t
 }
@@ -349,6 +362,11 @@ pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
     e(unsafe { syscall::link(path1.as_ptr(), path2.as_ptr()) }) as c_int
 }
 
+pub fn listen(socket: c_int, backlog: c_int) -> c_int {
+    // TODO
+    0
+}
+
 pub fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t {
     e(syscall::lseek(
         fd as usize,
@@ -499,6 +517,23 @@ pub fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
     e(syscall::setreuid(ruid as usize, euid as usize)) as c_int
 }
 
+pub fn setsockopt(
+    socket: c_int,
+    level: c_int,
+    option_name: c_int,
+    option_value: *const c_void,
+    option_len: socklen_t,
+) -> c_int {
+    let _ = write!(::FileWriter(2), "unimplemented: setsockopt({}, {}, {}, {:p}, {})",
+        socket, level, option_name, option_value, option_len);
+    -1
+}
+
+pub fn shutdown(socket: c_int, how: c_int) -> c_int {
+    let _ = write!(::FileWriter(2), "unimplemented: shutdown({}, {})", socket, how);
+    -1
+}
+
 pub fn stat(path: *const c_char, buf: *mut stat) -> c_int {
     let path = unsafe { c_str(path) };
     match syscall::open(path, O_RDONLY) {
@@ -543,6 +578,17 @@ pub unsafe fn socket(domain: c_int, mut kind: c_int, protocol: c_int) -> c_int {
     }
 }
 
+pub fn socketpair(
+    domain: c_int,
+    kind: c_int,
+    protocol: c_int,
+    socket_vector: *mut c_int,
+) -> c_int {
+    let _ = write!(::FileWriter(2), "unimplemented: socketpair({}, {}, {}, {:p})",
+        domain, kind, protocol, socket_vector);
+    -1
+}
+
 pub fn unlink(path: *const c_char) -> c_int {
     let path = unsafe { c_str(path) };
     e(syscall::unlink(path)) as c_int
diff --git a/src/sys_socket/src/lib.rs b/src/sys_socket/src/lib.rs
index c360c972..b8fa277e 100644
--- a/src/sys_socket/src/lib.rs
+++ b/src/sys_socket/src/lib.rs
@@ -75,12 +75,12 @@ pub unsafe extern "C" fn getsockopt(
     option_value: *mut c_void,
     option_len: *mut socklen_t,
 ) -> c_int {
-    unimplemented!();
+    platform::getsockopt(socket, level, option_name, option_value, option_len)
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn listen(socket: c_int, backlog: c_int) -> c_int {
-    unimplemented!();
+    platform::listen(socket, backlog)
 }
 
 #[no_mangle]
@@ -156,12 +156,12 @@ pub unsafe extern "C" fn setsockopt(
     option_value: *const c_void,
     option_len: socklen_t,
 ) -> c_int {
-    unimplemented!();
+    platform::setsockopt(socket, level, option_name, option_value, option_len)
 }
 
 #[no_mangle]
 pub unsafe extern "C" fn shutdown(socket: c_int, how: c_int) -> c_int {
-    unimplemented!();
+    platform::shutdown(socket, how)
 }
 
 #[no_mangle]
@@ -172,9 +172,9 @@ pub unsafe extern "C" fn socket(domain: c_int, kind: c_int, protocol: c_int) ->
 #[no_mangle]
 pub unsafe extern "C" fn socketpair(
     domain: c_int,
-    _type: c_int,
+    kind: c_int,
     protocol: c_int,
-    socket_vector: [c_int; 2],
+    socket_vector: *mut c_int,
 ) -> c_int {
-    unimplemented!();
+    platform::socketpair(domain, kind, protocol, socket_vector)
 }
-- 
GitLab