aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-08-13 13:42:33 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-08-13 13:42:33 +0200
commit58f40bb5cbb40b33f57532cebafce83c651f2d6b (patch)
treea50958e9852c4c316bc6ef6a4dfd6344fe0f910a /src/util.rs
parentd18e158b1134eb5c712005555f1f86698ac9f2f1 (diff)
initial solicitor
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..245f755
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,33 @@
+use std::ffi::{c_char, c_int};
+use std::io;
+
+/// Helper macro to execute a system call that returns an `io::Result`.
+macro_rules! syscall {
+ ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
+ #[allow(unused_unsafe)]
+ let res = unsafe { libc::$fn($($arg, )*) };
+ if res == -1 {
+ Err(io::Error::last_os_error())
+ } else {
+ Ok(res)
+ }
+ }};
+}
+
+#[allow(clippy::missing_safety_doc)]
+pub unsafe fn setsockopt(
+ fd: c_int,
+ opt: c_int,
+ val: c_int,
+ payload: *const c_char,
+ optlen: c_int,
+) -> io::Result<()> {
+ syscall!(setsockopt(
+ fd,
+ opt,
+ val,
+ payload.cast(),
+ optlen as libc::socklen_t
+ ))
+ .map(|_| ())
+}