@@ -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+ }
0 commit comments