diff --git a/include/bits/sys/socket.h b/include/bits/sys/socket.h index 95d9031232..b7a2e57705 100644 --- a/include/bits/sys/socket.h +++ b/include/bits/sys/socket.h @@ -13,4 +13,20 @@ struct ucred { gid_t gid; }; +// These definitions were taken from musl, license MIT { +#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) +#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) +#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) + +#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) +#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ + __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ + ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) +#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + +#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) +#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) +// } from musl, license MIT + #endif // _BITS_SYS_SOCKET_H diff --git a/src/header/sys_socket/mod.rs b/src/header/sys_socket/mod.rs index cf07bfe990..58f9e5912a 100644 --- a/src/header/sys_socket/mod.rs +++ b/src/header/sys_socket/mod.rs @@ -41,6 +41,9 @@ pub struct cmsghdr { pub cmsg_type: c_int, } +#[no_mangle] +pub extern "C" fn _cbindgen_export_cmsghdr(cmsghdr: cmsghdr) {} + #[repr(C)] #[derive(Default)] pub struct sockaddr { @@ -68,72 +71,6 @@ pub struct sockaddr_storage { __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) -} - -#[no_mangle] -pub unsafe extern "C" fn CMSG_LEN(length: c_uint) -> c_uint { - (CMSG_ALIGN(mem::size_of::()) + length as usize) as c_uint -} - -#[no_mangle] -pub unsafe extern "C" fn CMSG_SPACE(len: c_uint) -> c_uint { - (CMSG_ALIGN(len as size_t) + CMSG_ALIGN(mem::size_of::())) as c_uint -} - -#[no_mangle] -pub unsafe extern "C" fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr { - unsafe { - if (*mhdr).msg_controllen as usize >= mem::size_of::() { - (*mhdr).msg_control as *mut cmsghdr - } else { - 0 as *mut cmsghdr - } - } -} - -#[no_mangle] -pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr { - if cmsg.is_null() { - return CMSG_FIRSTHDR(mhdr); - }; - - unsafe { - let next = cmsg as usize - + CMSG_ALIGN((*cmsg).cmsg_len as usize) - + CMSG_ALIGN(mem::size_of::()); - let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize; - if next > max { - 0 as *mut cmsghdr - } else { - (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr - } - } -} - -#[no_mangle] -pub unsafe extern "C" fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar { - unsafe { (cmsg as *mut c_uchar).offset(CMSG_ALIGN(mem::size_of::()) as isize) } -} - -#[no_mangle] -pub fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar { - unsafe { (*mhdr).msg_control.offset((*mhdr).msg_controllen as isize) }.cast() -} - -#[no_mangle] -pub fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t { - ((unsafe { (*cmsg).cmsg_len as size_t } + mem::size_of::() - 1) - & !(mem::size_of::() - 1)) as ssize_t -} - -#[no_mangle] -pub fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar { - (unsafe { cmsg.offset(__CMSG_LEN(cmsg)) }) as *mut c_uchar -} - #[no_mangle] pub unsafe extern "C" fn accept( socket: c_int,