Impl sockaddr_storage, structs in netinet.h

This commit is contained in:
Josh Megnauth
2024-12-03 21:45:47 +00:00
committed by Jeremy Soller
parent a64a6a4cd0
commit 6295ce73ea
3 changed files with 52 additions and 5 deletions
+12 -1
View File
@@ -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
+17 -1
View File
@@ -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],
+23 -3
View File
@@ -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::<sa_family_t>() - mem::size_of::<usize>();
/// 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::<size_t>() - 1) & !(mem::size_of::<size_t>() - 1)