summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tunnell-Jones <andrew@tj.id.au>2018-10-19 03:39:55 +0000
committerAndrew Tunnell-Jones <andrew@tj.id.au>2018-10-19 03:39:55 +0000
commit2701f620d29bcd45c3d61b6f52bc4b86d17d2a68 (patch)
tree210a630293967bd82ad2b2e8b0b075f97a269b72
parent814ce1ee957a5463024290c63c88784f646f2c84 (diff)
downloadrust-libc-2701f620d29bcd45c3d61b6f52bc4b86d17d2a68.tar.gz
Add CMSG macros for unix/bsd and unix/notbsd
-rw-r--r--src/unix/bsd/mod.rs37
-rw-r--r--src/unix/notbsd/mod.rs37
2 files changed, 74 insertions, 0 deletions
diff --git a/src/unix/bsd/mod.rs b/src/unix/bsd/mod.rs
index 98d91d046c..05cf8fee4d 100644
--- a/src/unix/bsd/mod.rs
+++ b/src/unix/bsd/mod.rs
@@ -332,6 +332,43 @@ pub const POLLRDBAND: ::c_short = 0x080;
pub const POLLWRBAND: ::c_short = 0x100;
f! {
+ pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
+ if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
+ (*mhdr).msg_control as *mut cmsghdr
+ } else {
+ 0 as *mut cmsghdr
+ }
+ }
+
+ pub fn CMSG_NXTHDR(mhdr: *const msghdr,
+ cmsg: *const cmsghdr) -> *mut cmsghdr {
+ if cmsg.is_null() {
+ return CMSG_FIRSTHDR(mhdr);
+ };
+ let pad = mem::align_of::<cmsghdr>() - 1;
+ let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
+ let max = (*mhdr).msg_control as usize
+ + (*mhdr).msg_controllen as usize;
+ if next < max {
+ next as *mut cmsghdr
+ } else {
+ 0 as *mut cmsghdr
+ }
+ }
+
+ pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut ::c_uchar {
+ cmsg.offset(1) as *mut ::c_uchar
+ }
+
+ pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
+ let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
+ mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
+ }
+
+ pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
+ mem::size_of::<cmsghdr>() as ::c_uint + length
+ }
+
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
let fd = fd as usize;
diff --git a/src/unix/notbsd/mod.rs b/src/unix/notbsd/mod.rs
index eead3fd847..6e4500684e 100644
--- a/src/unix/notbsd/mod.rs
+++ b/src/unix/notbsd/mod.rs
@@ -977,6 +977,43 @@ pub const ARPHRD_VOID: u16 = 0xFFFF;
pub const ARPHRD_NONE: u16 = 0xFFFE;
f! {
+ pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
+ if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
+ (*mhdr).msg_control as *mut cmsghdr
+ } else {
+ 0 as *mut cmsghdr
+ }
+ }
+
+ pub fn CMSG_NXTHDR(mhdr: *const msghdr,
+ cmsg: *const cmsghdr) -> *mut cmsghdr {
+ if cmsg.is_null() {
+ return CMSG_FIRSTHDR(mhdr);
+ };
+ let pad = mem::align_of::<cmsghdr>() - 1;
+ let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
+ let max = (*mhdr).msg_control as usize
+ + (*mhdr).msg_controllen as usize;
+ if next < max {
+ next as *mut cmsghdr
+ } else {
+ 0 as *mut cmsghdr
+ }
+ }
+
+ pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut ::c_uchar {
+ cmsg.offset(1) as *mut ::c_uchar
+ }
+
+ pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
+ let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
+ mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
+ }
+
+ pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
+ mem::size_of::<cmsghdr>() as ::c_uint + length
+ }
+
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
let fd = fd as usize;
let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;