From 6295ce73ea3e694a86575580a747e26edeb2b244 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Tue, 3 Dec 2024 21:45:47 +0000 Subject: [PATCH] Impl `sockaddr_storage`, structs in `netinet.h` --- src/header/netinet_in/cbindgen.toml | 13 ++++++++++++- src/header/netinet_in/mod.rs | 18 +++++++++++++++++- src/header/sys_socket/mod.rs | 26 +++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/header/netinet_in/cbindgen.toml b/src/header/netinet_in/cbindgen.toml index fcc704f4bb..651700f05d 100644 --- a/src/header/netinet_in/cbindgen.toml +++ b/src/header/netinet_in/cbindgen.toml @@ -7,7 +7,18 @@ no_includes = true cpp_compat = true [export] -include = ["sockaddr_in6", "sockaddr_in", "ipv6_mreq", "ip_mreq", "ip_mreq_source"] +include = [ + "sockaddr_in6", + "sockaddr_in", + "ipv6_mreq", + "ip_mreq", + "ip_mreq_source", + "group_req", + "group_source_req", +] + +[export.rename] +"sockaddr_storage" = "struct sockaddr_storage" [enum] prefix_with_name = true diff --git a/src/header/netinet_in/mod.rs b/src/header/netinet_in/mod.rs index 721d7a7d72..4621b85b33 100644 --- a/src/header/netinet_in/mod.rs +++ b/src/header/netinet_in/mod.rs @@ -1,6 +1,9 @@ #![allow(non_camel_case_types)] -use crate::{header::sys_socket::sa_family_t, platform::types::*}; +use crate::{ + header::sys_socket::{sa_family_t, sockaddr_storage}, + platform::types::*, +}; pub type in_addr_t = u32; pub type in_port_t = u16; @@ -97,6 +100,19 @@ pub struct ip_mreq { pub imr_interface: in_addr, } +#[repr(C)] +pub struct group_req { + pub gr_interface: u32, + pub gr_group: sockaddr_storage, +} + +#[repr(C)] +pub struct group_source_req { + pub gsr_interface: u32, + pub gsr_group: sockaddr_storage, + pub gsr_source: sockaddr_storage, +} + #[no_mangle] pub static in6addr_any: in6_addr = in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], diff --git a/src/header/sys_socket/mod.rs b/src/header/sys_socket/mod.rs index 659eceac32..30b0e772a6 100644 --- a/src/header/sys_socket/mod.rs +++ b/src/header/sys_socket/mod.rs @@ -11,7 +11,7 @@ use crate::{ pub mod constants; pub type sa_family_t = u16; -pub type socklen_t = u32; +pub type socklen_t = size_t; #[repr(C)] #[derive(Default)] @@ -30,13 +30,13 @@ pub struct msghdr { pub msg_iov: *mut iovec, pub msg_iovlen: size_t, pub msg_control: *mut c_void, - pub msg_controllen: size_t, + pub msg_controllen: socklen_t, pub msg_flags: c_int, } #[repr(C)] pub struct cmsghdr { - pub cmsg_len: size_t, + pub cmsg_len: socklen_t, pub cmsg_level: c_int, pub cmsg_type: c_int, } @@ -48,6 +48,26 @@ pub struct sockaddr { pub sa_data: [c_char; 14], } +// Max size of [`sockaddr_storage`] +const _SS_MAXSIZE: usize = 128; +// Align to pointer width +const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::() - mem::size_of::(); + +/// Opaque storage large enough to hold any protocol specific address structure. +/// +/// ## Implementation notes +/// * The total size of this struct is 128 bytes which is based off of `musl` and `glibc` +/// * The underscore fields are implementation specific details for padding that may change +/// * [`usize`] is used because it's the width of a pointer for a given platform +/// * The order of the fields is important because the bytes in the padding will be cast to and +/// from protocol structs in C +#[repr(C)] +pub struct sockaddr_storage { + pub ss_family: sa_family_t, + __ss_pad2: [u8; _SS_PADDING], + __ss_align: usize, +} + #[no_mangle] pub unsafe extern "C" fn CMSG_ALIGN(len: size_t) -> size_t { (len + mem::size_of::() - 1) & !(mem::size_of::() - 1)