Skip to content

Commit 060be56

Browse files
committed
Move setsockopt in libc_shims
1 parent a905b5f commit 060be56

3 files changed

Lines changed: 116 additions & 436 deletions

File tree

‎libcc2rs/src/libc_shims/socket.rs‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,104 @@ impl Sockaddr {
378378
out_len.write(ss.len());
379379
}
380380
}
381+
382+
pub fn setsockopt_refcount(fd: i32, level: i32, optname: i32, optval: crate::AnyPtr) -> i32 {
383+
let res = match (level, optname) {
384+
(::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => {
385+
let v = optval.reinterpret_cast::<i32>().read() != 0;
386+
crate::FdRegistry::with_fd(fd, |borrowed| {
387+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpNoDelay, &v)
388+
})
389+
}
390+
(::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => {
391+
let v = optval.reinterpret_cast::<i32>().read() != 0;
392+
crate::FdRegistry::with_fd(fd, |borrowed| {
393+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::KeepAlive, &v)
394+
})
395+
}
396+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => {
397+
let v = optval.reinterpret_cast::<u32>().read();
398+
crate::FdRegistry::with_fd(fd, |borrowed| {
399+
nix::sys::socket::setsockopt(
400+
&borrowed,
401+
nix::sys::socket::sockopt::TcpKeepInterval,
402+
&v,
403+
)
404+
})
405+
}
406+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => {
407+
let v = optval.reinterpret_cast::<u32>().read();
408+
crate::FdRegistry::with_fd(fd, |borrowed| {
409+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpKeepCount, &v)
410+
})
411+
}
412+
#[cfg(target_os = "linux")]
413+
(::libc::IPPROTO_IP, ::libc::IP_TOS) => {
414+
let v = optval.reinterpret_cast::<i32>().read();
415+
crate::FdRegistry::with_fd(fd, |borrowed| {
416+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Ipv4Tos, &v)
417+
})
418+
}
419+
#[cfg(target_os = "linux")]
420+
(::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => {
421+
let v = optval.reinterpret_cast::<i32>().read();
422+
crate::FdRegistry::with_fd(fd, |borrowed| {
423+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Ipv6TClass, &v)
424+
})
425+
}
426+
#[cfg(target_os = "linux")]
427+
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => {
428+
let v = optval.reinterpret_cast::<u32>().read();
429+
crate::FdRegistry::with_fd(fd, |borrowed| {
430+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::TcpKeepIdle, &v)
431+
})
432+
}
433+
#[cfg(target_os = "linux")]
434+
(::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => {
435+
let v = ::std::ffi::OsString::from(optval.reinterpret_cast::<u8>().to_rust_string());
436+
crate::FdRegistry::with_fd(fd, |borrowed| {
437+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::BindToDevice, &v)
438+
})
439+
}
440+
#[cfg(target_os = "linux")]
441+
(::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => {
442+
let v = optval.reinterpret_cast::<i32>().read() != 0;
443+
crate::FdRegistry::with_fd(fd, |borrowed| {
444+
nix::sys::socket::setsockopt(
445+
&borrowed,
446+
nix::sys::socket::sockopt::IpBindAddressNoPort,
447+
&v,
448+
)
449+
})
450+
}
451+
#[cfg(target_os = "linux")]
452+
(::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => {
453+
let v = optval.reinterpret_cast::<i32>().read() != 0;
454+
crate::FdRegistry::with_fd(fd, |borrowed| {
455+
nix::sys::socket::setsockopt(
456+
&borrowed,
457+
nix::sys::socket::sockopt::TcpFastOpenConnect,
458+
&v,
459+
)
460+
})
461+
}
462+
#[cfg(target_os = "linux")]
463+
(::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => {
464+
let v = optval.reinterpret_cast::<i32>().read();
465+
crate::FdRegistry::with_fd(fd, |borrowed| {
466+
nix::sys::socket::setsockopt(&borrowed, nix::sys::socket::sockopt::Priority, &v)
467+
})
468+
}
469+
(l, o) => panic!(
470+
"setsockopt: unsupported option (level={}, optname={})",
471+
l, o
472+
),
473+
};
474+
match res {
475+
Ok(()) => 0,
476+
Err(e) => {
477+
crate::cpp2rust_errno().write(e as i32);
478+
-1
479+
}
480+
}
481+
}

‎rules/socket/tgt_refcount.rs‎

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -220,104 +220,11 @@ fn f17(a0: i32, a1: i32) -> i32 {
220220
}
221221

222222
fn f7(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: u32) -> i32 {
223-
let __res = match (a1, a2) {
224-
(::libc::IPPROTO_TCP, ::libc::TCP_NODELAY) => {
225-
let __v = a3.reinterpret_cast::<i32>().read() != 0;
226-
FdRegistry::with_fd(a0, |__fd| {
227-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpNoDelay, &__v)
228-
})
229-
}
230-
(::libc::SOL_SOCKET, ::libc::SO_KEEPALIVE) => {
231-
let __v = a3.reinterpret_cast::<i32>().read() != 0;
232-
FdRegistry::with_fd(a0, |__fd| {
233-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::KeepAlive, &__v)
234-
})
235-
}
236-
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPINTVL) => {
237-
let __v = a3.reinterpret_cast::<u32>().read();
238-
FdRegistry::with_fd(a0, |__fd| {
239-
nix::sys::socket::setsockopt(
240-
&__fd,
241-
nix::sys::socket::sockopt::TcpKeepInterval,
242-
&__v,
243-
)
244-
})
245-
}
246-
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPCNT) => {
247-
let __v = a3.reinterpret_cast::<u32>().read();
248-
FdRegistry::with_fd(a0, |__fd| {
249-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepCount, &__v)
250-
})
251-
}
252-
#[cfg(target_os = "linux")]
253-
(::libc::IPPROTO_IP, ::libc::IP_TOS) => {
254-
let __v = a3.reinterpret_cast::<i32>().read();
255-
FdRegistry::with_fd(a0, |__fd| {
256-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv4Tos, &__v)
257-
})
258-
}
259-
#[cfg(target_os = "linux")]
260-
(::libc::IPPROTO_IPV6, ::libc::IPV6_TCLASS) => {
261-
let __v = a3.reinterpret_cast::<i32>().read();
262-
FdRegistry::with_fd(a0, |__fd| {
263-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Ipv6TClass, &__v)
264-
})
265-
}
266-
#[cfg(target_os = "linux")]
267-
(::libc::IPPROTO_TCP, ::libc::TCP_KEEPIDLE) => {
268-
let __v = a3.reinterpret_cast::<u32>().read();
269-
FdRegistry::with_fd(a0, |__fd| {
270-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::TcpKeepIdle, &__v)
271-
})
272-
}
273-
#[cfg(target_os = "linux")]
274-
(::libc::SOL_SOCKET, ::libc::SO_BINDTODEVICE) => {
275-
let __v = ::std::ffi::OsString::from(a3.reinterpret_cast::<u8>().to_rust_string());
276-
FdRegistry::with_fd(a0, |__fd| {
277-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::BindToDevice, &__v)
278-
})
279-
}
280-
#[cfg(target_os = "linux")]
281-
(::libc::IPPROTO_IP, ::libc::IP_BIND_ADDRESS_NO_PORT) => {
282-
let __v = a3.reinterpret_cast::<i32>().read() != 0;
283-
FdRegistry::with_fd(a0, |__fd| {
284-
nix::sys::socket::setsockopt(
285-
&__fd,
286-
nix::sys::socket::sockopt::IpBindAddressNoPort,
287-
&__v,
288-
)
289-
})
290-
}
291-
#[cfg(target_os = "linux")]
292-
(::libc::IPPROTO_TCP, ::libc::TCP_FASTOPEN_CONNECT) => {
293-
let __v = a3.reinterpret_cast::<i32>().read() != 0;
294-
FdRegistry::with_fd(a0, |__fd| {
295-
nix::sys::socket::setsockopt(
296-
&__fd,
297-
nix::sys::socket::sockopt::TcpFastOpenConnect,
298-
&__v,
299-
)
300-
})
301-
}
302-
#[cfg(target_os = "linux")]
303-
(::libc::SOL_SOCKET, ::libc::SO_PRIORITY) => {
304-
let __v = a3.reinterpret_cast::<i32>().read();
305-
FdRegistry::with_fd(a0, |__fd| {
306-
nix::sys::socket::setsockopt(&__fd, nix::sys::socket::sockopt::Priority, &__v)
307-
})
308-
}
309-
(__l, __o) => panic!(
310-
"setsockopt: unsupported option (level={}, optname={})",
311-
__l, __o
312-
),
313-
};
314-
match __res {
315-
Ok(()) => 0,
316-
Err(__e) => {
317-
libcc2rs::cpp2rust_errno().write(__e as i32);
318-
-1
319-
}
320-
}
223+
let __a0 = a0;
224+
let __a1 = a1;
225+
let __a2 = a2;
226+
let __a3 = a3;
227+
libcc2rs::setsockopt_refcount(__a0, __a1, __a2, __a3)
321228
}
322229

323230
fn f8(a0: i32, a1: i32, a2: i32, a3: AnyPtr, a4: Ptr<u32>) -> i32 {

0 commit comments

Comments
 (0)