summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-10-29 23:12:06 +0000
committerbors <bors@rust-lang.org>2018-10-29 23:12:06 +0000
commit04a13d1b979e12a999fcef99437eabc0e6c806c6 (patch)
treec3b1bd8172a58409ee472f15b05d0be3415af911
parent4a8304d47e844eeec8a4619b3d3e0f3bdd958521 (diff)
parent2701f620d29bcd45c3d61b6f52bc4b86d17d2a68 (diff)
downloadrust-libc-04a13d1b979e12a999fcef99437eabc0e6c806c6.tar.gz
Auto merge of #1098 - andrewtj:atj-cmsg-macros, r=alexcrichton
Add CMSG macros for unix/bsd and unix/notbsd This adds the ["Ancillary Data Object Macros" from RFC 2292](https://tools.ietf.org/html/rfc2292#section-4.3). My C's weak but I think I've translated them faithfully. Tested on macOS and Linux/glibc.
-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;